zoukankan      html  css  js  c++  java
  • 数据结构之中缀式计算

    还是直接贴代码

    // 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!=''){
            if(isdigit(*ptr) || *ptr=='.' || *ptr=='E' || *ptr=='e')
                operand[i++]=*ptr;
            else{
                operand[i]='';
                i=0;
                if(operand[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;
    }
    
    
    
     
    
     
  • 相关阅读:
    Nhibernate多表查询解决办法
    Nhibernate compositeid class must override Equals()解决办法
    SQLSERVER2005安装说明
    IList转化为Dataset C#
    Named SQL queries
    把object转换成DataSet,进行数据绑定
    使用Lucene检索文档中的关键字
    使用代理为业务操作添加安全检测
    mongodb副本集架构搭建
    Java生成中文验证码
  • 原文地址:https://www.cnblogs.com/abc123456789/p/3433446.html
Copyright © 2011-2022 走看看