Advertisement
Conner_36

c3 assignment

Sep 19th, 2011
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  c3
  4. //
  5. //  Created by Betsalel Williamson on 9/15/11.
  6. //
  7. //  Chapter 3:
  8. //  Write an encryption and decryption program.  
  9. //  Encrypt each digit by adding 7 and taking the remainder after division by 10.  
  10. //  After encrypting each digit, swap the first and third then swap the second and fourth.
  11. //  Decryption should reverse the process.
  12. //  Program must input a single integer.  
  13. //  Program must do encode and decode in one file.
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18.  
  19. //below is how to get the
  20. #define ONES_PLACE(A)      (A/1)    -    ((A/10) *10)
  21. #define TENS_PLACE(A)      (A/10)   -   ((A/100) *10)
  22. #define HUNDREDS_PLACE(A)  (A/100)  -  ((A/1000) *10)
  23. #define THOUSANDS_PLACE(A) (A/1000) - ((A/10000) *10)
  24.  
  25. static int GetInputMessage (void) {
  26.     int input;
  27.    
  28.     printf("Enter a four digit number: ");
  29.     scanf("%d", &input);
  30.    
  31.     return input;
  32. }
  33.  
  34. static int GetChoiceMessage (void) {
  35.     int input;
  36.    
  37.     printf("Encode (1) Decode (2): ");
  38.     scanf("%d", &input);
  39.    
  40.     return input;
  41. }
  42.  
  43. static int GetContinueMessage (void) {
  44.     int input;
  45.    
  46.     printf("Continue (1) Exit (0): ");
  47.     scanf("%d", &input);
  48.    
  49.     if (input != 0) {
  50.         input = GetChoiceMessage();
  51.     }
  52.    
  53.     return input;
  54. }
  55.  
  56. static int Encode (int notCode) {
  57.    
  58.     // get the 4 digits
  59.     int fourthDigit = ONES_PLACE(notCode);
  60.     int thirdDigit  = TENS_PLACE(notCode);
  61.     int secondDigit = HUNDREDS_PLACE(notCode);
  62.     int firstDigit  = THOUSANDS_PLACE(notCode);
  63.    
  64.     //  the below commented out code is psudeo-degugging tools
  65.     //    printf("notcode %d\n", notCode);
  66.     //    printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
  67.    
  68.     fourthDigit += 7;
  69.     thirdDigit  += 7;
  70.     secondDigit += 7;
  71.     firstDigit  += 7;
  72.    
  73.     //    printf("notcode %d\n", notCode);
  74.     //    printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
  75.    
  76.     fourthDigit = fourthDigit%10;
  77.     thirdDigit  = thirdDigit%10;
  78.     secondDigit = secondDigit%10;
  79.     firstDigit  = firstDigit%10;
  80.    
  81.     //    printf("notcode %d\n", notCode);
  82.     //    printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
  83.    
  84.     return printf("Encoded Digits: %d%d%d%d\n", thirdDigit, fourthDigit, firstDigit, secondDigit);
  85. }
  86.  
  87. static int Decode (int code) {
  88.    
  89.     //get the four digits
  90.     int fourthDigit = ONES_PLACE(code);
  91.     int thirdDigit  = TENS_PLACE(code);
  92.     int secondDigit = HUNDREDS_PLACE(code);
  93.     int firstDigit  = THOUSANDS_PLACE(code);
  94.    
  95.     //  the below commented out code is psudeo-degugging tools
  96.     //    printf("code %d\n", code);
  97.     //    printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
  98.    
  99.     fourthDigit = fourthDigit+10;
  100.     thirdDigit  = thirdDigit+10;
  101.     secondDigit = secondDigit+10;
  102.     firstDigit  = firstDigit+10;
  103.    
  104.     //    printf("code %d\n", code);
  105.     //    printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
  106.    
  107.     fourthDigit -= 7;
  108.     thirdDigit  -= 7;
  109.     secondDigit -= 7;
  110.     firstDigit  -= 7;
  111.    
  112.     //    printf("code %d\n", code);
  113.     //    printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
  114.    
  115.     fourthDigit = fourthDigit%10;
  116.     thirdDigit  = thirdDigit%10;
  117.     secondDigit = secondDigit%10;
  118.     firstDigit  = firstDigit%10;
  119.    
  120.     //    printf("code %d\n", code);
  121.     //    printf("%d, %d, %d, %d\n", firstDigit, secondDigit, thirdDigit, fourthDigit);
  122.    
  123.     return printf("Decoded Digits: %d%d%d%d\n", thirdDigit, fourthDigit, firstDigit, secondDigit);
  124. }
  125.  
  126.  
  127. int main (int argc, const char * argv[])
  128. {
  129.     //  the input is declared and initialized before the while loop to make sure that it's not 0.
  130.     int input = GetChoiceMessage();
  131.    
  132.     while (input != 0) {
  133.        
  134.         if (input == 1) {
  135.             input = GetInputMessage();
  136.             Encode(input);
  137.         }
  138.        
  139.         else if (input == 2) {
  140.             input = GetInputMessage();
  141.             Decode(input);
  142.         }
  143.        
  144.         input = GetContinueMessage();
  145.     }
  146.    
  147.     return EXIT_SUCCESS;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement