📄 p-7.cpp
字号:
#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 delet();
int select();
int modify();
int display();
int total();
void savefile();
void quit();
};
void grade::init()
{
cout<<"************************************主菜单************************************\n"<<endl;
cout<<"\t1输入学生资料\t\t\t\t\t2删除学生资料\n"<<endl;
cout<<"\t3查询学生资料\t\t\t\t\t4修改学生资料\n"<<endl;
cout<<"\t5显示学生资料\t\t\t\t\t6统计学生成绩\n"<<endl;
cout<<"\t7保存学生资料\t\t\t\t\t0退出系统\n"<<endl;
cout<<"******************************************************************************\n"<<endl;
head=last=NULL;
}
void grade::insert()
{
struct student * p;
//p=(struct student *)malloc(sizeof(struct student));
p = new 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::delet()
{
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_num++;
p=p->next;
}
cout<<"the high score:"<<max<<endl;
cout<<"不及格学生人数:"<<jg_num<<endl;
return 0;
}
void grade::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 Grade;
Grade.init();
while(1)
{
cin>>choice;
switch(choice)
{
case 1:
Grade.insert();
break;
case 2:
Grade.delet();
break;
case 3:
Grade.select();
break;
case 4:
Grade.modify();
break;
case 5:
Grade.display();
break;
case 6:Grade.total();
break;
case 7:
Grade.savefile();
break;
case 0:
return 0;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -