设计并实现一个表达式求值的大作业。
提交材料:源程序、大作业报告
满分:25分
评分依据:程序运行结果(15分)、大作业报告(10分)
根据程序运行结果,满分15分,分四个等级。实现一位数的后缀表达式求值(10分)、多位(包括小数)后缀表达式(12分)、一般的中缀表达式求值(包括+、-、*、/、())(13分)、任意多位中缀表达式求值(包括+、-、*、/、()、负数、除数为0)(15分)。中缀直接求值,还是先转后缀再求值,由学生自己设计,等级没区别。
大作业报告,满分10分,其中分析设计(3分),算法设计(3分),程序运行结果(2分),大作业总结(2分)
#include<bits/stdc++.h>
using namespace std;
int dengji[128];
void Specialmode()
{
cout << "——这里是高级模式——" << endl;
cout << "——支持绝大多数的数字计算——" << endl << endl;
cout << "格式要求:" << endl;
cout << "1.不支持空格的输入" << endl;
cout << "2.若数字为负数需要把负数加上小括号(),队首除外" << endl << endl;
cout << "__请输入一个中缀表达式以计算其值__" <<endl;
dengji['+'] = 1; dengji['-'] = 1;
dengji['*'] = 2; dengji['/'] = 2;
dengji['('] = 3; dengji[')'] = 3;
string str;//初始数据的输入
stack <char> k;//用于中缀转换后缀的储存操作符号的栈
stack <double> dre;//用于储存后缀表达式中的计算数,以进行最后计算
queue <string> ans;//用于中缀转换后缀时储存操作数
int i = 0, start = 0, flag = 0;
cin >> str;
while (i < str.size())
{
if (flag == 1 || str[0] == '-') i++;//i不加的数字写入操作就不会进行
while (i < str.size() && str[i] >= '0' && str[i] <= '9' || str[i] == '.') i++;//非运算符号,为了写入一个长有效数字
if (i > start)
{
ans.push(str.substr(start, i - start));
flag = 0;
}
if (dengji[str[i]])//运算符号
{
if (str[i] == '(')
flag = 1;
if (k.empty())
k.push(str[i]);
else
{
if (str[i] == ')')
{
while (!k.empty() && k.top() != '(')
{
string temp = "";
temp.push_back(k.top());
ans.push(temp);
k.pop();
}
k.pop();
}
else if (k.top() == '(' || dengji[k.top()] < dengji[str[i]])//加减乘除左括号
k.push(str[i]);
else
{
while (!k.empty() && k.top() != '(' && dengji[k.top()] >= dengji[str[i]])
{
string temp = "";
temp.push_back(k.top());
ans.push(temp);
k.pop();
}
k.push(str[i]);
}
}
}
i++;
start = i;
}
while (!k.empty())
{
string temp = "";
temp.push_back(k.top());
ans.push(temp);
k.pop();
}
while (!ans.empty())
{
string t = ans.front();
double sum = 0;
int f2 = 1, f3 = -999, h;
if (t == "+" || t == "-" || t == "*" || t == "/")//计算
{
double top1 = 0, top2 = 0;
if (t == "+")
{
top1 = dre.top();
dre.pop();
top2 = dre.top();
dre.pop();
dre.push(top2 + top1);
}
if (t == "-")
{
top1 = dre.top();
dre.pop();
top2 = dre.top();
dre.pop();
dre.push(top2 - top1);
}
if (t == "*")
{
top1 = dre.top();
dre.pop();
top2 = dre.top();
dre.pop();
dre.push(top2 * top1);
}
if (t == "/")
{
top1 = dre.top();
dre.pop();
top2 = dre.top();
dre.pop();
if (top1 == 0)
{
cout << "计算中出现了除数为0的情况!该步计算结果记0"<<endl;
dre.push(0);
}
else
dre.push(top2 / top1);
}
ans.pop();
}
else//把字符串形式的操作数字转化为可计算的数字
{
for (int i = 0; i < t.size(); i++)
{
if (t[i] == '-')
{
f2 = -1;
}
else if (t[i] == '.')
{
f3 = i;
sum /= 10;
}
else
{
h = (t[i] - 48) * pow(10, (t.size() - 1 - i));
sum += h;
}
}
if (f3 != -999)
sum = sum * pow(0.1, (t.size() - 1 - f3));//小数点处理
sum *= f2;//正负号处理
dre.push(sum);
ans.pop();
}
}
cout << "输入表达式为:" << str << endl;
cout << "计算结果为:" << dre.top() << endl << endl;
system("pause");
system("cls");
}
int main()
{
for (int i = 0; i < 100; i++)
{
int key;
cout << "******************************************" << endl;
cout << "*** 欢迎使用本计算系统 ***" << endl;
cout << "*** 1.高级模式 ***" << endl;
cout << "*** 2.退出 ***" << endl;
cout << "******************************************" << endl;
cout << "请输入对应的编号以继续__" << endl;
cin >> key;
system("cls");
if (key == 1)
Specialmode();
if (key == 2)
{
cout << endl;
cout<< "本次旅程到此结束,感谢你的使用!" << endl;
float x, y, p;
for (y = 1.3; y > -1.3; y -= 0.1)
{
for (x = -1.5; x < 1.5; x += 0.04)
{
p = x * x + y * y - 1;
putchar(p * p * p - x * x * y * y * y <= 0.0 ? '*' : ' ');
}
system("color 0c");
putchar('\n');
}
break;
}
}
return 0;
}