1b1n1m 发表于 2017-1-4 19:14:02

请教一道C primer plus第六版上面的练习题

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

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

#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)
{
printf("Now enter the first operand:\n");
while ((scanf("%d",b))==1)
{
resaut = b%a;
printf("%d % %d is %d",b,a,resaut);
}
}
printf("errer!");

getchar();
getchar();
return 0;

}


lzgw 发表于 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");
                }
        }
}
是不是这个意思?

陌雨 发表于 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;   
}

xw0314 发表于 2017-1-5 10:15:30

while(scanf("%d", &b) == 1 && b > 0)
页: [1]
查看完整版本: 请教一道C primer plus第六版上面的练习题