四則運算

/*********************************************
Copyright zxf
Filename four_operrations.c
Author: zxf  Version: 1.0 Date:2012.04
Description: 輸入一個字符串表達式,計算他的值
Function  List:
              1.topostfix()
       把輸入的字符串表達式轉換成中綴表達式
       2.value()
       計算中綴表達式的值
       3.main()
       給上面兩個函數傳遞參數
**********************************************/

#include<stdio.h>
#include<stdlib.h>

/***************************************************
Filename: linkstack.h
Description: 一個鏈式字符棧的頭文件
Function List:
             1.push()
      入棧
      2.pop()
      出棧
      3.get()
      取得棧頂元素
****************************************************/

#include"linkstack.h"

/***************************************************
Filename: linkstack_int.h
Description: 一個鏈式整形棧的頭文件
Function List:
             1.push_int()
      入棧
      2.pop_int()
      出棧
      3.get_int()
      返回棧頂元素
****************************************************/

#include"linkstack_int.h"
#include"debug.h"

/**************************************
Function: topostfix()
Description: 把輸入的字符串轉換成中綴表達式
Called by: main()
Input: 字符串指針
Return: 指向中綴表達式字符串的指針
***************************************/

char * topostfix(char *str)
{
    char *postfix = (char *)malloc(100);  //申請空間
    char *head = postfix;                 //記錄首地址
    while( (*str) != '\0')
    {
        switch(*str)                       //對字符串表達式逐個檢索
 {
     case '(':
             {                      //‘(’直接入棧
          push(*str);
   str++;
   break;
      }
            case ')':
             {
          while( (top != NULL) && ( get()!='(' ) )
   {
       *postfix = get();
       pop();
       postfix++;          //出棧直到出棧的是'('
   }
   pop();
   str++;
   break;
      }
            case '+':
     case '-':
             {
          while( (top != NULL) && ( get() != '('))
   {
       *postfix = get();        
       postfix++;               //如果是操作符,如果
       pop();                   //優先級高於棧頂操作符
   }                            //就直接入棧,否則出棧
   push(*str);                  //直到棧頂爲'('或優先
   str++;                       //級比其低再入棧
   break;
      }
     case '*':
     case '/':
             {
          while( (top != NULL) && (get()== '/'||get()== '*'))
   {
       *postfix = get();
       postfix++;
       pop();
   }
   push(*str);
   str++;
   break;
      }
            default:
            {
         while(*str >= '0' && *str <= '9' && *str != '\0')
         {
             *postfix = *str;     //如果是數字字符就直接賦值
      postfix++;
      str++;
         }
         *postfix = ' ';        //在每個操作數後,加個空格,方
         postfix++;             //便value()函數識別
         break;
     }
 }
    }
 while( top != NULL)
 {
     *postfix = get();   //把棧中剩餘的操作符出棧
     pop();
     postfix++;
 }
 *postfix = '\0';
 return head;           //返回中綴表達式字符串的首地址
   
}

/*********************************************
Function:value()
Description:計算中綴表達式的值
Called by:main()
Input: 指向中綴表達式的字符串指針
Return: 中綴表達式的值
***********************************************/

int value(char *postfix)
{
    int result;
    while(*postfix != '\0')
    {
 while(*postfix >= '0' && *postfix <= '9')
 {
            result =0;
     while(*postfix != ' ')
     {
                result = result*10 + ( *postfix - '0'); //通過空格識別操作
  postfix++;                              //數,並把其入棧
     }
     //debug_msg("%d %d\n",__LINE__,result);
     push_int(result);
     postfix++;
        }
 if( *postfix != '\0')
 {
     int y = get_int();
     pop_int();
     int x = get_int();
     pop_int();
     switch(*postfix)                   //如果是操作符,就從棧中
     {                                  //取兩個操作數操作,並把
         case '+':                      //結果寫回棧中
          {
       result = x+y;
       break;
   }
         case '-':
          {
       result = x- y;
       break;
   }
  case '*':
          {
       result = x*y;
       break;
   }
  case '/':
          {
       result = x/y;
       break;
   }
     }
     //debug_msg("%d %d\n",__LINE__,result);
     push_int(result);           //把操作後的結果寫回棧中
     postfix++;
 }
    }
    result = get_int();
    return result;
}

int main()
{
    int result;
    char *ptr = "121+10*(53-49+20)/((35-25)*2+10)";
    printf("原表達式爲 %s\n",ptr);
    char *fix = topostfix(ptr);
    printf("轉換後的中綴表達式爲 %s\n",fix);
    result = value(fix);
    printf("結果爲:%d\n",result);
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章