c++计算总是打印出11

C++ Calculation always printing out 11

本文关键字:打印 计算 c++      更新时间:2023-10-16

我目前正在编写一个程序,根据用户输入的ISBN的一部分的数字为用户提供校验和。我对13位ISBN的计算得到了正确的答案,但出于某种原因,对10位ISBN的计算总是得到11,无论我输入什么。

cout << "Enter the digits one at a time, as if they were to be read from right to left." << endl;
cin >> digit_one;
cin >> digit_two;
cin >> digit_three;
cin >> digit_four;
cin >> digit_five;
cin >> digit_six;
cin >> digit_seven;
cin >> digit_eight;
cin >> digit_nine;
checksum = (11 - (((10, 9, 8, 7, 6, 5, 4, 3, 2) * (digit_one, digit_two, digit_three, digit_four, digit_five, digit_six, digit_seven, digit_eight, digit_nine)) % 11));
cout << "The checksum for this ISBN is " << checksum << endl;

我是否错过了一些简单的东西?提前感谢您的帮助

您似乎误解了操作符,的工作原理。(2, 3)生成3,而不是Python中的元组。

同样,(2,3,4) * (1,2,3)4 * 3几乎相等。

在你的代码中

checksum = (11 - (((10, 9, 8, 7, 6, 5, 4, 3, 2) * (digit_one, digit_two, digit_three, digit_four, digit_five, digit_six, digit_seven, digit_eight, digit_nine)) % 11))

没有区别
checksum = (11 - ((2 * digit_nine) % 11)) 

那么可以安全地假设您的digit_nine0¹,这就是为什么最终checksum的值为11 - 0

¹或11的乘法,而不是数字