-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
231 lines (215 loc) · 6.63 KB
/
Copy pathmain.cpp
File metadata and controls
231 lines (215 loc) · 6.63 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
Author: Sreeram Kondapalli
Date: 10/20/2022
Description:
Usage: <usage syntax>
*/
//required includes
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
const char* englishToBaconCode(char);
char baconCodeToEnglish (const char*);
void baconize(const char*, const char*); //creating a function to baconize the input file
void englishize(const char*, const char*); //creating a function to englishize the input file
int main(int argc, char** argv){
//declaring and opening the input output files
ifstream input (argv[1]);
ofstream output (argv[3]);
if (argc == 4) {
//using .fail incase invalid input file is asked to be found
if (input.fail()){
cout << "Input file does not exist" << endl;
}
else if (strcmp(argv[2], "-bc") == 0) {
cout << "Translating " << argv[1] << " to BACON code" << endl;
//calling the function baconize the input file
baconize(argv[1],argv[3]);
cout << argv[3] << " file completed." << endl;
}
else if(strcmp(argv[2], "-e") == 0) {
cout << "Translating " << argv[1] << " to ENGLISH " << endl;
//calling the function englishize the input file
englishize(argv[1],argv[3]);
cout << argv[3] << " file completed." << endl;
}
else{
cout << "Error -- usage: <exe> <input file> <-bc|-e> <output file>\n" << endl;
}
}
else{
cout << "Error -- usage: <exe> <input file> <-bc|-e> <output file>\n" << endl;
}
return 0;
}
//baconize function
void baconize(const char* inputFile, const char* outputFile){
ifstream input (inputFile);
ofstream output (outputFile);
//setting the char limit
char sourceString[255];
char *token;
while (!input.eof()) {
input.getline(sourceString, 255);
token = strtok(sourceString, "\n"); //first read
while (token != NULL) {
//creating a for loop to go past every word and split it letter by letter
for (int i = 0; i < strlen(token); i++) {
output << englishToBaconCode(token[i]);
if (i < (strlen(token) - 1))
//adding | after every word
output << "|";
}
token = strtok(NULL, "\n"); //keep tokenizing notice NULL
}
output << "\n";
}
//closing the files
input.close();
output.close();
}
//englishize function
void englishize(const char* inputFile, const char* outputFile){
ifstream input(inputFile);
ofstream output(outputFile);
//setting the char limit
char sourceString[1600];
char sourceOutput[255];
char* token;
while (!input.eof()) {
//creating a for loop to go past every word and split it letter by letter
input.getline(sourceString, 1600);
token = strtok(sourceString, "|"); //first read
int i = 0;
while (token != NULL) {
//converting each token to english and storing it in the charecter array
sourceOutput[i] = baconCodeToEnglish(token);
token = strtok(NULL, "|"); //keep tokenizing notice NULL
i++;
}
sourceOutput[i] = '\0';
output << sourceOutput << "\n";
}
//closing the files
input.close();
output.close();
}
const char* englishToBaconCode(char val){
switch (val) {
case 'A':
case 'a': return "aaaaa"; break;
case 'B':
case 'b': return "aaaab"; break;
case 'C':
case 'c': return "aaaba"; break;
case 'D':
case 'd': return "aaabb"; break;
case 'E':
case 'e': return "aabaa"; break;
case 'F':
case 'f': return "aabab"; break;
case 'G':
case 'g': return "aabba"; break;
case 'H':
case 'h': return "aabbb"; break;
case 'I':
case 'i': return "abaaa"; break;
case 'J':
case 'j': return "abaab"; break;
case 'K':
case 'k': return "ababa"; break;
case 'L':
case 'l': return "ababb"; break;
case 'M':
case 'm': return "abbaa"; break;
case 'N':
case 'n': return "abbab"; break;
case 'O':
case 'o': return "abbba"; break;
case 'P':
case 'p': return "abbbb"; break;
case 'Q':
case 'q': return "baaaa"; break;
case 'R':
case 'r': return "baaab"; break;
case 'S':
case 's': return "baaba"; break;
case 'T':
case 't': return "baabb"; break;
case 'U':
case 'u': return "babaa"; break;
case 'V':
case 'v': return "babab"; break;
case 'W':
case 'w': return "babba"; break;
case 'X':
case 'x': return "babbb"; break;
case 'Y':
case 'y': return "bbaaa"; break;
case 'Z':
case 'z': return "bbaab"; break;
case ' ': return "/"; break;
default: return "!!!!!"; break;
}
}
char baconCodeToEnglish (const char* val){
if(strcmp(val, "aaaaa") == 0)
return 'A';
else if(strcmp(val, "aaaab") == 0)
return 'B';
else if(strcmp(val, "aaaba") == 0)
return 'C';
else if(strcmp(val, "aaabb") == 0)
return 'D';
else if(strcmp(val, "aabaa") == 0)
return 'E';
else if(strcmp(val, "aabab") == 0)
return 'F';
else if(strcmp(val, "aabba") == 0)
return 'G';
else if(strcmp(val, "aabbb") == 0)
return 'H';
else if(strcmp(val, "abaaa") == 0)
return 'I';
else if(strcmp(val, "abaab") == 0)
return 'J';
else if(strcmp(val, "ababa") == 0)
return 'K';
else if(strcmp(val, "ababb") == 0)
return 'L';
else if(strcmp(val, "abbaa") == 0)
return 'M';
else if(strcmp(val, "abbab") == 0)
return 'N';
else if(strcmp(val, "abbba") == 0)
return 'O';
else if(strcmp(val, "abbbb") == 0)
return 'P';
else if(strcmp(val, "baaaa") == 0)
return 'Q';
else if(strcmp(val, "baaab") == 0)
return 'R';
else if(strcmp(val, "baaba") == 0)
return 'S';
else if(strcmp(val, "baabb") == 0)
return 'T';
else if(strcmp(val, "babaa") == 0)
return 'U';
else if(strcmp(val, "babab") == 0)
return 'V';
else if(strcmp(val, "babba") == 0)
return 'W';
else if(strcmp(val, "babbb") == 0)
return 'X';
else if(strcmp(val, "bbaaa") == 0)
return 'Y';
else if(strcmp(val, "bbaab") == 0)
return 'Z';
else if(strcmp(val, "!!!!!") == 0)
return '#';
else if(strcmp(val, "/") == 0)
return ' ';
else
return '%';
}