-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit.c
160 lines (120 loc) · 2.89 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
149
150
151
152
153
154
155
156
157
158
159
160
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int SUM = 0;
int get_numbers(long number, int digit);
int sum(int length, int multiply);
int confirm(int start[], int digit);
int main(void)
{
long creditid = get_long("Number: ");
int ndigit = log10(creditid) + 1;
get_numbers(creditid, ndigit);
}
int get_numbers(long number, int digit)
{
int start[2];
int m = 1;
int s = 0;
if (digit <= 13)
{
m = 0;
s = 1;
}
for (int i = m; i <= digit; i += 2)
{
// get starts number if digit = 13
if (digit == 13)
{
if (i == 10)
{
start[0] = number / (long) pow(10, i + 2) % 10;
start[1] = 0;
continue;
}
}
// get starts number if digit = 16
else if (digit == 16 && i == digit - 1)
{
start[0] = number / (long) pow(10, i) % 10;
start[1] = number / (long) pow(10, i - 1) % 10;
}
// get starts number i digit = 15
else if (digit == 15 && i == digit)
{
start[0] = number / (long) pow(10, i - 1) % 10;
// printf("---- %i \n",start[0]);
start[1] = number / (long) pow(10, i - 2) % 10;
}
// sum numbers multiply onle
sum(digit, (number / (long) pow(10, i) % 10) * 2);
}
// get other numbers
for (int i = s; i <= digit; i += 2)
{
sum(digit, number / (long) pow(10, i) % 10);
}
// get result
confirm(start, digit);
return 0;
}
int sum(int length, int multiply)
{
// Verifying the sent number is the result of the multiplication process
if (multiply > 9)
{
for (int i = 0; i <= 1; i++)
{
if (i == 0)
{
// Converting a number to a number and then adding the values
SUM += multiply % 10;
}
else
{
SUM += multiply / 10 % 10;
}
}
}
// If the number is the result of another process
else
{
SUM += multiply;
}
return 0;
}
int confirm(int start[], int digit)
{
/*
Confirmation stage
of the validity of the
card number.
*/
// Assign the sent values o the variable
int start_with = start[0] * 10 + start[1];
// heck the result of the addition from multiples of 10
if (SUM % 10 == 0)
{
if (start[0] == 4)
{
printf("VISA\n");
}
else if (start_with >= 51 && start_with < 56)
{
printf("MASTERCARD\n");
}
else if (start_with == 34 || start_with == 37)
{
printf("AMEX\n");
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
return 0;
}