-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuPN.cpp
131 lines (129 loc) · 3.69 KB
/
uPN.cpp
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
//---------------------------------------------------------------------------
#pragma hdrstop
#include "uPN.h"
#include <stack>
#include <cmath>
using namespace std;
//---------------------------------------------------------------------------
typedef struct _Notation {
UnicodeString Value;
int RelPrior, StackPrior, Rank;
} Notation;
//---------------------------------------------------------------------------
Notation MakeNotation(UnicodeString Value, int RelPrior, int StackPrior, int Rank)
{
Notation Item;
Item.Value = Value;
Item.RelPrior = RelPrior;
Item.StackPrior = StackPrior;
Item.Rank = Rank;
return Item;
}
Notation* ParseInputStr(UnicodeString Str, int *size)
{
Notation* PostFix = new Notation[*size];
int i = 1, Current = 0;
while (i <= *size)
{
if (UnicodeString(Str[i]) == "+" || UnicodeString(Str[i]) == "-") {
PostFix[Current++] = MakeNotation(Str[i], 1, 2, -1);
} else if (UnicodeString(Str[i]) == "*" || UnicodeString(Str[i]) == "/") {
PostFix[Current++] = MakeNotation(Str[i], 3, 4, -1);
} else if (UnicodeString(Str[i]) == "^") {
PostFix[Current++] = MakeNotation(Str[i], 6, 5, -1);
} else if (UnicodeString(Str[i]) == "(") {
PostFix[Current++] = MakeNotation(Str[i], 9, 0, -2);
} else if (UnicodeString(Str[i]) == ")") {
PostFix[Current++] = MakeNotation(Str[i], 0, -2, -2);
} else if ((UnicodeString(Str[i]) >= "0" && UnicodeString(Str[i]) <= "9")
|| UnicodeString(Str[i]) == ",")
{
UnicodeString Value = "";
while (i <= *size && ((UnicodeString(Str[i]) >= "0" &&
UnicodeString(Str[i]) <= "9") || UnicodeString(Str[i]) == ","))
{
Value = Value + UnicodeString(Str[i]);
i++;
}
i--;
PostFix[Current++] = MakeNotation(Value, 7, 8, 1);
}
i++;
}
Notation *Temp = new Notation[Current];
for (int i = 0; i < Current; i++) {
Temp[i] = PostFix[i];
}
*size = Current;
return Temp;
}
Notation* StackConversion(Notation *PostFix, int *size) {
int Current = 0;
Notation* Result = new Notation[*size];
stack<Notation> Stack;
for (int i = 0 ; i < *size; i++) {
if (Stack.empty()) {
Stack.push(PostFix[i]);
} else if (PostFix[i].RelPrior > Stack.top().StackPrior) {
Stack.push(PostFix[i]);
} else if (PostFix[i].Value == ")") {
while (Stack.top().Value != "(")
{
Result[Current++] = Stack.top();
Stack.pop();
}
Stack.pop();
} else {
while (!Stack.empty() &&
(PostFix[i].RelPrior <= Stack.top().StackPrior)) {
Result[Current++] = Stack.top();
Stack.pop();
}
Stack.push(PostFix[i]);
}
}
while (!Stack.empty()) {
Result[Current++] = Stack.top();
Stack.pop();
}
Notation* Temp = new Notation[Current];
for (int i = 0; i < Current; i++) {
Temp[i] = Result[i];
}
*size = Current;
return Temp;
}
Currency Calculate(Notation* PostFix, int size) {
stack<Currency> Stack;
for (int i = 0; i < size; i++) {
if (PostFix[i].Rank == 1) {
Stack.push(StrToCurr(PostFix[i].Value));
} else {
Currency a = Stack.top();
Stack.pop();
Currency b = Stack.top();
Stack.pop();
if (PostFix[i].Value == "+") {
a = b + a;
} else if (PostFix[i].Value == "-") {
a = b - a;
} else if (PostFix[i].Value == "*") {
a = b * a;
} else if (PostFix[i].Value == "/") {
a = b / a;
} else if (PostFix[i].Value == "^") {
a = (Currency) pow(b, (int) a);
}
Stack.push(a);
}
}
return Stack.top();
}
Currency GetValue(UnicodeString Str)
{
int size = Str.Length();
Notation *PostFix = new Notation[size];
PostFix = ParseInputStr(Str, &size);
PostFix = StackConversion(PostFix, &size);
return Calculate(PostFix, size);
}