Skip to content

Commit 4229f83

Browse files
#TDP - 17 Reverse Polish Notation(RPN)
Add solution for just example a+b+(c*d) a + function(x,y,z+2)
1 parent b7cb1c0 commit 4229f83

11 files changed

+246
-58
lines changed

MethodsDevelopmentTranslator.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@
158158
</ItemGroup>
159159
<ItemGroup>
160160
<Text Include="C.txt" />
161+
<Text Include="lexical.txt" />
162+
<Text Include="test.txt" />
161163
</ItemGroup>
162164
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
163165
<ImportGroup Label="ExtensionTargets">

MethodsDevelopmentTranslator.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,11 @@
5555
<Text Include="C.txt">
5656
<Filter>Файлы ресурсов</Filter>
5757
</Text>
58+
<Text Include="test.txt">
59+
<Filter>Файлы ресурсов</Filter>
60+
</Text>
61+
<Text Include="lexical.txt">
62+
<Filter>Файлы ресурсов</Filter>
63+
</Text>
5864
</ItemGroup>
5965
</Project>

RPN.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
I1 N1 O1 I2 I3 I4 I5 N2 O1 4� O2
2+

ReversePolishNotation.cpp

Lines changed: 214 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ ReversePolishNotation::~ReversePolishNotation()
88
{
99
}
1010

