i want to make a smal program that evaluate a math expresion like "3*14+25-5"...and i realy dont know what i do wrong...
my idea is to put evrything in a array of strings... so it will look like this:
myvect[0]=3
myvect[1]=*
myvect[2]=14
myvect[3]=+
......
and after i check the priority of the operators using the "grad(int pozitie)" function and i dont know what i do wrong... heres are the codes + attaches with all i'v done so far...
Qt Code:
double MyMainWindow::calculeaza() { lastposition=aux-1; redo: if(lastposition>=1){ lastposition-=2;} if(lastposition>0){goto redo;} return myvect[0].toDouble(); } double MyMainWindow::calcul(int pozitie) { if (lastposition > pozitie)lastposition=pozitie; if(grad(pozitie)<grad(pozitie-2)) { if(myvect[pozitie]=="+") return myvect[pozitie-1].toDouble() + calcul(pozitie-2); else if(myvect[pozitie]=="-") return myvect[pozitie-1].toDouble() - calcul(pozitie-2); else if(myvect[pozitie]=="x") return myvect[pozitie-1].toDouble() * calcul(pozitie-2); else if(myvect[pozitie]==":") return myvect[pozitie-1].toDouble() / calcul(pozitie-2); } else { if(myvect[pozitie]=="+") return myvect[pozitie-1].toDouble() + myvect[pozitie+1].toDouble(); else if(myvect[pozitie]=="-") return myvect[pozitie-1].toDouble() - myvect[pozitie+1].toDouble(); else if(myvect[pozitie]=="x") return myvect[pozitie-1].toDouble() * myvect[pozitie+1].toDouble(); else if(myvect[pozitie]==":") return myvect[pozitie-1].toDouble() / myvect[pozitie+1].toDouble(); } return 0; } int MyMainWindow::grad(int pozitie) { if(pozitie<0)return 0; if(myvect[pozitie]=="+"||myvect[pozitie]=="-") return 1; if(myvect[pozitie]==":"||myvect[pozitie]=="x") return 2; return 0; }To copy to clipboard, switch view to plain text mode
here is a link with all i've done...http://www.4shared.com/file/Quv4vcMG/mycalc.html
thx
Bookmarks