要求:用户输入表达式,代码结果输出计算的结果
这里会用到<map>、<iomanip>、<stack>头文件,map用于存储操作符的优先级,stack有两个栈,一个用来存储操作数、一个用来存储操作符。
PS:我给出了两种实现,一种是封装成类,一种是不进行封装
类实现:
#include<iostream>
#include<iomanip>
#include<stack>
#include<map>
using namespace std;
class Counter {
private:
string s;
stack<char>oper;//运算符栈
stack<double>digit;//数值栈
//利用map头文件
//直接连接他们的优先级关系
map<char, int>mp = { {'+',1}, {'-',1}, {'*',2}, {'/',2} };
public:
//获得string函数
void GetString() {
cin >> s;
}
//计算函数
void Calculate() {
//给操作数赋值
//由于栈的特性,这是第二个数值
double b = digit.top();
digit.pop();
//第一个数值
double a = digit.top();
digit.pop();
//定义计算结果
double result;
//定义运算符
char c = oper.top();
oper.pop();
//计算
if (c == '+')
result = a + b;
if (c == '-')
result = a - b;
if (c == '*')
result = a * b;
if (c == '/')
result = a / b;
digit.push(result);
}
//功能实现函数
void func() {
double x;//计算数字
for (int i = 0; i < s.length(); i++) {
//如果是数字
if (s[i] >= '0' && s[i] <= '9') {
x = s[i] - '0';
int y = 0;
for (int j = i + 1; s[j] >= '0' && s[j] <= '9'; j++) {
x = x * 10 + s[j] - '0';
y++;
}
digit.push(x);//数值入栈
i += y;//跳跃
}
//如果是左括号,直接入栈
else if (s[i] == '(')
oper.push(s[i]);
//如果是右括号,则必有左括号,计算到左括号为止,左括号出栈
else if (s[i] == ')') {
//如果还没到左括号就一直计算
while (oper.top() != '(')
Calculate();
//到了左括号,左括号出栈
oper.pop();
}
//如果是运算符,看它们的优先级,如果优先级小于等于栈顶元素,计算
//如果栈空则直接入栈
else {
while (!oper.empty() && mp[oper.top()] >= mp[s[i]])
Calculate();
oper.push(s[i]);
}
}
//如果还有运算符剩余,则一直计算
while (!oper.empty())
Calculate();
//保留两位小数
cout << setiosflags(ios::fixed) << setprecision(2) << digit.top() << endl;
digit.pop();//相当于清空栈
}
};
int main()
{
int n;
cout << "请输入表达式的个数:";
cin >> n;
while (n--) {
cout << "输入一个表达式:" << endl;
Counter c;
c.GetString();
c.func();
}
cout << endl;
system("pause");
return 0;
}
非类实现:
#include<iostream>
#include<iomanip>
#include<stack>
#include<map>
using namespace std;
stack<char>oper;//运算符栈
stack<double>digit;//数值栈
//利用map头文件
//直接连接他们的优先级关系
map<char, int>mp = { {'+',1}, {'-',1}, {'*',2}, {'/',2} };
//计算函数
void Calculate() {
//给操作数赋值
//由于栈的特性,这是第二个数值
double b = digit.top();
digit.pop();
//第一个数值
double a = digit.top();
digit.pop();
//定义计算结果
double result;
//定义运算符
char c = oper.top();
oper.pop();
//计算
if (c == '+')
result = a + b;
if (c == '-')
result = a - b;
if (c == '*')
result = a * b;
if (c == '/')
result = a / b;
digit.push(result);
}
//功能实现函数
void func() {
string s;
cin >> s;
double x;//计算数字
for (int i = 0; i < s.length(); i++) {
//如果是数字
if (s[i] >= '0' && s[i] <= '9') {
x = s[i] - '0';
int y = 0;
for (int j = i + 1; s[j] >= '0' && s[j] <= '9'; j++) {
x = x * 10 + s[j] - '0';
y++;
}
digit.push(x);//数值入栈
i += y;//跳跃
}
//如果是左括号,直接入栈
else if (s[i] == '(')
oper.push(s[i]);
//如果是右括号,则必有左括号,计算到左括号为止,左括号出栈
else if (s[i] == ')') {
//如果还没到左括号就一直计算
while (oper.top() != '(')
Calculate();
//到了左括号,左括号出栈
oper.pop();
}
//如果是运算符,看它们的优先级,如果优先级小于等于栈顶元素,计算
//如果栈空则直接入栈
else {
while (!oper.empty() && mp[oper.top()] >= mp[s[i]])
Calculate();
oper.push(s[i]);
}
}
//如果还有运算符剩余,则一直计算
while (!oper.empty())
Calculate();
//保留两位小数
cout << setiosflags(ios::fixed) << setprecision(2) << digit.top() << endl;
digit.pop();//相当于清空栈
}
int main()
{
int n;
cout << "请输入表达式的个数:";
cin >> n;
while (n--) {
cout << "输入一个表达式:" << endl;
func();
}
cout << endl;
system("pause");
return 0;
}
代码实现: