Advertisement
Guest User

hasdfj

a guest
Mar 1st, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #define SIZE 4
  6.  
  7. //prototypes
  8. void input(char[], int[]);
  9. void encrypt(int[], const int [], int*, int*, int*);
  10. void decrypt(int[], int*);
  11.  
  12. main()
  13. {
  14. //2 variables to store number of times the code was entered successfully ot incorrectly
  15. int right;
  16. int wrong;
  17. //store chosen option
  18. char option;
  19. //store code that user entered
  20. char user_code[SIZE] = " ";
  21. //store converted code (from char to int)
  22. int int_code[SIZE] = {0};
  23. //access code that is constant
  24. const int access_code[SIZE] = {4, 5, 2, 3};
  25. //counter to check if code is already encrypted
  26. int already_encrypted;
  27.  
  28. right = wrong = already_encrypted = 0;
  29.  
  30. do
  31. {
  32.  
  33. printf("\nPlease select an option\n");
  34. printf("1. Enter code\n");
  35. printf("2. Encrypt code and verify if correct\n");
  36. printf("3. Decrypt code\n");
  37. printf("4. Number of times code entered correctly and incorrectly\n");
  38. printf("5. Exit program\n\n");
  39.  
  40. //read in user input
  41. scanf("%c", &option);
  42.  
  43. //to determine which function to call
  44. switch(option)
  45. {
  46. //executes when option 1 is chosen
  47. case '1':
  48. {
  49. //will ignore numbers after the first if user enters more than one number (is the same for every case)
  50. flushall();
  51.  
  52. //call function input()
  53. input(user_code, int_code);
  54.  
  55. break;
  56. }//end case 1
  57.  
  58. //executes when option 2 is chosen
  59. case '2':
  60. {
  61. flushall();
  62.  
  63. //call function encrypt()
  64. encrypt(int_code, access_code, &right, &wrong, &already_encrypted);
  65.  
  66. break;
  67. }//end case 2
  68.  
  69. //executes when option 3 is chosen
  70. case '3':
  71. {
  72. flushall();
  73.  
  74. //call funtion decrypt()
  75. decrypt(int_code, &already_encrypted);
  76.  
  77. break;
  78. }//end case 3
  79.  
  80. //executes when option 4 is chosen
  81. case '4':
  82. {
  83. flushall();
  84. clrscr();
  85.  
  86. //prints number of times correct and number of times wrong
  87. printf("\nThe number of times the code was entered successfully is: \t%d \n", right);
  88. printf("The number of times the code was entered incorrectly is: \t%d \n", wrong);
  89.  
  90. break;
  91. }//end case 4
  92.  
  93. //executes when option 5 is chosen
  94. case '5':
  95. {
  96. flushall();
  97. clrscr();
  98.  
  99. printf("\nProgram will now end.");
  100.  
  101. break;
  102. }//end case 5
  103.  
  104.  
  105. //executes when user enters invalid option
  106. default:
  107. {
  108. flushall();
  109.  
  110. printf("\nError: Invalid option chosen. \n");
  111. getchar();
  112.  
  113. break;
  114.  
  115. }//end default
  116.  
  117. }//end switch
  118.  
  119.  
  120. }//end do
  121. while(option != '5');
  122.  
  123. getchar();
  124. }//end main()
  125.  
  126.  
  127. /*Implement function input(). This function allows user to enter a 4-digit code that will
  128. be stored to be used in encryption process.
  129. */
  130. void input(char user_code[SIZE], int int_code[SIZE])
  131. {
  132. clrscr();
  133.  
  134. int i;
  135. int error; //if there are values that aren't integers
  136. //int temp; //to store a value temporarily while converting from char to int
  137.  
  138. error = 0;
  139.  
  140. //prompt then read user input
  141. printf("\nPlease enter a %d-digit code.\n", SIZE);
  142. gets(user_code);
  143.  
  144. //check if all values are numbers
  145. for(i = 0; i <SIZE ; i++)
  146. {
  147. switch(*(user_code + i))
  148. {
  149. //if values are numbers
  150. case '1':
  151. case '2':
  152. case '3':
  153. case '4':
  154. case '5':
  155. case '6':
  156. case '7':
  157. case '8':
  158. case '9':
  159. case '0':
  160. {
  161. break;
  162. }//end cases
  163.  
  164. //if there is a value that is not an integer
  165. default:
  166. {
  167. error++;
  168. break;
  169. }//end default
  170.  
  171. }//end switch
  172.  
  173. }//end for
  174.  
  175. //if there is an error
  176. if(error != 0)
  177. {
  178. printf("\n %d errors encountered.\n", error);
  179. }//end if
  180. else
  181. {
  182.  
  183. //convert array of characters into array of integers
  184. for(i = 0; i < SIZE; i++)
  185. {
  186. *(int_code + i) = *(user_code + i) - '0';
  187. }//end for
  188.  
  189. }//end else
  190.  
  191. }//end input()
  192.  
  193.  
  194. /*Implement function encrypt(). This will encrypt the code that the user entered and it will
  195. then compare the encrypted code with the access code. If codes match, display message
  196. "CORRECT CODE". If codes don't match, display message "ERROR CODE".
  197. */
  198. void encrypt(int int_code[SIZE], const int access_code[SIZE], int *right, int *wrong, int *already_encrypted)
  199. {
  200. clrscr();
  201.  
  202. //temporary variable to store values for swapping
  203. int temp;
  204. int i;
  205. int num;//for amount of matches out of 4 elements
  206.  
  207. temp = num = 0;
  208.  
  209. if(*already_encrypted == 0)
  210. {
  211. //swap elements
  212. for(i = 0; i < SIZE; i++)
  213. {
  214. //swap 1st and 3rd element
  215. if(i == 0)
  216. {
  217. //put 1st element in temp1
  218. temp = *(int_code + i);
  219. //put 3rd element in 1st position
  220. *(int_code + i) = *(int_code + (i+2));
  221. //put 1st element (stored in temp) in 3rd position
  222. *(int_code + (i+2)) = temp;
  223. }//end if
  224.  
  225. //repeat previous if() but with 2nd and 4th elements
  226. if(i == 1)
  227. {
  228. temp = *(int_code + i);
  229. *(int_code + i) = *(int_code + (i+2));
  230. *(int_code + (i+2)) = temp;
  231. }//end if
  232.  
  233. }//end for
  234.  
  235. //add 1 to all elements
  236. for(i = 0; i < SIZE; i++)
  237. {
  238. *(int_code + i) = (*(int_code + i) + 1);
  239.  
  240. //if value is 10, change it to 0
  241. if(*(int_code + i) == 10)
  242. {
  243. *(int_code + i) = 0;
  244. }//end if
  245.  
  246. }//end for
  247.  
  248. //check if codes match
  249. for(i = 0; i < SIZE; i++)
  250. {
  251. if(*(int_code + i) == *(access_code + i))
  252. {
  253. num++;
  254. }//end if
  255.  
  256. }//end for
  257.  
  258. //increments right by 1 if there are 4 matches
  259. if(num == SIZE)
  260. {
  261. *right = (*right + 1);
  262. printf("\nCORRECT CODE\n");
  263. }//end if
  264. else
  265. {
  266. *wrong = (*wrong + 1);
  267. printf("\nERROR CODE\n");
  268. }//end else
  269.  
  270. *already_encrypted = 1;
  271. }//end if
  272. else
  273. {
  274. printf("\nThe code has already been encrypted.Cannot encrypt again.\n");
  275. }//end else
  276.  
  277. }//end encrypt()
  278.  
  279.  
  280. /*Implement function decrypt(). This will allow the user to decrypt the code ONLY if the code has
  281. already been encrypted.
  282. */
  283. void decrypt(int int_code[SIZE], int *already_encrypted)
  284. {
  285. clrscr();
  286.  
  287. int i;
  288. int temp = 0;
  289.  
  290. if(*already_encrypted == 1)
  291. {
  292. //subtract 1 from all elements
  293. for(i = 0; i < SIZE; i++)
  294. {
  295. *(int_code + i) = (*(int_code + i) - 1);
  296.  
  297. //if value is -1, change it to 9
  298. if(*(int_code + i) == -1)
  299. {
  300. *(int_code + 1) = 9;
  301. }//end if
  302.  
  303. }//end for
  304.  
  305. //swap elements
  306. for(i = 0; i < SIZE; i++)
  307. {
  308. //swap 1st and 3rd element
  309. if(i == 0)
  310. {
  311. //put 1st element in temp1
  312. temp = *(int_code + i);
  313. //put 3rd element in 1st position
  314. *(int_code + i) = *(int_code + (i+2));
  315. //put 1st element (stored in temp) in 3rd position
  316. *(int_code + (i+2)) = temp;
  317. }//end if
  318.  
  319. //repeat previous if() but with 2nd and 4th elements
  320. if(i == 1)
  321. {
  322. temp = *(int_code + i);
  323. *(int_code + i) = *(int_code + (i+2));
  324. *(int_code + (i+2)) = temp;
  325. }//end if
  326.  
  327. }//end for
  328.  
  329. *already_encrypted = 0;
  330.  
  331. }//end if
  332. else
  333. {
  334. printf("\nThe code has not been encrypted.\n");
  335. }//end else
  336.  
  337. }//end decrypt()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement