wheelsmanx

CPS 171 Machine Problem 5

Oct 17th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. // ConsoleApplication5.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include <wctype.h>
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. // the file is C://temp//New Text Document.txt
  12. using namespace std;
  13. //arraymatrix for decoding-encoding
  14. char arraymatrix0[5] = { 'H','A','P','I','N' };
  15. char arraymatrix1[5] = { 'E','S','B','C','D' };
  16. char arraymatrix2[5] = { 'F','G','J','K','L' };
  17. char arraymatrix3[5] = { 'M','O','Q','R','T' };
  18. char arraymatrix4[5] = { 'U','V','W','X','Y' };
  19. // needed special char and numbers and space
  20. char arraymatrix5[1] = { (char)' ', };
  21. char arraymatrix6[5] = { '1','2','3','4','5' };
  22. char arraymatrix7[5] = { '6','7','8','9','0' };
  23.  
  24.  
  25. bool encrypt;
  26. bool newline = true;
  27. bool isfirst = true;
  28. int row;
  29. int col;
  30. char gear;
  31. int t = 0;
  32. string previous;
  33. char charref;
  34. string realstring;
  35. string newstring = "H";
  36.  
  37. //enum alphabetlower { a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y };
  38. //enum alphabetupper { A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y };
  39.  
  40.  
  41. void alphamatrix(int matrixrow, int matrixcol)
  42. {
  43. // if it is going to decrypt this- then we are going to store it in an string and make them lowercase- so if encrypt is false
  44. if (encrypt == false) {
  45. if (matrixcol == 0) {
  46. realstring += tolower(arraymatrix0[matrixrow]); previous += charref; col = 0; row = 0;
  47. }
  48. if (matrixcol == 1) {
  49. realstring += tolower(arraymatrix1[matrixrow]); previous += charref; col = 0; row = 0;
  50. }
  51. if (matrixcol == 2) {
  52. realstring += tolower(arraymatrix2[matrixrow]); previous += charref; col = 0; row = 0;
  53. }
  54. if (matrixcol == 3) {
  55. realstring += tolower(arraymatrix3[matrixrow]); previous += charref; col = 0; row = 0;
  56. }
  57. if (matrixcol == 4) {
  58. realstring += tolower(arraymatrix4[matrixrow]); previous += charref; col = 0; row = 0;
  59. }
  60. if (matrixcol == 5) {
  61. realstring += " "; previous += charref; col = 0; row = 0;
  62. }
  63. if (matrixcol == 6) {
  64. realstring += tolower(arraymatrix6[matrixrow]); previous += charref; col = 0; row = 0;
  65. }
  66. if (matrixcol == 7) {
  67. realstring += tolower(arraymatrix7[matrixrow]); previous += charref; col = 0; row = 0;
  68. }
  69. } // else if it is going to be encrypted - then lowercase the encrypted from variable.
  70. else {
  71. // the structure here is look in arrayx[at x] add to encrypted from variable - then reset the rows.
  72. if (matrixcol == 0) {
  73. realstring += arraymatrix0[matrixrow]; previous += tolower(charref); col = 0; row = 0;
  74. }
  75. if (matrixcol == 1) {
  76. realstring += arraymatrix1[matrixrow]; previous += tolower(charref); col = 0; row = 0;
  77. }
  78. if (matrixcol == 2) {
  79. realstring += arraymatrix2[matrixrow]; previous += tolower(charref); col = 0; row = 0;
  80. }
  81. if (matrixcol == 3) {
  82. realstring += arraymatrix3[matrixrow]; previous += tolower(charref); col = 0; row = 0;
  83. }
  84. if (matrixcol == 4) {
  85. realstring += arraymatrix4[matrixrow]; previous += tolower(charref); col = 0; row = 0;
  86. }
  87. if (matrixcol == 5) {
  88. realstring += " "; previous += charref; col = 0; row = 0;
  89. }
  90. if (matrixcol == 6) {
  91. realstring += arraymatrix6[matrixrow]; previous += charref; col = 0; row = 0;
  92. }
  93. if (matrixcol == 7) {
  94. realstring += arraymatrix7[matrixrow]; previous += charref; col = 0; row = 0;
  95. }
  96. }
  97. }
  98.  
  99.  
  100. int main()
  101. {
  102. //here we are going to define the file that we are trying to stream
  103. ifstream fileInput;
  104. fileInput.open("c:\\temp\\New Text Document.txt");
  105.  
  106. // start here please if you want to understand this.
  107. while (!fileInput.eof())
  108. {
  109. fileInput >> std::noskipws >> charref;
  110. // here we make an exception for the first line.
  111. if (charref == (char)'H' && newline == true) {
  112. encrypt = true;
  113. gear = 'D';
  114. t = 1;
  115. cout << "Key Encrypted: " << "H";
  116.  
  117.  
  118. }
  119. // check to see if the line is a new line and wether or not an E and D
  120. if (charref == (char)'E' && newline == true) {
  121. encrypt = true;
  122. gear = 'D';
  123. t = 1;
  124. cout << "Encrypted: ";
  125.  
  126. }
  127. if (charref == (char)'D' && newline == true) {
  128. encrypt = false;
  129. gear = 'D';
  130. t = 1;
  131. cout << "Decrypted: ";
  132.  
  133. }
  134.  
  135. // this is just to take it out of gear incase the code goes hay wire
  136. if (isfirst == false && newline == false) { gear = 'N'; }
  137.  
  138. // if the line ends and it is a new line then we are going to out put the code
  139. if (charref == (char)'\n' && newline == false) {
  140. // at this point my code looks for the first letter of new lines - and so I made it add an H to the begining of the first string before output
  141. if (previous == "appiness") {
  142. newstring += previous;
  143. previous = newstring;
  144. }
  145. // were going to output some stuff
  146. cout << realstring << endl;
  147. cout << endl << "From: " << previous << endl;;
  148. cout << endl << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
  149. previous = "";
  150. realstring = "";
  151. newline = true;
  152. gear = 'D';
  153. cout << endl;
  154.  
  155. }
  156. // if its encrypted or not - look through array matrix and pass those array positions to the function - reverse the rows and col variables for decryption.
  157. if (encrypt == true && newline == false && gear != 'D' || gear != 'D' && encrypt == true && isfirst == true) {
  158. if (isfirst == true) { isfirst = false; }
  159. gear = 'N';
  160. int arraySize = 5;
  161. charref = toupper(charref);
  162. char itemToFind = charref;
  163.  
  164. for (int i = 0; i < arraySize; i++) {
  165. if (arraymatrix0[i] == itemToFind) {
  166. row = 0;
  167. col = i;
  168. alphamatrix(row, col);
  169. break;
  170. }
  171. if (arraymatrix1[i] == itemToFind) {
  172. row = 1;
  173. col = i;
  174. alphamatrix(row, col);
  175. break;
  176. }
  177. if (arraymatrix2[i] == itemToFind) {
  178. row = 2;
  179. col = i;
  180. alphamatrix(row, col);
  181. break;
  182. }
  183. if (arraymatrix3[i] == itemToFind) {
  184. row = 3;
  185. col = i;
  186. alphamatrix(row, col);
  187. break;
  188. }
  189. if (arraymatrix4[i] == itemToFind) {
  190. row = 4;
  191. col = i;
  192. alphamatrix(row, col);
  193. break;
  194. }
  195. if (arraymatrix5[i] == itemToFind) {
  196. row = 0;
  197. col = 5;
  198. alphamatrix(row, col);
  199. break;
  200. }
  201. if (arraymatrix6[i] == itemToFind) {
  202. row = i;
  203. col = 6;
  204. alphamatrix(row, col);
  205. break;
  206. }
  207. if (arraymatrix7[i] == itemToFind) {
  208. row = i;
  209. col = 7;
  210. alphamatrix(row, col);
  211. break;
  212. }
  213. }
  214. } //decrypt
  215. if (encrypt == false && newline == false && gear != 'D' || gear != 'D' && encrypt == false && isfirst == true) {
  216. if (isfirst == true) { isfirst = false; }
  217. gear = 'N';
  218. int arraySize = 5;
  219. charref = toupper(charref);
  220. char itemToFind = charref;
  221.  
  222. for (int i = 0; i < arraySize; i++) {
  223. if (arraymatrix0[i] == itemToFind) {
  224. row = 0;
  225. col = i;
  226. alphamatrix(row, col);
  227. break;
  228. }
  229. if (arraymatrix1[i] == itemToFind) {
  230. row = 1;
  231. col = i;
  232. alphamatrix(row, col);
  233. break;
  234. }
  235. if (arraymatrix2[i] == itemToFind) {
  236. row = 2;
  237. col = i;
  238. alphamatrix(row, col);
  239. break;
  240. }
  241. if (arraymatrix3[i] == itemToFind) {
  242. row = 3;
  243. col = i;
  244. alphamatrix(row, col);
  245. break;
  246. }
  247. if (arraymatrix4[i] == itemToFind) {
  248. row = 4;
  249. col = i;
  250. alphamatrix(row, col);
  251. break;
  252. }
  253. if (arraymatrix5[i] == itemToFind) {
  254. row = 0;
  255. col = 5;
  256. alphamatrix(row, col);
  257. break;
  258. }
  259. if (arraymatrix6[i] == itemToFind) {
  260. row = i;
  261. col = 6;
  262. alphamatrix(row, col);
  263. break;
  264. }
  265. if (arraymatrix7[i] == itemToFind) {
  266. row = i;
  267. col = 7;
  268. alphamatrix(row, col);
  269. break;
  270. }
  271. }
  272.  
  273.  
  274.  
  275.  
  276. }
  277.  
  278. // if this is first - then reset the bool
  279. if (isfirst == true) { isfirst = false; }
  280. // if we are in gear and it is a new line - and the t count says that we already chose to encrypt or decrypt then reset t count and set newline bool to false to allow code to go
  281. if (newline == true && gear == 'D' && t == 1) {
  282. t = 0;
  283. newline = false;
  284. }
  285. // same thing as above - but allows for gear R as well
  286. if (newline == true && gear == 'R' && t == 1) {
  287. t = 0;
  288. newline = false;
  289. }
  290. }
  291. if (fileInput.eof() == true) {
  292. cout << realstring;
  293. cout << endl << "From: " << previous << endl;
  294. cout << endl << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
  295. }
  296. cout << endl;
  297. system("pause");
  298. return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment