Доброго времени суток.
Нужно сделать программу которая переводит арифм. выражение в обратную польскую запись и считает его по ней.
Кто может помочь с написанием программы с рукописным стеком? Или скинуть другие варианты реализации данной программы.
Спасибо.
C++ |
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
| #include "stdafx.h"
#include <iostream>
#include <stack>
#include <vector>
#include <iterator>
#include <math.h>
using namespace std;
bool is_symbol(char sym)
{
switch (sym)
{
case '+': return true;
case '-': return true;
case '(': return true;
case '^': return true;
case '/': return true;
case '*': return true;
}
return false;
}
bool is_mid_priority(char sym)
{
switch (sym)
{
case '/': return true;
case '*': return true;
}
return false;
}
bool is_low_priority(char sym)
{
switch (sym)
{
case '-': return true;
case '+': return true;
}
return false;
}
bool is_high_priority(char sym)
{
return sym == '^' ? true : false;
}
string InputData()
{
cout << endl;
cout << "|--------------|" << endl;
cout << "Enter expression" << endl;
cout << "|--------------|" << endl;
char input[100];
cin.getline(input, sizeof(input));
string result = input;
return result;
}
int main()
{
string expression;
stack<char> symbols;
vector<char> exit_string;
stack<double> result;
double buffer = 0;
expression = InputData();
for (string::iterator it = expression.begin(); it != expression.end(); ++it)
{
if (isdigit(*it)) exit_string.push_back(*it);
else if (is_symbol(*it))
{
if (is_low_priority(*it))
{
if (symbols.size() && (is_mid_priority(symbols.top()) || is_high_priority(symbols.top())))
{
exit_string.push_back(symbols.top());
symbols.pop();
}
symbols.push(*it);
}
else if (is_mid_priority(*it))
{
if (symbols.size() && is_high_priority(symbols.top()))
{
exit_string.push_back(symbols.top());
symbols.pop();
}
symbols.push(*it);
}
else if (is_high_priority(*it))
{
symbols.push(*it);
}
else symbols.push(*it);
}
else if (*it == ')')
{
while (symbols.top() != '(')
{
exit_string.push_back(symbols.top());
symbols.pop();
}
if (symbols.size()) { symbols.pop(); }
}
}
while (symbols.size())
{
exit_string.push_back(symbols.top());
symbols.pop();
}
copy(exit_string.begin(), exit_string.end(), std::ostream_iterator<char>(std::cout, " "));
for (vector<char>::iterator it = exit_string.begin(); it != exit_string.end(); ++it)
{
if (isdigit(*it)) result.push(*it - '0');
else
switch (*it)
{
case '+': { buffer = result.top(); result.pop(); result.top() += buffer; break; }
case '-': { buffer = result.top(); result.pop(); result.top() -= buffer; break; }
case '*': { buffer = result.top(); result.pop(); result.top() *= buffer; break; }
case '/': { buffer = result.top(); result.pop(); result.top() /= buffer; break; }
case '^': { buffer = result.top(); result.pop(); result.top() = pow(result.top(), buffer); break; }
}
}
cout << "\nANSWER: " << result.top() << endl;
system("pause");
return 0;
} |
|