【发布时间】:2018-04-23 10:09:14
【问题描述】:
我在章节中遗漏了一些东西;我已经阅读了它们的正面和背面,但我认为我需要一些一般性的指导。
不允许使用loops,我已经阅读了JAVA 和Python 示例。
我应该修改我的第一个(顶部)代码以使用 getline 的字符串输入,然后计算 ISBN-10 的最后一位。
输入013601267,我不确定为什么我的输出是5,在我修改后的代码中的第10位校验和之后。该值应为1。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout
<< "Enter the first nine digits as integerss of an ISBN.."
<<endl;
int d1, d2, d3, d4, d5, d6, d7, d8, d9;
int d10;
cin
>> d1
>> d2
>> d3
>> d4
>> d5
>> d6
>> d7
>> d8
>> d9;
d10 = ( d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9 ) % 11;
if ( d10 > 9)
{
cout
<< "d10: "
<< d10
<< endl
<<"The corresponding ISBN-10 is... "
<< d1
<< d2
<< d3
<< d4
<< d5
<< d6
<< d7
<< d8
<< d9
<< 'X'
<< endl;
}
else
{
cout
<< "d10: "
<< d10
<< endl
<<"The corresponding ISBN-10 is... "
<< d1
<< d2
<< d3
<< d4
<< d5
<< d6
<< d7
<< d8
<< d9
<< d10
<<endl;
}
return 0;
}
下面是修改后的代码,如果我成功了,我会将 ISBN 连接到 d10,但我将它们分开,因为我试图查看数学索引元素的值是什么。
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
cout
<< "Enter the first nine digits of an ISBN-10..."
<< endl;
string ISBN;
getline(cin, ISBN, '\n');
int d10 = ( ISBN[0] * 1 + ISBN[1] * 2 + ISBN[2] * 3 + ISBN[3] * 4 + ISBN[4] * 5 + ISBN[5] * 6 + ISBN[6] * 7 + ISBN[7] * 8 + ISBN[8] * 9 ) % 11;
cout
<< d10
<< endl
<< ISBN
<< endl;
return 0;
}
【问题讨论】: