c++中的链表应用

#include<fstream.h>
#include<process.h>
#include<iomanip.h>
#include<string.h>
const char*file_name="stuinfo.txt";

class Student
{
public:
Student();
~Student();
void add_info();//添加学生信息
void del_info();//删除学生信息
void search_info_name();//通过学生名字来查找信息
void search_info_Snum();//通过学号来查找信息
void out_all_info();//输出所有学生的信息
void creat_file();//创建文件,学生的信息保存在文件里,即使退出了程序,下次运行还可以找到学生的信息
int get_info();//从文件中获取学生的信息
int search_file();//判断是否存在一个保存学生信息的文件
protected:
char name[10];//学生名字最长为10个字符
long Snum;//学号是长整行
int Chinese,English,Math,Computer;//语文 英语 数学 计算机 共四门课
Student *head,*next;//创建链表使用的指针
};

/**
*构造函数,用来创建对象同时初始化相关指针变量
*/
Student::Student()
{
head=NULL;
next=NULL;
}

/**
*判断是否存在一个保存学生信息的文件
*/
int Student::search_file()
{
ifstream input_file;
input_file.open(file_name);
if(!input_file)//不存在文件
return 0;
else
input_file.close();//存在文件
return 1;
}

/**
*从文件中获取学生的信息
*/
int Student::get_info()
{
Student *temp,*loop;
temp=new Student();
loop=new Student();
ifstream out_file;
out_file.open(file_name,ios::beg);//打开文件,设置读指针在文件的开头
if(!out_file)//打开文件失败
{
cout<<"Fail to get information from the file!";
cout<<"/nPress any key to exit.";
cin.get();
exit(0);//结束程序
}
while(!out_file.eof())//循环读文件,直到读到了文件的末尾
{
//文件的结构是:文件的一行就是一个学生的信息, 从左到右是:学号 姓名 语文 英语 数学 计算机
//这些信息可以在本程序提供的功能生成并保存到文件里
out_file>>(temp->Snum)>>(temp->name)>>(temp->Chinese)>>(temp->English)>>(temp->Math)>>(temp->Computer);
if(temp->Snum==0) break;
//使用链表把学生的信息保存到内存中
if(head==NULL) head=temp;
else
{
if(head->Snum>temp->Snum)
{
temp->next=head;
head=temp;
}
else
{
loop=head;
while(loop->next!=NULL)
{
if(loop->next->Snum>temp->Snum)
{
temp->next=loop->next;
loop->next=temp;
break;
}
loop=loop->next;
}
if(loop->next==NULL)
{
loop->next=temp;
temp->next=NULL;
}
}
}
temp=new Student;
loop=new Student;
}
out_file.close();//关闭文件
if(head==NULL) return 0;
else return 1;
}

