02-线性结构3. 求前缀表达式的值

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

输入格式说明:

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

输出格式说明:

输出前缀表达式的运算结果,精确到小数点后1位,或错误信息“ERROR”。

样例输入与输出:
|序号 |输入 |输出|
|:—:|
|1 |+ + 2 3 - 7 4 / 8 4|13.0
|2 |/ -25 +
- 2 3 4 / 8 4|12.5
|3 |/ 5 + * - 2 3 4 / 8 2|ERROR
|4 |+10.23|10.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include<iostream>
#include<sstream>
#include<string>
#include <iomanip>
using namespace std;

const int MAX_SIZE = 30;
struct Stack
{
string data[MAX_SIZE];
int top;
};

class c02_3
{
public:
c02_3();
void push(string element);
string pop();
void result(string str);
void recursion(string str);
string compute(string str1, string str2, string str3);
void print();
bool isEmpty();
bool isFull();
bool isStr(string str);
private:
Stack m_stack;
};

c02_3::c02_3()
{
m_stack.top = -1;

}
string c02_3::pop()
{
if (-1 == m_stack.top)
{
cout << "the stack is empty!" << endl;
return "";
}
else
{
return m_stack.data[m_stack.top--];
}

}

void c02_3::push(string element)
{
if (MAX_SIZE == m_stack.top)
{
cout << "the stack is full!" << endl;
return;
}
else
{
m_stack.data[++m_stack.top] = element;
}
}

bool c02_3::isEmpty()
{
if (m_stack.top == -1)
{
return true;
}
else
{
return false;
}
}

bool c02_3::isFull()
{
if (m_stack.top == MAX_SIZE - 1)
{
return true;
}
else
{
return false;
}

}
void c02_3::result(string str)
{
if (isStr(str))
{
push(str);
}
else
{
recursion(str);
}
}
bool c02_3::isStr(string str)
{
if (str == "+" || str == "-" || str == "*" || str == "/" || str == "ERROR")
{
return true;
}
else if (!str.empty())
{
return false;
}
}
string c02_3::compute(string str1, string str2, string str3)
{
double a = 0.0, b = 0.0, c = 0.0;
a = atof(str2.c_str());
b = atof(str3.c_str());
stringstream ss;
string str4;
if ("+" == str1)
{
c = a + b;
}
else if ("-" == str1)
{
c = a - b;
}
else if ("*" == str1)
{
c = a * b;
}
else if ("/" == str1)
{
if (0 == b)
{
return "ERROR";
}
c = a / b;

}
ss << c;
ss >> str4;
return str4;
}
void c02_3::recursion(string str)
{
if (!isEmpty() && !isStr(m_stack.data[m_stack.top]))
{
string strTemp = compute(m_stack.data[m_stack.top - 1], m_stack.data[m_stack.top], str);
pop();
pop();
recursion(strTemp);
}
else
{
push(str);
}
}
void c02_3::print()
{
string str = pop();
if ("ERROR" == str)
{
cout << "ERROR" << endl;
}
else if (isStr(str))
{
cout << "ERROR" << endl;
}
else
{
if (m_stack.top > 0)
{
cout << "ERROR" << endl;
}
else if (m_stack.top == 0)
{
if ("*" == m_stack.data[m_stack.top] || "/" == m_stack.data[m_stack.top])
{
cout << "ERROR" << endl;
}
}
else
{
float f1 = atof(str.c_str());
cout << setiosflags(ios::fixed) << setprecision(1) << f1 << endl;
}
}

}

int main()
{

c02_3 m_a;
string str;
while (cin >> str)
{
m_a.result(str);
}
m_a.print();
// system("pause");
return 0;
}

这是我第一次在编译器上调好,在oj上测试一次性直接通过的代码啊,泪奔!
写的比较烦,因为这是数据结构mooc上的作业,本意就是想让我们用数据结构自己写一个堆栈来做,当然我们也可以自己不写堆栈来做,直接用C++中STL里面的stack来做,这样就不用自己构造堆栈了!