多位数后缀表达式

Multidigits postfix expression

本文关键字:表达式 后缀 多位数      更新时间:2023-10-16

我写了这段代码,但仅在第一次操作时有效,例如 1 2 +,结果它打印 3,但我需要给它一个这样的表达式:1 2 + 3 4 - *,它必须返回值 3。取而代之的是,我的代码打印 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;
    }

一些修改

#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;
} 

输出测试

$ ./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

这是您的问题的正确解决方案:

#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;
}