Advertisement
Guest User

prog 1

a guest
Dec 20th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.00 KB | None | 0 0
  1. /**********************************************************************
  2. * nurseprogram.c *
  3. * A program with intergrated login system that let's a nurse write *
  4. * patient details to a file and has an option to change password. *
  5. * Written details are then encrypted to a file. *
  6. * Candidate NO: 181452 *
  7. * December 2017 *
  8. **********************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14.  
  15.  
  16.  
  17. /*Function prototypes*/
  18. void emptyBuffer(void);
  19. int nurseLogin();
  20. void createPatientFile
  21. (char *patientLastName, char *patientDay,
  22. char *patientMonth, char *patientYear,
  23. char *patientsFileName);
  24. void displayTitle(char *title);
  25. void equalsLine(void);
  26. int mainMenu();
  27. void waitForKey(void);
  28. void decryptString(char dest[], char source[]);
  29. void encryptString(char dest[], char source[]);
  30. void addOtherPatient();
  31. void changeString(char encrypted[], char source[], int change);
  32. char toLower(char any);
  33.  
  34. int main()
  35. {
  36. int choice;
  37. int nurse = -1;
  38. while(nurse == -1)
  39. {
  40. nurse = nurseLogin();
  41. }
  42.  
  43.  
  44. while(choice != 3)
  45. {
  46. choice = mainMenu();
  47. switch(choice)
  48. {
  49. case 1:
  50. /*Call addOtherPatient function*/
  51. addOtherPatient();
  52. break;
  53. case 2:
  54. /*Call alterPassword function*/
  55. alterPassword(nurse);
  56. break;
  57. case 3:
  58. /*Clear the contents and display a closing message*/
  59. printf("See you soon!");
  60. break;
  61. }
  62. }
  63.  
  64. return 0;
  65. }
  66. /* function that requires the user to enter valid username and
  67. password */
  68.  
  69. int nurseLogin()
  70. {
  71. FILE *filesPassword;
  72. int i;
  73. int nurse;
  74. char nurseUsername[9];
  75. char nursePassword[9];
  76. char filesUsername[9];
  77. char filePassword[9];
  78.  
  79. /*Displaying the header*/
  80.  
  81. displayTitle("\t\t || NURSE LOGIN ||");
  82.  
  83. printf("Enter your username and password:\n\n");
  84.  
  85. /*Receiving user string inputs*/
  86. printf("Username: ");
  87. scanf("%9[^\n]", nurseUsername);
  88. emptyBuffer();
  89. printf("Password: ");
  90. scanf("%9[^\n]", nursePassword);
  91. emptyBuffer();
  92.  
  93. /*Declare variables for encrypted username and password*/
  94. char encryptedNurseUsername[9];
  95. char encryptedNursePassword[9];
  96.  
  97. /*Use encryptString to encrypt username and password*/
  98. encryptString(nurseUsername, encryptedNurseUsername);
  99. encryptString(nursePassword, encryptedNursePassword);
  100.  
  101.  
  102. /*Opening file for reading purposes*/
  103. filesPassword = fopen("passwords.txt", "r");
  104.  
  105. /*Scanning for username and password within a file*/
  106. while(0<fscanf(filesPassword, " %[^\t] %[^\n]", filesUsername,
  107. filePassword))
  108. {
  109.  
  110. /*Comparing user input strings with file strings*/
  111. if((strcmp(filesUsername, encryptedNurseUsername) == 0)
  112. && (strcmp(filePassword, encryptedNursePassword) == 0))
  113. {
  114. /*Closing the file*/
  115. fclose(filesPassword);
  116. /*Returning a nurse address*/
  117. return i;
  118. }
  119. /*Adding one to i*/
  120. ++i;
  121. }
  122. /*Closing the file*/
  123. fclose(filesPassword);
  124.  
  125. puts("\n!! Wrong username or password, please try again !!\n");
  126. waitForKey();
  127.  
  128. return -1;
  129. }
  130. /*Main menu function which displays main menu contents including
  131. a header and returns user's input*/
  132. int mainMenu()
  133. {
  134. int choice;
  135. /*Displaying menu list choices*/
  136. displayTitle("\t\t || NURSE MENU ||");
  137. puts("1) Add a patient");
  138. puts("2) Change password");
  139. puts("3) Exit");
  140.  
  141. printf(">>> ");
  142.  
  143. /*If the input is less than 1 or higher than 3 then
  144. display an error message*/
  145. while(((scanf(" %d", &choice)) == 0) ||
  146. (choice < 1) || (choice > 3))
  147. {
  148. emptyBuffer();
  149. puts("!! Please make a valid choice !!");
  150. printf(">>> ");
  151. }
  152. emptyBuffer();
  153.  
  154. return choice;
  155. }
  156.  
  157. void addOtherPatient(void)
  158. {
  159. /*Declaring variables for iteriation and patient details, using
  160. 2d array to store date of birth*/
  161. FILE *patientDetails;
  162. char patientFileName[265];
  163. char patientsFirstName[256];
  164. char patientsLastName[256];
  165. char patientsDateOfBirth[3][256];
  166. char patientsHeight[256];
  167. char patientsWaist[256];
  168. char patientsWeight[256];
  169. char patientsComment[512];
  170.  
  171.  
  172. /*Displaying the header*/
  173. displayTitle("\t\t || ADD A PATIENT ||");
  174.  
  175. /*Storing non-encryped patient details into [0]*/
  176. printf("First Name: ");
  177. scanf("%[^\n]", &patientsFirstName);
  178. emptyBuffer();
  179.  
  180. printf("Last Name: ");
  181. scanf("%[^\n]", &patientsLastName);
  182. emptyBuffer();
  183.  
  184. printf("DOB Day [DD]: ");
  185. scanf("%[^\n]", &patientsDateOfBirth[0]);
  186. emptyBuffer();
  187.  
  188. printf("DOB Month [MM]: ");
  189. scanf("%[^\n]", &patientsDateOfBirth[1]);
  190. emptyBuffer();
  191.  
  192. printf("DOB Year [YYYY]: ");
  193. scanf("%[^\n]", &patientsDateOfBirth[2]);
  194. emptyBuffer();
  195.  
  196. printf("Height [cm]: ");
  197. scanf("%[^\n]", &patientsHeight);
  198. emptyBuffer();
  199.  
  200. printf("Waist measurement [inches]: ");
  201. scanf("%[^\n]", &patientsWaist);
  202. emptyBuffer();
  203.  
  204. printf("Weight [kg]: ");
  205. scanf("%[^\n]", &patientsWeight);
  206. emptyBuffer();
  207.  
  208. printf("Comment: ");
  209. scanf("%[^\n]", &patientsComment);
  210. emptyBuffer();
  211.  
  212. /*Creating file name using the createPatientFile function*/
  213. createPatientFile(patientsLastName, patientsDateOfBirth[0], patientsDateOfBirth[1],
  214. patientsDateOfBirth[2], patientFileName);
  215.  
  216. /*Encrypting patient's details*/
  217. encryptString(patientsFirstName, patientsFirstName);
  218. encryptString(patientsLastName, patientsLastName);
  219. encryptString(patientsDateOfBirth[0], patientsDateOfBirth[0]);
  220. encryptString(patientsDateOfBirth[1], patientsDateOfBirth[1]);
  221. encryptString(patientsDateOfBirth[2], patientsDateOfBirth[2]);
  222. encryptString(patientsHeight, patientsHeight);
  223. encryptString(patientsWaist, patientsWaist);
  224. encryptString(patientsWeight, patientsWeight);
  225. encryptString(patientsComment, patientsComment);
  226.  
  227. /*Opening the file for writing purposes*/
  228. patientDetails = fopen(patientFileName, "w");
  229.  
  230. /*Writing user to a file*/
  231. fprintf(patientDetails, "%s\n", patientsFirstName);
  232. fprintf(patientDetails, "%s\n", patientsLastName);
  233.  
  234. fprintf(patientDetails, "%s/%s/%s\n", patientsDateOfBirth[0],
  235. patientsDateOfBirth[1], patientsDateOfBirth[2]);
  236.  
  237. fprintf(patientDetails, "%s\n", patientsHeight);
  238. fprintf(patientDetails, "%s\n", patientsWaist);
  239. fprintf(patientDetails, "%s\n", patientsWeight);
  240. fprintf(patientDetails, "%s\n", patientsComment);
  241.  
  242. /*Closing the file*/
  243. fclose(patientDetails);
  244.  
  245. equalsLine();
  246. puts("Patient has been added!");
  247. /*Display continue message using waitForKey function*/
  248. waitForKey();
  249. }
  250.  
  251. /*Function that takes patient's last name and DOB input and returns
  252. a file name for the patient*/
  253. void createPatientFile(char *patientLastName, char *patientDay, char *patientMonth,
  254. char *patientYear, char *patientsFileName)
  255. {
  256. int i;
  257. char copyYearLastTwoDigits[3];
  258.  
  259. strcpy(patientsFileName, "Patients/");
  260.  
  261. /*Adding last name, day and month to a file name*/
  262. strcat(patientsFileName, patientLastName);
  263. strcat(patientsFileName, patientDay);
  264. strcat(patientsFileName, patientMonth);
  265.  
  266. /*Copy two last characters from the patient year of birth and store
  267. it to the variable*/
  268. memcpy(copyYearLastTwoDigits, &patientYear[2], 2);
  269.  
  270. /*Adding \0 to the variable to make it a string*/
  271. copyYearLastTwoDigits[2] = '\0';
  272.  
  273. /*Adding last two DOB digits to the file name*/
  274. strcat(patientsFileName, copyYearLastTwoDigits);
  275. /*Add the .aow extension to the file name*/
  276. strcat(patientsFileName, ".aow");
  277.  
  278. /*Iterating trough a file name*/
  279. for(i = 0; patientsFileName[i]; ++i)
  280. {
  281. /*Changing file first character to lowercase*/
  282. patientsFileName[i] = toLower(patientsFileName[i]);
  283. }
  284.  
  285. }
  286. /*A function which takes a string and transforms it's characters*/
  287. void changeString(char encrypted[], char source[], int change)
  288. {
  289. /*storing length of the string to encrypt*/
  290. int strLen = strlen(source);
  291.  
  292. int i;
  293. int ch;
  294.  
  295. /*Iterate trough the string*/
  296. for(i = 0; i < strLen; ++i)
  297. {
  298. /*Assign one character of the string to
  299. the character variable*/
  300. ch = source[i];
  301.  
  302. /*If the character is between a and z*/
  303. if(ch >= 'a' && ch <= 'z')
  304. {
  305. /*Add change(7) to the character*/
  306. ch += change;
  307.  
  308. /*If character is out of the range of z*/
  309. if(ch > 'z')
  310. {
  311. /*Get the difference between the character a and z,
  312. subtract 1 and add it to the a value*/
  313. ch = 'a' + (ch - 'z') - 1;
  314. }
  315. /*If the character is out of the range of a*/
  316. else if(ch < 'a')
  317. {
  318. /*Get the difference between the character z and a,
  319. add 1 and add it to the z value*/
  320. ch = 'z' + (ch - 'a') + 1;
  321. }
  322. }
  323. /*If the character is between A and Z*/
  324. else if(ch >= 'A' && ch <= 'Z')
  325. {
  326. /*Add change(7) to the character*/
  327. ch += change;
  328.  
  329. /*If character is out of the range of Z*/
  330. if(ch > 'Z')
  331. {
  332. /*Get the difference between the character A and Z,
  333. subtract 1 and add it to the A value*/
  334. ch = 'A' + (ch - 'Z') - 1;
  335. }
  336. /*If the character is out of the range of A*/
  337. else if(ch < 'A')
  338. {
  339. /*Get the difference between the character Z and A,
  340. add 1 and add it to the Z value*/
  341. ch = 'Z' + (ch - 'A') + 1;
  342. }
  343. }
  344. /*If the character is between 0 and 9*/
  345. else if(ch >= '0' && ch <= '9')
  346. {
  347. //Add change(7) to the character
  348. ch += change;
  349.  
  350. /*If character is out of the range of 9*/
  351. if(ch > '9')
  352. {
  353. /*Get the difference between the character 0 and 9,
  354. subtract 1 and add it to the 9 value*/
  355. ch = '0' + (ch - '9') - 1;
  356. }
  357. /*If character is out of the range of 0*/
  358. else if(ch < '0')
  359. {
  360. /*Get the difference between the character 9 and 0,
  361. add 1 and add it to the 0 value*/
  362. ch = '9' + (ch - '0') + 1;
  363. }
  364. }
  365. /*Store the encrypted character in the encrypted array*/
  366. encrypted[i] = ch;
  367. }
  368. /*Add \0 to the encrypted array to make it a string*/
  369. encrypted[strLen] = '\0';
  370.  
  371. }
  372. /*A function for encryption which uses transform function*/
  373. void encryptString(char dest[], char source[])
  374. {
  375. /*Transforming character and moving it forwards by seven places*/
  376. changeString(dest, source, +7);
  377. }
  378. /*A function for decryption which uses transform function*/
  379. void decryptString(char dest[], char source[])
  380. {
  381. /*Transforming character and moving it backwards by seven places*/
  382. changeString(dest, source, -7);
  383. }
  384. /*A function which allows a nurse to change a password within
  385. a specified array location depending which nurse is logged in*/
  386. void alterPassword(int nurse)
  387. {
  388. FILE *passwordFileRead;
  389. FILE *passwordFileWrite;
  390.  
  391. char newPassword[10];
  392. char reinforcePassword[10];
  393.  
  394. /*Displaying the header*/
  395. displayTitle("\t\t || CHANGE PASSWORD ||");
  396.  
  397. puts("Enter your new password:");
  398. printf(">>> ");
  399. /*Receiving user first password input*/
  400. scanf("%[^\n]", newPassword);
  401. emptyBuffer();
  402. /*If password string is not equal to 8 characters then display
  403. an an appropiate error*/
  404. if(strlen(newPassword) != 8)
  405. {
  406. puts("!! Password MUST be 8 characters long !!");
  407. waitForKey();
  408. return;
  409. }
  410. puts("Confirm your new password:");
  411. printf(">>> ");
  412.  
  413. /*Receiving user second password input*/
  414. scanf("%[^\n]", reinforcePassword);
  415. emptyBuffer();
  416.  
  417. /*Compaing the two password strings to see if they match*/
  418. if(strcmp(newPassword, reinforcePassword) == 0)
  419. {
  420. char encryptedPassword[10];
  421.  
  422. /*Encrypting the new password*/
  423. encryptString(reinforcePassword, encryptedPassword);
  424.  
  425. char allPasswords[5][10];
  426. char allUsernames[5][10];
  427.  
  428. /*Opening file for reading purposes*/
  429. passwordFileRead = fopen("passwords.txt", "r");
  430. int i = 0;
  431.  
  432. /*Read all usernames and passwords*/
  433. while(2==fscanf(passwordFileRead, " %[^\t] %[^\n]",
  434. allUsernames[i], allPasswords[i]))
  435. {
  436. i++;
  437. }
  438.  
  439. /*Closing the file*/
  440. fclose(passwordFileRead);
  441. /*Opening the file for writing purposes*/
  442. passwordFileWrite = fopen("passwords.txt", "w");
  443. /*Overwriting an old password with a new password string*/
  444. strcpy(allPasswords[nurse], encryptedPassword);
  445.  
  446. for (i = 0; i < 5; ++i)
  447. {
  448. /*Writing new strings to a file*/
  449. fprintf(passwordFileWrite, "%s\t%s\n", allUsernames[i],
  450. allPasswords[i]);
  451. }
  452. /*Closing the file*/
  453. fclose(passwordFileWrite);
  454.  
  455. equalsLine();
  456. puts("Password has been changed!");
  457. waitForKey();
  458. return;
  459. }
  460. else
  461. {
  462. puts("!! Your password did not match, please try again !!");
  463. waitForKey();
  464. return;
  465. }
  466. }
  467. /*Function that display the header*/
  468. void displayTitle(char *header)
  469. {
  470. puts("*********************************************************");
  471. printf("\t\t ACTION ON WEIGHT\n");
  472. puts("*********************************************************");
  473. equalsLine();
  474. printf(header);
  475. printf("\n");
  476. equalsLine();
  477. printf("\n");
  478. }
  479. /*Function that displays some ending text and waits for user
  480. to press any key*/
  481. void waitForKey(void)
  482. {
  483. puts("=========================================================");
  484. printf("Press the Enter Key to Continue...\n");
  485. puts("=========================================================");
  486. getchar();
  487.  
  488. }
  489. /*Function which displays a line*/
  490. void equalsLine(void)
  491. {
  492. puts("=========================================================");
  493. }
  494. /*Function that emptys the buffer*/
  495. void emptyBuffer(void)
  496. {
  497. while(getchar() != '\n')
  498. {
  499. ;
  500. }
  501. }
  502. char toLower(char any)
  503. {
  504. return tolower(any);
  505. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement