qq_38174200 2017-04-04 11:32 采纳率: 0%
浏览 1214
已结题

急! C++链表中怎样实现对文件的删除和查询

```#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();
};
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
    • ¥15 画两个图 python或R