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:
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:
Code:
#include <stdio.h>
int main(){
int ch;
int num = 0,temp=0;
char op;
while ((ch = getchar())!= EOF){
if (ch != '\n'){
if (ch >= '0' && ch <='9'){
num = num*10 + ch- '0';
}else {
op = ch;
temp = num;
num = 0;
}
}
int result =0;
switch(op){
case '+':
result = temp + num;
break;
case '-':
result = temp - num;
break;
case '*':
result = temp * num;
break;
case '/':
result = temp / num;
break;
}
if (ch == '\n'){
printf("%d\n",result);
num = 0;
temp = 0;
}
}
}
Comment