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
|
#ifndef ISBN_H
#define ISBN_H
#include <iostream>
#include <cstring>
using namespace std;
class ISBN {
private:
char group[6]; // group code is 1-5 digits
char publisher[8]; // publisher code is 1-7 digits
char book[8]; // book code is 1-7 digits
char check;
char compute_check(); // compute the check character
public:
ISBN();
ISBN(char set[ ]); // e.g., ISBN book("0-670-82162-4");
ISBN(char setA[ ], char setB[ ], char setC[ ], char setD); // 4-argument constructor is passed
// group code, publisher code, book
// code, and check character. e.g.
// ISBN book("0","670","82162","4");
ISBN(char setE[ ], char setF[ ], char setG[ ]); // 3-argument constructor is passed
// group code, publisher code, and
// book code. It computes the
// check character
// e.g. ISBN book("0","670","82162");
bool valid(); // true if check character is correct
char *getpublisher(); // return publisher code
void print(ostream &o); // print string with hyphens
};
#endif
//FUNCTIONS
ISBN::ISBN() {}
ISBN::ISBN(char set[ ]) //full string of ISBN
{
char deliminator[] = "-";
char check2[2];
strtok(set, deliminator);
strcpy(group, set);
strtok(NULL, deliminator);
strcpy(publisher, NULL);
strtok(NULL, deliminator);
strcpy(book, NULL);
strtok(NULL, deliminator);
strcpy(check2, NULL);
check = check2[0];
}
ISBN::ISBN(char setA[ ], char setB[ ], char setC[ ], char setD)
{
strcpy(group, setA);
strcpy(publisher, setB);
strcpy(book, setC);
check = setD;
}
ISBN::ISBN(char setA[ ], char setB[ ], char setC[ ]) //this will compute the check character
{
strcpy(group, setA);
strcpy(publisher, setB);
strcpy(book, setC);
check = compute_check();
int i = 0; //setA
int k = 0; //setA
int d = 0; //setB
int p = 0; //setB
int x = 0; //setC
int y = 0; //setC
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
int fsum = 0;
for(k=0; k<i; k++)
for(setA[i]=0; i>0; i++)
{
sum1=(setA[i]*k)+sum1;
}
for(p=0; p<d; p++)
for(setB[d]=0; d>0; d++)
{
sum2=(setB[d]*p)+sum2;
}
for(y=0; x<y; y++)
for(setC[x]=0; x>0; x++)
{
sum3=(setC[x]*y)+sum3;
}
fsum = (sum1+sum2+sum3)/11;
return fsum = 9(sum1+sum2+sum3)/11);
}
bool ISBN::valid()
{
char tCheck = compute_check();
return(tCheck = check);
}
char ISBN:: *getpublisher()
{
return publisher;
}
void ISBN::print(ostream &o)
{
cout << group << "-" << publisher << "-" << book << "-" << check << endl;
}
| |