原文地址:ISBN管理器
Introduction
对书本的ISBN信息进行管理,熟悉Loop, Function以及Vector的使用方法。
Background
In this assignment, you have to use C++ loops, functions, vectors.
Write a program that prompts the user to enter the first 9 digits of an ISBN code and displays the complete 10- digit ISBN code including all 0.
The book ISBN code
The International Standard Book Number (ISBN) is a unique numeric commercial book identifier based upon the 9-digit Standard Book Numbering (SBN) code created by Gordon Foster, Emeritus Professor of Statistics at Trinity College, Dublin for the booksellers and stationers W. H. Smith and others in 1965.
A 10-digit ISBN book number consists of 10 digits
The last digit D9 is a checksum which is calculated from the first 9 digits using the formula:
D9 = (D0 x 1 + D1 x 2 + D2 x 3 + D3 x 4 + D4 x 5 + D5 x 6 + D6 x 7 + D7 x 8 + D8 x 9) modulus 11
If the checksum is 10, the last character D9 is set to the letter X because we can not store 10 as a single digit. The modulus operation is the remainder operation.
Requirements
Test your code and compare with the posted results.
Do the following
Step 1
Use the C++ string function at(n) to get the character at position n where n is from 0 to string length - 1.
Step 2
Define a C++ function to validate the user input
bool validateInput(string isbn) {
...
return
}
This function returns true if the input is valid and returns false if it is not valid. Things that need to be checkedin this order:
- Input length must be exactly 9
- Each character must be a digit
If the first check fails, there is no need to check the second one.
This function is called from main()
Step 3
Define a C++ function to calculate the checksum. This function must a contain a loop to go through each digit of the ISBN code. The function must return a character result ('0' through '9', or an 'X'). This function must be able to return an 'X' if necessary.
char getCheckSum(string isbn) {
// Calculate the check sum char result;
// Fill in your code
...
return result;
}
This function is called from main().
Create function prototypes and put them at the top of the program before main() as mentioned in the slides of chapter 6. Put all complete function definition code AFTER main().
Step 4
Note that if a character c contains the character '5', then the ASCII code of it is 53. If you use c directly in calculation, the value 53 is used, not value 5. See http://www.asciitable.com/. To get the digit value 5 from character '5', calculate the offset of character c from character '0': int value = c - '0';
Step 5
Here is a simple main() function that you can use for a single input:
int main()
{
string isbn;
cout << "This program calculates the checksum of a 9-digit ISBN code and display the 10-digit ISBN." << endl;
cout << "Enther the 9 digits for the ISBN code: ";
cin >> isbn;
bool isValid = validateInput(isbn);
if (!isValid) {
cout << "\nThe input ISBN code is not valid.\n";
return (1);
}
char extra = getCheckSum(isbn);
cout << "\nThe 10-digit ISBN code is " << isbn + extra << endl;
}
Step 6
After your code works correctly with one single input, do the following:
- Change the main() code to use a loop to accept multiple user inputs in a single run. To stop the user input loop, user will enter an X. When users enter an invalid ISBN code, the program should also loop to let user enter another one.
- Write an additional function formatWithHyphen() that takes the 10-digit ISBN code and returns an ISBN string with 3 embedded "-" after the first digit, after the fourth digit and the ninth digit position. For example, this function will take a parameter containing"0306406152" and returns a string "0-306-40615-2"
string formatWithHyphen(string isbn);
This function returns a string and does NOT display any output. You can add the following statement in main() to call this function to display the ISBN code with the extra hyphens in addition to the original 10-digit ISBN.
cout << "\nThe hyphenated ISBN code is " << formatWithHyphen(isbn + extra) << endl;
Step 7
Add two C++ vectors, one to store the valid input ISBN code (9-character string) and the other to store the calculated checksum characters.
In the loop of main(), add the valid ISBN code and the checksum to these 2 vectors. When user hits X to exit the program, use a loop to display a summary report containing all valid input ISBN codes with their corresponding calculated checksums.
Sample output
This program calculates the checksum of a 9-digit ISBN code and display the whole 10-digit ISBN.
Enter 9 digits for the ISBN code or X to exit: 123456789
The 10-digit ISBN code is 123456789X
The hyphenated ISBN code is 1-234-56789-X
Enter 9 digits for the ISBN code or X to exit: 124563299
The 10-digit ISBN code is 124563299X
The hyphenated ISBN code is 1-245-63299-X
Enter 9 digits for the ISBN code or X to exit: 111111111
The 10-digit ISBN code is 1111111111
The hyphenated ISBN code is 1-111-11111-1
Enter 9 digits for the ISBN code or X to exit: 030640615
The 10-digit ISBN code is 0306406152
The hyphenated ISBN code is 0-306-40615-2
Enter 9 digits for the ISBN code or X to exit: 5456456
The input ISBN code is not valid.
Enter 9 digits for the ISBN code or X to exit: 34567g124
The input ISBN code is not valid.
Enter 9 digits for the ISBN code or X to exit: X
(本文出自csprojectedu.com,转载请注明出处)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。