-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortingStantion.cpp
130 lines (106 loc) · 3.32 KB
/
SortingStantion.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
#include<iostream>
#include <string>
#include "Stack.cpp"
using namespace std;
int priority(string i) {
if (i == "(") return 1;
if (i == "+" || i == "-") return 2;
if (i == "*" || i == "/") return 3;
if (i == "cos" || i == "sin") return 4;
if (i == "^") return 5;
return 0;
}
int SortingStantion(string str) {
setlocale(0, "Rus");
Stack <string> stc;
string postfix = "";
string sincos;
string buffer = "";
string element;
int pr;
for (int i = 0; i < str.size(); i++)
{
if ((i < str.size() - 4) && str[i] == 's' && str[i + 1] == 'i' && str[i + 2] == 'n') {
pr = 4;
i += 2;
element = "sin";
pr = priority(element);
}
else if ((i < str.size() - 4) && str[i] == 'c' && str[i + 1] == 'o' && str[i + 2] == 's') {
pr = 4;
i += 2;
element = "cos";
pr = priority(element);
}
else {
buffer += str[i];
element = buffer;
pr = priority(element);
buffer = "";
}
switch (pr)
{
case 1: { // îòêðûâàþùàÿñÿ ñêîáêà
postfix += " ";
stc.push(element);
break;
}
case 0: {
if (element == ")") {
postfix += " ";
while (!stc.empty() && stc.top() != "(") {
postfix += " ";
postfix += stc.top();
postfix += " ";
stc.pop();
}
if (!stc.empty()) {
postfix += " ";
stc.pop();
}
else {
cout << "Íåïðàâèëüíîå ðàñïîëîæåíèå ñêîáîê, èõ äóáëèðîâàíèå èëè íåäîñòàòîê\n";
return 0;
}
}
else {
postfix += element; // äîáàâëåíèå ñèìâîëà â ñòðîêó ïîñòôèêñíîé çàïèñè
}
break;
}
default:
{
if (!stc.empty())
{
postfix += " ";
while (!stc.empty() && priority(stc.top()) >= pr) {
postfix += stc.top();
postfix += " ";
stc.pop();
}
stc.push(element);
}
else {
postfix += " ";
stc.push(element);
}
break;
}
}
}
if (!stc.empty()) { // âûòàñêèâàíèå èç ñòåêà îòñàâøèõñÿ îïåðàöèé
postfix += " ";
while (!stc.empty())
{
postfix += stc.top();
postfix += " ";
stc.pop();
}
}
cout << postfix;
return 0;
}
void main() {
string a = "32323 + sin(3232^21) * cos(120 * 32 + (12 + 93) + (32 - 3)) * cos(3/4)";
SortingStantion(a);
}