mirror of https://github.com/me50/kukemuna.git
This commit is contained in:
commit
a7997c4636
|
|
@ -0,0 +1,133 @@
|
||||||
|
#include <cs50.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Prototypes
|
||||||
|
long long get_card_number(void);
|
||||||
|
void find_issuer(long long card_number);
|
||||||
|
int validate(long long card_number);
|
||||||
|
int get_id(long long card_number);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
long long card_number = get_card_number();
|
||||||
|
|
||||||
|
if (validate(card_number))
|
||||||
|
{
|
||||||
|
find_issuer(card_number);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
long long get_card_number()
|
||||||
|
{
|
||||||
|
long long number;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
number = get_long("Insert card number: ");
|
||||||
|
}
|
||||||
|
while (number < 0);
|
||||||
|
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
|
||||||
|
void find_issuer(long long card_number)
|
||||||
|
{
|
||||||
|
int id = get_id(card_number);
|
||||||
|
|
||||||
|
// Number beginning with 34 or 37
|
||||||
|
if (id / 100 == 34 || id / 100 == 37)
|
||||||
|
{
|
||||||
|
printf("AMEX\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Number beginning with 4
|
||||||
|
if (id / 1000 == 4)
|
||||||
|
{
|
||||||
|
printf("VISA\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Number beginning with 51-55 or 2221-2720
|
||||||
|
if ((id / 100 >= 51 && id / 100 <= 55) || (id >= 2221 && id <= 2720))
|
||||||
|
{
|
||||||
|
printf("MASTERCARD\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("INVALID\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int validate(long long card_number)
|
||||||
|
{
|
||||||
|
// Add together individual digits while multiplying every other digit from one after least significant before addition
|
||||||
|
// If multiplication product is geater than 9 then add individual digits together. 3456 => 6 + (1 + 0) + 4 + 6
|
||||||
|
int sum = 0, multiply = 0;
|
||||||
|
|
||||||
|
while (card_number > 0)
|
||||||
|
{
|
||||||
|
if (multiply)
|
||||||
|
{
|
||||||
|
if (card_number % 10 * 2 > 9) // Is multiplication product greater than 9?
|
||||||
|
{
|
||||||
|
int n = card_number % 10 * 2;
|
||||||
|
while (n > 0) // If so, add individual digits to the sum
|
||||||
|
{
|
||||||
|
sum += n % 10;
|
||||||
|
n /= 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sum += card_number % 10 * 2;
|
||||||
|
}
|
||||||
|
card_number /= 10;
|
||||||
|
multiply = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sum += card_number % 10;
|
||||||
|
card_number /= 10;
|
||||||
|
multiply = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sum % 10 != 0)
|
||||||
|
{
|
||||||
|
printf("INVALID\n");
|
||||||
|
return 0; // Invalid number
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 1; // Valid number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_id(long long card_number)
|
||||||
|
{
|
||||||
|
int count = 0, id;
|
||||||
|
long long n, trunc = 1;
|
||||||
|
|
||||||
|
n = card_number;
|
||||||
|
|
||||||
|
while (n > 0)
|
||||||
|
{
|
||||||
|
n /= 10;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count < 13 || count > 16)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < count - 4; i++) // Get four most significant digits
|
||||||
|
{
|
||||||
|
trunc *= 10;
|
||||||
|
}
|
||||||
|
id = card_number / trunc;
|
||||||
|
return id;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue