这题考查两个点
1、先将算术表达式转换成后缀表达式,转换原理可以参考这个博客,写得非常详细http://blog.youkuaiyun.com/antineutrino/article/details/6763722
2、再对后缀表达式这里算值。
小心:
这里输入必须要整行读入,因为表达式中可能会揉进一些空格和tab,所以使用cin读入会出错。
另外,混合使用cin和cin.getline时,使用cin之后,会在缓冲区留下一个换行,会被下面的cin.getline吃掉,所以第一个cin.getline无法读到正确的数据,必须中间执行一次cin.ignore(),清空缓冲区。
代码如下:
//by werflychen 2013-12-21
#include <iostream>
#include <string>
#include <stack>
using namespace std;
inline bool IsLegal(char ch)
{
if (ch == '+' || ch == '-' || ch == '*' || ch == '(' || ch == ')'
|| (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z')
|| (ch >= 'A' && ch <= 'Z'))
{
return true;
}
return false;
}
void change(char *inorder, char *postorder)
{
if (inorder == NULL)
{
return;
}
stack<char> sta_operator;
int j = 0;
char *p = inorder;
while('\0' != *p)
{
if (IsLegal(*p))
{
switch(*p)
{
case '+':
case '-':
{
//遇到sta_operator栈顶运算符相同或较高的情况下,将栈顶运算符先出栈压入postorder,再将自身入栈
if (!sta_operator.empty() && ('+' == sta_operator.top() || '-' == sta_operator.top() || '*' == sta_operator.top()))
{
char chtmp = sta_operator.top();
postorder[j++] = chtmp;
sta_operator.pop();
sta_operator.push(*p);
}
//其他情况都直接进sta_operator
else
{
sta_operator.push(*p);
}
break;
}
case '*':
{
if (!sta_operator.empty() && '*' == sta_operator.top())
{
char chtmp = sta_operator.top();
postorder[j++] = chtmp;
sta_operator.pop();
sta_operator.push(*p);
}
else
{
sta_operator.push(*p);
}
break;
}
case '(':
{
sta_operator.push(*p);
break;
}
case ')':
//将sta_operator栈顶开始弹栈,直至遇到'('
{
char ch = sta_operator.top();
bool flag = sta_operator.empty();
while (!sta_operator.empty())
{
if ('(' == sta_operator.top())
{
sta_operator.pop();
break;
}
else
{
char chtmp = sta_operator.top();
postorder[j++] = chtmp;
sta_operator.pop();
}
}
}
break;
default:
postorder[j++] = *p;
break;
}
}
++p;
}
if (!sta_operator.empty())
{
postorder[j++] = sta_operator.top();
}
}
inline int CAL(char chOperator, int n1, int n2)
{
int iRes = 0;
switch (chOperator)
{
case '+':
{
iRes = n1 + n2;
break;
}
case '-':
{
iRes = n1 - n2;
break;
}
case '*':
{
iRes = n1 * n2;
break;
}
}
return iRes;
}
bool CalResult(char *strPost, int &iRes)
{
if (NULL == strPost)
{
return false;
}
stack<int> sta_operator;
char *p = strPost;
while(*p != '\0')
{
switch (*p)
{
case '+':
case '-':
case '*':
{
if (!sta_operator.empty())
{
int n2 = sta_operator.top();
sta_operator.pop();
int n1 = 0;
if (!sta_operator.empty())
{
n1= sta_operator.top();
sta_operator.pop();
}
else
{
return false;
}
int iCurRes = CAL(*p, n1, n2);
sta_operator.push(iCurRes);
}
else
{
return false;
}
break;
}
default:
{
if (*p >= '0' && *p <= '9')
{
sta_operator.push((int)(*p - '0'));
}
else
{
sta_operator.push((int)*p);
}
break;
}
}
++p;
}
iRes = sta_operator.top();
return true;
}
int main()
{
// freopen("input.txt", "r", stdin);
char str1[100];
char strPost1[100];
char str2[100];
char strPost2[100];
int iRes1 = 0, iRes2 = 0;
int num = 0;
cin >> num;
cin.ignore();
for (int i = 0; i < num; ++i)
{
cin.getline(str1, 100, '\n');
cin.getline(str2, 100, '\n');
memset((void*)strPost1, 0, sizeof(strPost1));
memset((void*)strPost2, 0, sizeof(strPost2));
change(str1, strPost1);
change(str2, strPost2);
// cout << "strPost1 = " << strPost1 << endl;
// cout << "strPost2 = " << strPost2 << endl;
int b1 = CalResult(strPost1, iRes1);
int b2 = CalResult(strPost2, iRes2);
if (iRes1 == iRes2)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
// cout << "iRes1 = " << iRes1 << " b1 = " << b1 << endl;
// cout << "iRes2 = " << iRes2 << " b2 = " << b2 << endl;
}
//cout << endl;
return 0;
}