|
5鱼币
- #include <stdio.h>
- #include <stdlib.h>
- int main(void)
- {
- double *ptd;
- int max;
- int number;
- int i = 0;
-
- puts("What is the maximum number of type double entries?");
- if(scanf("%d", &max) !=1)
- {
- puts("Number not correctly entered -- bye.");
- exit(EXIT_FAILURE);
- }
- ptd = (double *) malloc(max * sizeof(double));
- if( ptd == NULL)
- {
- puts("Memory allocatuon failed.Goodbye.");
- exit(EXIT_FAILURE);
- }
-
- puts("Enter the values (q to quit):");
- while( i < max && scanf("%lf", &ptd[i]) == 1)
- ++i;
- printf("Here are your %d entries:\n", number = i);
- for( i = 0; i < number; i++)
- {
- printf("%7.2f", ptd[i]);
- if( i % 7 == 6)
- putchar('\n');
- }
- if(i % 7 != 0)
- putchar('\n');
- puts("Done.");
- free(ptd);
-
- return 0;
- }
复制代码
这段代码,我有几个不理解的地方。
第一:if(scanf("%d", &max) !=1) 这个if 语句中的内容 !=1 我理解是输入一个数不等于1的话执行下面的内容,实际上if语句内的并没有执行。
第二:while( i < max && scanf("%lf", &ptd[i]) == 1)
++i;
这个循环 我感觉是一个当 i <max 和 scanf内容同时为真的时候 循环内容执行 但是 scanf("%lf,&ptd[i])这个没有看懂
1. - 第一:if(scanf("%d", &max) !=1) 这个if 语句中的内容 !=1 我理解是输入一个数不等于1的话执行下面的内容,实际上if语句内的并没有执行。
复制代码scanf 的返回值是成功读入的元素个数。
这里如果正常读入 max,那么 scanf 返回值应该为1。
所以条件的意思是没有成功读入 max。
2. - i < max && scanf("%lf", &ptd[i]) == 1 没有看懂
复制代码就是判断是否成功读入了ptd 的第 i 个元素。
满意请给个最佳哦~
|
最佳答案
查看完整内容
1.scanf 的返回值是成功读入的元素个数。
这里如果正常读入 max,那么 scanf 返回值应该为1。
所以条件的意思是没有成功读入 max。
2.就是判断是否成功读入了ptd 的第 i 个元素。
满意请给个最佳哦~
|