四则运算

/*********************************************
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;
}

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