用C++编写一个计算器软件
时间: 2023-08-04 14:16:29 浏览: 46
好的,以下是C++编写的简单计算器软件:
```cpp
#include <iostream>
#include <stack>
#include <cmath>
using namespace std;
double evaluate(string expr) {
stack<double> operands;
stack<char> operators;
for (int i = 0; i < expr.length(); i++) {
if (expr[i] == ' ') {
continue;
} else if (isdigit(expr[i])) {
double num = 0;
while (i < expr.length() && isdigit(expr[i])) {
num = num * 10 + (expr[i] - '0');
i++;
}
i--;
operands.push(num);
} else if (expr[i] == '(') {
operators.push(expr[i]);
} else if (expr[i] == ')') {
while (operators.top() != '(') {
char op = operators.top();
operators.pop();
double a = operands.top();
operands.pop();
double b = operands.top();
operands.pop();
if (op == '+') {
operands.push(b + a);
} else if (op == '-') {
operands.push(b - a);
} else if (op == '*') {
operands.push(b * a);
} else if (op == '/') {
operands.push(b / a);
} else if (op == '^') {
operands.push(pow(b, a));
}
}
operators.pop();
} else if (expr[i] == '+' || expr[i] == '-') {
while (!operators.empty() && operators.top() != '(') {
char op = operators.top();
operators.pop();
double a = operands.top();
operands.pop();
double b = operands.top();
operands.pop();
if (op == '+') {
operands.push(b + a);
} else if (op == '-') {
operands.push(b - a);
} else if (op == '*') {
operands.push(b * a);
} else if (op == '/') {
operands.push(b / a);
} else if (op == '^') {
operands.push(pow(b, a));
}
}
operators.push(expr[i]);
} else if (expr[i] == '*' || expr[i] == '/') {
while (!operators.empty() && (operators.top() == '*' || operators.top() == '/')) {
char op = operators.top();
operators.pop();
double a = operands.top();
operands.pop();
double b = operands.top();
operands.pop();
if (op == '+') {
operands.push(b + a);
} else if (op == '-') {
operands.push(b - a);
} else if (op == '*') {
operands.push(b * a);
} else if (op == '/') {
operands.push(b / a);
} else if (op == '^') {
operands.push(pow(b, a));
}
}
operators.push(expr[i]);
} else if (expr[i] == '^') {
operators.push(expr[i]);
}
}
while (!operators.empty()) {
char op = operators.top();
operators.pop();
double a = operands.top();
operands.pop();
double b = operands.top();
operands.pop();
if (op == '+') {
operands.push(b + a);
} else if (op == '-') {
operands.push(b - a);
} else if (op == '*') {
operands.push(b * a);
} else if (op == '/') {
operands.push(b / a);
} else if (op == '^') {
operands.push(pow(b, a));
}
}
return operands.top();
}
int main() {
string expr;
cout << "Enter an expression: ";
getline(cin, expr);
double result = evaluate(expr);
cout << "Result: " << result << endl;
return 0;
}
```
程序使用栈来实现表达式计算。首先,程序接收一个表达式作为输入,并调用evaluate函数来计算表达式。evaluate函数使用两个栈,一个用于操作数(operands),另一个用于操作符(operators)。程序逐个字符读取表达式,如果字符是空格,则跳过。如果字符是数字,则将数字转换为double类型,并将其推入操作数栈。如果字符是左括号,则将其推入操作符栈。如果字符是右括号,则将操作符栈中的操作符弹出,直到遇到左括号。在每一个弹出的操作符上,程序从操作数栈中弹出两个操作数,进行相应的运算,并将结果推回操作数栈。如果字符是加号或减号,则将操作符栈中的所有乘号、除号、加号和减号弹出,直到遇到左括号或操作符栈为空。在每一个弹出的操作符上,程序从操作数栈中弹出两个操作数,进行相应的运算,并将结果推回操作数栈。然后,程序将当前操作符推入操作符栈。如果字符是乘号或除号,则将操作符栈中的所有乘号和除号弹出,直到遇到加号、减号、左括号或操作符栈为空。在每一个弹出的操作符上,程序从操作数栈中弹出两个操作数,进行相应的运算,并将结果推回操作数栈。然后,程序将当前操作符推入操作符栈。如果字符是乘方符号,则将其推入操作符栈。最后,当所有字符都被读取后,程序从操作符栈中弹出所有操作符,并在每一个操作符上执行相应的计算,直到操作符栈为空。最后,程序返回操作数栈中的唯一元素,即计算结果。