0

I wrote this code but only works at first operation for example 1 2 + and it prints 3 as result but I need to give it an expression like this: 1 2 + 3 4 - * and it must have to return the value 3. Instead of that, my code prints 2115.

#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main() {
  float op1,op2,value;
  string expression;
  cin>>expression;
  stack<float>p;
  for (int i=0;i<expression.length();++i){
    if (expression[i] != '+' && expression[i] != '-' && expression[i] != '*' && expression[i] != '/'){
      if (expression[i] != ' '){
        p.push(expression[i]);
      }
      else{
        continue;
      }
    }
    if (expression[i] == '+'){
      op1=p.top()-'0';p.pop();
      op2=p.top()-'0';p.pop();
      value=op1+op2;
      p.push(value);
    }
    if (expression[i] == '-'){
      op1=p.top()-'0';
      p.pop();
      op2=p.top()-'0';
      p.pop();
      value=op1-op2;
      p.push(value);      
    }
    if (expression[i]=='*'){
      op1=p.top()-'0';
      p.pop();
      op2=p.top()-'0';
      p.pop();
      value=op1*op2;
      p.push(value);  
    }
    if (expression[i]=='/'){
      op1=p.top()-'0';p.pop();
      op2=p.top()-'0';p.pop();
      value=op1/op2;
      p.push(value);        
       }
      }
      cout<<value<<endl;
    }
apk
  • 37
  • 10

2 Answers2

1

some modified

#include <iostream>
#include <string>
#include <stack>
#include <sstream>
#include <vector>

using namespace std;
int main() {
  float op1,op2,value;
  string inputs ;
  stack<float> p;
  vector<string> nodes ;

    op1=op2=value=0 ;   // initialize to zero.

    getline(cin, inputs) ;
    cout << "input :" << inputs << endl ;

    // tokenning by space
    stringstream ss(inputs) ;
    string item ;
    while (getline(ss, item, ' ') ) {
       nodes.push_back(item) ;
       cout << "token : "<<item << endl ;
    }

  for (int i=0;i<nodes.size();i++){
    string node = nodes[i] ;
    if ( isdigit(node[0]) ) {
        p.push(stof(node));
        cout << "push(num) :" << node << endl;
        continue ;
    }
    if (node == "+"){
      op2=p.top(); p.pop();
      op1=p.top(); p.pop();
      value=op1+op2;
      cout << "add : "<<op1 << " " << op2 << "="<<value << endl;
      p.push(value);
    }
    if (node == "-"){
      op2=p.top(); p.pop();
      op1=p.top(); p.pop();
      value=op1-op2;
      cout << "minus : "<<op1 << " " << op2 << "="<<value << endl;
      p.push(value);      
    }
    if (node=="*"){
      op2=p.top(); p.pop();
      op1=p.top(); p.pop();
      value=op1*op2;
      p.push(value);  
      cout << "mul : "<<op1 << " " << op2 << "="<<value << endl;
    }
    if (node=="/"){
      op2=p.top(); p.pop();
      op1=p.top(); p.pop();
      value=op1/op2;
      p.push(value);        
      cout << "div : "<<op1 << " " << op2 << "="<<value << endl;
     }
  }
  cout<< "result="<< value<<endl;
} 

output test

$ ./a.out
1 2 + 3 4 - *
input :1 2 + 3 4 - *
token : 1
token : 2
token : +
token : 3
token : 4
token : -
token : *
push(num) :1
push(num) :2
add : 1 2=3
push(num) :3
push(num) :4
minus : 3 4=-1
mul : 3 -1=-3
result=-3

$ ./a.out
4.5 1.5 + 9.0 3.0 / +
input :4.5 1.5 + 9.0 3.0 / +
token : 4.5
token : 1.5
token : +
token : 9.0
token : 3.0
token : /
token : +
push(num) :4.5
push(num) :1.5
add : 4.5 1.5=6
push(num) :9.0
push(num) :3.0
div : 9 3=3
add : 6 3=9
result=9
Junhee Shin
  • 688
  • 6
  • 8
  • Thanks now I understand where I was failing. But what it does 'isdigit(node[0])', I know isdigit checks if caracter is a number but why node[0]? – apk Mar 14 '18 at 21:20
  • isdigit() function's prototype int isdigit(int c). the parameter c must be int type. node(token) is string type. so I checked the first character(node[0]) is digit. if you want to check whole string is number correctly, watch this link https://stackoverflow.com/questions/4654636/how-to-determine-if-a-string-is-a-number-with-c – Junhee Shin Mar 15 '18 at 01:33
0

Here is the correct solution for your problem:

#include <iostream>
#include <string>
#include <stack>
#include <ctype.h>                 //Header file for isdigit() function
using namespace std;
int main() {
  float op1,op2,value;
  string expression;
  cin>>expression;
  stack<float>p;
  for (int i=0;i<expression.length();++i){
    if (isdigit(expression[i])){          //Use isdigit() to check if the given char is Digit or not
      p.push(expression[i] - '0');        //Here you should have convert your char to int 
    }
    else if (expression[i] == '+'){
      op1=p.top();p.pop();
      op2=p.top();p.pop();
      value=op1+op2;
      p.push(value);
    }
    else if (expression[i] == '-'){
      op1=p.top();
      p.pop();
      op2=p.top();
      p.pop();
      value=op2-op1;
      p.push(value);      
    }
    else if (expression[i] == '*'){
      op1=p.top();
      p.pop();
      op2=p.top();
      p.pop();
      value=op1*op2;
      p.push(value);  
    }
    else if (expression[i]=='/'){
      op1=p.top();p.pop();
      op2=p.top();p.pop();
      value=op2/op1;
      p.push(value);        
    }
  }
      cout<<value<<endl;
}
Nishant Gupta
  • 2,874
  • 1
  • 8
  • 16