-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit.c
111 lines (101 loc) · 2.54 KB
/
credit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "digit.h"
#include <stdio.h>
#define MAX_LENGTH 16
#define MIN_LENGTH 13
#define AMEX_LENGTH 15
#define MASTERCARD_LENGTH 16
#define VISA_LENGTH1 13
#define VISA_LENGTH2 16
typedef enum
{
true = 1, false = 0
}
bool;
bool luhn_algorithm(long number, int count);
int main(void)
{
const long n = get_long("Credit card number: ");
int count = digit_count(n);
if (count == MIN_LENGTH || count == AMEX_LENGTH || count == MAX_LENGTH)
{
if (luhn_algorithm(n, count))
{
int p, p1, p2;
switch (count)
{
case AMEX_LENGTH:
p = prefix(n, 2);
if (p == 34 || p == 37)
{
printf("AMERICAN EXPRESS\n");
}
else
{
printf("INVALID\n");
}
break;
case VISA_LENGTH1:
p = prefix(n, 1);
if (p == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
break;
case MAX_LENGTH:
p1 = prefix(n, 1);
p2 = prefix(n, 2);
if (p1 == 4)
{
printf("VISA\n");
}
else if (p1 == 5 && (p2 > 50 && p2 < 56))
{
printf("MASTERCARD\n");
}
else
{
printf("INVALID\n");
}
break;
}
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
}
bool luhn_algorithm(long n, int count)
{
int odd_sum = 0;
for (int i = 1; i <= count; i += 2)
{
odd_sum += digit_extractor(n, i);
}
int even_sum = 0;
for (int j = 2; j <= count; j += 2)
{
int even_mul_digit = digit_extractor(n, j) * 2;
if (even_mul_digit > 9)
{
for (int k = 1; k <= digit_count(even_mul_digit); k++)
{
even_sum += digit_extractor(even_mul_digit, k);
}
}
else
{
even_sum += even_mul_digit;
}
}
int checksum = even_sum + odd_sum;
return (checksum % 10 != 0) ? false : true;
}