int exp(char* input)
{
char*token=strtok(input, "");
stack <int>num;
stack <string> sym;
int o1=0;
int o2=0;
int result=0;
for(int i=strlen(input)-1; i>=0; i--)
{
if(isNumber(token)==true)
{
num.push((int)token);
}
elseif(isOperator(token)==true&&sym.empty()==true)
{
sym.push(token);
}
elseif(isOperator(token)==true && !sym.empty() && precedence(token)>precedence(sym.top()))
{
sym.push(token);
}
while(!sym.empty()){
num.top()=o2;
num.pop();
num.top()=o1;
num.pop();
if(sym.top()=="+")
{
result=o1+o2;
}
if(sym.top()=="-")
{
result=o1-o2;
}
if(sym.top()=="*")
{
result=o1*o2;
}
if(sym.top()=="/")
{
result=o1/o2;
}
}
}
return(result);
}
void main()
{
char* input=newchar[100];
cout<<"Please enter an arithmetic expression"<<endl;
cin>>input;
int answer;
answer=exp(input);
cout<<"Answer to this expression"<<endl;
cout<<answer<<endl;
system("pause");
}
I've seen many posts on this topic, but none of them explain something that is troubling me. I am using two stacks here. How can I grab integers from my tokens? If a token is a number, how do I push it on to my integer stack?
The function in question here is int exp(char* input)
My goal is to take a prefix expression, and evaluate so I can get an answer.
This translated pretty hideously into the codeblocks on here. If anyone needs any help reading anything in my code, just let me know. I appreciate all responses.