gpt4 book ai didi

检查 ISBN 以查看其在 C 语言中是否有效

转载 作者:行者123 更新时间:2023-11-30 15:38:14 39 4
gpt4 key购买 nike

我正在尝试检查 ISBN 在 C 中是否有效。我想忽略任何非整数的数组值,例如忽略 - 或空格。我收到以下代码部分的错误:

if(s[jj] == '-' || size[jj] == ' ') 

错误信息如下:

error: subscripted value is neither array nor pointer

int checkISBN( char s[] ) {
int result = 0;
int theSize = 18; /* Most ISBNs are 10 digits long, with some being
* 13. I just chose 18 incase there are a lot of
* blank spaces or dashes in the array. */
int n = 1; /* This comes in handy for determing if the check
character is valid. */
int sum = 0; /* We divide this value by 11 to determine if the
check character is valid */
int jj;

for (jj = 0; jj sum += s[i] * n; ++n;
/* For the below, the formula works like this:
* The sum for the ISBN 0-8065-0959-7 is

1*0 + 2*8 + 3*0 + 4*6 + 5*5 + 6*0 + 7*9 + 8*5 + 9*9 = 249

* The remainder when 249 is divided by 11 is 7, the last
* character in the ISBN. The check character is used to
* validate an ISBN.
* */
...
}

...

int checkCharacter = sum / 11; 

if (checkCharacter == s[size-1]) {
result = 1;
/* I know this part is wrong.
* If the array only contains 10 values, then s[17] will be null.
* I'll fix this later.
* */
return result;;
/* Fix this to return a calculated value based on string */
}

最佳答案

简单的拼写错误,size 不是数组。应该是:

if(s[jj] == '-' || s[jj] == ' ')
^^^

关于检查 ISBN 以查看其在 C 语言中是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21869961/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com