栈的应用——计算表达式

挺久之前写的一个作业,ACM选修课的,用栈实现的计算表达式,有两种,一种是将表达式转换为后缀表达式再计算,一种是直接计算中缀表达式,下面是代码。

(1)转后缀再计算:

#include<stack>
#include<iostream>
using namespace std;
int main()
{
	stack <char> s1;
	stack <int> s2;
	int i=0,s,v1,v2,v3;
	char p[50];
	cin.getline(p,50);
	cout<<'=';
	while(p[i]!='\0')
	{
		s=0;
		switch(p[i])
		{
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			while( (p[i]>='0') && (p[i]<='9') )
			{
				s=s*10+(p[i]-'0');
				i++;
			}
			s2.push(s);
			i--;
			break;
		case '+':
		case '-':
			if( (s1.empty()) || (s1.top()=='(') )
				s1.push(p[i]);
			else
			{
				while( (!s1.empty()) && (s1.top()!='(') )
				{
					switch(s1.top())
					{
					case '+':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v1+v2;
						s2.push(v3);
						break;
					case '-':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v2-v1;
						s2.push(v3);
						break;
					case '*':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v1*v2;
						s2.push(v3);
						break;
					case '/':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v2/v1;
						s2.push(v3);
						break;
					}//switch
					s1.pop();
				}//while
				s1.push(p[i]);
			}//else
			break;
		case '*':
		case '/':
			if( (s1.empty()) || (s1.top()=='(') || (s1.top()=='+') || (s1.top()=='-') )
				s1.push(p[i]);
			else
			{
				while( (!s1.empty()) && (s1.top()!='(') && (s1.top()!='+') && (s1.top()!='-') )
				{
					switch(s1.top())
					{
					case '*':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v1*v2;
						s2.push(v3);
						break;
					case '/':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v2/v1;
						s2.push(v3);
						break;
					}
					s1.pop();
				}
				s1.push(p[i]);
			}
			break;
		case '(':
			s1.push(p[i]);
			break;
		case ')':
			while( (!s1.empty()) && (s1.top()!='(') )
			{
				switch(s1.top())
				{
				case '+':
					v1=s2.top();
					s2.pop();
					v2=s2.top();
					s2.pop();
					v3=v1+v2;
					s2.push(v3);
					break;
				case '-':
					v1=s2.top();
					s2.pop();
					v2=s2.top();
					s2.pop();
					v3=v2-v1;
					s2.push(v3);
					break;
				case '*':
					v1=s2.top();
					s2.pop();
					v2=s2.top();
					s2.pop();
					v3=v1*v2;
					s2.push(v3);
					break;
				case '/':
					v1=s2.top();
					s2.pop();
					v2=s2.top();
					s2.pop();
					v3=v2/v1;
					s2.push(v3);
					break;
				}
				s1.pop();
			}
			if(!s1.empty())//pop掉左括号
				s1.pop();
			break;
		}//switch
		i++;
	}//while
	while(!s1.empty())
	{
		switch(s1.top())
		{
		case '+':
			v1=s2.top();
			s2.pop();
			v2=s2.top();
			s2.pop();
			v3=v1+v2;
			s2.push(v3);
			break;
		case '-':
			v1=s2.top();
			s2.pop();
			v2=s2.top();
			s2.pop();
			v3=v2-v1;
			s2.push(v3);
			break;
		case '*':
			v1=s2.top();
			s2.pop();
			v2=s2.top();
			s2.pop();
			v3=v1*v2;
			s2.push(v3);
			break;
		case '/':
			v1=s2.top();
			s2.pop();
			v2=s2.top();
			s2.pop();
			v3=v2/v1;
			s2.push(v3);
			break;
		}//switch
		s1.pop();
	}//while
	v3=s2.top();
	s2.pop();
	cout<<v3<<endl;
	return 0;
}

 

输入: 55+34*(3*(3+6))-12

