Click to See Complete Forum and Search --> : c++ rpn expression code error


webdesigner
03-30-2004, 06:55 PM
this program calculate rpn expression
of the form 42 3 +
and print the result
i want toknow what errors in code
and if any body have any ideas:( :( :( :(
#include<string.h>
#include<stdlib.h>
#include<iostream.h>
#include<ctype.h>
#include<conio.h>
#include "stack.h"

stack s;

class RPNexp
{
public:
string str;
bool isnumb_str(const char*t);
string calcRPN(str);
};

bool RPNexp::isnumb_str(const char*t)
{
bool isnumb=false;
while(*t)
{
//for(int i=o;i<str.length();i++)
isnumb=isdigit(*t);
if(!isnumb)
break;
++t;
}
return isnumb;
}
string RPNexp::calcRPN(str)
{
char tmp1,tmp2,value;
/*check to see if it was a number*/
for(int i=0;i<str.length();i++)
{
token=str[i];
while(token!='$')
{
bool isnumb=isnumb_str(token);

/*
*if it's a number push it to a stack
*if it's an operator then calculate both values
*/
if(isnumb)
{
s.push(atoi(token));//convert string to integer
}
else
switch (token)
{
case'+':
value=s.pop()+s.pop();
s.push(value);
break;
case'-':
tmp2=s.pop();
tmp1=s.pop();
value=v1-v2;
s.push(value);
case'*':
value=s.pop()*s.pop();
case'/':
{
tmp2=s.pop();
tmp1=s.pop();
if(tmp1==0)
{
cout<<"error divide by zero";
error=true;
}
else
{
value=mp2/tmp1;
s.push(value);
}
}//case'/'
break;
default:
cout<<"error unknown."<<endl;
}//switch
}
}
return value;
}


int main()
{
RPNexp r;
cout<<"\nEnter RPN expression to calculate it.";
cout<<"\n of the form 2 4 7 + *.";
cout<<"\n dont't use parantheses.";
cout<<"\n you must separate operators and operands by white space.";
cout<<"\n end the expression with'$'.\n";
cout<<"\n enter expression.";
cin.getline(r.str,'$');
cout<<"The total result of the expression is: "<<r.calcRPN(str)<<endl;
getch();
return 0;
}

Aronya1
03-31-2004, 12:33 AM
You're WAY off in your choice of forum. You should try posting this in the "Other" forum area.

CardboardHammer
04-12-2004, 05:40 PM
*sniff* Do I smell homework? If so, either your instructor is assigning overly complicated problems in the process of teaching OO in C++ or you really should have paid (a lot) more attention in your previous courses (the more likely scenario, based on my observations as both a student and an instructor).

If nothing else, try compiling it and you'll get at least one message telling you what is wrong.