代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<malloc.h>
using namespace std;
struct node{
char str[50];
int stu_num;
char sex;
int math,chinese,english,pe,c;
int sum_score;
double aver_score;
int num;
struct node *next;
}*h;
void surface(){
int i;
printf(" ");
for(i=1;i<=47;i++)
printf("%c",4);
printf("\n");
printf(" |%c%c%c%c 学生信息管理系统 %c%c%c%c|\n",3,4,5,6,3,4,5,6);
printf(" |%c 1.增加学生信息 6.对学生信息进行排序 %c|\n",5,5);
printf(" |%c 2.修改学生信息 7.保存学生信息至记录文件 %c|\n",5,5);
printf(" |%c 3.显示学生信息 8.从纪录文件读取学生信息 %c|\n",5,5);
printf(" |%c 4.查询学生信息 9.新建学生信息文件 %c|\n",5,5);
printf(" |%c 5.删除学生信息 10.运行结束 %c|\n",5,5);
printf(" ");
for(i=1;i<=47;i++)
printf("%c",4);
printf("\n");
printf(" ------Lsh\n");
}
void swapp(struct node *a,struct node *b){
swap(a->str,b->str);
swap(a->stu_num,b->stu_num);
swap(a->sex,b->sex);
swap(a->math,b->math);
swap(a->chinese,b->chinese);
swap(a->english,b->english);
swap(a->pe,b->pe);
swap(a->c,b->c);
swap(a->sum_score,b->sum_score);
swap(a->aver_score,b->aver_score);
swap(a->num,b->num);
}
void sort_score1(){
struct node *p,*p1;
int flag=1;
for(p=h;p!=NULL;p=p->next)
for(p1=p;p1!=NULL;p1=p1->next)
if(p1->sum_score>p->sum_score)
swapp(p1,p);
for(p=h;p!=NULL;p=p->next)
p->num=flag++;
}
void sort_score2(){
struct node *p,*p1;
int flag=1;
for(p=h;p!=NULL;p=p->next)
for(p1=p;p1!=NULL;p1=p1->next)
if(p1->sum_score<p->sum_score)
swapp(p1,p);
for(p=h;p!=NULL;p=p->next)
p->num=flag++;
}
void sort_score3(){
struct node *p,*p1;
int flag=1;
for(p=h;p!=NULL;p=p->next)
for(p1=p;p1!=NULL;p1=p1->next)
if(p1->stu_num>p->stu_num)
swapp(p1,p);
for(p=h;p!=NULL;p=p->next)
p->num=flag++;
}
void sort_score4(){
struct node *p,*p1;
int flag=1;
for(p=h;p!=NULL;p=p->next)
for(p1=p;p1!=NULL;p1=p1->next)
if(p1->stu_num<p->stu_num)
swapp(p1,p);
for(p=h;p!=NULL;p=p->next)
p->num=flag++;
}
void sort_score5(){
struct node *p,*p1;
int flag=1;
for(p=h;p!=NULL;p=p->next)
for(p1=p;p1!=NULL;p1=p1->next)
if(strcmp(p1->str,p->str)>0)
swapp(p1,p);
for(p=h;p!=NULL;p=p->next)
p->num=flag++;
}
void sort_score6(){
struct node *p,*p1;
int flag=1;
for(p=h;p!=NULL;p=p->next)
for(p1=p;p1!=NULL;p1=p1->next)
if(strcmp(p1->str,p->str)<0)
swapp(p1,p);
for(p=h;p!=NULL;p=p->next)
p->num=flag++;
}
void create(){
struct node *p1,*p2;
int i,flag=1;
p1=(struct node *)malloc(sizeof(struct node));
p1->next=NULL;
h=NULL;
printf("请输入学生信息,输入#以表示全部建立完成:\n");
printf("例如:lishuhao 2014214011 m 100 100 100 100 100 m:男;f:女\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
cin>>p1->str;
if(p1->str[0]=='#'){
printf("%c%c%c没有学生信息,无法建立.%c%c%c\n",1,1,1,1,1,1);
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
return;
}
cin>>p1->stu_num>>p1->sex;
cin>>p1->math>>p1->chinese>>p1->english>>p1->pe>>p1->c;
p1->sum_score=p1->math+p1->chinese+p1->english+p1->pe+p1->c;
p1->aver_score=p1->sum_score*1.0/5;
h=p1;
p2=p1;
p1=(struct node *)malloc(sizeof(struct node));
p1->next=NULL;
cin>>p1->str;
if(p1->str[0]=='#'){
printf("%c%c%c建立成功%c%c%c\n",1,1,1,1,1,1);
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
return;
}
p2->next=p1;
while(p1->str[0]!='#'){
cin>>p1->stu_num>>p1->sex;
cin>>p1->math>>p1->chinese>>p1->english>>p1->pe>>p1->c;
p1->sum_score=p1->math+p1->chinese+p1->english+p1->pe+p1->c;
p1->aver_score=p1->sum_score*1.0/5;
p2=p1;
p1=(struct node *)malloc(sizeof(struct node));
p1->next=NULL;
p2->next=p1;
cin>>p1->str;
if(p1->str[0]=='#')
p2->next=NULL;
// printf("请输入学生信息,输入#以表示全部建立完成:\n");
}
printf("%c%c%c建立成功%c%c%c\n",1,1,1,1,1,1);
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
sort_score1();
}
void add(){
struct node *p1,*p2,*p3;
int flag=1,i;
for(p1=h;p1!=NULL;p1=p1->next)
p2=p1;
p3=(struct node *)malloc(sizeof(struct node));
p2->next=p3;
cin>>p3->str;
cin>>p3->stu_num>>p3->sex;
cin>>p3->math>>p3->chinese>>p3->english>>p3->pe>>p3->c;
p3->sum_score=p3->math+p3->chinese+p3->english+p3->pe+p3->c;
p3->aver_score=p3->sum_score*1.0/5;
p3->next=NULL;
sort_score1();
printf("添加成功!!!");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
}
void change(int n){
struct node *p;
int sign=0,flag=1,i;
for(p=h;p!=NULL;p=p->next){
if(p->stu_num==n){
sign=1;
printf("输入想要修改同学的资料:(例如:lishuhao 2014214011 m 100 100 100 100 100 m:男;f:女)\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
cin>>p->str>>p->stu_num>>p->sex;
cin>>p->math>>p->chinese>>p->english>>p->pe>>p->c;
p->sum_score=p->math+p->chinese+p->english+p->pe+p->c;
p->aver_score=p->sum_score*1.0/5;
}
}
if(!sign)
printf("%c%c查无此人%c%c\n",4,4,4,4);
else
printf("修改成功!!!\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
sort_score1();
}
void display(){
struct node *p;
int i;
for(p=h;p!=NULL;p=p->next){
printf("姓名:%s 学号:%d 性别:%c\n",p->str,p->stu_num,p->sex);
printf("数学:%d 语文:%d 英语:%d 体育:%d C语言:%d\n",p->math,p->chinese,p->english,p->pe,p->c);
printf("总成绩:%d 平均成绩:%.2lf 排名:%d\n",p->sum_score,p->aver_score,p->num);
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
}
}
void search_num(int n){
struct node *p;
int sign=0,i;
for(p=h;p!=NULL;p=p->next){
if(p->stu_num==n){
sign=1;
printf("姓名:%s 学号:%d 性别:%c\n",p->str,p->stu_num,p->sex);
printf("数学:%d 语文:%d 英语:%d 体育:%d C语言:%d\n",p->math,p->chinese,p->english,p->pe,p->c);
printf("总成绩:%d 平均成绩:%lf 排名:%d\n",p->sum_score,p->aver_score,p->num);
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
}
}
if(!sign){
printf("查无此人\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
}
}
void search_name(char s[]){
struct node *p;
int sign=0,i;
for(p=h;p!=NULL;p=p->next){
if(strcmp(p->str,s)==0){
sign=1;
printf("姓名:%s 学号:%d 性别:%c\n",p->str,p->stu_num,p->sex);
printf("数学:%d 语文:%d 英语:%d 体育:%d C语言:%d\n",p->math,p->chinese,p->english,p->pe,p->c);
printf("总成绩:%d 平均成绩:%lf 排名:%d\n",p->sum_score,p->aver_score,p->num);
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
}
}
if(!sign){
printf("查无此人\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
}
}
void search_score(int n){
struct node *p;
int sign=0,i;
for(p=h;p!=NULL;p=p->next){
if(p->sum_score==n){
sign=1;
printf("姓名:%s 学号:%d 性别:%c\n",p->str,p->stu_num,p->sex);
printf("数学:%d 语文:%d 英语:%d 体育:%d C语言:%d\n",p->math,p->chinese,p->english,p->pe,p->c);
printf("总成绩:%d 平均成绩:%.2lf 排名:%d\n",p->sum_score,p->aver_score,p->num);
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
}
}
if(!sign){
printf("查无此人!!!\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
}
}
void del(int n){
struct node *p1,*p2;
int flag=1,i;
p1=h;p2=h->next;
if(h->stu_num==n){
h=p2;
return;
}
while(p1->next!=NULL){
p2=p1;p1=p1->next;
if(p1->stu_num==n){
flag=0;
p2->next=p1->next;
break;
}
}
if(flag)
printf("查无此人!!!\n");
else
printf("删除成功!!!\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
flag=1;
sort_score1();
}
void save(char s[]){
struct node *p;
FILE *fp;
fp=fopen(s,"w");
p=h;
while(1){
fprintf(fp,"%s %d %c %d %d %d %d %d %d %lf %d\n",p->str,p->stu_num,p->sex,p->math,p->chinese,p->english,p->pe,p->c,p->sum_score,p->aver_score,p->num);
if(p->next==NULL)
break;
p=p->next;
}
fclose(fp);
}
void read(char s[]){
FILE *fp;
struct node *p1,*p2;
fp=fopen(s,"r");
h=p2=(struct node *)malloc(sizeof(struct node));
fscanf(fp,"%s %d %c %d %d %d %d %d %d %lf %d",p2->str,&p2->stu_num,&p2->sex,&p2->math,&p2->chinese,&p2->english,&p2->pe,&p2->c,&p2->sum_score,&p2->aver_score,&p2->num);
while(!feof(fp)){
p1=(struct node *)malloc(sizeof(struct node));
fscanf(fp,"%s %d %c %d %d %d %d %d %d %lf %d",p1->str,&p1->stu_num,&p1->sex,&p1->math,&p1->chinese,&p1->english,&p1->pe,&p1->c,&p1->sum_score,&p1->aver_score,&p1->num);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
fclose(fp);
}
int main()
{
int i,num,num1,num2;
struct node *p;
char s[50];
surface();
while(1){
printf("请输入1——10中某个数字实现其对应功能:\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
cin>>num;
if(num==10){
printf("%c%c%c感谢您的使用!%c%c%c\n",1,1,1,1,1,1);
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
break;
}
if(num<=0||num>10){
printf("x_x请按正确格式输入x_x\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
continue;
}
if(num==1){
printf("请输入想要增加学生的学生信息,注意每次只能添加一人!!!\n");
printf("输入想要修改同学的资料:(例如:lishuhao 2014214011 m 100 100 100 100 100)\n");
printf("请输入:\n");
add();
}
if(num==2){
printf("请输入想要修改同学的学号:\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
cin>>num1;
change(num1);
}
if(num==3){
display();
}
if(num==4){
printf("1.通过学号查询 2.通过姓名查询\n");
printf("3.通过总分查询 4.通过排名查询\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
printf("请选择查询方式:\n");
cin>>num1;
if(num1==1){
printf("输入查询同学学号:\n");
cin>>num2;
search_num(num2);
}
if(num1==2){
printf("输入查询同学姓名:\n");
cin>>s;
search_name(s);
}
if(num1==3){
printf("输入查询同学总分:\n");
cin>>num2;
search_score(num2);
}
}
if(num==5){
printf("输入想要删除学生的学号:\n");
cin>>num1;
del(num1);
}
if(num==6){
int sort_type;
printf("1.依据总分从大到小排序 2.依据总分从小到大排序\n");
printf("3.依据学号从大到小排序 4.依据学号从小到大排序\n");
printf("5.依据姓名从大到小排序 6.依据姓名从小到大排序\n");
printf("请选择想要排序类型:\n");
cin>>sort_type;
if(sort_type==1)
sort_score1();
if(sort_type==2)
sort_score2();
if(sort_type==3)
sort_score3();
if(sort_type==4)
sort_score4();
if(sort_type==5)
sort_score5();
if(sort_type==6)
sort_score6();
printf("排序成功!!!\n");
for(i=1;i<=37;i++)
printf("%c",4);
printf("\n");
}
if(num==7){
printf("请输入文件存储位置:");
cin>>s;
save(s);
printf("保存成功!!!");
}
if(num==8){
printf("输入想要读取文件的地址:\n");
cin>>s;
read(s);
for(p=h;p!=NULL;p=p->next){
printf("姓名:%s 学号:%d 性别:%c\n",p->str,p->stu_num,p->sex);
printf("数学:%d 语文:%d 英语:%d 体育:%d C语言:%d\n",p->math,p->chinese,p->english,p->pe,p->c);
printf("总成绩:%d 平均成绩:%.2lf 排名:%d\n",p->sum_score,p->aver_score,p->num);
}
}
if(num==9)
create();
}
return 0;
}