# Thread: Help on a program that solves math equations!

1. ## Help on a program that solves math equations!

I wrote a program that solves an equation of two numbers, but in addition to that, I want it to be able to continue to solve longer equations. Ex: ( solves 2 * 4, or 2 * 4 - 5).
I want to put this part of the program's result into a variable and go from there. How do I place the result of the calculation into a variable, and where would it go?
Code:
```#include <stdio.h>
#include <string.h>

int main(void) {
int a;
scanf("%d", &a);
char s;
scanf("%s", s);
int b;
scanf("%d", &b);

if (strcmp(s, "+") == 0) {
printf("%d", a + b);
return 1;
}
if (strcmp(s, "-") == 0) {
printf("%d", a - b);
return 1;
}
if (strcmp(s, "*") == 0) {
printf("%d", a * b);
return 1;
}
if (strcmp(s, "/") == 0) {
printf("%d", a / b);
return 1;
}
return 0;
}``` 2. read the equation in as a string and have your program solve it in steps starting with the ')' that has priority 3. parsing arbitrary length expressions with proper precedence rules is quite a bit more complicated than comparing strings. but as a start, if you want to skip the operator precedence and just evaluate expressions left to right that are of the form <term> <op> <term> op <term> ....
do something like this

[code]
read number and store it in variable 'result'
loop
read next operator. if EOF, you are done
read number and store it in variable 'val'. if EOF error
execute the operation result <op> val, store in result
end loop 4. thanks for breaking it down a bit, dmh. i think i've got it now  5. Go to my website and look around the MiniBasic pages. There's also a book.

It explains how to set about the problem of reading arbitrarily complex equations. Popular pages Recent additions 