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

该博客主要讨论如何实现计算前缀表达式值的程序。作者对比了两种不同的C++代码实现,尝试理解并解决代码运行结果不正确的问题。代码涉及栈数据结构的使用,处理包括数字、运算符、小数点和负号在内的各种情况,同时检查除法运算中的除数为零错误。
摘要由CSDN通过智能技术生成

3-8 求前缀表达式的值 (20 分)
算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。前缀表达式指二元运算符位于两个运算数之前,例如2+3*(7-4)+8/4的前缀表达式是:+ + 2 * 3 - 7 4 / 8 4。请设计程序计算前缀表达式的结果值。

输入格式:
输入在一行内给出不超过30个字符的前缀表达式,只包含+、-、*、/以及运算数,不同对象(运算数、运算符号)之间以空格分隔。

输出格式:
输出前缀表达式的运算结果,保留小数点后1位,或错误信息ERROR。

输入样例:

    • 2 * 3 - 7 4 / 8 4
      结尾无空行
      输出样例:
      13.0
      明明那个就是照着人家的C++代码改的,也运行出来了,但是就是答案不正确
#include<stdio.h>
int main()
{
    char temp1,str[1000];
    double stack [1000];
    int  k=0,top=-1;
    double res=0;
    while((temp1=getchar())!='\n')
    {
        if(temp1==' ')
            temp1='#';
        str[k++]=temp1;
    }
    str[k]='\0';
    for(int i=k-1;i>=0;--i)//从右向左遍历
    {
        if(str[i]=='#')continue;//空格继续
        if(str[i]>='0'&&str[i]<='9')
        {
            double num=str[i]-'0',mul=10;
            i--;
            for(;i>=0;--i)//解决连续数字
            {
                if(str[i]>='0'&&str[i]<=9)
                {
                    num+=(str[i]-'0')*mul;
                    mul=mul*10;
                }
                else if(str[i]=='.')//解决小数点的问题
                {
                    num=num/mul;//说明刚刚求出的是小数,除以mul就是小数了
                    mul=1;
                }
                else if(str[i]=='-')//解决负号问题
                {
                    num=-num;
                }
                else 
                    break;
            }
            stack[++top]=num;//是数字就进栈
        }
        else//是运算符的话就从栈里取出两个数进行运算
        {
            double l=stack[top--];
            double r=stack[top--];
           
            if(str[i]=='+')res=l+r;
            if(str[i]=='-')res=l-r;
            if(str[i]=='*')res=l*r;
            if(str[i]=='/')
            {
                if(r==0)
                {
                    printf("ERROR\n");
                    return 0;
                }
                else
                    res=l*1.0/r;

            }
            stack[++top]=res;
        }
    }
    printf("%.1f\n",stack[top]);

    return 0;
}

这是别人的代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <cmath>

using namespace std;

char str[200005];
stack<double>s;

int main()
{
	//ios::sync_with_stdio(0);
	//cin.tie(0); cout.tie(0);

	char temp;
	int k = 0;

	while ((temp = getchar()) != '\n')
	{
		if (temp == ' ')
			temp = '#';
		str[k++] = temp;
	}

	str[k] = '\0';

	int cnt = 0;

	for (int i = k - 1; i >= 0; --i)
	{
		if (str[i] == '#') continue;
		if (str[i] >= '0' && str[i] <= '9')
		{
			double num = str[i] - '0', mul = 10;
			i--;
			for (;i >= 0; --i)
			{
				if (str[i] >= '0' && str[i] <= '9')
				{
					num += (str[i] - '0') * mul;
					mul *= 10;
				}
				else if (str[i] == '.')
				{
					num /= mul;
					mul = 1;
				}
				else if (str[i] == '-')
				{
					num = -num;
				}
                else 
                    break;
			}
			s.push(num);
		}
		else
		{
			double l = s.top(); s.pop();
			double r = s.top(); s.pop();
			double res;
			if (str[i] == '+') res = l + r;
			if (str[i] == '-') res = l - r;
			if (str[i] == '*') res = l * r;
			if (str[i] == '/')
			{
				if (r == 0)
				{
					printf("ERROR\n");
					return 0;
				}
				res = l * 1.0 / r;
			}

			s.push(res);
		}
	}

	printf("%.1lf\n", s.top());

	return 0;
}

我试一下自己写个栈吧

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
    double data[30];
    int top;
}Stack;
Stack* Init();
Stack* Init()
{
    Stack *s;
    s=(Stack*)malloc(sizeof(Stack));
    s->top=-1;
    return s;
}
int Empty_Stack(Stack*s);
int Empty_Stack(Stack*s)
{
    if(s->top==-1)
        return 1;
    return 0;
}
int Push_Stack(Stack*s,double x);
int Push_Stack(Stack*s,double x)
{
    if(s->top==29)
        return 1;
    else
    {
        s->top++;
        s->data[s->top]=x;
        return 1;
    }
}
double Pop_Stack(Stack *s);
double Pop_Stack(Stack *s)
{
    double x;
    if(Empty_Stack(s))
        return 0;
    else
    {
        x=s->data[s->top];
        s->top--;
        return x;
    }
}
double Top_Stack(Stack*s);
double Top_Stack(Stack*s)
{
     double x;
    if(Empty_Stack(s))
        return 0;
    else
    {
        x=s->data[s->top];
        return x;
    }
}
int main()
{
    char temp1,str[30];
    int  k=0;
    Stack *s;
    s=Init();
    while((temp1=getchar())!='\n')
    {
        if(temp1==' ')
            temp1='#';
        str[k++]=temp1;
    }
    str[k]='\0';
    for(int i=k-1;i>=0;i--)//从右向左遍历
    {
        if(str[i]=='#')continue;//空格继续
        if(str[i]>='0'&&str[i]<='9')
        {
            double num=str[i]-48,mul=10;
            if(str[i-1]!='#')
            {
             i--;
            for(;i>=0;i--)//解决连续数字
            {
                if(str[i]>='0'&&str[i]<=9)
                {
                    num+=(str[i]-'0')*mul;
                    mul=mul*10;
                }
                else if(str[i]=='.')//解决小数点的问题
                {
                    num=num/mul;//说明刚刚求出的是小数,除以mul就是小数了
                    mul=1;
                }
                else if(str[i]=='-')//解决负号问题
                {
                    num=-num;
                }
            }
            }
           Push_Stack(s,num);//是数字就进栈
        }
        else//是运算符的话就从栈里取出两个数进行运算
        {
            double l=Pop_Stack(s);
            double r=Pop_Stack(s);
            double res=0;
            if(str[i]=='+')res=l+r;
            if(str[i]=='-')res=l-r;
            if(str[i]=='*')res=l*r;
            if(str[i]=='/')
            {
                if(r==0)
                {
                    printf("ERROR\n");
                    return 0;
                }
                else
                    res=l*1.0/r;

            }
            Push_Stack(s,res);
        }
    }
    printf("%.1f\n",Top_Stack(s));

}

写完了还是不对,原来是
!!!没加引号
在这里插入图片描述
拜拜了
还是说一下这题思路吧,就是前缀表达式求值,从右向左遍历,遇到数字就把它入栈,遇到符号就把它从栈里拿出来,需要注意的是符号,多位数,小数,除法错误的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值