Advertisement
Guest User

Credit (if statement)

a guest
Nov 22nd, 2019
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cs50.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. // Break credit card number into smaller integers (4x4 for 16-length, 3x5 for 15 length), and performs arithmetic on those smaller pieces to build sum 1 and sum 2.
  8. int segment16_sum1_1(long long a);
  9. int segment16_sum1_2(long long a);
  10. int segment16_sum1_3(long long a);
  11. int segment16_sum1_4(long long a);
  12. int segment16_sum2_1(long long a);
  13. int segment16_sum2_2(long long a);
  14. int segment16_sum2_3(long long a);
  15. int segment16_sum2_4(long long a);
  16.  
  17. int main(void)
  18. {
  19.     // Acquire credit card string for character and length validation.
  20.     string credit_card1 = get_string("CC#: ");
  21.  
  22.     // Convert string to long long integer. Establish sum tallies.
  23.     long long credit_card2 = atoll(credit_card1);
  24.     int sum1 = 0;
  25.     int sum2 = 0;
  26.  
  27.     // Card number validation.
  28.     if (strlen(credit_card1) == 16)
  29.     {
  30.         sum1 = sum1 + segment16_sum1_1(credit_card2);
  31.         printf("SUM 1: %i\n", sum1);
  32.         sum1 = sum1 + segment16_sum1_2(credit_card2);
  33.         printf("SUM 1: %i\n", sum1);
  34.         sum1 = sum1 + segment16_sum1_3(credit_card2);
  35.         printf("SUM 1: %i\n", sum1);
  36.         sum1 = sum1 + segment16_sum1_4(credit_card2);
  37.         printf("SUM 1: %i\n", sum1);
  38.  
  39.         sum2 = sum2 + segment16_sum2_1(credit_card2);
  40.         printf("SUM 2: %i\n", sum2);
  41.         sum2 = sum2 + segment16_sum2_2(credit_card2);
  42.         printf("SUM 2: %i\n", sum2);
  43.         sum2 = sum2 + segment16_sum2_3(credit_card2);
  44.         printf("SUM 2: %i\n", sum2);
  45.         sum2 = sum2 + segment16_sum2_4(credit_card2);
  46.         printf("SUM 2: %i\n", sum2);
  47.     }
  48.  
  49.     printf("%i\n", sum1);
  50.     printf("%i\n", sum2);
  51. }
  52.  
  53. // Doubles every other digit of the first quarter of a 16-length card, starting with the second digit, and adds total to sum 1.
  54. int segment16_sum1_1(long long a)
  55. {
  56.     long first_quarter = a % 10000;
  57.     long divisor1 = 100;
  58.     long divisor2 = 10;
  59.     int sum1 = 0;
  60.  
  61.     for (int i = 0; i < 2; i++)
  62.     {
  63.         int isolate1 = first_quarter % divisor1;
  64.         int isolate2 = isolate1 % divisor2;
  65.         int isolate3 = (isolate1 - isolate2) / divisor2;
  66.         int doubled = isolate3 * 2;
  67.         printf("whole -- %i\n", doubled);
  68.  
  69.         // If the sum is a two digit number, set sum to the sum of those two digits.
  70.         if (sum1 > 9)
  71.         {
  72.             int sum1_A = (sum1 - (sum1 % 10)) / 10;
  73.             int sum1_B = (sum1 % 10);
  74.             doubled = sum1_A + sum1_B;
  75.             printf("divided -- %i - %i\n", sum1_A, sum1_B);
  76.         }
  77.  
  78.         sum1 = sum1 + doubled;
  79.         divisor1 = divisor1 * 100;
  80.         divisor2 = divisor2 * 100;
  81.  
  82.     }
  83.  
  84.     return sum1;
  85. }
  86.  
  87. // Doubles every other digit of the second quarter of a 16-length card, starting with the second digit, and adds total to sum 1.
  88. int segment16_sum1_2(long long a)
  89. {
  90.     long second_quarter = ((a - (a % 10000)) / 10000) % 10000;
  91.     long divisor1 = 100;
  92.     long divisor2 = 10;
  93.     int sum1 = 0;
  94.  
  95.     for (int i = 0; i < 2; i++)
  96.     {
  97.         int isolate1 = second_quarter % divisor1;
  98.         int isolate2 = isolate1 % divisor2;
  99.         int isolate3 = (isolate1 - isolate2) / divisor2;
  100.         int doubled = isolate3 * 2;
  101.         printf("whole -- %i\n", doubled);
  102.  
  103.         // If the sum is a two digit number, set sum to the sum of those two digits.
  104.         if (sum1 > 9)
  105.         {
  106.             int sum1_A = (sum1 - (sum1 % 10)) / 10;
  107.             int sum1_B = (sum1 % 10);
  108.             doubled = sum1_A + sum1_B;
  109.             printf("divided -- %i - %i\n", sum1_A, sum1_B);
  110.         }
  111.  
  112.         sum1 = sum1 + doubled;
  113.         divisor1 = divisor1 * 100;
  114.         divisor2 = divisor2 * 100;
  115.  
  116.     }
  117.  
  118.     return sum1;
  119. }
  120.  
  121. // Doubles every other digit of the third quarter of a 16-length card, starting with the second digit, and adds total to sum 1.
  122. int segment16_sum1_3(long long a)
  123. {
  124.     long third_quarter = ((a % 1000000000000) - (a % 100000000)) / 100000000;
  125.     long divisor1 = 100;
  126.     long divisor2 = 10;
  127.     int sum1 = 0;
  128.  
  129.     for (int i = 0; i < 2; i++)
  130.     {
  131.         int isolate1 = third_quarter % divisor1;
  132.         int isolate2 = isolate1 % divisor2;
  133.         int isolate3 = (isolate1 - isolate2) / divisor2;
  134.         int doubled = isolate3 * 2;
  135.         printf("whole -- %i\n", doubled);
  136.  
  137.         // If the sum is a two digit number, set sum to the sum of those two digits.
  138.         if (sum1 > 9)
  139.         {
  140.             int sum1_A = (sum1 - (sum1 % 10)) / 10;
  141.             int sum1_B = (sum1 % 10);
  142.             doubled = sum1_A + sum1_B;
  143.             printf("divided -- %i - %i\n", sum1_A, sum1_B);
  144.         }
  145.  
  146.         sum1 = sum1 + doubled;
  147.         divisor1 = divisor1 * 100;
  148.         divisor2 = divisor2 * 100;
  149.  
  150.     }
  151.  
  152.     return sum1;
  153. }
  154.  
  155. // Doubles every other digit of the fourth quarter of a 16-length card, starting with the second digit, and adds total to sum 1.
  156. int segment16_sum1_4(long long a)
  157. {
  158.     long fourth_quarter = (a - (a % 1000000000000)) / 1000000000000;
  159.     long divisor1 = 100;
  160.     long divisor2 = 10;
  161.     int sum1 = 0;
  162.  
  163.     for (int i = 0; i < 2; i++)
  164.     {
  165.         int isolate1 = fourth_quarter % divisor1;
  166.         int isolate2 = isolate1 % divisor2;
  167.         int isolate3 = (isolate1 - isolate2) / divisor2;
  168.         int doubled = isolate3 * 2;
  169.         printf("whole -- %i\n", doubled);
  170.  
  171.         // If the sum is a two digit number, set sum to the sum of those two digits.
  172.         if (sum1 > 9)
  173.         {
  174.             int sum1_A = (sum1 - (sum1 % 10)) / 10;
  175.             int sum1_B = (sum1 % 10);
  176.             doubled = sum1_A + sum1_B;
  177.             printf("divided -- %i - %i\n", sum1_A, sum1_B);
  178.         }
  179.  
  180.         sum1 = sum1 + doubled;
  181.         divisor1 = divisor1 * 100;
  182.         divisor2 = divisor2 * 100;
  183.  
  184.     }
  185.  
  186.     return sum1;
  187. }
  188.  
  189. // Doubles every other digit of the first quarter of a 16-length card, starting with the first digit, and adds total to sum 1.
  190. int segment16_sum2_1(long long a)
  191. {
  192.     long first_quarter = a % 10000;
  193.     int sum2 = 0;
  194.  
  195.     sum2 = sum2 + (first_quarter % 10);
  196.     sum2 = sum2 + ((first_quarter % 1000) - (first_quarter % 100)) / 100;
  197.     printf("whole2 -- %i\n", sum2);
  198.  
  199.     return sum2;
  200. }
  201.  
  202. // Doubles every other digit of the second quarter of a 16-length card, starting with the first digit, and adds total to sum 1.
  203. int segment16_sum2_2(long long a)
  204. {
  205.     long second_quarter = ((a - (a % 10000)) / 10000) % 10000;
  206.     int sum2 = 0;
  207.  
  208.     sum2 = sum2 + (second_quarter % 10);
  209.     sum2 = sum2 + ((second_quarter % 1000) - (second_quarter % 100)) / 100;
  210.     printf("whole2 -- %i\n", sum2);
  211.  
  212.     return sum2;
  213. }
  214.  
  215. // Doubles every other digit of the third quarter of a 16-length card, starting with the first digit, and adds total to sum 1.
  216. int segment16_sum2_3(long long a)
  217. {
  218.     long third_quarter = ((a % 1000000000000) - (a % 100000000)) / 100000000;
  219.     int sum2 = 0;
  220.  
  221.     sum2 = sum2 + (third_quarter % 10);
  222.     sum2 = sum2 + ((third_quarter % 1000) - (third_quarter % 100)) / 100;
  223.     printf("whole2 -- %i\n", sum2);
  224.  
  225.     return sum2;
  226. }
  227.  
  228. // Doubles every other digit of the fourth quarter of a 16-length card, starting with the first digit, and adds total to sum 1.
  229. int segment16_sum2_4(long long a)
  230. {
  231.     long fourth_quarter = (a - (a % 1000000000000)) / 1000000000000;
  232.     int sum2 = 0;
  233.  
  234.     sum2 = sum2 + (fourth_quarter % 10);
  235.     sum2 = sum2 + ((fourth_quarter % 1000) - (fourth_quarter % 100)) / 100;
  236.     printf("whole2 -- %i\n", sum2);
  237.  
  238.     return sum2;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement