Evaluate Postfix Expression

本文介绍了一个用于评估后缀表达式的程序实现。该程序能够处理四种基本运算符:加、减、乘、除,并使用栈来存储操作数。文章详细展示了如何解析输入字符串并逐步计算表达式的值。

Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /.

Format of functions:

ElementType EvalPostfix( char *expr );

where expr points to a string that stores the postfix expression. It is guaranteed that there is exactly one space between any two operators or operands. The function EvalPostfix is supposed to return the value of the expression. If it is not a legal postfix expression, EvalPostfix must return a special value Infinity which is defined by the judge program.

Sample program of judge:

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

typedef double ElementType;
#define Infinity 1e8
#define Max_Expr 30   /* max size of expression */

ElementType EvalPostfix( char *expr );

int main()
{
    ElementType v;
    char expr[Max_Expr];
    gets(expr);
    v = EvalPostfix( expr );
    if ( v < Infinity )
        printf("%f\n", v);
    else
        printf("ERROR\n");
    return 0;
}

/* Your function will be put here */

Sample Input 1:

11 -2 5.5 * + 23 7 / -

Sample Output 1:

-3.285714

Sample Input 2:

11 -2 5.5 * + 23 0 / -

Sample Output 2:

ERROR

Sample Input 3:

11 -2 5.5 * + 23 7 / - *

Sample Output 3:

ERROR

result:
ElementType EvalPostfix( char *expr )
{
 double r;
 double a[30];
 int h;
 int i;
 int c;
 double t;
 double tz,tw;
 double count;
 h=0;
 i=0;
 while(expr[i]!='\0')
 {
   t=tz=tw=0;
  if(expr[i]<='9'&&expr[i]>='0')
  {
   tz=(expr[i]-'0')*1.0;
   i++;
   while(expr[i]!=' '&&expr[i]!='\0')
   {
      if(expr[i]<='9'&&expr[i]>='0')
      {
       while((expr[i]<='9'&&expr[i]>='0'))
       {
     tz=tz*10+(expr[i]-'0')*1.0;
     i++;
       }
      }
      else if(expr[i]=='.')
      {  
          i++;
          count=10;
    while((expr[i]<='9'&&expr[i]>='0'))
    {
    tw=tw+(expr[i]-'0')*1.0/count;
    count=count*10;
    i++;
    }
      }
   }
   a[h]=tw+tz;
   h++;
   if(expr[i]==' ')
   i++;
  }
  else if(expr[i]=='+'||expr[i]=='-'||expr[i]=='*'||expr[i]=='/')
  {
   if((expr[i+1]<='9'&&expr[i+1]>='0')&&(expr[i]='-'))
   {
      i++;
      tz=(expr[i]-'0')*1.0;
      i++;
     while(expr[i]!=' '&&expr[i]!='\0')
     {
      if(expr[i]<='9'&&expr[i]>='0')
      {
       while((expr[i]<='9'&&expr[i]>='0'))
       {
     tz=tz*10+(expr[i]-'0')*1.0;
     i++;
       }
      }
      else if(expr[i]=='.')
      {  
          i++;
          count=10;
    while((expr[i]<='9'&&expr[i]>='0'))
    {
    tw=tw+(expr[i]-'0')*1.0/count;
    count=count*10;
    i++;
    }
      }
     }
      a[h]=-(tz+tw);
      h++;
     }
   else if(h<2)
   {
    return Infinity;
   }
   else 
   {
    if(expr[i]=='+')
    {
     t=a[h-2]+a[h-1]; 
     a[h-2]=t;
     h--;
     i++;
    }
    else if(expr[i]=='-')
    { 
        t=a[h-2]-a[h-1]; 
        a[h-2]=t;
        h--; 
        i++;
    }
    else if (expr[i]=='*')
    {
     t=a[h-2]*a[h-1]; 
     a[h-2]=t; 
     h--;
     i++;
    }
    else if(expr[i]=='/')
    {
     if(a[h-1]==0) 
     return Infinity; 
        t=a[h-2]/a[h-1];
     a[h-2]=t;
     h--;
     i++;
    }
   }
  }
  if(expr[i]==' ')
  i++;
 }
 return a[0];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值