实现在实数范围内的表达式求值(不包括负数)

#include<iostream>     
#include<cstring>
#include<cstdio>
#include<cctype>
#include<stack>
using namespace std;
stack<char> optr;//运算符栈
stack<float> opnd;//操作数栈
int num=0;
int getIndex(char theta){
    int index=0;
    switch(theta){
        case '+':
            index = 0;
            break;
        case '-':
            index = 1;
            break;
        case '*':
            index = 2;
            break;
        case '/':
            index = 3;
            break;
        case '(':
            index = 4;
            break;
        case ')':
            index = 5;
            break;
        case '#':
            index = 6;
        default:break;
    }
    return index;
}
char getPriority(char theta1,char theta2){
    const char priority[][7]{
        { '>','>','<','<','<','>','>' },
        { '>','>','<','<','<','>','>' },
        { '>','>','>','>','<','>','>' },
        { '>','>','>','>','<','>','>' },
        { '<','<','<','<','<','=','0' },
        { '>','>','>','>','0','>','>' },
        { '<','<','<','<','<','0','=' },
    };
    int index1 = getIndex(theta1);
    int index2 = getIndex(theta2);
    return priority[index1][index2];
}//取得优先级 
float calculate(float b, char theta, float a)   //计算b theta a
{
    switch (theta)
    {
    case '+':
        return b + a;
    case '-':
        return b - a;
    case '*':
        return b * a;
    case '/':
        return b / a;
    default:
        break;
    }
}
//输入的表达式要以'#'结尾
float EvaluteExpression(){
    optr.push('#');
    char ch;
    ch=getchar();
    int counter=0;//标志符,若上一次输入的为数字,记为1,若为2,说明记录的是小数;
    while(ch!='#'||optr.top()!='#'){
        if(isdigit(ch)){
            if(counter==1){     
                float t=opnd.top();
                opnd.pop();
                t=t*10+(ch-'0');
                opnd.push(t);
                num=0;
            }
            else if(counter==2)
            {
                float t=opnd.top();
                opnd.pop();
                float temp=ch-'0';
                for(int i=0;i<=num;i++){
                    temp=temp*0.1;
                }
                    cout<<temp<<endl;
                t+=temp;
                num++;
                opnd.push(t);

            }
            else{
                opnd.push(ch-'0');
                counter=1;
            }//若counter为1,表示上一次读入的也为数字,所以要将运算数的栈顶取出并结合 
            cin>>ch;
        }
        else if(ch=='.'){
            cin>>ch;
            float t=opnd.top();
            opnd.pop();
            t=t+(ch-'0')*0.1;
            opnd.push(t);
            counter=2;
            num++;
            cin>>ch;
        } 
        else{
            counter=0;
            num=0;
            switch(getPriority(optr.top(),ch))
            {
                case '<':
                    optr.push(ch);
                    cin>>ch;
                    break;//如果读入的运算符优先级比栈顶大,则直接压栈
                case '=':
                    optr.pop();
                    cin>>ch;
                    break;//若优先级相等,表示读入的为)且栈顶为(,此时只要直接将(出栈即可;
                case '>':
                    char c = optr.top();
                    optr.pop();
                    float b = opnd.top();
                    opnd.pop();
                    float a = opnd.top();
                    opnd.pop();
                    opnd.push(calculate(a,c,b));//若读入的运算符优先级比栈顶小,则需从运算数栈中取出两个数进行计算,并将结果压入运算数栈
            }
        }
    }
    return opnd.top();
}
int main(){
    float answer=EvaluteExpression();
    cout<<answer;
}

代码可完美运行,原理使用栈实现表达式求值,详情参考数据结构严版,本代码是基于课本上代码的改进,实现多位数及浮点数的运算,负数的实现也很简单,如果读入为-号的话将其相反数压入栈中即可,大家可以自己尝试,小白第一次发帖,希望大家多多指教

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值