• 35648

    文章

  • 23

    评论

  • 20

    友链

  • 最近新加了很多技术文章,大家多来逛逛吧~~~~
  • 喜欢这个网站的朋友可以加一下QQ群,我们一起交流技术。

ISBN管理器

欢迎来到阿八个人博客网站。本 阿八个人博客 网站提供最新的站长新闻,各种互联网资讯。 喜欢本站的朋友可以收藏本站,或者加QQ:我们大家一起来交流技术! URL链接:https://www.abboke.com/jsh/2019/0816/106425.html 1190000020088321
原文地址:ISBN管理器

Introduction

对书本的ISBN信息进行管理,熟悉Loop, Function以及Vector的使用方法。

ISBN

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,转载请注明出处)

相关文章

暂住......别动,不想说点什么吗?
  • 全部评论(0
    还没有评论,快来抢沙发吧!