195、编写一个程序,读取手机的国际移动设备识别码(IMEI,15位数字),并检查其是否为正品。这里的检查依据是通过Luhn算法,即先对前14位数字进行处理,奇数位数字直接相加,偶数位数字先乘以2,若结果为两位数则将两个数位上的数字相加,然后将所有处理后的数字求和,得到的和对10取余,若余数不为0则用10减去余数得到校验数字,最后将该校验数字与IMEI的第15位数字进行比较,若相等则IMEI有效,否则无效。
#include <stdio.h>
int main(void) {
char chk_dig;
int i, ch, sum, temp;
sum = 0;
printf("Enter IMEI (15 digits): ");
for(i = 1; i < 15; i++) /* Read the first 14 IMEI's digits.*/
{
ch = getchar();
if((i & 1) == 1) /* Check if the digit's position is odd. */
sum += ch - '0'; /* To find the numeric value of that digit, the ASCII value of 0 is subtracted. */
else
{
temp = 2 * (ch - '0');
if(temp >= 10)
temp = (temp / 10) + (temp % 10); /* If the digit's doubling produces a two-digit number we calculate the sum of these digits. */
sum += temp;
}
}
ch = getchar(); /* Read the IMEI's last digit, that is, the Luhn digit. */
ch = ch - '0';
chk_dig = sum % 10;
if(chk_dig != 0)
chk_dig = 10 - chk_dig;
if(ch == chk_dig)
printf("*** Valid IMEI ***\n");
else
printf("*** Invalid IMEI ***\n");
return 0;
}
196、以下程序的输出是什么?#include int main(void) { char str[] = “Right‘0’Wrong”; printf(“%s\n”, str); return 0; }
程序将输出:Right‘0’Wrong
197、如果我们编写了以下代码:char str[] = “Right’\0’Wrong”; 输出会是什么?
Right’
198、以下程序将两个字符串存储在两个数组中,交换它们并显示新内容。是否有错误?#include int main(void) { char temp[100], str1[100] = “Let see”, str2[100] = “Is everything OK?”; temp = str1; str1 = str2; str2 = temp; printf(“%s %s”, str1, str2); return 0; }
- 有错误。数组名作为指针时是常量指针,不允许改变其值并指向其他地址,因此
temp = str1;、str1 = str2;、str2 = temp;这些语句是非法的,程序无法编译。
199、以下程序的输出是什么?#include int main(void) { char str1[] = “test”, str2[] = “test”; (str1 == str2) ? printf(“One\n”) : printf(“Two\n”); return 0; }
Two
IMEI校验与字符串处理

最低0.47元/天 解锁文章
57

被折叠的 条评论
为什么被折叠?



