-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpn.h
159 lines (147 loc) · 3.42 KB
/
rpn.h
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
#pragma once
#include<iostream>
#include <string>
#include "stack.h"
#include "table.h"
class rpn {
private:
string *stringarr;
int size;// îáúåì ïîëíîãî ìàññèâà
int count;// ðåàëüíîå êîë-âî ýëåìåíòîâ, çàíèìàåìûõ ìàññèâ
table<double> valuetable;
table<int> priortable;
stack<record<int> > priorstack;
void MakePrior() {
priortable = table<int>(10);
priortable.Add("+", 1);
priortable.Add("-", 1);
priortable.Add("*", 2);
priortable.Add("/", 2);
priortable.Add("^", 3);
}
void LeftBreaket() {
priorstack.Push(record<int>("(", 0));
}
void RightBreaket() {
while (!(priorstack.is_empty()) && priorstack.Top().Getname() != "(") {
stringarr[count++] = priorstack.Pop().Getname();
//std::cout << priorstack.Pop().Getname() << endl;
}
priorstack.Pop();
}
void Operand(string _str) {
stringarr[count++] = _str;
if ((_str[0] >= '0') && ((int)(_str[0]) <= '9'))
valuetable.Add(_str, std::stod(_str));
else valuetable.Add(_str, 0);
}
void Operation(record <int> tmp) {
int f, g ,p,q;
f = !priorstack.is_empty();
g = priorstack.Top().Getvalue() >= tmp.Getvalue();
p = priorstack.Top().Getvalue();
q = tmp.Getvalue();
while ( !(priorstack.is_empty()) && (priorstack.Top().Getvalue() >= tmp.Getvalue()) )
stringarr[count++] = priorstack.Pop().Getname();
priorstack.Push(tmp);
}
void EndString(void) {
while (!(priorstack.is_empty()))
stringarr[count++] = priorstack.Pop().Getname();
}
void To_String(string st, int &k, string *w) {
k = 0;
string sth = "+-*/^()";
int len = st.length();
int l;
for (size_t i = 0; i < len - 1; i++)
w[i] = "";
for (int i = 0; i < len; i++)
{
l = sth.find(st[i]);
if (l < 0)
w[k] += st[i];
else {
if (w[k] != "")
k++;
w[k] += st[i];
k++;
}
}
if (w[k] != "")
k++;
}
void LexemsToRpn(string str)
{
int lexsize = 0;
string *lexems = new string[str.size() + 1];
To_String(str, lexsize, lexems);
count = 0;
for (int i = 0;i < lexsize;i++)
{
if (lexems[i] == "(")
LeftBreaket();
else if (lexems[i] == ")")
RightBreaket();
else
{
int priority = priortable.Search_value(lexems[i]);
if (priority <= 0)
{
Operand(lexems[i]);
if (valuetable.Search_number(lexems[i]) < 0)
if (lexems[i][0]<'0' || lexems[i][0]>'9')
valuetable.Add(lexems[i], 0);
else
valuetable.Add(lexems[i], atoi(lexems[i].c_str()));
}
else
Operation(record<int>(lexems[i],priortable.Search_value(lexems[i])));
}
}
EndString();
delete[] lexems;
}
public:
rpn(string str = "()")
{
valuetable = table<double>(25);
stringarr = new string[100];
priorstack = stack<record<int> >(20);
size = 0;
count = 0;
MakePrior();
LexemsToRpn(str);
};
~rpn(void) {
delete[] stringarr;
}
rpn(rpn & tmp) {
//delete[] stringarr;
stringarr = new string[tmp.count];
size = tmp.size;
count = tmp.count;
for (size_t i = 0; i < count; i++)
stringarr[i] = tmp.stringarr[i];
valuetable = tmp.valuetable;
priorstack = tmp.priorstack;
};
rpn & operator=( rpn & tmp) {
if (count != tmp.count)
delete[] stringarr;
stringarr = new string[tmp.count];
count = tmp.count;
for (int i = 0;i < count;i++)
stringarr[i] = tmp.stringarr[i];
valuetable = tmp.valuetable;
priorstack = tmp.priorstack;
return *this;
};
string * Getrpn() {
return stringarr;
}
table<double> Getvaluetable() {
return valuetable;
}
int Getsize() { return count; }
};