第1个回答 推荐于2016-10-28
下面这个是我以前自己写的,你可以参考一下,实现的功能和你那是一样的几乎是一样的:
//link.h
#ifndef LINK_HHH
#define LINK_HHH
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
class stu
{
public:
int num;
string name;
float yuwen;
float shuxue;
float yingyu;
float all;
float average;
stu *next;
void display()//输出一个数据
{ cout.setf(ios::left);
cout<<setw(8)<<num<<setw(6)<<name<<setw(8)<<yuwen<<setw(8)<<shuxue<<setw(8)<<yingyu
<<setw(10)<<setprecision(2)<<setiosflags(ios::fixed)<<all<<setw(10)<<average<<resetiosflags(ios::fixed)<<endl;
cout.unsetf(ios::left);
}
void duru(ifstream &ifile)//将文本读入
{
ifile>>num>>name>>yuwen>>shuxue>>yingyu>>all>>average;
}
};
class student
{
public:
student();
~student();
void creat();
void display1();
void display2();
void save();
void add();
void del();
void repair();
void paixu();
void paixu1();
void search();
void search1();
void search2();
void search3();
void geshi1();
private:
stu *head,*end;
ifstream ifile;
ofstream ofile;
};
#endif
//link.cpp
#ifndef LINK_CPP
#define LINK_CPP
#include"link.h"
#include<conio.h>
student::student()
{
head=new stu;
head->next=new stu;
end=head->next;//用end来实现文件的存取
ifile.open("stu.text");
if(!ifile)
{
cerr<<"文本为空!!!"<<endl;
}
else
{
while(!ifile.eof())
{
end->duru(ifile);
if(end->num==-842150451)//这种情况是判断当没有数据的时候就不再从磁盘中输出
break;
end->next=new stu;//用end,end一直用来接收从磁盘中读取的数据,通过动态存取的方式
end=end->next;
}
end->next=NULL;
ifile.close();
cout<<"数据读入完毕……"<<endl;
}
}
void student::save()//对数据的保存
{
ofstream ofile("stu.text");//用ASCII文件来存取数据
if(!ofile)//这种情况一般不发生
{
cerr<<"数据保存出错!!!"<<endl;
abort();
}
else
{
for(stu *p=head->next;p!=end;p=p->next)//将数据全部存入文件中
{
ofile<<p->num<<setw(8)<<p->name<<setw(8)<<p->yuwen<<setw(8)
<<p->shuxue<<setw(8)<<p->yingyu<<setw(8)<<p->all<<setw(8)<<p->average<<endl;
}
ofile.close();
}
}
student::~student()//调用析构函数
{
save();
stu *p=head->next;
if(p->num==-842150451)//当不存在任何数据,而要关闭的情况,防止出错
delete head,end;
else
{
while(p->next!=end)
{
head->next=p->next;
delete p;
p=head->next;
}
delete head,end;
}
cout<<"已结束!!!"<<endl;
}
void student::creat()
{
int num;
string name;
float yuwen;
float shuxue;
float yingyu;
while(1)
{
exp:
cout<<"请输入 学号(按 0 退出):"<<endl;
cin>>num;
if(num==0)
break;
for(stu *p=head->next;p!=end;p=p->next)//用来判断学号是否会有重复的,不应该有学号重复的情况
{
if(p->num==num)
{
cout<<"已存在该学号!请重新输入……"<<endl;
goto exp;
}
}
end->num=num;
exp1:
cout<<"请输入 姓名:"<<endl;
cin>>name;
for(int i=0;name[i]!='\0';i++)// 判断名字,名字中只能有字母
if(name[i]>='a' &&name[i]<='z' || name[i]>='A' && name[i]<='Z');
else
{
cout<<"名字输入有误,请重新输入……"<<endl;
goto exp1;
}
end->name=name;
cout<<"请输入 语文:"<<endl;
cin>>yuwen;
end->yuwen=yuwen;
cout<<"请输入 数学:"<<endl;
cin>>shuxue;
end->shuxue=shuxue;
cout<<"请输入 英语:"<<endl;
cin>>yingyu;
end->yingyu=yingyu;
end->all=(end->yuwen+end->shuxue+end->yingyu);
end->average=(end->all)/3.0;
end->next=new stu;
end=end->next;
}
end->next=NULL;
}
void student::geshi1()//输出的格式
{
cout<<"序号:"<<setw(4)<<"学号:"<<setw(8)<<"姓名:"<<setw(8)<<"语文:"<<setw(8)
<<"数学:"<<setw(8)<<"英语:"<<setw(8)<<"总分:"<<setw(11)<<"平均分:"<<endl;
}
void student::display1()
{
paixu();//按学号的小到大输出
geshi1();
int n=1;
for(stu *p=head->next;p!=end;p=p->next)
{
cout.setf(ios::left);
cout<<setw(8)<<n;
p->display();//用p来指向在stu中定义的函数,来达到输出的目的
n++;
}
_getch();
}
void student::display2()
{
paixu1();//按成绩的高到底输出成绩
geshi1();
int n=1;
for(stu *p=head->next;p!=end;p=p->next)
{
cout.setf(ios::left);
cout<<setw(8)<<n;
p->display();
n++;
}
_getch();
}
void student::add()// 添加数据
{
int num;
string name;
float yuwen;
float shuxue;
float yingyu;
cout<<"请输入num(按 0 退出):"<<endl;
cin>>num;
if(num==0)
return;
for(stu *p=head->next;p!=end;p=p->next)
{
if(p->num==num)
{
cout<<"已存在该学号……"<<endl;
return;
}
}
end->num=num;
exp1:
cout<<"请输入name:"<<endl;
cin>>name;
for(int i=0;name[i]!='\0';i++)
if(name[i]>='a' &&name[i]<='z' || name[i]>='A' && name[i]<='Z');
else
{
cout<<"名字输入有误,请重新输入……"<<endl;
goto exp1;
}
end->name=name;
cout<<"请输入 语文:"<<endl;
cin>>yuwen;
end->yuwen=yuwen;
cout<<"请输入 数学:"<<endl;
cin>>shuxue;
end->shuxue=shuxue;
cout<<"请输入 英语:"<<endl;
cin>>yingyu;
end->yingyu=yingyu;
end->all=(end->yuwen+end->shuxue+end->yingyu);
end->average=(end->all)/3.0;
end->next=new stu;
end=end->next;
end->next=NULL;
}
void student::paixu()//按学号排序(降序)
{
int min;
string name;
float yuwen;
float shuxue;
float yingyu;
float all;
float average;
for(stu *p=head->next;p!=end;p=p->next)//用冒泡的方式,对每个结点的num的大小进行排序
for(stu *q=p->next;q!=end;q=q->next)// 只交换数据,而他们的next却不变,从而达到交换数据的效果
{
min=p->num;
if(q->num<min)
{
p->num=q->num;
q->num=min;
name=p->name;
p->name=q->name;
q->name=name;
yuwen=p->yuwen;
p->yuwen=q->yuwen;
q->yuwen=yuwen;
shuxue=p->shuxue;
p->shuxue=q->shuxue;
q->shuxue=shuxue;
yingyu=p->yingyu;
p->yingyu=q->yingyu;
q->yingyu=yingyu;
all=p->all;
p->all=q->all;
q->all=all;
average=p->average;
p->average=q->average;
q->average=average;
}
}
}
void student::paixu1()//按成绩顺序排列
{
float max;
int num;
string name;
float yuwen;
float shuxue;
float yingyu;
float average;
for(stu *p=head->next;p!=end;p=p->next)
for(stu *q=p->next;q!=end;q=q->next)
{
max=p->all;
if(q->all>max)
{
num=p->num;
p->num=q->num;
q->num=num;
name=p->name;
p->name=q->name;
q->name=name;
yuwen=p->yuwen;
p->yuwen=q->yuwen;
q->yuwen=yuwen;
shuxue=p->shuxue;
p->shuxue=q->shuxue;
q->shuxue=shuxue;
yingyu=p->yingyu;
p->yingyu=q->yingyu;
q->yingyu=yingyu;
p->all=q->all;
q->all=max;
average=p->average;
p->average=q->average;
q->average=average;
}
}
}
void student::repair()// 修改数据
{
stu *p=head->next;
cout<<"请输入要修改的学号(num):"<<endl;// 通过学号来修改数据
int num;
int n=1;
cin>>num;
for(;p->num!=num;p=p->next)
if(p==end)break;
if(p==end)
cout<<"找不到该学号……"<<endl;
else
{
cout.setf(ios::left);//一下是对修改的重新输入的数据进行控制
cout<<setw(8)<<n;
geshi1();
p->display();
int num;
string name;
float yuwen;
float shuxue;
float yingyu;
exp:
cout<<"请输入 学号(按 0 退出):"<<endl;
cin>>num;
if(num==0)
return;
for(p=head->next;p!=end;p=p->next)
{
if(p->num==num)
{
cout<<"已存在该学号!请重新输入……"<<endl;
goto exp;
}
}
end->num=num;
exp1:
cout<<"请输入 姓名:"<<endl;
cin>>name;
for(int i=0;name[i]!='\0';i++)
if(name[i]>='a' &&name[i]<='z' || name[i]>='A' && name[i]<='Z');
else
{
cout<<"名字输入有误,请重新输入……"<<endl;
goto exp1;
}
end->name=name;
cout<<"请输入 语文:"<<endl;
cin>>yuwen;
p->yuwen=yuwen;
cout<<"请输入 数学:"<<endl;
cin>>shuxue;
p->shuxue=shuxue;
cout<<"请输入 英语:"<<endl;
cin>>yingyu;
p->yingyu=yingyu;
cout<<"修改成功……"<<endl;
}
}
void student::del()//删除学生信息
{
stu *p=head->next,*q=p;
cout<<"请输入要删除的学号(num):"<<endl;
int num,n=1;
cin>>num;
while(p->num!=num)
{
q=p;
p=p->next;
}
if(p==NULL)
cout<<"找不到该学号……"<<endl;
else
{
cout.setf(ios::left);
cout<<setw(8)<<n;
geshi1();
p->display();
cout<<"确定要删除吗???确认请按【Y】取消请按【N】…… "<<endl;
char ch;
while(1)
{
cin>>ch;
if(ch=='y' || ch=='Y')
{
q->next=p->next;
delete p;
cout<<"删除成功……"<<endl;
break;
}
if(ch=='n' || ch=='N')
return;
cout<<"输入有误!!!"<<endl;
}
}
}
void student::search()//查找
{
int command;
while(1)
{
cout<<"\n 1 按学号查询\n 2 按姓名查询\n 3 查询不及格人\n 0 退出"<<endl;
cout<<"**********************"<<endl;
command=_getch();
switch(command)
{
case '1':search1();break;
case '2':search2();break;
case '3':search3();break;
case '0':break;
}
if(command=='0')
break;
}
}
void student::search1()//学号查找
{
stu *p=head->next;
cout<<"请输入要查找的学号(num):"<<endl;
int num,n=1;
cin>>num;
for(;p->num!=num;p=p->next)
if(p->next==end)break;
if(p->next==end)
cout<<"找不到该学号……"<<endl;
else
{
geshi1();
cout.setf(ios::left);
cout<<setw(8)<<n;
p->display();
}
}
void student::search2()//姓名查找
{
stu *p=head->next;
int n=1;
cout<<"请输入要查找的姓名……"<<endl;
string name;
cin>>name;
for(;p->name!=name;p=p->next)
if(p->next==end)break;
if(p->next==end)
cout<<"找不到该同学……"<<endl;
else
{
geshi1();
cout.setf(ios::left);
cout<<setw(8)<<n;
p->display();
}
}
void student::search3()//查找不及格的人
{
int n=0,m=1;
geshi1();
for(stu *p=head->next;p!=end;p=p->next)
{
if(p->yuwen<60 || p->shuxue<60 || p->yingyu<60)
{
cout.setf(ios::left);
cout<<setw(8)<<m;
p->display();
m++;
n++;
}
}
cout<<"总的有 "<<n<<" 个不及格的人……"<<endl;
}
#endif
//main.cpp
#include"link.h"
#include<conio.h>
#include<windows.h>
void about()
{
char *str=" 谢谢使用…… \n"
"\n"
" 再见…… \n";
for(int i=0;i<strlen(str);i++)
{
if(str[i]!=' ')
Sleep(20);
cout<<str[i];
}
}
int main()
{
system("color a");//用于设置屏幕显示的颜色
password p;//账号的对象
int command,n=0;
while(1)
{
cout<<"**********************"<<endl;
cout<<"\n\n\n"
" 欢迎光临 \n"
"\n\n\n"
" 1 登陆 \n"
"\n"
" 0 退出 \n"
"\n\n"<<endl;
cout<<"**********************"<<endl;
command=_getch();
switch(command)
{
case '1':denglu1();break;
case '0':n=1;about();break;
}
if(n==1)
break;
}
_getch();
}本回答被提问者采纳