栈--算术表达式

#include "StdAfx.h"
#include <iostream>
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

#define STACK_INIT_SIZE 100;//存储空间初始分配量
#define STACKINCREMENT 10;//存储空间分配增量
//*************************************************************************************************************************
//类模版定义
template <class T>
class SqStack 
{
	private:
		T *base; 
	    T *top;
	    int stacksize;

	public:
		SqStack();   //建立一个新栈
		int Push(T e);   //压入一个新数据
		int GetTop();   //取得最上面的数据
		int Pop();   //弹出最上面的数据
};

template <class T>
SqStack <T>::SqStack()
{
	base = (T*)malloc(100*sizeof(T));
	if(!base)
		exit (0);   //分配内存失败
	top=base;
	stacksize = 100;
}

template <class T>
int SqStack <T>::GetTop()
{
	T e;
	if(top==base)
		return 0;     //栈空的情况
	e=*(top-1);
	return e;
}

template <class T>
int SqStack <T>::Pop()
{
	T e;
	if(top==base)
		return 0;     //栈空的情况
	e=* --top;
	return e;
}

template <class T>
int SqStack <T>::Push(T e)
{
	if(top-base>=stacksize)     //栈满的情况
	{
		base=(T*)realloc(base,(stacksize+10)* sizeof(T));    //追加空间
		if(!base) 
			exit (0);        //空间分配失败
		top=base+stacksize;
		stacksize+=STACKINCREMENT;    //每次添加STACKINCREMENT的空间
	}
	*top++=e;
	return 1;
}
//************************************************************************************************************************
//基本函数定义

int Isnum(char e)     //判断是否是数字
{
	if(e>='0'&&e<='9') 
		return 1;
	else
		return 0;
}

int Primary(char x)    //返回符号对应的数值
{
	int e=-1;
	switch (x)
	{
	case '+':  e=0;break;
	case '-':  e=1;break;
	case '*':  e=2;break;
	case '/':  e=3;break;
	case '(':  e=4;break;
	case ')':  e=5;break;
	case '#':  e=6;break;
	}
	
	if(e = =-1)
	{  
		cout<<"输入表达式含不规范字符!" <<endl;
	    exit(0);
	}
	return e;

}

int Precede(char x,char y)    //符号优先级定义
{
	int num[7][7]={{1,1,-1,-1,-1,1,1},
					{1,1,-1,-1,-1,1,1},
					{1,1,1,1,-1,1,1},
					{1,1,1,1,-1,1,1},
					{-1,-1,-1,-1,-1,0,2},
					{1,1,1,1,2,1,1},
					{-1,-1,-1,-1,-1,2,0}};
	int a,b;
	a = Primary(x);
	b = Primary(y);
	return num[a][b];
}

int Operate(int a ,char thera ,int b )
{
	if (thera=='+')
		return (a+b);
	if (thera=='-')
		return (a-b);
	if (thera=='*')
		return (a*b);
	if (thera=='/')
		return (a/b);
}

void Cal()
{
	SqStack <int> OPND;
						//操作数栈
	SqStack <char> OPTR;
						//运算符栈
	char c,d;
	int num;
	c=getchar();
	OPTR.Push(c);     //将#压入运算符栈
	cout<<"\t\tOPTR 进栈:  "<<c<<endl;
	c=getchar();

	while (c!='#'||OPTR.GetTop()!='#')
	{
		
		if(Isnum(c))  
		{
			num = int(c-48);
			d=getchar();     //读入下一字符
			while(Isnum(d))
			{			
				num = 10*num + int(d-48);
				d=getchar();
			}

			OPND.Push(num);
			cout<<"\t\tOPND 进栈:  "<<num<<endl;
			c=d;
		}

		else
			switch(Precede(OPTR.GetTop(),c))
		{
			
			case -1:     //栈顶元素优先级低
				OPTR.Push(c);
				cout<<"\t\tOPTR 进栈:  "<<c<<endl;
				c=getchar();
				break;

			case 0:      //脱括号并接收下一字符
				OPTR.Pop();
				c=getchar(); 
				break;

			case 1:     //退栈并将运算结果入栈
				char thera;
				int a ,b;
				thera=OPTR.Pop();
				b=OPND.Pop();
				a=OPND.Pop();
				cout<<"\t\t\tOPTR 出栈:  "<<thera<<endl;
				cout<<"\t\t\tOPND 出栈:  "<<a<<" , "<<b<<endl;
				cout<<"\t\t\t\t计算:  "<<a<<thera<<b<<endl;
				OPND.Push(Operate(a,thera,b));
				break;

			case 2:       //输入错误的情况
				cout<<"输入表达式错误!"<<endl;
				exit(0);
				break;

		}//switch
	}//while
	cout<<"\n**************************************************************";
	cout<<"\n最后的结果为: "<<OPND.GetTop();
}

//*******************************************************************************************************************
//定义主函数

int main()
{
	cout<<"请输入表达式:"<<endl;
	cout<<"\t例子:  #12+(3*(4/2))# "<<endl;
	Cal();
	cout<<endl;
	system("pause");     //显示“请按任意键继续”字样
	return 0;
}

/*请输入表达式:
        例子:  #12+(3*(4/2))#
#12+(3*(4/2))#
                OPTR 进栈:  #
                OPND 进栈:  12
                OPTR 进栈:  +
                OPTR 进栈:  (
                OPND 进栈:  3
                OPTR 进栈:  *
                OPTR 进栈:  (
                OPND 进栈:  4
                OPTR 进栈:  /
                OPND 进栈:  2
                        OPTR 出栈:  /
                        OPND 出栈:  4 , 2
                                计算:  4/2
                        OPTR 出栈:  *
                        OPND 出栈:  3 , 2
                                计算:  3*2
                        OPTR 出栈:  +
                        OPND 出栈:  12 , 6
                                计算:  12+6

**************************************************************
最后的结果为: 18
Press any key to continue*/
	

 

posted @ 2012-11-16 18:04  i Traveling Light  阅读(157)  评论(0)    收藏  举报