中缀表达式转变为后缀表达式&后缀表达式求值

#include <string.h>
#include "stdio.h"
#include "stdlib.h"
#define max 100               //栈的最大容量
typedef struct
{
   int stack[max];
   int top;
}qstack;

void tranaform(qstack *s,char exp[])
{
   s->top=-1;
   char ch;
   char str[max];
   int i=0,j=0,tm;
   gets(str);                                    
   int StrEnd = strlen(str);
   str[StrEnd] = '#';  //输入表达式,以‘#’结束
   //printf("strlen(str) = %d\n", strlen(str));
   ch=str[0];
   while(ch!='#')
   {
      switch(ch)
      {
      case '(':                                 //判断为左括号
          s->stack[++s->top]=ch;
          break;
      case ')':                                 //判定为右括号
          while(s->stack[s->top]!='(')
          {
             exp[i++]=s->stack[s->top--];
          }
          s->top--;
          break;
      case '-':                                 //判定为加减号
      case '+':
          while(s->top>=0&&s->stack[s->top]!='(')
          {
             exp[i++]=s->stack[s->top--];
          }
           s->stack[++s->top]=ch;
           break;
      case '/':                                 //判定为乘除号
      case '*':
          while((s->stack[s->top]=='*'||s->stack[s->top]=='/')&&s->top>=0)
          {
             exp[i++]=s->stack[s->top--];
          }
          s->stack[++s->top]=ch;
          break;
      default:                                  //判定为数字
             tm=0;
             
             /**以字符的ASCII 10进制值表示数字,实际上存入exp的是字符,
             *如若数字为48,则存入的字符为'0',
             *后面计算的时候用的数字48而非字符本身值
             **/
             
             while(ch>='0'&&ch<='9')
             {
                 tm=tm*10+ch-'0';              //转换为10进制
                 ch=str[++j];
             }
             exp[i++]=tm;                      
             --j;
             break;
      }//switch
         ch=str[++j];
   }//while循环
   while(s->top>=0)
   {
      exp[i++]=s->stack[s->top--];
   }
   exp[i]='#';                                //置#结束符
   for(j=0;j<=i;j++)                          //输出后缀表达式
   {
       if(exp[j]=='#') break;
       if(exp[j]=='-'||exp[j]=='+'||exp[j]=='/'||exp[j]=='*')
           printf(" %c",exp[j]);
       else
           printf(" %d",exp[j]);
   }
}
void compute(qstack *s,char exp[])
{
   int i=0,j,temp;
   char ch;
   s->top=-1;
   ch=exp[0];
   while(ch!='#')
   {
        switch(ch)
        {
        case '-':
            temp=s->stack[s->top]-s->stack[s->top-1];
            s->stack[--s->top]=temp;
            printf("\ntemp=%d",temp);
            break;
        case '+':
            temp=s->stack[s->top]+s->stack[s->top-1];
            s->stack[--s->top]=temp;
            printf("\ntemp=%d",temp);
            break;
        case '/':
            temp=s->stack[s->top]/s->stack[s->top-1];

            s->stack[--s->top]=temp;
            printf("\ntemp=%d",temp);
            break;
        case '*':
            temp=s->stack[s->top]*s->stack[s->top-1];
            s->stack[--s->top]=temp;
            printf("\ntemp=%d",temp);
            break;
        default:
            s->stack[++s->top]=ch;
            break;
        }
        ch=exp[++i];
   }
   printf(" %d ",s->stack[s->top]);             //输出结果
}
int main(int argc, char* argv[])
{
    char exp[max];            //存储转换后的后缀表达式
    qstack s;
        tranaform(&s,exp);        //转换为后缀表达式
        compute(&s,exp);          //计算结果
    return 0;
}


你可能感兴趣的:(中缀表达式转变为后缀表达式&后缀表达式求值)