PTA 求前缀表达式的值

文章介绍了如何使用C++编程语言设计一个程序来计算给定的前缀表达式的结果,涉及栈的使用、小数处理和负数考虑。主要展示了计算过程和关键部分的优化策略。
摘要由CSDN通过智能技术生成

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

输入格式:

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

输出格式:

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

输入样例:

+ + 2 * 3 - 7 4 / 8 4

输出样例:

13.0

这一题和之前表达式转换的接题思路差不多,这里就不再重复,只提一些自己认为的关键部分。

1.小数读取的问题

因为进栈是先进后出,所以他小数也是小数点后的数字先进栈,只要将这部分数字放到小数点后其他部分还是原来的数字数据处理。

2.负数部分,对这道题TM又没有在题目中说明有负数害我调试了那么久,这个看前面的表达式转换就好了。

#include <iostream>
#include<malloc.h>
#include<iomanip>
using namespace std;
#define MaxSize 40
typedef char ElemType;
typedef struct
{
	ElemType data[MaxSize];
	int top;
}SqStack;
bool flag=true;
void InitStack(SqStack*& s)
{
	s = (SqStack*)malloc(sizeof(SqStack));
	s->top = -1;
	//直接通过top充当下标来更新data的值域;
}
void DestoryStack(SqStack*& s)
{
	free(s);
}
bool StackEmtype(SqStack* s)
{
	return (s->top == -1);
}
bool Push(SqStack*& s, ElemType e)//进栈
{
	if (s->top == MaxSize - 1)
		return false;
	s->top++;
	s->data[s->top] = e;
	return true;
}
bool Pop(SqStack*& s, ElemType& e)
{
	if (s->top == -1)
		return false;
	e = s->data[s->top];
	s->top--;
	return true;
}
bool GetTop(SqStack* s, ElemType& e)
{
	if (s->top == -1)
		return false;
	e = s->data[s->top];
	return true;
}
typedef struct
{
	double data[MaxSize];
	int top;
}SqStack1;
void InitStack1(SqStack1*& s)
{
	s = (SqStack1*)malloc(sizeof(SqStack1));
	s->top = -1;
	//直接通过top充当下标来更新data的值域;
}
void DestoryStack1(SqStack1*& s)
{
	free(s);
}
bool Stack1Emtype(SqStack1* s)
{
	return (s->top == -1);
}
bool Push1(SqStack1*& s, double e)//进栈
{
	if (s->top == MaxSize - 1)
		return false;
	s->top++;
	s->data[s->top] = e;
	return true;
}
bool Pop1(SqStack1*& s, double& e)
{
	if (s->top == -1)
		return false;
	e = s->data[s->top];
	s->top--;
	return true;
}
bool GetTop1(SqStack1* s, double& e)
{
	if (s->top == -1)
		return false;
	e = s->data[s->top];
	return true;
}
double compvalue(char* postexp, int size)
{
	SqStack* p;
	InitStack(p);
	char tmp, tmp1;
	int i = 0;
	while (i < size)
	{
		Push(p, *postexp);
		postexp++;
		i++;
	}
	bool flag1 ;
	SqStack1* q;
	InitStack1(q);
	double a, b, c, e, point, x;
	while (!StackEmtype(p))
	{
		GetTop(p, tmp);
			point = 1, x = 0;
			flag1 = false;
			while (!StackEmtype(p))
			{
				Pop(p, tmp);
				
				if (tmp >= '0' && tmp <= '9')
				{
					flag1 = true;
					x = x + (tmp - '0') * point;
					point *= 10;
				}
				else if (tmp == '.')
				{
					x = 1.0 * x / point;
					point = 1;
				}
				else if (tmp == '-' && flag1)
				{
					x = -x;
					Pop(p, tmp);
				}
				if (tmp == ' '&&flag1||StackEmtype(p)&&flag1)
				{
					Push1(q, x);
					point = 1, x = 0;
					flag1 = false;
					continue;
				}
				switch (tmp) {
				case '+':
					Pop1(q, a);
					Pop1(q, b);
					c = a + b;
					Push1(q, c);
					break;
				case'-':

					Pop1(q, a);
					Pop1(q, b);
					c = a - b;
					Push1(q, c);
					break;
				case'*':
					Pop1(q, a);
					Pop1(q, b);
					c = b * a;
					Push1(q, c);
					break;
				case'/':
					Pop1(q, a);
					Pop1(q, b);
					if (b == 0)
					{
						flag = false;
						return 0;
					}
					c = (a * 1.0) / b;
					Push1(q, c);
					break;
				}
			}
		}
		GetTop1(q, e);
		return e;
	}

int main()
{
	char a;
	int i = 0;
	char postexp[MaxSize];
	while ((a = getchar()) != '\n')
	{
		postexp[i] = a;
		i++;
	}
	double target = compvalue(postexp,i);
	if (!flag)
	{
		cout << "ERROR" << endl;
	}
	else
		cout << fixed << setprecision(1) << target << endl;
		return 0;

}

如有优化,请留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值