请给出代码c++
时间: 2024-02-15 12:05:51 浏览: 16
以下是一个简单的 C++ 实现,实现了基本要求中的功能,但没有添加选做内容。代码中使用了两个栈来实现括号匹配和逆波兰表达式的转换。
```c++
#include <iostream>
#include <stack>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;
// 判断运算符的优先级
int getPriority(char c) {
switch (c) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return 0;
}
}
// 判断运算符的结合性
bool isLeftAssociative(char c) {
return (c != '^');
}
// 将中缀表达式转换为逆波兰表达式
string infixToPostfix(string infix) {
stack<char> opStack;
string postfix;
for (int i = 0; i < infix.size(); i++) {
if (isdigit(infix[i]) || infix[i] == '.') {
postfix += infix[i];
while (isdigit(infix[i+1]) || infix[i+1] == '.') {
i++;
postfix += infix[i];
}
postfix += ' ';
}
else if (infix[i] == '(') {
opStack.push(infix[i]);
}
else if (infix[i] == ')') {
while (!opStack.empty() && opStack.top() != '(') {
postfix += opStack.top();
postfix += ' ';
opStack.pop();
}
if (!opStack.empty() && opStack.top() == '(') {
opStack.pop();
}
else {
return "Error: Mismatched parentheses\n";
}
}
else if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^') {
while (!opStack.empty() && opStack.top() != '(' && ((isLeftAssociative(infix[i]) && getPriority(infix[i]) <= getPriority(opStack.top())) || (!isLeftAssociative(infix[i]) && getPriority(infix[i]) < getPriority(opStack.top())))) {
postfix += opStack.top();
postfix += ' ';
opStack.pop();
}
opStack.push(infix[i]);
}
else {
return "Error: Invalid character\n";
}
}
while (!opStack.empty()) {
if (opStack.top() == '(' || opStack.top() == ')') {
return "Error: Mismatched parentheses\n";
}
postfix += opStack.top();
postfix += ' ';
opStack.pop();
}
return postfix;
}
// 对逆波兰表达式求值
double evaluatePostfix(string postfix) {
stack<double> numStack;
char* token = strtok(&postfix[0], " ");
while (token != NULL) {
if (isdigit(token[0]) || (token[0] == '-' && isdigit(token[1]))) {
numStack.push(atof(token));
}
else if (token[0] == '+' || token[0] == '-' || token[0] == '*' || token[0] == '/' || token[0] == '^') {
if (numStack.size() < 2) {
return NAN;
}
double b = numStack.top();
numStack.pop();
double a = numStack.top();
numStack.pop();
switch (token[0]) {
case '+':
numStack.push(a + b);
break;
case '-':
numStack.push(a - b);
break;
case '*':
numStack.push(a * b);
break;
case '/':
numStack.push(a / b);
break;
case '^':
numStack.push(pow(a, b));
break;
}
}
else {
return NAN;
}
token = strtok(NULL, " ");
}
if (numStack.size() == 1) {
return numStack.top();
}
else {
return NAN;
}
}
// 判断中缀表达式中的括号是否匹配
bool isParenthesesMatched(string infix) {
stack<char> parenStack;
for (int i = 0; i < infix.size(); i++) {
if (infix[i] == '(') {
parenStack.push(infix[i]);
}
else if (infix[i] == ')') {
if (parenStack.empty() || parenStack.top() != '(') {
return false;
}
else {
parenStack.pop();
}
}
}
return parenStack.empty();
}
int main() {
while (true) {
cout << "Calculator\n\n";
cout << "Select an option:\n";
cout << "1. Evaluate expression\n";
cout << "2. Exit\n\n";
cout << "Option: ";
int option;
cin >> option;
if (option == 1) {
cout << "Enter an expression: ";
string infix;
cin >> infix;
if (isParenthesesMatched(infix)) {
string postfix = infixToPostfix(infix);
if (postfix.substr(0, 6) == "Error:") {
cout << postfix << endl;
}
else {
double result = evaluatePostfix(postfix);
if (isnan(result)) {
cout << "Error: Invalid expression\n";
}
else {
cout << "Result: " << result << endl;
}
}
}
else {
cout << "Error: Mismatched parentheses\n";
}
}
else if (option == 2) {
break;
}
else {
cout << "Invalid option\n";
}
cout << endl;
}
return 0;
}
```