鱼C论坛

 找回密码
 立即注册
查看: 2029|回复: 3

[已解决]请教一道C primer plus第六版上面的练习题

[复制链接]
发表于 2017-1-4 19:14:02 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 1b1n1m 于 2017-1-5 20:27 编辑

编写一个程序,显示求模运算的结果.把用户输入的第一个整数作为求模运算符的第二个
运算对象,该数在运算过程中保持不变.用户后面输入的数是第一个运算对象.当用户输入一个
非正值时,程序结束.这是C primer plus第六版 第五章134页上面的练习题(第8题).我敲出来的代码我自己看着都懵逼

  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int a = 0,b = 0,resaut = 0;
  5. printf("This program computes moduli.\n" "Enter an integer to serve as the second oprtand:\n");

  6. while (scanf("%d",&a)==1)
  7. {
  8. printf("Now enter the first operand:\n");
  9. while ((scanf("%d",b))==1)
  10. {
  11. resaut = b%a;
  12. printf("%d % %d is %d",b,a,resaut);
  13. }
  14. }
  15. printf("errer!");

  16. getchar();
  17. getchar();
  18. return 0;

  19. }
复制代码



最佳答案
2017-1-4 20:27:09
本帖最后由 lzgw 于 2017-1-4 20:36 编辑

#include <stdio.h>

int main()
{
        int a = 0,b = 0,resaut = 0;
        printf("This program computes moduli.\n" "Enter an integer to serve as the second oprtand:\n");
       
        while (scanf("%d",&a) ==1 )
        {
        getchar();
                printf("Now enter the first operand:\n");
                while ((scanf("%d",&b))==1)
                {
                        getchar();
                        if (b < 0) //输入的是负数就退出
                        {
                                printf("errer!\n");
                                return 0;
                        }
                        resaut = b%a;
                        printf("%d %% %d is %d\n",b,a,resaut);
                        printf("Now enter the first operand:\n");
                }
        }
}
是不是这个意思?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-1-4 20:27:09 | 显示全部楼层    本楼为最佳答案   
本帖最后由 lzgw 于 2017-1-4 20:36 编辑

#include <stdio.h>

int main()
{
        int a = 0,b = 0,resaut = 0;
        printf("This program computes moduli.\n" "Enter an integer to serve as the second oprtand:\n");
       
        while (scanf("%d",&a) ==1 )
        {
        getchar();
                printf("Now enter the first operand:\n");
                while ((scanf("%d",&b))==1)
                {
                        getchar();
                        if (b < 0) //输入的是负数就退出
                        {
                                printf("errer!\n");
                                return 0;
                        }
                        resaut = b%a;
                        printf("%d %% %d is %d\n",b,a,resaut);
                        printf("Now enter the first operand:\n");
                }
        }
}
是不是这个意思?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-4 21:23:35 | 显示全部楼层
int main(){
    int a,b;
   
    scanf("%d",&a);
    while(1){
        scanf("%d",&b);
        if(b<=0)return 1;
        print("%d \n",b/a);
    }
    return 0;   
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-5 10:15:30 | 显示全部楼层
while(scanf("%d", &b) == 1 && b > 0)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-19 13:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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