|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
3.修改P39的程序upc.c,使用户可以一次输入11位数字,而不用先录入1位,再录入5位,最后再录入5位。
Enter the first 11 digits of a UPC: 05150024128
Check digit: 8
题目要求时这样的,可我运行的结果都是9,找不出错误
#include<stdio.h>
int main(void)
{
int a,s,d,f,g,h,j,k,l,z,x,c,v,b;
printf("Enter the first 11 digits of a UPC:");
scanf("%.1d%.1d%.1d%.1d%.1d%.1d%.1d%.1d%.1d%.1d%.1d%",&a,&s,&d,&f,&g,&h,&j,&k,&l,&z,&x);
v=a+d+g+j+l+x;
b=s+f+h+k+z;
printf("check digit:%d",9-((3*v+b-1)%10));
return 0;
}
输入1位整数的格式描述符是 "%1d" 不是 "%.1d"
- #include<stdio.h>
- int main(void)
- {
- int d[11] , i , k , s , s1 , s2 ;
- printf("Enter the 11 digits of a UPC : ") ;
- for(k = 0 ; k < 11 ; k ++) scanf("%1d" , & d[k]) ;
- for(s1 = 0 , i = 0 ; i < 11 ; i += 2) s1 += d[i] ;
- for(s2 = 0 , i = 1 ; i < 11 ; i += 2) s2 += d[i] ;
- s = s1 * 3 + s2 ;
- printf("Check digit: %d\n" , 9 - ((s - 1) % 10)) ;
- }
复制代码
编译、运行实况:
- D:\00.Excise\C>g++ -o x x.c
- D:\00.Excise\C>x
- Enter the 11 digits of a UPC : 01380015173
- Check digit: 5
- D:\00.Excise\C>x
- Enter the 11 digits of a UPC : 05150024128
- Check digit: 8
- D:\00.Excise\C>
复制代码
|
-
|