数据结构之中缀式计算

还是直接贴代码

 

// project1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<stack>
#include<sstream>
#include<ctype.h>
#define LENGTH 1000
using namespace std;

int priority(char op){
    switch(op){
    case '(':
        return 0;
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
        return 2;
    default:
        return -1;
    }
}
//将一个中缀表达式转换成后缀表达式
string inffix_to_suffix(char exp[]){
    stack<char> op_stk;
    string suffix;
    char *ptr=exp;
    char operand[LENGTH];
    int i=0;
    while(*ptr!='\0'){
        if(isdigit(*ptr) || *ptr=='.' || *ptr=='E' || *ptr=='e')
            operand[i++]=*ptr;
        else{
            operand[i]='\0';
            i=0;
            if(operand[0]!='\0')
                suffix+=string(" ").append(operand);
            if(*ptr!='#'){//exp以#结束
                    switch (*ptr){
                        case '('://入栈
                            op_stk.push(*ptr);break;
                        case ')'://弹出所有在左括号之前的操作符
                            while(!op_stk.empty() && op_stk.top()!='('){
                                suffix+=' ';suffix+=op_stk.top();
                                op_stk.pop();
                            }
                            if(op_stk.top()=='(')
                                op_stk.pop();
                            break;
                        case '+':
                        case '-':
                        case '*':
                        case '/':
                            while(!op_stk.empty() && priority(op_stk.top())>=priority(*ptr)){
                                suffix+=' ';suffix+=op_stk.top();
                                op_stk.pop();
                            }
                            op_stk.push(*ptr);
                            break;
                        default:
                            break;
                }

            }
            else {
                while(! op_stk.empty()){
                    suffix+=' ';suffix+=op_stk.top();
                    op_stk.pop();
                }
                break;
            }
        }
        ptr++;
    }
    return suffix;//返回一个后缀表达式的string串
}
//计算一个后缀表达式的值
double cal_suffix(string suffix){
    istringstream iss(suffix);
    string str;
    double op1,op2;
    stack<double> stk;
    while(iss>>str){
        if(str.length()==1){//操作符
            op1=stk.top();stk.pop();
            op2=stk.top();stk.pop();
            switch(str[0]){
            case '+':
                stk.push((op1+op2));break;
            case '-':
                stk.push((op2-op1));break;
            case '*':
                stk.push((op1*op2));break;
            case '/':
                stk.push((op2/op1));break;
            }
        }
        else stk.push(atof(str.c_str()));

    }
    if(!stk.empty())
        return stk.top();
}

int _tmain(int argc, _TCHAR* argv[])
{
    char exp[LENGTH]="(3.14+5.0E1/10-4.14)*(0.12-0.02)/0.1-5.01e2*0.01#";
    string suffix=inffix_to_suffix(exp);
    cout<<suffix<<endl;
    double result=cal_suffix(suffix);
    cout<<result<<endl;
    return 0;
}



 

 

 

posted @ 2013-11-20 14:06  IT_cnblogs  阅读(209)  评论(0编辑  收藏  举报