C语言问题请教高手!!成功解答后送5Q币

来源:百度知道 编辑:UC知道 时间:2024/09/28 13:31:04
下面是一个本人写的一个“粮食销售计算程式”,要求用户输入价格和每次粮食重量,当输入的粮食重量为0时结束程序,输出结果。本人编译能通过,但无法出现正确结果,希望高手不吝指教!多谢!!!
#include<stdio.h>
#include<malloc.h> /*动态开辟空间函数*/
#define NULL 0
#define LEN sizeof(struct add)
int n=0; /*全局变量*/

struct add
{
float height;
struct add *next;
};

struct add *creat(void) /*建立链表*/
{
struct add *head;
struct add *p1,*p2;
p1=p2=(struct add*)malloc(LEN);
printf("请输入重量(公斤即kg为单位),输入完毕后请键入数字0,按Enter键:");
scanf("%f\n",&p1->height);
head=NULL;
while(p1->height!=0)
{ n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct add*)malloc(LEN);
printf("请输入重量(公斤即kg为单位),输入完毕后请键入数字0,按Enter键:");
scanf("%f\n",&p1->height);
}
p2->next=NULL;
return (head);
}

//在Create函数中链表构造有点问题,然后是Process函数中的循环条件,最后malloc函数申请的空间不要忘了用free释放。
#include<stdio.h>
#include<malloc.h> /*动态开辟空间函数*/

#define NULL 0
#define LEN sizeof(struct add)
int n=0; /*全局变量*/

struct add
{
float height;
struct add *next;
};

struct add *creat(void) /*建立链表*/
{
struct add *head, *p1, *p2;

head=NULL;
p1=p2=(struct add*)malloc(LEN);

printf("请输入重量(公斤即kg为单位),输入完毕后请键入数字0,按Enter键结束:");
scanf("%f",&p1->height);

while(p1->height!=0)
{
n=n+1;

if(n==1)
{
head=p1;
}
else
{
p2->next=p1;
p2 = p1;
}

p1=(struct add*)malloc(LEN);
printf("请输入重量(公斤即kg为单位),输入完毕后请键入数字0,按Enter键结束:");
scanf("%f",&p1->height);
}
free(p1);
p2->next=NULL;
return (head);
}
<