算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。前缀表达式指二元运算符位于两个运算数之前,例如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;
}
如有优化,请留言。