fork download
  1. /**************************************************
  2.   * credit.c *
  3.   * *
  4.   * CS50 *
  5.   * *
  6.   * Prompts the user for a credit card number and *
  7.   * *
  8.   * determines the type of the card. *
  9.   *************************************************/
  10.  
  11. #include <cs50.h>
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <string.h>
  15.  
  16. /* enumeration containing the result of the test on the credit card number ...
  17.   frankly I do not know why I decided to do it that way ... */
  18. enum credit_card_type {AMEX, MASTERCARD, VISA, INVALID} type;
  19.  
  20. // used to obtain a string equivalent of an enumerated value
  21. char card_type[][26] = {"AMEX", "MASTERCARD", "VISA", "INVALID"};
  22.  
  23. void is_it_legit(long long int credit_card_number, int credit_card_number_length);
  24. void what_kinda_credit_card(long long int credit_card_number, int credit_card_number_length);
  25.  
  26. int
  27. main(void)
  28. {
  29. // used to determine the length of the credit card number
  30. char temp_credit_card_number[17];
  31.  
  32. // prompt the user for a credit card number
  33. printf("Enter a credit card number: ");
  34. long long int credit_card_number = GetLongLong();
  35.  
  36. // "credit_card_number" converted to a string in order to determine its length
  37. snprintf(temp_credit_card_number, 17, "%lld", credit_card_number);
  38. int credit_card_number_length = strlen(temp_credit_card_number);
  39.  
  40. what_kinda_credit_card(credit_card_number, credit_card_number_length);
  41.  
  42. return 0;
  43. }
  44.  
  45. /*
  46.   * Test the legitimacy of the credit card number.
  47.   */
  48. void
  49. is_it_legit(long long int credit_card_number, int credit_card_number_length)
  50. {
  51. long long int every_other_digit;
  52. long long int left_out_digit;
  53. int products_digits_sum = 0;
  54. int left_out_digits_sum = 0;
  55.  
  56. /* isolate every other digits starting with the number's second-to-last digit,
  57.   multiply it by 2, and then add those products' digits together */
  58. for (int i = credit_card_number_length - 2; i > -1; i -= 2 ) {
  59. every_other_digit = ((long long int) (credit_card_number / pow(10, i))) % 10;
  60. products_digits_sum += ((every_other_digit * 2) / 10) + ((every_other_digit * 2) % 10);
  61. }
  62.  
  63. // isolate the digits which weren't multiplied by 2, and then add these digits together
  64. for (int i = credit_card_number_length - 1; i > -1; i -= 2) {
  65. left_out_digit = ((long long int) (credit_card_number / pow(10, i))) % 10;
  66. left_out_digits_sum += left_out_digit;
  67. }
  68.  
  69. int final_sum = products_digits_sum + left_out_digits_sum;
  70.  
  71. // test weather the last digit of "final_sum" is 0 or not
  72. if (final_sum % 10 == 0)
  73. // print type of credit card
  74. printf("%s\n", card_type[type]);
  75. else
  76. // print invalid message
  77. printf("%s\n", card_type[3]);
  78. }
  79.  
  80. /*
  81.   * Determine the type of the credit card.
  82.   */
  83. void
  84. what_kinda_credit_card(long long int credit_card_number, int credit_card_number_length)
  85. {
  86. // these variables store the first or two first digits of a credit card number.
  87. int visa_first_digit_13 = (credit_card_number / pow(10, 12));
  88. int visa_first_digit_16 = (credit_card_number / pow(10, 15));
  89. int amex_two_first_digits = (credit_card_number / pow(10, 13));
  90. int mastercard_two_first_digits = (credit_card_number / pow(10, 14));
  91.  
  92. switch (credit_card_number_length) {
  93. case 13:
  94. // VISA ?
  95. if (visa_first_digit_13 == 4) {
  96. /* the integer value "2" is assigned to the variable "result".
  97.   In the enumeration "credit_card_type" the integer constant 2
  98.   is named VISA */
  99. type = 2;
  100. is_it_legit(credit_card_number, credit_card_number_length);
  101. } else
  102. // print invalid message
  103. printf("%s\n", card_type[3]);
  104. break;
  105.  
  106. case 15:
  107. // AMEX ?
  108. if ((amex_two_first_digits == 34) || (amex_two_first_digits == 37)) {
  109. type = 0;
  110. is_it_legit(credit_card_number, credit_card_number_length);
  111. } else
  112. printf("%s\n", card_type[3]);
  113. break;
  114.  
  115. case 16:
  116. // VISA ?
  117. if (visa_first_digit_16 == 4) {
  118. type = 2;
  119. is_it_legit(credit_card_number, credit_card_number_length);
  120. // MASTERCARD ?
  121. } else if ((mastercard_two_first_digits == 51)
  122. || (mastercard_two_first_digits == 52)
  123. || (mastercard_two_first_digits == 53)
  124. || (mastercard_two_first_digits == 54)
  125. || (mastercard_two_first_digits == 55)) {
  126. type = 1;
  127. is_it_legit(credit_card_number, credit_card_number_length);
  128. } else
  129. printf("%s\n", card_type[3]);
  130. break;
  131.  
  132. default:
  133. printf("%s\n", card_type[3]);
  134. }
  135. }
  136.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty