前缀表达式求解

#include<cstdio>
#include<iostream>
#include<stack> 
using namespace std;
int main()
{

	char ch;
	char ch1;
	char ch2;
	char result;
	stack<char>s;
	stack<char>a;
	while((ch=getchar())!='\n')
	{
		s.push(ch);
	}
	while(s.empty()==false)
	{
		ch=s.top();//取栈顶元素成功 
		printf("%c\n",ch);
	
		if(ch >='0' && ch <= '9')
			{
			a.push(ch);	//入栈 
			//printf("*%c\n",ch);
			}	

		else
		{
	//		printf("%c\n",ch);
			ch1=a.top();
	//		printf("%c\n",ch1);
			a.pop();
			ch2=a.top();
	//		printf("%c\n",ch2);
			a.pop();

			if(ch=='+')
			a.push(((ch1-='0')+(ch2-'0'))+'0'); 
			else if(ch=='-')
			a.push(((ch1-'0')-(ch2-'0'))+'0');
			else if(ch=='*')
			a.push(((ch1-'0')*(ch2-'0'))+'0');
			else
			a.push(((ch1-'0')/(ch2-'0'))+'0');
		//	printf("结果:%c\n",a.top());
		}
		s.pop();

	}

	result=a.top();
	printf("%d",result-'0');

	return 0;
}
如何实现浮点数的存储呢?

猜你喜欢

转载自blog.csdn.net/MyCodeQueen/article/details/45600125