多位数后缀表达式
Multidigits postfix expression
我写了这段代码,但仅在第一次操作时有效,例如 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;
}
相关文章:
- 如何使用 ASCII 转换使用字符堆栈计算后缀表达式
- 多位数后缀表达式
- 检查字符串是否包含有效的后缀表达式
- 在C++中从后缀表达式字符串打印单个操作
- C++后缀表达式未定义与未指定行为
- 如何从后缀表达式生成表达式树
- 中缀到后缀表达式转换代码不起作用
- 使用Stacks从中缀表达式转换为后缀(C++)
- 我想看到一个在整个后缀表达式的上下文中查找转换类型 id 的示例
- 使用堆栈评估后缀表达式时出现分段错误
- 如何正确测试我的后缀表达式是否有效
- 从 txt 文件中将后缀表达式作为字符串读取
- 在C++中使用树计算后缀表达式
- 后缀表达式中的等号运算符
- 计算后缀表达式.pop() 返回不正确的值来计算函数
- 使用堆栈C++后缀表达式
- 计算具有两个以上操作数的后缀表达式时出现问题
- 如何在表达式中计算后缀和前缀增量运算符
- 在c++中计算后缀表达式时接收堆栈错误
- 后缀表达式求值