# 给一列算术表达式解析，并找到最终结果

255人阅读 评论(0)

1.输入：

2.输出：

#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <cctype>
#include <algorithm>
using namespace std;

const string op = "^*/%+-";

map <string, string> defines;

//0.判别循环与重复定义
string getPar(const string &s)
{
return !defines.count(s)? s: defines[s] = getPar(defines[s]);
}

//0.字符化为小写
void toLower(string &s)
{
for (int i = 0; i < s.length(); i++)
s[i] = tolower(s[i]);
}

//1.除法计算
int divid(int a, int b)
{
if (a == 0 || b == 0)
return 0;
bool minus = a < 0 ^ b < 0;
int res = abs(a) / abs(b);
return minus? -res: res;
}

//2.模运算
int mod(int a, int b)
{
if (a == 0 || b == 0)
return 0;
bool minus = a < 0 ^ b < 0;
int rem = abs(a) % abs(b);
return minus? -rem: rem;
}

//3.乘法计算
int toPower(int a, int p)
{
bool minus = a < 0 && p % 2;
a = abs(a);
int res = 1;
while (p)
{
if (p & 1) res = res * a;
p >>= 1;
a = a * a;
}
return minus? -res: res;
}

//4.递归求值过程
int Eval(const string &s, int l, int r);

int main()
{
freopen("plcool.in", "r", stdin);    //绑定*.in与stdin
freopen("plcool.out", "w", stdout);  //绑定*.out与stdout
string op, a, b;

while (cin >> op) //标准流读取一个对象（空格隔开）
if (op == "define")
{
cin >> a >> b;
toLower(a);
toLower(b);
//处理define
if (!defines.count(a) && getPar(b) != a) defines[a] = b;
}
else
{
getline(cin, a); //读取剩余的一行
for (int i = 0; i < a.length(); i++)
//擦除空格
if (a[i] == ' ')
{
a.erase(i, 1);
i--;
}
//处理eval
cout << Eval(a, 0, a.length() - 1) << endl;
}
return 0;
}
//----------------------------------------------------------
//4.求值过程(Eval递归）
int Eval(const string &s, int l, int r)
{
if (l > r) return 0;
// Searching non-unary +, -
int st = 0;
for (int i = r; i >= l; i--)
if (s[i] == ')') st++;
else if (s[i] == '(') st--;
else if (st == 0 && s[i] == '+' && i > l && op.find(s[i - 1]) == string::npos) return Eval(s, l, i - 1) + Eval(s, i + 1, r);
else if (st == 0 && s[i] == '-' && i > l && op.find(s[i - 1]) == string::npos) return Eval(s, l, i - 1) - Eval(s, i + 1, r);
// Searching *, /, %
st = 0;
for (int i = r; i >= l; i--)
if (s[i] == ')') st++;
else if (s[i] == '(') st--;
else if (st == 0 && s[i] == '*') return Eval(s, l, i - 1) * Eval(s, i + 1, r);
else if (st == 0 && s[i] == '/') return divid(Eval(s, l, i - 1), Eval(s, i + 1, r));
else if (st == 0 && s[i] == '%') return mod(Eval(s, l, i - 1), Eval(s, i + 1, r));
// Searching ^
st = 0;
for (int i = l; i <= r; i++)
if (s[i] == '(') st++;
else if (s[i] == ')') st--;
else if (st == 0 && s[i] == '^') return toPower(Eval(s, l, i - 1), Eval(s, i + 1, r));
// Searching unary +, -
if (s[l] == '+') return Eval(s, l + 1, r);
if (s[l] == '-') return -Eval(s, l + 1, r);
// ()
if (s[l] == '(' && s[r] == ')') return Eval(s, l + 1, r - 1);
// Variable or constant
string quer = s.substr(l, r - l + 1);
toLower(quer);
string par = getPar(quer);
if (!isdigit(par[0])) return 0;
stringstream ss(par);
int res;
ss >> res;
return res;
}

（3）格式化

