第八届河南省赛试题挑战密室表达式求值类_NYOJ1236

原题地址:点击打开链接

#include<stdio.h>
#include<string.h>
#include<stack>
#define MAX 1<<31-1
using namespace std;
int a[256];
int jisuan(int i,int b)
{
	return a[i]*b;
}
int main()
{
	int k,len,i,res,count;
	char str[500];
	a['N']=14;a['C']=12;a['O']=16;a['C'+'l'*2]=35;a['S']=32;a['H']=2;a['A'+'l'*2]=27;a['C'+'a'*2]=40;//为什么要乘2?,因为 'C'+'l'=='N'+'a',好吧。。
	a['Z'+'n'*2]=65; a['N'+'a'*2]=23;
	scanf("%d",&k);
	while(k--)
	{
		res=0;
		count=1;
		stack<int>s;
		scanf("%s",str);
		len=strlen(str);
		for(i=0;i<len;i++)
			if(str[i]=='=')
				break;
			i++;
			if(str[i]>='0'&&str[i]<='9')
				count=str[i++]-'0';
		while(str[i]!='+'&&str[i]!='\0')
		{
			if(str[i]>='A'&&str[i]<='Z'&&str[i+1]>='a'&&str[i+1]<='z')
			{
				if(str[i+2]>='0'&&str[i+2]<='9')
				{
					int res=jisuan(str[i+1]*2+str[i],str[i+2]-'0');
					s.push(res);
					i+=3;
				}
				else
				{
					s.push(a[str[i+1]*2+str[i]]);
					i+=2;
				}
			}
			else if(str[i]>='A'&&str[i]<='Z')
			{
				if(str[i+1]>='0'&&str[i+1]<='9')
				{
					int res=jisuan(str[i],str[i+1]-'0');
					s.push(res);
					i+=2;
				}
				else
				{
					s.push(a[str[i]]);
					i+=1;
				}
			}
			else if(str[i]=='(')
			{
				i++;
				s.push(MAX);
			

			}
			else if(str[i]==')')
			{
				int sum=0;
				while(s.top()!=MAX)
				{
					sum+=s.top();
					s.pop();
				}
				s.pop();
				i++;
				if(str[i]>='0'&&str[i]<='9')
				{
					s.push(sum*(str[i]-'0'));
					i++;
				}
				else
				{
					s.push(sum);
				}
			}
		}
		while(!s.empty())
		{
			res+=s.top();
			s.pop();
		}
		if(res<10)
			printf("0");
		if(res<100)
			printf("0");
		if(res<1000)
			printf("0");
		printf("%d\n",res*count);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值