自己编的学生成绩管理系统,欢迎大虾们前来测试
#include<iostream.h>#include<string.h>
#include<malloc.h>
#include<stdio.h>
#define LEN sizeof(student)
class student
{
public:
int ban;
int xue;
char name[20];
int cs;
int cm;
int ce;
int av;
student *next;
student(int b=0,int x=0,char n[40]="无名氏",int s=0,int m=0,int e=0)
{
ban=b;xue=x;strcpy(name,n);cs=s;cm=m;ce=e;av=(cs+cm+ce)/3;
}
};
student *head;
int n;
int shanchu(int x)
{
student *p4,*p5;
int i=0;
p4=head;
if(x==head->xue)
{
i=1;
head=p4->next;
cout<<"删除成功"<<endl;return 0;
}
else
while(p4->next!=0)
{
p5=p4;
p4=p4->next;
if(p4->xue==x)
{
n--;
i=1;
p5->next=p4->next;
cout<<"删除成功"<<endl;return 0;
}
}
if(i==0)
cout<<"没有这个学号"<<endl;return 1;
}
int chazhao(int x)
{
student *p3=head;
int i=0;
while(p3!=0)
{
if(x==p3->xue)
{i=1;
cout<<"姓名"<<p3->name<<" 班级"<<p3->ban<<" 学号"<<p3->xue<<" C语言"<<p3->cs<<" 高数"<<p3->cm<<" 英语"<<p3->ce<<" 平均分"<<p3->av<<endl;return 0;break;}
else p3=p3->next;
}
if(i==0)
cout<<"没有这个学号"<<endl;return 1;
}
void paixu(student *x)
{
int temp1,temp2,temp3,temp4,temp5,temp6;
char temp[20];
student *p,*q,*p8;
p=x;
while(p) //如果P存在
{
q=p->next; //临近的2个数据进行比较
while(q)
{
if(p->av<q->av) //对比
{
temp1=p->av; //先吧P的值放到temp里面
temp2=p->ban;
temp3=p->ce;
temp4=p->cm;
temp5=p->cs;
temp6=p->xue;
strcpy(temp,p->name);
p->av=q->av; //把Q的值赋值给P
p->ban=q->ban;
p->ce=q->ce;
p->cm=q->cm;
p->cs=q->cs;
p->xue=q->xue;
strcpy(p->name,q->name);
q->av=temp1; //把原来temp里面的只放到Q上
q->ban=temp2;
q->ce=temp3;
q->cm=temp4;
q->cs=temp5;
q->xue=temp6;
strcpy(q->name,temp);
}
q=q->next;
}
p=p->next;
} p8=x;
while(p8)
{
cout<<"姓名"<<p8->name<<" 班级"<<p8->ban<<" 学号"<<p8->xue<<" C语言"<<p8->cs<<" 高数"<<p8->cm<<" 英语"<<p8->ce<<" 平均分"<<p8->av<<endl;
p8=p8->next;
}
}
student *creat()
{
student *p1,*p2,*p3;
n=0;int h=1;
p1=p2=(student *)malloc(LEN);
cout<<"学号";cin>>p1->xue;
cout<<"姓名";cin>>p1->name;
cout<<"班级";cin>>p1->ban;
cout<<"C语言";cin>>p1->cs;
cout<<"高数";cin>>p1->cm;
cout<<"英语";cin>>p1->ce;
p1->next=0;
p1->av=(p1->cs+p1->cm+p1->ce)/3;
head=0;int k=0,t=0;
while(n<=9)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;p3=head;
p1=(student *)malloc(LEN);
while(p3)
{
t=0; p3=head;
cout<<"学号";cin>>t;
if(t==0) {k=1;break;}
while(p3)
{
if(t==p3->xue)
{
cout<<"学号被占用"<<endl;
break;
}
p3=p3->next;
}
}
if(k==1) break;
p3=head;
p1->xue=t;
cout<<"姓名";cin>>p1->name;
cout<<"班级";cin>>p1->ban;
cout<<"C语言";cin>>p1->cs;
cout<<"高数";cin>>p1->cm;
cout<<"英语";cin>>p1->ce;
p1->next=0;
p1->av=(p1->cs+p1->cm+p1->ce)/3;
}
p2->next=0;
return (head);
}
int rewrite(int x)
{
int i=0;
student *p1=head;
do
{
if(p1->xue==x)
{
i=1;
cout<<"姓名";cin>>p1->name;
cout<<"班级";cin>>p1->ban;
cout<<"C语言";cin>>p1->cs;
cout<<"高数";cin>>p1->cm;
cout<<"英语";cin>>p1->ce;
p1->av=(p1->cs+p1->cm+p1->ce)/3;return 1;break;
}
else
p1=p1->next;
}while(p1!=0);
if(i==0)
cout<<"没有这个学号"<<endl;return 0;
}
void exit()
{
cout<<"感谢使用";
}
void charu()
{ int wei;
student *p9,*p11,*p10=(student *)malloc(LEN);
p9=p11=head;int shu=0;
while(p11)
{
shu++;
p11=p11->next;
}
p11=head;
int k1=0;int k=0;
while(k1==0)
{
cout<<"插入范围(1—"<<shu<<')'<<endl;cout<<"请输入插入的位置"<<endl;
cin>>wei;
for(;wei-1>0;wei--)
{
if(wei==0||wei>shu)
{cout<<"非法输入"<<endl;k1=1;break;}
p9=p9->next;
}
if(k1==1)
k1=0;
else k1=1;
}
if(k==0)
{
p10->next=p9->next;
p9->next=p10;int t=0;
while(p11)
{ k=0;p11=head;
cout<<"学号";cin>>t;
while(p11)
{
if(p11->xue==t||p11->xue==0)
{
k=2;
cout<<"学号被占用"<<endl;
break;
}
p11=p11->next;
}
}
if(k==0)
{p10->xue=t;
cout<<"姓名";cin>>p10->name;
cout<<"班级";cin>>p10->ban;
cout<<"C语言";cin>>p10->cs;
cout<<"高数";cin>>p10->cm;
cout<<"英语";cin>>p10->ce;
p10->av=(p10->cs+p10->cm+p10->ce)/3;
}
}
}
void chakan()
{
student *p8;
p8=head;
while(p8)
{
cout<<"姓名"<<p8->name<<" 班级"<<p8->ban<<" 学号"<<p8->xue<<" C语言"<<p8->cs<<" 高数"<<p8->cm<<" 英语"<<p8->ce<<" 平均分"<<p8->av<<endl;
p8=p8->next;
}
}
void baocun()
{
FILE *fp; student *p=head;
if((fp=fopen("g:\\学生信息表.txt","w"))==0) cout<<"失败";
while(p)
{
fprintf(fp,"姓名%s,班级%d,学号%d,C语言%d,高数%d,英语%d,平均分%d\n",p->name,p->ban,p->xue,p->cs,p->cm,p->ce,p->av);
p=p->next;
}
fclose(fp);
cout<<"文件已保存在G:\\学生信息表\n";
}
student *duqu()
{
FILE *fp;
if((fp=fopen("g:\\学生信息表.txt","r+"))==0) cout<<"失败";
student *p1,*p2;
n=0;int h=1;
p1=p2=(student *)malloc(LEN);
fscanf(fp,"姓名%s,班级%d,学号%d,C语言%d,高数%d,英语%d,平均分%d\n",p1->name,p1->ban,p1->xue,p1->cs,p1->cm,p1->ce,p1->av);
p1->next=0;
head=0;
while(n<=9)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(student *)malloc(LEN);
if(fscanf(fp,"姓名%s,班级%d,学号%d,C语言%d,高数%d,英语%d,平均分%d\n",p1->name,p1->ban,p1->xue,p1->cs,p1->cm,p1->ce,p1->av)==EOF)
break;
}
p2->next=0;
fclose(fp);
return (head);
}
void main()
{
int cai=0;
int hao=0;
int z=0;
cout<<"*********学生成绩管理系统**********"<<endl;
cout<<" 制作者 石楠"<<endl<<endl;
while(1)
{
cout<<"1录入,2删除,3查找,4排序,5修改,6插入,7查看,8保存,9读取,10退出";cin>>cai;
if(cai==1)
{cout<<"学号输入0可终止录入"<<endl;creat();
z=1;
}
if(cai==10)
{exit();break;} if(cai==9) {cout<<"此功能完善中\n";}
if(cai!=1&&z==0)
cout<<"请先进行录入"<<endl;
if(z==1)
{
if(cai==2)
{
int k=1;
while(k==1)
{
cout<<"请输入要删除的学号";
int num1;
cin>>num1;
k=shanchu(num1);
}
}
if(cai==3)
{
int k=1;
while(k==1)
{
cout<<"请输入要查询的学号";
int num;
cin>>num;
k=chazhao(num);
}
}
if(cai==4)
paixu(head);
if(cai==5)
{
int xiu;int k=0;
while(k==0)
{
cout<<"请输入要修改的学号"<<endl;
cin>>xiu;
k=rewrite(xiu);
}
}
if(cai==6)
{
charu();
}
if(cai==7)
chakan();
if(cai==8)
{
baocun();
}
}
}
}