表达式求值

#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
//two steps solving the problem:1.transfer the infixexpression to the postfixexpression
// 2.using a stack to calculate the value of the infixexperssion.
double readnumber(char f[],int *i){
      double x=0.0;
      int k=0;
      while(f[*i]>='0'&&f[*i]<='9') x=10*x+(f[(*i)++]-'0');//处理整数部分
      if(f[*i]=='.'){//处理小数部分
         (*i)++;
         while(f[*i]>='0'&&f[*i]<='9'){
               x=x*10+(f[(*i)++]-'0');
               k++;
         }
      }
      while(k--){
            x/=10.0;
      }
      return x;
}

double evalpost(char f[]){
       double obst[100];/*操作数栈*/
       int top=0;
       int i=0;
       double x1,x2;
       while(f[i]!='#'){
             if(f[i]>='0'&&f[i]<='9') obst[top++]=readnumber(f,&i);
             else if(f[i]==' ') i++;
             else{
                  x2=obst[--top];
                  x1=obst[--top];
                  if(f[i]=='+') obst[top]=x1+x2;
                  else if(f[i]=='-') obst[top]=x1-x2;
                  else if(f[i]=='*') obst[top]=x1*x2;
                  else if(f[i]=='/') obst[top]=x1/x2;
                  i++;
                  top++;
             }
       }
       return obst[0];
}

int priority(char op){
    switch (op)
    {
    	case '#':return -1;//在比较操作符优先级时,保证栈不空
    	case '(':return 0;//当栈顶元素为(时运算符入栈
    	case '+':
    	case '-':return 1;
    	case '*':
    	case '/':return 2;
    	default:return -1;
    }
}

int is_operation(char op){
    switch (op)
    {
    	case '+':
    	case '-':
    	case '*':
    	case '/':return 1;
    	default:return 0;
    }
}

void postfix(char *e,char *f){
     int i=0,j=0,top=1,t;
     char opst[100];
     opst[0]='#';
     while(e[i]!='#'){
           if((e[i]>='0'&&e[i]<='9')||(e[i]=='.')) f[j++]=e[i];//遇到数字和小数点直接写入后缀表达式
           else if(e[i]=='(') opst[top++]=e[i];//遇到左括号进入操作符栈
           else if(e[i]==')'){
                   t=top-1;//遇到右括号将其对应的左括号后的操作符号全部写入后缀表达式
                   while(opst[t]!='('){
                         f[j++]=opst[t];
                         t--;
                   }
                   top=t;//左括号出栈
           }
           else if(is_operation(e[i])){
                   f[j++]=' ';//用空格分开两个操作数
                   while(priority(opst[top-1])>=priority(e[i]))
                         f[j++]=opst[--top];
                   opst[top++]=e[i];//当前元素进栈
           }
           i++;//处理下一个元素
     }
     while(top) f[j++]=opst[--top];
     f[j]=0;
}

int main(){
    char s[100],ts[100];
    int n;
    printf("please input the number of expressions you want to calculate:\n");
    scanf("%d",&n);
    getchar();
    while(n--){
          printf("please input your expressions(end with'#'):\n");
          gets(s);
          postfix(s,ts);
          //printf("%s\n",ts);
          printf("the answer is: %f\n",evalpost(ts));
    }
    return 0;
}

说明:本文大部分内容来自《数据结构(C语言版)》(李云清、杨庆红、揭安全编著),写此文只为备忘,转载请留此说明,谢谢。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值