Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. //Program written by Jimmy Johnson
  2. //GSI Instructor: Robert Perricone
  3. //This program decodes a message from
  4. //a text file written in a ceaser cipher
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <fstream>
  9.  
  10. using namespace std;
  11.  
  12. //Requires: nothing
  13. //Modifies: nothing
  14. //Effects: reads in the name of a file
  15. // input by the user
  16. string getFile();
  17.  
  18. //Requires: fileName must have characters in it
  19. //Modifies: fileName
  20. //Effects: Checks to make sure that the file name
  21. // given by the user points to a valid address.
  22. // If it doesn't, the program asks the user to input
  23. // a correct address
  24. string checkFile(string);
  25.  
  26. //Requires: fileName must point to a valid address
  27. //Modifies: nothing
  28. //Effects: Reads each character from the outfile. It also
  29. // determines which direction, and by how much, the letters
  30. // of the ceaser cipher shift
  31. void readFile(string);
  32.  
  33. //Requires: shiftToA and shiftToZ assigned to either true or false,
  34. // Totalshift to be assigned some value, and a ch to be assigned
  35. // to an ASCII value.
  36. //Modifies: nothing
  37. //Effects: Calls the function "toA" if shiftToA and ShiftIsPositive
  38. // are both true, or if shiftToZ is true and ShiftIsPositive is false.
  39. // Calls the function "toZ" if shiftToZ and ShiftIsPositive are both
  40. // true, or if shiftToA is true, and ShiftIsPositive is false.
  41. void translateAndPrint(bool, bool, int, char);
  42.  
  43. //Requires: ch must be assigned the ASCII value of a capital letter,
  44. // and totalShift must be assigned a value
  45. //Modifies: nothing
  46. //Effects: shifts ch toward Z by the value of totalShift
  47. void toZ(char, int);
  48.  
  49. //Requires: ch must be assigned the ASCII value of a capital letter,
  50. // and totalShift must be assigned a value
  51. //Modifies: nothing
  52. //Effects: shifts ch toward A by the value of totalShift
  53. void toA(char, int);
  54.  
  55. int main()
  56. {
  57. string fileName;
  58. fileName = getFile();
  59. fileName = checkFile(fileName);
  60. readFile(fileName);
  61. return 0;
  62. }
  63.  
  64. string getFile()
  65. {
  66. string fileName;
  67. cout << "Please enter the name of the file: ";
  68. cin >> fileName;
  69. cout << endl;
  70. return fileName;
  71. }
  72.  
  73. string checkFile(string fileName)
  74. {
  75. ifstream ins;
  76. string file;
  77. ins.open(fileName);
  78. while (ins.fail() == true)
  79. {
  80. cout << "\n*** Error Code 1 -- Error in opening file ***\n"
  81. << "You entered an invalid file name. Please try again.\n\n";
  82. ins.open(file = getFile());
  83. fileName = file;
  84. }
  85. ins.close();
  86. return fileName;
  87. }
  88.  
  89. void readFile(string fileName)
  90. {
  91. ifstream ins;
  92. char ch = ' ';
  93. bool shiftToZ = false, shiftToA = false;
  94. int shift = 0, shiftPlus = 0, shiftPlusTotal = 0, totalShift = 0;
  95.  
  96. ins.open(fileName);
  97. ins >> noskipws;
  98. ins >> ch;
  99. while (!ins.eof())
  100. {
  101. while (ch != '#')
  102. {
  103. if (ins.eof())
  104. {
  105. return;
  106. }
  107. while (ch == '@' || ch == '%' || ch == '^')
  108. {
  109. if (ch == '@')
  110. {
  111. shift = 0;
  112. shiftPlus = 0;
  113. shiftPlusTotal = 0;
  114. shiftToZ = false;
  115. shiftToA = true;
  116. ins >> shift;
  117. if (shift == 0)
  118. {
  119. cout << "*** Error Code 2 -- No shift value as expected ***\n";
  120. return;
  121. }
  122. ins >> ch;
  123. }
  124. else if (ch == '%')
  125. {
  126. shift = 0;
  127. shiftPlus = 0;
  128. shiftPlusTotal = 0;
  129. shiftToA = false;
  130. shiftToZ = true;
  131. ins >> shift;
  132. if (shift == 0)
  133. {
  134. cout << "*** Error Code 2 -- No shift value as expected ***\n";
  135. return;
  136. }
  137. ins >> ch;
  138. }
  139. else if (ch == '^')
  140. {
  141. shiftPlus = 0;
  142. ins >> shiftPlus;
  143. shiftPlusTotal += shiftPlus;
  144. if (shiftPlus == 0)
  145. {
  146. cout << "*** Error Code 3 -- No shift alteration value as expected ***\n";
  147. return;
  148. }
  149. ins >> ch;
  150. }
  151. }
  152. totalShift = 0;
  153. totalShift = shift + shiftPlusTotal;
  154. translateAndPrint(shiftToA, shiftToZ, totalShift, ch);
  155. ins >> ch;
  156. }
  157. cout << endl << endl;
  158. ins >> ch;
  159. }
  160. return;
  161. }
  162.  
  163. void translateAndPrint(bool shiftToA, bool shiftToZ, int totalShift, char ch)
  164. {
  165. char translatedChar;
  166. int x = 0;
  167. bool shiftIsPositive;
  168.  
  169. if (ch > 'Z' || ch < 'A')
  170. {
  171. cout << ch;
  172. return;
  173. }
  174. if (totalShift >= 0)
  175. {
  176. shiftIsPositive = true;
  177. }
  178. else
  179. {
  180. shiftIsPositive = false;
  181. totalShift *= -1;
  182. }
  183.  
  184. if (shiftToA == true && shiftIsPositive == true)
  185. {
  186. toA(ch, totalShift);
  187. }
  188. else if (shiftToA == true && shiftIsPositive == false)
  189. {
  190. toZ(ch, totalShift);
  191. }
  192. if (shiftToZ == true && shiftIsPositive == true)
  193. {
  194. toZ(ch, totalShift);
  195. }
  196. else if (shiftToZ == true && shiftIsPositive == false)
  197. {
  198. toA(ch, totalShift);
  199. }
  200. }
  201.  
  202. void toA(char ch, int totalShift)
  203. {
  204. char translatedChar;
  205. int x;
  206. translatedChar = ch;
  207. for (x = 1; x <= totalShift; x++)
  208. {
  209. translatedChar -= 1;
  210. if (translatedChar < 'A')
  211. translatedChar = 'Z';
  212. }
  213. cout << translatedChar;
  214. }
  215.  
  216. void toZ(char ch, int totalShift)
  217. {
  218. char translatedChar;
  219. int x;
  220. translatedChar = ch;
  221. for (x = 1; x <= totalShift; x++)
  222. {
  223. translatedChar += 1;
  224. if (translatedChar > 'Z')
  225. translatedChar = 'A';
  226. }
  227. cout << translatedChar;
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement