Hi_mynameis
Hi_mynameis

Reputation: 11

I need help on my reverse polish notation calculator

I am working on a program to make a reverse polish notation calculator and I am wondering if anyone can give me some hints. The calculator will intake a single line from the user like 2 3 + 7 4 - *; with spaces in between and I am suppose to print one result after every operation.

here is part of my code

#include <iostream>
#include <string>
#include <stack>
#include <sstream>
using namespace std;

int main() {
  stack<float>stack;
  int i;
  float num,result,first,second;
  char op,ch;
  string str;

  getline(cin,str);
  istringstream is(str);
  for(int i=0;i<str.size();i++) {
    is>>num;
    stack.push(num);
  }
  for (i=0;i<str.size();++i) {
    ch=str[i];
  }
  if (ch=='+'||'-'||'*'||'/') {
    if (ch='+') {
      first=stack.top();
      stack.pop();
      second=stack.top();
      stack.pop();
      result=first+second;
      stack.push(result);
      cout<<result;
    }
//  } // missing from question
//}

I have been getting weird numbers as the outcome. Am I reading in my stack correctly?

Upvotes: 1

Views: 8235

Answers (2)

gman
gman

Reputation: 1272

A clean and elegant way to do this is to push all the entries into the stack(if not they are operators)as a float and perform the pop operations on the stack when you encounter an operator get the operator. If its an operator perform two pops and get the operants and perform the appropriate operation and push the result back to the stack

The reason for your wired numbers output is that you have the float values of the operators also in the stack and you loop through the string to find the operators. So for the example 2 3 + 7 4 - * .you will have the stack as 2 3 float(+) 7 4 float(-) float(*)<-being the stack top. so when you loop through the string and find the '+' symbol. you add the values of float(*) and float(-) and push it into the stack. I hope this will make your doubt clear. :)

Edit : This is the code for the above explanation.

#include <iostream>
#include <string.h> 
#include <stack>
#include <sstream>
#include <stdlib.h>

using namespace std ;

int main()
{stack<float>stack;
int i;
float num,result,first,second;  
char op,ch;
string str,str1;

getline(cin,str);
istringstream is(str); 

for(;is>>str1;){

if(str1.compare("+")==0){

    first=stack.top();
    stack.pop();
    second=stack.top();
    stack.pop();


    stack.push(first+second);

    }else if(str1.compare("-")==0){

    first=stack.top();
    stack.pop();
    second=stack.top();
    stack.pop();
    stack.push(first-second);
    }else if(str1.compare("*")==0){

    first=stack.top();
    stack.pop();
    second=stack.top();
    stack.pop();
    stack.push(first*second);
    }else if(str1.compare("/")==0){

    first=stack.top();
    stack.pop();
    second=stack.top();
    stack.pop();
    stack.push(first/second);
    }else{

    stack.push(strtof(str1.c_str(),NULL));              
    }
}
cout<<"The result of the expression is:"<<stack.top();
} 

Upvotes: 0

OmnipotentEntity
OmnipotentEntity

Reputation: 17131

This is probably not your only issue, but you have:

if (ch=='+'||'-'||'*'||'/') {

when you probably actually mean:

if (ch=='+' ||
    ch=='-' ||
    ch=='*' ||
    ch=='/') {

Also right under that you have:

if (ch='+') {

you probably actually mean:

if (ch=='+') {

= is assignment (you are setting ch to '+') whereas == is comparison (you are testing whether or not ch is equal to '+')

Upvotes: 3

Related Questions