表达式求值算法的实现

来源:互联网 发布:淘宝9.9限时抢购在哪里 编辑:程序博客网 时间:2024/06/02 06:00

#include<stdio.h>

#include<stdlib.h>

#define STACK_INIT_SIZE 100

#define STACKINCREMENT 10

typedef struct{

      int *base;

         int *top;

         int stacksize;

}Sqstack1;

Sqstack1 opnd;

typedef struct{

      char *base;

         char *top;

         int stacksize;

}Sqstack2;

Sqstack2 optr;

void Initstack_optr(){

   optr.base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));

   if(!optr.base)  exit(1);

   optr.top=optr.base;

   optr.stacksize=STACK_INIT_SIZE;

   *optr.top++='#';

//printf("the first tergert is %c",*(optr.top-1));

}

void Initstack_opnd(){

       opnd.base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));

    if(!opnd.base)  exit(1);

       opnd.top=opnd.base;

       opnd.stacksize=STACK_INIT_SIZE;

}

int In(char c){

   int n;

   switch(c){

   case'+':

   case'-':

   case'*':

   case'/':

   case'(':

   case')':

   case'[':

   case']':

   case'#':  n=1;break;

   default:  n=0;break;

   }

   return n;

}

void pushopnd(int c){

    if(opnd.top-opnd.base>=opnd.stacksize)

       {

              opnd.base=(int *)realloc(opnd.base,(opnd.stacksize+STACKINCREMENT)*sizeof(int));

              if(!opnd.base)  exit(1);

              opnd.top=opnd.base+opnd.stacksize;

              opnd.stacksize+=STACKINCREMENT;

       }

       *opnd.top++=c;

//printf("%d have been put into opndstack!/n",c);

}

void pushoptr(char c){

    if(optr.top-optr.base>=optr.stacksize)

       {optr.base=(char*)realloc(optr.base,(optr.stacksize+STACKINCREMENT)*sizeof(char));

       if(!optr.base)  exit(1);

       optr.stacksize+=STACKINCREMENT;

}

*optr.top++=c;

//printf("%c have been put into optrstack!/n",c);

}

 

int Gettopopnd(){

    if(opnd.top==opnd.base)  exit(1);

       return *(opnd.top-1);

}

 

int Gettopoptr(){

   if(optr.top==optr.base)   exit(1);

   return  *(optr.top-1);

}

int popopnd(){

  if(opnd.top==opnd.base)   exit(1);

  return  *--opnd.top;

}

int popoptr(){

   if(optr.top==optr.base)   exit(1);

   return  *--optr.top;

}

 

int  operate(int a,char theta,int b){

       int terminal;//printf("hello  !");

       switch (theta){

       case '+': terminal=a+b;break;

       case '-': terminal=a-b;break;

       case '*': terminal=a*b;break;

       case '/': terminal=a/b;break;

       }

       return terminal;

}

 

int Precede(char  c1,char c2){

char result;

switch(c2){

case '#': if(c1=='#') result='=';

                  else

                               result='>';break;

case '+':

case '-':if(c1=='('||c1=='#')  result='<';

       else

              result='>';break;

case '*':

case '/':

       if(c1=='*'||c1=='/'||c1==')')

              result='>';

       else

              result='<';break;

case '(': result='<';break;

case ')': if(c1=='(')

                       result='=';

       else

       result='>';break;

}

return result;

}

 

int main(){

       char c,c1,theta;

       int a,b,terminal,mid;

       //int i=0,j;

       //char c2;

       Initstack_optr();

       /*for(;i<=5;i++){

       c=getchar();

              pushoptr(c);

       }

       while (optr.base !=optr.top ){

             

    c2=popoptr();

       printf("%c",c2);

       }*/

    /*for(;i<=5;i++){

       j=getchar();

              pushopnd(j);

       }

       while (opnd.base !=opnd.top ){

             

    j=popopnd();

       printf("%d",j);

       }*/

       Initstack_opnd();

       c=getchar();

      

       while('#'!=c||('#'!=(Gettopoptr()))){

              if(!(In(c))){

                     //int first;

         pushopnd(int(c-48));

              //first=Gettopopnd();

              //printf("the top is %d",first);

               c=getchar();

              }

              else

              {

                     c1=Gettopoptr();

                     switch(Precede(c1,c)){

                 case '<': pushoptr(c);c=getchar();break;

                             case '=': popoptr();c=getchar();break;

                             case '>': theta=popoptr();a=popopnd();b=popopnd();

                                    mid=operate(b,theta,a);

                                    //printf("%d",mid);                                  

                                    pushopnd(mid);

                                    if (c==')'){

                                           popoptr();c=getchar();}

                                    else if(c!='#'){ pushoptr(c);

                                    c=getchar();}

                                    else continue;break;

              }

              }

}

 

      terminal=Gettopopnd();

              printf("the terminal tergert is %d",terminal);

              return 0;

}

原创粉丝点击