Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <cs50.h>
- #include <string.h>
- #include <ctype.h>
- // 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.
- int segment16_sum1_1(long long a);
- int segment16_sum1_2(long long a);
- int segment16_sum1_3(long long a);
- int segment16_sum1_4(long long a);
- int segment16_sum2_1(long long a);
- int segment16_sum2_2(long long a);
- int segment16_sum2_3(long long a);
- int segment16_sum2_4(long long a);
- int main(void)
- {
- // Acquire credit card string for character and length validation.
- string credit_card1 = get_string("CC#: ");
- // Convert string to long long integer. Establish sum tallies.
- long long credit_card2 = atoll(credit_card1);
- int sum1 = 0;
- int sum2 = 0;
- // Card number validation.
- if (strlen(credit_card1) == 16)
- {
- sum1 = sum1 + segment16_sum1_1(credit_card2);
- printf("SUM 1: %i\n", sum1);
- sum1 = sum1 + segment16_sum1_2(credit_card2);
- printf("SUM 1: %i\n", sum1);
- sum1 = sum1 + segment16_sum1_3(credit_card2);
- printf("SUM 1: %i\n", sum1);
- sum1 = sum1 + segment16_sum1_4(credit_card2);
- printf("SUM 1: %i\n", sum1);
- sum2 = sum2 + segment16_sum2_1(credit_card2);
- printf("SUM 2: %i\n", sum2);
- sum2 = sum2 + segment16_sum2_2(credit_card2);
- printf("SUM 2: %i\n", sum2);
- sum2 = sum2 + segment16_sum2_3(credit_card2);
- printf("SUM 2: %i\n", sum2);
- sum2 = sum2 + segment16_sum2_4(credit_card2);
- printf("SUM 2: %i\n", sum2);
- }
- printf("%i\n", sum1);
- printf("%i\n", sum2);
- }
- // Doubles every other digit of the first quarter of a 16-length card, starting with the second digit, and adds total to sum 1.
- int segment16_sum1_1(long long a)
- {
- long first_quarter = a % 10000;
- long divisor1 = 100;
- long divisor2 = 10;
- int sum1 = 0;
- for (int i = 0; i < 2; i++)
- {
- int isolate1 = first_quarter % divisor1;
- int isolate2 = isolate1 % divisor2;
- int isolate3 = (isolate1 - isolate2) / divisor2;
- int doubled = isolate3 * 2;
- printf("whole -- %i\n", doubled);
- // If the sum is a two digit number, set sum to the sum of those two digits.
- if (sum1 > 9)
- {
- int sum1_A = (sum1 - (sum1 % 10)) / 10;
- int sum1_B = (sum1 % 10);
- doubled = sum1_A + sum1_B;
- printf("divided -- %i - %i\n", sum1_A, sum1_B);
- }
- sum1 = sum1 + doubled;
- divisor1 = divisor1 * 100;
- divisor2 = divisor2 * 100;
- }
- return sum1;
- }
- // Doubles every other digit of the second quarter of a 16-length card, starting with the second digit, and adds total to sum 1.
- int segment16_sum1_2(long long a)
- {
- long second_quarter = ((a - (a % 10000)) / 10000) % 10000;
- long divisor1 = 100;
- long divisor2 = 10;
- int sum1 = 0;
- for (int i = 0; i < 2; i++)
- {
- int isolate1 = second_quarter % divisor1;
- int isolate2 = isolate1 % divisor2;
- int isolate3 = (isolate1 - isolate2) / divisor2;
- int doubled = isolate3 * 2;
- printf("whole -- %i\n", doubled);
- // If the sum is a two digit number, set sum to the sum of those two digits.
- if (sum1 > 9)
- {
- int sum1_A = (sum1 - (sum1 % 10)) / 10;
- int sum1_B = (sum1 % 10);
- doubled = sum1_A + sum1_B;
- printf("divided -- %i - %i\n", sum1_A, sum1_B);
- }
- sum1 = sum1 + doubled;
- divisor1 = divisor1 * 100;
- divisor2 = divisor2 * 100;
- }
- return sum1;
- }
- // Doubles every other digit of the third quarter of a 16-length card, starting with the second digit, and adds total to sum 1.
- int segment16_sum1_3(long long a)
- {
- long third_quarter = ((a % 1000000000000) - (a % 100000000)) / 100000000;
- long divisor1 = 100;
- long divisor2 = 10;
- int sum1 = 0;
- for (int i = 0; i < 2; i++)
- {
- int isolate1 = third_quarter % divisor1;
- int isolate2 = isolate1 % divisor2;
- int isolate3 = (isolate1 - isolate2) / divisor2;
- int doubled = isolate3 * 2;
- printf("whole -- %i\n", doubled);
- // If the sum is a two digit number, set sum to the sum of those two digits.
- if (sum1 > 9)
- {
- int sum1_A = (sum1 - (sum1 % 10)) / 10;
- int sum1_B = (sum1 % 10);
- doubled = sum1_A + sum1_B;
- printf("divided -- %i - %i\n", sum1_A, sum1_B);
- }
- sum1 = sum1 + doubled;
- divisor1 = divisor1 * 100;
- divisor2 = divisor2 * 100;
- }
- return sum1;
- }
- // Doubles every other digit of the fourth quarter of a 16-length card, starting with the second digit, and adds total to sum 1.
- int segment16_sum1_4(long long a)
- {
- long fourth_quarter = (a - (a % 1000000000000)) / 1000000000000;
- long divisor1 = 100;
- long divisor2 = 10;
- int sum1 = 0;
- for (int i = 0; i < 2; i++)
- {
- int isolate1 = fourth_quarter % divisor1;
- int isolate2 = isolate1 % divisor2;
- int isolate3 = (isolate1 - isolate2) / divisor2;
- int doubled = isolate3 * 2;
- printf("whole -- %i\n", doubled);
- // If the sum is a two digit number, set sum to the sum of those two digits.
- if (sum1 > 9)
- {
- int sum1_A = (sum1 - (sum1 % 10)) / 10;
- int sum1_B = (sum1 % 10);
- doubled = sum1_A + sum1_B;
- printf("divided -- %i - %i\n", sum1_A, sum1_B);
- }
- sum1 = sum1 + doubled;
- divisor1 = divisor1 * 100;
- divisor2 = divisor2 * 100;
- }
- return sum1;
- }
- // Doubles every other digit of the first quarter of a 16-length card, starting with the first digit, and adds total to sum 1.
- int segment16_sum2_1(long long a)
- {
- long first_quarter = a % 10000;
- int sum2 = 0;
- sum2 = sum2 + (first_quarter % 10);
- sum2 = sum2 + ((first_quarter % 1000) - (first_quarter % 100)) / 100;
- printf("whole2 -- %i\n", sum2);
- return sum2;
- }
- // Doubles every other digit of the second quarter of a 16-length card, starting with the first digit, and adds total to sum 1.
- int segment16_sum2_2(long long a)
- {
- long second_quarter = ((a - (a % 10000)) / 10000) % 10000;
- int sum2 = 0;
- sum2 = sum2 + (second_quarter % 10);
- sum2 = sum2 + ((second_quarter % 1000) - (second_quarter % 100)) / 100;
- printf("whole2 -- %i\n", sum2);
- return sum2;
- }
- // Doubles every other digit of the third quarter of a 16-length card, starting with the first digit, and adds total to sum 1.
- int segment16_sum2_3(long long a)
- {
- long third_quarter = ((a % 1000000000000) - (a % 100000000)) / 100000000;
- int sum2 = 0;
- sum2 = sum2 + (third_quarter % 10);
- sum2 = sum2 + ((third_quarter % 1000) - (third_quarter % 100)) / 100;
- printf("whole2 -- %i\n", sum2);
- return sum2;
- }
- // Doubles every other digit of the fourth quarter of a 16-length card, starting with the first digit, and adds total to sum 1.
- int segment16_sum2_4(long long a)
- {
- long fourth_quarter = (a - (a % 1000000000000)) / 1000000000000;
- int sum2 = 0;
- sum2 = sum2 + (fourth_quarter % 10);
- sum2 = sum2 + ((fourth_quarter % 1000) - (fourth_quarter % 100)) / 100;
- printf("whole2 -- %i\n", sum2);
- return sum2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement