Performing Operations on User-Input Formulas

I'm attempting to make a program that will allow you to input a formula, input variables, and it will solve the formula. The problem I'm facing is that I don't know how to go about performing the arithmetic on the formula.

1
2
3
4
5
6
7
8
9
cout << "How many variables are there in your formula?\n";
cin >> scope;
cin.ignore("\n");
cout << "Type out the formula with spaces between operators, variables, and constants, but exponents are not allowed and square roots should be written as shown.\n
ONE VARIABLE MUST BE A, ANOTHER B, THEN C, ETC. WITHOUT SKIPPING ANY LETTERS!!!!\n
Example: a * d * d + b * d + c   is the quadratic equation.\n
Example: (0 - b + sqrt(b * b - 4 * a * c)) / (2 * a)   is HALF of (plus OR minus) the quadratic formula.\n";
cin >> formula;
cin.ignore("\n");}

P.S. I know there's pow() but this is for people who don't understand how to write in C++.

After having them define the variables, how would I go about performing the operations? Is there a way to empty the content of the [string formula] into the program so that it can perform the operations? Or is there a different way of going about this so that it will perform each operation seperately, without taking too much time for the user to do?

Any help or criticism is appreciated, and on a side note, is there a better way to get the variables (I'm sure there is) than this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
loop = 0;
while(loop < scope) { //scope being the number of variables in the formula, as input by the user
cout << "Define variable A.\n";
cin >> a;
cin.ignore("\n");
loop++;
cout << "Define variable B.\n";
cin >> b;
cin.ignore("\n");
loop++;
.................
cin >> z;
cin.ignore("\n");
loop++;}


It limits the user to the first letters of the alphabet, but it's the best way I could think of to define variables in the formula.


EDIT: I just realized the rules of local variables; looks like I need to declare ALL the variable variables no matter what. Hopefully that's not that much more consuming than it needs to be.
Last edited on
I found this the other day, you might look at it for ideas

http://www.codeproject.com/Tips/381509/Math-Parser-NET-Csharp
Thanks for the help. I've not really ever delved into other C-based languages like C#, so I don't know if the syntax and the commands will work in C++, but that's better than anything I've been able to find.

And if anyone else has any help, suggestions, or constructive criticisms consider this still unsolved.

EDIT: As far as I can tell, the only C code in there uses this as the input string, but that doesn't make sense because the string I want to use as input is already something chosen by the code. I'll try to see if I can get it to work like that, but that has no variables so I am unsure. Thanks for the help. I won't be able to test it for a few hours, but hopefully it'll work, at least without variables.
Last edited on
This one is for C++ and I've heard good things about it:
http://muparser.beltoforion.de/
Topic archived. No new replies allowed.