表达式的应用

来源:互联网 发布:linux piwik 安装 编辑:程序博客网 时间:2024/05/04 19:04

这个是主要用到了栈的思想;

下边是我用来调试的代码,供大家参考:

#include<iostream>
#include<stack>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
//char a[100];
stack <double>operands;//运算操作数
stack<char> operators;//运算操作符
stack<int> points;//小数的小数部分
int Priority(char a,char b){//判断优先级
if(b=='\n')if(a=='#')return 0;else return 1;
else if(a=='('){
    if(b==')')  return 0;
    else return-1;
}
else if(a==')')return 1;
else if(a=='#') if(b=='#')return 0;else return -1;
else if(a=='+'||a=='-'){
    if(b=='*'||b=='/'||b=='(')return -1;
    else return 1;
}
else if(a=='*'||a=='/'){
    if(b=='(')return -1;
    else return 1;
}
}
double arithmetic(double a,char ch,double b){//算术部分
             if(ch=='+')return (double)(a+b);
        else if(ch=='-')return (double)(a-b);
        else if(ch=='*')return (double)(a*b);
        else if(ch=='/')return (double)(1.0*a/b);
}char ch;
int i=0;
void solve()
{
    operators.push('#');
        ch=getchar();
        double t=0;
        int num=0;
        bool isHavePoint=false;
        while(ch!='\n'||operators.top()!='#')//输入的时候是输入一个字符串,然后以换行结束
        {
            if(ch<='9'&&ch>='0'){
                    if(isHavePoint)
                    {
                        points.push(ch-'0');
                        ch=getchar();
                        continue;
                    }
                    num=0;
                    t=t*10+ch-'0';
                    ch=getchar();
                    continue;
            }
            if(num==0){//只能够让一个多位数的整数部分的数进栈一次,否则像22+22*3的例子里边,会有0多余的进栈一次
                    num++;
            operands.push(t);
//            cout<<"即将进操作数栈的整数部分"<<":"<<t<<endl;
            t=0;
            }
            if(ch=='.'){
                    isHavePoint=true;
                    ch=getchar();continue;
            }
            if(isHavePoint){
            isHavePoint=false;
            double tt=0;
            while(!points.empty())
            {
                int a=points.top();
                points.pop();
                tt=(tt+a)*0.1;
            }
            double x=operands.top();
            operands.pop();
            x+=tt;
//            cout<<"加上整数部分即将进操作数栈的小数部分的数的和"<<":"<<x<<endl;
            operands.push(x);
            }
            if(Priority(operators.top(),ch)>0)
            {
                char op=operators.top();
                operators.pop();
                double a=operands.top();
                operands.pop();
                double b=operands.top();
                operands.pop();
                double result=arithmetic(b,op,a);
                operands.push(result);
            }
            else if(Priority(operators.top(),ch)==0)
            {
                operators.pop();
                ch=getchar();
            }
            else
            {
                operators.push(ch);
                ch=getchar();
            }
        }
        double result=operands.top();
        while(!operands.empty())operands.pop();
        while(!operators.empty())operators.pop();
        cout<<result<<endl;
//        getchar();//如果是以#作为结束标志的话就需要这个
}
int main()
{
    while(true)
    {
        cout<<"请输入一个算术表达式,以换行结束,注意不要输入错误,否则会崩"<<endl;
        solve();
    }
}
//(23+4.5*2)/4          -----------------------------------------------------8
//22+22*3                -----------------------------------------------------88
//(22+3)*(33/3.0)     ------------------------------------------------------275

0 0