```#include
#include "Student.h"
#include
#include
using namespace std;
Student *head;
int n=0;
/********** ① **********/
void storefile1()
{
Student *p1,*p2;
ifstream infile("xinxi.txt",ifstream::binary);
if(!infile)
{
cerr<<"open error!"<<endl;
abort();
}
n=0;
while(! infile.eof())
{
p1=p2=new Student;
n++;
infile.read((char*)p1,sizeof(Student));
if(n==1)
{
head=p1;
if(p1->next==NULL)
break;
}
else
{
p2->next=p1;
p2=p1;
if(p1->next==NULL)
break;
}
}
infile.close();
}
void storefile(Student *p) //定义存储函数
{
Student *x;
x=p;
ofstream outfile("xinxi.txt",ios::app);
if(!outfile)
{
cerr<<"open error!"<<endl;
abort();
}
outfile << x->id << endl;
outfile << x->name << endl;
outfile << x->sex << endl;
outfile << (*x).next << endl;
outfile.close();
}
/********** ② **********/
void outfile() //定义读取数据函数
{
Student p;
ofstream outfile;
outfile.open("xinxi.txt",ofstream::binary);
if(!outfile)
{
cerr<<"open error"<<endl;
abort();
}
p=head;
while(p!=NULL)
{
outfile.write((char)p,sizeof(Student));
p=p->next;
}
outfile.close();
}
/********** 1 **********/
int Student::Add()//增加学生
{
Student *p = new Student;
Student *x = head;
cout << " 请输入学生学号: ";
cin >> p->id;
while( (x->next != NULL) && ( ((x->next)->id) != p->id ) )
{
x = x->next;
}
if ( (x->next != NULL) && ((x->next)->id) == p->id )
{
cout << "已经添加该学号的学生" << endl;
return 0;
}
cout << " 请输入学生姓名: ";
cin >> p->name;
cout << " 请输入学生性别(男生请输入“1” 女生请输入“0”): ";
cin >> p->sex;
p->next = head->next; // head中next域的地址 给了 p中的next域
head->next = p; // p的地址 给了 head中的next域
storefile(p);
cout << " 成功加入学生!" << endl << endl;
return 0;
}
/********** 2 ***********/
int Student::Delete (int x)// 删除学生
{
storefile1();/******************************************/
Student *p,*q;
if( (head->next) == NULL)
{
return (0);
}
q = head;
while( ((q->next) != NULL) && ( ((q->next)->id) != x ) )
{
q = q->next;
}
if(q->next == NULL)
{
return(0);
}
p = q->next;
q->next = p->next;
delete p;
outfile();/********************************************/
return(1);
}
/********** 3 ***********/
int Student::Search(int x)
{
Student *p,*q;
if( (head->id) == x)
{
cout << " 学生学号: " << head->id << endl;
cout << " 学生姓名: " << head->name << endl;
cout << " 学生性别: ";
if ( (head->sex) == 1)
cout<< "男" << endl;
else
cout << "女" << endl;
}
q = head;
while( (q->next != NULL) && ( ((q->next)->id) != x ) )
{
q = q->next;
}
if(q->next == NULL)
{
return(0);
}
cout << "学生学号: " << (q->next)->id << endl;
cout << "学生姓名: " << (q->next)->name << endl;
cout << "学生性别: ";
if ( ((q->next)->sex) == 1)
cout<< "男" << endl;
else
cout << "女" << endl;
return(1);
}
/********** 4 ***********/
void Student::Display()//全部显示
{
Student *p,*q;
q=head;
while ( (q->next) != NULL )
{
cout << "学生学号: " << (q->next)->id << endl;
cout << "学生姓名: " << (q->next)->name << endl;
cout << "学生性别: ";
if ( ((q->next)->sex) == 1)
cout<< "男" << endl;
else
cout << "女" << endl;
q= q->next;
}
if ((q->next) != NULL)
{
cout << "学生学号: " << q->id << endl;
cout << "学生姓名: " << q->name << endl;
cout << "学生性别: ";
if ( (q->sex) == 1)
cout<< "男" << endl;
else
cout << "女" << endl;
}
}
int main()
{
head = new Student;
head->next = NULL;
int num;// num:操作指令
while (1)
{
cout << " 请输入您的操作 " <<endl<<endl;
cout << "1.增加学生 2.删除学生 3.查询学生 4.显示全部 5.结束操作"<<endl<<endl;
cin >> num;//输入对应操作
if (num == 1)
{
head->Add();
}
if (num == 2)
{
int del;
cout << " 请输入要删除的学号: ";
cin >> del;
if( head->Delete(del) )
{
cout << " 成功删除该学生" << endl;
}
else
cout << endl << " 班级中无该学生!!!" << endl;
}
if (num == 3)
{
int ser;
cout << " 请输入要查询的学号: ";
cin >> ser;
if( head->Search (ser) == 1 )
{
cout <<" 成功查询到该学生" << endl;
}
else
cout << endl << " 班级中无该学生!!!" << endl;
}
if (num == 4)
{
cout << "该办全体学生信息如下:" << endl;
head->Display();
}
if (num == 5)
{
break;
}
}
return 0;
}
代码如上, 求大神帮修改一下,实现对文件的删除、查询、显示全部这三部分
谢谢!
类的具体内容如下
#include <string>
using namespace std;
class Student //(学号 姓名 性别 指向下一位的指针)
{
private:
int id;
string name;
bool sex; //男为1 女为0
public:
Student *next;
int Add();
int Delete (int x);
int Search(int x);
void Display();
friend void storefile(Student *p);
friend void storefile1();
friend void outfile();
};