这是我2018年初上计算概论写的大作业。现在回看,哎。。当时怎么写成这样=__= 这当作一个黑历史吧,警醒我代码也能丑成这样。
题目是让我们写一个能实现赋值运算和四则运算的计算器,包括一些难的数学公式。
>>x=3+3*7
>>>24
>>y+3
>>>error
>>cos(3*9-27)/2+4
>>>4.5
>>2333333334/(8-9+1)
>>>error (除数为0)
>>x=x+3
>>>27
>>y=90
>>x+y
>>>117
>>2^10
>>>1024
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<stack>
#include<algorithm>
#include<sstream>
#include<windows.h>
using namespace std;
HANDLE hout;
#define M_PI 3.14159265358979323846
#define maxnum 12
string keys[maxnum];
string values[maxnum];
string keys1[7] = { "arcsin","arccos","arctan","sqrt" ,"sin","cos","tan" };
string values1[7] = { "%","`","?","~" , "!","@","$" };
int assignmentstring = 0;//变量的总个数
int len;
char Prior[17][17] = // 运算符优先级表
{
// '+' '-' '*' '/' '=' '(' ')' '#' '^','sin','cos','tan','asin','acos','atan','sqrt'
{ '>', '>', '>', '>', '>', '<','<', '<', '<' ,'>','>','>'},
/*'+'*/{ '>', '>', '>', '<', '<', '<','<', '>', '>','<','<','<','<','<','<','<' },
/*'-'*/{ '>', '>', '>', '<', '<','<', '<', '>', '>','<','<','<','<','<','<','<' },
/*'*'*/{ '>', '>', '>', '>', '>','<', '<', '>', '>','<','<','<','<','<','<','<' },
/*'/'*/{ '>', '>', '>', '>', '>', '<','<', '>', '>' ,'<','<','<','<','<','<','<'},
/*'='*/{ '>', '>', '>', '>', '>', '>','>', '>', '>' ,'>', '>', '>','>', '>', '>' ,'>'},
/*'('*/{ '>', '<', '<', '<', '<', '<','<', '=', '<','<', '<', '<', '<', '<', '<', '<','<'},
/*')'*/{ '>', '>', '>', '>', '>', '<',' ', '>', '>', '>', '>', '>', '>', '>', '>', '>','>'},
/*'#'*/{ '>', '<', '<', '<', '<', '<','<', '>', '=','<' , '<', '<', '<', '<', '<','<'},
/*'^'*/{ '>', '>', '>', '>', '>','<', '<', '>', '>','>', '>','>','>','>','>','>'},
/*'sin'*/{ '>', '>', '>', '>', '>','<', '<', '>', '>','<', '>', '>','>', '>', '>' ,'>' },
/*"cos"*/{ '>', '>', '>', '>', '>','<', '<', '>', '>','<' , '>', '>','>', '>', '>' ,'>'},
/*tan */ { '>', '>', '>', '>', '>','<', '<', '>', '>','<', '>', '>','>', '>', '>' ,'>' },
/*asin*/ { '>', '>', '>', '>', '>','<', '<', '>', '>','<', '>', '>','>', '>', '>' ,'>' },
/*acos*/ { '>', '>', '>', '>', '>','<', '<', '>', '>','<' , '>', '>','>', '>', '>' ,'>'},
/*atan*/{ '>', '>', '>', '>', '>','<', '<', '>', '>','<', '>', '>','>', '>', '>' ,'>' },
/*sqrt*/{ '>', '>', '>', '>', '>','<', '<', '>', '>','<', '>', '>','>', '>', '>' ,'>' },
};
int judge(char c)
{
switch (c)
{
case'+':return 1;
case'-':return 2;
case'*':return 3;
case'/':return 4;
case'=':return 5;
case'(':return 6;
case')':return 7;
case'#':return 8;
case'^':return 9;
case'!':return 10;
case'@':return 11;
case'$':return 12;
case'%':return 13;
case'?':return 14;
case'~':return 15;
case'`':return 16;
default:return 0;
}
}
//优先级比较
char precede(char m, char n)
{
int a, b;
a = judge(m);
b = judge(n);
return Prior[a][b];
}
//括号匹配
bool kuohao_pipei(string &exp1)
{
int leftt = 0, len = exp1.length();
for (int i = 0; i < len; i++)
{
if (exp1[i] == '(')
{
leftt++;
}
if (exp1[i] == ')')
{
leftt--;
}
if (leftt < 0)
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "error" << endl;
return false;
}
}
if (leftt != 0)
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "error" << endl;
return false;
}
else
return true;
}
//符号计算
long double mul(long double a, long double b, char c)
{
switch (c)
{
case '+':
return b + a;
case '-':
return b - a;
case '*':
return b * a;
case '/':
return b / a;
case'^':
return pow(b, a);
//'sin', 'cos', 'tan', 'asin', 'acos', 'atan'
case'!':return sin(a);
case'@':return cos(a);
case'$':return tan(a);
case'%':return asin(a);
case'`':return acos(a);
case'?':return atan(a);
case'~':return sqrt(a);
default:
return -1;
}
}
//判断是否是运算符
bool Comop(char ch)
{
switch (ch)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
case'#':
case'^':
case'~':
case'!':
case'@':
case'$':
case'%':
case'`':
case'?':
return true;
default:
return false;
}
}//判断ch是否为运算符
//中缀表达式转换后缀表达式
void TransmitExpression(string &exp1, string &posexp1)
{
stack<char>q2;
int i = 0;
q2.push('#');
len = exp1.length();
while (i < len)
{
if (!Comop(exp1[i]))
{
string a = "3";
while ((exp1[i] >= '0'&&exp1[i] <= '9') || exp1[i] == '.')//读取一个数字串
{
a[0] = exp1[i++];
posexp1 += a;
}
posexp1 += "#";
}
else
{
switch (precede(q2.top(), exp1[i]))
{
case'<':
q2.push(exp1[i]);
i++;
break;
case'=':
q2.pop();
i++;
break;
case'>':
string a = "3";
a[0] = q2.top();
posexp1 += a;
q2.pop();
break;
}
}
}
while (q2.top() != '#')
{
string a = "3";
a[0] = q2.top();
posexp1 += a;
q2.pop();
}
}
//后缀表达式的计算
long double EvaluateExpression(string &posexp1)
{
stack<long double>q1;
long double a, b, d;
int kk = posexp1.length();
int i = 0;
while (i < kk)
{
switch (posexp1[i])
{
case'+':
case'-':
case'*':
case'/':
case'^':
case'~':
case'!':
case'@':
case'$':
case'%':
case'`':
case'?':
a = q1.top();
q1.pop();
b = q1.top();
q1.pop();
q1.push(mul(a, b, posexp1[i]));
break;
default:
d = 0;
int zz = 1;
long double ss = 0.1;
while ((posexp1[i] >= '0'&&posexp1[i] <= '9') || (posexp1[i] == '.'))//计算数值
{
if (posexp1[i] == '.')
{
zz = 0;
i++;
continue;
}
if (zz == 1) {
d = 10 * d + posexp1[i] - '0';
}
if (zz == 0)
{
d += ss * (posexp1[i] - '0');
ss *= 0.1;
}
i++;
}
q1.push(d);
break;
}
i++;
}
return q1.top();
}
string& replace_all_distinct(string& str, const string& old_value, const string& new_value)
{
for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
if ((pos = str.find(old_value, pos)) != string::npos)
str.replace(pos, old_value.length(), new_value);
else break;
}
return str;
}
void replace_variables(string &exp1)
{
string aa = "(";
string bb = ")";
for (int i = 0; i < assignmentstring; i++)
if (exp1.find(keys[i]) != string::npos)
{
exp1 = replace_all_distinct(exp1, keys[i], aa + values[i] + bb);
}
for (int i = 0; i < 7; i++)
{
if (exp1.find(keys1[i]) != string::npos)
{
exp1 = replace_all_distinct(exp1, keys1[i], values1[i]);
}
}
}
bool error(string &exp1)
{
int k, i = 0;
int tt = exp1.length();
while (i < tt)
{
k = judge(exp1[i]);
if (
(((k >= 1 && k <= 5) || exp1[i] == '.'||(k>=9&&k<=15)) && (exp1[i + 1] == ')'))
|| (((k > 2 && k <= 5) || exp1[i] == '.' || k == 1|| (k >= 9 && k <= 16)) && (exp1[i - 1] == '('))//(-1*2)这样是正确的
|| (exp1[i] == ')'&&exp1[i + 1] == '(')
|| (exp1[i] == '('&&exp1[i + 1] == ')')
|| ((exp1[i] == ')') && exp1[i + 1] >= '0'&& exp1[i + 1] <= '9')
|| (exp1[i] >= '0'&&exp1[i] <= '9'&&exp1[i + 1] == '(')
|| (exp1[0] == '+' || exp1[0] == '*' || exp1[0] == '/' || exp1[0] == ')' || exp1[0] == '='||
exp1[0] == '^'||exp1[0]=='~'|| exp1[0] == '~' || exp1[0] == '!' || exp1[0] == '@' ||
exp1[0] == '$' || exp1[0] == '%' || exp1[0] == ']' || exp1[0] == '?')
|| ((exp1[i] == '+' || exp1[i] == '-' || exp1[i] == '*' || exp1[i] == '/' || exp1[i] == '='||exp1[i] == '^' || exp1[i] == '~' || exp1[i] == '~' || exp1[i] == '!' || exp1[i] == '@' ||
exp1[i] == '$' || exp1[i] == '%' || exp1[i] == '`' || exp1[i] == '?') && (exp1[i + 1] == '+' || exp1[i + 1] == '*' || exp1[i + 1] == '/' || exp1[i + 1] == '-' || exp1[i+1] == '='|| exp1[i+1] == '^' || exp1[i+1] == '~' || exp1[i+1] == '~' || exp1[i+1] == '!' || exp1[i+1] == '@' ||
exp1[i+1] == '$' || exp1[i+1] == '%' || exp1[i+1] == '`' || exp1[i+1] == '?'))
|| (exp1[i] == '/'&&exp1[i + 1] == '0')
|| ((exp1[i] == '+' || exp1[i] == '-' || exp1[i] == '*' || exp1[i] == '/'|| exp1[i] == '^') && (exp1[i + 1] == '+' || exp1[i + 1] == '*' || exp1[i + 1] == '/' || exp1[i + 1] == '='|| exp1[i+1] == '^'))
|| (exp1[i] == '.' && (exp1[i + 1]<'0' || exp1[i + 1]>'9'))
|| (exp1[i] == '.' && (exp1[i - 1]<'0' || exp1[i - 1]>'9'))
||(i==exp1.length()-1&&(exp1[i] == '+' || exp1[i] == '-' || exp1[i] == '*' || exp1[i] == '/' || exp1[i] == '='||exp1[i] == '^' || exp1[i] == '~' || exp1[i] == '~' || exp1[i] == '!' || exp1[i] == '@' ||
exp1[i] == '$' || exp1[i] == '%' || exp1[i] == '`' || exp1[i] == '?'))
)
return false;
else {
if (i == len)
return true;
}
i++;
}
return true;
}
int judge1(char a)
{
if (a == '_' || (a >= '0'&&a <= '9') || (a >= 'a'&&a <= 'z') || (a >= 'A'&&a <= 'Z'))return 1;
return 0;
}
bool judge_name(string &a)
{
int k, i, flag = 0;
k = a.length();
if (a[0] >= '0'&& a[0] <= '9')return false;//首字母不是数字
for (i = 0; i < k; i++)
{
if (flag == 0 && (a[i]<'0' || a[i]>'9')) flag = 1;//保证不全是数字
if (a[i] == '_' || (a[i] >= '0'&&a[i] <= '9') || (a[i] >= 'a'&&a[i] <= 'z') || (a[i] >= 'A'&&a[i] <= 'Z'))
continue;
else
return false;
}
if (flag == 0)return false;
return true;
}
bool judge_value(string &b)
{
int k = b.length();
int t = 0;
if (b[0] == '-')
{
t = 1;
if (b[1] == '.' || b[k - 1] == '.')
return false;
}
if (b[t] == '.' || b[k - 1] == '.')
return false;
int num = 0;
for (int i = t; i < k; i++)
{
if (b[i] == '.')num++;
if (b[i] >= '0'&&b[i] <= '9' || b[i] == '.')
continue;
return false;
}
if (num >= 2)return false;
return true;
}
long double turnnum(string aa)
{
long double d = 0, ss = 0.1;
int i = 0, zz = 1, mm = 1;
int length1 = aa.length();
if (aa[0] == '-')
{
i = 1;
mm = -1;
}
if (aa[0] == '+')
{
i = 1;
mm = 1;
}
while (i < length1)
{
if (aa[i] == '.')
{
zz = 0;
i++;
continue;
}
if (zz == 1) {
d = 10 * d + (aa[i] - '0');
}
if (zz == 0)
{
d += ss * (aa[i] - '0');
ss *= 0.1;
}
i++;
}
return d * mm;
}
bool judge_othervarialbes(string &exp1)
{
int k = exp1.length();
for (int i = 0; i < k; i++)
{
if ((exp1[i] >= '0' && exp1[i] <= '9') || (judge(exp1[i]) >= 1 && judge(exp1[i]) <= 7) || exp1[i] == '.'|| exp1[i] == '^' || exp1[i] == '~' || exp1[i] == '!' || exp1[i] == '@' ||
exp1[i] == '$' || exp1[i] == '%' || exp1[i] == '`' || exp1[i] == '?')
continue;
else
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "error1" << endl;
return false;
}
}
return true;
}
string doubleToString(long double num)
{
ostringstream out;
int prec = 100;
out.precision(prec);
out << num;
string str = out.str();
size_t n = str.find('.');
if (n != string::npos&&str.size() > n + 50)
{
str[n + 50] = '\0';
}
string temp = str.c_str();
str = temp;
return str;
}
int judge4(char c)
{
switch (c)
{
case'+':return 1;
case'-':return 2;
case'*':return 3;
case'/':return 4;
case'=':return 5;
default:return 0;
}
}
void mysorted()
{
for (int i = 0; i < assignmentstring - 1; i++)
{
for (int j = 0; j < assignmentstring - 1 - i; j++)
{
if (keys[j].length() < keys[j + 1].length())
{
swap(keys[j], keys[j + 1]);
swap(values[j], values[j + 1]);
}
}
}
}
void change(string &aa)
{
int k = aa.length();
int i = 0;
while (i < k)
{
if (aa[i] == '-'&&i == 0)
{
aa.insert(aa.begin(), '0');
k++;
}
else if (i < k - 1 && aa[i] == '('&&aa[i + 1] == '-')
{
aa.insert(aa.begin() + (i + 1), '0');
k++;
}
i++;
}
}
int insertdd(int x,string &aa)
{
int k = 0;
for (int i = x; i < aa.length(); i++)
{
if (aa[i] == '(')k++;
if ((k == 0 && judge4(aa[i])) || (aa[i] == ')'&&k == 1))return i;
if (aa[i] == ')')k--;
}
return-1;
}
void changesin(string &aa)
{
int k = aa.length();
int i = 0;
while (i < k)
{
if ((aa[i] == '~'||aa[i]=='!'||aa[i]=='$'||aa[i]=='%'||aa[i]=='`'||aa[i]=='?'||aa[i]=='@'))
{
aa.insert(aa.begin() +i, '(');
k++;
aa.insert(aa.begin() +i + 1, '0');
k++;
int tt = insertdd(i + 3, aa);
if (tt != -1)
{
aa.insert(aa.begin() + tt, ')');
}
if (tt == -1)
{
aa += ")";
}
k++;
i = i + 3;
continue;
}
i++;
}
}
bool panpan(string &exp)//判断赋值语句是否有多个=
{
int cnt = 0, len = exp.length();
for (int i = 0; i < len; i++)
{
if (exp[i] == '=')
cnt++;
}
if (cnt == 1)
return true;
else
return false;
}
bool zero(string &b)
{
int kk = 0;
for (int i = 0; i < b.length(); i++)
{
if (b[i] == '/'&&b[i + 1] == '(')
{
kk++;
for (int k = i + 2; k < b.length(); k++)
{
if (b[k] == ')')
{
kk--;
if (kk == 0)
{
string aa = b.substr(i + 1, k);
string bbb;
TransmitExpression(aa, bbb);
long double c = EvaluateExpression(bbb);
if (c == 0)return false;
i = k + 1;
break;
}
}
}
}
}
return true;
}
bool assignment(string &exp)//赋值语句
{
len = exp.length();
if (exp.find("=") == string::npos)return true;
if (!panpan(exp))
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "error2" << endl;
return false;
}
int pos = exp.find("=");
string a = exp.substr(0, pos);
string b = exp.substr(pos + 1, len - 1);
replace_variables(b);//替换变量
changesin(b);
if (!judge_othervarialbes(b))
{
return false;
}
if (judge_value(b))
{
for (int i = 0; i < assignmentstring; i++)
{
if (keys[i] == a)
{
values[i] = b;
long double c = turnnum(b);
if (abs(long long(c) - c) < 1e-6)
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << long long(c) << endl;
}
else
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << setprecision(2) << c << endl;
}
return false;
}
}//如果a不在key中看a是否合法
if (judge_name(a))
{
keys[assignmentstring] = a;
values[assignmentstring] = b;
long double c = turnnum(b);
if (abs(long long(c) - c) < 1e-6)
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << long long(c) << endl;
}
else
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << fixed << setprecision(2) << c << endl;
}
assignmentstring++;
mysorted();
return false;
}
else
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "error3" << endl;
return false;
}
}
//如果以上不成立,那么b又是另一个赋值运算
if (!error(b))
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "error4" << endl;
return false;
}
else //如果他是一个正确的表达式
{
change(b);
if (!zero(b))
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "error5" << endl;
return false;
}
string mm;
TransmitExpression(b, mm);
long double c = EvaluateExpression(mm);
b = doubleToString(c);
for (int i = 0; i < assignmentstring; i++)
{
if (keys[i] == a)
{
values[i] = b;
if (abs(long long (c)-c)<1e-6)
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << long long(c) << endl;
}
else
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << fixed << setprecision(2) << c << endl;
}
return false;
}
}
}
if (judge_name(a))
{
keys[assignmentstring] = a;
values[assignmentstring] = b;
long double c = turnnum(b);
if (abs(long long(c)-c)<1e-6)
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout <<long long( c) << endl;
}
else
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << fixed << setprecision(2) << c << endl;
}
assignmentstring++;
mysorted();
return false;
}
else
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "error6" << endl;
return false;
}
}
void trim(string &exp)//去空格
{
if (!exp.empty())
{
exp.erase(0, exp.find_first_not_of(" "));
exp.erase(exp.find_last_not_of(" ") + 1);
}
int index = 0;
if (!exp.empty())
{
while ((index = exp.find(' ', index)) != string::npos)
{
exp.erase(index, 1);
}
}
}
bool judge_blank(string &exp)
{
for (int i = 0; i < len; i++)//判断是不是出现5 6*8类似这样的情况
{
if (i + 2 < len && judge1(exp[i]) && exp[i + 1] == ' '&& judge1(exp[i + 2]))
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "error7" << endl;
return false;
}
}
trim(exp);
len = exp.length();
return true;
}
int insertkuohao(int x, string &exp1)
{
int k = 0;
for (int i = x; i < exp1.length(); i++)
{
if (exp1[i] == '(')k++;
if ((k == 0 && judge4(exp1[i])) || (exp1[i] == ')'&&k == 1))return i;
if (exp1[i] == ')')k--;
}
return -1;
}
void change1(string &exp1)//保障负数的正确运算
{
int kk = exp1.length();
for (int i = 1; i < kk; i++)
{
if (judge4(exp1[i]) == 2 && (judge4(exp1[i - 1]) != 2 && judge4(exp1[i - 1]) != 0))
{
exp1.insert(exp1.begin() + i, '(');
int k = insertkuohao(i + 2, exp1);
if (k != -1)
{
exp1.insert(exp1.begin() + k, ')');
}
if (k == -1)
{
exp1 += ')';
}
kk++;
}
}
}
int main()
{
hout = GetStdHandle(STD_OUTPUT_HANDLE);
while (1)
{
string exp;
string posexp;
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);//绿色的箭头
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY);//输入为原来的颜色
getline(cin, exp);
if (exp == "exit")
return 0;
len = exp.length();
if (exp[len - 1] == ';') {
exp = exp.substr(0, len - 1);
}
len = exp.length();
if (!judge_blank(exp)) continue;//判断出现空格的地方是否错误
if (!kuohao_pipei(exp)) continue;//判断是否括号匹配
change1(exp);//保证负数运算的正确性
if (!assignment(exp)) continue;//判断并处理赋值语句
replace_variables(exp);//替换变量
changesin(exp);
len= exp.length();
if (!judge_othervarialbes(exp))continue;//判断还有其他未出现的变量
if (error(exp))
{
change(exp);
len = exp.length();
if (!zero(exp))
{
cout << exp << endl;
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "error8" << endl;
continue;
}
len=exp.length();
TransmitExpression(exp, posexp);
long double c = EvaluateExpression(posexp);
if (abs(long long(c) - c) < 1e-6)
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << long long(c) << endl;
}
else
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << fixed << setprecision(2) << c << endl;
}
}
else
{
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << ">>";
SetConsoleTextAttribute(hout, FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "error9" << endl;
}
}
return 0;
}