### Calculation

I am a beginner and I need help writing a calculation program.
The program should be able to handle at least 8 operators, square, sin, cos, tan, powers, +, -, *, /.
The program should be able to handle all these things in one expression.

For example: v for square, s for sin, c for cos, t for tan and ^ for power. v(s3.4/5^8). T
he program needs to understand parenthesis. And square, sin, cos and tan should be used as a "prefix" (executed on the following operator).
And when '=' is put in the result is written out, the program resets and restarts.

And if you could put in some comentaries in the code, so I know waht you've dont. It would be very helpful for the future.

Thank you
Last edited on
Unfortunately, no one here is going to do your homework for you.

I don't know why CS 101/102 professors continue to give this as homework. It is not a particularly simple thing to do.

You might want to google around "recursive descent".

Good luck.
It is NOT simple, at all.

For a while, I've tried to do this, and have never succeded...

If you get a clue, contact me. I'd love to know.

PS: See the book listed in my Bio, it has an exercise after chapter 12 describing how to do this in detail.
hello i'm mo3taz from egypt,
i,ve solved this problem but just for the operators +,-;
example input: 1+2+3-2+3-4+5.
out put: the result
don't forget the fullstop at the end
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````# include int main() { int sum = 0; int x , y; // two successive numbers in the input between them char test = 'r'; // an operator char op; // stands for an operator while (test == 'c' || test == 'r') // tests for continue(c) or restart (r) { if ( test == 'r') { sum = 0; cout << "___________________________________________________________________"; cout << "\nplease enter some successive numbers between each two an operator\n"; cout << "the equation encludes just '+' or '-' ( example:1+2-3+4.) \n"; } if (test == 'c') { cout << " please enter another sequence to add to the old sequence\n"; cout << "the old value was : " << sum << endl; } cin >> x; cin >> op; cin >> y; if (op == '+') sum = (sum + x + y); else if ( op == '-') sum = (sum + x - y); while (op != '.') { cin >> op; if (op == '.') break ; // notice that the operator may be '.' cin >> y ; // after entering the while loop if (op == '+') sum = (sum + y); else if (op == '-') sum = (sum - y); } cout <<"the result of this sequenc is: \n:" << sum << endl; cout << "please enter c to continue calculating to the old result\n"; cout << "please enter r to restart and calculate a new value\n"; cout << " please enter e to exit \n "; cin>> test; if (test == 'e') break; } }``````

if there is any problem with this tell me , and i'll try to insert other operators
my e_mail is : mo3taz_kilany@hotmail.com
Last edited on
There is a way to solve this problem with a recurive descent:

I have *fully* copied this code from Arnold Willemers page (http://www.willemer.de/informatik/cpp/rekursion.htm)
But you can also find it in one of his books.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149`` ``````enum tToken { PLUS, MINUS, MUL, DIV, LPAR, RPAR, NUMBER, END, ERROR }; // Globale Variablen: tToken aktToken; // das zuletzt erkannte Token double TokenZahlenWert; // der Wert bei Zahlenkonstanten char *srcPos; // Programm-Position tToken sucheToken() // Sucht ab der aktuellen Stringposition das nächste Token. // Hier werden auch die Zahlenkonstanten bestimmt und in // der globalen Variablen TokenZahlenWert abgelegt. { aktToken = ERROR; if (*srcPos==0) { aktToken = END; } else { switch (*srcPos) { case '(': aktToken=LPAR; break; case ')': aktToken=RPAR; break; case '*': aktToken=MUL; break; case '/': aktToken=DIV; break; case '+': aktToken=PLUS; break; case '-': aktToken=MINUS; break; } if (*srcPos>='0' && *srcPos<'9') { aktToken=NUMBER; TokenZahlenWert = 0.0; } while (*srcPos>='0' && *srcPos<'9') { TokenZahlenWert *= 10; TokenZahlenWert += *srcPos-'0'; srcPos++; } if (aktToken != NUMBER) { srcPos++; } } return aktToken; } tToken Error(char *s) // Meldung ausgeben und Fehler-Token zurückliefern { cerr << s << endl; return ERROR; } double PlusMinus(); // Prototyp double Klammern() // wertet Klammern, Zahlen und Vorzeichen aus // ruft Klammern() und PlusMinus() dadurch rekursiv auf! { double Wert; switch(aktToken) { case NUMBER: sucheToken(); return TokenZahlenWert; case MINUS: sucheToken(); return -Klammern(); case LPAR: sucheToken(); Wert = PlusMinus(); if (aktToken != RPAR) { return Error(") expected"); { sucheToken(); return Wert; case END: return 1; } return Error("primary expected"); } double MulDiv() // wertet Multiplikation und Division aus // ruft Klammern() auf, dadurch rekursiv! { double Wert; // rufe erst die Operation mit der nächsthöheren // Priorität auf Wert = Klammern(); while (aktToken==MUL || aktToken==DIV) { if (aktToken==MUL) { sucheToken(); Wert *= Klammern(); } else if (aktToken==DIV) { sucheToken(); Wert /= Klammern(); } } return Wert; } double PlusMinus() // wertet Summe und Differenz aus // ruft MulDiv() auf, dadurch rekursiv { double Wert; // rufe erst die Operation mit der nächsthöheren // Priorität auf Wert = MulDiv(); while (aktToken==PLUS || aktToken==MINUS) { if (aktToken==PLUS) { sucheToken(); Wert += MulDiv(); } else if (aktToken==MINUS) { sucheToken(); Wert -= MulDiv(); } } return Wert; } double Auswertung(char *s) { srcPos = s; sucheToken(); // bestimme das erste Token vorweg return PlusMinus(); } int main(int argc, char* argv[]) { double Wert = Auswertung(argv[1]); cout << Wert << endl; return 0; }``````

its in german, sorry for that :)

It loops trough all token and looks what it is and then calls the needed function.

Hope it helps

wooden nose
Last edited on