nyoj305表达式求值 栈的应用

感觉和nyoj02括号配对很像。。。 
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;
int main()
{
	char str[305],t;
	stack<int>s1;
	stack<char>s2;
	scanf("%d",&t);
	while(t--)
	{
		memset(str,0,sizeof(str));
		scanf("%s",str);
		for(int i=0;str[i]!='\0';i++)
		{
			if(str[i]>='a'&&str[i]<='z')
			{
				s2.push(str[i+1]);
				i+=3;
			}
			if(str[i]>='0'&&str[i]<='9')
			{
				int temp=0;
				while(str[i]>='0'&&str[i]<='9')
				temp=temp*10+str[i]-'0',i++;
				s1.push(temp);
			}
			if(str[i]==')')
			{
				int num1,num2;
				char ch;
				num1=s1.top();
				s1.pop();
				num2=s1.top();
				s1.pop();
				ch=s2.top();
				s2.pop();
				if(ch=='i')
				s1.push(num1>num2?num2:num1);
				if(ch=='a')
				s1.push(num1>num2?num1:num2);
				if(ch=='d')
				s1.push(num1+num2);
			}
			
		}
		printf("%d\n",s1.top());
		s1.pop();
	}
	return 0;
}        

你可能感兴趣的:(栈,表达式求值,nyoj305)