哪位大佬逐行逐句地解释一下这段代码,我给他磕头了!!

weixin_45906870 2020-02-15 01:16:23
#include <stdio.h>
#include <stdlib.h>

int main()
{
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 allocation 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;
}
...全文
113 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eritque arcus 2020-02-15
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>         

int main()
{
//声明变量
    double *ptd;//这是一个双精度小数类型的指针
    int max;
    int number;
    int i = 0;
    
    puts("What is the maximum number of type double entries?");//输出字符串
    if (scanf("%d", &max) != 1)//输入数字到max变量
    {
        puts("Number not correctly entered -- bye.");
        exit(EXIT_FAILURE);//退出,和return在main里面同样
    }
    ptd = (double *) malloc(max * sizeof(double));//分配内存
    if (ptd == NULL)
    {
        puts("Memory allocation 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)//i取余7
            putchar('\n');//输出单个字符
    }
    if (i%7 != 0)
        putchar('\n');
    puts("Done.");
    free(ptd);//释放变量占有的内存
    
    return 0;
}
寻开心 2020-02-15
  • 打赏
  • 举报
回复
输入一个数max , scanf返回1才表示这个数被正确获得到
if (scanf("%d", &max) != 1)
建立一个动态数组,大小就是max
ptd = (double *) malloc(max * sizeof(double));
输入max个浮点数
while (i<max && scanf("%lf", &ptd[i]) == 1) ++i;
七个一行的输出, i%7 从0到6 , 6的时候表示已经输出了7个,所以在此输出个回车好换行
if (i%7==6) putchar('\n');
最后一行不满七个也换行
if (i%7 != 0) putchar('\n');

释放内存
free(ptd);

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