鱼C论坛

 找回密码
 立即注册
查看: 1180|回复: 4

[已解决]学校的作业,找了好久找不出错误

[复制链接]
发表于 2020-11-7 10:39:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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;       
}
最佳答案
2020-11-7 11:15:40
输入1位整数的格式描述符是 "%1d"  不是 "%.1d"
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4.         int d[11] , i , k , s , s1 , s2                  ;
  5.         printf("Enter the 11 digits of a UPC : ")        ;
  6.         for(k = 0 ; k < 11 ; k ++) scanf("%1d" , & d[k]) ;
  7.         for(s1 = 0 , i = 0 ; i < 11 ; i += 2) s1 += d[i] ;
  8.         for(s2 = 0 , i = 1 ; i < 11 ; i += 2) s2 += d[i] ;
  9.         s = s1 * 3 + s2                                  ;
  10.         printf("Check digit: %d\n" , 9 - ((s - 1) % 10)) ;
  11. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. Enter the 11 digits of a UPC : 01380015173
  4. Check digit: 5

  5. D:\00.Excise\C>x
  6. Enter the 11 digits of a UPC : 05150024128
  7. Check digit: 8

  8. D:\00.Excise\C>
复制代码
微信图片_20201107103745.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-7 10:50:35 | 显示全部楼层

  1. scanf("%.1d%.1d%.1d%.1d%.1d%.1d%.1d%.1d%.1d%.1d%.1d%",&a,&s,&d,&f,&g,&h,&j,&k,&l,&z,&x);
复制代码

改成

  1. scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%",&a,&s,&d,&f,&g,&h,&j,&k,&l,&z,&x);
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-7 11:15:40 | 显示全部楼层    本楼为最佳答案   
输入1位整数的格式描述符是 "%1d"  不是 "%.1d"
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4.         int d[11] , i , k , s , s1 , s2                  ;
  5.         printf("Enter the 11 digits of a UPC : ")        ;
  6.         for(k = 0 ; k < 11 ; k ++) scanf("%1d" , & d[k]) ;
  7.         for(s1 = 0 , i = 0 ; i < 11 ; i += 2) s1 += d[i] ;
  8.         for(s2 = 0 , i = 1 ; i < 11 ; i += 2) s2 += d[i] ;
  9.         s = s1 * 3 + s2                                  ;
  10.         printf("Check digit: %d\n" , 9 - ((s - 1) % 10)) ;
  11. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. Enter the 11 digits of a UPC : 01380015173
  4. Check digit: 5

  5. D:\00.Excise\C>x
  6. Enter the 11 digits of a UPC : 05150024128
  7. Check digit: 8

  8. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-11-7 23:14:36 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-7 23:16:26 | 显示全部楼层
jackz007 发表于 2020-11-7 11:15
输入1位整数的格式描述符是 "%1d"  不是 "%.1d"

        编译、运行实况:

谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 01:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表