hays

          海納百川
          posts - 25, comments - 48, trackbacks - 0, articles - 0
            BlogJava :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

          表達式求值(C實現)

          Posted on 2007-11-07 12:49 hays(海納百川) 閱讀(1652) 評論(2)  編輯  收藏
          #include <stdio.h>
          #include 
          <stdlib.h>
          #include 
          <string.h>
          #define MAXSIZE 
          4000;
          typedef struct 
          {
              
          char data[10];
              
          int top;//頭地址
              int base;//基地址
              int length;//長度
          }
          Stack;

          void init(Stack *st)//初始化棧
          {
              st
          ->base=0;
              st
          ->top=0;
              st
          ->length=0;
          }


          int isEmpty(Stack *st)
          {
              
          int n=0,top,base;
              top 
          =st->top;
              base 
          =st->base;
              
          if(top==base)
              
          {
                  
          return 1;
              }

              
          return n;
          }


          int isFull(Stack *st)
          {
              
          int n=0,top,base;
              top 
          =st->top;
              
          if(top>=4000)
              
          {
                  
          return 1;
              }

              
          return n;
          }


          char  getTop(Stack *st)// 返回top值,不改變棧的結構
          {
              
          char n;
              
          if(isEmpty(st))
              
          {
                  printf(
          "棧為空\n");
                  
          return 0;
              }

              
          int positon= st->top-1;
              n
          = st->data[positon];//取出數據;
              return n;
          }


          char  pop(Stack *st)// 出棧,返回
          {
              
          char n;
              
          if(isEmpty(st))
              
          {
                  printf(
          "棧為空\n");
                  
          return 0;
              }

              
          int positon= st->top-1;
              n
          = st->data[positon];//取出數據;

              st
          ->top--;
              st
          ->length--;
              st
          ->data[positon]='\0';//消除數據
              return n;
          }


          void push(char n,Stack *st)//入棧
          {
              
          int positon ;
              
          if(isFull(st))
              
          {
                  printf(
          "棧滿\n");
                  
              }

              
          else
              
          {
                  positon
          = st->top;//獲取位置

                  st
          ->data[positon]=n;//存入數據

                  st
          ->top++;//改變位置
              }


          }


          void show(Stack *m1)//輸出棧中的數據
          {
              
          int top,base;
              top
          =m1->top;
              base
          =m1->base;
              
          while(top>base)
              
          {
                  printf(
          "%c,",m1->data[--top]);
              }

              printf(
          "\n");
          }


          int isOperate(char temp)//是否是操作符
          {
              
          if(temp=='+'||temp=='-'||temp=='*'||temp=='/'||temp=='('||temp==')'||temp=='#')
              
          {
                  
          return 1;
              }

              
          return 0;
          }


          int isValue(char temp)//是否是數值
          {
              
          if(temp>='0'&&temp<='9')//
              {
                  
          return 1;
              }

              
          else
              
          {
                  
          return 0;
              }

          }


          int isAvail(char temp)//是否有效字符
          {
              
          if(isOperate(temp)||isValue(temp))//如果temp既不是操作符和數值的話,則它是非法的
              {
                  
          return 1;
              }

              
          return 0;
          }


          int detect(char temp)//搜索矩陣位置
          {
              
          int i=0;
              
          char oper[7]={'+','-','*','/','(',')','#'};
              
          for(i=0;i<7;i++)
              
          {
                  
          if(temp==oper[i])
                  
          {
                      
          return i;
                  }

              }

          }


          char Priority(char temp,char optr)//判斷優先級
          {
              
          /*
                      +  -  *  /  (  )  #
                      1  2  3  4  5  6  7
                  + 1 <  <  <  <  >  >  >   
                  - 2 <  <  <  <  >  >  >
                  * 3 >  >  <  <  >  >  >
                  / 4 >  >  <  <  >  >  > 
                  ( 5 >  >  >  >  >  =  0  
                  ) 6 <  <  <  <  =  0  > 
                  # 7 <  <  <  <  >  0  =
              
          */

              
          int row ,col;
              
          char priority[7][7]={/*      +      -      *   /      (   )   #                */        
                                       
          {'<','<','<','<','>','>','>'},

                                      
          {'<','<','<','<','>','>','>'},

                                      
          {'>','>','<','<','>','>','>'},

                                      
          {'>','>','<','<','>','>','>'},

                                      
          {'>','>','>','>','>','=','>'},

                                      
          {'<','<','<','<','=','0','>'},

                                      
          {'<','<','<','<','>','<','='},                            
                                  }
          ;


              row 
          = detect(temp);//找出對應的矩陣下標;
              col = detect(optr);    
          //    printf("%d,%d",row,col);
                
              
          //優先級存儲在一個7x7的矩陣中,對應關系上圖;
              
              
          return priority[row][col];

          }

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

          }

          int calculateExpress(char *express)//計算表達式
          {
              
          int result=0;
              Stack OPTR,OPND;
          //OPTR存儲操作符,OPND操作數值
              init(&OPTR);
              init(
          &OPND);
              push(
          '#',&OPTR);//默認第一個位'#'

              
          ////////////////////-算法-////////////////////////////
              while(*express!='\0')
              
          {
                  
          char temp;
                  temp
          = *(express); 
                  printf(
          "---------------------------------\n");
                  printf(
          "當前的符號為%c\n",temp);
                  
          if(isAvail(temp))//是否是有效字符
                  {    
                          
          if(isOperate(temp) )//輸入的是操作符
                          {
                              
          char optr = getTop(&OPTR);//棧中top位的操作符
                              printf("棧頂操作符位:%c\n",optr);
                              
          char prior = Priority(temp,optr);//判斷優先級
                              switch(prior)
                              
          {
                                  
          case '>':
                                              push(temp,
          &OPTR);
                                              printf(
          "將符號位%c入棧\n",temp);
                                              express
          ++;
                                              
          break;
                                  
          case '<':
                                              
          int a,b;

                                              
          char oper,result;

                                              a
          =pop(&OPND)-'0';//存在棧中的都是char字符
                                              b=pop(&OPND)-'0';
                                              oper
          =pop(&OPTR);
                                              
                                              result
          =evaluate(b,a,oper);//出棧一個操作符,計算結果
                                              
          //printf("%d",result-'0');
                                              push(result,&OPND);//結果入OPND
                                              printf("%d%c%d結果為:%d\n",b,oper,a,result-'0');
                                              
          break;
                              
                                  
          case '=':
                                              
          //消除括號
                                              pop(&OPTR);
                                              printf(
          "消除括號\n");
                                              express
          ++;
                                              
          break;
                                  
                              }


                          }

                          
          if(isValue(temp))//輸入的是數值
                          {
                              push(temp,
          &OPND);//將數值位入棧;
                              express++;
                              printf(
          "將數值%c壓入棧\n",temp);
                              
          //show(&OPND);
                          }

                  }

                  
          else //表達式中有非法字符
                  {
                      printf(
          "表達式中有非法字符\n");
                      exit(
          -1);//退出程序
                  }

                  
              
              }

          //    show(&OPND);
          //    show(&OPTR);
              return getTop(&OPND)-'0';
          }


          void inputExpress(char *express)//輸入表達式
          {
              
          int length=0;
              printf(
          "請輸入一個表達式:");
              scanf(
          "%s",express);
              
          int len =strlen(express);
              express[len]
          ='#';//表達式最后一位默認為'#';
              express[len+1]='\0';

          }


          void output(char *express,int result)//輸出表達式
          {
              
          int i=0;
              printf(
          "----------------------------------------\n表達式:");
              
          while(express[i]!='#')
              
          {
                  printf(
          "%c",express[i]);
                  i
          ++;
              }

              printf(
          "=%d\n",result);



          }


          void main()
          {
              
              
          char express[100];//表達式 
              int result =0;
              
              inputExpress(express);
          //輸入表達式
              
              result 
          = calculateExpress(express);//計算表達式;
              
              output(express,result);    
          //輸出表達式
              
              
          //、、、、、、、、、、、、、測試優先級。
              /*
              char m='7' ;
              m=Priority('+','*');
              printf("優先級為%c",m);
              
              int m=evaluate(5,6,'m');
              printf("%d",m);
              
          */

          }

          評論

          # re: 表達式求值(C實現)  回復  更多評論   

          2009-10-10 22:00 by www
          三百多行的代碼居然只能做算術運算?43+34=7 。。。。。

          # re: 表達式求值(C實現)  回復  更多評論   

          2011-12-20 19:50 by yelangjunjie
          十位數就不會算了,需要改進啊

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 定边县| 阿克苏市| 日喀则市| 丰原市| 安康市| 鸡泽县| 鹤庆县| 庄河市| 萝北县| 常州市| 富阳市| 故城县| 玉屏| 平利县| 耒阳市| 田林县| 宁安市| 建昌县| 新津县| 七台河市| 绥德县| 明溪县| 甘泉县| 甘南县| 灌阳县| 类乌齐县| 永春县| 中山市| 岗巴县| 阿瓦提县| 西乌珠穆沁旗| 府谷县| 米泉市| 昌吉市| 元江| 榕江县| 钟山县| 常宁市| 永吉县| 常州市| 吉木萨尔县|