qq_29278595 2015-07-02 04:31 采纳率: 0%
浏览 1551

创建建链表 那部分的错误是什么 看了半天没有看出来

#include
#include
#include

struct student
{
char num[9];
char name[9];
float scorel;
float scorec;
float scored;
float sum;
struct student next;
};
struct student *creat(struct student *p)
{
struct student *p1=NULL;
p1=(struct student
)malloc(sizeof(struct student));
p->next=p1;
p1->next=NULL;
printf("请输入添加学生的信息\n");
printf("学号:"); scanf("%s",p1->num);
printf("姓名:"); scanf("%s",p1->name);
printf("linux成绩:"); scanf("%f",&p1->scorel);
printf("单片机成绩:"); scanf("%f",&p1->scored);
printf("C语言成绩:"); scanf("%f",&p1->scorec);
p1->sum=0;
p1->sum= p1->scorel + p1->scored + p1->scorec;
printf("总分:%d",p1->sum);
return p1;
}
void save1(struct student *p)//将链表从内存保存到文件
{
FILE *fp;
if((fp=fopen("F:\学生成绩管理系统.txt","ab+"))==NULL)
printf("创建失败!\n");
while(p!=NULL)
{
fwrite(p,sizeof(struct student),1,fp);
p=p->next;
}
fclose(fp);
printf("保存成功!\n");
}
struct student *read()
{
FILE *fp;
struct student *head=NULL,*p1=NULL,*p2=NULL;
if((fp=fopen("F:\学生成绩管理系统.txt","rb+"))==NULL)
{
printf("读取时,文件打开失败!\n");
return NULL;
}
head =p1=(struct student *)malloc(sizeof(struct student));
while(fp!=NULL)
{
if(fread(p1,sizeof(struct student),1,fp)!=1)
break;
p1->next=(struct student *)malloc(sizeof(struct student));
p2=p1;
p1=p1->next;
}
p2->next=NULL;
fclose(fp);
return head;
}
struct student *input()
{
char ch;
struct student *p=NULL,*head=NULL;
head=p=(struct student *)malloc(sizeof(struct student));
p->next=NULL;
printf("请输入要添加的学生信息\n");
printf("学号:"); scanf("%s",p->num);
printf("姓名:"); scanf("%s",p->name);
printf("linux成绩:"); scanf("%f",&p->scorel);
printf("单片机成绩:"); scanf("%f",&p->scored);
printf("C语言成绩:"); scanf("%f",&p->scorec);
p->sum=0;
p->sum=p->scorel + p->scored + p->scorec;
printf("总分:%d",p->sum);
for(;;)
{
printf("\n是否继续输入学生信息?(y/n):\n");
getchar();
ch=getchar();
if(ch=='y'||ch=='Y')
p=creat(p);
else if(ch=='n'||ch=='N')
{
save1(head);
break;
}
else
printf("您的输入有错,请重新输入:");

}
return p->next;

}
struct student output()
{
struct student *p;
p=read();
if(read==NULL)
exit(1);
printf("\n
********************STUDENT*********************\n");
printf("----------------------------------\n");
printf(" | 学号 | 姓名 | linux | 单片机 | C语言 | 总分 |\n");
printf("----------------------------------\n");
while(p!=NULL)
{
p->sum=0;
p->sum=p->scorel + p->scored + p->scorec;
printf(" | %4s | %-4s | %3d | %3d | %3d | %3d |\n",p->sum,p->name,p->scorel,p->scored,p->scorec,p->sum);
printf("----------------------------------\n");
p=p->next;
}
return p;
}图片说明

  • 写回答

5条回答 默认 最新

  • 91program 博客专家认证 2015-07-02 04:36
    关注

    p1=(struct student)malloc(sizeof(struct student));
    应该是:
    p1=(struct student *)malloc(sizeof(struct student));

    多一个指针的标识*

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!