11-
void ReversePolishNotation::reversePolishNotationAnalyze(std::string filePathOrName_C, std::string fileName_Path_SaveAnalis)
12-
{
13-
14-
}
15-
1611
int ReversePolishNotation::getPriority(std::string word)
1712
{
1813
//priority
@@ -23,4 +18,218 @@ int ReversePolishNotation::getPriority(std::string word)
2318
}
2419
return -1;
2520
}
21+
22+
bool ReversePolishNotation::isExistsSymbolToTable(std::string word)
23+
{
24+
for (auto& item : priority)
25+
if (item.first == word)
26+
return true;
27+
return false;
28+
}
29+
30+
31+
void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_lexical, std::string fileName_RPN)
32+
{
33+
std::ifstream lexical;
34+
std::ofstream fileAnalysis(fileName_RPN);
35+
lexical.exceptions(std::ifstream::badbit);
36+
try
37+
{
38+
lexical.open(fileName_lexical);
39+
40+
if (lexical.is_open())
41+
{
42+
bool readComment = false;
43+
std::string temp = "";
44+
while (!lexical.eof())
45+
{
46+
std::string lineLexical = "";
47+
getline(lexical, lineLexical);
48+
if (lineLexical.length() > 2 && (isComment((int)lineLexical[0],(int)lineLexical[1]) || isOneStringComment((int)lineLexical[0], (int)lineLexical[1])))
49+
continue;
50+
51+
size_t pos = 0;
52+
std::string token="", tempReadFunction="";
53+
bool readFunction = false;
54+
while ((pos = lineLexical.find(' ')) != std::string::npos || lineLexical.length() !=0)
55+
{
56+
if (pos == std::string::npos)
57+
{
58+
token = lineLexical.substr(0, lineLexical.length());
59+
lineLexical = "";
60+
}
61+
else
62+
{
63+
token = lineLexical.substr(0, pos);
64+
lineLexical.erase(0, pos + 1);
65+
}
66+
if (tempReadFunction != "")
67+
{
68+
if (tempReadFunction[0] == 'I' && token == "R3")
69+
readFunction = true;
70+
else
71+
tempReadFunction = token;
72+
}else
73+
tempReadFunction = token;
74+
75+
76+
int priorityToken = getPriority(token);
77+
if (priorityToken > -1)
78+
{
79+
if (stack.size() == 0)
80+
{
81+
std::map<std::string, int> tempMap;
82+
tempMap.insert(std::pair<std::string,int>(token, 0));
83+
stack.push_back(tempMap);
84+
continue;
85+
}
86+
else
87+
{
88+
if (token == "R3")
89+
{
90+
if (readFunction == false)
91+
{
92+
std::map<std::string, int> tempMap;
93+
tempMap.insert(std::pair<std::string, int>(token, 0));
94+
stack.push_back(tempMap);
95+
continue;
96+
}
97+
else
98+
{
99+
std::map<std::string, int> tempMap;
100+
tempMap.insert(std::pair<std::string, int>("Ô", 1));
101+
stack.push_back(tempMap);
102+
continue;
103+
}
104+
105+
}
106+
if (token == "R4")
107+
{
108+
std::string end = "R3";
109+
if (readFunction == true)
110+
end = "Ô";
111+
while (stack.size() != 0 && stack.back().rbegin()->first != end)
112+
{
113+
auto upElemStack = stack.back().rbegin();
114+
fileAnalysis << upElemStack->first << " ";
115+
stack.pop_back();
116+
}
117+
if (readFunction == true && stack.size() !=0)
118+
{
119+
auto upElemStack = stack.back().rbegin();
120+
upElemStack->second++;
121+
fileAnalysis << upElemStack->second << upElemStack->first << " ";
122+
readFunction = false;
123+
}
124+
stack.pop_back();
125+
continue;
126+
}
127+
if (token == "R1")
128+
{
129+
std::map<std::string, int> tempMap;
130+
tempMap.insert(std::pair<std::string, int>("ÀÝÌ", 2));
131+
stack.push_back(tempMap);
132+
continue;
133+
}
134+
if (token == "R8")
135+
{
136+
if (readFunction == false)
137+
{
138+
auto upElemStack = stack.back().rbegin();
139+
while (stack.size() != 0 && upElemStack->first != "ÀÝÌ")
140+
{
141+
fileAnalysis << upElemStack->first << " ";
142+
stack.pop_back();
143+
if (stack.size() != 0)
144+
upElemStack = stack.back().rbegin();
145+
}
146+
upElemStack->second++;
147+
continue;
148+
}
149+
else
150+
{
151+
auto upElemStack = stack.back().rbegin();
152+
while (stack.size() != 0 && upElemStack->first != "Ô")
153+
{
154+
fileAnalysis << upElemStack->first << " ";
155+
stack.pop_back();
156+
if (stack.size() != 0)
157+
upElemStack = stack.back().rbegin();
158+
}
159+
upElemStack->second++;
160+
continue;
161+
}
162+
163+
}
164+
if (token == "R2")
165+
{
166+
auto upElemStack = stack.back().rbegin();
167+
while (stack.size() != 0 && upElemStack->first != "ÀÝÌ" )
168+
{
169+
fileAnalysis << upElemStack->first << " ";
170+
stack.pop_back();
171+
if (stack.size() != 0)
172+
upElemStack = stack.back().rbegin();
173+
}
174+
fileAnalysis << upElemStack->second << upElemStack->first << " ";
175+
stack.pop_back();
176+
continue;
177+
}
178+
auto upElemStack = stack.back().rbegin();
179+
bool upElemDownPriop = false;
180+
while (stack.size() != 0 && getPriority(upElemStack->first)>=getPriority(token))
181+
{
182+
upElemDownPriop = true;
183+
fileAnalysis << upElemStack->first << " ";
184+
stack.pop_back();
185+
if (stack.size() != 0)
186+
upElemStack = stack.back().rbegin();
187+
}
188+
if (upElemDownPriop == true)
189+
{
190+
upElemDownPriop = false;
191+
std::map<std::string, int> tempMap;
192+
tempMap.insert(std::pair<std::string, int>(token, 0));
193+
stack.push_back(tempMap);
194+
continue;
195+
}
196+
std::map<std::string, int> tempMap;
197+
tempMap.insert(std::pair<std::string, int>(token, 0));
198+
stack.push_back(tempMap);
199+
200+
}
201+
}
202+
else
203+
{
204+
fileAnalysis << token << " ";
205+
}
206+
}
207+
208+
if (stack.size() != 0)
209+
{
210+
auto upElemStack = stack.back().rbegin();
211+
while (stack.size() != 0)
212+
{
213+
fileAnalysis << upElemStack->first << " ";
214+
stack.pop_back();
215+
if (stack.size() != 0)
216+
upElemStack = stack.back().rbegin();
217+
218+
219+
}
220+
}
221+
fileAnalysis << std::endl;
222+
}
223+
}
224+
225+
}
226+
catch (const std::ifstream::failure & exep)
227+
{
228+
std::cout << " Exception opening/reading file";
229+
std::cout << exep.what();
230+
}
231+
232+
lexical.close();
233+
fileAnalysis.close();
26234
}
235+

ReversePolishNotation.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
#define REVERSEPOLISHNOTATION_H
44
#include "Translator.h"
55

6-
typedef std::vector<std::map<std::string, int>> stack_type;
6+
typedef std::vector<std::map<std::string, int>> mystack;
77

88
class ReversePolishNotation : public Translator
99
{
1010
public:
1111
ReversePolishNotation();
1212
~ReversePolishNotation();
13-
void reversePolishNotationAnalyze(std::string filePathOrName_C, std::string fileName_Path_SaveAnalis);
13+
void reversePolishNotationAnalyze(std::string fileName_lexical, std::string fileName_RPN);
1414
private:
15-
stack_type stack;
15+
mystack stack;
1616
int getPriority(std::string word);
17-
17+
bool isExistsSymbolToTable(std::string word);
1818

1919
};
2020

