Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This is program to check if credit card numbers are valid using Luhns algorithm and simple rules
- for the starting and lenght numbers of cards
- Optimized version
- */
- #include <cs50.h>
- #include <stdio.h>
- long mypow(int base, int power);
- int countdigits(long number);
- string ccchecker(long c_card);
- int main(void)
- {
- long c_card = get_long("Number: ");
- string result = ccchecker(c_card);
- printf("%s\n", result);
- }
- string ccchecker(long c_card)
- {
- //First we use the simple rules to check if a credit card is valid or not
- //First we use a function to get the total ammount of digits and we create a variable to store the card type
- int t_digits = countdigits(c_card);
- string c_brand;
- //Part of the rules are the first digits so we use my exponiation function to get those
- int firstdigits = c_card / (mypow(10, (t_digits - 2)));
- if ((t_digits == 13 || t_digits == 16) && ((firstdigits / 10) == 4))
- {
- c_brand = "VISA";
- }
- else if (t_digits == 15 && (firstdigits == 34 || firstdigits == 37))
- {
- c_brand = "AMEX";
- }
- else if (t_digits == 16 && ((firstdigits > 50 && firstdigits < 56) || firstdigits == 22))
- {
- c_brand = "MASTERCARD";
- }
- else
- {
- //Since the card is invalid we just return that
- return "INVALID";
- }
- //After that we will use the Luhn Algorithm to check if a card has a valid checksum
- //First we create a variable to temporaily store the cc number
- long cc_temp = c_card;
- //then we create a switch that will determine if we should sum or multiply and sum taht will change every other loop
- short simplesumswitch = 1;
- //Variables that we will use
- int digitssum = 0;
- int digitssumsimple = 0;
- int c_digit;
- while (t_digits > 0)
- {
- //now we fork depending on the switch
- if (simplesumswitch == 1)
- {
- //Here we go from the last digit and sum every other digit
- c_digit = cc_temp % 10;
- digitssumsimple += c_digit;
- //Here we are chopping the last digit off
- cc_temp = cc_temp / 10;
- simplesumswitch = 0;
- t_digits--;
- }
- else if (simplesumswitch == 0)
- {
- //Now we go from second to last number and multuiply by 2 every other digit
- //Then we must summ all digits of that multiplication (so if multiplication ends in two digits we sum those)
- c_digit = cc_temp % 10;
- int templuhnsum = c_digit * 2;
- //If a number is 10 or more this will sum both digits
- digitssum += (templuhnsum % 10) + (templuhnsum / 10);
- cc_temp = cc_temp / 10;
- simplesumswitch = 1;
- t_digits--;
- }
- }
- //after the sum we now sum both parts
- int totalsumdigits = digitssum + digitssumsimple;
- //If the last digit of that sum is 0 then the checksum is correct and we return the card type
- if ((totalsumdigits % 10) == 0)
- {
- return c_brand;
- }
- else
- {
- return "INVALID";
- }
- }
- //Simple function to count digits, basically a loop that will go around chopping a number until its zero
- //it counts each loop to get the digits
- //Based on code by Acry (the func is identical) got it on Grepper
- int countdigits(long number)
- {
- int digits = 0;
- while (number > 0)
- {
- number = number / 10;
- digits++;
- }
- return digits;
- }
- //Simple exponation func
- //Based on code by Numan from Bangladesh got it on Grepper
- long mypow(int base, int power)
- {
- long result = 1;
- if (power > 0)
- {
- for (int i = 0; i < power; i++)
- {
- result = result * base;
- }
- return result;
- }
- else if (power == 0)
- {
- return 1;
- }
- //It does not deal with negative power since its a long I just set zero as the answer instead of dealing
- //with data type changing and stuff I wont need
- else
- {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment