PDA

View Full Version : i don't know what i do wrong..



Hardstyle
27th June 2010, 14:10
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...



double MyMainWindow::calculeaza()
{
lastposition=aux-1;
redo:
if(lastposition>=1){
myvect[lastposition-1]= QString::number(calcul(lastposition));
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;
}



here is a link with all i've done...http://www.4shared.com/file/Quv4vcMG/mycalc.html



thx

tbscope
27th June 2010, 15:05
Can you explain what the problem is?

One thing I noticed is that you check for "x" when multiplying and in your description you use *

Hardstyle
27th June 2010, 17:33
it dosn't work ....download the link and u will see...