火星计算器规则如下:M@N =(M-1)*(N+1);M#N =(2M+5)*(3N+60);M$N =(M+1)*(2*M+3)*(N-1)*(2N-3);
M&N =(M+7)/(N-8)+(M+N+1)*(N-9);
#include<iostream>
#include<cstring>
#include<cctype>
#include<stack>
using std::stack;
const int L=201;
int hflag =0;
stack<char> OPTR;//运算符栈
stack<int> OPND;//运算数栈
int ToOperand(char t[],int len)//字符转int型数字
{
int res=0;
for(int i=0;i<len;i++)
{
res=res*10+(t[i]-'0');
}
return res;
}
char Judge(char optr1,char optr2)
{//根据运算符的关系来决定下一步操作,注意运算符和运算数出栈时的先后顺序
if(optr1=='*' && optr2=='*')
return '=';
if(optr1=='*')
return '<';
if(optr2=='*')
return '>';
if(optr2=='&'||optr1=='@')
return '>';
if(optr2=='@'||optr1=='&')
return '<';
if(optr2=='$'||optr1=='#')
return '>';
if(optr2=='#'||optr1=='$')
return '<';
hflag=1;
return '>';
}
int Calculate(int operand1,char optr,int operand2)
{
if(optr=='@')
return (operand1-1)*(operand2+1);
if(optr=='#')
return (2*operand1+5)*(3*operand2+60);
if(optr=='$')
return (operand1+1)*(2*operand1+3)*(operand2-1)*(2*operand2-3);
if(optr=='&')
if((operand2-8)!=0)
{
return (operand1+7)/(operand2-8)+(operand1+operand2+1)*(operand2-9);
}
hflag=1;
return 0;
}
int main()
{
int i,j;
char optr1,optr2;
char s[L],t[L];
while(gets(s))//输入运算表达式
{
if(s[0]=='0' && s[1]=='\0')
break;
strcat(s,"*");
while(OPTR.empty()==false)//清空运算符栈
OPTR.pop();
while(OPND.empty()==false)//清空数字栈
OPND.pop();
OPTR.push('*');
for(i=0;s[i]!='\0';)
{
if(s[i]==' ')
{
i++;
continue;
}
if(isdigit(s[i]))//检查是否为阿拉伯数字
{
for(j=0;isdigit(s[i]);i++,j++)
t[j]=s[i];
int operand=ToOperand(t,j);//字符转int型数字
OPND.push(operand);
}
else if(s[i]=='@'||s[i]=='#'||s[i]=='$'||s[i]=='&'||s[i]=='*')
{
optr1=OPTR.top(),optr2=s[i];
//根据运算符的关系来决定下一步操作,注意运算符和运算数出栈时的先后顺序
switch(Judge(optr1,optr2))
{
case '<'://运算符优先级比较
OPTR.push(optr2);
i++;
break;
case '=':
i++;
break;
case '>':
OPTR.pop();
int operand2=OPND.top();
OPND.pop();
int operand1=OPND.top();
OPND.pop();
int result=Calculate(operand1,optr1,operand2);
OPND.push(result);
}
}else
{hflag=1;
break;
}
}
if(hflag==0)
{printf("%d\n",OPND.top());
}
}
return 0;
}