#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
struct student
{
char num[10];//学号
char name[10];//姓名
float score[3];
float avr;//平均成绩
float sum;//总成绩
char rank;//排名
};
typedef struct table
{
struct student stu;//学生信息
struct table* next;//下一节点指针
}list,*plist;
plist free_list(plist head)
{
plist p=head;
plist q=head;
while(p)
{
q=p;
p=p->next;
free(q);
}
return NULL;
}
void show_list(plist head)//显示成绩
{
plist p=head;
system("cls");
if(head==NULL)
{
printf("无信息\n");
system("pause");
return ;
}
printf("学号 姓名 英语 数学 c语言 总分 平均 排名\n");
while(p)
{
printf("%-8s",p->stu.num);
printf("%-8s",p->stu.name);
printf("%5.2f",p->stu.score[0]);
printf("%5.2f",p->stu.score[1]);
printf("%5.2f",p->stu.score[2]);
printf("%8.2f",p->stu.sum);
printf("%8.2f\n",p->stu.avr);
printf("%-8s",p->stu.rank);
p=p->next;
}
system("pause");
}
int menu()
{
int n;
system("cls");
printf("1:添加学生信息\n");
printf("2:修改学生信息\n");
printf("3:显示学生信息\n");
printf("4:查询学生信息\n");
printf("5:删除学生信息\n");
printf("9:排序学生信息\n");
printf("0:运行结束\n");
scanf("%d",&n);
while(n<0||n>9)
{
printf("输入范围错误!请重新输入\n");
scanf("%d,&n");
}
return n;
}
void Save_File(plist head)
{
FILE *fp;
plist p=head;
if((fp=fopen("student.txt","wb"))==NULL)
{
printf("文件打开失败!");
system("pause");
return ;
}
while(p)
{
fwrite(&p->stu,sizeof(struct student),1,fp);
p=p->next;
}
printf("保存成功!\n");
fclose(fp);
system("pause");
}
plist Open_File(plist head)
{
FILE *fp;
head=free_list(head);
head=(plist)malloc(sizeof(list));
head->next=NULL;
plist p=head;
plist q=head;
if((fp=fopen("student.txt","rb"))==NULL)
{
printf("文件打开失败\n");
}
else
{
while(fread(&p->stu,sizeof(struct student),1,fp))
{
p->next=(plist)malloc(sizeof(list));
q=p;
p=p->next;
p->next=NULL;
}
q->next=NULL;
free(p);
fclose(fp);
printf("文件数据读取成功!\n");
}
system("pause");
return head;
}
plist add_stu(plist head)//录入成绩
{
plist p=NULL;
if(head==NULL)
{
head=(plist)malloc(sizeof(list));
head->next=NULL;
p=head;
}
else
{
p=head;
while(p->next)
{
p=p->next;
}
p->next=(plist)malloc(sizeof(list));
p=p->next;
p->next=NULL;
}
system("cls");
printf("请输入学生学号\n");
scanf("%s",&p->stu.num);
printf("请输入学生姓名\n");
scanf("%s",&p->stu.name);
printf("请输入学生英语成绩\n");
scanf("%f",&p->stu.score[0]);
printf("请输入学生数学成绩\n");
scanf("%f",&p->stu.score[1]);
printf("请输入学生c语言成绩\n");
scanf("%f",&p->stu.score[2]);
printf("录入成功!\n");
system("pause");
return head;
}
void find_num(plist head)
{
char temp[20];
int flag=1;
plist p=head;
system("cls");
printf("请输入要查找的学生学号\n");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.num))
{
flag=0;
printf("学号 姓名 英语 数学 c语言 总分 平均 排名\n");
printf("%-8s",p->stu.num);
printf("%-8s",p->stu.name);
printf("%5.2f",p->stu.score[0]);
printf("%5.2f",p->stu.score[1]);
printf("%5.2f",p->stu.score[2]);
printf("%8.2f",p->stu.sum);
printf("%8.2f\n",p->stu.avr);
printf("%-8s",p->stu.rank);
break;
}
p=p->next;
}
if(flag)
{
printf("无该学号信息!\n");
}
system("pause");
}
void find_name(plist head)
{
char temp[20];
int flag=1;
plist p=head;
system("cls");
printf("请输入要查找的学生姓名\n");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.name))
{
flag=0;
printf("学号 姓名 英语 数学 c语言 总分 平均 排名\n");
printf("%-8s",p->stu.num);
printf("%-8s",p->stu.name);
printf("%5.2f",p->stu.score[0]);
printf("%5.2f",p->stu.score[1]);
printf("%5.2f",p->stu.score[2]);
printf("%8.2f",p->stu.sum);
printf("%8.2f\n",p->stu.avr);
printf("%-8s",p->stu.rank);
break;
}
p=p->next;
}
if(flag)
{
printf("无该学生信息!\n");
}
system("pause");
}
void find_menu(plist head)
{
int n;
system("cls");
if(head==NULL)
{
printf("无任何数据\n");
system("pause");
return ;
}
do
{
system("cls");
printf("1:按学号查找\n");
printf("2:按姓名查找\n");
printf("0:返回\n");
scanf("%d",&n);
while(n<0||n>2)
{
printf("输入范围错误!请重新输入\n");
scanf("%d,&n");
}
switch(n)
{
case 1:
find_num(head);
break;
case 2:
find_name(head);
break;
}
}while(n!=0);
}
plist del_num(plist head)
{
char temp[20];
int flag=1;
plist p=head;
plist q=head;
system("cls");
printf("请输入要删除的学生学号\n");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.num))
{
flag=0;
printf("学号 姓名 英语 数学 c语言 总分 平均 排名\n");
printf("%-8s",p->stu.num);
printf("%-8s",p->stu.name);
printf("%5.2f",p->stu.score[0]);
printf("%5.2f",p->stu.score[1]);
printf("%5.2f",p->stu.score[2]);
printf("%8.2f",p->stu.sum);
printf("%8.2f\n",p->stu.avr);
printf("%-8s",p->stu.rank);
if(q==p)
{
head=head->next;
free(p);
}//都指向头结点
else
{
q->next=p->next;
free(p);
}
break;
}
q=p;
p=p->next;
}
if(flag)
{
printf("无该学号信息!\n");
}
else
{
printf("删除成功!\n");
}
system("pause");
return head;
}
plist del_name(plist head)
{
char temp[20];
int flag=1;
plist p=head;
plist q=head;
system("cls");
printf("请输入要删除的学生姓名\n");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.name))
{
flag=0;
printf("学号 姓名 英语 数学 c语言 总分 平均 排名\n");
printf("%-8s",p->stu.num);
printf("%-8s",p->stu.name);
printf("%5.2f",p->stu.score[0]);
printf("%5.2f",p->stu.score[1]);
printf("%5.2f",p->stu.score[2]);
printf("%8.2f",p->stu.sum);
printf("%8.2f\n",p->stu.avr);
printf("%-8s",p->stu.rank);
if(p==head)
{
head=head->next;
free(p);
}//都指向头结点
else
{
q->next=p->next;
free(p);
}
break;
}
p=p->next;
}
if(flag)
{
printf("无该学生信息!\n");
}
else
{
printf("删除成功!\n");
}
system("pause");
return head;
}
plist del_menu(plist head)
{
int n;
system("cls");
if(head==NULL)
{
printf("无任何数据\n");
system("pause");
return head;
}
do
{
system("cls");
printf("1:按学号删除\n");
printf("2:按姓名删除\n");
printf("0:返回\n");
scanf("%d",&n);
while(n<0||n>2)
{
printf("输入范围错误!请重新输入\n");
scanf("%d,&n");
}
switch(n)
{
case 1:
head=del_num(head);
break;
case 2:
head=del_name(head);
break;
}
}while(n!=0);
return head;
}
void modify(plist head)
{
char temp[20];
char ch;
int flag=1;
plist p=head;
system("cls");
printf("请输入要修改的学生学号\n");
scanf("%s",temp);
while(p)
{
if(!strcmp(temp,p->stu.num))
{
flag=0;
printf("学号 姓名 英语 数学 c语言 总分 平均 排名\n");
printf("%-8s",p->stu.num);
printf("%-8s",p->stu.name);
printf("%5.2f",p->stu.score[0]);
printf("%5.2f",p->stu.score[1]);
printf("%5.2f",p->stu.score[2]);
printf("%8.2f",p->stu.sum);
printf("%8.2f\n",p->stu.avr);
printf("%-8s",p->stu.rank);
break;
}
p=p->next;
}
if(flag)
{
printf("无该学号信息!\n");
}
else
{
printf("是否确认修改?Y/N");
fflush(stdin);
scanf("%c",&ch);
fflush(stdin);
if(ch=='y'||ch=='Y')
{
printf("请输入学生新学号\n");
scanf("%s",&p->stu.num);
printf("请输入学生新姓名\n");
scanf("%s",&p->stu.name);
printf("请输入学生英语成绩\n");
scanf("%f",&p->stu.score[0]);
printf("请输入学生数学成绩\n");
scanf("%f",&p->stu.score[1]);
printf("请输入学生c语言成绩\n");
scanf("%f",&p->stu.score[2]);
p->stu.sum=p->stu.score[0]+p->stu.score[1]+p->stu.score[2];
p->stu.avr=p->stu.sum/3;
printf("修改成功!\n");
}
}
system("pause");
}
plist sort_num(plist head)
{
struct student temp;
plist p,q;
p=q=head;
while(q)
{
p=q->next;
while(p)
{
if(strcmp(q->stu.num,p->stu.num)>0)
{
temp=q->stu;
q->stu=p->stu;
p->stu=temp;
}
p=p->next;
}
q=q->next;
}
show_list(head);
return head;
}
plist sort_name(plist head)
{
struct student temp;
plist p,q;
p=q=head;
while(q)
{
p=q->next;
while(p)
{
if(strcmp(q->stu.name,p->stu.name)>0)
{
temp=q->stu;
q->stu=p->stu;
p->stu=temp;
}
p=p->next;
}
q=q->next;
}
show_list(head);
return head;
}
plist sort_menu(plist head)
{
int n;
system("cls");
if(head==NULL)
{
printf("无任何数据\n");
system("pause");
return NULL;
}
do
{
system("cls");
printf("1:按学号排序\n");
printf("2:按姓名排序\n");
printf("0:返回\n");
scanf("%d",&n);
while(n<0||n>2)
{
printf("输入范围错误!请重新输入\n");
scanf("%d,&n");
}
switch(n)
{
case 1:
head=sort_num(head);
break;
case 2:
head=sort_name(head);
break;
}
}while(n!=0);
return head;
}
int main()
{
plist head=NULL;
int n;
do
{
n=menu();
switch(n)
{
case 0:
head=free_list(head);
break;
case 1:
head=add_stu(head);
break;
case 2:
modify(head);
break;
case 3:
show_list(head);
break;
case 4:
find_menu(head);
break;
case 5:
head=del_menu(head);
break;
case 6:
head=Open_File(head);
break;
case 7:
Save_File(head);
break;
case 8:
head=add_stu(head);
break;
case 9:
head=sort_menu(head);
break;
}
}while(n!=0);
return 0;
}
无法显示学生排名,最高分,最低分以及及格率
大概修改了一下,添加了2个函数。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
struct student
{
char num[10]; //学号
char name[10]; //姓名
float score[3];
float avr; //平均成绩
float sum; //总成绩
char rank; //排名
};
typedef struct table
{
struct student stu; //学生信息
struct table *next; //下一节点指针
} list, *plist;
plist free_list(plist head)
{
plist p = head;
plist q = head;
while (p)
{
q = p;
p = p->next;
free(q);
}
return NULL;
}
void print(plist p)
{
printf("学号 姓名 英语 数学 c语言 总分 平均 排名\n");
printf("%-8s", p->stu.num);
printf("%-8s", p->stu.name);
printf("%6.2f", p->stu.score[0]);
printf("%6.2f", p->stu.score[1]);
printf("%6.2f", p->stu.score[2]);
printf("%8.2f", p->stu.sum);
printf("%8.2f", p->stu.avr); // printf("%8.2f\n", p->stu.avr);
printf("%-8d\n", p->stu.rank); // printf("%-8s", p->stu.rank);
}
void show_list(plist head) //显示成绩
{
plist p = head;
system("cls");
if (head == NULL)
{
printf("无信息\n");
system("pause");
return;
}
printf("学号 姓名 英语 数学 c语言 总分 平均 排名\n");
while (p)
{
printf("%-8s", p->stu.num);
printf("%-8s", p->stu.name);
printf("%6.2f", p->stu.score[0]); //
printf("%6.2f", p->stu.score[1]); //
printf("%6.2f", p->stu.score[2]); //
printf("%8.2f", p->stu.sum);
printf("%8.2f", p->stu.avr); // printf("%8.2f\n", p->stu.avr);
printf("%-8d\n", p->stu.rank); // printf("%-8s", p->stu.rank);
p = p->next;
}
system("pause");
}
int menu()
{
int n;
system("cls");
printf("1:添加学生信息\n");
printf("2:修改学生信息\n");
printf("3:显示学生信息\n");
printf("4:查询学生信息\n");
printf("5:删除学生信息\n");
printf("8:显示最大值信息\n");//
printf("9:排序学生信息\n");
printf("0:运行结束\n");
scanf("%d", &n);
while (n < 0 || n > 9)
{
printf("输入范围错误!请重新输入\n");
scanf("%d", &n); // scanf("%d,&n");
}
return n;
}
void Save_File(plist head)
{
FILE *fp;
plist p = head;
if ((fp = fopen("student.txt", "wb")) == NULL)
{
printf("文件打开失败!");
system("pause");
return;
}
while (p)
{
fwrite(&p->stu, sizeof(struct student), 1, fp);
p = p->next;
}
printf("保存成功!\n");
fclose(fp);
system("pause");
}
plist Open_File(plist head)
{
FILE *fp;
head = free_list(head);
head = (plist)malloc(sizeof(list));
head->next = NULL;
plist p = head;
plist q = head;
if ((fp = fopen("student.txt", "rb")) == NULL)
{
printf("文件打开失败\n");
}
else
{
while (fread(&p->stu, sizeof(struct student), 1, fp))
{
p->next = (plist)malloc(sizeof(list));
q = p;
p = p->next;
p->next = NULL;
}
q->next = NULL;
free(p);
fclose(fp);
printf("文件数据读取成功!\n");
}
system("pause");
return head;
}
plist add_stu(plist head) //录入成绩
{
plist p = NULL;
if (head == NULL)
{
head = (plist)malloc(sizeof(list));
head->next = NULL;
p = head;
}
else
{
p = head;
while (p->next)
{
p = p->next;
}
p->next = (plist)malloc(sizeof(list));
p = p->next;
p->next = NULL;
}
system("cls");
printf("请输入学生学号\n");
scanf("%s", p->stu.num); // scanf("%s", &p->stu.num);
printf("请输入学生姓名\n");
scanf("%s", p->stu.name); // scanf("%s", &p->stu.name);
printf("请输入学生英语成绩\n");
scanf("%f", &p->stu.score[0]);
printf("请输入学生数学成绩\n");
scanf("%f", &p->stu.score[1]);
printf("请输入学生c语言成绩\n");
scanf("%f", &p->stu.score[2]);
//////
p->stu.sum = p->stu.score[0] + p->stu.score[1] + p->stu.score[2];
p->stu.avr = p->stu.sum / 3;
p->stu.rank = 0;
/////
printf("录入成功!\n");
system("pause");
return head;
}
void find_num(plist head)
{
char temp[20];
int flag = 1;
plist p = head;
system("cls");
printf("请输入要查找的学生学号\n");
scanf("%s", temp);
while (p)
{
if (!strcmp(temp, p->stu.num))
{
flag = 0;
print(p); //
break;
}
p = p->next;
}
if (flag)
{
printf("无该学号信息!\n");
}
system("pause");
}
void find_name(plist head)
{
char temp[20];
int flag = 1;
plist p = head;
system("cls");
printf("请输入要查找的学生姓名\n");
scanf("%s", temp);
while (p)
{
if (!strcmp(temp, p->stu.name))
{
flag = 0;
print(p); //
break;
}
p = p->next;
}
if (flag)
{
printf("无该学生信息!\n");
}
system("pause");
}
void find_menu(plist head)
{
int n;
system("cls");
if (head == NULL)
{
printf("无任何数据\n");
system("pause");
return;
}
do
{
system("cls");
printf("1:按学号查找\n");
printf("2:按姓名查找\n");
printf("0:返回\n");
scanf("%d", &n);
while (n < 0 || n > 2)
{
printf("输入范围错误!请重新输入\n");
scanf("%d", &n); // scanf("%d,&n");
}
switch (n)
{
case 1:
find_num(head);
break;
case 2:
find_name(head);
break;
}
} while (n != 0);
}
plist del_num(plist head)
{
char temp[20];
int flag = 1;
plist p = head;
plist q = head;
system("cls");
printf("请输入要删除的学生学号\n");
scanf("%s", temp);
while (p)
{
if (!strcmp(temp, p->stu.num))
{
flag = 0;
print(p); //
if (q == p)
{
head = head->next;
free(p);
} //都指向头结点
else
{
q->next = p->next;
free(p);
}
break;
}
q = p;
p = p->next;
}
if (flag)
{
printf("无该学号信息!\n");
}
else
{
printf("删除成功!\n");
}
system("pause");
return head;
}
plist del_name(plist head)
{
char temp[20];
int flag = 1;
plist p = head;
plist q = head;
system("cls");
printf("请输入要删除的学生姓名\n");
scanf("%s", temp);
while (p)
{
if (!strcmp(temp, p->stu.name))
{
flag = 0;
print(p); //
if (p == head)
{
head = head->next;
free(p);
} //都指向头结点
else
{
q->next = p->next;
free(p);
}
break;
}
p = p->next;
}
if (flag)
{
printf("无该学生信息!\n");
}
else
{
printf("删除成功!\n");
}
system("pause");
return head;
}
plist del_menu(plist head)
{
int n;
system("cls");
if (head == NULL)
{
printf("无任何数据\n");
system("pause");
return head;
}
do
{
system("cls");
printf("1:按学号删除\n");
printf("2:按姓名删除\n");
printf("0:返回\n");
scanf("%d", &n);
while (n < 0 || n > 2)
{
printf("输入范围错误!请重新输入\n");
scanf("%d", &n); // scanf("%d,&n");
}
switch (n)
{
case 1:
head = del_num(head);
break;
case 2:
head = del_name(head);
break;
}
} while (n != 0);
return head;
}
void modify(plist head)
{
char temp[20];
char ch;
int flag = 1;
plist p = head;
system("cls");
printf("请输入要修改的学生学号\n");
scanf("%s", temp);
while (p)
{
if (!strcmp(temp, p->stu.num))
{
flag = 0;
print(p); //
break;
}
p = p->next;
}
if (flag)
{
printf("无该学号信息!\n");
}
else
{
printf("是否确认修改?Y/N");
fflush(stdin);
scanf("%c", &ch);
fflush(stdin);
if (ch == 'y' || ch == 'Y')
{
printf("请输入学生新学号\n");
scanf("%s", p->stu.num); // scanf("%s", &p->stu.num);
printf("请输入学生新姓名\n");
scanf("%s", p->stu.name); // scanf("%s", &p->stu.name);
printf("请输入学生英语成绩\n");
scanf("%f", &p->stu.score[0]);
printf("请输入学生数学成绩\n");
scanf("%f", &p->stu.score[1]);
printf("请输入学生c语言成绩\n");
scanf("%f", &p->stu.score[2]);
p->stu.sum = p->stu.score[0] + p->stu.score[1] + p->stu.score[2];
p->stu.avr = p->stu.sum / 3;
printf("修改成功!\n");
}
}
system("pause");
}
plist sort_sum(plist head) //按成绩排序
{
struct student temp;
plist p, q;
p = q = head;
int i = 1; //排名
while (q)
{
p = q->next;
while (p)
{
if (q->stu.sum < p->stu.sum)
{
temp = q->stu;
q->stu = p->stu;
p->stu = temp;
}
p = p->next;
}
q->stu.rank = i++; //
q = q->next;
}
show_list(head);
return head;
}
plist sort_num(plist head)
{
struct student temp;
plist p, q;
p = q = head;
while (q)
{
p = q->next;
while (p)
{
if (strcmp(q->stu.num, p->stu.num) > 0)
{
temp = q->stu;
q->stu = p->stu;
p->stu = temp;
}
p = p->next;
}
q = q->next;
}
show_list(head);
return head;
}
plist sort_name(plist head)
{
struct student temp;
plist p, q;
p = q = head;
while (q)
{
p = q->next;
while (p)
{
if (strcmp(q->stu.name, p->stu.name) > 0)
{
temp = q->stu;
q->stu = p->stu;
p->stu = temp;
}
p = p->next;
}
q = q->next;
}
show_list(head);
return head;
}
plist sort_menu(plist head)
{
int n;
system("cls");
if (head == NULL)
{
printf("无任何数据\n");
system("pause");
return NULL;
}
do
{
system("cls");
printf("1:按学号排序\n");
printf("2:按姓名排序\n");
printf("3:按成绩排序\n"); //
printf("0:返回\n");
scanf("%d", &n);
while (n < 0 || n > 3)
{
printf("输入范围错误!请重新输入\n");
scanf("%d", &n); // scanf("%d,&n");
}
switch (n)
{
case 1:
head = sort_num(head);
break;
case 2:
head = sort_name(head);
break;
case 3:
head = sort_sum(head);
break;
}
} while (n != 0);
return head;
}
////
void show_max(plist head)
{
plist p;
p = head;
float max[3], min[3], ave[3];
int count = 0, jige[3] = {0}, i;
while (p)
{
if (count == 0)
{
for (i = 0; i < 3; i++)
{
max[i] = min[i] = p->stu.score[i];
if (max[i] >= 60)
jige[i] = 1;
}
}
else
{
for (i = 0; i < 3; i++)
{
if (max[i] < p->stu.score[i])
max[i] = p->stu.score[i];
if (min[i] > p->stu.score[i])
min[i] = p->stu.score[i];
if (p->stu.score[i] >= 60)
jige[i] += 1;
}
}
count++;
p = p->next;
}
for (i = 0; i < 3; i++)
{
ave[i] = jige[i] * 1.0 / count * 100;
printf("kec:%-5d max:%-8.2f min:%-8.2f jige:%5.2f%%\n", i + 1, max[i], min[i], ave[i]);
}
}
int main()
{
plist head = NULL;
int n;
do
{
n = menu();
switch (n)
{
case 0:
head = free_list(head);
break;
case 1:
head = add_stu(head);
break;
case 2:
modify(head);
break;
case 3:
show_list(head);
break;
case 4:
find_menu(head);
break;
case 5:
head = del_menu(head);
break;
case 6:
head = Open_File(head);
break;
case 7:
Save_File(head);
break;
case 8:
show_max(head);//
break;
case 9:
head = sort_menu(head);
break;
}
} while (n != 0);
return 0;
}