69,371
社区成员
发帖
与我相关
我的任务
分享
#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;
}