求大神帮我改成链表方法。。

#include
#include
#include
struct Student
{
char id[20];
char name[20];
float Chinese;
float Math;
float English;
float average;

}students[1024];
int j=0;
int id1(char id[])
{
int i;
for (i=0;i<j;i++)
{
if (strcmp(students[i].id,id)==0)
{
return i;
}
}
return -1;
}

int name1(char name[])
{
int i;
for (i=0;i<j;i++)
{
if (strcmp(students[i].name,name)==0)
{
return i;
}
}
return -1;
}

void personal(int code)
{
printf("%10s%10s%10s%10s%10s%10s\n","学号","姓名","语文成绩","数学成绩","英语成绩","平均成绩");
printf("**************************************************************\n");
printf("%10s%10s%10g%10g%10g%10g\n",students[code].id,students[code].name,students[code].Chinese,students[code].Math,students[code].English,students[code].average);
puts("");
}

void display()
{
int i;
printf("%10s%10s%10s%10s%10s%10s\n","学号","姓名","语文成绩","数学成绩","英语成绩","平均成绩");
printf("**************************************************************\n");
for (i=0;i<j;i++)
{
printf("%10s%10s%10g%10g%10g%10g\n",students[i].id,students[i].name,students[i].Chinese,students[i].Math,students[i].English,students[i].average);
}
}

void namesearch()
{
while(1)
{
char name[20];
int code;
printf("请输入要查询的学生的姓名:");
scanf("%s",&name);
puts("");
getchar();
code=name1(name);
if (code==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要查询的学生信息为:\n");
puts("");
personal(code);
}
printf("是否继续?(Y/N)");
if (getchar()=='N')
{
break;
}
}
}

void idsearch()
{
while(1)
{
char id[20];
int code;
printf("请输入要查询学生的学号:");
scanf("%s",&id);
puts("");
getchar();
code=id1(id);
if (code==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要查询的学生信息为:\n");
puts("");
personal(code);
}
printf("是否继续?(Y/N)");
if (getchar()=='N')
{
break;
}
}
}

void input()
{
while(1)
{
printf("输入学号:");
scanf("%s",&students[j].id);
getchar();

    printf("输入姓名:");
    scanf("%s",&students[j].name);
    getchar();

    printf("输入语文成绩:");
    scanf("%f",&students[j].Chinese);
    getchar();

    printf("输入数学成绩:");
    scanf("%f",&students[j].Math);
    getchar();

    printf("输入英语成绩:");
    scanf("%f",&students[j].English);
    getchar();

    students[j].average=(students[j].Chinese+students[j].Math+students[j].English)/3;
    printf("平均成绩为:%g\n",students[j].average);
    j++;

    puts("");
    printf("是否继续?(Y/N)");
    if (getchar()=='N')
    {
        break;
    }
}
puts("");

}

int cmp(const void*a,const void*b)
{
return ((struct Student*)a)->average < ((struct Student*)b)->average ? 1 : 0 ;
}
void sort()
{
qsort(students,j,sizeof(struct Student),cmp);
}

void modify()
{
while(1)
{
char id[20];
int code;
printf("请输入要修改的学生的学号:");
scanf("%s",&id);
puts("");
getchar();
code=id1(id);
if (code==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要修改的学生信息为:\n");
puts("");
personal(code);
printf("************ 请重新输入学生信息 ************\n");
puts("");
printf("输入学号:");
scanf("%s",&students[code].id);
getchar();

        printf("输入姓名:");
        scanf("%s",&students[code].name);
        getchar();

        printf("输入语文成绩:");
        scanf("%f",&students[code].Chinese);
        getchar();

        printf("输入数学成绩:");
        scanf("%f",&students[code].Math);
        getchar();

        printf("输入英语成绩:");
        scanf("%f",&students[code].English);
        getchar();

        students[j].average=(students[code].Chinese+students[code].Math+students[code].English)/3;
        printf("平均成绩为:%g\n",students[code].average);
    }
    puts("");
    printf("是否继续?(Y/N)");
    if (getchar()=='N')
    {
        break;
    }

}
puts("");
}

void delete()
{
int i;
while(1)
{
char id[20];
int code;
printf("请输入要删除的学生的学号:");
scanf("%s",&id);
puts("");
getchar();
code=id1(id);
if (code==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要删除的学生信息为:\n");
personal(code);
printf("确定?(Y/N)");
if (getchar()=='Y')
{
for (i=code;i<j-1;i++)
{
students[i]=students[i+1];
}
j--;
}
getchar();
}
printf("是否继续?(Y/N)");
if (getchar()=='N')
{
break;
}
}
}
void read()
{
FILE *fp;
int i;
if ((fp=fopen("c:\Database.txt","rb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if (fread(&j,sizeof(int),1,fp)!=1)
{
j=-1;
}
else
{
for(i=0;i<j;i++)
{
fread(&students[i],sizeof(struct Student),1,fp);
}
}
fclose(fp);
}

void write()
{
FILE *fp;
int i;
if ((fp=fopen("c:\Database.txt","wb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if (fwrite(&j,sizeof(int),1,fp)!=1)
{
printf("写入文件错误!\n");
}
for (i=0;i<j;i++)
{
if (fwrite(&students[i],sizeof(struct Student),1,fp)!=1)
{
printf("写入文件错误!\n");
}
}
fclose(fp);
}

int main()
{
int option;
read();
while(1)
{
printf("\n------ 学生成绩管理系统------\n");
printf("1. 增加学生记录\n");
printf("2. 修改学生记录\n");
printf("3. 删除学生记录\n");
printf("4. 按姓名查询学生记录\n");
printf("5. 按学号查询学生记录\n");
printf("6. 按平均成绩排序\n");
printf("7. 退出\n");
printf("请选择(1-7):");
scanf("%d",&option);
getchar();
switch(option)
{
case 1:
input();
break;
case 2:
modify();
break;
case 3:
delete();
break;
case 4:
namesearch();
break;
case 5:
idsearch();
case 6:
sort();
display();
break;
case 7:
exit(0);
break;
}
write();
}
}

作业要求用链表做 可是我不会链表,求大神帮忙修改下

查看全部
apocalyps3
apocalyps3
2015/12/27 09:15
  • c
  • 链表
  • 点赞
  • 收藏
  • 回答
    私信

1个回复