Advertisement
Guest User

Untitled

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