-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasst6.cpp
More file actions
145 lines (122 loc) · 2.57 KB
/
asst6.cpp
File metadata and controls
145 lines (122 loc) · 2.57 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
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
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <cstdlib>
#include <string.h>
#include <ctype.h>
using namespace std ;
bool chk_isbn(string ISBNtoCheck);
bool chk_isbn(string ISBN)
{
cout << endl << "ISBNtoCheck: " << ISBN << endl;
bool cleanflag = false;
int pos = 0;
while (!cleanflag)
{
if (!isdigit(ISBN[pos]))
{
if (ISBN.length() >= 10 && toupper(ISBN[pos]) != 'X' || pos != ISBN.length() - 1 )
{
ISBN.erase(pos, 1) ;
pos = -1;
}
}
if (pos != ISBN.length() - 1)
{
pos++;
}
else
{
cleanflag = true;
}
}
cout << endl << "ISBNtoCheck: " << ISBN << endl;
if (ISBN.length() == 10)
{
cout << endl << "ISBN length = 10 ";
int sum = 0;
int checkDigit;
if (toupper(ISBN[9]) != 'X')
{
checkDigit = (ISBN[9] - 48);
}
else
{
checkDigit = (int) 'X';
}
cout << "checkDigit : " << checkDigit << endl;
for (int x = 0; x < ISBN.length(); x++)
{
if ( x != 9)
{
sum += ((ISBN[x] - 48 ) * (x+1));
}
cout << "sum is : " << sum << endl;
}
int calcCheckDig = (sum%11);
cout << "calcCheckDig : " << calcCheckDig << endl;
if ( calcCheckDig < 10 && calcCheckDig > 0)
{
if (calcCheckDig == checkDigit)
{
return true;
}
else
{
return false;
}
}
else
{
if (calcCheckDig == 10 && checkDigit == (int) 'X')
{
return true;
}
return false;
}
}
cout << endl << "ISBNtoCheck: " << ISBN << endl;
if (ISBN.length() == 13)
{
int sum = 0;
int checkDigit = (ISBN[12] - 48);
cout << "checkDigit : " << checkDigit << endl;
for (int x = 0; x < ISBN.length(); x++)
{
if (x != 12)
{
if (((x+1)%2) == 0)
sum += ((ISBN[x] - 48) * (3));
if (((x+1)%2) != 0)
sum += ((ISBN[x] - 48) * (1));
}
cout << "sum is : " << sum << endl;
}
cout << "sum is : " << sum << endl;
cout << "sum mod 10 : " << (sum%10) << endl;
int calcCheckDig = (10 - (sum%10));
cout << "calcCheckDig : " << calcCheckDig << endl;
cout << "checkDigit : " << checkDigit << endl;
if (calcCheckDig == checkDigit)
{
return true;
}
else
{
return false;
}
}
}
int main(int argc, char* argv[])
{
string ISBNtoCheck = (string) argv[1];
if (chk_isbn(ISBNtoCheck))
{
cout << "RESULT : VALID ISBN" << endl;
}
else
{
cout<< "RESULT : INVALID ISBN" << endl;
}
return 0;
}