-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuhn.cpp
More file actions
103 lines (87 loc) · 2.09 KB
/
Luhn.cpp
File metadata and controls
103 lines (87 loc) · 2.09 KB
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
#include <iostream>
using namespace std;
int Luhn(char *BrKart, int n)
{
int temp=0;
int sum=0;
bool alt = true;
for(int i=n-1; i>=0; i--)
{
temp=BrKart[i]-'0';
if(alt)
{
temp*=2;
if(temp>9)
{
temp-=9;
}
}
sum+=temp;
alt=!alt;
}
sum%=10;
if(sum!=0)
sum=10-sum;
return sum;
}
int LuhnValid(char *BrKart, int n)
{
int temp=0;
int sum=0;
bool alt = false;
for(int i=n-1; i>=0; i--)
{
temp=BrKart[i]-'0';
if(alt)
{
temp*=2;
if(temp>9)
{
temp-=9;
}
}
sum+=temp;
alt=!alt;
}
return (sum%10==0);
}
int main()
{
int N, x;
cout << "==================================" << endl;
cout << "1. Racunanje kontrolnog broja" << endl;
cout << "2. Provjera valjanosti kartice" << endl;
cout << "0. Izlaz" << endl;
cout << "==================================" << endl;
cin >> x;
cout << endl;
if(x == 1)
{
cout << "Broj znamenaka: ";
cin >> N;
int M=N+1;
char BrKart[M];
cout << "Broj kartice: ";
cin.ignore();
cin.getline(BrKart, M);
int contBr = Luhn(BrKart, N);
cout << "Kontrolni broj kartice: " << contBr << endl;
cout << "Potpuni broj kartice:" << BrKart << contBr << endl << endl;
}
if(x == 2)
{
cout << "Broj znamenaka: ";
cin >> N;
int M=N+1;
char BrKart[M];
cout << "Broj kartice: ";
cin.ignore();
cin.getline(BrKart, M);
if(LuhnValid(BrKart, N) == 1)
cout << "Kartica " << BrKart << " je vazeca!" << endl << endl;
else
cout << "Kartica " << BrKart << " je nevazeca!" << endl << endl;
}
system("pause");
return 0;
}