55+34*(3*(3+6))-12
55 34 3 3 6 +  *  * + 12 -=961
Press any key to continue


(2)中缀直接计算

include<stack>
#include<iostream>
using namespace std;
int main()
{
	stack <char> s1;
	stack <int> s2;
	int i=0,s,v1,v2,v3;
	char p[50];
	cin.getline(p,50);
	cout<<'=';
	while(p[i]!='\0')
	{
		s=0;
		switch(p[i])
		{
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			while( (p[i]>='0') && (p[i]<='9') )
			{
				s=s*10+(p[i]-'0');
				i++;
			}
			s2.push(s);
			i--;
			break;
		case '+':
		case '-':
			if( (s1.empty()) || (s1.top()=='(') )
				s1.push(p[i]);
			else
			{
				while( (!s1.empty()) && (s1.top()!='(') )
				{
					switch(s1.top())
					{
					case '+':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v1+v2;
						s2.push(v3);
						break;
					case '-':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v2-v1;
						s2.push(v3);
						break;
					case '*':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v1*v2;
						s2.push(v3);
						break;
					case '/':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v2/v1;
						s2.push(v3);
						break;
					}//switch
					s1.pop();
				}//while
				s1.push(p[i]);
			}//else
			break;
		case '*':
		case '/':
			if( (s1.empty()) || (s1.top()=='(') || (s1.top()=='+') || (s1.top()=='-') )
				s1.push(p[i]);
			else
			{
				while( (!s1.empty()) && (s1.top()!='(') && (s1.top()!='+') && (s1.top()!='-') )
				{
					switch(s1.top())
					{
					case '*':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v1*v2;
						s2.push(v3);
						break;
					case '/':
						v1=s2.top();
						s2.pop();
						v2=s2.top();
						s2.pop();
						v3=v2/v1;
						s2.push(v3);
						break;
					}
					s1.pop();
				}
				s1.push(p[i]);
			}
			break;
		case '(':
			s1.push(p[i]);
			break;
		case ')':
			while( (!s1.empty()) && (s1.top()!='(') )
			{
				switch(s1.top())
				{
				case '+':
					v1=s2.top();
					s2.pop();
					v2=s2.top();
					s2.pop();
					v3=v1+v2;
					s2.push(v3);
					break;
				case '-':
					v1=s2.top();
					s2.pop();
					v2=s2.top();
					s2.pop();
					v3=v2-v1;
					s2.push(v3);
					break;
				case '*':
					v1=s2.top();
					s2.pop();
					v2=s2.top();
					s2.pop();
					v3=v1*v2;
					s2.push(v3);
					break;
				case '/':
					v1=s2.top();
					s2.pop();
					v2=s2.top();
					s2.pop();
					v3=v2/v1;
					s2.push(v3);
					break;
				}
				s1.pop();
			}
			if(!s1.empty())//pop掉左括号
				s1.pop();
			break;
		}//switch
		i++;
	}//while
	while(!s1.empty())
	{
		switch(s1.top())
		{
		case '+':
			v1=s2.top();
			s2.pop();
			v2=s2.top();
			s2.pop();
			v3=v1+v2;
			s2.push(v3);
			break;
		case '-':
			v1=s2.top();
			s2.pop();
			v2=s2.top();
			s2.pop();
			v3=v2-v1;
			s2.push(v3);
			break;
		case '*':
			v1=s2.top();
			s2.pop();
			v2=s2.top();
			s2.pop();
			v3=v1*v2;
			s2.push(v3);
			break;
		case '/':
			v1=s2.top();
			s2.pop();
			v2=s2.top();
			s2.pop();
			v3=v2/v1;
			s2.push(v3);
			break;
		}//switch
		s1.pop();
	}//while
	v3=s2.top();
	s2.pop();
	cout<<v3<<endl;
	return 0;
}

输入: 55+34*(3*(3+6))-12

55+34*(3*(3+6))-12
=961
Press any key to continue


 

你可能感兴趣的:(作业)