C++ 后续表达式 写的时候用STL库里的Stack类的时候遇到的坑 附上代码和注释

#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main(void){
	int i;
	string str;//输入 
	string s;//表达式 
	stack<char> stk; 
	cin>>str;
	
	for(i=0;str[i]!='\0';i++){
		if('0'<= str[i] && str[i]<= '9'){ //  数字直接放进后序表达式 
			s.push_back(str[i]);
		}
		
		else if(str[i]=='('){//左括号 入栈 
			stk.push('('); 
		}
		
		else if(str[i]==')'){//右括号  把左括号之前的都放进后序表达式,然后左括号出栈 
			while(stk.top()!='('){
				s.push_back(stk.top());
				stk.pop();
			}
			stk.pop();
		}
		else if(str[i]=='+' || str[i]=='-'){// 加减 左括号以前的字符依次出栈 并放进后序表达式  然后把加减压栈 
			while(stk.top()!='(' && !stk.empty()){ //这里一定要判断栈是否为空 不然如果遇到 + -号前面没有括号 就会异常 终止 
				s.push_back(str[i]);
				stk.pop();
			}
			stk.push(str[i]); 
		}
		else  if(str[i]=='*' || str[i]=='/'){ //乘除 将顶端的 * / 出栈 并且放进后序表达式 然后将 * / 压栈 
			if(!stk.empty()){ //这里一定要判断 栈是否为空 否者访问栈顶会异常 程序会终止!!!! 
				while(stk.top()=='*' || stk.top()=='/'){
				cout<<"!!"<<endl;	
				s.push_back(stk.top());
				stk.pop();
				}
			}
			stk.push(str[i]);
		}
		
	}
	
	while(!stk.empty()){ //字符串读取完毕 把栈里面的剩余运算符出栈 并且放入后续表达式 
		s.push_back(stk.top());
		stk.pop();
	}
	cout<<s<<endl; //输出后续表达式 
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用 C++STL 库来实现用表示的算术表达式时,可以按照以下步骤进行: 步骤 1:引入必要的头文件 ```cpp #include <iostream> #include <stack> #include <string> ``` 步骤 2:定义函数来判断运算符的优先级 ```cpp int getPriority(char op) { if (op == '+' || op == '-') return 1; else if (op == '*' || op == '/') return 2; else return 0; } ``` 步骤 3:实现函数来判断一个字符是否为运算符 ```cpp bool isOperator(char ch) { return (ch == '+' || ch == '-' || ch == '*' || ch == '/'); } ``` 步骤 4:实现函数来执行运算 ```cpp int performOperation(int operand1, int operand2, char op) { switch(op) { case '+': return operand1 + operand2; case '-': return operand1 - operand2; case '*': return operand1 * operand2; case '/': return operand1 / operand2; default: return 0; } } ``` 步骤 5:实现函数来计算算术表达式的值 ```cpp int evaluateExpression(const std::string& expression) { std::stack<int> operands; std::stack<char> operators; for (int i = 0; i < expression.length(); i++) { if (expression[i] == ' ') continue; if (isdigit(expression[i])) { int operand = 0; while (i < expression.length() && isdigit(expression[i])) { operand = operand * 10 + (expression[i] - '0'); i++; } operands.push(operand); } if (isOperator(expression[i])) { while (!operators.empty() && getPriority(operators.top()) >= getPriority(expression[i])) { int operand2 = operands.top(); operands.pop(); int operand1 = operands.top(); operands.pop(); char op = operators.top(); operators.pop(); int result = performOperation(operand1, operand2, op); operands.push(result); } operators.push(expression[i]); } } while (!operators.empty()) { int operand2 = operands.top(); operands.pop(); int operand1 = operands.top(); operands.pop(); char op = operators.top(); operators.pop(); int result = performOperation(operand1, operand2, op); operands.push(result); } return operands.top(); } ``` 步骤 6:在主函数中调用 evaluateExpression() 函数进行测试 ```cpp int main() { std::string expression; std::cout << "Enter an arithmetic expression: "; std::getline(std::cin, expression); int result = evaluateExpression(expression); std::cout << "Result: " << result << std::endl; return 0; } ``` 这样就可以使用来表示并计算算术表达式了。请注意,上述代码仅处理整数运算,对于浮点数运算或其他特殊情况,可能需要进行适当的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值