Advertisement
Guest User

prog2

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