这里是我写的=_=,有很多问题
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
char name[20];
float score1,score2,score3;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%s,%ld,%f,%f,%f",p1->name,&p1->num,&p1->score1,&p1->score2,&p1->score3);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%s,%ld,%f,%f,%f",p1->name,&p1->num,&p1->score1,&p1->score2,&p1->score3);
printf("\n");
}
p2->next=NULL;
return (head);
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("\nlist null!\n");return head;
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if (num==p1->num)
{
if(p1==head)
head=p1->next;
else p2->next=p1->next;
printf("delete:%d\n",num);
n=n-1;
}
else printf("%d not been found!\n",num);
return (head);
}
void print(struct student *head)
{
struct student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%s,%ld,%f,%f,%f\n",p->name,p->num,p->score1,p->score2,p->score3);
p=p->next;
}
while(p!=NULL);
}
void Del_list(struct student *head )//delete the whole list
{
struct student *p,*L; p->next = L->next;head = L->next;
while( head != NULL )
{
p->next = head->next;
free( head );
head = p->next;
}
L->next = head;
return ;
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;p0->next=NULL;
}
else
{
while(p0->num>p1->num)
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
return (head);
}
void PrintHelp()//打印选项
{
printf("1: 创建学生数据\n");
printf("2: 删除某个学生信息\n");
printf("3: 打印所有学生信息\n");
printf("4: 删除所有学生信息\n");
printf("5: 保存学生信息到文件\n");
printf("6: 显示帮助信息\n");
printf("0: 退出程序\n");
}
void output(struct Student *head)
//保存学生信息到txt文件功能函数
{
FILE *write;
char filename[20];
struct Student *p1,*p2;
if (head == NULL)
//对空链表进行处理
{
printf("
没有学生信息,保存失败\n
");
system("pause");
return;
}
p1 = p2 = head;
printf("
请输入保存的文件名(如save): ");
while(!scanf("%s",filename))
//接受文件名,并进行排错(几乎不会出现错误的现象,慎重而设)
{
printf("
输入的文件名有误,请重新输入:");
fflush(stdin);
}
strcat(filename,".txt");
//链接后缀名
write = fopen(filename,"w");
//以写入方式打开文件
fclose(write);
printf("
保存成功\n
");
}
void Run()
{
char c;int a=0;struct student *head,*stu;long del_num;
PrintHelp();//第一次运行告诉用户那些选项对应哪些操作
printf("请输入操作选项:\n");
while(a!=1)
{
//读入用户的选项
switch(c=getchar())
{
case '0': a=1; break;
case '1':{creat();head=creat();break;}
case '2':del(head,del_num);break;
case'3': print(head);break;
case'4': Del_list(head);break;
case'5':output(head);break;
case'6': Del_list(head);break;
case'7': PrintHelp(); break;
}
}
}
void main()
{
Run();
}