栈的应用实例---表达式求值(C语言)

栈是限定仅在表尾进行插入或删除操作的线性表。对栈来说,表尾端有其特殊含义,称为栈顶(top),相应地,表头端称为栈底(bottom)。不含元素的空表称为空栈。 

stack_s.c
#include  "stdio.h"
#include  "malloc.h"
#define  TRUE 1
#define  FALSE 0
#define  OK  1
#define  ERROR  0
#define  INFEASIBLE -1
#define  OVERFLOW -2
typedef int Status;
#define STACK_INIT_SIZE  100
#define STACKINCREMENT  10
typedef struct {
            SELemType *base;
            SELemType *top;
            int   stacksize;
}SqStack;
Status InitStack (SqStack *S)(
       S->base=(SElemType *) malloc (STACK_INIT_SIZE *sizeof(SElemType));
       if (!S->base)
            exit (OVERFLOW);
       S->top=S->base;
       S->stacksize=STACK_INIT_SIZE;
       return OK;
}
Status StackEmpty (SqStack S){
       if (S.top==S.base)
           return TRUE;
       else
           return FALSE;
}
Status GetTop(SqStack S,SElemType *e){
       if (S.top==S.base)
           return ERROR;
       *e=*(S.top-1);
       return OK;
}
Status Push(SqStack *S,SElemType e){
       if ((S->top-S->base)>=S->stacksize){
           S->base=(SElemType *) realloc (S->base,(S->stacksize +STACKINCREMENT)*sizeof(SElemType));
       if (!S->base)
           exit (OVERFLOW);
       S->top=S->base+S->stacksize;
       S->stacksize+=STACKINCREMENT;
}
       *(S->top)=e;
       S->top++;
       return OK;
}
Status POP(SqStack *S,SElemType *e){
       if (S->top==S->base)
          return ERROR;
       S->top--;
       *e=*(S->top);
       return OK;
}
Status Output (SElemType e){
       printf("%d",e);
       return OK;
}
 
 
typedef int SElemType;
#include <stype.h>
#include "stack_s.c"
 
Status in (char c,char op[]){
       char *P;
       p=op;
       while(*p!='\0'){
             if (c==*P)
                 return TRUE;
             p++;
       }
       return FALSE;
}
char Precede(char a,char b){
       int i,j;
       char pre[][7]={
       {'>','>','<','<','<','>','>'},
       {'>','>','<','<','<','>','>'},
       {'>','>','>','>','<','>','>'},
       {'>','>','>','>','<','>','>'},
       {'<','<','<','<','<','=','0'},
       {'>','>','>','>','0','>','>'},
       {'<','<','<','<','<','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;
    }
    return Pre[i][j];
}
int Operate(int a,char theta, int b){
    int i,j,result;
    i=a;
    j=b;
    switch(theta) {
       case '+':result =i+j;break;
       case '-':result =i-j;break;
       case '*':result =i*j;break;
       case '/':result =i/j;break;
    }
    return result;
}
int getNext(int *n){
    char c;
    *n=0;
    while((c=getchar())=='');
        if (!=isdigit(c)){
             *n=c;
             return 1;
        }
        do {
           *n=*n*10+(c-'0');
           c=getchar();
           }while (isdigit (c));
           ungetc(c,stdin);
           return 0;
}
int EvaluateExpression(){
      int n;
      int flag;
      int c;
      char x,theta;
      int a,b;
      char op[]="+ - * / ( ) #";
      SqStack OPTR;
      SqStack OPND;
      
      InitStack (& OPTR);Push (&OPTR,'#');
      InitStack(& OPND);
      flag=getNext(&c);
 
      GetTop(OPTR,&x);
      while (c!='#' || x!='#'){
         if (flag ==0){
              Push (&OPND,c);
              flag =getNext(&c);
         }
         else{
              GetTop(OPTR,&x);
              switch(Precede(x,c)){
                    case '<':
                       Push(&OPTR,c);
                       flag=getNext(&c);
                       break;
                    case '=':
                       Pop(&OPTR,&x);
                       flag=getNext(&c);
                       break;
                    case '>':
                       Pop(&OPTR,&theta);
                       Pop(&OPND,&b);
                       Pop(&OPND,&a);
                       Push(&OPND,Operate (a,theta,b));
                       break;
               }
       }
        GetTop(OPTR,&x);
    }
       GetTop(OPND,&c);
        return c;
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值