#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <cctype>
#include <algorithm>
using namespace std;

const string op = "^*/%+-";

map <string, string> defines;

//0.判别循环与重复定义
string getPar(const string &s)
{
return !defines.count(s)? s: defines[s] = getPar(defines[s]);
}

//0.字符化为小写
void toLower(string &s)
{
for (int i = 0; i < s.length(); i++)
s[i] = tolower(s[i]);
}

//1.除法计算
int divid(int a, int b)
{
if (a == 0 || b == 0)
return 0;
bool minus = a < 0 ^ b < 0;
int res = abs(a) / abs(b);
return minus? -res: res;
}

//2.模运算
int mod(int a, int b)
{
if (a == 0 || b == 0)
return 0;
bool minus = a < 0 ^ b < 0;
int rem = abs(a) % abs(b);
return minus? -rem: rem;
}

//3.乘法计算
int toPower(int a, int p)
{
bool minus = a < 0 && p % 2;
a = abs(a);
int res = 1;
while (p)
{
if (p & 1) res = res * a;
p >>= 1;
a = a * a;
}
return minus? -res: res;
}

//4.递归求值过程
int Eval(const string &s, int l, int r);

int main()
{
freopen("plcool.in", "r", stdin);    //绑定*.in与stdin
freopen("plcool.out", "w", stdout);  //绑定*.out与stdout
string op, a, b;

while (cin >> op) //标准流读取一个对象（空格隔开）
if (op == "define")
{
cin >> a >> b;
toLower(a);
toLower(b);
//处理define
if (!defines.count(a) && getPar(b) != a) defines[a] = b;
}
else
{
getline(cin, a); //读取剩余的一行
for (int i = 0; i < a.length(); i++)
//擦除空格
if (a[i] == ' ')
{
a.erase(i, 1);
i--;
}
//处理eval
cout << Eval(a, 0, a.length() - 1) << endl;
}
return 0;
}
//----------------------------------------------------------
//4.求值过程(Eval递归）
int Eval(const string &s, int l, int r)
{
if (l > r) return 0;
// Searching non-unary +, -
int st = 0;
for (int i = r; i >= l; i--)
if (s[i] == ')') st++;
else if (s[i] == '(') st--;
else if (st == 0 && s[i] == '+' && i > l && op.find(s[i - 1]) == string::npos) return Eval(s, l, i - 1) + Eval(s, i + 1, r);
else if (st == 0 && s[i] == '-' && i > l && op.find(s[i - 1]) == string::npos) return Eval(s, l, i - 1) - Eval(s, i + 1, r);
// Searching *, /, %
st = 0;
for (int i = r; i >= l; i--)
if (s[i] == ')') st++;
else if (s[i] == '(') st--;
else if (st == 0 && s[i] == '*') return Eval(s, l, i - 1) * Eval(s, i + 1, r);
else if (st == 0 && s[i] == '/') return divid(Eval(s, l, i - 1), Eval(s, i + 1, r));
else if (st == 0 && s[i] == '%') return mod(Eval(s, l, i - 1), Eval(s, i + 1, r));
// Searching ^
st = 0;
for (int i = l; i <= r; i++)
if (s[i] == '(') st++;
else if (s[i] == ')') st--;
else if (st == 0 && s[i] == '^') return toPower(Eval(s, l, i - 1), Eval(s, i + 1, r));
// Searching unary +, -
if (s[l] == '+') return Eval(s, l + 1, r);
if (s[l] == '-') return -Eval(s, l + 1, r);
// ()
if (s[l] == '(' && s[r] == ')') return Eval(s, l + 1, r - 1);
// Variable or constant
string quer = s.substr(l, r - l + 1);
toLower(quer);
string par = getPar(quer);
if (!isdigit(par[0])) return 0;
stringstream ss(par);
int res;
ss >> res;
return res;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：24028次
• 积分：701
• 等级：
• 排名：千里之外
• 原创：46篇
• 转载：14篇
• 译文：0篇
• 评论：1条
阅读排行
评论排行
最新评论