-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (155 loc) · 5.99 KB
/
main.cpp
File metadata and controls
189 lines (155 loc) · 5.99 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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cstring>
#include <cctype>
#include <algorithm>
const std::string filename = "input.txt";
std::string readFile() {
std::string content;
std::ifstream file(filename);
std::getline(file, content);
return content;
}
void appendFile(std::string& text) {
std::ofstream file(filename, std::ios::app);
file << text;
}
void overwriteFile(std::string text) {
std::ofstream file(filename);
file << text;
}
std::string cipher(std::string text, int shift, std::string mode) {
const std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
std::string encoded_message;
for (char c : text) {
if (std::isalpha(c)) {
char lower_c = std::tolower(c);
size_t index = alphabet.find(lower_c);
char encoded_char = alphabet[(index + ((mode == "encode") ? shift : -shift) + 26) % 26];
encoded_message += encoded_char;
} else {
encoded_message += c;
}
}
return encoded_message;
}
std::string vigenere(std::string text, std::string key, std::string mode) {
const std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
std::string encoded_message;
int i = 0;
for (char c : text) {
if (std::isalpha(c)) {
char lower_c = std::tolower(c);
size_t index = alphabet.find(lower_c);
size_t shift = key[i % key.length()] - 'a';
char encoded_char = alphabet[(index + ((mode == "encode") ? shift : -shift) + 26) % 26];
encoded_message += encoded_char;
i++;
} else {
encoded_message += c;
}
}
return encoded_message;
}
void testCipher(
std::string original,
std::string expected,
int shift,
std::string mode,
int test_number,
std::string cipher_type,
std::string key
) {
std::string encoded_message = (cipher_type=="vigenere") ? vigenere(original, key, mode) : cipher(original, shift, mode);
overwriteFile(encoded_message);
const bool passed = (readFile() == expected);
if (passed == false) {
std::cerr
<< "=========TEST "<<std::to_string(test_number)<<"=========" << std::endl
<< "Original: " << original << std::endl
<< "Expected: " << expected << std::endl
<< "Got: " << readFile() << std::endl
<< "Result: " << (passed ? "Passed✅" : "Failed❌") << std::endl
<< "========================" << std::endl;
throw std::invalid_argument("Test failed^");
}
}
void tests() {
// TEST CIPHER
std::cout << std::endl << "=======================";
std::cout << std::endl << "Testing Cipher Function";
testCipher("Hello, World!", "khoor, zruog!", 3, "encode", 1, "cipher", "");
testCipher(readFile(), "hello, world!", 3, "decode", 2, "cipher", "");
testCipher("abcd", "bcde", 1, "encode", 3, "cipher", "");
testCipher(readFile(), "abcd", 1, "decode", 4, "cipher", "");
std::cout << "✅";
// TEST ROT CIPHER
std::cout << std::endl << "Testing ROT Function";
testCipher("Hello", "uryyb", 13, "encode", 1, "rot", "");
testCipher(readFile(), "hello", 13, "decode", 2, "rot", "");
testCipher("fnynq", "salad", 13, "encode", 3, "rot", "");
testCipher(readFile(), "fnynq", 13, "decode", 4, "rot", "");
std::cout << "✅";
// TEST VIGENERE CIPHER
std::cout << std::endl << "Testing Vigenere Function";
testCipher("Hello", "rijvs", 0, "encode", 1, "vigenere", "key");
testCipher(readFile(), "hello", 0, "decode", 2, "vigenere", "key");
testCipher("lwapp", "salad", 0, "encode", 3, "vigenere", "hello");
testCipher(readFile(), "lwapp", 0, "decode", 4, "vigenere", "hello");
std::cout << "✅";
std::cout << std::endl << "=======================";
std::cout << std::endl << "ALL TESTS PASSED✅" << std::endl;
std::cout << "=======================" << std::endl << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 4) {
std::cerr << "Usage: ./program <inputFile> <outputFile> <transformation> [shift/key] [encode/decode]" << std::endl;
return 1;
}
std::string inputFile = argv[1];
std::string outputFile = argv[2];
std::string transformation = argv[3];
// Read from the actual input file
std::ifstream inFile(inputFile);
std::string content;
std::getline(inFile, content);
inFile.close();
std::string result = "";
if (transformation == "upper") {
for (char c : content) result += std::toupper(c);
} else if (transformation == "lower") {
for (char c : content) result += std::tolower(c);
} else if (transformation == "reverse") {
result = content;
std::reverse(result.begin(), result.end());
} else if (transformation == "cipher") {
if (argc < 6) { std::cerr << "Usage: ./program <in> <out> cipher <shift> <encode/decode>" << std::endl; return 1; }
int shift = std::stoi(argv[4]);
std::string mode = argv[5];
result = cipher(content, shift, mode);
} else if (transformation == "rot") {
if (argc < 5) { std::cerr << "Usage: ./program <in> <out> rot <shift>" << std::endl; return 1; }
int shift = std::stoi(argv[4]);
std::string mode = (argc >= 6) ? argv[5] : "encode";
result = cipher(content, shift, mode);
} else if (transformation == "vigenere") {
if (argc < 6) { std::cerr << "Usage: ./program <in> <out> vigenere <key> <encode/decode>" << std::endl; return 1; }
std::string key = argv[4];
std::string mode = argv[5];
result = vigenere(content, key, mode);
} else if (transformation == "tests") {
tests();
return 0;
} else {
std::cerr << "Unknown transformation: " << transformation << std::endl;
return 1;
}
// Write to the actual output file
std::ofstream outFile(outputFile);
outFile << result;
outFile.close();
std::cout << "Result: " << result << std::endl;
return 0;
}