














Copyright © 2004
by Polaris Computing.
All rights reserved.
| |
| |
C/C++ Code for ISBN
Conversion
The following program converts a 10-character
ISBN to a 13-digit
ISBN with the "978" prefix, and vise versa. The
function ISBN1013 can be used to convert your currently existing ISBN's into
the new format.
You can use Borland C++ Builder to compile
the following code into a console application. Minor modifications might be needed if you use other compilers. The main
function simply passes an argument to one of the conversion functions,
ISBN1310 and ISBN1013, and conversions are all done by these functions.
|
Language |
C or C++ |
|
Compiler |
Borland C++ Builder |
|
Disclaimer: All sample
code contained herein is provide to you "AS IS." ALL IMPLIED
WARRANTIES, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF THE
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE EXPRESSLY
DISCLAIMED. |
// This
program gets an argument from the command line.
// If the argument is a 10-character SIBN, the program will
// print the corresponding 13-gigit ISBN. If the argument
// is a 13-digit ISBN, the program will print the
// corresponding 10-character ISBN.
#pragma hdrstop
#include <condefs.h>
#include <string.h>
#include <stdio.h>
#pragma argsused
//-------------------- Convert ISBN-13 to ISBN-10 -----------------------
void ISBN1310(char *ISBN, char ISBN2[]) {
char s9[10], s1[2];
int i, n;
bool ErrorOccurred;
ErrorOccurred = false;
strcpy(s9, ISBN + 3);
s9[9] = 0;
n = 0;
for (i=0; i<9; i++) {
if (!ErrorOccurred) {
if (s9[i]>='0' && s9[i]<='9') n = n + (10 - i) *
(s9[i] - '0');
else ErrorOccurred = true;
}
}
if (ErrorOccurred) strcpy(ISBN2, "ERROR");
else {
n = 11 - (n%11);
if (n==11) s1[0] = '0';
else if (n==10) s1[0] = 'X';
else s1[0] = n + '0';
s1[1] = 0;
strcpy(ISBN2, s9);
strcat(ISBN2, s1);
}
}
//-------------------- Convert ISBN-10 to ISBN-13 -----------------------
void ISBN1013(char *ISBN, char ISBN2[]) {
char s12[13], s1[2];
int i, n;
bool ErrorOccurred;
ErrorOccurred = false;
strcpy(s12, "978");
strcat(s12, ISBN);
s12[12] = 0;
n = 0;
for (i=0; i<12; i++) {
if (!ErrorOccurred) {
if (s12[i]>='0' && s12[i]<='9') {
if (i%2==0) n = n + s12[i] - '0';
else n = n + 3*(s12[i] - '0');
}
}
}
if (ErrorOccurred) strcpy(ISBN2, "ERROR");
else {
n = n%10;
if (n!=0) n = 10 - n;
s1[0] = n + '0';
s1[1] = 0;
strcpy(ISBN2, s12);
strcat(ISBN2, s1);
}
}
//--------------------------------- Main --------------------------------
void main(int argc, char **argv) {
char arg1[32], ISBN[14];
if (argc==2) {
strcpy(arg1, argv[1]);
if (strlen(arg1)==10) ISBN1013(arg1, ISBN);
else if (strlen(arg1)==13) ISBN1310(arg1, ISBN);
else strcpy(ISBN, "Invalid ISBN");
printf("%s", ISBN);
}
else printf("Usege: isbnconv ISBN\n");
}
|
|
|