423,846 Members | 1,959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,846 IT Pros & Developers. It's quick & easy.

Calculating mathematical expressions in C with getchar()

P: 2
I want to create a program in C language that calculates mathematical expressions like 23+45*8 and returns the result of it.
I have already tried to solve this problem but the program I made is working only for two numbers and I can do only one operation each time.
How can I make this program work for many numbers and many operations?
Any ideas or suggestions would be helpful.
Here is also the code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main(){
  4.     int ch;
  5.     int num = 0,temp=0;
  6.     char op;
  7.     while ((ch = getchar())!= EOF){
  8.         if (ch != '\n'){
  9.         if (ch >= '0' && ch <='9'){ 
  10.             num = num*10 + ch- '0'; 
  11.             }else {
  12.                 op = ch;
  13.             temp = num;
  14.             num = 0;
  15.            }
  16.         }
  17.     int result =0;
  18.     switch(op){
  19.         case '+':
  20.             result = temp + num;
  21.                break;
  22.         case '-':
  23.                result = temp - num;
  24.             break;
  25.         case '*':
  26.             result = temp * num;
  27.                break;
  28.         case '/':
  29.               result = temp / num;
  30.                break;
  31.     }
  32.     if (ch == '\n'){
  33.         printf("%d\n",result); 
  34.         num = 0;
  35.         temp = 0;
  36.         }
  37.  
  38.     }
  39. }
  40.  
1 Week Ago #1

✓ answered by weaknessforcats

This is usually done with two stacks. One for the values and one for the operation.


Let's assume two stacks: Value and Operation.

So 3 + 4 + 5 is parsed to

3 push to Value
+ push to operation

4 push to value

+ push to operation


5 push to value.

So the stacks look like

Value
5
4
3

Operation

+
+

To solve, pop the operation. It's a +.

Next, pop value twice for the numbers 5 and 4.

+ is an addition so add 5 and 4 and push the result back onto Value.

Value now looks like:

9
3

Pop the operation for the second plus.


It's another add so pop Value twice for the numbers 9 and 3.

Add 9 and 3 and push the 12 back onto the Value stack.

Now pop the operation again and we see that it's empty. That means the final result is on top of the Value stack.


Pop the 12 as your answer.

This is just a teaser. A little research here will give you ideas on handle parenthetical expressions and all sorts of things.

Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,196
This is usually done with two stacks. One for the values and one for the operation.


Let's assume two stacks: Value and Operation.

So 3 + 4 + 5 is parsed to

3 push to Value
+ push to operation

4 push to value

+ push to operation


5 push to value.

So the stacks look like

Value
5
4
3

Operation

+
+

To solve, pop the operation. It's a +.

Next, pop value twice for the numbers 5 and 4.

+ is an addition so add 5 and 4 and push the result back onto Value.

Value now looks like:

9
3

Pop the operation for the second plus.


It's another add so pop Value twice for the numbers 9 and 3.

Add 9 and 3 and push the 12 back onto the Value stack.

Now pop the operation again and we see that it's empty. That means the final result is on top of the Value stack.


Pop the 12 as your answer.

This is just a teaser. A little research here will give you ideas on handle parenthetical expressions and all sorts of things.
1 Week Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.