表达式求解程序

来源:互联网 发布:东方有线网络客服电话 编辑:程序博客网 时间:2024/04/30 03:17

//均已调试通过,编译器为DEV C++ 

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define  Stack_Size 100
char Operator[7]={'+','-','*','/','(',')','#'};      //用6出错!!!!
int Result,ch2;
char Opr;   //定义全局变量
//*************优先关系**************************
char PriorityTable[7][7]={
 {'>','>','<','<','<','>','>'},
 {'>','>','<','<','<','>','>'},
 {'>','>','>','>','<','>','>'},
 {'>','>','>','>','<','>','>'},
 {'<','<','<','<','<','=','o'},
 {'>','>','>','>','o','>','>'},
 {'<','<','<','<','<','o','='},

};
//***************运算符结构体*****************
typedef struct {
         char *top;
         char *base;
         int stacklength; 
        }Sqstack1,*Pstack1;
 //**************操作数结构*******************      
typedef struct {
       int *top;
       int *base;
        int stacklength;
        }Sqstack2,*Pstack2;
//***************创建堆栈**********************              
 void  InitStack1(Pstack1 S)
       {
          S->base=(char *)malloc(Stack_Size*sizeof(char));
          if(!S->base)
          exit(1);
          S->top=S->base;
          S->stacklength=Stack_Size;           
       }
  void InitStack2(Pstack2 S)
       {
          S->base=(int *)malloc(Stack_Size*sizeof(int));
          if(!S->base)
          exit(1);
          S->top=S->base;
          S->stacklength=Stack_Size;           
       }
  //******************入栈******************************    
int PushStack1(Pstack1 S,char ch)
        {
              if(S->top-S->base>=Stack_Size)
                 exit(1);
              *(S->top++)=ch;
        }
  void   PushStack2(Pstack2 S,int ch)
        {
              if(S->top-S->base>=Stack_Size)
                 exit(1);
              *(S->top++)=ch;
        }
   //******************出栈*********************       
  void   PopStack1(Pstack1 S,char *ch)
         {
               if(S->top==S->base)
                  exit(1);
              *ch=*(--S->top);
         }
  void PopStack2(Pstack2 S,int *ch)
         {
               if(S->top==S->base)
                  exit(1);
              *ch=*(--S->top);
         }
  //**********取栈顶元素*****************
 int GetTop1(Pstack1 S)
        {
            if(S->top==S->base)
                  exit(1);
               return  *(S->top-1);
        }
 int GetTop2(Pstack2 S)
        {
            if(S->top==S->base)
                  exit(1);
            return  *(S->top-1);
        } 
 //*************销毁符号栈************************ 
 void DestoryStack1(Pstack1 S)
    {
           if(S->base)
               free(S->base);
               S->top=S->base=NULL;
    }                             
 //*************销毁数据栈************************ 
 void DestoryStack2(Pstack2 S)
    {
           if(S->base)
               free(S->base);
               S->top=S->base=NULL;
    }                           
  //************查找优先权并返回*******************
int CheckPriority(char operator_1,char operator_2)
{
 int i,j;
 for(i=0;operator_1!=Operator[i];i++);
   for(j=0;operator_2!=Operator[j];j++);
     return PriorityTable[i][j]; 
}
//**************计算中间表达式的值*********************
   int Count(int a,char ch,int b)
          {
                 switch(ch)
                    {
                           case'+':
                                   return a+b;
                                    break;
                           case'-':
                                   return b-a;
                                    break;
                           case'*':
                                   return a*b;
                                    break;
                           case'/':
                                  if( b==0){
                                     printf(" div 0 is error!/n");
                                     return 0;
                                     }
                                  else
                                     return  b/a;
                    }
          }  
//*****************计算总表达式的值****************
 void Sum()
     {
           Sqstack1 S1;
           Sqstack2 S2;
           InitStack1( &S1);
           InitStack2( &S2);  
           char temp,ch,c;
           int num;
         int data,sum,a,b;
            PushStack1( &S1, '#');   //去掉访问为例
         ch=getchar();
         while((ch!='#')||(GetTop1(&S1)!='#'))
               {    sum=0;            // 注意sum的位置
                    if(ch>='0'&&ch<='9')
                      {
                          while(ch>='0'&&ch<='9'){     //实现对多位数的处理
                             data=ch-'0';
                              sum=sum*10+data;
                          ch=getchar();
                            }
                     PushStack2(&S2, sum);
                  }
                    else  
                   switch(CheckPriority(GetTop1(&S1) ,ch))  //比较优先级
                            { 
                                  case'<':
                                          PushStack1( &S1, ch); 
                                         ch=getchar();
                                         break;
                                   case'=':
                                           PopStack1(&S1, &temp);
                                           ch=getchar();
                                           break;
                                 case'>':
                                        PopStack2(&S2,&a);
                                         PopStack2(&S2,&b);
                                         PopStack1(&S1,&c);
                                         num=Count(a,c,b);
                                       PushStack2(&S2,num);
                                       break;
                                 default:
                                           break;
                         }
               } 
    Result=GetTop2(&S2);    //取数据栈栈顶元素
    DestoryStack1(&S1);
    DestoryStack2(&S2);
}
//******************提示信息**********************
void Message()
   {       printf("*****************************/n");
           printf("Welcome to use!/n");
           printf("It can culture + - * / ( )!/n");
           printf("please end with '#'!/n");
           printf("******************************/n");
   }     
//*******************主函数***************************       
int main()
  {    int i=1;
       Message();
       printf("Please input  Expression %d :",i++);
       Sum();
       printf("=%d/n",Result);
  return 0;
 }                 

原创粉丝点击