Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.35 KB | None | 0 0
  1. /**
  2. * caesar.cpp
  3. *
  4. * <#Names#>
  5. * <#Uniqnames#>
  6. *
  7. * EECS 183: Project 3
  8. *
  9. * <#description#>
  10. */
  11.  
  12. #include "caesar.h"
  13. #include <iostream>
  14.  
  15.  
  16. char shiftAlphaCharacter(char c, int n) {
  17. int shift;
  18. //store dummy character
  19. char result = '#';
  20. //find shift in positive integer between 0 and 26
  21. if (n < 0) {
  22. shift = n % 26 + 26;
  23. }
  24. else {
  25. shift = n % 26;
  26. }
  27. //separate case for lowercase
  28. if (c >= 'a') {
  29. //loop back if go past 'z'
  30. if (c + shift > 'z') {
  31. result = c + shift - 26;
  32. }
  33. else {
  34. result = c + shift;
  35. }
  36. }
  37. //separate case for uppercase
  38. else {
  39. // loop back if go past 'Z'
  40. if (c + shift > 'Z') {
  41. result = c + shift - 26;
  42. }
  43. else {
  44. result = c + shift;
  45. }
  46. }
  47.  
  48. return result;
  49. }
  50.  
  51.  
  52. string caesarCipher(string original, int key, bool encrypt) {
  53. //Multiplies key by -1 if already encrypted to decrypt
  54. int multiplier = (encrypt * 2 - 1) * key;
  55. for (int i = 0; i < original.size(); i++) {
  56. if (isalpha(original.at(i))) {
  57. original.at(i) = shiftAlphaCharacter(original.at(i), multiplier);
  58. }
  59. }
  60.  
  61. // returning a string to avoid compile error
  62. return original;
  63. }
  64.  
  65.  
  66.  
  67.  
  68. /**
  69. * utility.cpp
  70. *
  71. * <#Names#>
  72. * <#Uniqnames#>
  73. *
  74. * EECS 183: Project 3
  75. *
  76. * <#description#>
  77. */
  78.  
  79. #include "utility.h"
  80. #include <iostream>
  81.  
  82.  
  83. string toUpperCase(string original) {
  84. //Calls and replaces each char with the uppercase version
  85. for (int i = 0; i < original.size(); i++) {
  86. original.at(i) = toupper(original.at(i));
  87. }
  88.  
  89. return original;
  90. }
  91.  
  92. string removeNonAlphas(string original) {
  93.  
  94. string result = "";
  95. for (int i = 0; i < original.size(); i++) {
  96. //Adds only alphabetical characters to result
  97. if (isalpha(original.at(i))) {
  98. result += original.at(i);
  99. }
  100. }
  101.  
  102. return result;
  103. }
  104.  
  105. string removeDuplicate(string original) {
  106.  
  107. string result = "";
  108. for (int i = 0; i < original.size(); i++) {
  109. bool hasAppeared = false;
  110. char current = original.at(i);
  111. //Check to see if character has already appeared
  112. for (int j = 0; j < result.size(); j++) {
  113. if (current == result.at(j)) {
  114. hasAppeared = true;
  115. }
  116. }
  117. if (!hasAppeared) {
  118. result += current;
  119. }
  120. }
  121.  
  122. return result;
  123. }
  124.  
  125. int charToInt(char original) {
  126. int result = -1;
  127. switch (original) {
  128. case '0':
  129. result = 0;
  130. break;
  131. case '1':
  132. result = 1;
  133. break;
  134. case '2':
  135. result = 2;
  136. break;
  137. case '3':
  138. result = 3;
  139. break;
  140. case '4':
  141. result = 4;
  142. break;
  143. case '5':
  144. result = 5;
  145. break;
  146. case '6':
  147. result = 6;
  148. break;
  149. case '7':
  150. result = 7;
  151. break;
  152. case '8':
  153. result = 8;
  154. break;
  155. case '9':
  156. result = 9;
  157. break;
  158. }
  159.  
  160. return result;
  161. }
  162.  
  163. ////////////////////////////////////////////////////////////////////////////////
  164. // Do not touch code below. ////////////////////////////////////////////////////
  165. ////////////////////////////////////////////////////////////////////////////////
  166.  
  167. void printGrid(const char grid[SIZE][SIZE]) {
  168. for (int col = 0; col < SIZE; col++) {
  169. cout << " ---";
  170. }
  171. cout << endl;
  172. for (int row = 0; row < SIZE; row++) {
  173. cout << "| ";
  174. for (int col = 0; col < SIZE; col++) {
  175. cout << string(1, grid[row][col]) + " | ";
  176. }
  177. cout << endl;
  178. for (int col = 0; col < SIZE; col++) {
  179. cout << " ---";
  180. }
  181. cout << endl;
  182. }
  183. }
  184.  
  185.  
  186.  
  187.  
  188. /**
  189. * vigenere.cpp
  190. *
  191. * <#Names#>
  192. * <#Uniqnames#>
  193. *
  194. * EECS 183: Project 3
  195. *
  196. * <#description#>
  197. */
  198.  
  199. #include "utility.h"
  200. #include "caesar.h"
  201. #include "vigenere.h"
  202. #include <iostream>
  203.  
  204.  
  205. string vigenereCipher(string original, string keyword, bool encrypt) {
  206. int multiplier = encrypt * 2 - 1;
  207. string key = removeNonAlphas(toUpperCase(keyword));
  208. int keyCounter = 0;
  209. int keyCap = key.size() - 1;
  210. for (int i = 0; i < original.size(); i++) {
  211. if (isalpha(original.at(i))) {
  212. int shift = (key.at(keyCounter) - 'A') * multiplier;
  213. original.at(i) = shiftAlphaCharacter(original.at(i), shift);
  214. keyCounter++;
  215. }
  216.  
  217. //loop keyCounter to repeat the key.
  218. if (keyCounter > keyCap) {
  219. keyCounter = 0;
  220. }
  221. }
  222.  
  223.  
  224.  
  225. // returning a string to avoid compile error
  226. return original;
  227. }
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234. /*
  235. * test.cpp
  236. * EECS 183 Project 3: Ciphers
  237. *
  238. * Justin Lee & Lucy Liu
  239. * JJCLEE &
  240. *
  241. * Testing helper functions for Project 3
  242. */
  243.  
  244. #include "utility.h"
  245. #include "caesar.h"
  246. #include "vigenere.h"
  247. #include "polybius.h"
  248. #include <iostream>
  249. #include <string>
  250.  
  251. void testToUpperCase();
  252. void testRemoveNonAlphas();
  253. void testRemoveDuplicate();
  254. void testCharToInt();
  255. void testShiftAlphaCharacter();
  256. void testCaesarCipher();
  257. void testVigenereCipher();
  258. void testFillGrid();
  259. void testMixKey();
  260. void testFindInGrid();
  261. void testPolybiusSquare();
  262.  
  263. int main(){
  264. testToUpperCase();
  265. testRemoveNonAlphas();
  266. testRemoveDuplicate();
  267. testCharToInt();
  268. testShiftAlphaCharacter();
  269. testCaesarCipher();
  270. testVigenereCipher();
  271. testFillGrid();
  272. testMixKey();
  273. testFindInGrid();
  274. testPolybiusSquare();
  275. return 0;
  276.  
  277. }
  278. void testToUpperCase() {
  279. //testing toUpperCase()
  280. cout << "TESTING toUpperCase()" << endl;
  281. cout << toUpperCase("hi") << endl;
  282. cout << toUpperCase("HI") << endl;
  283. cout << toUpperCase("Hi") << endl;
  284. cout << toUpperCase("!@#$%^&*()") << endl;
  285. cout << toUpperCase("ThIs Is A vErY fUn PrOjEcT!") << endl;
  286. cout << "FINISHED TESTING toUpperCase()" << endl << endl;
  287.  
  288. }
  289. void testRemoveNonAlphas() {
  290. //testing removeNonAlphas()
  291. cout << "TESTING removeNonAlphas()" << endl;
  292. cout << removeNonAlphas("Hi") << endl;
  293. cout << removeNonAlphas("Hi!") << endl;
  294. cout << removeNonAlphas("!Hi") << endl;
  295. cout << removeNonAlphas("#&^$*&^*") << endl;
  296. cout << removeNonAlphas("A*SF&S^AD*&AS^D") << endl;
  297. cout << removeNonAlphas("&*#(HFA(*SDJA") << endl;
  298. cout << removeNonAlphas("a0a0a0a0a0a0a0a0a") << endl;
  299. cout << "FINISHED TESTING removeNonAlphas()" << endl << endl;
  300.  
  301. }
  302. void testRemoveDuplicate() {
  303. //testing removeDuplicate()
  304. cout << "TESTING removeDuplicate()" << endl;
  305. cout << removeDuplicate("AAAAAAAAAAAA") << endl;
  306. cout << removeDuplicate("A1B1C1D1E1F1") << endl;
  307. cout << removeDuplicate("AABBCCDDEEFF") << endl;
  308. cout << removeDuplicate("001122334455") << endl;
  309. cout << removeDuplicate("AAAAAFFFFFFF") << endl;
  310. cout << removeDuplicate("A1A1B2B2C3C3") << endl;
  311. cout << "FINISHED TESTING removeDuplicate()" << endl << endl;
  312.  
  313. }
  314. void testCharToInt() {
  315. //testing charToInt()
  316. cout << "TESTING charToInt()" << endl;
  317. cout << charToInt('0') << endl;
  318. cout << charToInt('1') << endl;
  319. cout << charToInt('2') << endl;
  320. cout << charToInt('3') << endl;
  321. cout << charToInt('4') << endl;
  322. cout << charToInt('5') << endl;
  323. cout << charToInt('6') << endl;
  324. cout << charToInt('7') << endl;
  325. cout << charToInt('8') << endl;
  326. cout << charToInt('9') << endl;
  327. cout << "FINISHED TESTING charToInt()" << endl << endl;
  328.  
  329. }
  330. void testShiftAlphaCharacter() {
  331. //testing shiftAlphaCharacters()
  332. cout << "TESTING shiftAlphaCharacters()" << endl;
  333. cout << shiftAlphaCharacter('A', 52) << endl;
  334. cout << shiftAlphaCharacter('a', 0) << endl;
  335. cout << shiftAlphaCharacter('b', 2) << endl;
  336. cout << shiftAlphaCharacter('X', 5) << endl;
  337. cout << shiftAlphaCharacter('X', 50) << endl;
  338. cout << shiftAlphaCharacter('A', -3) << endl;
  339. cout << "FINISHED TESTING shiftAlphaCharacters()" << endl << endl;
  340.  
  341. }
  342. void testCaesarCipher() {
  343. //testing caesarCipher()
  344. cout << "TESTING caesarCipher()" << endl;
  345. cout << caesarCipher("EECS 183 is a fun class", 12, true) << endl;
  346. cout << caesarCipher("QQOE 183 ue m rgz oxmee", 12, false) << endl;
  347. cout << caesarCipher("QQOE 183 ue m rgz oxmee", -12, true) << endl;
  348. cout << caesarCipher("EECS 183 is a fun class", -12, false) << endl;
  349. cout << "FINISHED TESTING caesarCipher()" << endl << endl;
  350. }
  351. void testVigenereCipher() {
  352. //testing vigenereCipher()
  353. cout << "TESTING vigenereCipher()" << endl;
  354. cout << vigenereCipher("Meet me at the Diag at 11 p.m.", "Squirrel!", true)
  355. << endl;
  356. cout << vigenereCipher("Euyb dv ee lxy Lzrk ll 11 f.g.", "Squirrel!", false)
  357. << endl;
  358. cout << vigenereCipher("EECS 183 is a fun class", "m1m2m3", true) << endl;
  359. cout << "FINISHED TESTING vigenereCipher()" << endl << endl;
  360.  
  361. }
  362. void testFillGrid() {
  363. //testing fillGrid()
  364.  
  365. }
  366. void testMixKey() {
  367. //testing mixKey()
  368.  
  369. }
  370. void testFindInGrid() {
  371. //testing findInGrid()
  372.  
  373. }
  374. void testPolybiusSquare() {
  375. //testing polybiusSquare()
  376.  
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement