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));

    多一个指针的标识*

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于#flink#的问题:关于docker部署flink集成hadoop的yarn,请教个问题flink启动yarn-session.sh连不上hadoop
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题