#include <iostream>
#include <string>
using namespace std;
bool tt;
double calculate(string a) // 计算除去()的式子
{
double x[1001];
int y[1000]; // x存数字,y存运算符号
bool t = false; // 接来下的数字是否为小数点后
int t0 = 0; // 计算小数点后第几位
for (int i = 0; i < 101; i++) // 初始化数组
{
x[i] = 0.0;
}
int yy = 0; // 计算运算符号个数
for (int i = 0; i < (int)a.length(); i++)
{
if (a[i] == 43) // +
{
y[yy] = 1;
yy++;
t = false;
}
else if (a[i] == 45) // -
{
y[yy] = 2;
yy++;
t = false;
}
else if (a[i] == 42) // *
{
y[yy] = 3;
yy++;
t = false;
}
else if (a[i] == 47) // /
{
y[yy] = 4;
yy++;
t = false;
}
else if (a[i] == 46) // 小数点
{
t = true;
t0 = 0; // 重置t0
}
else // 提取数字
{
if (t)
{
double u = (double)a[i] - 48;
for (int k = 0; k <= t0; k++)
{
u /= 10;
}
x[i] += u;
t0++;
}
else
{
x[yy] = x[yy] * 10 + a[i] - 48;
}
}
}
for (int i = 0; i < yy; i++) // 先算*和/
{
if (y[i] == 3)
{
x[i] = x[i] * x[i + 1];
for (int k = i + 1; k < yy; k++)
{
y[k - 1] = y[k];
x[k] = x[k + 1];
}
yy--;
i--;
continue;
}
else if (y[i] == 4)
{
if (x[i + 1] == 0)
{
tt = true;
return 0.0;
}
x[i] = x[i] / x[i + 1];
for (int k = i + 1; k < yy; k++)
{
y[k - 1] = y[k];
x[k] = x[k + 1];
}
yy--;
i--;
continue;
}
}
for (int i = 0; i < yy; i++) // 计算+和-
{
if (y[i] == 1)
{
x[i] = x[i] + x[i + 1];
for (int k = i + 1; k < yy; k++)
{
y[k - 1] = y[k];
x[k] = x[k + 1];
}
yy--;
i--;
continue;
}
if (y[i] == 2)
{
x[i] = x[i] - x[i + 1];
for (int k = i + 1; k < yy; k++)
{
y[k - 1] = y[k];
x[k] = x[k + 1];
}
yy--;
i--;
continue;
}
}
return x[0];
}
int main()
{
tt = false;
cout << "C\t(\t)\t/\n\n7\t8\t9\t*\n\n4\t5\t6\t-\n\n1\t2\t3\t+\n\n \t0\t.\t=\n" << endl;
cout << "欢迎使用cw自制计算器\n请输入宁要计算的式子:" << endl;
string a;
for (;a.find("=") == string::npos ; ) // 输入式子
{
string b;
cin >> b;
a = a + b;
}
if (a.find("=") != a.length() - 1) // =不在最后
{
cout << "数学错误!" << endl;
return 0;
}
if (((a[0] > 41) && (a[0] < 48)) || ((a[a.length() - 2] > 41) && (a[a.length() - 2] < 48)))
{
cout << "数学错误!" << endl;
return 0;
}
for (int i = 0; i < a.length() - 1; i++)
{
if (((((a[i] == 42) || (a[i] == 43)) || ((a[i] == 45) || (a[i] == 47))) || (a[i] == 40)) && ((a[i + 1] == 41) || (((a[i + 1] == 42) || (a[i + 1] == 43)) || ((a[i + 1] == 45) || (a[i + 1] == 47)))))
{
cout << "数学错误!" << endl;
return 0;
}
if ((a[i] == 41) && (a[i + 1] == 40))
{
cout << "数学错误!" << endl;
return 0;
}
if (((a[i] == 46) && (a[i + 1]) < 48) || ((a[i] < 48) && (a[i + 1] == 46)))
{
cout << "数学错误!" << endl;
return 0;
}
}
int pos = -1; // (位置
a.erase(a.length() - 1); // 除去=
for (; a.find('(') != string::npos; ) // 运算括号内内容
{
if (a.find(')', pos + 1) == string::npos)
{
cout << "数学错误!" << endl;
return 0;
}
if (a.find('(', pos + 1) < a.find(')', pos + 1))
{
pos = a.find('(', pos + 1);
}
else
{
if (pos == -1)
{
cout << "数学错误!" << endl;
return 0;
}
double r = calculate(a.substr(pos + 1, a.find(')', pos + 1) - pos - 1));
string q = a.substr(0, pos);
string w = a.substr(a.find(')', pos + 1) + 1);
a = q + to_string(r) + w;
pos = -1;
}
}
if (a.find(')') != string::npos)
{
cout << "数学错误!" << endl;
return 0;
}
double r = calculate(a); // 没有括号时运算式子
if (tt) // 数学错误
{
cout << "数学错误!" << endl;
return 0;
}
if (r - (int)r == 0)
{
cout << (int)r;
}
else
{
cout << r;
}
}