Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <conio.h>
- #define SIZE 4
- //prototypes
- void input(char[], int[]);
- void encrypt(int[], const int [], int*, int*, int*);
- void decrypt(int[], int*);
- main()
- {
- //2 variables to store number of times the code was entered successfully ot incorrectly
- int right;
- int wrong;
- //store chosen option
- char option;
- //store code that user entered
- char user_code[SIZE] = " ";
- //store converted code (from char to int)
- int int_code[SIZE] = {0};
- //access code that is constant
- const int access_code[SIZE] = {4, 5, 2, 3};
- //counter to check if code is already encrypted
- int already_encrypted;
- right = wrong = already_encrypted = 0;
- do
- {
- printf("\nPlease select an option\n");
- printf("1. Enter code\n");
- printf("2. Encrypt code and verify if correct\n");
- printf("3. Decrypt code\n");
- printf("4. Number of times code entered correctly and incorrectly\n");
- printf("5. Exit program\n\n");
- //read in user input
- scanf("%c", &option);
- //to determine which function to call
- switch(option)
- {
- //executes when option 1 is chosen
- case '1':
- {
- //will ignore numbers after the first if user enters more than one number (is the same for every case)
- flushall();
- //call function input()
- input(user_code, int_code);
- break;
- }//end case 1
- //executes when option 2 is chosen
- case '2':
- {
- flushall();
- //call function encrypt()
- encrypt(int_code, access_code, &right, &wrong, &already_encrypted);
- break;
- }//end case 2
- //executes when option 3 is chosen
- case '3':
- {
- flushall();
- //call funtion decrypt()
- decrypt(int_code, &already_encrypted);
- break;
- }//end case 3
- //executes when option 4 is chosen
- case '4':
- {
- flushall();
- clrscr();
- //prints number of times correct and number of times wrong
- printf("\nThe number of times the code was entered successfully is: \t%d \n", right);
- printf("The number of times the code was entered incorrectly is: \t%d \n", wrong);
- break;
- }//end case 4
- //executes when option 5 is chosen
- case '5':
- {
- flushall();
- clrscr();
- printf("\nProgram will now end.");
- break;
- }//end case 5
- //executes when user enters invalid option
- default:
- {
- flushall();
- printf("\nError: Invalid option chosen. \n");
- getchar();
- break;
- }//end default
- }//end switch
- }//end do
- while(option != '5');
- getchar();
- }//end main()
- /*Implement function input(). This function allows user to enter a 4-digit code that will
- be stored to be used in encryption process.
- */
- void input(char user_code[SIZE], int int_code[SIZE])
- {
- clrscr();
- int i;
- int error; //if there are values that aren't integers
- //int temp; //to store a value temporarily while converting from char to int
- error = 0;
- //prompt then read user input
- printf("\nPlease enter a %d-digit code.\n", SIZE);
- gets(user_code);
- //check if all values are numbers
- for(i = 0; i <SIZE ; i++)
- {
- switch(*(user_code + i))
- {
- //if values are numbers
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- case '0':
- {
- break;
- }//end cases
- //if there is a value that is not an integer
- default:
- {
- error++;
- break;
- }//end default
- }//end switch
- }//end for
- //if there is an error
- if(error != 0)
- {
- printf("\n %d errors encountered.\n", error);
- }//end if
- else
- {
- //convert array of characters into array of integers
- for(i = 0; i < SIZE; i++)
- {
- *(int_code + i) = *(user_code + i) - '0';
- }//end for
- }//end else
- }//end input()
- /*Implement function encrypt(). This will encrypt the code that the user entered and it will
- then compare the encrypted code with the access code. If codes match, display message
- "CORRECT CODE". If codes don't match, display message "ERROR CODE".
- */
- void encrypt(int int_code[SIZE], const int access_code[SIZE], int *right, int *wrong, int *already_encrypted)
- {
- clrscr();
- //temporary variable to store values for swapping
- int temp;
- int i;
- int num;//for amount of matches out of 4 elements
- temp = num = 0;
- if(*already_encrypted == 0)
- {
- //swap elements
- for(i = 0; i < SIZE; i++)
- {
- //swap 1st and 3rd element
- if(i == 0)
- {
- //put 1st element in temp1
- temp = *(int_code + i);
- //put 3rd element in 1st position
- *(int_code + i) = *(int_code + (i+2));
- //put 1st element (stored in temp) in 3rd position
- *(int_code + (i+2)) = temp;
- }//end if
- //repeat previous if() but with 2nd and 4th elements
- if(i == 1)
- {
- temp = *(int_code + i);
- *(int_code + i) = *(int_code + (i+2));
- *(int_code + (i+2)) = temp;
- }//end if
- }//end for
- //add 1 to all elements
- for(i = 0; i < SIZE; i++)
- {
- *(int_code + i) = (*(int_code + i) + 1);
- //if value is 10, change it to 0
- if(*(int_code + i) == 10)
- {
- *(int_code + i) = 0;
- }//end if
- }//end for
- //check if codes match
- for(i = 0; i < SIZE; i++)
- {
- if(*(int_code + i) == *(access_code + i))
- {
- num++;
- }//end if
- }//end for
- //increments right by 1 if there are 4 matches
- if(num == SIZE)
- {
- *right = (*right + 1);
- printf("\nCORRECT CODE\n");
- }//end if
- else
- {
- *wrong = (*wrong + 1);
- printf("\nERROR CODE\n");
- }//end else
- *already_encrypted = 1;
- }//end if
- else
- {
- printf("\nThe code has already been encrypted.Cannot encrypt again.\n");
- }//end else
- }//end encrypt()
- /*Implement function decrypt(). This will allow the user to decrypt the code ONLY if the code has
- already been encrypted.
- */
- void decrypt(int int_code[SIZE], int *already_encrypted)
- {
- clrscr();
- int i;
- int temp = 0;
- if(*already_encrypted == 1)
- {
- //subtract 1 from all elements
- for(i = 0; i < SIZE; i++)
- {
- *(int_code + i) = (*(int_code + i) - 1);
- //if value is -1, change it to 9
- if(*(int_code + i) == -1)
- {
- *(int_code + 1) = 9;
- }//end if
- }//end for
- //swap elements
- for(i = 0; i < SIZE; i++)
- {
- //swap 1st and 3rd element
- if(i == 0)
- {
- //put 1st element in temp1
- temp = *(int_code + i);
- //put 3rd element in 1st position
- *(int_code + i) = *(int_code + (i+2));
- //put 1st element (stored in temp) in 3rd position
- *(int_code + (i+2)) = temp;
- }//end if
- //repeat previous if() but with 2nd and 4th elements
- if(i == 1)
- {
- temp = *(int_code + i);
- *(int_code + i) = *(int_code + (i+2));
- *(int_code + (i+2)) = temp;
- }//end if
- }//end for
- *already_encrypted = 0;
- }//end if
- else
- {
- printf("\nThe code has not been encrypted.\n");
- }//end else
- }//end decrypt()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement