Skip to content

Commit 101633c

Browse files
#TDP-9 Change && Modify
1. Add method for check operation arif and logic 2. Fixed Bug 3. Small refactoring
1 parent fb97045 commit 101633c

File tree

6 files changed

+136
-123
lines changed

6 files changed

+136
-123
lines changed

C.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ int main(int argc, const char* argv[])
3232
int sum7Digit7 = 0;
3333
for (int i = 0; i < size; i++)
3434
{
35-
printf("array[%d] = %d \n", i, digitsNumber[i]);
35+
if(i<100 && i >0)
36+
printf("array[%d] = %d \n", i, digitsNumber[i]);
3637
sum7Digit7 += digitsNumber[i];
3738
}
3839
printf("Summa digit: %d", sum7Digit7);

MethodsDevelopmentTranslator.cpp

Lines changed: 56 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -25,172 +25,134 @@ using namespace std;
2525
Cycle: while() {..}
2626
*/
2727

28-
bool isSeparators(int elem)
29-
{
30-
return elem == 40 || elem == 41 || elem == 91 || elem == 93 || elem == 123 || elem == 125 || elem == 59 ? true : false;
31-
}
32-
33-
//\t\a\b...
34-
bool isServiceSymbols(int elem)
35-
{
36-
return elem == 7 || elem == 8 || elem == 9 || elem == 10 || elem == 11 || elem == 12 || elem == 13 ? true : false;
37-
}
38-
/* +-/*/
39-
bool isOperation(string str, int ind)
40-
{
41-
return (str[ind] == '+' && str[ind + 1] != '+') || str[ind] == '-' || str[ind] == '/' || str[ind] == '*' ? true : false;
42-
}
43-
44-
int main()
28+
int main(int argc, char* argv[])
4529
{
4630
ifstream fileC;
47-
ofstream fileAnalysis("lexical.txt");
31+
ofstream fileAnalysis("C:\\Users\\swat5\\source\\repos\\StrongerProgrammer7\\MethodsDevelopmentTranslator\\lexical.txt");
4832
fileC.exceptions(ifstream::badbit);
4933
try
5034
{
51-
fileC.open("C.txt");
35+
fileC.open("C:\\Users\\swat5\\source\\repos\\StrongerProgrammer7\\MethodsDevelopmentTranslator\\C.txt");
5236

5337
if (fileC.is_open())
5438
{
5539
while (!fileC.eof())
5640
{
57-
string stringC = "";
41+
string stringLanguageC = "";
5842
string temp = "";
59-
getline(fileC, stringC);
60-
for (int i = 0; i < stringC.length(); i++)
43+
getline(fileC, stringLanguageC);
44+
for (unsigned int i = 0; i < stringLanguageC.length(); i++)
6145
{
62-
if (isServiceSymbols((int)stringC[i]) == true)
46+
if (isServiceSymbols((int)stringLanguageC[i]) == true)
6347
continue;
6448

65-
if (isSeparators((int)stringC[i]) == true && temp[0] != '\"')
49+
if (isSeparators((int)stringLanguageC[i]) == true && temp[0] != '\"')
6650
{
6751
if (temp.length() != 0)
6852
fileAnalysis << getCodeWord(temp) << " ";
69-
temp = stringC[i];
53+
temp = stringLanguageC[i];
7054
fileAnalysis << getCodeWord(temp) << " ";
7155
temp = "";
7256
continue;
7357
}
74-
if (isOperation(stringC,i)==true)
75-
{
76-
string temp2 = "";
77-
temp2 += stringC[i];
78-
if (stringC[i + 1] == '+' || stringC[i + 1] == '-')
79-
{
80-
temp2 += stringC[i + 1];
81-
i++;
82-
}
83-
84-
fileAnalysis << getCodeWord(temp2) << " ";
85-
temp = "";
86-
continue;
87-
}
8858

89-
// <library.h>
90-
if (stringC[i] == '<' || stringC[i] == '\"')
59+
// <library.h> and "string"
60+
if (stringLanguageC[i] == '<' || stringLanguageC[i] == '\"')
9161
{
9262
int posClose = 0;
9363
int countSymbols = 0;
94-
if (stringC[i] == '<')
95-
{
96-
posClose = stringC.find(">", 1);
97-
if (posClose != -1)
98-
{
99-
countSymbols = stringC.length() - i;
100-
temp.assign(stringC, i, countSymbols);
101-
}
102-
103-
}
64+
if (stringLanguageC[i] == '<')
65+
posClose = stringLanguageC.find(">", 1);
10466
else
67+
posClose = stringLanguageC.rfind('\"');
68+
69+
if (posClose != -1)
10570
{
106-
posClose = stringC.rfind('\"');
107-
if (posClose != -1)
71+
countSymbols = posClose + 1 - i;
72+
temp.assign(stringLanguageC, i, countSymbols);
73+
if (temp.find(".h") != -1)
10874
{
109-
countSymbols = posClose+1 - i;
110-
temp.assign(stringC, i, countSymbols);
75+
fileAnalysis << getCodeWord(temp) << " ";
76+
temp = "";
77+
if (stringLanguageC[posClose + 1] == '\0')
78+
break;
79+
else
80+
i = posClose;
11181
}
112-
113-
}
114-
if (temp.find(".h") != -1)
115-
{
116-
fileAnalysis << getCodeWord(temp) << " ";
117-
temp = "";
118-
if (stringC[posClose + 1] == '\0')
119-
break;
12082
else
121-
i = posClose;
122-
}
123-
else
124-
{
125-
if (temp[0] == '\"')
12683
{
127-
if (temp != "" && (int)temp[temp.length() - 2] != 92)
84+
if (temp[0] == '\"')
12885
{
12986
fileAnalysis << getCodeWord(temp) << " ";
13087
i = posClose;
88+
13189
}
13290
}
133-
else
134-
{
135-
string temp2 = "";
136-
temp2 += stringC[i];
137-
fileAnalysis << getCodeWord(temp2) << " ";
138-
}
139-
14091
temp = "";
92+
//continue;
93+
}
94+
}
14195

96+
if (isOperation(stringLanguageC, i) == true || isLogicalOperation(stringLanguageC, i) == true)
97+
{
98+
if (isIncrement(stringLanguageC, i) == true || isDoubleOperation(stringLanguageC, i) == true)
99+
{
100+
temp += stringLanguageC[i];
101+
i++;
142102
}
103+
temp += stringLanguageC[i];
104+
fileAnalysis << getCodeWord(temp) << " ";
105+
temp = "";
143106
continue;
144107
}
145108

146-
if (stringC[i] != ' ')
109+
if (stringLanguageC[i] != ' ')
147110
{
148-
if (isLetter((int)stringC[i]) == true && (isLetter((int)stringC[i+1])==false && isDigit((int)stringC[i+1])==false))
111+
if (isLetter((int)stringLanguageC[i]) == true && (isLetter((int)stringLanguageC[i+1])==false && isDigit((int)stringLanguageC[i+1])==false))
149112
{
150-
if (temp[0] == '\"')
113+
/*if (temp[0] == '\"')
151114
{
152-
if (stringC[i] == '\"' && stringC[i + 1] == ' ' && stringC[i + 1] == '\0')
115+
if (stringLanguageC[i] == '\"' && stringLanguageC[i + 1] == ' ' && stringLanguageC[i + 1] == '\0')
153116
{
154-
temp += stringC[i];
117+
temp += stringLanguageC[i];
155118
fileAnalysis << getCodeWord(temp) << " ";
156119
temp = "";
157120
continue;
158121
}
159122
else
160123
{
161-
temp += stringC[i];
124+
temp += stringLanguageC[i];
162125
continue;
163126
}
164127
}
165128
else
166-
{
167-
temp += stringC[i];
129+
{*/
130+
temp += stringLanguageC[i];
168131
fileAnalysis << getCodeWord(temp) << " ";
169132
temp = "";
170133
continue;
171-
}
134+
172135
}
173136
else
174137
{
175-
if (stringC[i] == '#')
138+
if (stringLanguageC[i] == '#')
176139
{
177-
temp += stringC[i];
140+
temp += stringLanguageC[i];
178141
continue;
179142
}
180143

181144
}
182-
temp += stringC[i];
145+
temp += stringLanguageC[i];
183146
}
184147
else
185148
{
186-
if (stringC[i] == ' ' && temp == "\0")
149+
if (temp == "\0")
187150
continue;
188151
else
189-
if (stringC[i] == ' ')
190-
{
191-
fileAnalysis << getCodeWord(temp) << " ";
192-
temp = "";
193-
}
152+
{
153+
fileAnalysis << getCodeWord(temp) << " ";
154+
temp = "";
155+
}
194156
}
195157
}
196158
if (temp != "\0")

function.cpp

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,62 @@
11
#include "table.h"
22
#include "function.h"
33

4-
bool isLetter(int elem)
4+
// ( ) [ ] { } ;
5+
bool isSeparators(int const &elem)
6+
{
7+
return elem == 40 || elem == 41 || elem == 91 || elem == 93 || elem == 123 || elem == 125 || elem == 59 ? true : false;
8+
}
9+
10+
// \a \b \t \n \v \f \r
11+
bool isServiceSymbols(int const &elem)
12+
{
13+
return elem == 7 || elem == 8 || elem == 9 || elem == 10 || elem == 11 || elem == 12 || elem == 13 ? true : false;
14+
}
15+
/* +- * / % */
16+
bool isOperation(string const &str, int const &ind)
17+
{
18+
return str[ind] == '+' || str[ind] == '-' || str[ind] == '/' || str[ind] == '*' || str[ind]== '=' || str[ind] == '%' ? true : false;
19+
}
20+
21+
/* < > == >= <= != */
22+
bool isLogicalOperation(string const& str, int const& ind)
23+
{
24+
return str[ind] == '<' || str[ind] == '>' || (str[ind] == '=' && str[ind + 1] == '=') ||
25+
(str[ind] == '>' && str[ind + 1] == '=') || (str[ind] == '<' && str[ind + 1] == '=') || (str[ind] == '!' && str[ind + 1] == '=') ? true : false;
26+
}
27+
28+
//++ --
29+
bool isIncrement(string const& str, int const& ind)
30+
{
31+
return (str[ind] == '+' && str[ind + 1] == '+') || (str[ind] == '-' && str[ind + 1] == '-') ? true : false;
32+
}
33+
34+
/* += -= *= /= */
35+
bool isDoubleOperation(string const& str, int const& ind)
36+
{
37+
return (str[ind] == '+' && str[ind + 1] == '=') || (str[ind] == '-' && str[ind + 1] == '=') ||
38+
(str[ind] == '*' && str[ind + 1] == '=') || (str[ind] == '/' && str[ind + 1] == '=') ? true : false;
39+
}
40+
41+
bool isLetter(int const &elem)
542
{
643
return (elem >= 65 && elem <= 90) || (elem >= 97 && elem <= 122) ? true : false;
744
}
845

9-
bool isDigit(int elem)
46+
bool isDigit(int const &elem)
1047
{
1148
return elem >= 48 && elem <= 57 ? true : false;
1249
}
1350

14-
bool isNumber(string num)
51+
bool isNumber(string const &num)
1552
{
16-
for (int i = 0; i < num.length(); i++)
53+
for (unsigned int i = 0; i < num.length(); i++)
1754
if (isDigit((int)num[i]) == false)
1855
return false;
1956
return true;
2057
}
2158

22-
bool isLibrary_header(string word)
59+
bool isLibrary_header(string const &word)
2360
{
2461
return (int)word[0] == 34 && (int)word[word.length() - 1] == 34 && (int)word[word.length() - 2] == 104 && (int)word[word.length() - 3] == 46 ? true : false;
2562
}

function.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
#define FUNCTION_H
44
#include "include.h"
55

6-
bool isDigit(int);
7-
bool isLetter(int);
8-
bool isLibrary_header(string);
6+
bool isSeparators(int const &);
7+
bool isServiceSymbols(int const &elem);
8+
bool isOperation(string const &str, int const &ind);
9+
bool isLogicalOperation(string const& str, int const& ind);
10+
bool isIncrement(string const& str, int const& ind);
11+
bool isDoubleOperation(string const& str, int const& ind);
12+
bool isDigit(int const &);
13+
bool isLetter(int const &);
14+
bool isLibrary_header(string const &);
915
string getServiceWordCode(string);
1016
string getOperationsCode(string str);
1117
string getSeparatorsCode(string str);

lexical.txt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ W8 I2
44
W1 I3 R3 W1 I4 R4
55
R5
66
W1 I5 O5 N1 R7
7-
W7 R3 I4 O9 N1 R4
7+
W7 R3 I4 O5 N1 R4
88
R5
99
I4 O5 I4 O4 N2 R7
10-
I5 O14 R7
10+
I5 O20 R7
1111
R6
1212
W11 I5 R7
1313
R6
@@ -16,11 +16,11 @@ R5
1616
W1 I7 O5 I3 R3 I4 R4 R7
1717
W1 O3 I8 O5 R3 W1 O3 R4 W9 R3 I7 O3 W10 R3 W1 R4 R4 R7
1818
W1 I9 O5 N1 R7
19-
W7 R3 I4 O9 N1 R4
19+
W7 R3 I4 O5 N1 R4
2020
R5
21-
I8 R1 I9 R2 O5 I4 N2 R7
22-
I4 O4 O5 N2 R7
23-
I9 O14 R7
21+
I8 R1 I9 R2 O5 I4 O6 N2 R7
22+
I4 O13 N2 R7
23+
I9 O20 R7
2424
R6
2525
W11 I8 R7
2626
R6
@@ -30,12 +30,13 @@ R5
3030
W1 I7 O5 I3 R3 N3 R4 R7
3131
W1 O3 I13 O5 I6 R3 N3 R4 R7
3232
W1 I14 O5 N1 R7
33-
I15 R3 W1 I16 O5 N1 R7 I16 O7 I7 R7 I16 O14 R4
33+
I15 R3 W1 I16 O5 N1 R7 I16 O8 I7 R7 I16 O20 R4
3434
R5
35-
I17 R3 C1 R8 I16 R8 I13 R1 I16 R2 R4 R7
36-
I14 O13 I13 R1 I16 R2 R7
35+
W5 R3 I16 O8 N4 O17 I16 O7 N1 R4
36+
I17 R3 C1 C2 I16 R8 I13 R1 I16 R2 R4 R7
37+
I14 O14 I13 R1 I16 R2 R7
3738
R6
38-
I17 R3 C2 R8 I14 R4 R7
39+
I17 R3 C3 C2 I14 R4 R7
3940
I18 R3 I13 R4 R7
4041
W11 N1 R7
4142
R6

0 commit comments

Comments
 (0)