-4

For school, I'm currently learning C. Coming from Javascript, I'm trying to adapt to the way C is written. The syntax can be a bit confusing. I checked and there are a lot of obsolete functions my teacher used for examples.

My question is based on expression matching.

So let's assume I have a math question like this:

If Hanna has 100 apples and John 32 apples, then what is the sum?

How would I tell the program to interpret the sentence as 1+2 and then output the result?

In javascript, I usually use regular expressions but in C, this is really hard to accomplish or understand since regex is not a standard feature.

First: printf(A operation B)

I would need to iterate over each letter of the string and check if any of the letters include 0-9. If match is found then the first number is stored in variable A and the second number in Variable B. If sum is found, then it stores + in the operation variable. As a consequence the variables within printf become assigned and I get the result.

Now how do I do this technically in C?

donjuedo
  • 2,475
  • 18
  • 28
Asperger
  • 3,064
  • 8
  • 52
  • 100
  • What kind of `obsolete` functions does your teacher use? – Zorgatone Dec 08 '15 at 19:24
  • 1
    In C you are likely to use regex libraries rather than trying to roll your own, especially if you are a beginner in the language. Without using the full power of regular expressions, It wouldn't be hard to scan a string to extract numbers and check for key words such as `sum`, though that seems like a pretty fragile way to parse it. `strtok()` is the closest to a readily-available low-tech solution. – John Coleman Dec 08 '15 at 19:26
  • I think for him it's too early to use regex in C, learn the language before that – Zorgatone Dec 08 '15 at 19:28
  • 2
    This looks like a job for `strtok` as @johnColeman pointed out. I suspect that's the point of the assignment. If it is, @asperger, you may be interested to know that this is not obsolete at all but sits at the heart of many if not all compilers written in (or produced by a compiler compiler) in C. – David Hoelzer Dec 08 '15 at 19:30

2 Answers2

1

I assume the following: - Only one line has to be read. - There are only two numbers in the line. - The type of operation is indicated by a known set of words. For example, "sum", "multiplication" and so on.

The pseudocode is:

  • Read the whole line and store it in a string (in C, a variable whose type is char *)
  • Read first number, "A".
  • Read first number, "B".
  • Convert the strings into numbers. You can use the standard function "atoi".
  • Do the operation (sum, multiplication, etc.)

For calculating the result, in C you have to use conditional blocks. Something like that:

char * operator;

if (strcmp("sum", operator) == 0)
  result = A + B;
else if (strcmp("multiplication", operator) == 0)
  result = A * B;
..............
..............

printf("\nThe result is %d\n", sum);
Jose4Linux
  • 144
  • 7
1

NO, atoi is not obsolete.

However, strtol should be used if you are dealing with very big numbers, you are using a base other than 10 (non-decimal values) or you need to perform error-checking.

num = atoi(str);

is equivalent to

strtol(str, NULL, 10);

for most of the cases

Lundin, a very knowledgable C programmer, says in atoi() — string to int, that

"atoi is not deprecated, your source is incorrect. Nothing in the current C standard ISO 9899:2011 indicates this (see for example chapter 6.11 future language directions), nor anything in earlier standards."

For the sake of completeness, I recommend you keep on reading his answer, although it's not the accepted answer, but I do think it's the most comprehensive one.

You will find the atoi function in many situations, but it's good to know about strtol, as well.

Community
  • 1
  • 1
Jose4Linux
  • 144
  • 7