Advertisement
Guest User

Untitled

a guest
Dec 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. /**********************************************************************
  2. * prog1nurse.c
  3. * A program that allows only 5 nurses to sign in. Once logged on the
  4. program allows patient files to be created and nurses passwords to
  5. be changed.
  6. * December 2017
  7. **********************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12.  
  13. /* Function declarations */
  14. void emptyBuffer(void);
  15. void printLine(void);
  16. int nurseLogin();
  17. void createPatientFile
  18. (char *patientLastName, char *patientDay,
  19. char *patientMonth, char *patientYear,
  20. char *patientsFileName);
  21. int mainMenu();
  22. void decryptString(char decrypt[], char source[]);
  23. void encryptString(char encrypt[], char source[]);
  24. void addOtherPatient();
  25. void changeStringToChar(char encrypt[], char source[], int change);
  26. void alterPassword(int login);
  27.  
  28. int main()
  29. {
  30. int choice;
  31. int nurse = -1;
  32. while(nurse == -1)
  33. {
  34. nurse = nurseLogin();
  35. }
  36.  
  37.  
  38. while(choice != 3)
  39. {
  40. choice = mainMenu();
  41. switch(choice)
  42. {
  43. case 1:
  44. addOtherPatient();
  45. break;
  46. case 2:
  47. alterPassword(nurse);
  48. break;
  49. case 3:
  50. printf("Make sure to visit soon!");
  51. break;
  52. }
  53. }
  54.  
  55. return 0;
  56. }
  57.  
  58. /* function that requires the user to enter valid username and
  59. password, which is then checked with files*/
  60. int nurseLogin()
  61. {
  62. FILE *filesPassword;
  63. int i;
  64. char nurseUsername[9];
  65. char nursePassword[9];
  66. char filesUsername[9];
  67. char filePassword[9];
  68. char encryptUser[9];
  69. char encryptPass[9];
  70.  
  71.  
  72. printf("=======================================================\n");
  73. printf(" ACTION ON WEIGHT\n");
  74. printf("=======================================================\n");
  75.  
  76. printf("\n *// Nurse Login //*\n\n");
  77. printf("Please enter your Username: \n");
  78. scanf("%9[^\n]", nurseUsername);
  79. emptyBuffer();
  80. printf("Please enter your Password: \n");
  81. scanf("%9[^\n]", nursePassword);
  82. emptyBuffer();
  83.  
  84. encryptString(nurseUsername, encryptUser);
  85. encryptString(nursePassword, encryptPass);
  86.  
  87. filesPassword = fopen("passwords.txt", "r");
  88.  
  89. while(0<fscanf(filesPassword, " %[^\t] %[^\n]", filesUsername,
  90. filePassword))
  91. {
  92.  
  93. printf("%s %s : %s %s", filesUsername, filePassword,
  94. encryptUser, encryptPass);
  95. if((strcmp(filesUsername, encryptUser) == 0)
  96. && (strcmp(filePassword, encryptPass) == 0))
  97. {
  98. fclose(filesPassword);
  99. return i;
  100. }
  101. ++i;
  102. }
  103.  
  104. fclose(filesPassword);
  105. puts("\n|| The password or username you entered seems to be\n"
  106. "incorrect, please try again ||\n");
  107.  
  108. return -1;
  109. }
  110.  
  111. /* Function that displays the main menu options to the user */
  112. int mainMenu()
  113. {
  114. int menuChoice;
  115.  
  116. printLine();
  117. printf("\n *// Nurse Main Menu //*\n\n");
  118. printLine();
  119. printf("\n1. Add a patient file\n");
  120. printf("2. Change password\n");
  121. printf("3. Exit\n");
  122.  
  123. printf("\nPlease selection an option: ");
  124.  
  125. while(((scanf(" %d", &menuChoice)) == 0) ||
  126. (menuChoice < 1) || (menuChoice > 3))
  127. {
  128. emptyBuffer();
  129. printf("Please select an appropiate number(1-3)! \n");
  130. printf("Re-enter number: ");
  131. }
  132. emptyBuffer();
  133.  
  134. return menuChoice;
  135. }
  136.  
  137. /* function that allows the user to enter a new patients details */
  138.  
  139. void addOtherPatient(void)
  140. {
  141. FILE *patientDetails;
  142. char patientFileName[20];
  143. char patientsFirstName[20];
  144. char patientsSecondName[20];
  145. char patientsDateOfBirth[3][20];
  146. char patientsHeight[10];
  147. char patientsWaist[10];
  148. char patientsWeight[10];
  149. char patientsComment[500];
  150.  
  151. printLine();
  152. printf(" Add a patient to a file!\n");
  153. printLine();
  154.  
  155. printf("Patients First Name: ");
  156. scanf("%[^\n]", &patientsFirstName);
  157. emptyBuffer();
  158.  
  159. printf("Patients Second Name: ");
  160. scanf("%[^\n]", &patientsSecondName);
  161. emptyBuffer();
  162.  
  163. printf("Patients DOB Day: ");
  164. scanf("%[^\n]", &patientsDateOfBirth[0]);
  165. emptyBuffer();
  166.  
  167. printf("Patients DOB Month: ");
  168. scanf("%[^\n]", &patientsDateOfBirth[1]);
  169. emptyBuffer();
  170.  
  171. printf("Patients DOB Year: ");
  172. scanf("%[^\n]", &patientsDateOfBirth[2]);
  173. emptyBuffer();
  174.  
  175. printf("Patients Height: ");
  176. scanf("%[^\n]", &patientsHeight);
  177. emptyBuffer();
  178.  
  179. printf("Patients Waist Measurement: ");
  180. scanf("%[^\n]", &patientsWaist);
  181. emptyBuffer();
  182.  
  183. printf("Patients Weight: ");
  184. scanf("%[^\n]", &patientsWeight);
  185. emptyBuffer();
  186.  
  187. printf("Extra Comments: ");
  188. scanf("%[^\n]", &patientsComment);
  189. emptyBuffer();
  190.  
  191. /* Function to create the file name */
  192. createPatientFile(patientsSecondName, patientsDateOfBirth[0], patientsDateOfBirth[1],
  193. patientsDateOfBirth[2], patientFileName);
  194.  
  195. /*Encrypt information of the patients*/
  196. encryptString(patientsFirstName, patientsFirstName);
  197. encryptString(patientsSecondName, patientsSecondName);
  198. encryptString(patientsDateOfBirth[0], patientsDateOfBirth[0]);
  199. encryptString(patientsDateOfBirth[1], patientsDateOfBirth[1]);
  200. encryptString(patientsDateOfBirth[2], patientsDateOfBirth[2]);
  201. encryptString(patientsHeight, patientsHeight);
  202. encryptString(patientsWaist, patientsWaist);
  203. encryptString(patientsWeight, patientsWeight);
  204. encryptString(patientsComment, patientsComment);
  205.  
  206. patientDetails = fopen(patientFileName, "w");
  207.  
  208. fprintf(patientDetails, "%s\n", patientsFirstName);
  209. fprintf(patientDetails, "%s\n", patientsSecondName);
  210.  
  211. fprintf(patientDetails, "%s/%s/%s\n", patientsDateOfBirth[0],
  212. patientsDateOfBirth[1], patientsDateOfBirth[2]);
  213.  
  214. fprintf(patientDetails, "%s\n", patientsHeight);
  215. fprintf(patientDetails, "%s\n", patientsWaist);
  216. fprintf(patientDetails, "%s\n", patientsWeight);
  217. fprintf(patientDetails, "%s\n", patientsComment);
  218.  
  219. fclose(patientDetails);
  220.  
  221. printLine();
  222. puts("Patient successfully added!");
  223. }
  224.  
  225. /* Function which creates a file name for patients based on their
  226. last name and their date of birth */
  227.  
  228. void createPatientFile(char *patientLastName, char *patientDay, char *patientMonth,
  229. char *patientYear, char *patientsFileName)
  230. {
  231. int i;
  232. char copyYearLastTwoDigits[3];
  233.  
  234. strcpy(patientsFileName, "Patients/");
  235.  
  236. strcat(patientsFileName, patientLastName);
  237. strcat(patientsFileName, patientDay);
  238. strcat(patientsFileName, patientMonth);
  239.  
  240. memcpy(copyYearLastTwoDigits, &patientYear[2], 2);
  241.  
  242. copyYearLastTwoDigits[2] = '\0';
  243.  
  244. strcat(patientsFileName, copyYearLastTwoDigits);
  245.  
  246. strcat(patientsFileName, ".aow");
  247. }
  248.  
  249. /*A function which takes a string and transforms it's characters*/
  250. void changeStringToChar(char encrypt[], char source[], int change)
  251. {
  252. int length = strlen(encrypt);
  253.  
  254. char toEncrypt[length+1];
  255.  
  256. int i;
  257. int ch;
  258.  
  259. for(i = 0; i < length; ++i)
  260. {
  261. ch = encrypt[i];
  262.  
  263. if(ch >= '0' && ch <= '9')
  264. {
  265. ch += change;
  266.  
  267. if(ch > '9')
  268. ch = '0' + (ch - '9') - 1;
  269.  
  270. else if(ch < '0')
  271. ch = '9' + (ch - '0') + 1;
  272. }
  273. else if(ch >= 'A' && ch <= 'Z')
  274. {
  275. ch += change;
  276.  
  277. if(ch > 'Z')
  278. ch = 'A' + (ch - 'Z') - 1;
  279. else if(ch < 'A')
  280. ch = 'Z' + (ch - 'A') + 1;
  281. }
  282. else if(ch >= 'a' && ch <= 'z')
  283. {
  284. ch += change;
  285.  
  286. if(ch > 'z')
  287. ch = 'a' + (ch - 'z') - 1;
  288.  
  289. else if(ch < 'a')
  290. ch = 'z' + (ch - 'a') + 1;
  291. }
  292. toEncrypt[i] = ch;
  293. }
  294. toEncrypt[length] = '\0';
  295.  
  296. strcpy(source, toEncrypt);
  297.  
  298. }
  299. //A function for encryption which uses transform function
  300. void encryptString(char encrypt[], char source[])
  301. {
  302. changeStringToChar(encrypt, source, +7);
  303. }
  304.  
  305. //A function for decryption which uses transform function
  306. void decryptString(char decrypt[], char source[])
  307. {
  308. changeStringToChar(decrypt, source, -7);
  309. }
  310.  
  311. //A function which allows a nurse to change a password within
  312. //a specified array location depending which nurse is logged in
  313. void alterPassword(int login)
  314. {
  315. FILE *passwordFileRead;
  316. FILE *passwordFileWrite;
  317.  
  318. char newPassword[10];
  319. char reinforcePassword[10];
  320.  
  321. printLine();
  322. printf(" Change your password!\n");
  323. printLine();
  324.  
  325. printf("Please enter your new password:\n");
  326. scanf("%[^\n]", newPassword);
  327. emptyBuffer();
  328.  
  329. if(strlen(newPassword) != 8)
  330. {
  331. printf("Password must be 8 characters long! \n");
  332. return;
  333. }
  334. printf("Please confirm your new password: \n");
  335. scanf("%[^\n]", reinforcePassword);
  336. emptyBuffer();
  337.  
  338. /*function to compare two password strings to see if they
  339. match*/
  340. if(strcmp(newPassword, reinforcePassword) == 0)
  341. {
  342. char encryptedPassword[10];
  343.  
  344. encryptString(reinforcePassword, encryptedPassword);
  345.  
  346. char fileUsernames[5][10];
  347. char filePasswords[5][10];
  348.  
  349. passwordFileRead = fopen("passwords.txt", "r");
  350. int i = 0;
  351.  
  352. while(2==fscanf(passwordFileRead, " %[^\t] %[^\n]",
  353. fileUsernames[i], filePasswords[i]))
  354. {
  355. i++;
  356. }
  357.  
  358. fclose(passwordFileRead);
  359.  
  360. passwordFileWrite = fopen("passwords.txt", "w");
  361.  
  362. strcpy(filePasswords[login], encryptedPassword);
  363.  
  364. for (i = 0; i < 5; ++i)
  365. {
  366. fprintf(passwordFileWrite, "%s\t%s\n", fileUsernames[i],
  367. filePasswords[i]);
  368. }
  369.  
  370. fclose(passwordFileWrite);
  371.  
  372. printf("\nPassword successfully changed!\n");
  373. return;
  374. }
  375. else
  376. {
  377. puts("Your password did not match, please try again !");
  378. return;
  379. }
  380. }
  381. /*Function that asks user to press any key to continue */
  382. void printLine(void)
  383. {
  384. printf("-------------------------------------------------------\n");
  385.  
  386. }
  387.  
  388. /*Function to empty the buffer*/
  389. void emptyBuffer(void)
  390. {
  391. while(getchar() != '\n')
  392. {
  393. ;
  394. }
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement