利用栈求表达式的值,写出代码能够完成加减乘除及带括号的四则混合整数运算: 1. 运算符为加 (“”)、减(“-”)、爽(“*”)、除(“/”): 2. 运算数是整数,且位数不限(如:3、33、333); 3.括号(“(”和“)”),括号用于高优先级运算,一个运算可以被多重 括号所包国,如:“((1+2))*3〞 是合法的: 4. 当用户输入的表达式合法,则给出计算结果,如果表达式非法,则给出错误提示。2. 要求程序中应包含 下面几个功能函数: (1) void initstack0:初始化栈 (2)int Nake_str0:语法检查并计算 (3) int push operator (int operator):将运算符压入栈 (4) int push num (double num):将运算数压入栈 (5) int procede (int operator):处理运算符 (6) int change_ opnd (int operator):将字符型运算符转换成优先级 (7)int push opnd (int operator):将运算符压入栈 (8) int pop opnd (:将运算符弹出栈 (9)int caculate (int cur opnd):简单计算+,一,*, (10) double pop num Q:弹出运算数
时间: 2024-02-21 22:00:29 浏览: 79
以下是利用栈求表达式的值的代码:
```c++
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
using namespace std;
// 初始化栈
void initstack0(stack<char>& optr, stack<int>& opnd) {
while (!optr.empty()) {
optr.pop();
}
while (!opnd.empty()) {
opnd.pop();
}
}
// 判断字符是否为数字
bool isDigit(char ch) {
return (ch >= '0' && ch <= '9');
}
// 判断字符是否为运算符
bool isOperator(char ch) {
return (ch == '+' || ch == '-' || ch == '*' || ch == '/');
}
// 将字符型运算符转换成优先级
int change_opnd(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
// 将运算符压入栈
int push_optr(char op, stack<char>& optr, stack<int>& opnd) {
if (optr.empty() || op == '(') {
optr.push(op);
return 0;
}
char top_op = optr.top();
if (op == ')') {
while (top_op != '(') {
int val2 = opnd.top();
opnd.pop();
int val1 = opnd.top();
opnd.pop();
switch (top_op) {
case '+':
opnd.push(val1 + val2);
break;
case '-':
opnd.push(val1 - val2);
break;
case '*':
opnd.push(val1 * val2);
break;
case '/':
opnd.push(val1 / val2);
break;
}
optr.pop();
if (optr.empty()) {
return 0;
}
top_op = optr.top();
}
optr.pop();
} else {
int op1 = change_opnd(op);
int op2 = change_opnd(top_op);
if (op1 <= op2) {
int val2 = opnd.top();
opnd.pop();
int val1 = opnd.top();
opnd.pop();
switch (top_op) {
case '+':
opnd.push(val1 + val2);
break;
case '-':
opnd.push(val1 - val2);
break;
case '*':
opnd.push(val1 * val2);
break;
case '/':
opnd.push(val1 / val2);
break;
}
optr.pop();
}
optr.push(op);
}
return 0;
}
// 将运算数压入栈
int push_opnd(int num, stack<int>& opnd) {
opnd.push(num);
return 0;
}
// 弹出运算符
char pop_optr(stack<char>& optr) {
char op = optr.top();
optr.pop();
return op;
}
// 弹出运算数
int pop_opnd(stack<int>& opnd) {
int num = opnd.top();
opnd.pop();
return num;
}
// 简单计算+,-,*,/
int calculate(char op, int num1, int num2) {
switch (op) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
return num1 / num2;
}
return 0;
}
// 语法检查并计算
int Nake_str0(string str, stack<char>& optr, stack<int>& opnd) {
int i = 0;
while (i < str.length()) {
char ch = str[i];
if (ch == ' ') {
i++;
continue;
}
if (isDigit(ch)) {
int num = 0;
while (i < str.length() && isDigit(str[i])) {
num = num * 10 + (str[i] - '0');
i++;
}
push_opnd(num, opnd);
} else if (isOperator(ch)) {
push_optr(ch, optr, opnd);
i++;
} else if (ch == '(') {
optr.push(ch);
i++;
} else if (ch == ')') {
push_optr(ch, optr, opnd);
i++;
} else {
return -1; // 表达式非法
}
}
while (!optr.empty()) {
int val2 = pop_opnd(opnd);
int val1 = pop_opnd(opnd);
char op = pop_optr(optr);
int val = calculate(op, val1, val2);
push_opnd(val, opnd);
}
return opnd.top();
}
int main() {
stack<char> optr;
stack<int> opnd;
string str;
cout << "请输入表达式:" << endl;
getline(cin, str);
int result = Nake_str0(str, optr, opnd);
if (result == -1) {
cout << "表达式非法!" << endl;
} else {
cout << "计算结果为:" << result << endl;
}
return 0;
}
```
阅读全文
相关推荐


















