假定算数表达式的值均为非负整数常数,不包含变量、小 数和字符常量。 试设计一个算术四则运算表达式求值的简单计算器。 基本要求: 1、 操作数均为非负整数常数,操作符仅为+、-、*、/、(和); 2、 编写 main 函数进行测试。给出详细注释
时间: 2023-06-09 20:06:52 浏览: 45
好的,让我来回答您的问题!
首先,您提出的问题是关于设计一个简单的算术四则运算表达式求值的计算器,它要求操作数都是非负整数常数,操作符仅限于加、减、乘、除、左括号和右括号,并且需要在main函数中进行测试。
为了解决这个问题,我们可以采用栈这个数据结构。具体实现流程如下:
1. 定义一个栈来存储数字和运算符,同时定义一个变量来存储当前数字的值。
2. 用一个循环来遍历表达式中的所有字符。
3. 如果当前字符是数字,就将它加到当前数字的值上。
4. 如果当前字符是运算符或者是表达式末尾的符号,就将当前数字压入栈中,并将当前数字的值重置为0。
5. 如果当前字符是右括号,就弹出栈中的数字和运算符进行一次运算,并将结果再次压入栈中。
6. 最后检查栈中是否只剩下一个数字,如果是,则返回这个数字;否则表达式格式有误,返回错误信息。
代码如下(带有详细的注释):
```
#include <iostream>
#include <stack>
#include <string>
using namespace std;
// 计算器函数,参数为表达式字符串
int Calculator(string expr)
{
stack<int> nums; // 存储数字的栈
stack<char> ops; // 存储运算符的栈
int num = 0; // 当前数字的值
// 遍历表达式字符串的每一个字符
for (char c : expr) {
if (isdigit(c)) {
// 当前字符是数字,将其加到当前数字的值上
num = num * 10 + (c - '0');
} else {
// 当前字符是运算符或者是表达式末尾的符号
if (num != 0) {
// 如果当前数字的值不为0,则将其压入数字栈中
nums.push(num);
num = 0; // 重置当前数字的值
}
if (c == '(') {
// 当前字符是左括号,将其压入运算符栈中
ops.push(c);
} else if (c == '+' || c == '-') {
// 当前字符是加减号,弹出所有优先级大于或等于加减号的运算符,并进行计算
while (!ops.empty() && (ops.top() == '*' || ops.top() == '/' || ops.top() == '+' || ops.top() == '-')) {
int num2 = nums.top(); nums.pop();
int num1 = nums.top(); nums.pop();
if (ops.top() == '+') {
nums.push(num1 + num2);
} else if (ops.top() == '-') {
nums.push(num1 - num2);
} else if (ops.top() == '*') {
nums.push(num1 * num2);
} else if (ops.top() == '/') {
nums.push(num1 / num2);
}
ops.pop();
}
// 将当前加减号压入运算符栈中
ops.push(c);
} else if (c == '*' || c == '/') {
// 当前字符是乘除号,弹出所有优先级大于或等于乘除号的运算符,并进行计算
while (!ops.empty() && (ops.top() == '*' || ops.top() == '/')) {
int num2 = nums.top(); nums.pop();
int num1 = nums.top(); nums.pop();
if (ops.top() == '*') {
nums.push(num1 * num2);
} else if (ops.top() == '/') {
nums.push(num1 / num2);
}
ops.pop();
}
// 将当前乘除号压入运算符栈中
ops.push(c);
} else if (c == ')') {
// 当前字符是右括号,弹出所有运算符,并进行计算,直到遇到左括号
while (ops.top() != '(') {
int num2 = nums.top(); nums.pop();
int num1 = nums.top(); nums.pop();
if (ops.top() == '+') {
nums.push(num1 + num2);
} else if (ops.top() == '-') {
nums.push(num1 - num2);
} else if (ops.top() == '*') {
nums.push(num1 * num2);
} else if (ops.top() == '/') {
nums.push(num1 / num2);
}
ops.pop();
}
ops.pop(); // 弹出左括号
}
}
}
// 处理最后一个数字
if (num != 0) {
nums.push(num);
}
// 处理剩余的运算符
while (!ops.empty()) {
int num2 = nums.top(); nums.pop();
int num1 = nums.top(); nums.pop();
if (ops.top() == '+') {
nums.push(num1 + num2);
} else if (ops.top() == '-') {
nums.push(num1 - num2);
} else if (ops.top() == '*') {
nums.push(num1 * num2);
} else if (ops.top() == '/') {
nums.push(num1 / num2);
}
ops.pop();
}
// 如果只有一个数字,返回它;否则表达式语法错误
if (nums.size() == 1) {
return nums.top();
} else {
return -1; // 表达式含有语法错误
}
}
int main()
{
string expr = "2+3*4+(5-6)/2"; // 测试表达式
int result = Calculator(expr); // 调用计算器函数
if (result != -1) {
cout << "计算结果为:" << result << endl;
} else {
cout << "表达式语法错误!" << endl;
}
return 0;
}
```
希望我的回答能够满足您的需求,如果您还有其他问题,请随时提出。