Guest User

CreditOptimized

a guest
Mar 21st, 2022
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.06 KB | None | 0 0
  1. /*
  2.  
  3.     This is program to check if credit card numbers are valid using Luhns algorithm and simple rules
  4.     for the starting and lenght numbers of cards
  5.     Optimized version
  6.  
  7. */
  8.  
  9.  
  10. #include <cs50.h>
  11. #include <stdio.h>
  12.  
  13.  
  14. long mypow(int base, int power);
  15. int countdigits(long number);
  16. string ccchecker(long c_card);
  17.  
  18. int main(void)
  19. {
  20.     long c_card = get_long("Number: ");
  21.     string result = ccchecker(c_card);
  22.     printf("%s\n", result);
  23. }
  24.  
  25. string ccchecker(long c_card)
  26. {
  27.     //First we use the simple rules to check if a credit card is valid or not
  28.     //First we use a function to get the total ammount of digits and we create a variable to store the card type
  29.     int t_digits = countdigits(c_card);
  30.     string c_brand;
  31.     //Part of the rules are the first digits so we use my exponiation function to get those
  32.     int firstdigits = c_card / (mypow(10, (t_digits - 2)));
  33.  
  34.     if ((t_digits == 13 || t_digits == 16) && ((firstdigits / 10) == 4))
  35.     {
  36.         c_brand = "VISA";
  37.     }
  38.     else if (t_digits == 15 && (firstdigits == 34 || firstdigits == 37))
  39.     {
  40.         c_brand = "AMEX";
  41.     }
  42.     else if (t_digits == 16 && ((firstdigits > 50 && firstdigits < 56) || firstdigits == 22))
  43.     {
  44.         c_brand = "MASTERCARD";
  45.     }
  46.     else
  47.     {
  48.         //Since the card is invalid we just return that
  49.         return "INVALID";
  50.     }
  51.  
  52.     //After that we will use the Luhn Algorithm to check if a card has a valid checksum
  53.     //First we create a variable to temporaily store the cc number
  54.     long cc_temp = c_card;
  55.     //then we create a switch that will determine if we should sum or multiply and sum taht will change every other loop
  56.     short simplesumswitch = 1;
  57.     //Variables that we will use
  58.     int digitssum = 0;
  59.     int digitssumsimple = 0;
  60.     int c_digit;
  61.  
  62.     while (t_digits > 0)
  63.     {
  64.         //now we fork depending on the switch
  65.         if (simplesumswitch == 1)
  66.         {
  67.             //Here we go from the last digit and sum every other digit
  68.             c_digit = cc_temp % 10;
  69.             digitssumsimple += c_digit;
  70.             //Here we are chopping the last digit off
  71.             cc_temp = cc_temp / 10;
  72.             simplesumswitch = 0;
  73.             t_digits--;
  74.         }
  75.         else if (simplesumswitch == 0)
  76.         {
  77.             //Now we go from second to last number and multuiply by 2 every other digit
  78.             //Then we must summ all digits of that multiplication (so if multiplication ends in two digits we sum those)
  79.             c_digit = cc_temp % 10;
  80.             int templuhnsum = c_digit * 2;
  81.             //If a number is 10 or more this will sum both digits
  82.             digitssum += (templuhnsum % 10) + (templuhnsum / 10);
  83.             cc_temp = cc_temp / 10;
  84.             simplesumswitch = 1;
  85.             t_digits--;
  86.         }
  87.     }
  88.     //after the sum we now sum both parts
  89.     int totalsumdigits = digitssum + digitssumsimple;
  90.     //If the last digit of that sum is 0 then the checksum is correct and we return the card type
  91.     if ((totalsumdigits % 10) == 0)
  92.     {
  93.         return c_brand;
  94.     }
  95.     else
  96.     {
  97.         return "INVALID";
  98.     }
  99. }
  100.  
  101.  
  102. //Simple function to count digits, basically a loop that will go around chopping a number until its zero
  103. //it counts each loop to get the digits
  104. //Based on code by Acry (the func is identical) got it on Grepper
  105. int countdigits(long number)
  106. {
  107.     int digits = 0;
  108.     while (number > 0)
  109.     {
  110.         number = number / 10;
  111.         digits++;
  112.     }
  113.     return digits;
  114. }
  115.  
  116. //Simple exponation func
  117. //Based on code by Numan from Bangladesh  got it on Grepper
  118. long mypow(int base, int power)
  119. {
  120.     long result = 1;
  121.     if (power > 0)
  122.     {
  123.         for (int i = 0; i < power; i++)
  124.         {
  125.             result = result * base;
  126.         }
  127.         return result;
  128.     }
  129.     else if (power == 0)
  130.     {
  131.         return 1;
  132.     }
  133.     //It does not deal with negative power since its a long I just set zero as the answer instead of dealing
  134.     //with data type changing and stuff I wont need
  135.     else
  136.     {
  137.         return 0;
  138.     }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment