文章目录
(一)
新生基本信息统计软件
逐个录入学生信息,如:
学生姓名,性别,专业,出生日期,家庭地址,英语入学成绩。要求设计链表类来实现,并统计学生人数。
文本界面为:
- 新增学生信息
- 删除学生信息
- 导入学生信息(已经保存于的文件信息)
- 学生信息搜索(按姓名)
- 学生信息统计(按专业或性别或年龄—年龄要自动计算)
- 按英语成绩排序
- 学生信息保存 ( 文件 )
-
退出
******请选择:1
1) 案例一
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class Student
{
public:
int num;
char name[20];
char sex;
char address[100];
int english;
int born;
char major [20];
struct Student *next;
};
//创建头结点
/*S *head()
{
S *head=new S;
head->next=NULL;
return head;
}*/
//定义学生类
class Students
{
public:
Students()
{
head=new Student;
head->next=NULL;
}
void create();//创建链表
void edit(); //编辑函数
void search();//显示函数
void add(); //添加函数
void delet(); //删除函数
void sort();//排序函数
void show(); //查询函数
void sum(); //统计函数
void score();//成绩函数
void read(); //读取函数
void write(); //存储函数
private:
Student *head;
};
//创建函数的定义
void Students::create()
{
Student *p=head;
Student *q;
q=new Student;
cout<<"请输入学号:";
cin>>q->num;
cout<<"请输入姓名"<<endl;
cin>>q->name;
cout<<"请输入性别"<<endl;
cin>>q->sex;
cout<<"请输入专业";
cin>>q->major;
cout<<"请输入出生年份";
cin>>q->born;
cout<<"请输入家庭地址";
cin>>q->address;
cout<<"请输入英语成绩";
cin>>q->english;
q->next=NULL;
p->next=q;
p=q;
}
//排序函数的定义
void Students::sort()
{ int b=1;
if(head->next==NULL)
{
b=0;
}
if(b==0)
{
cout<<"当前无数据"<<endl;
}
if(b==1)
{
Student *p,*q;
int num1;
char name1[20];
char sex1;
char address1[20];
int english1;
int born1;
char major1[20];
int num2;
char name2[20];
char sex2;
char address2[20];
int english2;
int born2;
char major2[20];
for(p=head->next; NULL != p; p=p->next)
{
for(q=p->next; NULL !=q; q=q->next)
{
if(p->english < q->english)//当前一个学生的总分小于后一个学生的总分时,交换单链表的数据
{
num1=p->num;
strcpy(name1,p->name);
sex1=p->sex;
strcpy(address1,p->address);
english1=p->english;
born1=p->born;
strcpy(major1,p->major);
num2=q->num;
strcpy(name2,q->name);
sex2=q->sex;
strcpy(address2,q->address);
english2=q->english;
born2=q->born;
strcpy(major2,q->major);
p->num=num2;
strcpy(p->name,name2);
p->sex=sex2;
strcpy(p->address,address2);
p->english=english2;
p->born=born2;
strcpy(p->major,major2);
q->num=num1;
strcpy( q->name,name1);
q->sex=sex1;
strcpy(q->address,address1);
q->english=english1;
q->born=born1;
strcpy(q->major,major1);
}
}
}
cout<<"排序完成"<<endl;
}
}
//显示函数
void Students::search()
{
Student *p=head->next;
int b=1;
if(p==NULL)
{
b=0;
}
if(b==0)
{
cout<<"当前无数据"<<endl;
}
if(b==1)
{
while(p)
{ cout<<"学生学号";
cout<<p->num<<endl;
cout<<"学生姓名";
cout<<p->name<<endl;
cout<<"学生性别";
cout<<p->sex<<endl;
cout<<"学生专业";
cout<<p->major<<endl;
cout<<"出生年份";
cout<<p->born<<endl;
cout<<"家庭地址";
cout<<p->address<<endl;
cout<<"英语成绩";
cout<<p->english<<endl;
p=p->next;
}
}
}
//增加函数的定义
void Students::add()
{
Student *p=head->next;
Student *q;
int m=1;
int n,b=1;
//统计学生数
while(p->next!=NULL)
{ p=p->next;
m++;
}
//cout<<"M="<<m;
cout<<"请输入学号:";
cin>>n;
p=head->next;
//判断学号是否重复
for(int i=1;i<=m;i++)
{
if(p->num==n)
{
cout<<"编号重复"<<endl;
b=0;
break;
}
p=p->next;
}
//cout<<b;
if(b!=0)
{
p=head;
while(p->next!=NULL)
p=p->next;
q=new Student;
q->num=n;
cout<<"请输入姓名"<<endl;
cin>>q->name;
cout<<"请输入性别"<<endl;
cin>>q->sex;
cout<<"请输入专业";
cin>>q->major;
cout<<"请输入出生年份";
cin>>q->born;
cout<<"请输入家庭地址";
cin>>q->address;
cout<<"请输入英语成绩";
cin>>q->english;
q->next=NULL;
p->next=q;
p=q;
}
}
//查询函数的定义
void Students::show()
{ int b=0;
Student *p=head->next;
int a;
cout<<"*****************************"<<endl;
cout<<"****** 1 输入学生学号查询:**"<<endl;
cout<<"****** 2 输入学生姓名查询:**"<<endl;
cout<<"*****************************"<<endl;
cin>>a;
switch(a)
{
case 1:
int i;
cout<<"请输入学生学号";
cin>>i;
while(p!=NULL)
{
if(p->num==i)
{
cout<<"学生姓名";
cout<<p->name<<endl;
cout<<"学生性别";
cout<<p->sex<<endl;
cout<<"学生专业";
cout<<p->major<<endl;
cout<<"出生年份";
cout<<p->born<<endl;
cout<<"家庭地址";
cout<<p->address<<endl;
cout<<"英语成绩";
cout<<p->english<<endl;
b++;
}
p=p->next;
} if(b==0)
cout<<"该编号不存在";
break;
case 2:
char make[20];
cout<<"请输入学生姓名";
cin>>make;
while(p!=NULL)
{
if(strcmp(p->name,make)==0)
{
cout<<"学生姓名";
cout<<p->name<<endl;
cout<<"学生性别";
cout<<p->sex<<endl;
cout<<"学生专业";
cout<<p->major<<endl;
cout<<"出生年份";
cout<<p->born<<endl;
cout<<"家庭地址";
cout<<p->address<<endl;
cout<<"英语成绩";
cout<<p->english<<endl;
b++;
}
p=p->next;
}
if(b==0)
cout<<"该学生不存在";
break;
default:
cout<<" 请重新输入";
}
}
//统计函数的定义
void Students::sum()
{ int a;
Student *p=head;
if(p->next==NULL)
{
cout<<"当前无数据"<<endl;
a=0;
}
if(a!=0)
{
p=head->next;
int i=0;
int j=0;
int n,s=0,d=0,m;
int w=0,x=0;
cout<<"**********************"<<endl;
cout<<"*****1统计性别********"<<endl;
cout<<"*****2统计年龄********"<<endl;
cout<<"*****3统计专业********"<<endl;
cout<<"**********************"<<endl;
int a;
cin>>a;
switch(a)
{
case 1:
while(p)
{
if(p->sex=='m')
i++;
else
j++;
p=p->next;
}
cout<<"男生:"<<i<<endl;
cout<<"女生:"<<j<<endl;
break;
case 2:
while(p)
{
n=p->born;
m=2017-n;
if(m==18)
s++;
else
d++;
p=p->next;
}
cout<<"18岁:"<<s<<endl;
cout<<"19岁:"<<d<<endl;
break;
case 3:
while(p)
{ m=strcmp(p->major,"software");
if(m==0)
w++;
else
x++;
p=p->next;
}
cout<<"软件专业:"<<w<<endl;
cout<<"化学专业:"<<x<<endl;
break;
default:
cout<<" 请重新输入";
}
}
}
//删除函数的定义
void Students::delet()
{
Student *p=head;
Student *q;
int a;
if(p->next==NULL)
{
cout<<"当前无数据"<<endl;
a=0;
}
//cout<<a;
if(a!=0)
{
cout<<"请输入删除学生学号";
int i;
cin>>i;
while(p->next != NULL)
{
if(p->next->num==i)
{
q = p->next;
p->next = q->next;
delete q;
return;
}
p = p->next;
}
cout<<"学生不存在"<<endl;
}
}
//修改函数的定义
void Students::edit()
{
Student *p=head->next;
Student *q=head->next;
cout<<"输入需要修改学生的学号";
int n;
int b=0;
int j=1;
cin>>n;
while(p)
{
if(p->num==n)
{
b=1;
}
p=p->next;
}
if(b==0)
cout<<"没有该学生"<<endl;
if(b==1)
{
p=head->next;
while(p)
{
if(p->num==n)
{ cout<<"************************"<<endl;
cout<<"*****1修改学生学号******"<<endl;
cout<<"*****2修改学生姓名******"<<endl;
cout<<"*****3修改学生性别******"<<endl;
cout<<"*****4修改学生专业******"<<endl;
cout<<"*****5修改出生年份******"<<endl;
cout<<"*****6修改家庭地址******"<<endl;
cout<<"*****7修改英语成绩******"<<endl;
cout<<"************************"<<endl;
int m;
cin>>m;
switch(m)
{
case 1:
cout<<"请输入学号:";
int i;
cin>>i;
while(q)
{
if(q->num==i)
{
cout<<"编号重复";
break;
j=0;
}
q=q->next;
}
// cout<<j<<endl;
if(j!=0)
p->num=i;
break;
case 2:
cout<<"请输入姓名"<<endl;
cin>>p->name;
break;
case 3:
cout<<"请输入性别"<<endl;
cin>>p->sex;
break;
case 4:
cout<<"请输入专业";
cin>>p->major;
break;
case 5:
cout<<"请输入出生年份";
cin>>p->born;
break;
case 6:
cout<<"请输入家庭地址";
cin>>p->address;
break;
case 7:
cout<<"请输入英语成绩";
cin>>p->english;
break;
default:
cout<<" 请重新输入"<<endl;
}
break;
}
}
}
}
//成绩处理函数的定义
void Students::score()
{
Student *p=head->next;
int b=1;
if(p==NULL)
{
b=0;
}
if(b==0)
{
cout<<"当前无数据"<<endl;
}
if(b==1)
{
int m=1;
while(p->next!=NULL)
{
p=p->next;
m++;
}
//cout<<"M="<<m<<endl;
int sum=0;
p=head->next;
while(p)
{
sum=sum+p->english;
p=p->next;
}
float ave;
ave=sum/m;
cout<<"英语平均分为";
cout<<ave<<endl;
p=head->next;
cout<<"不及格学生:"<<endl;
int n=0;
while(p)
{
if(p->english<60)
{
cout<<"学生学号:"<<p->num<<" "<<"学生姓名"<<p->name<<" "<<"学生成绩"<<p->english<<endl;
n++;
}
p=p->next;
}
cout<<"不及格人数";
cout<<n;
}
}
//文件写入函数的定义
void Students::write()
{
ofstream outfile("d:\\file.txt");
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
Student *p=head->next;
while(p)
{
outfile<<p->num<<" ";
outfile<<p->name<<" ";
outfile<<p->sex<<" ";
outfile<<p->major<<" ";
outfile<<p->born<<" ";
outfile<<p->address<<" ";
outfile<<p->english <<" ";
p=p->next;
}
outfile.close();
cout<<"保存完成"<<endl;
}
//文件读取
void Students::read()
{
ifstream infile("d:\\file.txt");
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
Student *p=head,*q;
//将文件的数据输入到链表中
while(!infile.eof())
{
q=new Student;
if(infile>>q->num)
{
infile>>q->name;
infile>>q->sex;
infile>>q->major;
infile>>q->born;
infile>>q->address;
infile>>q->english;
q->next=NULL;
p->next=q;
p=q;
}
else
break;
}
cout<<"读取成功"<<endl;
infile.close();
}
void f()
{
cout<<"************************************"<<endl;
cout<<"************************************"<<endl;
cout<<"*******欢迎使用新生报到系统*********"<<endl;
cout<<"************************************"<<endl;
cout<<"************************************"<<endl;
cout<<"点击Enter键进行下一步";
getchar();
system("cls");
}
void g()
{
cout<<"**************************************"<<endl;
cout<<"********** 1 输入学生信息 ************"<<endl;
cout<<"********** 2 增加学生信息 ************"<<endl;
cout<<"********** 3 删除学生信息 ************"<<endl;
cout<<"********** 4 查询学生信息 ************"<<endl;
cout<<"********** 5 修改学生信息 ************"<<endl;
cout<<"********** 6 统计学生信息 ************"<<endl;
cout<<"********** 7 显示学生信息 ************"<<endl;
cout<<"********** 8 保存学生信息 ************"<<endl;
cout<<"********** 9 读取学生信息 ************"<<endl;
cout<<"********** 10排序学生信息 ************"<<endl;
cout<<"********** 11处理成绩信息 ************"<<endl;
cout<<"********** 12退出 系统 ************"<<endl;
cout<<"**************************************"<<endl;
}
int main()
{ f();
Students stu;
//St *h=head();
while(1)
{
int i;
g();
int n;
cin>>n;
switch(n)
{
case 1: stu.create();
break;
case 2: stu.add();
break;
case 3: stu.delet();
break;
case 4:stu.show();
break;
case 5: stu.edit();
break;
case 6:stu.sum();
break;
case 7: stu.search();
break;
case 8:stu.write();
break;
case 9:stu.read();
break;
case 10:stu.sort();
break;
case 11:stu.score();
break;
case 12:
exit(0);
break;
default:
cout<<"请重新输入";
}
if(i==-1)
break;
cout<<"点击Enter键进行下一步";
getchar();
getchar();
system("cls");
}
return 0;
}
2) 案例二
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct student)
#include<windows.h>
typedef struct student{
int id;
int num;
char name[20];
int score;
struct student *next;//指针域(指向节点的指针)
}stu;
struct student *creat() //创建头节点
{
stu *head;
head=(stu*)malloc(LEN);
if(head==NULL)
printf("申请失败");
else
{
head->next ==NULL;
}
return head;
}
void init_data(stu *p) //初始化数据
{
int n,i;
stu *q=NULL;
printf("请输入要添加的学生个数:");
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
q=(struct student*)malloc(LEN);
printf("请输入第%d个学生的学生序号:",i+1);
scanf("%d",&q->id);
printf("请输入第%d个学生的学生号:",i+1);
scanf("%d",&q->num );
printf("请输入第%d个学生的姓名:",i+1);
scanf("%s",q->name );
printf("请输入第%d个学生的分数:",i+1);
scanf("%d",&q->score );
p->next =q;
p=p->next ;
}
q->next =NULL;
printf("初始化信息成功\n");
}
void insert_data(stu *p) //插入学生信息
{
stu *Q=NULL;
stu *q=p->next ;
int n;
printf("你要插入的个体数:");
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
{
Q=(struct student*)malloc (LEN);
printf("请输入你要加入的学生序号:");
scanf("%d",&Q->id );
printf("请输入你要加入的学生号:");
scanf("%d",&Q->num );
printf("请输入你要加入的学生名字:");
scanf("%s",Q->name );
printf("请输入你要加入的学生分数:");
scanf("%d",&Q->score );
Q->next =q->next ;
q->next =Q;
}
q=q->next ;//指针后移
printf("添加成功\n");
}
void del_data(stu *p) //删除学生信息
{
stu *q=p->next ;
stu *l=p;
bool flag=false;
int i;
printf("请输入要删除的学生的编号:");
scanf("%d",&i);
while(p)
{
if(q->id ==i)
{
l->next =q->next ;
free(q);
flag=true;
break;
}
q=q->next ;
l=l->next ;
}
if(flag==false)
{
printf("无学生记录\n");
}
printf("删除成功\n");
}
void find_data(stu *p) //查找学生信息
{
stu *q=p->next ;
bool flag=false;
int i;
printf("请输入你要查找的学生号:");
scanf("%d",&i);
while(q)
{
if(i==q->id )
{
printf("学生序号:%d 学生学号:%d 学生姓名:%s 学生成绩:%d\n",q->id ,q->num ,q->name ,q->score );
flag=true;
}
q=q->next ;
}
if(flag==false)
{
printf("查找失败\n");
}
}
void change_data(stu *p) //修改学生信息
{
stu *q=p->next ;
bool flag=false;
int i;
printf("请输入需要修改的学生序号:");
scanf("%d",&i);
while(q)
{
if(q->id ==i)
{
printf("请输入修改后的编号:");
scanf("%d",&q->id );
printf("请输入修改后的学生号:");
scanf("%d",&q->num );
printf("请输入修改后的姓名:");
scanf("%s",&q->name );
printf("请输入修改后的分数:");
scanf("%d",&q->score );
flag=true;
}
q=q->next ;
}
if(flag==false)
{
printf("修改失败\n");
}else{
printf("修改成功\n");
}
}
void traverse(stu *p) //遍历链表
{
stu *q=p->next ;
while(q!=NULL)
{
printf("学生序号:%d 学生学号:%d 学生姓名:%s 学生成绩:%d\n",q->id ,q->num ,q->name ,q->score );
q=q->next ;
}
}
void choice() //显示列表
{
stu *p;
p=creat();
printf("登录日期:\n");
system("date/t");
printf("登陆时间:\n");
system("time/t");
while(1)
{
printf("-----------欢迎来到学生管理系统----------\n") ;
printf("\t1.****初始化学生信息****\n");
printf("\t2.****插入学生信息****\n");
printf("\t3.****删除学生信息****\n");
printf("\t4.****查找学生信息****\n");
printf("\t5.****修改学生信息****\n");
printf("\t6.****浏览学生信息****\n");
printf("\t0.****退出系统****\n");
printf("-----------------------------------------\n") ;
int choice;
printf("请选择: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("初始化信息:\n");
init_data(p);
break;
case 2:
system("CLS");
printf("插入信息\n");
insert_data(p);
break;
case 3:
system("CLS");
printf("删除信息\n");
del_data(p);
break;
case 4:
system("CLS");
printf("查找信息\n");
find_data(p);
break;
case 5:
system("CLS");
printf("修改信息\n");
change_data(p);
break;
case 6:
system("CLS");
printf("浏览信息\n");
traverse(p);
break;
case 0:
system("CLS");
printf("谢谢使用,再见!!!\n");
exit(0);
default:
system("CLS");
printf("输入错误,请重新输入!!!\n");
break;
}
}
}
void registerall(){
char a[20],b[20],c[20],d[20];
while(1)
{
int n;
printf("******** 1、注册 ********\n");
printf("******** 2、登陆 ********\n");
printf("******** 3、退出 ********\n");
printf("请输入你的编号:");
if(scanf(" %d",&n) == 1 ){
switch(n){
case 1:{
while(1)
{
int m;
printf("1 注册 2 退出\n");
if (scanf(" %d",&m) == 1 ){
if( m == 2 ){
break;
}
else if ( m == 1 ){
printf("注册账户:");
scanf(" %s",&a);
printf("注册密码:");
scanf(" %s",&b);
} else{
printf("输入错误\n");
}
} else{
printf("非法输入!\n");
break;
}
}
break;
}
case 2:{
while(1)
{
int n;
printf("1 登陆 2 退出登陆\n");
if ( scanf("%d",&n) == 1 ){
if( n == 2 ){
break;
}
else if ( n == 1 ){
printf("输入账户:");
scanf(" %s",&c);
printf("输入密码:");
scanf(" %s",&d);
if(strcmp(a,c)==0&&strcmp(b,d)==0){
printf("登陆成功!\n");
printf("内容正在加载中,请等待:\n");
for(int j=0; j<8; j++){
Sleep(500);
printf(" . ");
}
Sleep(2500);
system("CLS");
system ("color F");
choice();
}else{
printf("账户或密码错误!\n");
}
}else{
printf("input error\n");
}
}else{
printf("非法输入!重新运行!\n");
break;
}
}
break;
}
case 3:{
printf("退出成功!\n");
exit(0);
}
default:
printf("input error!\n");
}
}
else{
printf("非法输入!重新运行!\n");
break;
}
}
}
int main()
{
system("CLS");
system ("color 5F");
registerall();
return 0;
}
3) 案例三
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct student {
int num;
char name[20];
char add[20];
float achievement;
struct student *next;
} happy,*point;
struct student* creatlist() {
point head=(point)malloc(sizeof(happy));;
point two=head;
int i,n;
int num;
char name[20];
char add[20];
float achievement;
printf("请输入学生个数:");
scanf("%d",&n);
for(i=0; i<n; i++) {
point one=(point)malloc(sizeof(happy));
printf("请输入第%d个同学的数据:",i+1);
int num;
char name[20];
char add[20];
float achievement;
scanf("%d %s %s %f",&num,name,add,&achievement);
one->num =num;
strcpy(one->name ,name);
strcpy(one->add,add);
one->achievement =achievement;
two->next =one;
one->next=NULL;
two=one;
}
return head;
}
void travellist(point head) {
int i=0;
point one=head->next ;
while(one!=NULL) {
i++;
printf("第%d个学生的数据:\n",i);
printf("%d ",one->num );
printf("%s ",one->name);
printf("%s ",one->add);
printf("%f \n",one->achievement);
one=one->next ;
}
printf("\n");
}
void insert(point head) {
int num;
printf("输入添加的学生位置(在它之后添加)");
scanf("%d",&num);
int num_;
char name[20];
char add[20];
float achievement;
scanf("%d %s %s %f",&num_,name,add,&achievement);
point one=head;
point three;
int i=0;
while(i<num) {
one=one->next ;
i++;
}
three=one->next ;
point two=(point)malloc(sizeof(happy));
two->num =num_;
strcpy(two->name ,name);
strcpy(two->add,add);
two->achievement =achievement;
one->next =two;
two->next =three;
}
void delate(point head) {
int i=0;
int n;
int num;
point one=head;
point two;
printf("输入你要删除的学生位置:");
scanf("%d",&num);
while(i<num-1) {
one=one->next ;
i++;
}
two=one->next ;
one->next =two->next ;
free(two);
}
void search(point head) {
point one=NULL;
one=head->next ;
int num;
printf("你要查询的学号:") ;
scanf("%d",&num);
while(one!=NULL) {
if(one->num ==num) {
printf("查找成功\n");
printf("学号:%d\n",one->num );
printf("姓名:%s\n",one->name );
printf("地址:%s\n",one->add );
printf("成绩:%f\n",one->achievement );
break;
}
one=one->next ;
}
}
void revise(point head) {
int num;
int num_;
char name[20];
char add[20];
float achievement;
int n=0;
point one=head;
printf("输入学生位置:");
scanf("%d",&num);
while(n<num) {
one=one->next ;
n++;
}
scanf("%d %s %s %f",&num_,name,add,&achievement);
one->num =num_;
strcpy(one->name ,name);
strcpy(one->add,add);
one->achievement =achievement;
}
void headline() {
printf("1.输入学生数据\n");
printf("2.添加学生数据\n");
printf("3.删除学生数据\n");
printf("4.查询学生数据\n");
printf("5.修改学生数据\n");
printf("6.退出系统\n");
}
int main() {
int i;
point head;
do{
printf("\n");
headline();
printf("\n\n");
printf("请选择1——9:");
scanf("%d",&i);
switch(i)
{
case 1:head=creatlist();
travellist(head);
break;
case 2:insert(head);
travellist(head);
break;
case 3:delate(head);
travellist(head);
break;
case 4:search(head);
break;
case 5:revise(head);
travellist(head);
break;
case 6:break;
default:printf("选择错误,请重新选择");
}
}while(i!=6);
return 0;
}
4) 案例四
#include <iostream>
using namespace std;
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define N 1000
typedef struct student
{
int number;
char name[20];
int grade;
int gaoshu;
int yingyu;
int jisuanji;
int sum;
}STUDENT;
STUDENT student[N];
int shuliang=0; //用于记录有多少学生
class Student
{
public:
void fhzjm();
void DengJi();
void ShanChu();
void LiuLan();
void ChaZhao();
void PaiXu();
void Save();
void menu();
void DaoRu();
};
void Student::fhzjm() //每次操作结束 来提醒是否继续进行操作
{
char biaozhi[20];
cout<<endl;
cout<<"还需要操作么?如果需要操作请输入:yes,否则请输入:no"<<endl;
cin>>biaozhi;
if(strcmp(biaozhi,"yes")==0)
{
menu();
}
else if(strcmp(biaozhi,"no")==0)
exit(0); //退出系统 这是一个库函数 用于结束程序
else
{
cout<<"请输入正确的字符,谢谢!"<<endl;
fhzjm();
}
}
void Student::DengJi() //登记学生信息函数
{
int rs; //用于记录要输入多少条学生信息
int i,k=1;
system("CLS");
cout<<"请输入需要输入几个学生信息:"<<endl;
cin>>rs;
for(i=shuliang;i<shuliang+rs;i++,k++)
{
cout<<"请输入第"<<k<<"个学生的学号:";
cin>>student[i].number;
cout<<"请输入学生的姓名:";
cin>>student[i].name;
//cout<<"请输入学生3门课的成绩:";
cout<<"请输入第高数课的成绩:";
cin>>student[i].gaoshu;
cout<<"请输入英语课的成绩:";
cin>>student[i].yingyu;
cout<<"请输入第计算机课的成绩:";
cin>>student[i].jisuanji;
}
shuliang=shuliang+rs;
fhzjm();
}
void Student::ShanChu() //删除学生信息
{
char shanchuinfo[10];
system("CLS"); //清屏函数 这是一个库函数 用于清除控制台中的所有信息
cout<<"删除全部学生信息请输入\"all\",删除指定学号的学生信息请输入\"one\"\n";
cin>>shanchuinfo;
if(strcmp(shanchuinfo,"all")==0)
{
int j;
cout<<"你删除的学生信息如下:"<<endl;
cout<<"-----------学号-------------姓名-------------高数--------------英语--------------计算机\t"<<endl;
for(j=0;j<shuliang;j++)
cout<<"----%d-------%s-------%d-------%d-------%d\t\n",student[j].number,student[j].name,student[j].gaoshu,student[j].jisuanji;
shuliang=0;
cout<<"删除成功"<<endl;
}
else if(strcmp(shanchuinfo,"one")==0)
{
struct student *p=NULL;
int choice;
int i,j,k=0;
cout<<"请输入你要删除的人的学号:";
cin>>choice;
for(i=0;i<shuliang;i++)
{
if(choice==student[i].number)
{
k=1;j=i;break;
}
}
if(k)
{
if(shuliang==1)
{
p=&student[0];
free(p);
shuliang=0;
}
else
{
for(i=j;i<shuliang;i++)
{
student[i]=student[i+1];
}
shuliang=shuliang-1;
}
cout<<"删除成功"<<endl;
}
else
{
cout<<"输入数据错误!"<<endl;
}
}
fhzjm();
}
void Student::LiuLan() //浏览操作函数
{
int i;
system("CLS");
if(shuliang==0)
{
cout<<"系统里面没有任何学生的信息!"<<endl;
}
else
{
for(i=0;i<shuliang;i++)
{
cout<<"第"<<i+1<<"个学生的学号为:"<<student[i].number<<endl;
cout<<"第"<<i+1<<"个学生的姓名为:"<<student[i].name<<endl;
cout<<"第"<<i+1<<"个学生的高数课的成绩为:"<<student[i].gaoshu<<endl;
cout<<"第"<<i+1<<"个学生的英语课的成绩为:"<<student[i].yingyu<<endl;
cout<<"第"<<i+1<<"个学生的计算机课的成绩为:"<<student[i].jisuanji<<endl;
student[i].sum=student[i].gaoshu+student[i].yingyu+student[i].jisuanji;
cout<<"第"<<i+1<<"个学生的总成绩为:"<<student[i].sum<<endl;
}
}
fhzjm();
}
void Student::ChaZhao() //查找函数 实现查找功能 分为三种查询 按学号查询 按姓名 按成绩
{
int xx;
char choice,yy[20];
int i,j,k=0;
system("CLS");
if(shuliang==0)
{
cout<<"系统里面没有任何学生的信息!"<<endl;
fhzjm();
}
cout<<"三种查找方式:学号,姓名,成绩"<<endl;
cout<<"如果按学号查找请输1,如果按姓名查找请输2,如果按成绩查找请输3"<<endl;
cout<<"请输入您查找的方式:";
cin>>choice;
if(choice=='1')
{
cout<<"请输入需要查找学生的学号:";
cin>>xx;
cout<<"您所查找的学生的信息为:"<<endl;
cout<<"----学号----姓名----高数成绩----英语成绩----计算机成绩----\t"<<endl;
for(i=0;i<shuliang;i++)
{
if(xx==student[i].number)
{
j=i;k=1;
cout<<"----%d-------%s-------%d-------%d-------%d----\t\n",student[j].number,student[j].name,student[j].gaoshu,student[j].yingyu,student[i].jisuanji;
}
}
if(k==0)
cout<<"输入信息有误:"<<endl;
}
else if(choice=='2')
{
cout<<"请输入需要查找学生的姓名:";
cin>>yy;
cout<<"您所查找的学生的信息为:"<<endl;
cout<<"----学号----姓名----高数成绩----英语成绩----计算机成绩----\t"<<endl;
for(i=0;i<shuliang;i++)
{
if(strcmp(yy,student[i].name)==0)
{
j=i;k=1;
cout<<"----%d-------%s-------%d-------%d-------%d----\t\n",student[j].number,student[j].name,student[j].gaoshu,student[j].yingyu,student[j].jisuanji;
}
}
if(k==0)
cout<<"输入信息有误:"<<endl;
}
else if(choice=='3')
{
cout<<"请输入需要查找学生的成绩:"<<endl;
cin>>xx;
cout<<"您所查找的学生的信息为:"<<endl;
cout<<"----学号----姓名----高数----英语----计算机----\t"<<endl;
for(i=0;i<shuliang;i++)
{
if(xx==student[i].grade)
{
j=i;k=1;
cout<<"----%d-------%s-------%d-------%d-------%d----\t\n",student[j].number,student[j].name,student[j].gaoshu,student[j].yingyu,student[i].jisuanji;
}
}
if(k==0)
cout<<"输入信息有误:"<<endl;
}
fhzjm();
}
void Student::PaiXu() //排序函数 执行排序操作
{
struct student *p1[N],**p2,*temp;
int i,j;
system("CLS");
p2=p1;
for( i=0;i<shuliang;i++)
{
p1[i]=student+i;
}
for( i=0;i<shuliang;i++)
{
for( j=i+1;j<shuliang;j++)
{
if((*(p2+i))->sum<(*(p2+j))->sum)
{
temp=*(p2+i);*(p2+i)=*(p2+j);*(p2+j)=temp;
}
}
}
cout<<"按照总成绩排序之后的信息为:"<<endl;
cout<<"----学号----姓名----总成绩----\t"<<endl;
for( i=0;i<shuliang;i++)
{
student[i].sum=student[i].gaoshu+student[i].yingyu+student[i].jisuanji;
cout<<"----%d-----%s----%d-----\n",(*(p2+i))->number,(*(p2+i))->name,(*(p2+i))->sum;
}
fhzjm();
}
void Student::Save() //保存数据 把数据写到文件操作
{
int i;
FILE *rs;
if((rs=fopen("student.txt","w"))==NULL)
{
cout<<"not open";
exit(0);
}
for(i=0;i<shuliang;i++)
{
fwrite(&student[i], sizeof(student[i]), 1, rs);
}
if(ferror(rs))
{
fclose(rs);
perror("写文件失败!\n");
return;
}
cout<<"存储文件成功!"<<endl;
fclose(rs);
fhzjm();
}
void Student::DaoRu()
{
struct student t;
int i=0;
FILE* fp = fopen("student.txt", "r");
shuliang=0;
if(NULL==fp)
{
perror("读取文件打开失败!\n");
return;
}
memset(student,0x0,sizeof(student));
while(1)
{
fread(&t,sizeof(t),1,fp);
if(ferror(fp))
{
fclose(fp);
perror("读文件过程失败!\n");
return;
}
if(feof(fp))
{
break;
}
student[i]=t;
i++;
}
fclose(fp);
shuliang=i;
cout<<"导入文件成功!"<<endl;
fhzjm();
}
void Student::menu()
{
int n=0;
system("CLS");
cout<<" ==================学生信息管理系统==============="<<endl;
cout<<" = 1.登记学生信息 ="<<endl;
cout<<" = 2.删除学生信息 ="<<endl;
cout<<" = 3.浏览学生信息 ="<<endl;
cout<<" = 4.查找学生信息 ="<<endl;
cout<<" = 5.根据总成绩排序 ="<<endl;
cout<<" = 6.存储到文件 ="<<endl;
cout<<" = 7.从文件导入 ="<<endl;
cout<<" = 8.退出系统 ="<<endl;
cout<<" ================================================="<<endl;
cout<<" 请选择:";
cin>>n;
switch (n)
{
case 1:
DengJi();break; //等记信息
case 2:
ShanChu();break; //删除信息
case 3:
LiuLan();break; //浏览信息
case 4:
ChaZhao();break; //查找信息
case 5:
PaiXu();break; //对成绩记性排序
case 6:
Save();break; //保存数据到文件中
case 7:
DaoRu();break; //把文件中的数据导入到程序中
case 8:
exit(0);break; //退出系统
}
}
int main()
{
Student student;
student.menu();
return 0;
}
5) 案例五
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<fstream>
#include<windows.h>
using namespace std;
class student {
public:
int num; //学号
char s[20];
char name[20]; //姓名
double score[6]; //成绩
double total; //总分
student *next;
void input();
void readfile(ifstream&in); // 读出数据
};
void student::input() { //输入信息 调用它
cout<<"请输入学号:";
cin>>num;
cout<<"请输入姓名:";
cin>>name;
int a,b,c,d;
cout<<"请输入语文:";
cin>>a;
while(a>150||a<0) {
cout<<"输入成绩有错误,请从新输入:";
cin>>a;
}
cout<<"请输入高数:";
cin>>b;
while(b>150||b<0) {
cout<<"输入成绩有错误,请从新输入:";
cin>>b;
}
cout<<"请输入英语:";
cin>>c;
while(c>150||c<0) {
cout<<"输入成绩有错误,请从新输入:";
cin>>c;
}
cout<<"请输入专业课:";
cin>>d;
while(d>150||d<0) {
cout<<"输入成绩有错误,请从新输入:";
cin>>d;
}
score[1]=a;
score[2]=b;
score[3]=c;
score[4]=d;
total=a+b+c+d;
next=NULL;
}
void student::readfile(ifstream&in) { //文件的调用 直接调用 不能其他的 得一一的调用
in>>num>>name>>score[1]>>score[2]>>score[3]>>score[4]>>total;
}
class studentmessage {
public:
studentmessage();
void add();//添加
void display();
void maxmin();//输出最大最小的姓名
void save();//保存文件
void rank();//排序
void swap(student*p,student*q);
void menu();//菜单
void xiugai();//修改
void tianjia();
void shanchu();
void chazhao();
void average();
void youxiu();
void bukao();
void duqu();
protected:
student *head;
student *tail;
ofstream out;
ifstream in;
};
studentmessage::studentmessage() { //!!! 多看看
head=new student;
head->next=new student;
tail=head->next;
ifstream in("file.text");//打开文件
char ch=in.get(); // //读取一个文件的的字符 !!so 保存时添加无关一个字符
if(in.eof()) {
cout<<"是一个新系统,无学生信息。请先输入"<<endl;
}
while(!in.eof()) { //文件里有东西 读取 自动
tail->readfile(in);
if(tail->name[0]=='\0') { //判断mane 又没空格
break;
}
tail->next= new student; // 建立一个空间
tail->next->next=NULL; //不同
tail=tail->next;
tail->next=NULL; //用tail NULL 来判断 // 尾插
}
}
void studentmessage::add() {
system("cls");
int flag=1;
while(flag) {
tail->input();
tail->next=new student;
tail->next->next=NULL;
tail=tail->next;
cout<<"是否输入(Y/N)"<<endl;
char n;
cin>>n;
if(n=='N'|| n=='n') {
flag=0;
}
}
system("pause");
}
void studentmessage::display() {
system("cls");
student *p=head->next;
if(p==NULL) {
cout<<"没有"<<endl;
}
for(p; p->next!=NULL; p=p->next) { //???
cout<<"-----------------------"<<endl;
cout<<"学号:"<<p->num<<endl;
cout<<"姓名:"<<p->name<<endl;
cout<<"语文:"<<p->score[1]<<endl;
cout<<"高数:"<<p->score[2]<<endl;
cout<<"英语:"<<p->score[3]<<endl;
cout<<"专业课:"<<p->score[4]<<endl;
cout<<"总分:"<<p->total<<endl;
cout<<"------------------------"<<endl;
}
system("pause");
}
void studentmessage::swap(student *p,student *q) {
student *temp=new student;
temp->num=p->num;
strcpy(temp->name,p->name);
temp->score[1]=p->score[1];
temp->score[2]=p->score[2];
temp->score[3]=p->score[3];
temp->score[4]=p->score[4];
temp->total=p->total;
p->num=q->num;
strcpy(p->name,q->name);
p->score[1]=q->score[1];
p->score[2]=q->score[2];
p->score[3]=q->score[3];
p->score[4]=q->score[4];
p->total=q->total;
q->num=temp->num;
strcpy(q->name,temp->name);
q->score[1]=temp->score[1];
q->score[2]=temp->score[2];
q->score[3]=temp->score[3];
q->score[4]=temp->score[4];
q->total=temp->total;
}
void studentmessage::rank() {
system("cls");
student *p,*q;
p=head->next;
for(p; p->next!=NULL; p=p->next) {
for(q=p->next; q->next!=NULL; q=q->next) {
if(q->total>p->total)
swap(q,p);
}
}
cout<<"排序成功"<<endl;
student *p1=head->next;
for(p1; p1->next!=NULL; p1=p1->next) {
cout<<"-----------------------"<<endl;
cout<<"学号:"<<p1->num<<endl;
cout<<"姓名:"<<p1->name<<endl;
cout<<"语文:"<<p1->score[1]<<endl;
cout<<"高数:"<<p1->score[2]<<endl;
cout<<"英语:"<<p1->score[3]<<endl;
cout<<"专业课:"<<p1->score[4]<<endl;
cout<<"总分:"<<p1->total<<endl;
cout<<"------------------------"<<endl;
}
system("pause");
}
void studentmessage::menu() {
system("cls");
system("date/t");
system("time/t");
system("color 5F");
cout<<"\t\t\t\t***************************************************"<<endl;
cout<<"\t\t\t\t-------------------学生管理系统--------------------"<<endl;
cout<<"\t\t\t\t 1.录入信息 "<<endl;
cout<<"\t\t\t\t 2.显示信息 "<<endl;
cout<<"\t\t\t\t 3.修改信息 "<<endl;
cout<<"\t\t\t\t 4.删除信息 "<<endl;
cout<<"\t\t\t\t 5.添加信息 "<<endl;
cout<<"\t\t\t\t 6.查找信息 "<<endl;
cout<<"\t\t\t\t 7.保存信息 "<<endl;
cout<<"\t\t\t\t 8.退出系统 "<<endl;
cout<<"\t\t\t\t***************************************************"<<endl;
system("pause");
}
void studentmessage::xiugai() {
system("cls");
student *p=head->next;
int flag=1;
while(flag) {
int n;
cout<<"修改输入学号:"<<endl;
cin>>n;
while(p->num!=n&&p->next!=NULL) {
// cout<<"asdasd";
p=p->next;
}
p->input();
cout<<"修改成功"<<endl;
cout<<"选择是否接着修改(Y/N)"<<endl;
char k;
cin>>k;
if(k=='N'||k=='n') {
flag=0;
}
}
system("pause");
}
void studentmessage::tianjia() {
system("cls");
student *p=head;
student *p1=head->next;
student *p2=new student;
char k;
while(k!='N') {
int n;
cout<<"输入你添加的位数"<<endl;
cin>>n;
int i=0;
while(n!=i+1) {
p=p->next;
p1=p->next;
i++;
}
p2->input();
p2->next=p1;
p->next=p2;
cout<<"选择是否插入:(Y/N)"<<endl;
cin>>k;
}
system("pause");
}
void studentmessage::shanchu() {
system("cls");
student *p=head;
student *p1=head->next;
char k;
while(k!='N') {
int n;
cout<<"输入你删除的位数"<<endl;
cin>>n;
int i=0;
while(n!=i+1) {
p=p->next;
p1=p->next;
i++;
}
p->next=p1->next;
delete p1;
cout<<"删除成功"<<endl;
cout<<"选择是否删除:(Y/N)"<<endl;
cin>>k;
}
system("pause");
}
void studentmessage::chazhao() {
system("cls");
student *p=head->next;
int a;
cout<<"请输入要查找的学生的学号:";
cin>>a;
while(p) {
if(p->num==a) {
break;
}
p=p->next;
}
if(!p) {
cout<<"未找到要查找学生!"<<endl;
return ;
}
cout<<"查找成功";
cout<<"-----------------------"<<endl;
cout<<"学号:"<<p->num<<endl;
cout<<"姓名:"<<p->name<<endl;
cout<<"语文:"<<p->score[1]<<endl;
cout<<"高数:"<<p->score[2]<<endl;
cout<<"英语:"<<p->score[3]<<endl;
cout<<"专业课:"<<p->score[4]<<endl;
cout<<"总分:"<<p->total<<endl;
system("pause");
}
void studentmessage::maxmin() {
system("cls");
student *p,*q;
p=head->next;
for(p; p->next!=NULL; p=p->next) {
for(q=p->next; q->next!=NULL; q=q->next) {
if(q->total>p->total)
swap(q,p);
}
}
cout<<"最高分的名字是:"<<head->next->name<<endl;
student *p1=head->next;
//cout<<"最低分的名字是:"<<p1->name<<endl;
while(p1->next->next!=NULL)
{
// cout<<"最低分的名字是:"<<p1->name<<endl;
p1=p1->next;
}
cout<<"最低分的名字是:"<<p1->name<<endl;
cout<<"输出成功!"<<endl;
system("pause");
}
void studentmessage::average() {
system("cls");
student *p=head->next;
double average[5];
int i=0;
for(p; p!=NULL; p=p->next) {
average[1]=average[1]+p->score[1];
average[2]=average[2]+p->score[2];
average[3]=average[3]+p->score[3];
average[4]=average[4]+p->score[4];
i++;
}
cout<<"语文平均成绩:"<<average[1]/i<<endl;
cout<<"高数平均成绩:"<<average[2]/i<<endl;
cout<<"英语平均成绩:"<<average[3]/i<<endl;
cout<<"专业课平均成绩:"<<average[4]/i<<endl;
system("pause");
}
void studentmessage::save() {
system("cls");
out.open("file.text");
student *p=head->next;
//out<<"1"<<" ";
if(head->next==NULL) {
cout<<"没有文件"<<endl;
return ;
} else {
// out<<"学号: "<<"姓名: "<<"语文: "<<"高数: "<<"英语: ""专业课: "<<"总分: "<<endl;
for(p; p->next!=NULL; p=p->next) {
out<<" ";
out<<p->num<<" ";
out<<p->name<<" ";
out<<p->score[1]<<" ";
out<<p->score[2]<<" ";
out<<p->score[3]<<" ";
out<<p->score[4]<<" ";
out<<p->total<<endl;
}
}
out.close();
cout<<"文件保存成功!"<<endl;
system("pause");
}
void studentmessage::youxiu() {
system("cls");
student *p=head->next;
for(p; p!=NULL; p=p->next) {
if(p->total>340&&p->score[1]>70&&p->score[2]>70&&p->score[3]>70&&p->score[4]>70) {
cout<<"-----------------------"<<endl;
cout<<"学号:"<<p->num<<endl;
cout<<"姓名:"<<p->name<<endl;
cout<<"语文:"<<p->score[1]<<endl;
cout<<"高数:"<<p->score[2]<<endl;
cout<<"英语:"<<p->score[3]<<endl;
cout<<"专业课:"<<p->score[4]<<endl;
cout<<"总分:"<<p->total<<endl;
cout<<"------------------------"<<endl;
continue;
}
}
system("pause");
}
void studentmessage::bukao() {
system("cls");
student *p=head->next;
out.open("bukao.text");
out<<"补考名单:"<<endl;
for(p; p!=NULL; p=p->next) {
if(p->score[1]<60||p->score[2]<60||p->score[3]<60||p->score[4]<60) {
out<<p->name<<endl;;
continue;
}
}
out.close();
cout<<"文件保存成功!"<<endl;
system("pause");
}
class teather :public studentmessage {
public:
// teather();
void menu();
};
void teather::menu() {
system("cls");
system("date/t");
system("time/t");
system("color 5F");
cout<<"\t\t\t\t***************************************************"<<endl;
cout<<"\t\t\t\t-------------------老师管理系统--------------------"<<endl;
cout<<"\t\t\t\t 1.录入信息 "<<endl;
cout<<"\t\t\t\t 2.显示信息 "<<endl;
cout<<"\t\t\t\t 3.修改信息 "<<endl;
cout<<"\t\t\t\t 4.删除信息 "<<endl;
cout<<"\t\t\t\t 5.添加信息 "<<endl;
cout<<"\t\t\t\t 6.查找信息 "<<endl;
cout<<"\t\t\t\t 7.总分排序 "<<endl;
cout<<"\t\t\t\t 8.平均成绩 "<<endl;
cout<<"\t\t\t\t 9.优秀名单 "<<endl;
cout<<"\t\t\t\t 10.保存信息 "<<endl;
cout<<"\t\t\t\t 11.最大最小 "<<endl;
cout<<"\t\t\t\t 12.补考名单 "<<endl;
cout<<"\t\t\t\t 13.退出系统 "<<endl;
cout<<"\t\t\t\t***************************************************"<<endl;
system("pause");
}
class studentstudent:public studentmessage {
public:
};
int main() {
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
cout.width(65);
cout <<"系统正在启动,请稍等!";
for(int i=0; i<7; i++) {
printf(" .");
Sleep(200);
}
int flag=1;
while (flag) {
system("cls");
system("date/t");
system("time/t");
system("color 3F");
cout<<"\t\t\t\t---------------------------------------\t\t\t\t"<<endl;
cout<<"\t\t\t\t--------欢迎来到学生信息管理系统-------\t\t\t\t"<<endl;
cout<<"\t\t\t\t---------------1.老师管理--------------\t\t\t\t"<<endl;
cout<<"\t\t\t\t---------------2.学生管理--------------\t\t\t\t"<<endl;
cout<<"\t\t\t\t---------------3.退出系统--------------\t\t\t\t"<<endl;
cout<<"\t\t\t\t---------------------------------------\t\t\t\t"<<endl;
int i;
cout<<"请选择(1,2,3)"<<endl;
cin>>i;
if(i==1) {
teather m;
int flag1=1;
while(flag1) {
m.menu();
cout<<"你选择的编号:"<<endl;
int n;
cin>>n;
switch(n) {
case 1:
system("cls");
m.add();
break;
case 2:
system("cls");
m.display();
break;
case 3:
system("cls");
m.xiugai();
break;
case 4:
system("cls");
m.shanchu();
break;
case 5:
system("cls");
m.tianjia();
break;
case 6:
system("cls");
m.chazhao();
break;
case 7:
system("cls");
m.rank();
break;
case 8:
system("cls");
m.average();
break;
case 9:
system("cls");
m.youxiu();
break;
case 10:
system("cls");
m.save();
break;
case 11:
system("cls");
m.maxmin();
break;
case 12:
system("cls");
m.bukao();
break;
case 13:
system("cls");
flag1=0;
break;
}
}
}
if(i==2) {
studentstudent w;
int flag2=1;
while(flag2) {
w.menu();
cout<<"你选择的编号:"<<endl;
int n;
cin>>n;
switch(n) {
case 1:
system("cls");
w.add();
break;
case 2:
system("cls");
w.display();
break;
case 3:
system("cls");
w.xiugai();
break;
case 4:
system("cls");
w.shanchu();
break;
case 5:
system("cls");
w.tianjia();
break;
case 6:
system("cls");
w.chazhao();
break;
case 7:
system("cls");
w.save();
break;
case 8:
system("cls");
flag2=0;
break;
}
}
}
if(i==3) {
system("cls");
cout<<"欢迎下次再来"<<endl;
flag=0;
system("pause");
} else {
system("cls");
cout<<"欢迎来到主页面"<<endl;
system("pause");
}
}
return 0;
}
6) 案例六
#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<fstream>
using namespace std;
class Student{
public:
int id;
char name[20];
char sex[20];
int age;
int score;
};
class operate:public Student{
public:
int n;
operate(int score1)
{
score=score1;
}
operate *next;
operate *creat(operate *head);
void add(operate *head);
void del(operate *head);
void search(operate *head);
void rewrite(operate *head);
void show(operate *head);
void sort(operate *head);
int save(operate *head);
void read(operate *head);
void headline();
};
//void operate::save(operate *head)
//{
// ofstream outfile("D:\\a.txt");
// if(!outfile)
// {
// cerr<<"open error!"<<endl;
// exit(1);
// }
// operate *p=head->next;
// while(p)
// {
// outfile<<p->score<<endl ;
// p=p->next;
// }
// outfile.close();
// cout<<"保存完成"<<endl;
//}
int operate::save(operate *head)
{
operate *p=head->next;
fstream outfile("D:\\a.txt",ios::out);
if(!outfile)
{
cout<<"File open error"<<endl;
return -1;
}
while(p!=NULL)
{
outfile<<p->id<<endl;
outfile<<p->name<<endl;
outfile<<p->sex<<endl;
outfile<<p->age<<endl;
outfile<<p->score<<endl;
p=p->next;
}
outfile.close();
cout<<"保存成功"<<endl;
}
void operate::read(operate *head)
{
fstream infile("D:\\a.txt",ios::in);
if(!infile)
{
cerr<<"read error!"<<endl;
exit(1);
}
operate *p=head->next;
while(p!=NULL)
{
infile>>p->id;
infile>>p->name;
infile>>p->sex;
infile>>p->age;
infile>>p->score;
cout<<"id:"<<p->id<<endl;
cout<<"name:"<<p->name<<endl;
cout<<"sex:"<<p->sex<<endl;
cout<<"age:"<<p->age<<endl;
cout<<"score:"<<p->score<<endl;
p=p->next;
}
infile.close();
cout<<"读取完毕"<<endl;
}
/*void operate::save(operate *head)
{
FILE *fp;
operate *p;
p=head->next;
if((fp=fopen("stu.txt","a"))==NULL)
{
cout<<"error"<<endl;
return ;
}
while(p!=NULL)
{
fprintf(fp," %d \n",p->score);
p=p->next;
}
cout<<"保存成功"<<endl;
fclose(fp);
}*/
/*void operate::read(operate *head)
{
FILE *fp;
operate *pt;
int i=0;
pt=head->next;
if((fp=fopen("stu.txt","r"))==NULL)
{
cout<<"error";
exit(0);
}
fscanf(fp,"%d ",&pt->score);
while(pt!=NULL)
{
i++;
cout<<"第"<<i<<"个学生的分数:"<<pt->score<<endl;
pt=pt->next;
}
fclose(fp);
}*/
operate *operate::creat(operate *head)
{
head=(operate *)malloc(sizeof(operate));
operate *p;
p=head;
p->next==NULL;
int id1;
char name1[20];
char sex1[20];
int age1;
int score1;
cout<<"请输入要添加的人数:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
operate *q=(operate *)malloc(sizeof(operate));
cout<<"id:"<<endl;
cin>>id1;
cout<<"name:"<<endl;
cin>>name1;
cout<<"sex:"<<endl;
cin>>sex1;
cout<<"age:"<<endl;
cin>>age1;
cout<<"score:"<<endl;
cin>>score1;
q->id=id1;
strcpy(q->name,name1);
strcpy(q->sex,sex1);
q->age=age1;
q->score=score1;
p->next=q;
q->next=NULL;
p=q;
}
return head;
}
void operate::show(operate *head)
{
operate *p;
p=head->next;
while(p!=NULL)
{
cout<<"id:"<<p->id<<endl;
cout<<"name:"<<p->name<<endl;
cout<<"sex:"<<p->sex<<endl;
cout<<"age:"<<p->age<<endl;
cout<<"score:"<<p->score<<endl;
p=p->next;
}
}
void operate::add(operate *head)
{
operate *p1,*p2,*p3;
int b;
p1=head;
p3=p1->next;
int id1;
cout<<"请输入要插入哪个id后:"<<endl;
cin>>id1;
while (p3!=NULL)
{
if(p3->id==id1)
{
b=0;
break;
}
p3=p3->next;
}
if(b==0)
{
cout<<"请输入要插入的数:"<<endl;
p2=(operate *)malloc(sizeof(operate));
cin>>p2->id;
cin>>p2->name;
cin>>p2->sex;
cin>>p2->age;
cin>>p2->score;
p2->next=p3->next;
p3->next=p2;
}
else{
cout<<"无此学生"<<endl;
}
}
void operate::del(operate *head)
{
operate *p,*q;
int b=1,d=0;
q=head;
p=q->next;
cout<<"请输入要删除的id:"<<endl;
int id1;
cin>>id1;
while (p->id!=id1) {
q=q->next;
p=p->next;
if(p==NULL)
{
cout<<"无此学生,删除失败"<<endl;
b=0;
break;
}
}
if(b!=0)
{
q->next=p->next;
free(p);
}
}
void operate::search(operate *head)
{
operate *p,*q;
int b;
q=head;
p=head->next;
int id1;
cout<<"请输入要查找的id:"<<endl;
cin>>id1;
while(p!=NULL)
{
if(p->id==id1)
{
b=1;
cout<<"id:"<<p->id<<endl;
cout<<"name:"<<p->name<<endl;
cout<<"sex:"<<p->sex<<endl;
cout<<"age:"<<p->age<<endl;
cout<<"score:"<<p->score<<endl;
break;
}
else if(b!=1)
{
cout<<"无此学生"<<endl;
}
q=q->next;
p=p->next;
}
}
void operate::rewrite(operate *head)
{
operate *p,*q;
q=head;
p=head->next;
int id1;
cout<<"请输入要修改的id:"<<endl;
cin>>id1;
while(p!=NULL)
{
if(p->id==id1)
{
cout<<"请输入修改后的数据:"<<endl;
cin>>p->id;
cin>>p->name;
cin>>p->sex;
cin>>p->age;
cin>>p->score;
break;
}
q=q->next;
p=p->next;
if(q->id!=id1)
{
cout<<"无此学生,修改失败!"<<endl;
break;
}
}
}
void operate::sort(operate *head)
{
operate *p,*q,*q1;
int score1;
char name_[20];
char sex_[20];
int age1;
int id_;
q1=head->next;
int k=0;
while(q1!=NULL)
{
k++;
q1=q1->next;
}
for(int i=0;i<k;i++)
{
for(p=head->next,q=p->next;p!=NULL,q!=NULL;p=p->next,q=q->next)
{
if(p->score>q->score)
{
score1=q->score;
q->score=p->score;
p->score=score1;
strcpy(name_,q->name);
strcpy(q->name,p->name);
strcpy(p->name,name_);
strcpy(sex_,q->sex);
strcpy(q->sex,p->sex);
strcpy(p->sex,sex_);
age1=q->age;
q->age=p->age;
p->age=age1;
id_=q->id;
q->id=p->id;
p->id=id_;
}
}
}
}
void operate::headline()
{
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("= 8.学生成绩读取 =\n");
printf("= 9.退出系统 =\n");
}
int main() {
system("color 5F");
int i;
operate *head;
operate a(1);
do {
a.headline();
printf("= 请选择1-8 =\n");
printf("==================================\n");
scanf("%d",&i);
switch(i) {
case 1:
head=a.creat(head);
a.show(head);
break;
case 2:
a.add(head);
a.show(head);
break;
case 3:
a.del(head);
a.show(head);
break;
case 4:
a.search(head);
break;
case 5:
a.rewrite(head);
a.show(head);
break;
case 6:
a.sort(head);
a.show(head);
break;
case 7:
a.save(head);
break;
case 8:
a.read(head);
break;
case 9:
break;
default:
cout<<"选择错误,请重选!!"<<endl;
}
system("pause");
system("cls");
} while(i!=9);
return 0;
}
//int main()
//{
// operate *head;
// operate a(1);
// head=a.creat(head);
// a.show(head);
a.add(head);
a.show(head);
a.del(head);
a.show(head);
a.search(head);
a.rewrite(head);
a.show(head);
// a.sort(head);
// a.show(head);
// a.save(head);
// a.read(head);
// return 0;
//}
/* int c;
cout<<"请输入插入方式(1.头插 2.尾插 3.中插)"<<endl;
cin>>c;
switch(c)
{
case 1:
operate *p;
p=(operate *)malloc(sizeof(operate));
cout<<"输入要插入的分数:"<<endl;
cin>>p->score;
p->next=head->next;
head->next=p;
break;
case 2:
operate *p1,*q;
q=head->next;
p1=(operate *)malloc(sizeof(operate));
cout<<"输入要插入的分数:"<<endl;
int score1;
cin>>score1;
p1->score=score1;
while(q!=NULL)
{
if(q->score=score1)
{
cout<<"已存在的分数,请重新输入。"<<endl;
break;
}
q=q->next;
}
p1=q->next;
p1->next=NULL;
}
*/
7) 案例七
//录入其信息,如:学生姓名,性别,专业,出生日期,家庭地址,英语入学成绩
#include<iostream>
#include<string>
//#include<string.h>
#include<fstream>
#include<conio.h>
//#include<windows.h>
//#include<time.h>
using namespace std;
class student //信息
{
public:
student(string name,string sex,string major,string birthday,string home,string englishscore);
student(string zhanghao,string mima);
string s_name ; //姓名
string s_sex ; //性别
string s_major ; //专业
string s_birthday ; //生日
string s_home ; //住址
string s_englishscore; //英语成绩
int i_studentnum; //学生人数
student ** student_p; //学生指针
bool panduanwenjian; //判断文件
int getnum(); //得到文件中内用数量
void init(); //初始化数据
void dayin(); //打印
int panduanxuesheng(string name); //判断学生是否存在
~student();
};
student::student(string name,string sex,string major,string birthday,string home,string englishscore)
{
this->s_name=name;
this->s_sex=sex;
this->s_major=major;
this->s_birthday=birthday;
this->s_home=home;
this->s_englishscore=englishscore;
this->i_studentnum=0;
this->student_p=NULL;
}
student::~student()
{
if(this->student_p!=NULL)
{
delete[] this->student_p;
this->student_p=NULL;
}
}
class function:public student //功能
{
public:
function(string name,string sex,string major,string birthday,string home,string englishscore);
function(string zhanghao,string mima);
void mulu(); //目录
void tuichu(); //退出
void zengjia(); //增加
void shanchu(); //删除
void duwenjian(); //读文件
void chazhao(); //查找
void xiugai(); //修改
void xianshi(); //显示
void tongji(); //统计
void paixu(); //排序
void cunwenjian(); //存文件
~function(){};
};
function::function(string name,string sex,string major,string birthday,string home,string englishscore):student(name,sex,major,birthday,home,englishscore)
{
}
void function::zengjia() //增加
{
cout<<"请输入要添加的学生数"<<endl;
int addnum;
cin>>addnum;
if(addnum>0)
{
int newsize=this->i_studentnum+addnum;
student ** newspace=new student * [newsize];
if(this->student_p!=NULL)
{
for(int i=0;i<this->i_studentnum;i++)
{
newspace[i]=this->student_p[i];
}
}
for(int i=0;i<addnum;i++)
{
string name,sex,major,birthday,home,englishscore;
cout<<"请输入第"<<i+1<<"个新学生姓名"<<endl;
cin>>name;
cout<<"请输入第"<<i+1<<"个新学生性别"<<endl;
cin>>sex;
cout<<"请输入第"<<i+1<<"个新学生专业"<<endl;
cin>>major;
cout<<"请输入第"<<i+1<<"个新学生生日"<<endl;
cin>>birthday;
cout<<"请输入第"<<i+1<<"个新学生住址"<<endl;
cin>>home;
cout<<"请输入第"<<i+1<<"个新学生英语成绩"<<endl;
cin>>englishscore;
student * stu=NULL;
stu=new student(name,sex,major,birthday,home,englishscore);
newspace[this->i_studentnum+i]=stu;
}
delete [] this->student_p;
this->student_p=newspace;
this->i_studentnum=newsize;
this->panduanwenjian=false;
cout<<"成功添加"<<addnum<<"名学生"<<endl;
}
else
{
cout<<"输入有误"<<endl;
}
system("pause");
system("cls");
}
void function::shanchu() //删除
{
if(this->panduanwenjian)
{
cout<<"文件不存在或为空!"<<endl;
}
else
{
cout<<"请输入要删除的姓名:"<<endl;
string name;
cin>>name;
int index=this->panduanxuesheng(name);
if(index!=-1)
{
for(int i=index;i<this->i_studentnum-1;i++)
{
this->student_p[i]=this->student_p[i+1];
}
this->i_studentnum--;
this->cunwenjian();
cout<<"删除成功!"<<endl;
}
else
{
cout<<"删除失败,未找到该学生"<<endl;
}
}
system("pause");
system("cls");
}
void function::duwenjian() //读文件
{
ifstream ifs;
ifs.open("wenjian.txt",ios::in);
if(!ifs.is_open())
{
cout<<"文件不存在"<<endl;
this->i_studentnum=0;
this->panduanwenjian=true;
this->student_p=NULL;
ifs.close();
system("pause");
system("cls");
return ;
}
char ch;
ifs>>ch;
if(ifs.eof())
{
cout<<"文件为空"<<endl;
this->i_studentnum=0;
this->panduanwenjian=true;
this->student_p=NULL;
ifs.close();
system("pause");
system("cls");
return ;
}
int num=this->getnum();
// cout<<"文件中学生数为:"<<num<<endl; //测试
// system("pause");
this->i_studentnum=num;
this->student_p=new student * [this->i_studentnum];
init();
// for(int i=0;i<i_studentnum;i++) //测试
// {
// cout<<"姓名:"<<this->student_p[i]->s_name<<endl;
system("pause");
// }
}
void function::chazhao() //查找
{
if(this->panduanwenjian)
{
cout<<"文件不存在或为空!"<<endl;
}
else
{
cout<<"请输入要查找的学生姓名:"<<endl;
string name;
cin>>name;
bool flag=false;
for(int i=0;i<i_studentnum;i++)
{
if(student_p[i]->s_name==name)
{
cout<<"查找成功,信息如下:"<<endl;
flag=true;
this->student_p[i]->dayin();
}
}
if(flag==false)
{
cout<<"查找失败,查无此人"<<endl;
}
}
system("pause");
system("cls");
}
void function::xiugai() //修改
{
if(this->panduanwenjian)
{
cout<<"文件不存在或为空!"<<endl;
}
else
{
cout<<"请输入要修改的学生的姓名:"<<endl;
string name;
cin>>name;
int ret=this->panduanxuesheng(name);
if(ret!=-1)
{
delete this->student_p[ret];
string newname="";
string newsex="";
string newmajor="";
string newbirthday="";
string newhome="";
string newenglishscore="";
cout<<"查到"<<name<<"同学请输入新姓名:"<<endl;
cin>>newname;
cout<<"请输入新性别:"<<endl;
cin>>newsex;
cout<<"请输入新专业:"<<endl;
cin>>newmajor;
cout<<"请输入新生日:"<<endl;
cin>>newbirthday;
cout<<"请输入新住址:"<<endl;
cin>>newhome;
cout<<"请输入新英语成绩:"<<endl;
cin>>newenglishscore;
student * stu=NULL;
stu=new student(newname,newsex,newmajor,newbirthday,newhome,newenglishscore);
this->student_p[ret]=stu;
cout<<"修改成功!"<<endl;
}
else
{
cout<<"修改失败,查无此人!"<<endl;
}
}
system("pause");
system("cls");
}
void function::xianshi() //显示
{
if(this->panduanwenjian)
{
cout<<"文件不存在或为空!"<<endl;
}
else
{
for(int i=0;i<i_studentnum;i++)
{
this->student_p[i]->dayin();
}
}
system("pause");
system("cls");
}
void function::tongji() //统计
{
if(this->panduanwenjian)
{
cout<<"文件不存在或为空!"<<endl;
}
else
{
int a1=0,a2=0;
for(int i=0;i<i_studentnum;i++)
{
if(this->student_p[i]->s_sex=="男")
{
a1++;
}
else
{
a2++;
}
}
cout<<"男生人数:"<<a1<<endl;
cout<<"女生人数:"<<a2<<endl;
}
system("pause");
system("cls");
}
void function::paixu() //排序
{
if(this->panduanwenjian)
{
cout<<"文件不存在或为空!"<<endl;
}
else
{
for(int i=0;i<i_studentnum;i++)
{
int max=i;
for(int j=i+1;j<i_studentnum;j++)
{
if(student_p[max]->s_englishscore<student_p[j]->s_englishscore)
{
max=j;
}
}
if(i!=max)
{
student * temp=student_p[i];
student_p[i]=student_p[max];
student_p[max]=temp;
}
}
cout<<"排序成功,结果为:"<<endl;
this->cunwenjian();
this->xianshi();
}
}
void function::cunwenjian() //存文件
{
ofstream ofs;
ofs.open("wenjian.txt",ios::out);
for(int i=0;i<this->i_studentnum;i++)
{
ofs<<this->student_p[i]->s_name<<" "<<this->student_p[i]->s_sex<<" "<<this->student_p[i]->s_major<<" "<<this->student_p[i]->s_birthday<<" "<<this->student_p[i]->s_home<<" "<<this->student_p[i]->s_englishscore<<endl;
}
ofs.close();
}
void student::dayin() //打印
{
cout<<"姓名:"<<this->s_name<<"\t性别:"<<this->s_sex<<"\t专业:"<<this->s_major<<"\t生日:"<<this->s_birthday<<"\t住址:"<<this->s_home<<"\t英语成绩:"<<this->s_englishscore<<endl;
}
int student::getnum() //得文件中学生数量
{
ifstream ifs;
ifs.open("wenjian.txt",ios::in);
int num=0;
string s_name ; //姓名
string s_sex ; //性别
string s_major ; //专业
string s_birthday ; //生日
string s_home ; //住址
string s_englishscore; //英语成绩
while(ifs >> s_name && ifs >> s_sex && ifs >> s_major && ifs >> s_birthday && ifs >> s_home && ifs >> s_englishscore)
{
num++;
}
ifs.close();
return num;
}
void student::init() //初始化
{
ifstream ifs;
ifs.open("wenjian.txt",ios::in);
string s_name ; //姓名
string s_sex ; //性别
string s_major ; //专业
string s_birthday ; //生日
string s_home ; //住址
string s_englishscore; //英语成绩
int index=0;
while(ifs >> s_name && ifs >> s_sex && ifs >> s_major && ifs >> s_birthday && ifs >> s_home && ifs >> s_englishscore)
{
student * stu=NULL;
stu=new student(s_name,s_sex,s_major,s_birthday,s_home,s_englishscore);
this->student_p[index]=stu;
index++;
}
ifs.close();
}
int student::panduanxuesheng(string name) //判断学生
{
int index=-1;
for(int i=0;i<this->i_studentnum;i++)
{
if(this->student_p[i]->s_name==name)
{
index=i;
break;
}
}
return index;
}
void function::mulu() //目录
{
cout<<"**************************************"<<endl;
cout<<"*****欢迎使用新生基本信息统计软件*****"<<endl;
cout<<"\t1. 学生信息新增\t"<<endl;
cout<<"\t2. 学生信息删除\t"<<endl;
cout<<"\t3. 学生信息导入\t"<<endl;
cout<<"\t4. 学生信息搜索\t"<<endl;
cout<<"\t5. 学生信息修改\t"<<endl;
cout<<"\t6. 学生信息显示\t"<<endl;
cout<<"\t7. 学生信息统计(男/女)\t"<<endl;
cout<<"\t8. 按英语成绩排序\t"<<endl;
cout<<"\t9. 学生信息保存\t"<<endl;
cout<<"\t0. 退出\t"<<endl;
cout<<"**************************************"<<endl;
cout<<endl;
}
void function::tuichu() //退出
{
cout<<"--------------------"<<endl;
cout<<"谢谢使用"<<endl;
system("pause");
exit(0);
}
struct user{
string password;
string name;
};
void zhuce() //注册
{
system("cls");
string name;
char *password = new char[20];
user c;
ifstream fin;
fin.open("user.txt",ios::in);
cout<<"请输入用户名:";
cin>>name;
while(fin>>c.name>>c.password)
{
if(name==c.name)
{
cout<<"该用户名已被占用"<<endl;
system("pause");
zhuce();
}
}
char test;
int i=0;
cout<<"输入密码:";
while((test=getch())!='\r')
{
if(test==0)
{
if(i>0)
{
cout<<test<<" "<<test;
password[i--]='\0';
}
}
else
{
if(i<20)
{
cout<<"*";
password[i]=test;
i++;
}
}
}
password[i]='\0';
cout<<endl<<"再次输入密码:";
char *password2=new char[20];
i=0;
while((test=getch())!='\r')
{
if(test==0)
{
if(i>0)
{
cout<<test<<" "<<test;
password2[i--]='\0';
}
}
else
{
if(i<20)
{
cout<<"*";
password2[i]=test;
i++;
}
}
password2[i]='\0';
}
if(password==password2)
{
cout<<endl<<"两次密码不正确"<<endl;
system("pause");
zhuce();
}
else
{
cout<<endl<<"注册成功"<<endl;
system("pause");
}
ofstream fout;
fout.open("user.txt",ios_base::out|ios_base::app);
fout<<name<<" "<<password<<endl;
fout.close();
}
void denglu() //登录
{
system("cls");
char * password=new char[20];
string name;
cout<<"用户名:";
cin>>name;
cout<<"密码:";
char test;
int i=0;
while((test=getch())!='\r')
{
if(test==0)
{
if(i>0)
{
cout<<test<<" "<<test;
password[i--]='\0';
}
}
else
{
if(i<20)
{
cout<<"*";
password[i]=test;
i++;
}
}
}
password[i]='\0';
ifstream fin;
fin.open("user.txt",ios_base::in);
if(fin.fail())
{
cout<<"文件打开失败!"<<endl;
}
user c;
int f1;
while(fin>>c.name>>c.password)
{
if(c.name==name&&c.password==password)
{
cout<<endl<<"登陆成功"<<endl;
system("pause");
f1=1;
// ntreface();
}
}
if(f1==0)
{
cout<<"用户名或密码错误"<<endl;
system("pause");
denglu();
// logo();
}
}
void dengluzhuce() //登陆注册
{
while(1)
{
system("cls");
system("color 0b");
cout<<"*********欢迎使用新生基本信息统计软件*********"<<endl;
cout<<"\t\t1.登录\t\t"<<endl;
cout<<"\t\t2.注册\t\t"<<endl;
cout<<"\t\t3.退出\t\t"<<endl;
int a;
cin>>a;
switch(a)
{
case 1:
denglu();
return ;
break;
case 2:
zhuce();
break;
case 3:
exit(0);
break;
default:
break;
}
}
}
int main()
{
system("mode con cols=50 lines=25");
dengluzhuce();
function p1("0","0","0","0","0","0");
int a=1;
while(a)
{
system("color 07");
system("cls");
p1.mulu();
int b;
cin>>b;
switch(b)
{
case 1://新增学生信息
p1.zengjia();
break;
case 2://删除学生信息
p1.shanchu();
break;
case 3://导入学生信息(读取文件)
p1.duwenjian();
break;
case 4://学生信息搜索 (按姓名)
p1.chazhao();
break;
case 5://修改
p1.xiugai();
break;
case 6://显示
p1.xianshi();
break;
case 7://学生信息统计(按专业或性别或年龄)
p1.tongji();
break;
case 8://按英语成绩排序
p1.paixu();
break;
case 9://学生信息保存(入文件)
p1.cunwenjian();
break;
case 0://退出
p1.tuichu();
break;
default:
cout<<"指令错误!"<<endl;
system("pause");
system("cls");
break;
}
}
return 0;
}
8) 案例八
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
#include<windows.h>
using namespace std;
class student {
public:
int num;
char name[20];
float chi;
float eng;
float math;
float syn;
float total;
float paiming;
student *next;
};
class operate:public student {
private:
student*head;
student*p;
public:
operate();
void paixu();
void shuchuzongfen();
void conserve();
void input();
void maxmin();
void duqu();
void zongfendanke();
void gekepingjunfen();
void bukao();
void del();
void add();
void find();
void change();
};
operate::operate() { //创建头结点
head=new student;
head->next=NULL;
p=head;
}
void operate::input() {
p=head;
student*q;
int temp;
cout<<"请输入输入学生人数"<<endl;
cin>>temp;
for(int i=0; i<temp; i++) {
q=new student;
cout<<"\n请输入学生信息:\n"<<endl;
cout<<"学号:"<<endl;
cin>>q->num;
cout<<"姓名:"<<endl;
cin>>q->name;
cout<<"语文:"<<endl;
cin>>q->chi;
cout<<"数学:"<<endl;
cin>>q->math;
cout<<"英语:"<<endl;
cin>>q->eng;
cout<<"综合:"<<endl;
cin>>q->syn;
q->total=q->chi+q->eng+q->math+q->syn;
cout<<"总分:"<<q->total<<endl;
p->next=q;
p=p->next;
}
q->next=NULL;
cout<<"\n******************录入成功*******************\n";
cout<<"按任意键继续..."<<endl;
system("pause");
system("cls");
}
void operate::add() {
student*q,*s,*p;
p=head;
s=p->next;
q=new student;
cout<<"\n请输入学生信息:\n"<<endl;
cout<<"学号:"<<endl;
cin>>q->num;
cout<<"姓名:"<<endl;
cin>>q->name;
cout<<"语文:"<<endl;
cin>>q->chi;
cout<<"数学:"<<endl;
cin>>q->math;
cout<<"英语:"<<endl;
cin>>q->eng;
cout<<"综合:"<<endl;
cin>>q->syn;
q->total=q->chi+q->eng+q->math+q->syn;
cout<<"总分:"<<q->total<<endl;
p->next=q;
q->next=s;
cout<<"\n******************添加成功*******************\n";
cout<<"按任意键继续..."<<endl;
system("pause");
system("cls");
}
void operate::del() {
p=head;
student*q;
int delnum;
cout<<"输入要删除的学生学号"<<endl;
cin>>delnum;
while(p->next!=NULL) {
q=p->next;
if(delnum==q->num) {
p->next=q->next;
delete q;
} else {
p=p->next;
}
}
cout<<"删除成功"<<endl;
system("pause");
system("cls");
}
void operate::find() {
p=head;
int findnum;
cout<<"请输入查询的学生学号"<<endl;
cin>>findnum;
while(p->next!=NULL) {
if(p->next->num==findnum) {
cout<<"姓名:"<<p->next->name<<endl;
cout<<"学号:"<<p->next->num<<endl;
cout<<"语文:"<<p->next->chi<<endl;
cout<<"数学:"<<p->next->math<<endl;
cout<<"英语:"<<p->next->eng<<endl;
cout<<"综合:"<<p->next->syn<<endl;
cout<<"总分:"<<p->next->total<<endl;
cout<<"排名:"<<p->next->paiming<<endl;
break;
}
p=p->next;
if(p->next==NULL) {
cout<<"查无此人"<<endl;
}
}
system("pause");
}
void operate::change() {
p=head;
int ID;
cout<<"请输入要修改的学生学号"<<endl;
cin>>ID;
while(p->next!=NULL) {
if(p->next==NULL) {
cout<<"查无此人"<<endl;
}
if(p->next->num==ID) {
cout<<"请输入姓名:"<<endl;
cin>>p->next->name;
cout<<"请输入学号:"<<endl;
cin>> p->next->num;
cout<<"请输入语文成绩:"<<endl;
cin>>p->next->chi;
cout<<"请输入数学成绩:"<<endl;
cin>>p->next->math;
cout<<"请输入英语成绩:"<<endl;
cin>>p->next->eng;
cout<<"请输入综合成绩:"<<endl;
cin>>p->next->syn;
p->next->total =p->next->math +p->next->eng +p->next->chi+p->next->syn;
cout<<"总成绩:"<<p->next->total<<endl;
}
p=p->next;
}
cout<<"修改学生信息完毕"<<endl;
system("pause");
system("CLS");
}
void operate::paixu() {
student *q;
p=head;
int n=0;
int a;
char b[20];
int i,j;
while(p->next) {
p=p->next ;
n++;
p->paiming=n;
}
for(i=0; i<n-1; i++) {
q=head->next;
p=q->next;
for(j=0; j<n-i-1; j++) {
if(p->total>q->total ) {
a=q->num ;
q->num =p->num ;
p->num =a;
a=q->chi;
q->chi=p->chi;
p->chi=a;
a=q->math ;
q->math =p->math ;
p->math =a;
a=q->eng ;
q->eng =p->eng ;
p->eng =a;
a=q->syn;
q->syn =p->syn ;
p->syn=a;
a=q->total;
q->total =p->total ;
p->total =a;
strcpy(b,q->name);
strcpy(q->name ,p->name );
strcpy(p->name,b);
}
p=p->next;
q=q->next;
}
}
}
void operate::shuchuzongfen() {
p=head;
student*q=p->next;
int n=0;
cout<<"排名"<<"\t"<<"学号"<<"\t"<<"姓名"<<"\t"<<"语文"<<"\t"<<"数学"<<"\t"<<"英语"<<"\t"<<"综合"<<"\t"<<"总分"<<endl;
while(q!=NULL) {
cout<<q->paiming<<"\t"<<q->num<<"\t"<<q->name<<"\t"<<q->chi<<"\t"<<q->math<<"\t"<<q->eng<<"\t"<<q->syn<<"\t"<<q->total<<endl;
q=q->next;
}
cout<<"输出完毕"<<endl;
system("pause");
system("cls");
}
void operate::conserve() {
p=head;
ofstream outfile; // 定义输出文件流 outfile
outfile.open("学生信息.txt",ios::out|ios::app);
while(p->next!=NULL) {
outfile<<"学号:"<<p->next->num<<" ";
outfile<<"姓名:"<<p->next->name<<" ";
outfile<<"语文:"<<p->next->chi<<" ";
outfile<<"英语:"<<p->next->eng<<" ";
outfile<<"数学:"<<p->next->math<<" ";
outfile<<"综合:"<<p->next->syn<<" ";
outfile<<"总分:"<<p->next->total<<" ";
outfile<<"排名:"<<p->next->paiming<<" ";
outfile<<"\n";
p=p->next;
}
outfile.close();
cout<<"保存学生信息成功"<<endl;
system("pause");
system("cls");
}
void operate::maxmin() {
p=head;
cout<<"分数最高的学生:"<<p->next->name<<endl;
while(p->next!=NULL) {
p=p->next;
}
cout<<"分数最低的学生:"<<p->name<<endl;
cout<<"输出完毕"<<endl;
system("pause");
system("cls");
}
void operate::duqu() {
ifstream infile;
infile.open("学生信息.txt",ios::in);
p=head;
cout<<"排名"<<"\t"<<"学号"<<"\t"<<"姓名"<<"\t"<<"语文"<<"\t"<<"数学"<<"\t"<<"英语"<<"\t"<<"综合"<<"\t"<<"总分"<<endl;
while(p->next!=NULL) {
cout<<p->next->paiming<<"\t"<<p->next->num<<"\t"<<p->next->name<<"\t"<<p->next->chi<<"\t"<<p->next->math<<"\t"<<p->next->eng<<"\t"<<p->next->syn<<"\t"<<p->next->total<<endl;
p=p->next;
}
infile.close();
cout<<"输出完毕"<<endl;
system("pause");
system("cls");
}
void operate::zongfendanke() {
p=head->next;
while(p!=NULL) {
if(p->total>=340&&p->chi>=80&&p->math>=80&&p->eng>=80&&p->syn>=80) {
cout<<"总分在340分,单科成绩不低于80分的学生:"<<p->name<<endl;
}
p=p->next;
}
cout<<"输出完毕"<<endl;
system("pause");
system("cls");
}
void operate::gekepingjunfen() {
p=head;
int n=0;
float a=0,b=0,c=0,d=0;
float e,f,g,h;
while(p!=NULL) {
n++;
a=p->chi+a;
b=p->math+b;
c=p->eng+c;
d=p->syn+d;
p=p->next;
}
e=a/(n-1);
f=b/(n-1);
g=c/(n-1);
h=d/(n-1);
cout<<"语文平均分:"<<e<<endl;
cout<<"数学平均分:"<<f<<endl;
cout<<"英语平均分:"<<g<<endl;
cout<<"综合平均分: "<<h<<endl;
cout<<"输出完毕"<<endl;
system("pause");
system("cls");
}
void operate::bukao() {
p=head->next;
ofstream outfile; // 定义输出文件流 outfile
outfile.open("补考名单.txt",ios::out|ios::app);
while(p!=NULL) {
if(p->chi<60&&p->math<60&&p->eng<60&&p->syn<60) {
cout<<"补考学生名单:"<<p->name<<endl;
outfile<<"姓名:"<<p->name<<"\n";
}
p=p->next;
}
outfile.close();
cout<<"输出完毕,已存入文件"<<endl;
system("pause");
system("cls");
}
int menu() {
cout<<" ********************************** \n"<<endl;
cout<<" ***********目录************\n"<<endl;
cout<<" 输入学生信息:1\n"<<endl;
cout<<" 增加学生信息:2\n"<<endl;
cout<<" 删除学生信息:3\n"<<endl;
cout<<" 查询学生信息:4\n"<<endl;
cout<<" 修改学生信息:5\n"<<endl;
cout<<" 保存学生信息:6\n"<<endl;
cout<<" 读取学生信息:7\n"<<endl;
cout<<" 输出总分最高及最低学生:8\n"<<endl;
cout<<"总分在340分,单科成绩不低于80分学生名单:9\n"<<endl;
cout<<" 求出各科平均分数:10\n"<<endl;
cout<<" 按总分排名:11\n"<<endl;
cout<<" 输出补考名单并存入文件:12\n"<<endl;
cout<<" 退出程序:13\n"<<endl;
cout<<" ********************************** \n"<<endl;
}
int main() {
system("color f5");
int m;
operate x;
while(1) {
menu();
cin>>m;
switch(m) {
case 1:
x.input();
break;
case 2:
x.add();
break;
case 3:
x.del();
break;
case 4:
x.paixu();
x.find();
break;
case 5:
x.change();
break;
case 6:
x.paixu();
x.conserve();
break;
case 7:
x.duqu();
break;
case 8:
x.paixu();
x.maxmin();
break;
case 9:
x.zongfendanke();
break;
case 10:
x.gekepingjunfen();
break;
case 11:
x.paixu();
x.shuchuzongfen();
break;
case 12:
x.bukao();
break;
case 13:
cout<<"欢迎下次使用"<<endl;
return 0;
break;
}
}
}
9) 案例九
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstring>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int n=0;
char stop[12]= {"0"};
char name[12];
class Student {
public:
int id;
char name[12];
char sex[3];
char major[20];
char addr[30];
char year[5];
char month[3];
char day[3];
int engli;
char remark[100];
Student *next;
};
class Utils{
public:
virtual Student *add(Student *head)=0;
virtual Student *delet(Student *head)=0;
virtual void *search(Student *head)=0;
virtual Student *modify(Student *head)=0;
};
class information : public Utils {
private:
Student *head,*p1,*p2,*p3;
public:
information(){};
Student *creat();
Student *add(Student *head);
Student *delet(Student *head);
void *search(Student *head);
Student *modify(Student *head);
Student *sort(Student*head);
Student *statistics(Student*head);
void display(Student *head);
void displaynode(Student *head);
void write_file(Student *head);
Student * read_file();
~information () {};
};
class manage {
public:
char menu_manager();
char menu_user();
int choose_manager();
int choose_user(Student *head);
};
information s;
Student *information::creat() {
p1=p2=new Student;
head=NULL;
char sex1[3];
char sex2[3];
strcpy(sex1, "男");
strcpy(sex2, "女");
cout<<"请输入学生的基本信息:以姓名为0结束。\n";
while(1) {
cout<<"姓名: \t";
cin>>p1->name;
if( strcmp(stop,p1->name)==0 ) {
break;
}
cout<<"学号: \t";
cin>>p1->id;
cout<<"性别: \t";
cin>>p1->sex;
while(strcmp(sex1,p1->sex)!=0 &&strcmp(sex2,p1->sex)!=0 ) {
cout<<"请输入格式 男/女,请重新输入!"<<endl;
cout<<"性别: \t";
cin>>p1->sex;
}
cout<<"专业: \t";
cin>>p1->major;
cout<<"家庭住址: \t";
cin>>p1->addr;
cout<<"出生年:";
cin>>p1->year;
cout<<"出生月:";
cin>> p1->month;
cout<<"出生日:";
cin>>p1->day;
cout<<"英语:";
cin>>p1->engli;
while(p1->engli<0 || p1->engli>100) {
cout<<"英语成绩必须在0-100之间,请重新输入!"<<endl;
cout<<"英语:";
cin>>p1->engli;
}
cout<<"备注:";
cin>>p1->remark;
system("cls");
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=new Student;
}
p2->next=NULL;
return head;
}
Student *information::statistics(Student*head) {
p1=new Student;
p1=head;
int x=0,y=0;
char sex1[3];
char sex2[3];
strcpy(sex1, "男");
strcpy(sex2, "女");
for(p1=head; p1!=NULL; p1=p1->next) {
if(strcmp(sex1,p1->sex)==0 ) {
x=x+1;
}
if(strcmp(sex2,p1->sex)==0) {
y=y+1;
}
}
cout<<"男: "<<x<<endl;
cout<<"女: "<<y<<endl;
return head;
}
Student *information::add(Student *head) {
p1=p3=new Student;
p1=head;
char sex1[3];
char sex2[3];
strcpy(sex1, "男");
strcpy(sex2, "女");
if(p1==NULL) {
cout<<"在添加前,先新建!"<<endl;
return 0;
} else {
cout<<"请输入要添加学生的信息!\n";
cout<<"姓名: \t";
cin>>p3->name;
cout<<"学号: \t";
cin>>p3->id;
cout<<"性别: \t";
cin>>p3->sex;
while(strcmp(sex1,p3->sex)!=0 &&strcmp(sex2,p3->sex)!=0 ) {
cout<<"请输入格式 男/女,请重新输入!"<<endl;
cout<<"性别: \t";
cin>>p3->sex;
}
cout<<"专业: \t";
cin>>p3->major;
cout<<"家庭住址: \t";
cin>>p3->addr;
cout<<"出生年:";
cin>>p3->year;
cout<<"出生月:";
cin>> p3->month;
cout<<"出生日:";
cin>>p3->day;
cout<<"英语:";
cin>>p3->engli;
while(p3->engli<0 || p3->engli>100) {
cout<<"英语成绩必须在0-100之间,请重新输入!"<<endl;
cout<<"英语:";
cin>>p3->engli;
}
cout<<"备注:";
cin>>p3->remark;
cout<<"\n";
p3->next=p1->next;
p1->next=p3;
cout<<"添加成功!\n";
}
return head;
}
Student *information::delet(Student *head) {
p2=p1=new Student;
if(head==NULL) {
cout<<"没有数据,无法删除";
return 0;
}
cout<<"请输入要删除学生的名字:\n";
cin>>name;
p2=p1=head;
int j=0;
if( ( strcmp(name,head->name)==0 ) && (head!=NULL)) {
head=head->next;
free(p1);
j=1;
} else {
p1=head->next;
while(p1!=NULL) {
if(strcmp(name,p1->name)==0) {
p2->next=p1->next;
free(p1);
j=1;
break;
} else {
p2=p1;
p1=p2->next;
}
}
}
if(j==0)
cout<<"此学生不存在,删除失败!\n";
else
cout<<"删除成功!\n";
return head;
}
void *information::search(Student *head) {
char name[20];
p1=new Student;
cout<<"请输入要查找学生的姓名:\n";
cin>>name;
p1=head;
for(p1=head; p1!=NULL; p1=p1->next) {
if(strcmp(name,p1->name)==0 ) {
cout<<"学号:"<<p1->id<<endl;
cout<<"姓名:"<<p1->name<<endl;
cout<<"性别:"<<p1->sex<<endl;
cout<<"专业:"<<p1->major<<endl;
cout<<"家庭住址:"<<p1->addr<<endl;
cout<<"出生日期:"<<p1->year<<"年"<<p1->month<<"月"<<p1->day<<"日出生"<<endl;
cout<<"英语成绩:"<<p1->engli<<endl;
cout<<"备注:"<<p1->remark<<endl;
}
}
return head;
}
Student *information::modify(Student *head) {
if(head==NULL) {
cout<<"没有数据,请新建数据!";
return 0;
}
char name1[12];
char sex[3];
char major[20];
char addr[30];
char year[5];
char month[3];
char day[3];
int engli;
char remark[100];
p1=new Student;
int j=0;
p1=head;
cout<<"请输入你要更改学生的姓名:\n";
cin>>name;
if(strcmp( name, head->name)==0) {
cout<<"显示要修改学生的信息:\n";
s.displaynode(p1);
cout<<"请输入要更改学生的信息:\n";
cout<<"姓名: \t";
cin>>name1;
cout<<"性别: \t";
cin>>sex;
cout<<"专业: \t";
cin>>major;
cout<<"家庭住址: \t";
cin>>addr;
cout<<"出生年:";
cin>>year;
cout<<"出生月:";
cin>>month;
cout<<"出生日:";
cin>>day;
cout<<"英语:";
cin>>engli;
cout<<"备注:";
cin>>remark;
strcpy(head->name,name1);
strcpy(head->sex,sex);
strcpy(head->major,major);
strcpy(head->addr,addr);
strcpy(head->year,year);
strcpy(head->month,month);
strcpy(head->day,day);
head->engli=engli;
strcpy(head->remark,remark);
j=1;
} else {
p1=head->next;
while(p1!=NULL) {
if(strcmp(p1->name,name)==0)
cout<<"显示要修改学生的信息:\n";
s.displaynode(p1);
cout<<"请输入要更改学生的信息:\n";
cout<<"姓名: \t";
cin>>name1;
cout<<"性别: \t";
cin>>sex;
cout<<"专业: \t";
cin>>major;
cout<<"家庭住址: \t";
cin>>addr;
cout<<"出生年:";
cin>>year;
cout<<"出生月:";
cin>>month;
cout<<"出生日:";
cin>>day;
cout<<"英语:";
cin>>engli;
cout<<"备注:";
cin>>remark;
strcpy(p1->name,name1);
strcpy(p1->sex,sex);
strcpy(p1->major,major);
strcpy(p1->addr,addr);
strcpy(head->year,year);
strcpy(head->month,month);
strcpy(head->day,day);
p1->engli=engli;
strcpy(head->remark,remark);
j=1;
break;
}
}
if(j==0)
cout<<"没有找到你要更改的学生,更改失败!\n";
else
cout<<"更改成功!\n";
return head;
}
void information::display(Student *head) {
p1=head;
if(p1==NULL)
cout<<"没有学生信息,请先输入学生信息。"<<endl;
else {
while(p1!=NULL) {
s.displaynode(p1);
p1=p1->next;
}
}
}
void information::displaynode(Student *head) {
p1=head;
if(p1==NULL)
cout<<"这是一个空表!请先输入学生信息。"<<endl;
else {
cout<<"学号:"<<p1->id<<endl;
cout<<"姓名:"<<p1->name<<endl;
cout<<"性别:"<<p1->sex<<endl;
cout<<"专业:"<<p1->major<<endl;
cout<<"家庭住址:"<<p1->addr<<endl;
cout<<"生日: "<<p1->year<<"年";
cout<<p1->month<<"月"<<p1->day<<"日"<<endl;
cout<<"英语成绩:"<<p1->engli<<endl;
cout<<"备注:"<<p1->remark<<endl;
}
}
Student *information::read_file() {
int i=0;
int id;
char name2[12];
char major[20];
char sex[3];
char addr[30];
char year[5];
char month[3];
char day[3];
int engli;
char remark[100];
p1=p2=new Student;
head=NULL;
ifstream in;
in.open("E:\\新生管理系统.txt");
if(!in) {
cout<<"打开文件失败!"<<endl;
} else {
while(!in.eof()) {
in>>id>>name2>>sex>>major>>addr>>year>>month>>day>>engli>>remark;
//判断是否到文件的末尾
if(in.fail()){
break;
}
p1->id=id;
strcpy(p1->name,name2);
strcpy(p1->sex,sex);
strcpy(p1->major,major);
strcpy(p1->addr,addr);
strcpy(p1->year,year);
strcpy(p1->month,month);
strcpy(p1->day,day);
p1->engli=engli;
strcpy(p1->remark,remark);
i++;
if(i==1) {
head=p2=p1;
} else {
p2->next=p1;
}
p2=p1;
p1=new Student;
p1->next=NULL;
}
cout<<"成功读取文件!内容如下:"<<endl;
}
return head;
}
void information::write_file(Student *head) {
ofstream out;
out.open("E:\\新生管理系统.txt");
if(!out) {
cout<<"打开文件失败!"<<endl;
}
p1=NULL;
p1=head;
while(p1) {
out<<p1->id<<" "<<p1->name<<" "<<p1->sex<<" "<<p1->major<<" "<<p1->addr<<" "<<p1->year<<" "<<p1->month<<" "<<p1->day<<" "<<p1->engli<<" "<<p1->remark<<endl;
p1=p1->next;
}
out.close();
cout<<"写入完毕。";
}
Student *information::sort(Student *head) {
Student *p,*q,*small;
int id;
char name[12];
char sex[3];
char major[20];
char addr[30];
char year[5];
char month[3];
char day[3];
int engli;
char remark[100];
p=head;
for(; p->next!=NULL; p=p->next) {
small=p;
for(q=p->next; q; q=q->next) {
if(q->engli<small->engli) {
small=q;
}
}
if(small!= p) {
id=p->id;
p->id=small->id;
small->id=id;
engli=p->engli;
p->engli=small->engli;
small->engli=engli;
strcpy(name,p->name);
strcpy(p->name,small->name);
strcpy(small->name,name);
strcpy(major,p->major);
strcpy(p->major,small->major);
strcpy(small->major,major);
strcpy(year,p->year);
strcpy(p->year,small->year);
strcpy(small->year,year);
strcpy(month,p->month);
strcpy(p->month,small->month);
strcpy(small->month,month);
strcpy(day,p->day);
strcpy(p->day,small->day);
strcpy(small->day,day);
strcpy(addr,p->addr);
strcpy(p->addr,small->addr);
strcpy(small->addr,addr);
strcpy(sex,p->sex);
strcpy(p->sex,small->sex);
strcpy(small->sex,sex);
strcpy(remark,p->remark);
strcpy(p->remark,small->remark);
strcpy(small->remark,remark);
}
}
return head;
}
char manage::menu_manager() {
char ch;
cout<<endl;
cout<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" *****************新生管理系统**************** "<<endl;
cout<<" * 1.新增学生信息 * "<<endl;
cout<<" * 2.插入学生信息 * "<<endl;
cout<<" * 3.删除学生信息 * "<<endl;
cout<<" * 4.查找学生信息 * "<<endl;
cout<<" * 5.修改学生信息 * "<<endl;
cout<<" * 6.按照英语成绩排序 * "<<endl;
cout<<" * 7.统计男/女人数 * "<<endl;
cout<<" * 8.读取学生信息 * "<<endl;
cout<<" * 9.保存信息退出 * "<<endl;
cout<<" * 0.退出 * "<<endl;
cout<<" *********************************************"<<endl;
cout<<"请根据需要选择序号:"<<endl;
cin>>ch;
return ch;
}
char manage::menu_user() {
char ch;
cout<<endl;
cout<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" *****************新生管理系统**************** "<<endl;
cout<<" * 1.查找学生信息 * "<<endl;
cout<<" * 2.按照英语成绩排序 * "<<endl;
cout<<" * 3.统计男/女人数 * "<<endl;
cout<<" * 4.查询所有学生信息 * "<<endl;
cout<<" * 0.退出 * "<<endl;
cout<<" *********************************************"<<endl;
cout<<"请根据需要选择序号:"<<endl;
cin>>ch;
return ch;
}
int manage::choose_manager() {
manage m;
information s;
Student *head;
head=NULL;
int n=0;
char c;
while(1)
switch (m.menu_manager()) {
case'1':
head=s.creat();
system("pause");
system("cls");
break;
case'2':
head=s.add(head);
system("pause");
system("cls");
break;
case'3':
head=s.delet(head);
system("pause");
system("cls");
break;
case'4':
s.search(head);
system("pause");
system("cls");
break;
case'5':
head=s.modify(head);
system("pause");
system("cls");
break;
case'6':
s.sort(head);
s.display(head);
system("pause");
system("cls");
break;
case'7':
s.statistics(head);
system("pause");
system("cls");
break;
case'8':
head=s.read_file();
s.display(head);
system("pause");
system("cls");
break;
case'9':
s.write_file(head);
cout<<"谢谢使用!\n";
system("pause");
system("cls");
return 0;
case'0':
cout<<"谢谢使用!\n";
system("pause");
system("cls");
exit(0);
default:
cout<<"选择有错,请重新选择\n";
}
}
int manage::choose_user(Student *p) {
manage m;
information s;
Student *head=p;
int n=0;
char c;
while(1)
switch (m.menu_user()) {
case'1':
s.search(head);
system("pause");
system("cls");
break;
case'2':
s.sort(head);
s.display(head);
system("pause");
system("cls");
break;
case'3':
s.statistics(head);
system("pause");
system("cls");
break;
case'4':
s.display(head);
system("pause");
system("cls");
break;
case'0':
cout<<"谢谢使用!\n";
system("pause");
system("cls");
exit(0);
default:
cout<<"选择有错,请重新选择\n";
}
}
//简易版等待动画
void Wait()
{
int i = 0,k = 3;
Sleep(1000);
for (i = 0; i < 3;) {
system("cls");
cout << k-- << endl;
i++;
Sleep(1000);
}
}
//升级版等待动画
void Wait1()
{
Sleep(600);
system("cls");
int i,k,count;
long int a,b;
i=k=count=0;
while(!kbhit())
{
if(count==3){
break;
}
if(k==0)
{
printf("Please wait");
}
a=b=0;
for(a=0;a<6000;a++)
for(b=0;b<6000;b++);
printf(".");
a=b=0;
for(a=0;a<6000;a++)
for(b=0;b<6000;b++);
k++;
if(k==6)
{
k=0;
count++;
system("cls");
}
}
}
int login(){
int i;
cout<<" *-------------------------------------------------------*"<<endl;
cout<<" * 欢迎使用新生报到管理系统 *"<<endl;
cout<<" * ------------------------- *"<<endl;
cout<<" * 1.管理员 *"<<endl;
cout<<" * 2.普通用户 *"<<endl;
cout<<" * *"<<endl;
cout<<" * ------------------------------------------------------* "<<endl;
cout << "请选择登录身份:";
cin >> i;
return i;
}
int main () {
system("color 7C");
int i;
char ch;
char str[20];
char password[9]= {"123456"};
information f;
Student *head;
while(1){
system("cls");
int choose = login();
if(choose == 1){
while(1) {
i=0;
system("cls");
cout<<"输入密码:";
ch=getch() ;
while(ch!=13) {
if(ch==8&&i>0) {
i--;
str[i]='\0';
system("cls");//清屏 重新输出*
cout<<"输入密码:";
for(int j=0; j<i; j++) {
cout<<"*";
}
ch=getch();
} else {
printf("*");
str[i]=ch;
i++;
ch=getch();
}
}
str[i]='\0';
if(!strcmp(str,password)) {
system("cls");
cout<<"正确";
break;
} else {
cout<<"\n密码错误!!";
getch();
system("cls");
}
}
Wait1();
system("cls");
system("color 7d");
manage m;
m.choose_manager();
}else if(choose == 2){
head=f.read_file();
Wait();
system("cls");
system("color 7d");
manage m;
m.choose_user(head);
}else{
cout << "输入错误,请重新输入" << endl;
system("pause");
}
}
return 0;
}
10)案例十
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<fstream>
#include<windows.h>
using namespace std;
class student
{
public:
void scanf_student();
void print_student();
student *next;
char name[20];
char sex[10];
int year;
int month;
int day;
char major[20];
char grade;
char address[30];
long long int number;
float chinese_score;
float math_score;
float English_score;
void save_file(student *head);
void look_file(student *head);
};
void student::scanf_student()
{
cout<<"\n\n";
cout<<" 请输入学生姓名:";
cin>>name;
cout<<" 请输入学生性别:";
cin>>sex;
cout<<" 请输入学生专业:";
cin>>major;
cout<<" 请输入学生班级:";
cin>>grade;
cout<<" 请输入学生家庭地址:";
cin>>address;
cout<<" 请输入学生家庭电话:";
cin>>number;
cout<<" 请输入学生入学语文成绩;";
cin>>chinese_score;
cout<<" 请输入学生入学数学成绩;";
cin>>math_score;
cout<<" 请输入学生入学英语成绩;";
cin>>English_score;
}
void student::print_student()
{
cout<<"\n\n";
cout<<" 学生姓名:"<<name<<endl;
cout<<"\n";
cout<<" 学生性别:"<<sex<<endl;
cout<<"\n";
cout<<" 学生专业:"<<major<<endl;
cout<<"\n";
cout<<" 学生班级:"<<grade<<endl;
cout<<"\n";
cout<<" 学生家庭地址:"<<address<<endl;
cout<<"\n";
cout<<" 学生家庭电话:"<<number<<endl;
cout<<"\n";
cout<<" 学生入学语文成绩:"<<chinese_score<<endl;
cout<<"\n";
cout<<" 学生入学数学成绩:"<<math_score<<endl;
cout<<"\n";
cout<<" 学生入学英语成绩:"<<English_score<<endl;
cout<<"\n\n";
}
void student::save_file(student *head)
{
student *p;
p=head->next;
ofstream ofile("入学新生信息.txt",ios::out);
while(p!=NULL)
{
ofile<<p->name<<'\t'<<p->sex<<'\t'<<p->major<<'\t'<<p->grade<<'\t'<<p->address<<'\t'<<p->number<<'\t'<<p->chinese_score<<'\t'<<p->math_score<<'\t'<<p->English_score<<'\n';
p=p->next;
}
ofile.close();
cout<<"\n\n保存成功!\n\n";
}
void student::look_file(student *head)
{
student *p1,*p2;
p1=p2=head;
ifstream ifile("入学新生信息.txt");
if(!ifile)
{
cout<<"\n打开失败!"<<endl;
exit(0);
}
while(!ifile.eof())
{
p1=new student;
if(p1==NULL)
{
cout<<"\n创建失败!"<<endl;
exit(0);
}
ifile>>p1->name>>p1->sex>>p1->major>>p1->grade>>p1->address>>p1->number>>p1->chinese_score>>p1->math_score>>p1->English_score;
p2->next=p1;
p2=p1;
}
ifile.close();
p2->next=NULL;
cout<<"\n\n文件已打开!\n\n";
}
class function:public student
{
public:
void creat(student *head);
void show(student *head);
void amend(student *head);
void search(student *head);
void add(student *head);
void delet(student *head);
void sort(student *head);
void count(student *head);
};
void function::creat(student *head)
{
student *p1,*p2;
p1=p2=head;
int z,i;
cout<<"\n请输入要输入新生的人数:";
cin>>z;
system("CLS");
for(i=0;i<z;i++)
{
p1=new student;
if(p1==NULL)
{
cout<<"\n输入新生信息失败,请重新输入!\n\n";
system("CLS");
function school;
school.creat(head);
}
cout<<"\n*** 第"<<i+1<<"个新生 ***";
p1->scanf_student();
p2->next=p1;
p2=p1;
}
p2->next=NULL;
system("CLS");
cout<<"\n\n新生信息录入完毕!\n\n";
}
void function::show(student *head)
{
student *p=head->next;
int a=1;
if(p==NULL)
{
cout<<"\n\n没有新生信息!\n"<<endl;
}
while(p!=NULL)
{
cout<<"\n*** 第"<<a<<"名新生 ***";
p->print_student();
p=p->next;
a++;
}
}
void function::amend(student *head)
{
student *p;
p=head->next;
char name[10],a[10]=" ";
cout<<"\n请输入要修改学生的姓名:";
cin>>name;
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
strcpy(a,name);
break;
}
p=p->next;
}
if(strcmp(a," ")==0)
{
system("CLS");
cout<<"\n无该学生信息!"<<endl;
function school;
school.amend(head);
return;
}
p->print_student();
cout<<"修改为:"<<endl;
p->scanf_student();
system("CLS");
cout<<"\n\n修改成功!\n\n";
}
void function::search(student *head)
{
int c;
char major[10],name[10],a[10]=" ";
student *p=head->next;
cout<<"\n*请输入查找学生的姓名:";
cin>>name;
system("CLS");
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
{
strcpy(a,name);
cout<<"\n—— 学生信息 —— "<<endl;
p->print_student();
}
p=p->next;
}
if(strcmp(a," ")==0)
{
system("CLS");
cout<<"\n无该学生信息!"<<endl;
function school;
school.search(head);
}
}
void function::add(student *head)
{
int i,n;
cout<<"\n请输入要输入学生的人数:";
cin>>n;
for(i=0;i<n;i++)
{
student *p=head,*p1;
while(p->next!=NULL)
{
p=p->next;
}
p1=new student;
cout<<"\n请输入第"<<i+1<<"个新生信息:"<<endl;
p1->scanf_student();
p1->next=NULL;
p->next=p1;
}
system("CLS");
cout<<"\n\n添加成功!\n\n";
}
void function::delet(student *head)
{
student *p,*q;
p=head;
char name[10],a[10]=" ";
cout<<"\n请输入要删除新生的姓名:";
cin>>name;
while(p->next!=NULL)
{
if(strcmp(p->next->name,name)==0)
{
strcpy(a,name);
break;
}
p=p->next;
}
if(strcmp(a," ")==0)
{
system("CLS");
cout<<"\n没有这个新生的信息,请重新输入!\n";
function school;
school.delet(head);
return;
}
q=p->next;
p->next=q->next;
system("CLS");
cout<<"\n\n删除成功!\n\n" ;
}
void function::sort(student *head)
{
student *p,*q;
p=head;
int sum=-1;
while(p!=NULL)
{
sum++;
p=p->next;
}
int i,j;
float a;
char k[50];
for(i=0;i<sum;i++)
{
p=head->next;
q=p->next;
for(j=0;j<sum-1;j++)
{
if(p->English_score<p->next->English_score)
{
a=p->next->chinese_score;
p->next->chinese_score=p->chinese_score;
p->chinese_score=a;
a=p->next->math_score;
p->next->math_score=p->math_score;
p->math_score=a;
a=p->next->English_score;
p->next->English_score=p->English_score;
p->English_score=a;
strcpy(k,p->next->name);
strcpy(p->next->name,p->name);
strcpy(p->name,k);
strcpy(k,p->next->sex);
strcpy(p->next->sex,p->sex);
strcpy(p->sex,k);
strcpy(k,p->next->major);
strcpy(p->next->major,p->major);
strcpy(p->major,k);
a=p->next->grade;
p->next->grade=p->grade;
p->grade=a;
strcpy(k,p->next->address);
strcpy(p->next->address,p->address);
strcpy(p->address,k);
a=p->next->number;
p->next->number=p->number;
p->number=a;
}
p=p->next;
}
}
system("CLS");
cout<<"\n共"<<sum<<"个新生!"<<endl;
cout<<"\n按英语入学成绩排序:\n" ;
int r=1;
q=head->next;
while(q!=NULL)
{
cout<<"\n*** 第"<<r<<"名 ***";
q->print_student();
q=q->next;
r++;
}
}
void function::count(student *head)
{
system("CLS");
student *p;
p=head->next;
cout<<"\n —— 男生 ——\n";
cout<<"\n**************************\n\n";
while(p!=NULL)
{
if(strcmp(p->sex,"男")==0)
{
cout<<" 男生姓名如下: ";
cout<<p->name<<endl;
cout<<"\n";
}
p=p->next;
}
cout<<"**************************"<<endl;
p=head->next;
cout<<"\n\n\n —— 女生 ——\n";
cout<<"\n**************************\n\n";
while(p!=NULL)
{
if(strcmp(p->sex,"女")==0)
{
cout<<" 女生姓名如下: ";
cout<<p->name<<endl;
cout<<"\n";
}
p=p->next;
}
cout<<"**************************\n\n\n\n"<<endl;
}
int main()
{
int a;
cout<<"\n\n\t\t";
system("color 1");
cout<<"欢";
Sleep(60);
system("color 2");
cout<<"迎";
Sleep(60);
system("color 3");
cout<<"使";
Sleep(60);
system("color 4");
cout<<"用";
Sleep(60);
system("color 5");
cout<<"学";
Sleep(60);
system("color 6");
cout<<"生";
Sleep(60);
system("color 7");
cout<<"成";
Sleep(60);
system("color 8");
cout<<"绩";
Sleep(60);
system("color 1");
cout<<"管";
Sleep(60);
system("color 2");
cout<<"理";
Sleep(60);
system("color 3");
cout<<"系";
Sleep(60);
system("color 8");
cout<<"统\n";
Sleep(60);
system("color f4");
student *head;
head=new student;
if(head==NULL)
{
cout<<"\n\n 开辟失败!\n"<<endl;
exit(0);
}
function school;
while(1)
{
cout<<"\n";
cout<<"==========================================================\n";
cout<<"= 1 . 录入学生信息 =\n";
cout<<"= 2 . 添加学生信息 =\n";
cout<<"= 3 . 显示学生信息 =\n";
cout<<"= 4 . 搜索学生信息 =\n";
cout<<"= 5 . 修改学生信息 =\n";
cout<<"= 6 . 删除学生信息 =\n";
cout<<"= 7 . 统计学生信息 =\n";
cout<<"= 8 . 英语成绩排序 =\n";
cout<<"= 9 . 保存学生信息 =\n";
cout<<"= 10. 打开学生文件 =\n";
cout<<"= 0 . 退出信息系统 =\n";
cout<<"==========================================================\n";
cout<<"\n*** 请选择:";
scanf("%d",&a);
switch(a)
{
case 1:system("CLS");school.creat(head);break;
case 2:system("CLS");school.add(head);break;
case 3:system("CLS");school.show(head);break;
case 4:system("CLS");school.search(head);break;
case 5:system("CLS");school.amend(head);break;
case 6:system("CLS");school.delet(head);break;
case 7:system("CLS");school.count(head);break;
case 8:system("CLS");school.sort(head);break;
case 9:system("CLS");school.save_file(head);break;
case 10:system("CLS");school.look_file(head);break;
case 0:system("CLS");
cout<<"\n";
cout<<"=============================================================\n";
cout<<"|| ||\n";
cout<<"|| ||\n";
cout<<"|| 再见! ||\n";
cout<<"|| ||\n";
cout<<"|| ||\n";
cout<<"=============================================================\n";
exit(0);
default:system("CLS");cout<<"输入有误!"<<endl;
}
}
return 0;
}
11)案例十一
#include<iostream>
#include<vector>//vector容器的头文件
#include<fstream>//使用文件的头文件
#include<iomanip>
#include<windows.h>
using namespace std;
class Human {
public:
string hight;
string weight;
void Action() {
cout<<"laugh"<<endl;
cout<<"cry"<<endl;
cout<<"learn"<<endl;
}
};
class Student:public Human { //学生类
public://数据成员
string number;//string为字符串类型
string name;
string math,english,computer;
//函数成员
Student(string nu="0",string na="0",string ma="0",string en="0",string co="0");
void setstudent(string Nu,string Na,string Ma,string en,string co);
void setmath(string ma) {
math=ma;
}
void setenglish(string en) {
english=en;
}
void setcomputer(string eo) {
computer=eo;
}
bool searchByNum(string num) { //bool类型函数返回值(返回ture或者false)
return number==num;
}
bool searchByName(string num) {
return name==num;
}
bool search(string num) {
return number==num;
}
void display();
};
Student::Student(string nu,string na,string ma,string en,string co) { //
number=nu;
name=na;
math=ma;
english=en;
computer=co;
}
void Student::setstudent(string Nu,string Na,string Ma,string En,string Co) {
number=Nu;
name=Na;
math=Ma;
english=En;
computer=Co;
}
void Student::display() { //打印学生信息
cout<<number<<setw(8)<<name<<setw(6)<<math<<setw(8)
<<english<<setw(8)<<computer<<endl;
}
class Order { //操作函数分为一个类
protected:
vector<Student>stu;
public:
Order() {}
void Add(Student&s);
void IdAdd(Student&i);
void Delete();
void DeleteAll();
void Seek();
void Alter(string ma,string en,string co);
void Store();
void Load();
void Play();
void Begin();
void Exit();
void Sort();
void test();
};
void Order::Add(Student&s) { //添加学生的函数
cout<<"请输入需要添加的学号:";
cin>>s.number;
for(int i=0; i<stu.size(); ++i)
if(stu[i].search(s.number)) {
cout<<"该学号学生信息已存在!";
return;
}
cout<<"姓名:";
cin>>s.name;
cout<<"数学成绩:";
cin>>s.math;
cout<<"英语成绩:";
cin>>s.english;
cout<<"计算机成绩:";
cin>>s.computer;
stu.push_back(s);
s.display();
}
void Order::Delete() { //删除学生的函数
string num;
cout<<"输入您需要删除的学生的学号";
cin>>num;
for(int j=0; j<stu.size(); j++) {
if(stu[j].search(num))
stu.erase(stu.begin()+j);
}
cout<<num<<"学生信息已删除"<<endl;
Store();
}
void Order::DeleteAll() { //清除学生数据
stu.erase(stu.begin(),stu.end());
cout<<"清除成功"<<endl;
Store();
}
void Order::Seek() { //查找学生的函数
string nu;
cout<<"\t\t\t******************************************************************************"<<endl;
cout<<"\t\t\t****** 1.通过学号查找学生 *****"<<endl;
cout<<"\t\t\t****** 2.通过姓名查找学生 *****"<<endl;
cout<<"\t\t\t******************************************************************************"<<endl;
cout<<"\t\t\t\t请输入选项:"<<endl;
int a;
cin>>a;
switch(a) {
case 1:
cout<<"请输入您需要查询的学生的学号"<<endl;
cin>>nu;
for(int j=0; j<stu.size(); j++) {
if(stu[j].searchByNum(nu)) {
cout<<"找到学号为"<<nu<<"的学生,内容是:"<<endl;
stu[j].display();
}
}
break;
case 2:
cout<<"请输入您需要查询的学生的姓名"<<endl;
cin>>nu;
for(int j=0; j<stu.size(); j++) {
if(stu[j].searchByName(nu)) {
cout<<"找到姓名为"<<nu<<"的学生,内容是:"<<endl;
stu[j].display();
}
}
break;
cout<<"没有您要找的学生信息!"<<endl;
}
}
void Order::Alter(string ma,string en,string co) { //修改学生的函数
string nu;
cout<<"请输入您所需要修改成绩的学生学号:";
cin>>nu;
for(int j=0; j<stu.size(); j++) {
if(stu[j].search(nu)) {
cout<<"请您依次输入需要修改的数学,英语,计算机成绩:"<<endl;
cin>>ma>>en>>co;
stu[j].math=ma;
stu[j].english=en;
stu[j].computer=co;
cout<<"修改后的学生信息为:"<<endl;
stu[j].display();
}
}
}
void Order::Store() { //存储学生信息进入文件中
ofstream outfile("student.txt");
if(!outfile.is_open()) { //检查文件是否打开成功
cout<<"没有数据文件!\n\n";
return;
}
for(int i=0; i<stu.size(); i++) {
outfile<<stu[i].number<<" "<<stu[i].name<<" "<<stu[i].math<<" "<<stu[i].english<<" "<<stu[i].computer<<endl;
}
outfile.close();
}
void Order::Sort() { //给学生信息排序
cout<<"\t\t\t******************************************************************************"<<endl;
cout<<"\t\t\t****** 1.通过英语成绩给学生排序(升序) *****"<<endl;
cout<<"\t\t\t****** 2.通过学号给学生排序(升序) *****"<<endl;
cout<<"\t\t\t******************************************************************************"<<endl;
cout<<"\t\t\t\t请选择给学生排序的方法:"<<endl;
int a;
cin>>a;
Student temp;
Student remp;
Order m;
int i,j,swap,g;
switch(a) {
case 1:
for(i=0; i<stu.size()-1; ++i) {
swap=0;
for(j=0; j<stu.size()-i-1; ++j)
if(stu[j].english>stu[j+1].english) {
swap=1;
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
if(!swap)
break;
}
m.Store();
cout<<"学号"<<setw(6)<<"姓名"<<setw(6)<<"数学"<<setw(6)<<"英语"<<setw(8)<<"计算机"<<endl;
for(int i=0; i<stu.size(); i++)
stu[i].display();
break;
case 2:
for(g=0; g<stu.size()-1; ++g) {
swap=0;
for(j=0; j<stu.size()-g-1; ++j)
if(stu[j].number>stu[j+1].number) {
swap=1;
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
if(!swap)
break;
}
m.Store();
cout<<"学号"<<setw(6)<<"姓名"<<setw(6)<<"数学"<<setw(6)<<"英语"<<setw(8)<<"计算机"<<endl;
for(int g=0; g<stu.size(); g++)
stu[g].display();
break;
}
}
void Order::Load() { //从文件中读取学生信息
ifstream infile("student.txt");
if(!infile.is_open()) {//检测文件是否成功打开
cout<<"没有数据文件!\n\n";
return;
}
string na,nu,ma,en,co;
while(infile>>nu>>na>>ma>>en>>co) {
Student temp(nu,na,ma,en,co);
stu.push_back(temp);
}
infile.close();
}
void Order::Play() { //Play函数,打印学生信息的上标(学号,姓名,数学,英语,计算机)
cout<<"学号"<<setw(6)<<"姓名"<<setw(6)<<"数学"<<setw(6)<<"英语"<<setw(8)<<"计算机"<<endl;
for(int i=0; i<stu.size(); i++)
stu[i].display();
}
void Order::Begin() {
//欢迎界面
cout<<"\n\n\t\t\t\t\t"<<endl;
system("color 3");
cout<<" ";
cout<<"欢 ";
Sleep(300);
cout<<"迎 ";
Sleep(300);
cout<<"使 ";
Sleep(300);
cout<<"用 ";
cout<<"\n";
for(int i=1; i<3; i++) {
system("CLS");
cout<<"\t\t\t\t\t页面加载中";
cout<<".";
Sleep(400);
cout<<".";
Sleep(400);
cout<<".";
Sleep(400);
}
}
void Order::Exit() {
//退出界面
cout<<"\n\n\t\t\t";
system("color 3");
cout<<"感 ";
Sleep(300);
cout<<"谢 ";
Sleep(300);
cout<<"使 ";
Sleep(300);
cout<<"用 ";
cout<<"\n";
}
void Order::test() {
cout<<stu.size()<<endl;
}
/*
class Id
{
public:
string id;
string password;
bool searchId(id==ID);
};
class IdOrder
{
public:
void RecordBegin();
void Record();
void IdGreat(Id&m);
void Idstore();
void IdLoad();
vector<Id>ids;
};
void RecordBegin()
{
cout<<"\t\t ***************************"<<endl;
cout<<"-->登录";
cout<<" 注册";
cout<<"\t\t ***************************"<<endl;
cout<<"使用'w','s'键进行选择";
for(int i=1;i==1;){
system("cls");
char order;
cin>>order;
switch(order)
{
case 'w':
cout<<"\t\t ***************************"<<endl;
cout<<"-->登录";
cout<<" 注册";
cout<<"\t\t ***************************"<<endl;
break;
case 's':
cout<<"\t\t ***************************"<<endl;
cout<<" 登录";
cout<<"-->注册";
cout<<"\t\t ***************************"<<endl;
break;
case ' ':
i=0;
break;
}
}
if(order=='w')
Record();
else
IdGreat();
}
viod Record()
{
cout<<"请输入用户名:"<<endl;
cin>>id;
cout<<"请输入密码:"<<endl;
cin>>password;
cin>>password;
}
void IdOrder::IdGreat(Id&m)
{
cout<<"请输入用户名:"<<endl;
cin>>id;
cout<<"请输入密码:"<<endl;
cin>>password;
cout<<"请再输入一次密码:"<<endl;
cin>>"password1";
for(int i=0; i<ids.size(); ++i)
if(ids[i].search(m.id)) {
cout<<"该账号已存在!";
return;
}
if(password==password1)
cout<<"注册成功"<<endl;
ids.push_back(m);
}
void IdOrder::Idstore()//存储账号信息进入文件中
{
ofstream outfile("id.txt");
if(!outfile.is_open()) { //检查文件是否打开成功
cout<<"没有数据文件!\n\n";
return;
}
for(int i=0; i<ids.size(); i++) {
outfile<<ids[i].id<<" "<<ids[i].password<<endl;
}
outfile.close();
}
void IdOrder::IdLoad()
{
ifstream infile("id.txt");
if(!infile.is_open()) {//检测文件是否成功打开
cout<<"没有数据文件!\n\n";
return;
}
string na,nu,ma,en,co;
while(infile>>id>>password) {
Id temp(id,password);
ids.push_back(temp);
}
infile.close();
}
*/
int main() {
Student s;//从Student类中声明一个对象
Order m;//从Order中声明一个对象
m.Load();
m.Begin();
int way;
string ma,en,co;
while(1) {
system("cls");
system("color 3");
cout<<"\t\t *******************************************************************************"<<endl;
cout<<"\t\t ****** 学生信息管理系统 *****"<<endl;
cout<<"\t\t ****** *****"<<endl;
cout<<"\t\t ****** 请选择以下指令 *****"<<endl;
cout<<"\t\t ****** 1.添加学生信息 *****"<<endl;
cout<<"\t\t ****** 2.打印学生信息 *****"<<endl;
cout<<"\t\t ****** 3.查找学生信息 *****"<<endl;
cout<<"\t\t ****** 4.删除学生信息 *****"<<endl;
cout<<"\t\t ****** 5.清除所有信息 *****"<<endl;
cout<<"\t\t ****** 6.更改学生信息 *****"<<endl;
cout<<"\t\t ****** 7.学生信息排序 *****"<<endl;
cout<<"\t\t ****** 0.退出学生系统 *****"<<endl;
cout<<"\t\t *******************************************************************************"<<endl;
cin>>way;
switch(way) { //根据操作调用函数
case 1:
m.Add(s);
break;//添加
case 2:
m.Play();
break;//打印
case 3:
m.Seek();
break;//查找
case 4:
m.Delete();
break;//删除
case 5:
m.DeleteAll();
break;//清除
case 6:
m.Alter(ma,en,co);
break;//更改
case 7:
m.Sort();
break;
case 8:
m.test();
break;
case 0:
m.Store();
Order m;
m.Exit();
return 0;
break;//退出
}
cout<<"\n\t\t按回车键继续!";
getchar();
getchar();
}
return 0;
}
12)案例十二
#include <iostream>
#include <fstream>
#include <cstring>
#include <windows.h>
#include <time.h>
using namespace std;
class STU
{
public:
string id;
string name;
string sex;
string major;
char adress;
int score;
int year;
int month;
int day;
STU *next;
};
class Student: public STU
{
public:
//构造函数
Student()
{
}
//析构函数
~Student()
{
}
void Register(Student *head); //登录
void Menu(Student *head); //菜单
void insert(Student *head); //增加
void del(Student *head); //删除
void Search(Student *head); //查询 (根据学号或姓名)
void rewrite(Student *head); //修改
void Sort(Student *head); //排序
void Statistics(Student *head); //统计 (专业或性别)
void Print(Student *head); //输出
bool only(Student *head,string id); //查重
void write_File(Student *head); //文件保存
void read_File(Student *head); //文件读取
};
void Student::Register(Student *head)
{
char a[20],b[20],c[20],d[20];
int num;
cout<<"*****************************"<<endl;
cout<<" 1.注册 *"<<endl;
cout<<" 2.登录 *"<<endl;
cout<<" 0.退出 *"<<endl;
cout<<"*****************************"<<endl;
cout<<"请输入你的选择: "<<endl;
cin>>num;
if(num==1)
{
cout<<"请输入用户名: "<<endl;
cin>>a;
cout<<"请输入密码: "<<endl;
cin>>b;
cout<<"注册成功!!!"<<endl;
cout<<"即将离开注册页面 请稍候...... "<<endl;
Sleep(1000);
while(1)
{
system("CLS");
cout<<"**********************"<<endl;
cout<<"* 1.登录 *"<<endl;
cout<<"* 0.退出 *"<<endl;
cout<<"**********************"<<endl;
int x;
cout<<"请输入你的选择: "<<endl;
cin>>x;
if(x==1)
{
cout<<"请输入用户名: "<<endl;
cin>>c;
if(strcmp(a,c) == 0)
{
cout<<"请输入密码: "<<endl;
cin>>d;
if(strcmp(b,d) == 0)
{
cout<<"密码正确!!!"<<endl;
cout<<"即将进入菜单页面 请稍候..."<<endl;
Sleep(1500);
}else{
cout<<"密码错误!!!"<<endl;
cout<<"请重新输入密码: "<<endl;
cin>>d;
if(strcmp(b,d) == 0)
{
cout<<"密码正确!!!"<<endl;
cout<<"即将进入菜单页面 请稍候..."<<endl;
Sleep(1000);
}else{
cout<<"密码再次错误 即将离开该页面!!!"<<endl;
exit(0);
}
}
}else{
cout<<"用户名输入错误!!"<<endl;
cout<<"即将离开页面 请稍候..."<<endl;
Sleep(1500);
exit(0);
}
}if(x==0)
{
cout<<"成功退出!!"<<endl;
exit(0);
}
break;
}
}else if(num==2){
cout<<"尚未注册 无法登录!!!"<<endl;
exit(0);
}else if(num==0){
cout<<"成功退出!!!"<<endl;
exit(0);
}
return ;
}
void Student::Menu(Student *head)
{
Student STU;
while(1)
{
system("CLS");
system("date/t");
system("time/t");
cout<<" *********************************************************"<<endl;
cout<<" * *"<<endl;
cout<<" * *"<<endl;
cout<<" * 欢迎使用新生基本信息统计系统 *"<<endl;
cout<<" * *"<<endl;
cout<<" * *"<<endl;
cout<<" *********************************************************"<<endl;
cout<<" * **"<<endl;
cout<<" * **"<<endl;
cout<<" **********************************************************"<<endl;
cout<<" ** 1.增加学生信息 **"<<endl;
cout<<" ** 2.删除学生信息 **"<<endl;
cout<<" ** 3.查询学生信息(按学号或姓名) **"<<endl;
cout<<" ** 4.修改学生信息 **"<<endl;
cout<<" ** 5.排序(英语成绩) **"<<endl;
cout<<" ** 6.统计学生信息(专业/性别) **"<<endl;
cout<<" ** 7.显示学生信息 **"<<endl;
cout<<" ** 8.学生信息保存 **"<<endl;
cout<<" ** 9.学生信息读取 **"<<endl;
cout<<" ** 0.退出该系统 **"<<endl;
cout<<" **********************************************************"<<endl;
cout<<" ** **"<<endl;
cout<<" ** **"<<endl;
cout<<"请输入你的选择:"<<endl;
int choice;
cin>>choice;
switch(choice)
{
case 1 :
system("CLS");
STU.insert(head);
system("pause");
break;
case 2 :
system("CLS");
STU.del(head);
system("pause");
break;
case 3 :
system("CLS");
STU.Search(head);
system("pause");
break;
case 4 :
system("CLS");
STU.rewrite(head);
system("pause");
break;
case 5 :
system("CLS");
STU.Sort(head);
system("pause");
break;
case 6 :
system("CLS");
STU.Statistics(head);
system("pause");
break;
case 7 :
system("CLS");
STU.Print(head);
system("pause");
break;
case 8 :
system("CLS");
STU.write_File(head);
system("pause");
break;
case 9 :
system("CLS");
STU.read_File(head);
system("pause");
break;
case 0 :
system("CLS");
cout<<"************************"<<endl;
cout<<"* *"<<endl;
cout<<"* 谢谢使用! *"<<endl;
cout<<"* *"<<endl;
cout<<"************************"<<endl;
exit(0);
system("pause");
default:
cout<<"输入错误,请重新输入!"<<endl;
system("pause");
}
}
}
//增加
void Student::insert(Student *head)
{
STU *p1=head;
int num;
cout<<"请输入您要添加的学生数量: "<<endl;
cin>>num;
if(p1->next==NULL)
{
for(int i=0;i<num;i++)
{
STU *p2=new Student;
cout<<"请输入第"<<i+1<<"位学生的学号:"<<endl;
cin>>p1->id;
if(Student::only(head,p1->id))
{
return ;
}
cout<<"请输入第"<<i+1<<"位学生的姓名: "<<endl;
cin>>p1->name;
cout<<"请输入第"<<i+1<<"位学生的性别: "<<endl;
cin>>p1->sex;
cout<<"请输入第"<<i+1<<"位学生的专业: "<<endl;
cin>>p1->major;
cout<<"请输入第"<<i+1<<"位学生的住址: "<<endl;
cin>>p1->adress;
cout<<"请输入第"<<i+1<<"位学生的英语成绩: "<<endl;
cin>>p1->score;
cout<<"请输入第"<<i+1<<"位学生的出生日期: "<<endl;
cin>>p1->year>>p1->month>>p1->day;
cout<<" "<<endl;
cout<<"学生信息添加成功!!!"<<endl;
cout<<" "<<endl;
p1->next=p2;
p2->next=NULL;
p1=p2;
}
}else if(p1->next!=NULL){
//已有学生信息 插在尾部
while(p1->next!=NULL)
{
p1=p1->next;
}
for(int i=0;i<num;i++)
{
STU *p2=new Student;
cout<<"请输入第"<<i+1<<"位学生的学号: "<<endl;
cin>>p1->id;
if(Student::only(head,p1->id))
{
return ;
}
cout<<" "<<endl;
cout<<"请输入第"<<i+1<<"位学生的姓名: "<<endl;
cin>>p1->name;
cout<<"请输入第"<<i+1<<"位学生的性别: "<<endl;
cin>>p1->sex;
cout<<"请输入第"<<i+1<<"位学生的专业: "<<endl;
cin>>p1->major;
cout<<"请输入第"<<i+1<<"位学生的住址: "<<endl;
cin>>p1->adress;
cout<<"请输入第"<<i+1<<"位学生的英语成绩: "<<endl;
cin>>p1->score;
cout<<"请输入第"<<i+1<<"位学生的出生日期: "<<endl;
cin>>p1->year>>p1->month>>p1->day;
cout<<" "<<endl;
cout<<"学生信息添加成功!!!"<<endl;
cout<<" "<<endl;
p1->next=p2;
p2->next=NULL;
p1=p2;
}
}
}
//删除
void Student::del(Student *head)
{
STU *p1=head;
STU *p2=new Student;
string num;
int find=0;
cout<<"请输入您要删除的学生的学号: "<<endl;
cin>>num;
if(p1->next==NULL)
{
cout<<"暂无学生信息存入!"<<endl;
}else{
while(p1->next!=NULL)
{
if( (p1->next->id) == num)
{
p2=p1->next;
p1->next=p2->next;
// p1=p2->next;
// delete p2;
find=1;
break;
}
p2=p1; //p1,p2依次向后移一位
p1=p1->next;
break;
}
/* if(p1->next->id==num)
{
p2=p1->next;
p1->next=p2->next;
}
// p2=p2->next;
cout<<"删除成功"<<endl; */
}
if(find)
{
cout<<"删除该学生成功!"<<endl;
}else{
cout<<"暂无该学生信息记录"<<endl;
}
}
//查询
void Student::Search(Student *head)
{
STU *p1=head;
string a="学号",b="姓名";
string gl;
cout<<"请选择你想要查找的类别(请输入“学号”或“姓名”): "<<endl;
cin>>gl;
if(gl==a) //根据学号查找
{
STU *p1=head;
string num;
int t=0;
cout<<"请输入您要查找的学生学号:"<<endl;
cin>>num;
if(p1->next==NULL)
{
cout<<"无该学生信息!"<<endl;
}else{
while(p1->next!=NULL)
{
if(p1->id==num)
{
cout<<"学号: "<<p1->id<<" 姓名: "<<p1->name<<" 性别: "<<p1->sex<<" 专业: "<<p1->major<<" 住址: "<<p1->adress<<" 英语成绩: "<<p1->score<<" 出生日期: "<<p1->year<<"."<<p1->month<<"."<<p1->day<<endl;
t=1;
break;
}else{
p1=p1->next;
}
}
if(t=0)
{
cout<<"该学号不存在!!!"<<endl;
}else if(t=1){
cout<<"查询成功!!!"<<endl;
}
}
}else if(gl==b){ //根据姓名查找
string am;
int m=0;
cout<<"请输入您要查找的学生姓名:"<<endl;
cin>>am;
if(p1->next==NULL)
{
cout<<"无该学生信息!"<<endl;
}else{
while(p1->next!=NULL)
{
if(p1->name==am)
{
cout<<"学号: "<<p1->id<<" 姓名: "<<p1->name<<" 性别: "<<p1->sex<<" 专业: "<<p1->major<<" 住址: "<<p1->adress<<" 英语成绩: "<<p1->score<<" 出生日期: "<<p1->year<<"."<<p1->month<<"."<<p1->day<<endl;
m=1;
break;
}else{
p1=p1->next;
}
}
if(m=0)
{
cout<<"该姓名不存在!!!"<<endl;
}else if(m=1){
cout<<"查询成功!"<<endl;
}
}
}
}
//修改
void Student::rewrite(Student *head)
{
STU *p1=head;
string num;
int a=0;
cout<<"请输入你想要修改的学生的学号:"<<endl;
cin>>num;
if(p1->next==NULL)
{
cout<<"暂无学生信息存入!!!"<<endl;
}else{
while(p1->next!=NULL)
{
if(p1->id!=num)
{
p1=p1->next;
}else{
cout<<"修改姓名为: "<<endl;
cin>>p1->name;
cout<<"修改性别为: "<<endl;
cin>>p1->sex;
cout<<"修改专业为: "<<endl;
cin>>p1->major;
cout<<"修改地址为: "<<endl;
cin>>p1->adress;
cout<<"修改成绩为: "<<endl;
cin>>p1->score;
cout<<"修改出生日期为: "<<endl;
cin>>p1->year>>p1->month>>p1->day;
cout<<"学号: "<<p1->id<<" 姓名: "<<p1->name<<" 性别: "<<p1->sex<<" 专业: "<<p1->major<<" 地址: "<<p1->adress<<" 英语成绩: "<<p1->score<<" 出生日期: "<<p1->year<<p1->month<<p1->day<<endl;
a=1;
break;
}
}
if(a=0)
{
cout<<"暂无该学号存入!"<<endl;
}else if(a=1){
cout<<"信息修改成功!!!"<<endl;
}
}
}
//排序
void Student::Sort(Student *head)
{
STU *p=head;
STU *p1=p->next;
STU temp;
if(p->next==NULL)
{
cout<<"无学生信息记录!!!"<<endl;
}else{
cout<<"按照英语成绩排序: "<<endl;
cout<<" "<<endl;
for( ;p!=NULL;p=p->next)
{
for(p1=p->next;p1!=NULL;p1=p1->next)
{
if(p->score < p1->score) //将英语成绩从大到小进行排序
{
temp.id=p1->id;
p1->id=p->id;
p->id=temp.id;
temp.name=p1->name;
p1->name=p->name;
p->name=temp.name;
temp.sex=p1->sex;
p1->sex=p->sex;
p->sex=temp.sex;
temp.major=p1->major;
p1->major=p->major;
p->major=temp.major;
temp.adress=p1->adress;
p1->adress=p->adress;
p->adress=temp.adress;
temp.score=p1->score;
p1->score=p->score;
p->score=temp.score;
temp.year=p1->year;
p1->year=p->year;
p->year=temp.year;
temp.month=p1->month;
p1->month=p->month;
p->month=temp.month;
temp.day=p1->day;
p1->day=p->day;
p->day=temp.day;
}
}
}
Print(head); //输出排序后的信息
}
return ;
}
//统计
void Student::Statistics(Student *head)
{
STU *p1=head;
string a="专业",b="性别";
string gl;
cout<<"请选择您要统计的类别:(请输入“专业”或“性别”)"<<endl;
cin>>gl;
if(gl==a) //按专业统计
{
string subject;
cout<<"请输入想要统计的专业名称: "<<endl;
cin>>subject;
int sum=0;
if(p1->next==NULL)
{
cout<<"无学生信息记录!!!"<<endl;
}else{
while(p1->next!=NULL)
{
if(p1->major==subject)
{
sum++;
}
p1=p1->next;
}
cout<<"该专业的人数为: "<<sum<<endl;
}
}else if(gl==b) //按性别统计
{
string m;
cout<<"请输入要统计的性别:(请输入“男”或“女”)"<<endl;
cin>>m;
int sum=0;
if(p1->next==NULL)
{
cout<<"暂无学生信息记录!!!"<<endl;
}else{
while(p1->next!=NULL)
{
if(p1->sex==m)
{
sum++;
}
p1=p1->next;
}
cout<<m<<"性的人数为: "<<sum<<endl;
}
}
}
//显示学生信息
void Student::Print(Student *head)
{
STU *p1=head;
if(p1->next==NULL)
{
cout<<"无学生信息记录!"<<endl;
}else{
while(p1->next!=NULL)
{
cout<<"学号: "<<p1->id<<" 姓名: "<<p1->name<<" 性别: "<<p1->sex<<" 专业: "<<p1->major<<" 地址: "<<p1->adress<<" 英语成绩: "<<p1->score<<" 出生日期: "<<p1->year<<"."<<p1->month<<"."<<p1->day<<endl;
p1=p1->next;
}
cout<<"显示成功!!!"<<endl;
}
}
//学号查重
bool Student::only(Student *head,string id)
{
STU *p1=head;
while(p1->next!=NULL)
{
if(p1->id==id)
{
cout<<"学号重复,请勿添加!"<<endl;
return true;
}
p1=p1->next;
}
return false;
}
void Student::write_File(Student *head)
{
STU *p1=head;
ofstream outfile("students.txt",ios::out);
if(outfile==NULL)
{
cout<<"打开txt文件失败!"<<endl;
}else{
p1=head;
while(p1->next!=NULL)
{
outfile<<" 学号: "<<p1->id<<endl;
outfile<<" 姓名: "<<p1->name<<endl;
outfile<<" 性别: "<<p1->sex<<endl;
outfile<<" 专业: "<<p1->major<<endl;
outfile<<" 地址: "<<p1->adress<<endl;
outfile<<" 成绩: "<<p1->score<<endl;
outfile<<" 出生日期: "<<p1->year<<"."<<p1->month<<"."<<p1->day<<"."<<endl;
p1=p1->next;
}
outfile.close();
cout<<"写入txt文件成功!!!"<<endl;
cout<<"信息已保存!!!"<<endl;
}
}
void Student::read_File(Student *head)
{
STU *p1=head;
STU *p=new Student;
cout<<"正在读取信息 请稍等......"<<endl;
Sleep(1000);
system("CLS");
ifstream infile("students.txt",ios::in);
if(!infile)
{
cout<<"文件里没有数据!!!"<<endl;
return;
}else{
int total=0;
while(!infile.eof())
{
infile>>p->id;
infile>>p->name;
infile>>p->sex;
infile>>p->major;
infile>>p->adress;
infile>>p->score;
infile>>p->year>>p->month>>p->day;
total++;
cout<<"学号: "<<p->id<<endl;
cout<<"姓名: "<<p->name<<endl;
cout<<"性别: "<<p->sex<<endl;
cout<<"专业: "<<p->major<<endl;
cout<<"地址: "<<p->adress<<endl;
cout<<"成绩: "<<p->score<<endl;
cout<<"出生日期:"<<p->year<<"."<<p->month<<"."<<p->day<<endl;
cout<<"学生数量为: "<<total<<endl;
cout<<" "<<endl;
cout<<"读取成功!!!"<<endl;
// delete p;
p=NULL;
p1=p;
p1=p1->next;
}
infile.close();
}
}
int main()
{
system("CLS");
system("color 4");
Student STU;
Student *head=new Student;
STU.Register(head);
STU.Menu(head);
return 0;
}
13)案例十三
#include<iostream>
#include<fstream>
#include<cstring>
#include<stdlib.h>
#include<windows.h>
using namespace std;
//新生信息
class Student
{
public:
int num; //学号
char name[20]; //姓名
char sex[10]; //性别
char major[20]; //专业
int date; //出生日期
char address[20]; //家庭地址
int score; //英语入学成绩
Student *next;
};
class function
{
public:
function () {};
void Enter();
void Menu(Student *head); //菜单
void Creat(Student *head); //录入
void Add(Student *head); //增加
void Delete(Student *head); //删除
void Search(Student *head); //查询
void Change(Student *head); //修改
void Print(Student *head); //输出
void Sort(Student *head); //排序
void Statistics(Student *head); //统计
void In_File(Student *head); //文件读取
void Out_File(Student *head); //文件保存
~function () {};
};
//登录
void function::Enter()
{
int i=0;
char account[10]={0}; //账号
char password[10]={0}; //密码
while(i<3)
{
// system("color B5");
cout<<"\n";
cout<<"************欢迎使用新生信息统计系统************\n";
cout<<"请输入账号: \n";
cin>>account;
cout<<"请输入密码: \n";
cin>>password;
cout<<"\n";
if(strcmp(account,"fengmang")==0&&strcmp(password,"666")==0)
{
cout<<"登录成功\n";
break;
}
else
{
cout<<"密码或用户名错误,请重试\n";
continue;
}
}
system("pause"); //停顿
system("cls"); //清屏
}
//录入
void function::Creat(Student *head)
{
Student *p1=head;
char sex1[3];
char sex2[3];
strcpy(sex1, "男");
strcpy(sex2, "女");
int n;
cout<<"请输入学生数量:";
cin>>n;
cout<<endl;
for(int i=0;i<n;i++)
{
Student *p2=new Student;
cout<<"学号:";
cin>>p2->num;
cout<<"姓名:";
cin>>p2->name;
cout<<"性别:";
cin>>p2->sex;
while(strcmp(sex1,p2->sex)!=0 &&strcmp(sex2,p2->sex)!=0 )
{
cout<<"请输入格式 :男/女,请重新输入!"<<endl;
cout<<"性别:";
cin>>p2->sex;
}
cout<<"专业:";
cin>>p2->major;
cout<<"出生日期:";
cin>>p2->date;
cout<<"家庭住址:";
cin>>p2->address;
cout<<"英语入学成绩:";
cin>>p2->score;
cout<<endl;
p1->next=p2;
p1=p2;
}
p1->next ==NULL;
}
//输出
void function::Print(Student *head)
{
Student *p1=head;
Student *p2=p1->next;
while(p2!=NULL)
{
cout<<"学号:"<<p2->num<<endl;
cout<<"姓名:"<<p2->name<<endl;
cout<<"性别:"<<p2->sex<<endl;
cout<<"专业:"<<p2->major<<endl;
cout<<"出生日期:"<<p2->date<<endl;
cout<<"家庭住址:"<<p2->address<<endl;
cout<<"英语入学成绩:"<<p2->score<<endl;
cout<<endl;
p2=p2->next;
}
}
//增加
void function::Add(Student *head)
{
Student *p1=head;
Student *p2=head->next;
Student *p3;
char sex1[3];
char sex2[3];
strcpy(sex1, "男");
strcpy(sex2, "女");
int id;
bool flag=false;
cout<<"请输入要插入的位置:";
cin>>id;
while(p2!=NULL)
{
if(p2->num==id)
{
p3=new Student;
cout<<"请输入要增加的学生的信息:\n";
cout<<"学号:";
cin>>p3->num;
cout<<"姓名:";
cin>>p3->name;
cout<<"性别:";
cin>>p3->sex;
while(strcmp(sex1,p3->sex)!=0 &&strcmp(sex2,p3->sex)!=0 )
{
cout<<"请输入格式 :男/女,请重新输入!"<<endl;
cout<<"性别:";
cin>>p3->sex;
}
cout<<"专业:";
cin>>p3->major;
cout<<"出生日期:";
cin>>p3->date;
cout<<"家庭住址:";
cin>>p3->address;
cout<<"英语入学成绩:";
cin>>p3->score;
cout<<endl;
p3->next=p2->next;
p2->next=p3;
cout<<"添加成功!\n";
flag=true;
}
p2=p2->next;
}
if(flag==false)
{
printf("未查询到该学生,增加失败!\n");
}
}
//删除
void function::Delete(Student *head)
{
Student *p1=head;
Student *p2=p1->next;
int id;
bool flag=false;
cout<<"请输入要删除的学生的学号:";
cin>>id;
while(p2!=NULL)
{
if(p2->num==id)
{
p1->next=p2->next;
delete p2;
cout<<"\n删除成功!\n";
flag=true;
}
p2=p2->next;
}
if(flag==false)
{
cout<<"无该学生信息,删除失败!\n";
}
}
//查询
void function::Search(Student *head)
{
Student *p1=head;
Student *p2=head->next;
int id;
cout<<"请输入要查找的学生的学号:\n";
cin>>id;
bool flag=false;
while(p2!=NULL)
{
if(p2->num ==id)
{
cout<<"学号:"<<p2->num<<endl;
cout<<"姓名:"<<p2->name<<endl;
cout<<"性别:"<<p2->sex<<endl;
cout<<"专业:"<<p2->major<<endl;
cout<<"出生日期:"<<p2->date<<endl;
cout<<"家庭住址:"<<p2->address<<endl;
cout<<"英语入学成绩:"<<p2->score<<endl;
cout<<endl;
flag=true;
break;
}
p2=p2->next;
}
if(flag==false)
{
cout<<"无该学生的信息!\n";
}
}
//修改
void function::Change(Student *head)
{
Student *p1=head;
Student *p2=head->next;
char sex1[3];
char sex2[3];
strcpy(sex1, "男");
strcpy(sex2, "女");
int id;
bool flag=false;
cout<<"请输入需要修改的职工的职工号:";
cin>>id;
while(p2!=NULL)
{
if(p2->num==id)
{
cout<<"请输入修改后的学生信息:\n";
cout<<"学号:";
cin>>p2->num;
cout<<"姓名:";
cin>>p2->name;
cout<<"性别:";
cin>>p2->sex;
while(strcmp(sex1,p2->sex)!=0 &&strcmp(sex2,p2->sex)!=0 )
{
cout<<"请输入格式 :男/女,请重新输入!"<<endl;
cout<<"性别:";
cin>>p2->sex;
}
cout<<"专业:";
cin>>p2->major;
cout<<"出生日期:";
cin>>p2->date;
cout<<"家庭住址:";
cin>>p2->address;
cout<<"英语入学成绩:";
cin>>p2->score;
cout<<endl;
flag=true;
}
p2=p2->next ;
}
if(flag==false)
{
printf("无该学生信息,无法修改!\n");
}
}
//排序
void function::Sort(Student *head)
{
Student *p1=head->next;
int temp;
char t[20];
if(p1==NULL)
{
cout<<"无学生信息!\n";
}
else
{
cout<<"按英语成绩排序:\n";
while(p1!=NULL)
{
Student *p2=p1->next;
while(p2!=NULL)
{
if(p1->score < p2->score)
{
temp=p2->num;p2->num=p1->num;p1->num=temp;
strcpy(t,p2->name);strcpy(p2->name,p1->name);strcpy(p1->name,t);
strcpy(t,p2->sex);strcpy(p2->sex,p1->sex);strcpy(p1->sex,t);
strcpy(t,p2->major);strcpy(p2->major,p1->major);strcpy(p1->major,t);
temp=p2->date;p2->date=p1->date;p1->date=temp;
strcpy(t,p2->address);strcpy(p2->address,p1->address);strcpy(p1->address,t);
temp=p2->score;p2->score=p1->score;p1->score=temp;
}
p2=p2->next;
}
p1=p1->next;
}
}
}
//统计
void function::Statistics(Student *head)
{
Student *p1=head->next;
string a="专业",b="性别";
string category;
cout<<"请选择要统计的类别:(请输入“专业”或“性别”)\n";
cin>>category;
if(category==a) //按专业统计
{
string w;
cout<<"请输入专业名称:\n";
cin>>w;
int sum=0;
if(p1==NULL)
{
cout<<"无学生信息!\n";
}
else
{
while(p1!=NULL)
{
if(p1->major==w)
{
sum++;
}
p1=p1->next;
}
cout<<w<<"专业的人数:"<<sum<<endl;
}
}
else if(category==b) //按性别统计
{
string s;
cout<<"请输入要统计的性别:(请输入“男”或“女”)\n";
cin>>s;
int sum=0;
if(p1==NULL)
{
cout<<"无学生信息!\n";
}
else
{
while(p1!=NULL)
{
if(p1->sex==s)
{
sum++;
}
p1=p1->next;
}
cout<<s<<"生的人数:"<<sum<<endl;
}
}
}
//文件保存
void function::Out_File(Student *head)
{
Student *p1=head;
Student *p2=head->next;
ofstream out("D:\\students.txt",ios::out);
if(out==NULL)
{
cout<<"打开文件失败!\n";
exit(0);
}
while(p2!=NULL)
{
// out<<"学号"<<"\t"<<"姓名"<<"\t"<<"性别"<<"\t"<<"专业"<<"\t"<<"出生日期"<<"\t"<<"家庭住址"<<"\t"<<"英语入学成绩\n";
out<<p2->num<<"\t"<<p2->name<<"\t"<<p2->sex<<"\t"<<p2->major<<"\t"<<p2->date<<"\t"<<p2->address<<"\t"<<p2->score<<endl;
p2=p2->next;
}
out.close();
cout<<"\n写入文件成功!\n";
}
//文件读取
void function::In_File(Student *head)
{
ifstream in;
Student *p1=head;
Student *p2=head->next;
in.open("D:\\students.txt",ios::in);
if(in==NULL)
{
cout<<"打开文件文件失败!\n";
exit(0);
}
while(1)
{
p2=new Student;
in>>p2->num>>p2->name>>p2->sex>>p2->major>>p2->date>>p2->address>>p2->score;
if(in.fail() ) //判断是不是读到了文件结尾
{
delete p2;
p1->next=NULL;
break;
}
p1->next=p2;
p1=p1->next;
}
cout<<"读取成功!\n";
function::Print(head);
in.close();
}
//菜单
void function::Menu(Student *head)
{
function STU;
while(1)
{
// system("color 3F");
cout<<endl;
cout<<endl;
cout<<"**************新生信息统计系统***************"<<endl;
cout<<" 1.录入学生信息 "<<endl;
cout<<" 2.增加学生信息 "<<endl;
cout<<" 3.删除学生信息 "<<endl;
cout<<" 4.查询学生信息 "<<endl;
cout<<" 5.修改学生信息 "<<endl;
cout<<" 6.输出学生信息 "<<endl;
cout<<" 7.按照英语成绩排序 "<<endl;
cout<<" 8.统计学生信息 "<<endl;
cout<<" 9.保存学生信息 "<<endl;
cout<<" 0.读取学生信息 "<<endl;
cout<<" 10.退出系统 "<<endl;
cout<<"*********************************************"<<endl;
cout<<endl;
int x;
cout<<"请输入所选择的功能序号:"<<endl;
cin>>x;
switch(x)
{
case 1:
cout<<"***************录入学生信息***************\n";
STU.Creat(head);
system("pause");
system("cls");
break;
case 2:
cout<<"***************增加学生信息***************\n";
STU.Add(head);
system("pause");
system("cls");
break;
case 3:
cout<<"***************删除学生信息***************\n";
STU.Delete(head);
system("pause");
system("cls");
break;
case 4:
cout<<"***************查询学生信息***************\n";
STU.Search(head);
system("pause");
system("cls");
break;
case 5:
cout<<"***************修改学生信息***************\n";
STU.Change(head);
system("pause");
system("cls");
break;
case 6:
cout<<"***************输出学生信息***************\n";
STU.Print(head);
system("pause");
system("cls");
break;
case 7:
cout<<"*************按照英语成绩排序*************\n";
STU.Sort(head);
STU.Print(head);
system("pause");
system("cls");
break;
case 8:
cout<<"***************统计学生信息***************\n";
STU.Statistics(head);
system("pause");
system("cls");
break;
case 9:
cout<<"***************保存学生信息***************\n";
STU.Out_File(head);
system("pause");
system("cls");
break;
case 0:
cout<<"***************读取学生信息***************\n";
STU.In_File(head);
system("pause");
system("cls");
break;
case 10:
cout<<"*********感谢使用新生信息管理系统*********\n";
exit(0);
break;
default:
cout<<"选择错误,请重新选择\n";
system("pause");system("cls");function::Menu(head);
break;
}
}
}
//主函数
int main ()
{
function F;
Student *head=new Student;
F.Enter();
F.Menu(head);
return 0;
}
14)案例十四
#define MAX_NUM 1000
#include <String>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
bool quit = false;
struct StuNode {
int num;
char name[1000];
int math, eng, yuwen, cyuyan;
int pj;
int sum;
StuNode *nextstu;
};
class people {
int num;
char name[1000];
};
class SInfo:private people { //设计一个类
public:
int math, eng, yuwen, cyuyan;
int pj;
int sum;
private: //私有权限
StuNode *head,*StuListHead,*p1,*p2,*p3;
public: //公共权限
SInfo(); //构造函数
~SInfo(); //析构函数
StuNode *CreatSinfo(); //创建学生信息
StuNode *StuInsert(StuNode *head); //插入学生信息
void StuDelete(int snum); //删除学生信息
StuNode *StuFind(int snum); //查找学生信息,传入参数学号
void StuModify(int snum, int smath, int seng, int syuwen, int cyuyan, int spj); //修改学生信息
void StuCopy(StuNode *ptemp, StuNode *p); //学生信息拷贝
void StuSort(char ch);
void StuClassfy(); //分类合计
void StuRead(); //从文件读入学生信息
void StuSave(); //保存学生信息到文件
void StuQuit(); //退出程序
void ShowInfo(); //遍历输出学生信息
};
int Systemdoor()
{
string username = "0", password = "0";
string name, temp;
int number = 3;
while (1)
{
cout<<" ********************************** \n";
cout<<" ** ** \n";
cout<<" ** 欢迎使用学生信息管理系统! ** \n";
cout<<" ** ** \n";
cout<<" ********************************** \n";
cout<<" -----------祝你使用愉快!---------- \n";
cout << " 用 户 名:";
cin >> name;
cout << " 密 码:";
cin >> temp;
if (name != username || temp != password)
{
number--;
if (number >0)
{
cout << " 用户名/密码错误!你还有" << number << "次机会" << endl;
system("pause");
system("cls");
}
else
cout << " 用户名/密码错误!" << endl, exit(0);
system("cls");
}
else
{
cout << " *************密码正确*************" << endl<<endl;
system("cls");
return 1;
}
}
}
void ShowMenu()
{
system("cls");
char ch;
cout<<"\t\t\t*******************************************************\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t********** 欢迎使用! ***********\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t*******************************************************\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**------------------ 请选择: ---------------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**---------------0.安全退出学生系统: ---------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**---------------1.文件读入学生信息: ---------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**---------------2.录入新的学生信息: ---------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**---------------3.添加新的学生信息: ---------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**---------------4.删除已有学生信息: ---------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**---------------5.查找已有学生信息: ---------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**---------------6.修改已有学生信息: ---------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**---------------7.已有学生信息排序: ---------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**---------------8.分类合计学生信息: ---------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**---------------9.输出所有学生信息: ---------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t**---------------10.保存现有学生信息:---------------**\n";
cout<<"\t\t\t** **\n";
cout<<"\t\t\t*******************************************************\n";
time_t now = time(0);
char* dt = ctime(&now);
cout << " 登入系统时本地日期时间:" << dt << endl;
cout << "\n\t\n\t\t请选择:";
}
SInfo::SInfo() //构造函数,为对象的成员属性赋值 。
{
StuListHead = new StuNode;
StuListHead->nextstu = NULL;
}
SInfo::~SInfo() //析构函数,在对象销毁系统前系统自动调用,执行一些清理工作。
{
StuNode *p;
while (StuListHead)
{
p = StuListHead;
StuListHead = StuListHead->nextstu;
delete p;
}
StuListHead = NULL;
}
StuNode *SInfo::CreatSinfo() //创建学生信息表
{
int n;
StuNode *p, *s;
p = StuListHead;
cout << "请输入学生人数:";
cin >> n;
for (int i = 1; i <= n; i++)
{
s = new StuNode;
cout<<"请输入学生的信息!\n学号:";
cin>>s->num;
cout<<"姓名:";
cin>>s->name;
cout<<"数学成绩:";
cin>>s->math;
cout<<"英语成绩:";
cin>>s->eng;
cout<<"语文成绩:";
cin>>s->yuwen;
cout<<"C语言成绩:";
cin>>s->cyuyan;
s->pj = (s->math + s->eng + s->yuwen + s->cyuyan)/4;
s->sum = s->math + s->eng + s->yuwen + s->cyuyan;
s->nextstu = p->nextstu;
p->nextstu = s;
p = p->nextstu;
}
if (p == NULL) //判断学生信息表是否创建成功
{
cout << "创建失败请重新创建!" << endl;
CreatSinfo();
system("pause");
}
}
void SInfo::ShowInfo() //遍历输出
{
StuNode *p;
cout << "学号" << '\t' << "姓名" << '\t' << "数学" << '\t' << "英语" << '\t' << "语文" << '\t' << "C语言" << '\t' << "平均分" << '\t' << "总分" << endl;
for (p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
cout << p->num << '\t' << p->name << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->cyuyan << '\t' << p->pj << '\t' << p->sum << endl;
}
system("pause");
}
StuNode *SInfo::StuInsert(StuNode *head) //插入学生信息(头插法)
{
StuNode *s,*p;
int num;
char name;
int math;
int eng;
int yuwen;
int cyuyan;
int pj;
s = new StuNode;
cout << "请输入添加学生信息:";
cout<< "学号:";
cin >> s->num;
cout<<"姓名:";
cin>> s->name;
cout<<"数学成绩:";
cin>> s->math;
cout<<"英语成绩:";
cin>> s->eng;
cout<<"语文成绩:";
cin >> s->yuwen;
cout<< "C语言成绩:";
cin>> s->cyuyan;
s->pj = (s->math + s->eng + s->yuwen + s->cyuyan)/4;
s->sum = s->math + s->eng + s->yuwen + s->cyuyan;
p = StuListHead;
s->nextstu = p->nextstu;
p->nextstu = s;
}
void SInfo::StuDelete(int snum)
{
StuNode *p, *ptemp;
p = StuListHead;
ptemp = p;
while (p->nextstu!=NULL && p->num!=snum) //循环终止条件为p->nextstu不为空 而且没有找到相应学号的学生
{
ptemp = p;
p = p->nextstu;
}
if (p->num == snum)
{
ptemp->nextstu = p->nextstu;
delete p;
}
else
{
cout << "未找到该学生信息!" << endl;
}
}
StuNode *SInfo::StuFind(int snum)
{
StuNode *p;
p = StuListHead->nextstu;
while (p->nextstu && p->num != snum) //循环终止条件为p->nextstu不为空 而且没有找到相应学号的学生
{
p = p->nextstu;
}
if (p->num == snum)
{
return p;
}
else
{
cout << "未找到该学生信息!" << endl;
}
}
void SInfo::StuModify(int snum, int smath, int seng, int syuwen, int scyuyan, int spj)
{
StuNode *ItemStu = StuFind(snum); //直接调用查找函数
if (ItemStu != NULL)
{
ItemStu->num = snum;
ItemStu->math = smath;
ItemStu->math = smath;
ItemStu->eng = seng;
ItemStu->yuwen = syuwen;
ItemStu->cyuyan = scyuyan;
ItemStu->pj = (ItemStu->math + ItemStu->eng + ItemStu->yuwen + ItemStu->cyuyan)/4;
ItemStu->sum = ItemStu->math + ItemStu->eng + ItemStu->yuwen + ItemStu->cyuyan;
}
}
void SInfo::StuCopy(StuNode *ptemp, StuNode *p) //拷贝学生信息(将p的信息拷贝到ptemp中)
{
if (p == NULL)
{
cout << "拷贝目标为空!" << endl;
}
else
{
ptemp->num = p->num;
ptemp->math = p->math;
ptemp->eng = p->eng;
ptemp->yuwen = p->yuwen;
ptemp->cyuyan = p->cyuyan;
ptemp->pj = p->pj;
ptemp->sum = p->sum;
}
}
void SInfo::StuSort(char ch) //根据 总分排序
{
if (ch == '>')
{
for (StuNode *p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
for (StuNode *q = StuListHead->nextstu; q != NULL; q = q->nextstu)
{
if (p->sum > q->sum)
{
StuNode *ptemp = new StuNode;
StuCopy(ptemp, p);
StuCopy(p, q);
StuCopy(q, ptemp);
}
}
}
}
else if (ch == '<')
{
for (StuNode *p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
for (StuNode *q = StuListHead->nextstu; q != NULL; q = q->nextstu)
{
if (p->sum < q->sum)
{
StuNode *ptemp = new StuNode;
StuCopy(ptemp, p);
StuCopy(p, q);
StuCopy(q, ptemp);
}
}
}
}
else if (ch == 'o')
{
for (StuNode *p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
for (StuNode *q = StuListHead->nextstu; q != NULL; q = q->nextstu)
{
if (p->num < q->num)
{
StuNode *ptemp = new StuNode;
StuCopy(ptemp, p);
StuCopy(p, q);
StuCopy(q, ptemp);
}
}
}
}
else
{
cout << "排序条件出错!" << endl;
}
}
void SInfo::StuClassfy() //根据学生总分分类
{
int grade[5] = {0};
StuNode *p = StuListHead->nextstu;
while (p != NULL)
{
if (340 < p->sum)
{
grade[0]++;
if (p->cyuyan > 80 && p->eng > 80 && p->math > 80 && p->yuwen >80)
{
cout << "优秀学生信息:" <<endl;
cout << "学号" << '\t' << "姓名" << '\t' << "数学" << '\t' << "英语" << '\t' << "语文" << '\t' << "C语言" << '\t' << "平均分" << '\t' << "总分" << endl;
cout << p->num << '\t' << p->name << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->cyuyan << '\t' << p->pj << '\t' << p->sum << endl;
}
}
else if (300 < p->sum && p->sum < 340)
{
grade[1]++;
}
else if (260 < p->sum && p->sum < 300)
{
grade[2]++;
}
else if (240 < p->sum && p->sum < 260)
{
grade[3]++;
}
else
{
StuNode *p;
p = StuListHead->nextstu;
ofstream out("D://补考学生.txt");
if (!out) {
cout << "不能打开文件!" << endl;
return;
}
while (p != NULL)
{
if(240 > p->sum)
{
out << p->num << '\t' << p->name << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->cyuyan << '\t' << p->pj << '\t' << p->sum << '\n';
}
p = p->nextstu;
}
cout << "学生信息已保存..." << endl;
grade[4]++;
}
p = p->nextstu;
}
cout << "A" << '\t' << "B" << '\t' << "C" << '\t' << "D" << '\t' << "E" << endl;
for (int i = 0; i < 5; i++)
{
cout << grade[i] << '\t';
}
cout << endl;
}
void SInfo::StuRead() //从文件读入数据
{
StuNode *p;
p = StuListHead;
ifstream in("D://学生信息.txt");
if (!in) {
cout << "没有学生信息,请先录入学生信息!" << endl;
system("pause");
return;
}
char c;
cout << "读入学生信息表:" << endl;
cout << "学号" << '\t' << "姓名" << '\t' << "数学" << '\t' << "英语" << '\t' << "语文" << '\t' << "C语言" << '\t' << "平均分" << '\t' << "总分" << endl;
while(in.get(c))
while (p != NULL)
{
cout << p->num << '\t' << p->name << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->cyuyan <<'\t' << p->pj << '\t' << p->sum << '\n'<<endl;
p = p->nextstu;
}
cout.put(c);
system("pause");
}
void SInfo::StuSave() //保存学生信息
{
StuNode *p;
p = StuListHead->nextstu;
ofstream out("D://学生信息.txt");
if (!out) {
cout << "不能打开文件!" << endl;
return;
}
while (p != NULL)
{
out << p->num << '\t' << p->name << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->cyuyan <<'\t' << p->pj << '\t' << p->sum << '\n';
p = p->nextstu;
}
cout << "学生信息已保存..." << endl;
}
void SInfo::StuQuit() //学生信息写入文件
{
char choice;
cout << "是否保存学生信息:?(Y/N)";
cin >> choice;
if (choice == 'y' || choice == 'Y')
{
StuSave();
cout << "学生信息已保存..." << endl;
}
}
int main()
{
StuNode *head;
StuNode *StuListHead;
system("color F4");
Systemdoor();
int x = 100, pnum,pmath,peng,pyuwen,pcyuyan,ppj;
char pname[1000];
StuNode *pfind;
SInfo stu;
while (x != 0)
{
system("cls"); //清屏
ShowMenu();
cin >> x;
switch (x)
{
case 0:
stu.StuQuit();
break;
case 1:
stu.StuRead();
break;
case 2:
stu.CreatSinfo();
cout << "请核对输入学生信息!" << endl;
stu.ShowInfo();
break;
case 3:
stu.StuInsert(StuListHead);
cout << "更新学生信息表..." << endl;
stu.ShowInfo();
break;
case 4:
cout << "请输入要删除学生学号:";
cin >> pnum;
stu.StuDelete(pnum);
cout << "更新学生信息表..." << endl;
stu.ShowInfo();
break;
case 5:
cout << "请输入要查找学生学号:";
cin >> pnum;
pfind = stu.StuFind(pnum);
cout << "查找学生学号:" << pfind->num <<" 数学 "<<pfind->math<<" 英语 "<<pfind->eng<<" 语文 "<<pfind->yuwen<<" C语言 "<<pfind->cyuyan <<" 平均分 "<<pfind->pj <<" 总分 " << pfind->sum << endl;
system("pause");
break;
case 6:
cout << "请输入要修改学生学号:";
cin >> pnum;
cout << "请重新输入学生分数:";
cout << "请输入数学成绩:";
cin >> pmath ;
cout << "请输入英语成绩:";
cin >> peng;
cout << "请输入语文成绩:";
cin >> pyuwen;
cout << "请输入C语言成绩:";
cin >> pcyuyan;
stu.StuModify(pnum, pmath, peng, pyuwen, pcyuyan, ppj);
cout << "修改成功!" << endl;
cout << "更新学生信息表..." << endl;
stu.ShowInfo();
break;
case 7:
cout << "升序排序(1)降序排序(0)学号排序(10):";
cin >> pnum;
if (pnum == 1)
{
stu.StuSort('<');
stu.ShowInfo();
}
else if (pnum == 0)
{
stu.StuSort('>');
stu.ShowInfo();
}
else if (pnum == 10)
{
stu.StuSort('o');
stu.ShowInfo();
}
else
{
cout << "请输入正确选择!" << endl;
}
break;
case 8:
stu.StuClassfy();
system("pause");
break;
case 9:
stu.ShowInfo();
break;
case 10:
stu.StuSave();
system("pause");
break;
default:
cout<<"选择有错,请重新选择\n";
}
}
system("pause");
return 0;
}
15)案例十五
#include<iostream>
#include<fstream>//该数据类型通常表示文件流
#include<stdio.h>
#include<string>
#include<iomanip>
#include<cstring>
#include<ctime> //记录时间
using namespace std;
static int m=0;//总人数
class Student{
public://封装数据
void set_id(int id) {this->Id=id;}//调用this指针,把x的值赋给当前对象的X属性。
void set_name(string name) {this->Name=name;}
void set_sex(string sex) {this->Sex=sex;}
void set_spe(string spe) {this->Spe=spe;}
void set_year(int year) {this->Year=year;}
void set_month(int month) {this->Month=month;}
void set_day(int day) {this->Day=day;}
void set_address(string address) {this->Address=address;}
void set_age(int age) {this->Age=age;}
void set_Escore(float Escore) {this->ESCORE=Escore;}
int get_id() {return Id;}
string get_name() {return Name;}
string get_sex() {return Sex;}
string get_spe() {return Spe;}
int get_year() {return Year;}
int get_month()
{return Month;}
int get_day() {return Day;}
string get_address() {return Address;}
int get_age() {return Age;}
float get_Escore() {return ESCORE;}
Student *next;
void show();
void menu();
private: //定义属性
int Id;//学号
string Name;//姓名
string Sex;//性别
string Spe;//(specialty专业)
int Year;//年
int Month;//月
int Day;//日
string Address;//住址
int Age;//年龄
float ESCORE;//英语成绩
};
void Student::show() //单人输出show函数
{
cout<<"学号:"<<get_id()<<"\t";
cout<<"姓名:"<<get_name()<<"\t";
cout<<"性别:"<<get_sex()<<"\t";
cout<<"专业:"<<get_spe()<<"\t";
cout<<"出生年月:";
cout<<get_year()<<"年";
cout<<get_month()<<"月";
cout<<get_day()<<"日"<<"\t";
cout<<"住址:"<<get_address()<<"\t";
cout<<"英语成绩:"<<get_Escore()<<"\t";
cout<<"*********************************************"<<endl;
}
class message:public Student//函数集合 ,使用公有继承类Student
{
public:
message(){head=NULL;}
~message();
Student* find2(int id);
bool find1(string name);
void save();
void show1();
void search();
void add();
void read();
void tongji();
void rank();
void shanchu();
void revision();
void age_(Student *pt);
private:
Student *p1,*p2,*head;
};
message::~message()//引用析构函数,释放空间
{
Student *pt;
while(head)
{
pt=head;
head=head->next;
delete pt;
}
}
void message::show1()//遍历数据
{
if(head==NULL)
{
cout<<"这是一个空列表,请先添加学生信息"<<endl;
return;
}
cout<<"共"<<m<<"名学生"<<endl;
p1=head;
while(p1!=NULL)
{
p1->show();
p1=p1->next;
}
}
void message::add()//添加学生信息
{
int n,d,year,month,day;//n为添加新生人数,d为判断男女条件
int id;
string name,spe,sex,address;
float Escore;
p2=head;
while(p2)//将数据添加的尾部
{
if(!p2->next)
break;
p2=p2->next;
}
cout<<"请输入您要添加人数(人数大于0):";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"请输入第"<<i+1<<"个学生的信息"<<endl;
cout<<"学号:";
cin>>id;
p1=new Student();//为新添加的学生信息开辟空间
while(find2(id))//判断学号是否重复
{
cout<<"该学号已存在,请重新输入"<<endl;
cin>>id;
}
p1->set_id(id);
cout<<"姓名:";
cin>>name;
p1->set_name(name);
cout<<"性别"<<endl<<"1.男 "<<"2.女"<<endl<<"请选择性别(请输入1或2):";
cin>>d;
switch(d)
{
case 1:p1->set_sex("男");break;
case 2:p1->set_sex("女");break;
default:cout<<"稀有物种"<<endl;
}
cout<<"专业"<<endl<<"1.文科 "<<"2.理科"<<endl<<"请选择性别(请输入1或2):";
cin>>d;
switch(d)
{
case 1:p1->set_spe("文科");break;
case 2:p1->set_spe("理科");break;
default:cout<<"未知专业"<<endl;
}
cout<<"****出生年月****"<<endl<<"年:";
cin>>year;
p1->set_year(year);
cout<<"月(请输入1-12月):";
cin>>month;
p1->set_month(month);
cout<<"日:";
cin>>day;
p1->set_day(day);
cout<<"****************"<<endl;
cout<<"住址:";
cin>>address;
p1->set_address(address);
cout<<"英语成绩:";
cin>>Escore;
p1->set_Escore(Escore);
p1->next=NULL;//将指针的下一个指向空,结束输入
if(head==NULL)
head=p1;//将指针指向空,防止不必要的麻烦
else
p2->next=p1;
p2=p1;
p2->next=NULL;
m++;//将总人数+1
}
return;
}
void message::search()//查询
{
int id1;
string name1;
int N=0;//N为判断是否换种查询方式条件
static int n=0;//n为判断查询条件
p1=head;
if(p1==NULL)
{
cout<<"这是一个空列表,请先添加学生信息"<<endl;
return;
}
if(n==0)
{
cout<<"请选择您要查询的学生信息:"<<"\t"<<"1.学号 2.姓名"<<endl;
cin>>n;
}
if(n==1)
{
cout<<"请输入您要查询的学生学号"<<endl;
cin>>id1;
if(find2(id1))
find2(id1)->show();//输出查询到的数据
else
{
cout<<"没有该生信息,是否选择另一种查询方式?"<<endl<<"1.是 2.否"<<endl;
cin>>N;
if(N==1)
{ n=2; search(); }
else
{ n=0; return; }
}
}
else
{
cout<<"请输入您要查询的学生姓名"<<endl;
cin>>name1;
if(!find1(name1))
{
cout<<"没有该生信息,是否选择另一种查询方式?"<<endl<<"1.是 2.否"<<endl;
cin>>N;
if(N==1)
{ n=1; search();}
else
{ n=0; return; }
}
} n=0;//方便下次使用查询
}
bool message::find1(string name)//按姓名查找 ,引用bool函数,成立返回true,不成立返回false
{
Student *p=head;int i=0;
while(p)
{
if(p->get_name()==name)
{
p->show();i=1;
}
p=p->next;
}
if(i==0) return false;
return true;
}
Student* message::find2(int id)//按学号查找
{
Student *p=head;
while(p!=NULL)
{
if(id==p->get_id())
{
return p;
}
p=p->next;
}
return p;//返回查到数据的地址指针
}
void message::revision()//修改
{
int id;
int n,y,t,year,month,day,age;//y为修改人数,n为修改选项 ,t为继续选择
string name,spe,sex,address;
float Escore;//定义数据
if(head==NULL)//查看是否有数据
{
cout<<"这是一个空列表,请先添加学生信息"<<endl;
return;
}
cout<<"请输入您要修改的人数:"<<endl;
cin>>y;
while(y>m)
{
if(y>m)
cout<<"输入人数大于总人数,请重新输入"<<endl;
cin.clear();// 清空上一次输入的数据,重新输入,防止进入死循环
cin>>y;
}
for(int j=0;j<y;j++)//循环修改次数
{
p1=head;
p2=head;
cout<<"请输入您要修改的学生学号"<<endl;
cin>>id;
p1=find2(id);//利用查找函数传出数据的指针,将p1赋值
if(p1)//如果p1不为空
{
do{
system("pause");
system("cls");
cout<<"****************"<<endl
<<"*****1.学号*****"<<endl
<<"*****2.姓名*****"<<endl
<<"*****3.性别*****"<<endl
<<"*****4.专业*****"<<endl
<<"*****5.出生日期*"<<endl
<<"*****6.英语成绩*"<<endl
<<"*****7.地址*****"<<endl
<<"*****8.返回*****"<<endl
<<"****************"<<endl;
cout<<"请选择您要修改的选项:";
cin>>n;
switch(n)
{
case 1: cout<<"请输入您要修改的学号:"<<endl;
cin>>id;
while(find2(id))
{
cout<<"学号已存在,请重新输入"<<endl;
cin>>id;
}
p1->set_id(id);
cout<<"修改成功"<<endl;
break;
case 2: cout<<"请输入您要修改的姓名:"<<endl;
cin>>name;
p1->set_name(name);
cout<<"修改成功"<<endl;
break;
case 3: cout<<"请输入您要修改的性别:"<<endl<<"1.男 2.女"<<endl<<"请选择你要输入的性别编号";
cin>>n;
if(n==1)
p1->set_sex("男");
if(n==2)
p1->set_sex("女");
cout<<"修改成功"<<endl;
break;
case 4: cout<<"请输入您要修改的专业:"<<endl<<"1.理科 2.文科"<<endl<<"请选择你要输入的专业编号";
cin>>n;
if(n==1)
p1->set_spe("文科");
if(n==2)
p1->set_spe("理科");
cout<<"修改成功"<<endl;
break;
case 5: cout<<"*****出生日期*****"<<endl;
cout<<"年:";
cin>>year;
p1->set_year(year);
cout<<"月:";
cin>>month;
p1->set_month(month);
cout<<"日:";
cin>>day;
p1->set_day(day);
cout<<"**************"<<endl;
break;
case 6: cout<<"请输入您要修改的英语成绩:";
cin>>Escore;
p1->set_Escore(Escore);
cout<<"修改成功"<<endl;
break;
case 7: cout<<"请输入您要修改的地址:";
cin>>address;
p1->set_address(address);
cout<<"修改成功"<<endl;
break;
case 8: break;
default:cout<<"选择错误,请重新选择"<<endl;
}
}while(n!=8);//当n等于8时,跳出修改
cout<<"修改完毕"<<endl;
}
else
{
j--;cout<<"没有该生信息"<<endl;//如果未找到,将已经加上的j减一
}
}
cout<<"是否继续修改?"<<endl;
cout<<"1.是 2.否"<<endl;
cin>>t;
if(t==1)
revision();
else return;
}
void message::tongji()//信息统计
{
int i=0,j=0,k=0,a;//i,j,k为各统计人数 ,a为选择条件
p1=head;
if(head==NULL)
cout<<"这是一个空列表,请先添加学生信息"<<endl;
cout<<"请选择统计类型:"<<endl;
cout<<"1.性别"<<"\t"<<"2.专业"<<"\t"<<"3.年龄"<<endl;
cin>>a;
switch(a)
{
case 1:
cout<<"男生:"<<endl;
while(p1!=NULL)
{
if(p1->get_sex()=="男")
{ p1->show();++i;}
p1=p1->next;
}
cout<<"男生共"<<i<<"人"<<endl;
p1=head;
cout<<"女生:"<<endl;
while(p1!=NULL)
{
if(p1->get_sex()=="女")
{ p1->show();++j;}
p1=p1->next;
}
cout<<"女生共"<<j<<"人"<<endl;
p1=head;
break;
case 2:
cout<<"文科:"<<endl;
while(p1!=NULL)
{
if(p1->get_spe()=="文科")
{ p1->show();++i;}
p1=p1->next;
}cout<<"文科共"<<i<<"人"<<endl;
p1=head;
cout<<"理科:"<<endl;
while(p1!=NULL)
{
if(p1->get_spe()=="理科")
{ p1->show();++j;}
p1=p1->next;
}cout<<"理科共"<<j<<"人"<<endl;
p1=head;
break;
case 3:
cout<<"年龄为16~18:"<<endl;
while(p1!=NULL)//如果有数据
{
age_(p1);//将p1当做参数传到计算年龄的函数中,返回计算信息
if(p1->get_age()>=16&&p1->get_age()<=18)
{
cout<<"年龄:"<<p1->get_age()<<endl;
p1->show();
++i;
}
p1=p1->next;//判断下一个数据符合类型
}cout<<"年龄为16~18共"<<i<<"人"<<endl;
p1=head;//重新将p1指向开头
cout<<"年龄为19~21:"<<endl;
while(p1!=NULL)
{
age_(p1);
if(p1->get_age()>=19&&p1->get_age()<=21)
{
cout<<"年龄:"<<p1->get_age()<<endl;
p1->show();
++j;
}
p1=p1->next;
}cout<<"年龄为19~21共"<<j<<"人"<<endl;
p1=head;
cout<<"年龄在16以下,21以上:"<<endl;
while(p1!=NULL)
{ age_(p1);
if(p1->get_age()<16||p1->get_age()>21)
{
cout<<"年龄:"<<p1->get_age()<<endl;
p1->show();
++k;
}
p1=p1->next;
}cout<<"有"<<k<<"年龄在16以下,21以上"<<endl;
break;
default:cout<<"选择错误"<<endl;
}
cout<<"是否继续统计"<<endl<<"1.是 2.否"<<endl;
cin>>a;
if(a==1) tongji();
else return;
}
void message::age_(Student *pt)//年龄计算
{
int age_;
age_=2019-pt->get_year();//用2019-pt指向的年,得出年龄,并返回给p1
pt->set_age(age_);
}
void message::shanchu()//删除
{
int id1;int n;//n为判断是否按姓名查找学号和是否继续执行条件
string name1;//定义查找的名字和学号
p2=head;
if(head==NULL)
{
cout<<"这是一个空列表,请先添加学生信息"<<endl;
return;
}
cout<<"请选择您要查找的方式:1.姓名 2.学号"<<endl;
cin>>n;
if(n==1)
{
cout<<"请输入您要删除的学生姓名"<<endl;
cin>>name1;
if(!find1(name1))//如果find1传来的为flase,无此人
cout<<"无此人信息"<<endl;//如果未找的,会选择是否继续删除
else n=2; //重名现象,再用学号查找
}
if(n==2)
{
cout<<"请输入您要删除的学生学号:"<<endl;
cin>>id1;
p1=find2(id1);//将找的的数据的地址赋值给p1
if(p1)
{
ofstream out_("删除文件2.txt",ios::app);
out_<<"学号:"<<p1->get_id()<<" "<<"姓名:"<<p1->get_name()<<" "
<<"性别:"<<p1->get_sex()<<" "<<"专业:"<<p1->get_spe()<<" ";
out_<<"出生日期:"<<p1->get_year()<<"年"<<" "<<p1->get_month()<<"月"<<" "<<p1->get_day()<<"日"<<" ";
out_<<"家庭地址:"<<p1->get_address()<<" "<<"年龄:"<<p1->get_age()<<" " ;
out_<<"英语成绩:"<<p1->get_Escore()<<endl;
if(p1==head)
head=p1->next;
else
{
p2->next=p1;
p2->next=p1->next;
}
delete p1;
cout<<"删除成功"<<endl;
--m;
}
else cout<<"没有该生信息"<<endl;
}
if(head==NULL)
{
cout<<endl
<<"*******************************"<<endl
<<"***信息已全部删除,现文件为空***"<<endl
<<"*******************************"<<endl;
return;
}
cout<<"是否继续删除?"<<endl<<"1.是 2.否"<<endl;
cin>>n;
if(n==1)
shanchu();
else return;
}
void message::read()//读取
{
int year,month,day;
int id;
string name,spe,sex,address;
float Escore;
head=NULL;
p2=head;
ifstream in1("学生信息管理2.txt",ios::in);
in1>>m;
if(m==0)
{
cout<<"*****此为空文件*****"<<endl;
return;
}
for(int j=0;j<m;j++)
{
p1=new Student();
in1>>id
>>
name>>sex>>spe>>year>>month>>day
>>Escore>>address;
p1->set_id(id);
p1->set_name(name);
p1->set_sex(sex);
p1->set_spe(spe);
p1->set_year(year);
p1->set_month(month);
p1->set_day(day);
p1->set_Escore(Escore);
p1->set_address(address);
p1->next=NULL;
if(head==NULL)head=p1;
else
p2->next=p1;
p2=p1;
p2->next=NULL;
}
in1.close();
p2->next=NULL;
cout<<"***************************************************************"<<endl
<<"***上次保存信息已自动导入***"<<endl;
}
void message::rank()//排序
{
if(head==NULL)//判断是否为空文件
{
cout<<"这是一个空列表,请先添加学生信息"<<endl;
return;
}
cout<<"按英语成绩排序";
Student *pend=head,*p0=new Student();//定义指针pend,并为头结点,开辟新的空间p0
p0->next=head;
p1=head->next;
while(p1)
{ Student *pt=p0->next;
p2=p0;
while(pt!=p1&&pt->get_Escore()>=p1->get_Escore())
{
pt=pt->next;
p2=p2->next;
}
if(pt==p1)
pend=p1;
else
{
pend->next=p1->next;
p1->next=pt;
p2->next=p1;
}
p1=pend->next;
}
head=p0->next;
delete p0;
show1();
}
void message::save() //文件的保存
{
ofstream out[2];//定义一个保存文件流数组out【】
out[0].open("学生信息管理1.txt",ios::out);
out[1].open("学生信息管理2.txt",ios::out);
for(int i=0;i<2;i++)
{
p1=head;
if(i==0)
out[i]<<"共"<<m<<"名学生"<<endl;
if(i==1)
out[i]<<m<<endl;
while(p1!=NULL)
{
if(i==0)
{
out[i]<<"学号:"<<p1->get_id()<<" "<<"姓名:"<<p1->get_name()<<" "
<<"性别:"<<p1->get_sex()<<" "<<"专业:"<<p1->get_spe()<<" ";
out[i]<<"出生日期:"<<p1->get_year()<<"年"<<" "<<p1->get_month()<<"月"<<" "<<p1->get_day()<<"日"<<" ";
out[i]<<"家庭地址:"<<p1->get_address()<<" "<<"年龄:"<<p1->get_age()<<" " ;
out[i]<<"英语成绩:"<<p1->get_Escore()<<endl;
p1=p1->next;
}
if(i==1)
{out[i]<<p1->get_id()<<" "<<p1->get_name()<<" "<<p1->get_sex()<<" "<<p1->get_spe()<<" ";
out[i]<<p1->get_year()<<" "<<p1->get_month()<<" "<<p1->get_day()<<" ";
out[i]<<p1->get_Escore()<<endl<<p1->get_address()<<endl;
p1=p1->next;
}
}
}
out[0].close();
out[1].close();
cout<<"保存成功"<<endl<<"***信息已保存于学生信息管理1中***"<<endl;
}
void menu()//菜单
{
system("date/t");//返回当前系统时间
system("time/t");//显示系统时间
cout<<"******************************************"<<endl;
cout<<"1. 新增学生信息"<<endl;
cout<<"2. 删除学生信息"<<endl;
cout<<"3. 导入学生信息"<<endl;
cout<<"4. 学生信息搜索"<<endl;
cout<<"5. 学生信息统计"<<endl;
cout<<"6. 按英语成绩排序"<<endl;
cout<<"7. 浏览学生信息"<<endl;
cout<<"8. 保存学生信息"<<endl;
cout<<"9. 修改学生信息"<<endl;
cout<<"0. 退出"<<endl;
cout<<"******************************************"<<endl;
}
void Mima()
{
cout<<"___________________________________________________________"<<endl;
cout<<"|*****************欢迎使用学生成绩管理系统****************|"<<endl;
cout<<"|---------------------------------------------------------|"<<endl;
system("color 3"); //调用 color函数可以改变控制台的前景色和背景色
// 其中color后面的数字是背景色代号 ,大写字母是前景色代号
int i=3;
char num[20];
char num1[20];
char user[20];
char user1[20];
char mima[20];
char mima1[20];
char mima2[20];
FILE *fp; //定义一个指向文件的指针变量
char ch;
do
{
cout<<"1.登录"<<endl;
cout<<"2.注册"<<endl;
cout<<"3.修改密码"<<endl;
cout<<"4.忘记密码"<<endl;
cout<<"0.退出"<<endl;
cout<<"请输入你要选择的功能0~3 : "<<endl;
cin>>ch;
cin.clear(); //清空缓冲区
switch(ch)
{
case '1': //将fopen函数的返回值赋给指针变量fp
if((fp=fopen("mima.txt","r"))==NULL) //第一次登录时先创建账号
{
fclose(fp); //把指向fp的文件关闭,此后fp不在指向该文件
cout<<"请先注册账号"<<endl;
Mima();
}
else //第二次登陆时直接用密码账号登录即可
{
fp=fopen("mima.txt","r"); //打开文件
fscanf(fp,"%s %s",user,mima); // 从磁盘文件上读入ASCLL字符
while(1)
{
cout<<"请输入用户名:"<<endl;
cin>>user1;
if(strcmp(user,user1)!=0)
{
cout<<"用户名错误!!请重新输入!!"<<endl;
}
else
{
break;
}
}
cout<<"请输入密码:"<<endl;
while(1)
{
cin>>mima1;
if(strcmp(mima,mima1)!=0)
{
cout<<"密码错误!请重新输入密码:"<<endl;
if(i==1)
{
exit(0); //三次密码输入错误后直接结束该程序
}
i--;
}
else
{
break;
}
}
}
fclose(fp); //关闭文件
break;
case '2':
fp=fopen("mima.txt","w");
cout<<"请创建用户:"<<endl;
cin>>user;
while(1)
{
cout<<"请输入密码"<<endl;
cin>>mima;
cout<<"请从新输入密码"<<endl;
cin>>mima1;
if(strcmp(mima,mima1)!=0)
{
cout<<"密码错误!"<<endl;
}
else
{
cout<<"请输入你的联系方式:(方便找回账号)"<<endl;
cin>>num;
cout<<"创建成功!!"<<endl;
break;
}
}
fprintf(fp,"%s %s %s",user,mima,num);// 将用户名和密码和号码输出到磁盘文件上
fclose(fp);//关闭文件
break;
case '3':
fp=fopen("mima.txt","r");
fscanf(fp,"%s %s %s",user,mima,num);
fclose(fp);
cout<<"请输入用户名:"<<endl;
while(1)
{
cin>>user1;
if(strcmp(user,user1)!=0)
{
cout<<"用户名错误!请重新输入用户名:"<<endl;
}
else
{
break;
}
}
cout<<"请输入原密码:"<<endl;
while(1)
{
cin>>mima1;
if(strcmp(mima,mima1)!=0)
{
cout<<"密码错误!请重新输入密码:";
}
else
{
break;
}
}
cout<<"请输入新的密码:"<<endl;
cin>>mima1;
cout<<"请确认密码:";
while(1)
{
cin>>mima2;
if(strcmp(mima1,mima2)!=0)
{
cout<<"密码错误!请重新输入密码:";
}
else
{
strcpy(mima,mima1); //修改密码成功后用新密码覆盖原密码
cout<<"修改成功!!"<<endl;
fp=fopen("mima.txt","w");
fprintf(fp,"%s %s %s",user,mima,num); //然后重新输出到磁盘文件上
fclose(fp);
break;
}
}
break;
case '4':
fp=fopen("mima.txt","r");
fscanf(fp,"%s %s %s",user,mima,num);
fclose(fp);
cout<<"请输入用户名:"<<endl;
while(1)
{
scanf("%s",user1);
if(strcmp(user,user1)!=0)
{
cout<<"用户名错误!请重新输入用户名:"<<endl;
}
else
{
break;
}
}
cout<<"请输入联系方式:"<<endl;
while(1)
{
cin>>num1;
if(strcmp(num,num1)!=0)
{
cout<<"联系方式错误,请重新输入:";
}
else
{
break;
}
}
cout<<"请输入新的密码:"<<endl;
cin>>mima1;
cout<<"请确认密码:";
while(1)
{
cin>>mima2;
if(strcmp(mima1,mima2)!=0)
{
cout<<"密码错误!请重新输入密码:";
}
else
{
strcpy(mima,mima1); //修改密码成功后用新密码覆盖原密码
cout<<"修改成功!!"<<endl;
fp=fopen("mima.txt","w");
fprintf(fp,"%s %s %s",user,mima,num); //然后重新输出到磁盘文件上
fclose(fp);
break;
}
}
break;
case '0':cout<<endl<<"即将退出程序!"<<endl;
exit(0); //结束程序
default :system("CLS"); //清屏
cout<<"输入错误!请重新输入!"<<endl;
Mima(); //当输入的不是指定的三个数时重新跳转本页面
}
}while(ch==1||ch==0);
system("CLS"); //执行完后实现清屏操作
}
int main()
{
message s;
int i;
Mima();
do{
menu();
cin>>i;
system("cls");
switch(i)
{
case 1:system("cls");s.add();break;
case 2:system("cls");s.shanchu();break;
case 3:system("cls");s.read();break;
case 4:system("cls");s.search();break;
case 5:system("cls");s.tongji();break;
case 6:system("cls");s.rank();break;
case 7:system("cls");s.show1();break;
case 8:system("cls");s.save();break;
case 9:system("cls");s.revision();break;
case 0:cout<<"感谢你的使用!"<<endl;system("pause");return 0;
default:cout<<"选择错误,请重新输入"<<endl;system("cls");
}
}while(i!=0);
system("pause");
return 0;
}
版权声明:本文为qq_43573663原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。