-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program3.cpp
164 lines (131 loc) · 5.25 KB
/
Program3.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
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
160
161
162
//============================================================================
// Name : problemSolving.cpp
// Authors : Neema Badihian & Joseph Darani
// Version : 1.0
// Description : Converting to a High-level Language (C++ & Python)
//============================================================================
#include<iostream>
#include <fstream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
/* Reading the code from file */
string readFromFile(fstream &file, string filePath){
string result = ""; // Where we will store the whole text
file.open(filePath, ios::in); // open file for reading
char character;
while(!file.eof()){
file.get(character);
result += character; // add readed charachters to the result string
}
file.close();
return result; // return the resul string
}
/* Tokenizing different strings*/
vector<string> tokenize(string file){
int len = file.length();
vector<string> result;
for(int i = 0; i < len; i++){
if(file[i]!='\n' && file[i]!=' '){ // if the character if not white space
string token = "";
while(file[i]!='\n' && file[i]!=' ' && i < len){
token += file[i]; // create a token
i++;
}
result.push_back(token); // add the token to result vector
}
}
return result; // return result vector
}
void ConvertToCPlusPlus(fstream &file, string filePath, vector<string> tokens){
bool foundVar = false; // to mark when we find the word "var"
file.open(filePath, ios::out);
/* add the following code to the c++ file:
#include<iostream>
using namespace std; */
file << "#include<iostream>\nusing namespace std;\n";
for(int i = 0; i < tokens.size(); i++){ //iterate throught the tokens
if(foundVar){ // We only start adding code after finding "var"
if(tokens[i] == "integer"){
file << "int"; // replace "integer" with "int"
}
else if(tokens[i] == "begin"){
file << "int main()\n{\n"; // replace "begin" with "int main() {"
}
else if(tokens[i] == "show"){
file << "cout<< "; // replace "show()" with "cout<<"
file << tokens[i+2]; // skip '('
i += 3; // skip the ')'
}
else if(tokens[i] == "end"){
file << "}"; // replace "end" with "}"
}
else if(tokens[i] == ";"){
file<< ';' << '\n'; // add a new line after every ';'
}
else{ // if the token is not one of the above, add it as it is;
file << tokens[i];
}
file<<' '; // add spaces between tokens
}
if(tokens[i] == "var")foundVar = true;
}
file.close(); // close file
return ;
}
void ConvertToPython(fstream &file, string filePath, vector<string> tokens){
bool foundBegin = false; // to mark when we find the word "begin"
file.open(filePath, ios::out);
for(int i = 0; i < tokens.size(); i++){ //iterate through the tokens
if(foundBegin){ // We only start adding code after finding "begin"
// because we don't declare variables in python
if(tokens[i] == "show"){
file << "print"; // replace "show" with "print"
}
else if(tokens[i] == "end"){
file << "\nmain()"; // remove "end" and run the program
}
else if(tokens[i] == ";"){
file << '\n' << '\t'; // add a new line and indent after instead of ';'
}
else{ // if the token is not one of the above, add it as it is
file << tokens[i] << ' '; //add spaces between tokens
}
}
if(tokens[i] == "begin"){ // create a function called "main()" that will hold
// the whole program
foundBegin = true;
file << "def main():\n\t";
}
}
file.close(); // close file
return ;
}
int main() {
fstream file2, fileCpp, filePy;
string file2Path = "/Users/joseph/Desktop/finalp2.txt"; // for the second file
string fileCppPath = "/Users/joseph/Desktop/finalp3.cpp"; // for the C++ file
string filePyPath = "/Users/joseph/Desktop/textPy.py"; // for the Python file
string cPlusPlusOrPython;
string formattedCode = readFromFile(file2, file2Path); // storing the code in a string
formattedCode.pop_back(); // remove the last letter because it is EOF
vector<string> tokens = tokenize(formattedCode); // tokenizing the code
while(true) {
cout << "\nWould you like to translate to C++ or Python (c/p)? " << endl;
cin >> cPlusPlusOrPython;
if(tolower(cPlusPlusOrPython[0]) == 'c') {
ConvertToCPlusPlus(fileCpp, fileCppPath, tokens);
break;
}
else if(tolower(cPlusPlusOrPython[0]) == 'p'){
ConvertToPython(filePy, filePyPath, tokens);
break;
}
else{
cout << "You entered an invalid choice." << endl;
}
}
return 0;
}