作者:zean专家分:0 会员信息 发短消息 所属BLOG 发表时间:2006-11-27 17:40:00 [回复] [修改] [锁帖] [结帖] [醒目] 高质高效舒适地开发:使用Visual Unit 北京睿智汇海ASP.NET培训专家,2个月实现高薪梦想 楼主 1 main()2 {3 int a,b;4 printf("enter 2 No.:");5 scanf ("%d %d",&a,&b);6 printf(" %3d+%3d=%d\n", a,b, a+b);7 printf(" %3d-%3d=%d\n", a,b, a-b);8 printf(" %3d*%3d=%d\n", a,b, a*b);9 printf(" %3d/%3d=%d\n", a,b, a/b);0 printf(" %3d/%3d=%d -->residue\n", a,b, a%b); }我自己试着把3 去掉,或者换成4, 但是我感觉没有变化啊,输出的结果一样啊!我自己的理解是象 %2.1d 的用法一样,算小数点一共3个小数后1位,但是这里的意思是保留前3位么、?但是我输入,2个数字 1234 2344 其结果多于3位啊。这里我请教老师们一下,谢谢帮助!问题2 对比一下第9 于第10 谁能告诉我一下,求余数,是那个命令完成的呢?9 于0 都是%3d/%3d=%d啊。。。。 作者:咖喱野人专家分:130 会员信息 发短消息 所属BLOG 发表时间:2006-11-27 18:11:00 [回复] [引用] 1 楼 %3d的意思是按右对齐输出,且共占3位,比如你输入123,结果输出的还是123,这里%后的3是数据宽度,但是如果你输入的数据超过3位时,是得不到正确值的,比如你输入1234,结果输出是123%2.1的意思是按右对齐形式输出,且指定输出宽度为2位,小数点后输出1位数求余用的是算术运算符%,像第10 printf(" %3d/%3d=%d -->residue\n", a,b, a%b);应该改成 printf(" %d%%d=%d -->residue\n", a,b, a%b); 此帖被评20分 作者:zean专家分:0 会员信息 发短消息 所属BLOG 发表时间:2006-11-27 19:09:00 [修改] [回复] [引用] 2 楼 十分感谢,我的想法和你一样,但是当我执行了那个程式后,没有遇到问题,而且,我写的是3 但是输入4位,8位的数字,结果没有收到影响,这个例子是书上给的,按照他的程式答案是:enter 2 No.:7 4 7+ 4=11 7- 4=3 7* 4=28 7/ 4=1 7/ 4=3 -->residueExecution time 3.571 secondsPress any key to continue...按照你的是enter 2 No.:7 4 7+ 4=11 7- 4=3 7* 4=28 7/ 4=1 7%d=4 -->residueExecution time 8.462 secondsPress any key to continue...但是7/4 = 1.75 应该是从后面保留吧,因为只取起于数不管其商的原则,对么? 作者:zean专家分:0 会员信息 发短消息 所属BLOG 发表时间:2006-11-27 19:11:00 [修改] [回复] [引用] 3 楼 当输入4位的时候,也正确啊enter 2 No.:4000 1000 4000+1000=5000 4000-1000=3000 4000*1000=2304 4000/1000=4 4000/1000=0 -->residueExecution time 13.462 secondsPress any key to continue... 作者:咖喱野人专家分:130 会员信息 发短消息 所属BLOG 发表时间:2006-11-27 19:14:00 [回复] [引用] 4 楼 你把第10那部分改成%d%%d=%.2d,应该就可以了 此帖尚未评分 请对此回帖评分: 10分还行 20分较满意 30分很满意 帮助 作者:zean专家分:0 会员信息 发短消息 所属BLOG 发表时间:2006-11-27 19:15:00 [修改] [回复] [引用] 5 楼 而3 的意思,通过您的解释我已经明白了main() { int a,b; printf("enter 2 No.:"); scanf ("%d %d",&a,&b); printf(" %3d+%3d=%d\n", a,b, a+b); printf(" %5d-%5d=%d\n", a,b, a-b); printf(" %0d*%0d=%d\n", a,b, a*b); printf(" %1d/%1d=%d\n", a,b, a/b); printf(" %9d/%d=%d -->residue\n", a,b, a%b); }我作了个试验enter 2 No.:7 3 7+ 3=10 7- 3=4 7*3=21 7/3=2 7/3=1 -->residueExecution time 6.099 secondsPress any key to continue...谢谢 作者:咖喱野人专家分:160 会员信息 发短消息 所属BLOG 发表时间:2006-11-27 19:17:00 [回复] [引用] 6 楼 9 printf(" %3d/%3d=%d\n", a,b, a/b);0 printf(" %3d/%3d=%d -->residue\n", a,b, a%b);=后面的%d 是输出整型数的,要想让其保留小数点后的数,要不就定义为实型的,要不就用%n.md这种格式,这样就不是1,而是1.57了

评论