Translator.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ class Translator
7272
{
7373
// if ( [ ÀÝÌ Ô while for
7474
{"W5",0}, { "R3",0 },{ "R1",0 },{ "ÀÝÌ",0 },{ "Ô",0 },{"W7", 0},{"W12", 0},
75-
// { , ; ) ] else
76-
{"R5", 1},{ "R8",1 },{ "R7",1 },{ "R3",1 },{"R2", 1},{"W6", 1},
75+
// { , ; ) ] else
76+
{"R5", 1},{ "R8",1 },{ "R7",1 },{ "R4",1 },{"R2", 1},{"W6", 1},
7777
// =
7878
{"O5", 2},
7979
// |
@@ -91,6 +91,7 @@ class Translator
9191
// : }
9292
{":", 9},{"R6", 9},{"W11", 9}
9393
};
94+
9495
};
9596

9697
#endif

Translator_LanguageC.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using namespace System;
77
using namespace System::Windows::Forms;
88
using namespace System::IO;
9+
910
std::string fileText_C = "";
1011

1112
[STAThreadAttribute]
@@ -74,5 +75,12 @@ System::Void MethodsDevelopmentTranslator::Translator_LanguageC::Btn_analisator_
7475

7576
System::Void MethodsDevelopmentTranslator::Translator_LanguageC::Btn_reversePolishNotation_Click(System::Object^ sender, System::EventArgs^ e)
7677
{
77-
return System::Void();
78+
ReversePolishNotation RPN;
79+
std::string file = "";
80+
marshalString(tb_nameFileAnylize->Text, file);
81+
RPN.reversePolishNotationAnalyze(file, "RPN.txt");
82+
83+
StreamReader^ fileAnalyze = gcnew StreamReader("RPN.txt", System::Text::Encoding::GetEncoding(1251));//File::OpenText("RPN.txt");
84+
tb_reversePolishNotation->Text = fileAnalyze->ReadToEnd();
85+
fileAnalyze->Close();
7886
}

Translator_LanguageC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ namespace MethodsDevelopmentTranslator {
126126
// tb_reversePolishNotation
127127
//
128128
this->tb_reversePolishNotation->BackColor = System::Drawing::Color::Bisque;
129+
this->tb_reversePolishNotation->Font = (gcnew System::Drawing::Font(L"Times New Roman", 11.25F, System::Drawing::FontStyle::Regular,
130+
System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(204)));
129131
this->tb_reversePolishNotation->Location = System::Drawing::Point(442, 42);
130132
this->tb_reversePolishNotation->Multiline = true;
131133
this->tb_reversePolishNotation->Name = L"tb_reversePolishNotation";

include.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
#include <fstream>
88
#include <vector>
99
#include <map>
10+
#include <stack>
11+
1012

1113
#endif

lexical.txt

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1 @@
1-
W8 I1
2-
W8 I2
3-
4-
W1 I3 R3 W1 I4 R4
5-
R5
6-
W1 I5 O5 N1 R7
7-
W7 R3 I4 O10 N1 R4
8-
R5
9-
I4 O5 I4 O4 N2 R7
10-
I5 O20 R7
11-
R6
12-
W11 I5 R7
13-
R6
14-
/*test comment*/
15-
W1 I6 R3 W1 I4 R4
16-
R5
17-
W1 I7 O5 I3 R3 I4 R4 R7
18-
W1 O3 I8 O5 R3 W1 O3 R4 W9 R3 I7 O3 W10 R3 W1 R4 R4 R7
19-
W1 I9 O5 N1 R7
20-
W7 R3 I4 O10 N1 R4
21-
R5
22-
I8 R1 I9 R2 O5 I4 O6 N2 R7
23-
I4 O4 O5 N2 R7
24-
I9 O20 /*--*/ R7
25-
R6
26-
W11 I8 R7
27-
R6
28-
// Main
29-
/* double
30-
comment
31-
*/
32-
W1 W4 R3 W1 I10 R8 I11 W3 O3 I12 R1 R2 R4
33-
R5
34-
W1 I7 O5 I3 R3 N3 R4 R7
35-
W1 O3 I13 O5 I6 R3 N3 R4 R7
36-
W1 I14 O5 N1 R7
37-
W12 R3 W1 I15 O5 N1 R7 I15 O8 I7 R7 I15 O20 R4
38-
R5
39-
W5 R3 I15 O8 N4 O17 I15 O7 N1 R4
40-
I16 R3 C1 R8 I15 R8 I13 R1 I15 R2 R4 R7
41-
I14 O1 O5 I13 R1 I15 R2 R7
42-
R6
43-
I16 R3 C2 R8 I14 R4 R7
44-
I17 R3 I13 R4 R7
45-
W11 N1 R7
46-
R6
1+
I1 O1 N1 O2 I2 R3 I3 R8 I4 R8 I5 O1 N2 R4

0 commit comments

Comments
 (0)