#include <iostream.h> #include <string.h> #include <fstream.h> struct student { char num[10]; char name[20]; int age; int score; struct student *next; }; class grade { private : struct student * head,* last; public : void init(); void insert(); int delete(); int select(); int modify(); int display(); int total(); void savefile(); void quit(); }; void grade::init() { printf("************************************主菜单************************************\n"); printf("\t1输入学生资料\t\t\t\t\t2删除学生资料\n"); printf("\t3查询学生资料\t\t\t\t\t4修改学生资料\n"); printf("\t5显示学生资料\t\t\t\t\t6统计学生成绩\n"); printf("\t7保存学生资料\t\t\t\t\t0退出系统\n"); printf("******************************************************************************\n"); head=last=NULL; } void grade::insert() { struct student * p; p=(struct student *)malloc(sizeof(struct student)); cout<<"please insert num,name,age,score"<<endl; cin>>p->num>>p->name>>p->age>>p->score; if (head==NULL) {head=p;last=p;last->next=NULL;} else { last->next=p; last=p; last->next=NULL; } } int grade::delete() { char dnum;/* 要删除的学号*/ struct student * p,p1; cout<<"请你输入要删除的学号:"<<endl; cin>>dnum; if (head==NULL) {cout<<"no student can be found"<<endl;return 0;} if (head->next=NULL && strcpy(head->num,dnum)==0)/* 只有一个记录且是要删除的数据*/ {head=NULL;last=NULL;return 0;} p=head; while(p->next!=NULL) { p1=p->next; if (strcpy(p->num,dnum)==0 && p1->next==NULL )/* 数据在到数第二的位置*/ {p->next=last;cout<<"one student has been deleted"<<endl;return 0;} else if (p1==NULL && strcpy(p1->num,dnum)==0)/* 数据在到数第一的位置*/ {p->next=NULL;last=p;cout<<"one student has been deleted"<<endl;return 0;} else if (strcpy(p->num,dnum)==0 && p1->next!=NULL ) {p->next=p1->next;p=p1;/*p=p1;等一下次执行p=p->next;实际p已经指到p1->next了*/ cout<<"one student has been deleted"<<endl;return 0;} p=p->next; } cout<<"no student can be found"<<endl;return 0; } int grade::select() { char snum;/* 要查询的学号*/ struct student * p; cout<<"请你输入要查询的学号:"<<endl; cin>>snum; p=head; while(p!=NULL) { if (strcpy(p->num,snum)==0) { cout<<"num:"<<p->num<<"\nname"<<p->name<<"\nage"<<p->age<<"\nscore"<<p->score<<endl; return 0; } p=p->next; } cout<<"no student can be found"<<endl;return 0; } int grade::modify() { char mnum;/* 要修改的学号*/ struct student *p; cout<<"请你输入要修改的学号:"<<endl; cin>>mnum; p=head; while(p!=NULL) { if (strcpy(p->num,mnum)==0) { cout<<"please insert the new name,age,score"<<endl; cin>>p->name>>p->age>>p->score; cout<<"one student has been modify"<<endl; return 0; } p=p->next; } cout<<"no student can be found"<<endl;return 0; } int grade::display() { struct student *p; p=head; if (head==NULL) {cout<<"no student can be found"<<endl;return 0;} while(p!=NULL) { cout<<"num:"<<p->num<<"\nname"<<p->name<<"\nage"<<p->age<<"\nscore"<<p->score<<endl; p=p->next; } return 0; } int grade::total() { int max,jg_num=0; struct student *p; p=head; if (head==NULL) {cout<<"no student can be found"<<endl;return 0;} max=p->score; while(p!=NULL) { if (p->score>max) max=p->score; if (p->score<60) jg++; p=p->next; } cout<<"the high score:"<<max<<endl; cout<<"不及格学生人数:"<<jg<<endl; return 0; } void savefile() { struct student * p; ifstream input; input.open("student"); p=head; while(p!=head) { input>>p->num>>p->name>>p->age>>p->score; p=p->next; } cout<<"the file save over"<<endl; } int main() { int choice; grade Grage; Grade.init(); while(1) { cin>>choice; switch(choice) { case 1:void Grade.insert(); break; case 2:int Grade.delete(); break; case 3:int Grade.select(); break; case 4:int Grade.modify(); break; case 5:int Grade.display();break; case 6:int Grade.total(); break; case 7:int Grade.savefile();break; case 0: return 0; } } return 0; }

评论