hey everyone
i'm a student of c++ language. I have to implement an RPN calculator. RPN stands for revers polish notation and it's a more simple way o resolve equations. I was debugging the code when a really strange problem(for me) jumped out. I tried resolving (3+2)*5, this equation in RPN looks like this 3 2 + 5 *, my program does this, finds the "+" and erase it from my deque, the iterator goes back one space so know point to 2, finds the 2, he puts it in a variable called tmp and then erase it, the iterator know points at 3, he does tmp+3 and puts the result where the 3 was, and does the same operation with the 2 remaining number 5 5 *.
but if I pu (3+2)*5+4, everything in my program changes and I can't undestrand why. the problem is that going to resolve the same bit of before 3 2 +, when i erase the + the iterator doesn't point a 2 but at 3. this is my code
if(*it=="*"||*it=="+"||*it=="/"||*it=="-")
{
string sign=*it;
output.erase(it),*it--,str=*it;
strcpy(cstr,str.c_str());
tmp=atoi(cstr);
output.erase(it),*it--;
str=*it;
strcpy(cstr,str.c_str());
output.erase(it),*it--;
if(sign=="+")
tmp=tmp+atoi(cstr);
else if(sign=="-")
tmp=atoi(cstr)-tmp;
else if(sign=="*")
tmp=atoi(cstr)*tmp;
else if (sign=="/")
tmp=atoi(cstr)/tmp;
itoa(tmp,cstr,10);
str=cstr;
*it++;
output.insert(it,str);
it=output.begin();
}
*it++;
if(*it=="*"||*it=="+"||*it=="/"||*it=="-")
{
string sign=*it;
output.erase(it),*it--,str=*it;
strcpy(cstr,str.c_str());
tmp=atoi(cstr);
output.erase(it),*it--;
str=*it;
strcpy(cstr,str.c_str());
output.erase(it),*it--;
if(sign=="+")
tmp=tmp+atoi(cstr);
else if(sign=="-")
tmp=atoi(cstr)-tmp;
else if(sign=="*")
tmp=atoi(cstr)*tmp;
else if (sign=="/")
tmp=atoi(cstr)/tmp;
itoa(tmp,cstr,10);
str=cstr;
*it++;
output.insert(it,str);
it=output.begin();
}
*it++;
To copy to clipboard, switch view to plain text mode
can someone help me?
thanks
P.S. sorry for my english
Bookmarks