求前缀表达式的值 (20分)

之前写过一次,因为对C++理解不深,没有使用stack<自定义数据结构>,自定义数据结构的栈实现是自己编写的,这次重新写了一下,使用内置栈对自定义数据结构进行处理。

附一个上次写的帖子链接:

(21条消息) 3-8 求前缀表达式的值 (20分)_HBU_赵文熙的博客-CSDN博客

新代码:

#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stack>

using namespace std;

typedef struct Node
{
    int type;//1为操作符,2为数字
    float num;
    char operate;
    Node() :type(),num(),operate(){}
    Node(int type,float num,char operate) :type(type),num(num),operate(operate){}
    Node(char operate) :type(1),num(0.0),operate(operate){}
    Node(float num) :type(2),num(num),operate('\0'){}
}sNode;
typedef sNode *sp;


float calculation(char a,float b,float c)
{
    //printf("计算%f/%f=%f",b,c,b/c);
    switch(a)
    {
        case '+':return b+c;
        case '-':return b-c;
        case '*':return b*c;
        case '/':return b/c;
    }

}
int main()
{
    string s;
    getline(cin,s);
    s.append(1,' ');
    stack<sNode> stackp;

    int D=0;
    for(int i=0;i<s.length()-1;i++)
    {
        if(strchr("+-*/",s[i])&&s[i+1]==' ')//单个操作符号
        {
            stackp.push(sNode(s[i]));
            i++;//两次i++跳过空格
        }
        else if(strchr("0123456789",s[i])&&s[i+1]==' ')//单个数字
        {
            stackp.push(sNode((float)((int)s[i]-(int)'0')));
            i++;//两次i++跳过空格
        }
        else //组合数字(需要判断正负号、多位数字)
        {
            int is=0;float sum=0.0;int notd=1;int wei=-1;
            if(strchr("+-",s[i]))//第一位是符号位
            {
                if(s[i]=='-') is=1;
                i++;
                //printf("判断符号\n");
            }
            while(s[i]!=' ')
            {
                if(s[i]=='.')
                    notd=0;
                else if(notd==1)
                sum=sum*10+(int)s[i]-(int)'0';//最后s[i]的位置在空格,再加1,跳过空格
                else
                sum=sum+((int)s[i]-(int)'0')*pow(10,wei--);
                i++;
                //printf("S");
            }
            if(is) sum=-sum;
            stackp.push(sNode(sum));

        }
        D++;
    }//将数据存入栈中
    if(D==1)
    {
        printf("%.1f",stackp.top().num);return 0;
    }
    stack<float> stack2;//2栈只会存放数字
    for(int i=0;i<D;i++)
    {
        if(stackp.top().type==2)
        {
            stack2.push(stackp.top().num);
            stackp.pop();
            //cout<<stack2.top()<<endl;
        }
        else//计算
        {
            char operate=stackp.top().operate;
            stackp.pop();
            //cout<<"operate="<<operate<<endl;
            float temp[3];
            for(int j=0;j<2;j++)
            {
                if(!stack2.empty())
                {
                    temp[j]=stack2.top();
                    //cout<<"temp="<<temp[j]<<endl;
                    stack2.pop();
                }
                else
                {
                    printf("ERROR");
                    return 0;
                }
            }

            if(abs(temp[1])<1e-6&&operate=='/')
            {
                printf("ERROR");
                return 0;
            }
            temp[2]=calculation(operate,temp[0],temp[1]);
            stack2.push(temp[2]);
            //cout<<stack2.top()<<endl;
        }
    }

    float out=stack2.top();
    stack2.pop();
    if(stack2.empty())
        printf("%.1f",out);
    else
        printf("ERROR");
    return 0;
}
//char

#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <stack>

using namespace std;

typedef struct Node
{
    int type;//1为操作符,2为数字
    float num;
    char operate;
    Node() :type(),num(),operate(){}
    Node(int type,float num,char operate) :type(type),num(num),operate(operate){}
    Node(char operate) :type(1),num(0.0),operate(operate){}
    Node(float num) :type(2),num(num),operate('\0'){}
}sNode;
typedef sNode *sp;


float calculation(char a,float b,float c)
{
    //printf("计算%f/%f=%f",b,c,b/c);
    switch(a)
    {
        case '+':return b+c;
        case '-':return b-c;
        case '*':return b*c;
        case '/':return b/c;
    }

}
int main()
{
    string s;
    getline(cin,s);
    s.append(1,' ');
    stack<sNode> stackp;

    int D=0;
    for(int i=0;i<s.length()-1;i++)
    {
        if(strchr("+-*/",s[i])&&s[i+1]==' ')//单个操作符号
        {
            stackp.push(sNode(s[i]));
            i++;//两次i++跳过空格
        }
        else if(strchr("0123456789",s[i])&&s[i+1]==' ')//单个数字
        {
            stackp.push(sNode((float)((int)s[i]-(int)'0')));
            i++;//两次i++跳过空格
        }
        else //组合数字(需要判断正负号、多位数字)
        {
            int is=0;float sum=0.0;int notd=1;int wei=-1;
            if(strchr("+-",s[i]))//第一位是符号位
            {
                if(s[i]=='-') is=1;
                i++;
                //printf("判断符号\n");
            }
            while(s[i]!=' ')
            {
                if(s[i]=='.')
                    notd=0;
                else if(notd==1)
                sum=sum*10+(int)s[i]-(int)'0';//最后s[i]的位置在空格,再加1,跳过空格
                else
                sum=sum+((int)s[i]-(int)'0')*pow(10,wei--);
                i++;
                //printf("S");
            }
            if(is) sum=-sum;
            stackp.push(sNode(sum));

        }
        D++;
    }//将数据存入栈中
    if(D==1)
    {
        printf("%.1f",stackp.top().num);return 0;
    }
    stack<float> stack2;//2栈只会存放数字
    for(int i=0;i<D;i++)
    {
        if(stackp.top().type==2)
        {
            stack2.push(stackp.top().num);
            stackp.pop();
            //cout<<stack2.top()<<endl;
        }
        else//计算
        {
            char operate=stackp.top().operate;
            stackp.pop();
            //cout<<"operate="<<operate<<endl;
            float temp[3];
            for(int j=0;j<2;j++)
            {
                if(!stack2.empty())
                {
                    temp[j]=stack2.top();
                    //cout<<"temp="<<temp[j]<<endl;
                    stack2.pop();
                }
                else
                {
                    printf("ERROR");
                    return 0;
                }
            }

            if(abs(temp[1])<1e-6&&operate=='/')
            {
                printf("ERROR");
                return 0;
            }
            temp[2]=calculation(operate,temp[0],temp[1]);
            stack2.push(temp[2]);
            //cout<<stack2.top()<<endl;
        }
    }

    float out=stack2.top();
    stack2.pop();
    if(stack2.empty())
        printf("%.1f",out);
    else
        printf("ERROR");
    return 0;
}
//char
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值