/**
*创建文件,可以增加学生的信息
*/
void Student::creat_file()
{
Student *temp,*loop;
temp=new Student;
loop=new Student;
ofstream creat_file;
creat_file.open(file_name,ios::beg);
if(!creat_file)
{
cout<<"Fail to creat stdent information file!";
cout<<"/nPress any key to exit.";
cin.get();
exit(0);
}
cout<<"-----------------------------------------------------------"<<endl;
cout<<"Now creat file of student information."<<endl;
cout<<"Input the number of the student (0 to end):";
cin>>temp->Snum;//输入学号
while(temp->Snum!=0)//学号不为0
{
cout<<"Input the name of the student:";
cin>>temp->name;//姓名
cout<<"Input the score of Chinese:";
cin>>temp->Chinese;//语文
cout<<"Input the score of English:";
cin>>temp->English;//英语
cout<<"Input the score of Math:";
cin>>temp->Math;//数学
cout<<"Input the score of Computer Science:";
cin>>temp->Computer;//计算机
creat_file<<temp->Snum<<" "<<temp->name<<" "<<temp->Chinese<<" "<<temp->English<<" "<<temp->Math<<" "<<temp->Computer<<endl;
temp=new Student;
loop=new Student;
cout<<"/n/nInput the number of the student (0 to end):";
cin>>temp->Snum;//输入学号
}
creat_file<<0;
creat_file.close();
}
/**
*输出所有学生的信息
*/
void Student::out_all_info()
{
Student*temp;
temp=new Student;
cout<<"-----------------------------------------------------------"<<endl;
cout<<"The flowing is the information of the students."<<endl<<endl;
cout<<"Snum"<<setw(9)<<"name"<<setw(9)<<"Chinese"<<setw(9)<<"English"<<setw(9)
<<"Math"<<setw(18)<<"Coputer Science"<<endl;
temp=head;
while(temp!=NULL)//循环读链表,输出所有学生的信息
{
cout<<(temp->Snum)<<setw(9)<<(temp->name)<<setw(9)<<(temp->Chinese)<<setw(9)<<(temp->English)
<<setw(9)<<(temp->Math)<<setw(12)<<(temp->Computer)<<endl;
temp=temp->next;
}
}
/**
*通过姓名查找信息
*/
void Student::search_info_name()
{
Student *temp;
char name[10];
temp=new Student;
cout<<"-----------------------------------------------------------"<<endl;
cout<<"Input the name of the student you want to search:";
cin>>name;//输入姓名
temp=head;
while(temp!=NULL&&strcmp(temp->name,name)!=0)//在链表中逐个的比较姓名
temp=temp->next;
if(temp==NULL)//没有找到信息,就是说找不到需要查找姓名的学生的信息
cout<<"Sorry,no such student of the name you input!"<<endl;
else//输出学生的信息
{
cout<<"The flowing is the information of the student "<<name<<endl;
cout<<"Snum"<<setw(9)<<"name"<<setw(9)<<"Chinese"<<setw(9)<<"English"<<setw(9)
<<"Math"<<setw(18)<<"Coputer Science"<<endl;
cout<<(temp->Snum)<<setw(9)<<(temp->name)<<setw(9)<<(temp->Chinese)<<setw(9)<<(temp->English)
<<setw(9)<<(temp->Math)<<setw(12)<<(temp->Computer)<<endl;
}
}
/**
*通过学号查找信息
*/
void Student::search_info_Snum()
{
Student*temp;
long num;
temp=new Student;
cout<<"---------------------------------------------------------"<<endl;
cout<<"Input the number of the student you want to search:";
cin>>num;//输入学号
temp=head;
while(temp!=NULL&&temp->Snum!=num)//比较学号
temp=temp->next;
if(temp==NULL)//没有找到信息
cout<<"Sorry,no such student of the number you input!"<<endl;
else//输出信息
{
cout<<"The flowing is the information of the student "<<num<<endl;
cout<<"Snum"<<setw(9)<<"name"<<setw(9)<<"Chinese"<<setw(9)<<"English"<<setw(9)
<<"Math"<<setw(18)<<"Coputer Science"<<endl;
cout<<(temp->Snum)<<setw(9)<<(temp->name)<<setw(9)<<(temp->Chinese)<<setw(9)<<(temp->English)
<<setw(9)<<(temp->Math)<<setw(12)<<(temp->Computer)<<endl;
}
}
/**
*增加学生的信息
*/
void Student::add_info()
{
Student *temp,*loop,*loop1;
temp=new Student;
loop=new Student;
loop1=new Student;
cout<<"-----------------------------------------------------------"<<endl;
cout<<"Now add information of student."<<endl;
cout<<"Input the number of the student (0 to end):";
cin>>temp->Snum;//输入学号
loop1=temp;
while(temp->Snum!=0)//学号不为0
{
cout<<"Input the name of the student:";
cin>>temp->name;//姓名
cout<<"Input the score of Chinese:";
cin>>temp->Chinese;//语文
cout<<"Input the score of English:";
cin>>temp->English;//英语
cout<<"Input the score of Math:";
cin>>temp->Math;//数学
cout<<"Input the score of Computer Science:";
cin>>temp->Computer;//计算机
if(head==NULL) head=temp;//将信息添加到链表中
else
{
if(head->Snum>temp->Snum)
{
temp->next=head;
head=temp;
}
else
{
loop=head;
while(loop->next!=NULL)
{
if(loop->next->Snum>temp->Snum)
{
temp->next=loop->next;
loop->next=temp;
break;
}
loop=loop->next;
}
if(loop->next==NULL)
{
loop->next=temp;
temp->next=NULL;
}
}
}
temp=new Student;
loop=new Student;
cout<<"/n/nInput the number of the student (0 to end):";
cin>>temp->Snum;
}
cout<<"/nThe information you input is the flowing."<<endl;
cout<<"Snum"<<setw(9)<<"name"<<setw(9)<<"Chinese"<<setw(9)<<"English"<<setw(9)
<<"Math"<<setw(18)<<"Coputer Science"<<endl;
while(loop1!=NULL)
{
cout<<(loop1->Snum)<<setw(9)<<(loop1->name)<<setw(9)<<(loop1->Chinese)<<setw(9)<<(loop1->English)
<<setw(9)<<(loop1->Math)<<setw(12)<<(loop1->Computer)<<endl;
loop1=loop1->next;
}


}
/**
*通过学号删除信息
*/
void Student::del_info()
{
Student *temp,*loop1,*loop2;
long snum;
temp=new Student;
loop1=new Student;
loop2=new Student;
cout<<"----------------------------------------------------------"<<endl;
cout<<"Input the number of the student you want to delete:";
cin>>snum;//输入学号
temp=head;
while(temp!=NULL&&temp->Snum!=snum)//通过学号查找信息
{
loop1=temp;
temp=temp->next;
}
if(temp==NULL)//没有相应学号的学生信息
cout<<"Sorry,no such student of the number you input!"<<endl;
else
{
loop1->next=temp->next;//跳过链表的一个节点temp
cout<<"The information you delete is the flowing."<<endl;
cout<<"Snum"<<setw(9)<<"name"<<setw(9)<<"Chinese"<<setw(9)<<"English"<<setw(9)
<<"Math"<<setw(18)<<"Coputer Science"<<endl;
cout<<(temp->Snum)<<setw(9)<<(temp->name)<<setw(9)<<(temp->Chinese)<<setw(9)<<(temp->English)
<<setw(9)<<(temp->Math)<<setw(12)<<(temp->Computer)<<endl;
if(temp->Snum==head->Snum) head=head->next;
delete temp;//删除节点
}
}
/**
*析构函数,只用程序的正常结束才会执行改函数,并且把学生的信息保存到文件中
*/
Student::~Student()
{
Student*temp;
temp=new Student;
ofstream write_file;
write_file.open(file_name,ios::beg);
if(!write_file)
{
cout<<"Fail to write the information to the file!"<<endl;
cout<<"Press any key to exit.";
cin.get();
exit(0);
}
temp=head;
while(temp!=NULL)
{
write_file<<temp->Snum<<" "<<temp->name<<" "<<temp->Chinese<<" "<<temp->English<<" "<<temp->Math<<" "<<temp->Computer<<endl;
temp=temp->next;
}
write_file<<0;
write_file.close();
}
/**
*主函数,主要提供一些菜单选项
*/
void main()
{
char select;
int selection;
Student student;
cout<<"/n########################################################"<<endl;
if(student.search_file()==0)
{
cout<<"There is no file of student information."<<endl;
cout<<"Do you want to creat it?(Y/N):";
cin>>select;
if(select=='Y'||select=='y')
student.creat_file();
else
exit(0);
}
if(student.get_info()==0)
{
cout<<"There is no information in the file"<<endl;
cout<<"Do you want to add information to the file?(Y/N):";
cin>>select;
if(select=='y'||select=='Y')
student.add_info();
else exit(0);
}
cout<<"/n/n##########################################################"<<endl;
cout<<"Information of students.Selections are flowing."<<endl;
cout<<"Input number 1 to search information by name."<<endl;
cout<<"Input number 2 to search information by number."<<endl;
cout<<"Input number 3 to add nuw information to the file."<<endl;
cout<<"Input number 4 to delete information from the file."<<endl;
cout<<"Input number 5 to view all the students' information."<<endl;
cout<<"Input other numbers to exit."<<endl;
cout<<"Input your selection please:";
cin>>selection;
while(selection>=1&&selection<=5)
{
if(selection==1) student.search_info_name();
if(selection==2) student.search_info_Snum();
if(selection==3) student.add_info();
if(selection==4) student.del_info();
if(selection==5) student.out_all_info();
cout<<"/n/n########################################################"<<endl;
cout<<"Information of students.Selections are flowing."<<endl;
cout<<"Input number 1 to search information by name."<<endl;
cout<<"Input number 2 to search information by number."<<endl;
cout<<"Input number 3 to add nuw information to the file."<<endl;
cout<<"Input number 4 to delete information from the file."<<endl;
cout<<"Input number 5 to view all the students' information."<<endl;
cout<<"Input other numbers to exit."<<endl;
cout<<"Input your selection please:";
cin>>selection;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值