Advertisement
Guest User

prog1

a guest
Dec 20th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.26 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 loginWindow();
  17. int mainMenu();
  18. void addPatient();
  19. void changePassword(int nurse);
  20. void createFileName(char pLastName[], char pDay[], char pMonth[],
  21.                     char pYear[], char patientFileName[]);
  22. void decryptString(char toDecrypt[], char toReturn[]);
  23. void encryptString(char toEncrypt[], char toReturn[]);
  24. void transformString(char toEncrypt[], char toReturn[], int toJump);
  25. void showTopTitle(char header[]);
  26. void equalsLine(void);
  27. void waitForKey(void);
  28. void emptyBuffer(void);
  29.  
  30. int main()
  31. {
  32.     int choice;
  33.     int nurse = -1;
  34.  
  35.     /*Show the loginWindow and get the nurse login*/
  36.     while(nurse == -1)
  37.     {
  38.         nurse = loginWindow();
  39.     }
  40.  
  41.     /*Iterated switch selection used for calling a function using
  42.     maineMenu function return value*/
  43.     while(choice != 3)
  44.     {   /*Display mainu menu screen and get choice made by the user*/
  45.         choice = mainMenu();
  46.         switch(choice)
  47.         {
  48.             case 1:
  49.                 /*Call addPatient function*/
  50.                 addPatient();
  51.                 break;
  52.             case 2:
  53.                 /*Call changePassword function*/
  54.                 changePassword(nurse);
  55.                 break;
  56.             case 3:
  57.                 /*Clear the contents and display a closing message*/
  58.                 printf("See you soon!");
  59.                 break;
  60.         }
  61.     }
  62.  
  63.     return 0;
  64. }
  65. /*A function which prompts for a user login and password, if the user
  66. inputs matches the contents within a file the loop will end*/
  67. int loginWindow()
  68. {
  69.     FILE *passwordFile;
  70.     int i;
  71.     int nurse;
  72.     char nurseUsername[9];
  73.     char nursePassword[9];
  74.     char fileUsername[9];
  75.     char filePassword[9];
  76.  
  77.     /*Displaying the header*/
  78.     showTopTitle("\t\t      || LOGIN ||");
  79.  
  80.     puts("Enter your username and password:\n");
  81.  
  82.     /*Receiving user string inputs*/
  83.     printf("Username: ");
  84.     scanf("%9[^\n]", nurseUsername);
  85.     emptyBuffer();
  86.     printf("Password: ");
  87.     scanf("%9[^\n]", nursePassword);
  88.     emptyBuffer();
  89.  
  90.     /*Declare variables for encrypted username and password*/
  91.     char encryptedNurseUsername[9];
  92.     char encryptedNursePassword[9];
  93.  
  94.     /*Use encryptString to encrypt username and password*/
  95.     encryptString(nurseUsername, encryptedNurseUsername);
  96.     encryptString(nursePassword, encryptedNursePassword);
  97.  
  98.  
  99.     /*Opening file for reading purposes*/
  100.     passwordFile = fopen("passwords.txt", "r");
  101.  
  102.     /*Scanning for username and password within a file*/
  103.     while(0<fscanf(passwordFile, " %[^\t] %[^\n]", fileUsername,
  104.                    filePassword))
  105.     {
  106.  
  107.         /*Comparing user input strings with file strings*/
  108.         if((strcmp(fileUsername, encryptedNurseUsername) == 0)
  109.            && (strcmp(filePassword, encryptedNursePassword) == 0))
  110.         {
  111.             /*Closing the file*/
  112.             fclose(passwordFile);
  113.             /*Returning a nurse address*/
  114.             return i;
  115.         }
  116.         /*Adding one to i*/
  117.         ++i;
  118.     }
  119.     /*Closing the file*/
  120.     fclose(passwordFile);
  121.  
  122.     puts("\n!! Wrong username or password, please try again !!\n");
  123.     waitForKey();
  124.  
  125.     return -1;
  126. }
  127. /*Main menu function which displays main menu contents including
  128. a header and returns user's input*/
  129. int mainMenu()
  130. {
  131.     int choice;
  132.     /*Displaying menu list choices*/
  133.     showTopTitle("\t\t    || NURSE MENU ||");
  134.     puts("1) Add a patient");
  135.     puts("2) Change password");
  136.     puts("3) Exit");
  137.  
  138.     printf(">>> ");
  139.  
  140.     /*If the input is less than 1 or higher than 3 then
  141.     display an error message*/
  142.     while(((scanf(" %d", &choice)) == 0) ||
  143.           (choice < 1) || (choice > 3))
  144.     {
  145.         emptyBuffer();
  146.         puts("!! Please make a valid choice !!");
  147.         printf(">>> ");
  148.     }
  149.     emptyBuffer();
  150.  
  151.     return choice;
  152. }
  153.  
  154. void addPatient(void)
  155. {
  156.     /*Declaring variables for iteriation and patient details, using
  157.     2d array to store date of birth*/
  158.     FILE *patientDetails;
  159.     char patientFileName[265];
  160.     char pFirstName[256];
  161.     char pLastName[256];
  162.     char pDateOfBirth[3][256];
  163.     char pHeight[256];
  164.     char pWaistMeasurement[256];
  165.     char pWeight[256];
  166.     char pComment[512];
  167.  
  168.  
  169.     /*Displaying the header*/
  170.     showTopTitle("\t\t   || ADD A PATIENT ||");
  171.  
  172.     /*Storing non-encryped patient details into [0]*/
  173.     printf("First Name: ");
  174.     scanf("%[^\n]", &pFirstName);
  175.     emptyBuffer();
  176.  
  177.     printf("Last Name: ");
  178.     scanf("%[^\n]", &pLastName);
  179.     emptyBuffer();
  180.  
  181.     printf("DOB Day [DD]: ");
  182.     scanf("%[^\n]", &pDateOfBirth[0]);
  183.     emptyBuffer();
  184.  
  185.     printf("DOB Month [MM]: ");
  186.     scanf("%[^\n]", &pDateOfBirth[1]);
  187.     emptyBuffer();
  188.  
  189.     printf("DOB Year [YYYY]: ");
  190.     scanf("%[^\n]", &pDateOfBirth[2]);
  191.     emptyBuffer();
  192.  
  193.     printf("Height [cm]: ");
  194.     scanf("%[^\n]", &pHeight);
  195.     emptyBuffer();
  196.  
  197.     printf("Waist measurement [inches]: ");
  198.     scanf("%[^\n]", &pWaistMeasurement);
  199.     emptyBuffer();
  200.  
  201.     printf("Weight [kg]: ");
  202.     scanf("%[^\n]", &pWeight);
  203.     emptyBuffer();
  204.  
  205.     printf("Comment: ");
  206.     scanf("%[^\n]", &pComment);
  207.     emptyBuffer();
  208.  
  209.     /*Creating file name using the createFileName function*/
  210.     createFileName(pLastName, pDateOfBirth[0], pDateOfBirth[1],
  211.                    pDateOfBirth[2], patientFileName);
  212.  
  213.     /*Encrypting patient's details*/
  214.     encryptString(pFirstName, pFirstName);
  215.     encryptString(pLastName, pLastName);
  216.     encryptString(pDateOfBirth[0], pDateOfBirth[0]);
  217.     encryptString(pDateOfBirth[1], pDateOfBirth[1]);
  218.     encryptString(pDateOfBirth[2], pDateOfBirth[2]);
  219.     encryptString(pHeight, pHeight);
  220.     encryptString(pWaistMeasurement, pWaistMeasurement);
  221.     encryptString(pWeight, pWeight);
  222.     encryptString(pComment, pComment);
  223.  
  224.     /*Opening the file for writing purposes*/
  225.     patientDetails = fopen(patientFileName, "w");
  226.  
  227.     /*Writing user to a file*/
  228.     fprintf(patientDetails, "%s\n", pFirstName);
  229.     fprintf(patientDetails, "%s\n", pLastName);
  230.  
  231.     fprintf(patientDetails, "%s/%s/%s\n", pDateOfBirth[0],
  232.             pDateOfBirth[1], pDateOfBirth[2]);
  233.  
  234.     fprintf(patientDetails, "%s\n", pHeight);
  235.     fprintf(patientDetails, "%s\n", pWaistMeasurement);
  236.     fprintf(patientDetails, "%s\n", pWeight);
  237.     fprintf(patientDetails, "%s\n", pComment);
  238.  
  239.     /*Closing the file*/
  240.     fclose(patientDetails);
  241.  
  242.     equalsLine();
  243.     puts("Patient has been added!");
  244.     /*Display continue message using waitForKey function*/
  245.     waitForKey();
  246. }
  247.  
  248. /*Function that takes patient's last name and DOB input and returns
  249. a file name for the patient*/
  250. void createFileName(char pLastName[], char pDay[], char pMonth[],
  251.                     char pYear[], char patientFileName[])
  252. {
  253.     int i;
  254.     char pYearTwoDigits[3];
  255.  
  256.     strcpy(patientFileName, "Patients/");
  257.  
  258.     /*Adding last name, day and month to a file name*/
  259.     strcat(patientFileName, pLastName);
  260.     strcat(patientFileName, pDay);
  261.     strcat(patientFileName, pMonth);
  262.  
  263.     /*Copy two last characters from the patient year of birth and store
  264.     it to the variable*/
  265.     memcpy(pYearTwoDigits, &pYear[2], 2);
  266.  
  267.     /*Adding \0 to the variable to make it a string*/
  268.     pYearTwoDigits[2] = '\0';
  269.  
  270.     /*Adding last two DOB digits to the file name*/
  271.     strcat(patientFileName, pYearTwoDigits);
  272.     /*Add the .aow extension to the file name*/
  273.     strcat(patientFileName, ".aow");
  274.  
  275.     /*Iterating trough a file name*/
  276.     for(i = 0; patientFileName[i]; ++i)
  277.     {
  278.         /*Changing file first character to lowercase*/
  279.         patientFileName[i] = tolower(patientFileName[i]);
  280.     }
  281.  
  282. }
  283. /*A function for encryption which uses transform function*/
  284. void encryptString(char toEncrypt[], char toReturn[])
  285. {
  286.     /*Transforming character and moving it forwards by seven places*/
  287.     transformString(toEncrypt, toReturn, +7);
  288. }
  289. /*A function for decryption which uses transform function*/
  290. void decryptString(char toDecrypt[], char toReturn[])
  291. {
  292.     /*Transforming character and moving it backwards by seven places*/
  293.     transformString(toDecrypt, toReturn, -7);
  294. }
  295. /*A function which takes a string and transforms it's characters*/
  296. void transformString(char toEncrypt[], char toReturn[], int toJump)
  297. {
  298.     /*Declaring variables and storing length of the string to encrypt*/
  299.     int strLen = strlen(toEncrypt);
  300.     /*Declare variable that will hold the encrypted string*/
  301.     char encrypted[strLen+1];
  302.  
  303.     int i;
  304.     int character;
  305.  
  306.     /*Iterate trough the string*/
  307.     for(i = 0; i < strLen; ++i)
  308.     {
  309.         /*Assign one character of the string to
  310.         the character variable*/
  311.         character = toEncrypt[i];
  312.  
  313.         /*If the character is between a and z*/
  314.         if(character >= 'a' && character <= 'z')
  315.         {
  316.             /*Add toJump(7) to the character*/
  317.             character += toJump;
  318.  
  319.             /*If character is out of the range of z*/
  320.             if(character > 'z')
  321.             {
  322.                 /*Get the difference between the character a and z,
  323.                 subtract 1 and add it to the a value*/
  324.                 character = 'a' + (character - 'z') - 1;
  325.             }
  326.                 /*If the character is out of the range of a*/
  327.             else if(character < 'a')
  328.             {
  329.                 /*Get the difference between the character z and a,
  330.                 add 1 and add it to the z value*/
  331.                 character = 'z' + (character - 'a') + 1;
  332.             }
  333.         }
  334.             /*If the character is between A and Z*/
  335.         else if(character >= 'A' && character <= 'Z')
  336.         {
  337.             /*Add toJump(7) to the character*/
  338.             character += toJump;
  339.  
  340.             /*If character is out of the range of Z*/
  341.             if(character > 'Z')
  342.             {
  343.                 /*Get the difference between the character A and Z,
  344.                 subtract 1 and add it to the A value*/
  345.                 character = 'A' + (character - 'Z') - 1;
  346.             }
  347.                 /*If the character is out of the range of A*/
  348.             else if(character < 'A')
  349.             {
  350.                 /*Get the difference between the character Z and A,
  351.                 add 1 and add it to the Z value*/
  352.                 character = 'Z' + (character - 'A') + 1;
  353.             }
  354.         }
  355.             /*If the character is between 0 and 9*/
  356.         else if(character >= '0' && character <= '9')
  357.         {
  358.             //Add toJump(7) to the character
  359.             character += toJump;
  360.  
  361.             /*If character is out of the range of 9*/
  362.             if(character > '9')
  363.             {
  364.                 /*Get the difference between the character 0 and 9,
  365.                 subtract 1 and add it to the 9 value*/
  366.                 character = '0' + (character - '9') - 1;
  367.             }
  368.                 /*If character is out of the range of 0*/
  369.             else if(character < '0')
  370.             {
  371.                 /*Get the difference between the character 9 and 0,
  372.                 add 1 and add it to the 0 value*/
  373.                 character = '9' + (character - '0') + 1;
  374.             }
  375.         }
  376.         /*Store the encrypted character in the encrypted array*/
  377.         encrypted[i] = character;
  378.     }
  379.     /*Add \0 to the encrypted array to make it a string*/
  380.     encrypted[strLen] = '\0';
  381.  
  382.     /*Copy the encrypted string to the toReturn variable*/
  383.     strcpy(toReturn, encrypted);
  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 changePassword(int nurse)
  388. {
  389.     FILE *passwordFileRead;
  390.     FILE *passwordFileWrite;
  391.  
  392.     char newPassword[10];
  393.     char confirmPassword[10];
  394.  
  395.     /*Displaying the header*/
  396.     showTopTitle("\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]", confirmPassword);
  416.     emptyBuffer();
  417.  
  418.     /*Compaing the two password strings to see if they match*/
  419.     if(strcmp(newPassword, confirmPassword) == 0)
  420.     {
  421.         char encryptedPassword[10];
  422.  
  423.         /*Encrypting the new password*/
  424.         encryptString(confirmPassword, 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 showTopTitle(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 Any 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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement