Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // c3
- //
- // Created by Betsalel Williamson on 9/15/11.
- //
- // Chapter 3:
- // Write an encryption and decryption program.
- // Encrypt each digit by adding 7 and taking the remainder after division by 10.
- // After encrypting each digit, swap the first and third then swap the second and fourth.
- // Decryption should reverse the process.
- // Program must input a single integer.
- // Program must do encode and decode in one file.
- #include <stdio.h>
- #include <stdlib.h>
- //below is how to get the
- #define ONES_PLACE(A) (A/1) - ((A/10) *10)
- #define TENS_PLACE(A) (A/10) - ((A/100) *10)
- #define HUNDREDS_PLACE(A) (A/100) - ((A/1000) *10)
- #define THOUSANDS_PLACE(A) (A/1000) - ((A/10000) *10)
- static int GetInputMessage (void) {
- int input;
- printf("Enter a four digit number: ");
- scanf("%d", &input);
- return input;
- }
- static int GetChoiceMessage (void) {
- int input;
- printf("Encode (1) Decode (2): ");
- scanf("%d", &input);
- return input;
- }
- static int GetContinueMessage (void) {
- int input;
- printf("Continue (1) Exit (0): ");
- scanf("%d", &input);
- if (input != 0) {
- input = GetChoiceMessage();
- }
- return input;
- }
- static int Encode (int notCode) {
- // get the 4 digits
- int fourthDigit = ONES_PLACE(notCode);
- int thirdDigit = TENS_PLACE(notCode);
- int secondDigit = HUNDREDS_PLACE(notCode);
- int firstDigit = THOUSANDS_PLACE(notCode);
- // the below commented out code is psudeo-degugging tools
- // printf("notcode %d\n", notCode);
- // printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
- fourthDigit += 7;
- thirdDigit += 7;
- secondDigit += 7;
- firstDigit += 7;
- // printf("notcode %d\n", notCode);
- // printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
- fourthDigit = fourthDigit%10;
- thirdDigit = thirdDigit%10;
- secondDigit = secondDigit%10;
- firstDigit = firstDigit%10;
- // printf("notcode %d\n", notCode);
- // printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
- return printf("Encoded Digits: %d%d%d%d\n", thirdDigit, fourthDigit, firstDigit, secondDigit);
- }
- static int Decode (int code) {
- //get the four digits
- int fourthDigit = ONES_PLACE(code);
- int thirdDigit = TENS_PLACE(code);
- int secondDigit = HUNDREDS_PLACE(code);
- int firstDigit = THOUSANDS_PLACE(code);
- // the below commented out code is psudeo-degugging tools
- // printf("code %d\n", code);
- // printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
- fourthDigit = fourthDigit+10;
- thirdDigit = thirdDigit+10;
- secondDigit = secondDigit+10;
- firstDigit = firstDigit+10;
- // printf("code %d\n", code);
- // printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
- fourthDigit -= 7;
- thirdDigit -= 7;
- secondDigit -= 7;
- firstDigit -= 7;
- // printf("code %d\n", code);
- // printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
- fourthDigit = fourthDigit%10;
- thirdDigit = thirdDigit%10;
- secondDigit = secondDigit%10;
- firstDigit = firstDigit%10;
- // printf("code %d\n", code);
- // printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
- return printf("Decoded Digits: %d%d%d%d\n", thirdDigit, fourthDigit, firstDigit, secondDigit);
- }
- int main (int argc, const char * argv[])
- {
- // the input is declared and initialized before the while loop to make sure that it's not 0.
- int input = GetChoiceMessage();
- while (input != 0) {
- if (input == 1) {
- input = GetInputMessage();
- Encode(input);
- }
- else if (input == 2) {
- input = GetInputMessage();
- Decode(input);
- }
- input = GetContinueMessage();
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement