学生系统最新修改.txt
来自「一个不错的程序学生管理系统, 有各程序的输入.及其各种功能」· 文本 代码 · 共 437 行
TXT
437 行
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof (struct stu)
struct stu
{
char num[10];
float score[3];
struct stu *next;
};
struct stu *creat(int n) //创建链表
{
struct stu *head,*pf,*pb;
int i;
for(i = 0;i<n;i++)
{
pb=(struct stu *)malloc(LEN);
if(pb == NULL)
{
printf("error!");
exit(0);
}
printf("input Number:");
scanf("%s",pb->num);
printf("input score:\n");
printf("Chinese:");
scanf("%f",&pb->score[0]);
printf("Math:");
scanf("%f",&pb->score[1]);
printf("English:");
scanf("%f",&pb->score[2]);
if(i==0)
pf = head = pb;
else
pf->next = pb;
pb ->next = NULL;
pf = pb;
}
return(head);
}
void printlist(struct stu *head) //打印链表里的数据
{
struct stu *p;
int i = 0;
if(head == NULL)
printf("NULL");
p = head;
while(p != NULL)
{
printf("Number:%s\n ",p->num);
printf("Chinese:%g Math:%g English:%g\n",p->score[0],p->score[1],p->score[2]);
p = p -> next;
}
printf("\n");
}
struct stu *dellist(struct stu *head,char *num) //删除节点做法1
{
struct stu *front,*cursor;
cursor = head;
while(cursor != NULL && strcmp(cursor->num,num) != 0) //找到删除点,首地址为cursor
{
front = cursor;
cursor = cursor-> next;
}
if(cursor != NULL)
{
if(head == cursor)
head = cursor->next;
else
front->next = cursor->next; //删除节点,并释放内存
free(cursor);
}
return (head);
}
struct stu *insert(struct stu *head) //插入节点
{
struct stu *pf,*pb,*newone,*pi;
pb = head;
newone = (struct stu *)malloc(LEN);
printf("请输入要插入的学生的Number:");
scanf("%s",newone->num);
printf("请输入要插入的学生的Score:\n");
printf("Chinese:");
scanf("%f",&newone->score[0]);
printf("Math:");
scanf("%f",&newone->score[1]);
printf("English:");
scanf("%f",&newone->score[2]);
pi = newone;
if(head == NULL)
{
head = pi;
pi->next = NULL;
}
else
{
while(strcmp(pi->num,pb->num) < 0 && (pb->next != NULL))
{
pf = pb;
pb = pb->next;
} //找插入位置
if(strcmp(pi->num,pb->num) >= 0)
{
if(head == pb)
head = pi; //在第一结点之前插入
else
pf ->next = pi; //在其它位置插入
pi->next = pb;
}
else
{
pb->next = pi;
pi->next = NULL;
} //在表末插入
}
return head;
}
struct stu *paixu(struct stu *head,int i) //将链表按学生学号从小到大排列
{
struct stu *pb,*pf,*p1,*p2,*p3,*p;
p3 = pb = pf = head;
for(;pf->next != NULL;pf = pf->next)
{
p1 = pb = pf;
while(pb->next != NULL)//选出最大number的节点地址
{
p2 = pb;
pb = pb->next;
if(strlen(pf->num) == strlen(pb->num) && strcmp(pf->num,pb->num) > 0)
{
p = p2; //用p存放pf的前一个指针地址
pf = pb;//用pf存放number大的指针
}
if(strlen(pf->num) > strlen(pb->num))
{
p = p2; //用p存放pf的前一个指针地址
pf = pb;//用pf存放number大的指针
}
}
if(pf != p1)
{
p->next = pf->next;
pf->next = p1;
if(i != 1)
p3->next = pf;
p3 = pf; //p3为每次for循环前一次的pf
}
if(i == 1) //返回排序好的链表的头指针
goto end;
}
end:
return pf;
}
struct stu *paixu2(struct stu *head,int i,int j) //将链表按学生学号从小到大排列
{
struct stu *pb,*pf,*p1,*p2,*p3,*p;
p3 = pb = pf = head;
for(;pf->next != NULL;pf = pf->next)
{
p1 = pb = pf;
while(pb->next != NULL)//选出最大number的节点地址
{
p2 = pb;
pb = pb->next;
if(pf->score[j - 1] > pb->score[j - 1])
{
p = p2; //用p存放pf的前一个指针地址
pf = pb;//用pf存放number大的指针
}
}
if(pf != p1)
{
p->next = pf->next;
pf->next = p1;
if(i != 1)
p3->next = pf;
p3 = pf; //p3为每次for循环前一次的pf
}
if(i == 1) //返回排序好的链表的头指针
goto end;
}
end:
return pf;
}
struct stu *chmax(struct stu *head)
{
struct stu *pb,*pf,*p1;
pb = pf = head;
while(pb->next != NULL)//选出最小number的节点地址
{
p1 = pb;
pb = pb->next;
if(pf->score[0] < pb->score[0])
pf = pb;//用pf存放number小的指针
}
return pf;
}
struct stu *mtmax(struct stu *head)
{
struct stu *pb,*pf,*p1;
pb = pf = head;
while(pb->next != NULL)//选出最大number的节点地址
{
p1 = pb;
pb = pb->next;
if(pf->score[1] < pb->score[1])
pf = pb; //用pf存放number大的指针
}
return pf;
}
struct stu *engmax(struct stu *head)
{
struct stu *pb,*pf,*p1;
pb = pf = head;
while(pb->next != NULL) //选出最大number的节点地址
{
p1 = pb;
pb = pb->next;
if(pf->score[2] < pb->score[2])
pf = pb; //用pf存放number大的指针
}
return pf;
}
struct stu *lookup(struct stu *head) //按学号查找学生
{
char a[10] = {"0"};
int i,j = 0;
printf("请输入需查找学生的学号:");
scanf("%s",a);
while(head != NULL)
{
for(i = 0;a[i] != 0 || head->num[i] != 0;i++)
{
if(a[i] == head->num[i])
j++;
}
if(i == j)
return head;
else
head = head->next;
}
if(head == NULL && i != j)
return head;
}
void prtamong(struct stu *head,int i) //打印处于某个分数段的学生信息
{
float min,max;
struct stu *p;
p = head;
printf("请输入所要打印分数段的最低分数:");
scanf("%f",&min);
printf("请输入所要打印分数段的最高分数:");
scanf("%f",&max);
printf("分数处于%g到%g的学生为:\n",min,max);
while(p != NULL)
{
if(min <= p->score[i] && max >= p->score[i])
printf("Number: %s Score: %g\n",p->num,p->score[i]);
p = p->next;
}
}
double average(struct stu *head,int i) //求平均值
{
int j = 0;
double avrg = 0;
struct stu *p;
p = head;
while(p != NULL)
{
avrg += p->score[i];
j++;
p = p->next;
}
return (avrg / j);
}
void file(struct stu *head) //将学生数据存入文件中
{
FILE *ff;
struct stu *pp;
pp = head;
if((ff = fopen("stu_list.text","w+")) == NULL)
{
printf("打开错误!");
getchar();
exit(1);
}
while(pp != NULL)
{
fprintf(ff,"Number:%s\n Chinese:%g Math:%g English:%g\n",pp->num,pp->score[0],pp->score[1],pp->score[2]);
pp = pp->next;
}
fclose(ff);
}
void main()
{
int n,s,x;
char m[10];
struct stu *head,*head1,*p,*chmax1,*mtmax1,*engmax1,*lookup1;
printf("*********************************\n");
printf(" 欢迎进入学生管理系统\n");
printf("*********************************\n");
printf("为创建一个学生系统请输入学生个数:\n"); //创建链表
scanf("%d",&n);
head = creat(n);
printf("\n您输入的学生情况如下:\n");
printlist(head);
file(head);
p = head;
printf("*********************************\n");
printf(" (1)排列顺序(按学号)\n");
printf(" (2)查找学生\n");
printf(" (3)插入学生\n");
printf(" (4)删除学生\n");
printf(" (5)某分数段的学生\n");
printf(" (6)平均成绩\n");
printf(" (7)某科的最高成绩\n");
printf(" (8)排列某个科目的成绩\n");
printf(" (9)打印学生信息\n");
printf(" (10)将学生数据存入stu_list.text\n");
printf("*********************************\n");
while(1)
{
head = p;
printf("请输入你想进行的功能:");
scanf("%d",&x);
switch(x)
{
case 1:
printf("按Number从大到小排列后\n");
head = paixu(head,1);
head1 = paixu(head,2);
printlist(head);
p = head;
file(head);break;
case 2:
lookup1 = lookup(head);
if(lookup1 != NULL)
printf("Number:%s\n Chinese:%g Math:%g English:%g\n",lookup1->num,lookup1->score[0],lookup1->score[1],lookup1->score[2]);
else
printf("该学生不存在!\n"); break;
case 3:
head = insert(head);
printlist(head);
p = head;
file(head);break;
case 4:
printf("请输入要删除的学生的Number:"); //输入要删除的学生num,并把对应链表删除
scanf("%s",&m);
head = dellist(head,m);
printlist(head);
p = head;
file(head);break;
case 5:
printf("按序号输入科目(1)Chinese(2)Math(3)English:");
scanf("%d",&s);
prtamong(head,s - 1);break;
case 6:
printf("按序号输入科目(1)Chinese(2)Math(3)English:");
scanf("%d",&s);
printf("平均成绩为:%g\n\n",average(head,s - 1));break;
case 7:
printf("按序号输入科目(1)Chinese(2)Math(3)English:");
scanf("%d",&s);
if(s == 1)
{
chmax1 = chmax(head);
printf("Number:%s ",chmax1->num);
printf("Chinese:%g\n",chmax1->score[0]);
}
if(s == 2)
{
mtmax1 = mtmax(head);
printf("Number:%s ",mtmax1->num);
printf("Math:%g\n",mtmax1->score[1]);
}
if(s == 3)
{
engmax1 = engmax(head);
printf("Number:%s ",engmax1->num);
printf("English:%g\n",engmax1->score[2]);
}break;
case 8:
printf("请输入要排序的科目:(1)Chinese(2)Math(3)English:");
scanf("%d",&s);
head = paixu2(head,1,s);
head1 = paixu2(head,2,s);
printlist(head);
p = head;
file(head);break;
case 9:
printlist(head);break;
case 10:
file(head);
printf("学生数据已经存入stu_list.text中\n");break;
default:exit(0);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?