火星计算器


 火星计算器规则如下: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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值