数据结构 实验三 算术表达式求值演示

数据结构 实验三  算术表达式求值演示

//基本上已经OK了~~~~

/*
测试用例:

  8;1+2+3+4;88-1*5;1024/4*8;1024/(4*8);(20+2)*(6/2);
  3-3-3;8/(9-8);2*(6+2*(3+6*(6+6)));(((6+6)*6+3)*2+6)*2

*/

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

typedef int ElemType;
const int STACK_INIT_SIZE=100;
const int STACKINCREMENT=10;

typedef struct
{
 ElemType *base;
 ElemType *top;
 int stacksize;
}Stack;


int IfEmptyStack(Stack S);
void InitStack(Stack &S);
void EmptyStack(Stack &S);
void Push(Stack &S, ElemType e);
void Pop(Stack &S, ElemType &e);
void ShowStack(Stack S);


int In(char ch);
char Precede(char a, char b);
int Operate(int a, char f, int b);

void EvaluateExpression();


int main()
{
 cout<<"请输入表达式,以 # 结束..."<<endl;
 while(1)
 {
  EvaluateExpression();
  cout<<endl;
  cout<<"请再输入表达式,以 # 结束..."<<endl;
 }
 return 0;
}


int IfEmptyStack(Stack S)
{
 if(S.base==S.top)
  return 1;
 return 0;
}

void InitStack(Stack &S)
{
 S.base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
 S.top=S.base;
 S.stacksize=STACK_INIT_SIZE;
 return ;
}

void EmptyStack(Stack &S)
{
 S.top=S.base;
 return ;
}

void Push(Stack &S, ElemType e)
{
 if(S.top-S.base>=S.stacksize)
 {
  S.base=(ElemType *)realloc(S.base, (S.stacksize+STACKINCREMENT)*sizeof(ElemType));
  S.top=S.base+S.stacksize;
  S.stacksize+=STACKINCREMENT;
 }
 *S.top++=e;
 return ;
}

void Pop(Stack &S, ElemType &e)
{
 if(S.top==S.base)
  return ;
 e=*--S.top;
 return ;
}

ElemType GetTop(Stack &S)
{
 if(S.top==S.base)
  return 0;
 return *(S.top-1);
}

void ShowStack(Stack S)
{
 ElemType *p=S.base;
 while(p!=S.top)
  printf("%d",*p++);
    return;
}

int In(char ch)
{
 int res;
 switch(ch)
 {
  case '+':
  case '-':
  case '*':
  case '/':
  case '(':
  case ')':
  case '#': res=1;break;
  default: res=0;break;
 }
 return res;
}

char Precede(char a, char b)
{
 int i,j;
 int form[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}
 };
 switch(a)
 {
  case '+':i=0;break;
  case '-':i=1;break;
  case '*':i=2;break;
  case '/':i=3;break;
  case '(':i=4;break;
  case ')':i=5;break;
  case '#':i=6;break;
 }
 switch(b)
 {
  case '+':j=0;break;
  case '-':j=1;break;
  case '*':j=2;break;
  case '/':j=3;break;
  case '(':j=4;break;
  case ')':j=5;break;
  case '#':j=6;break;
 }
 if(form[i][j]==1)
  return '>';
 else if(form[i][j])
  return '<';
 else
  return '=';
}

int Operate(int a, char f, int b)
{
 switch(f)
 {
 case '+': return a+b;
 case '-': return a-b;
 case '*': return a*b;
 case '/': return a/b;
 }
 return 0;
}

void EvaluateExpression()
{
 char c, d[100];
 int i, f, num, tmpa, tmpb;
 Stack OPTR, OPND;
 InitStack(OPTR);InitStack(OPND);
 Push(OPTR, '#');
 c=getchar();
 while(c!='#'||GetTop(OPTR)!='#')
 {
  if(c>='0'&&c<='9')
  {
   i=0;
   do{
    d[i++]=c;
    c=getchar();
   }while(c>='0'&&c<='9');
   d[i]='\0';
   num=atoi(d);
   Push(OPND, num);
  }
  else if(In(c))
  {
   switch(Precede(GetTop(OPTR), c))
   {
    case '<': Push(OPTR, (int)c);c=getchar();
       break;
    case '=': Pop(OPTR, f);c=getchar();
       break;
    case '>': Pop(OPTR, f);Pop(OPND, tmpb);Pop(OPND, tmpa);
       Push(OPND, Operate(tmpa, f, tmpb));
       break;
   }
  }
 }
 c=getchar();//接收最后输入的一个回车符!!!否则在主函数中只能输入一次...
 cout<<"所求表达式的值为:";
 ShowStack(OPND);
 cout<<endl<<endl;
}

 

运行示意图:

 

posted @ 2009-03-13 22:27  jackhen->hench  阅读(7635)  评论(0编辑  收藏  举报