forked from chinhungtseng/cs50x2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit.c
148 lines (120 loc) · 3.3 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* CS50x 2021
* Problem Set 1: credit.c
*/
#include <stdio.h>
#include <cs50.h>
#include <stdbool.h>
int main(void)
{
long card_number;
void credit_card_validation(long card_number);
// Prompt user for credit card number.
card_number = get_long("Number: ");
// Credit card number validation.
credit_card_validation(card_number);
return 0;
}
// Get digits of card_number.
int number_of_digits(long n)
{
int len = 0;
// Convert negative value to positive value.
if (n < 0)
n = -n;
while (n > 0) {
n /= 10;
len++;
}
return len;
}
/**
* Credit card number validation.
*
* Credit card rules.
* |------------|--------|------------|
* | TYPE | DIGITS | START WITH |
* |------------|--------|------------|
* | AMEX | 15 | 34, 37 |
* | MASTERCARD | 16 | 51-55 |
* | VISA | 13, 16 | 4 |
* |------------|--------|------------|
*/
void credit_card_validation(long card_number)
{
int ndigits;
bool isValidAlgor;
int number_of_digits(long n);
bool luhn_algorithm(long number);
bool is_in(int target, int *array, int n);
ndigits = number_of_digits(card_number);
isValidAlgor = luhn_algorithm(card_number);
if (ndigits == 15 && isValidAlgor) {
int start_with[2] = {34, 37};
int first_two_digits = (int) (card_number / (long) 1e13);
if (is_in(first_two_digits, start_with, 2))
printf("AMEX\n");
else
printf("INVALID\n");
} else if (ndigits == 16 && isValidAlgor) {
int start_with[5] = {51, 52, 53, 54, 55};
int first_two_digits = (int) (card_number / (long) 1e14);
if (is_in(first_two_digits, start_with, 5))
printf("MASTERCARD\n");
else if ((first_two_digits / 10) == 4)
printf("VISA\n");
else
printf("INVALID\n");
} else if (ndigits == 13 && isValidAlgor) {
int start_with = 4;
int first_digits = (int) (card_number / (long) 1e12);
if (first_digits == start_with)
printf("VISA\n");
else
printf("INVALID\n");
} else
printf("INVALID\n");
}
/**
* Luhn’s Algorithm
*
* 1) Multiply every other digit by 2, starting with the number’s
* second-to-last digit, and then add those products’ digits together.
*
* 2) Add the sum to the sum of the digits that weren’t multiplied by 2.
*
* 3) If the total’s last digit is 0 (or, put more formally,
* if the total modulo 10 is congruent to 0), the number is valid!
*/
bool luhn_algorithm(long number)
{
int digit, ndigits;
long results = 0;
bool isValid = true;
int number_of_digits(long n);
ndigits = number_of_digits(number);
for (int i = 1; i <= ndigits; i++) {
digit = number % 10;
number /= 10;
if (!(i % 2)) {
digit *= 2;
if (number_of_digits(digit) > 1) {
digit = (digit % 10) + (digit / 10);
}
}
results += digit;
}
if (results % 10)
isValid = false;
return isValid;
}
bool is_in(int target, int *array, int n)
{
int *end = (array + n - 1);
while (array <= end) {
if ( *array == target)
return true;
array++;
}
return false;
}