# include<stdio.h>
# include<stdlib.h>
# include<windows.h>
typedef struct student{
int sno;
char name[20];
char sex[5];
int Chinese;
int math;
int English;
int score;
struct student *pNext;
}STU,*Pstu;
void welcome(void);
Pstu creatList(void);
int count(Pstu pHead);
int checkSno(int sno,Pstu pHead);
Pstu readFile(Pstu L);
void add(Pstu pHead);
void SaveFile(Pstu pHead);
void alter(Pstu pHead);
void seekScore(Pstu pHead);
void arrange(Pstu pHead);
void fileCaller(void);
void printfAll(Pstu pHead);
int main(void)
{
int t;
system("color F0");
Pstu pHead = creatList();
readFile(pHead);
while(1){
welcome();
printf("请输入操作选项:");
scanf("%d",&t);
switch(t)
{
case 1:system("cls");add(pHead);break;
case 2:system("cls");alter(pHead);break;
case 3:system("cls");seekScore(pHead);break;
case 4:system("cls");arrange(pHead);break;
case 5:system("cls");fileCaller();break;
case 6:system("cls");printfAll(pHead);break;
case 7:system("cls");SaveFile(pHead);break;
case 0:exit(-1);break;
default:printf("输入有误,请重新检查!");
}
}
return 0;
}
void welcome(void){
printf(" ________________________________________________\n");
printf(" | |\n");
printf(" | 学生信息管理系统 |\n");
printf(" | |\n");
printf(" | 1、录入学生信息 |\n");
printf(" | 2、修改学生信息 |\n");
printf(" | 3、查找学生的信息 |\n");
printf(" | 4、排列学生资料 |\n");
printf(" | 5、调出文件资料 |\n");
printf(" | 6、浏览全部学生信息 |\n");
printf(" | 7、保存学生信息到文件 |\n");
printf(" | 0、退出系统 |\n");
printf(" | |\n");
printf(" |________________________________________________|\n");
return ;
}
Pstu creatList(void){
Pstu Head = (Pstu)malloc(sizeof(STU));
Pstu pTail = Head;
pTail->pNext = NULL;
return Head;
}
int count(Pstu pHead){
int N=0;
Pstu p = pHead->pNext;
while(p){
p = p->pNext;
N++;
}
return N;
}
int checkSno(int sno,Pstu pHead){
Pstu p = pHead->pNext;
for(p;p;p=p->pNext){
if(!(p->sno-sno)){
printf("学号为%d的学生已经存在!",sno);
return 0;
}
}
return 1;
}
Pstu readFile(Pstu L){
Pstu P = L;
FILE *fp;
fp = fopen("student.txt","a+");
char a[20];
char b[20];
int q,w,e,r;
int N ;
fscanf(fp,"%d",&N);
for(int i=0; i<N; ++i){
Pstu pNew = (Pstu)malloc(sizeof(STU));
pNew->pNext = NULL;
P->pNext = pNew;
fscanf(fp,"%d %s %s %d %d %d %d", &pNew->sno,pNew->name,pNew->sex,&pNew->Chinese,&pNew->math,&pNew->English,&pNew->score);
P = P->pNext;
}
fclose(fp);
}
void add(Pstu pHead){
Pstu p = pHead;
Pstu q;
while(p){
q = p;
p = p->pNext;
}
int flag = 1;
while(flag){
Pstu pNew = (Pstu)malloc(sizeof(STU));
pNew->pNext = NULL;
puts("请输入学生学号:");
int temp;
scanf("%d",&temp);
while(!checkSno(temp,pHead)){
puts("请输入学生学号:");
scanf("%d",&temp);
}
pNew->sno = temp;
puts("请输入学生姓名:");scanf("%s",&pNew->name);
puts("请输入学生性别:");scanf("%s",&pNew->sex);
puts("请输入学生语文成绩:");scanf("%d",&pNew->Chinese);
puts("请输入学生数学成绩:");scanf("%d",&pNew->math);
puts("请输入学生英语成绩:");scanf("%d",&pNew->English);
pNew->score = pNew->Chinese + pNew->math + pNew->English;
q->pNext = pNew;
q = pNew;
puts("是否继续录入学生信息?(输入1继续录入,输入0结束录入)");
scanf("%d",&flag);
}
}
void SaveFile(Pstu pHead){
FILE * fp;
fp = fopen("student.txt","w");
Pstu p = pHead->pNext;
int N = count(pHead);
fprintf(fp,"%d ",N);
while(p){
fprintf(fp,"%d %s %s %d %d %d %d ",p->sno,p->name,p->sex,p->Chinese,p->math,p->English, p->Chinese + p->English + p->math);
p = p->pNext;
}
fclose(fp);
}
void alter(Pstu pHead){
printf(" 请输入操作选项(1\\2):\n");
printf(" ____________________\n");
printf(" | |\n");
printf(" | 1、修改学生信息 |\n");
printf(" | 2、删除学生 |\n");
printf(" | |\n");
printf(" |____________________|\n");
int t;
scanf("%d",&t);
switch(t)
{
int sno;
case 1:{
puts("输入学生学号sno:");
scanf("%d",&sno);
Pstu p = pHead->pNext;
int flag =0;
for(p; p; p = p->pNext){
if(p->sno == sno){
flag = 1;
break;
}
}
if(!flag){
printf("没有学号为%d的学生\n",sno);
return;
}
int flag2=1;
while(flag2){
puts("选择要修改的数据:\n");
puts("1、姓名 2、性别\n");
puts("3、语文成绩 4、数学成绩 5、英语成绩\n");
int temp;
scanf("%d",&temp);
switch(temp)
{
case 1:puts("将数据修改为:");
while(!checkSno(temp,pHead)){
puts("请输入学生学号:");
scanf("%d",&temp);
break;
}
case 2:puts("将数据修改为:");scanf("%s",&p->sex);break;
case 3:{
puts("将数据修改为:");
int temp=p->Chinese;
scanf("%d",&p->Chinese);
p->score=p->score-temp+p->Chinese;
break;
}
case 4:{
puts("将数据修改为:");
int temp=p->math;
scanf("%d",&p->math);
p->score=p->score-temp+p->math;
break;
}
case 5:{
puts("将数据修改为:");
int temp=p->English;
scanf("%d",&p->English);
p->score=p->score-temp+p->English;
break;
}
}
puts("是否继续修改?1、继续修改 0、结束修改");
scanf("%d",&flag2);
}
break;
}
case 2:
{
puts("输入要删除的学生学号sno:");
scanf("%d",&sno);
Pstu p = pHead->pNext;
Pstu q;
int flag =0;
for(p; p; q=p, p=p->pNext){
if(p->sno == sno){
flag = 1;
break;
}
}
if(!flag){
printf("没有学号为%d的学生\n",sno);
return;
}
q->pNext = p->pNext;
free(p);
}
}
}
void seekScore(Pstu pHead){
puts("输入想要查询学生成绩的学号:");
int sno;
scanf("%d",&sno);
Pstu p = pHead->pNext;
int flag =0;
for(p; p; p = p->pNext){
if(p->sno == sno){
flag = 1;
break;
}
}
if(!flag){
printf("没有学号为%d的学生\n",sno);
return;
}
printf("学号:%d\n姓名:%s\n语文成绩:%d\n数学成绩:%d\n英语成绩:%d\n总分:%d\n",p->sno,p->name,p->Chinese,p->math,p->English,p->score);
}
void arrange(Pstu pHead){
int t;
puts("1、按学号由小到大排列\n2、按总分由高到低排列\n");
scanf("%d",&t) ;
switch(t)
{
case 1:{
if(pHead->pNext==NULL||pHead->pNext->pNext==NULL)
{
return;
}
Pstu pre,p,tail,temp;
tail = NULL;
pre = pHead;
while(pHead->pNext->pNext!=tail){
p = pHead->pNext;
pre = pHead;
while(p->pNext!=tail){
if(p->sno>p->pNext->sno){
pre->pNext = p->pNext;
temp = p->pNext->pNext;
p->pNext->pNext = p;
p->pNext = temp;
p = pre->pNext;
}
p = p->pNext;
pre = pre->pNext;
}
tail = p;
}
}
case 2:{
if(pHead->pNext==NULL||pHead->pNext->pNext==NULL)
{
return;
}
Pstu pre,p,tail,temp;
tail = NULL;
pre = pHead;
while(pHead->pNext->pNext!=tail){
p = pHead->pNext;
pre = pHead;
while(p->pNext!=tail){
if(p->score<p->pNext->score){
pre->pNext = p->pNext;
temp = p->pNext->pNext;
p->pNext->pNext = p;
p->pNext = temp;
p = pre->pNext;
}
p = p->pNext;
pre = pre->pNext;
}
tail = p;
}
}
}
}
void fileCaller(void){
FILE *fp = fopen("student.txt","r");
char c;
while(!feof(fp)){
fscanf(fp,"%c",&c);
printf("%c",c);
}
printf("\n");
fclose(fp);
printf("文件已全部保存!\n");
}
void printfAll(Pstu pHead){
Pstu p = pHead->pNext;
while(p){
printf("学号:%2d 姓名:%5s 性别:%2s\n语文成绩:%3d 英语成绩:%3d 数学成绩:%3d \n总分:%d\n\n",p->sno,p->name,p->sex,p->Chinese,p->math,p->English,p->score);
p = p->pNext;
}
}