hhhhyiuxiu
hhhhyiuxiu
采纳率63.6%
2019-06-28 21:32

一个C++链表问题........

40
已采纳

写完了一半我觉得C++真是一门神秘的语言,我小看它了。。。求大佬帮助,写了个鬼的链表它死活就是连不上,代码如下。ps:大家可以不用看其他的函数,重点看看链接链表的函数,它只要能连上其他的不是什么问题。。

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h> 
#pragma warning(disable:4996)
using namespace std;
class Class
{
private:
    std::string c_id;
    std::string c_name;
    std::string nature;
    std::string total_time;
    std::string xuefen;
    std::string nos;  //选修该课程学生的人数
    char **S;
public:
    Class();
    Class(std::string, std::string, std::string ,std::string, std::string, std::string);
    friend class Student;
    friend class Manage;
    Class *me;
    Class *next;
    void set(std::string, std::string, std::string, std::string, std::string, std::string);
    void add_c(Class &,Class &);
    void search1(std::string);
    void display(Class);
    void display_ALL();
    void edit(std::string);
    void del(std::string);
    void count();
    void count_s();
    void save();
    void load();
};

class Manage
{
public:
    Student *s1;
    Class *c1;
    Manage();
    void choose_c();
};

class Student
{
private:
    std::string s_id;
    std::string name;
    char sex;
    int age;
    std::string xi;
    std::string class1;
    std::string tel;
    char **C;
public:
    Manage m;
    Class *p;
    Student *me;
    Student *next;
    Student();
    Student(std::string, std::string, char, int, std::string, std::string, std::string, char *);
    friend class Manage;
    void add_s(Student);
    void search(std::string);
    void display();
    void display_ALL();
    void edit(std::string);
    void del(std::string);
    void count();
    void save();
    void load();
};


//class Manage
//{
//public:
//  Student *s1;
//  Class *c1;
//  Manage();
//  void choose_c();
//};


Class::Class()
{
    int i;
    this->me =this;
    this->next = NULL;
    this->S = new char*[5];
    for (i = 0; i < 5; i++)
    {
        *(S + i) = new char[50];
        *(S + i) = NULL;
    }
}

Class::Class(std::string s, std::string s1,std::string c, std::string a, std::string b, std::string c1)
{
    int i = 0;
    this->c_id = s;
    this->c_name = s1;
    this->nature = c;
    this->total_time = a;
    this->xuefen = b;
    this->nos = c1;
    this->S = new char*[5];
    for (i = 0; i < 5; i++)
    {
        *(S + i) = new char[50];
        *(S + i) = NULL;
    }
    me = this;
    this->next = NULL;
}

void Class::set(std::string s, std::string s1, std::string s2, std::string s3, std::string s4, std::string s5)
{
    this->c_id = s;
    this->c_name = s1;
    this->nature = s2;
    this->total_time = s3;
    this->xuefen = s4;
    this->nos = s5;
}

void Class::add_c(Class &c,Class &c1)
{
    c.next = this->next;
    this->next = c.me;
}

void Class::search1(std::string s)
{
    int i = 0;
    Class *p;
    p = this->me;
    while (p != NULL)
    {
        if (p->c_name == s)
        {
            std::cout << p->c_id << endl;
            std::cout << p->c_name << endl;
            std::cout << p->nature << endl;
            cout << p->total_time << endl;
            cout << p->xuefen << endl;
            cout << p->nos << endl;
            if (p->S == NULL)
                cout << "还未选课,无选课信息." << endl;
            else
            {
                for (i = 0; i < 5; i++)
                    cout << p->S[i] << endl;
            }
            break;
        }
        else
            p = p->next;
    }
    if (p == NULL)
        cout << "未找到指定信息" << endl;
}


void Class::display(Class c)
{
    int i = 0;
    cout << c.c_id << endl;
    cout << c.c_name << endl;
    cout << c.nature << endl;
    cout << c.total_time << endl;
    cout << c.xuefen << endl;
    cout << c.nos << endl;
    if (c.S == NULL)
        cout << "还未选课,无选课信息." << endl;
    else
    {
        for (i = 0; i < 5; i++)
            cout << c.S[i] << endl;
    }
}


void Class::display_ALL()
{
    int i = 0;
    Class *p;
    p = this;
    if (p == NULL)
        cout << "表中无信息" << endl;
    while (p != NULL)
    {
        cout << p->c_id << endl;
        cout << p->c_name << endl;
        cout << p->nature << endl;
        cout << p->total_time << endl;
        cout << p->xuefen << endl;
        cout << p->nos << endl;
        if (p->S == NULL)
            cout << "还未选课,无选课信息." << endl;
        else
        {
            for (i = 0; i < 5; i++)
                cout << p->S[i] << endl;
        }
        i = 0;
        p = p->next;
    }
}

void Class::edit(std::string s)
{
    Class *p;
    p = this;
    while (p != NULL)
    {
        if (p->c_name == s)
        {
            cout << "请输入想要修改的学号:" << endl;
            cin >> p->c_id;
        }
        else
            p = p->next;
    }
    if (p == NULL)
        cout << "未找到修改位置." << endl;
}

void Class::del(std::string s)
{
    Class *p,*p1;
    p = this;
    while (p != NULL)
    {
        if (p->next->c_name == s)
        {
            p1 = p->next;
            p->next = p->next->next;
            delete p1;
            break;
        }
        else
            p = p->next;
    }
    if (p == NULL)
        cout << "未找到想删除的元素." << endl;
}

void Class::count()
{
    int count = 0;
    Class *p;
    p = this;
    while (p != NULL)
        count++;
    cout << "一共" << count << "门课程" << endl;
}


void Class::save()
{
    //char *p1;
    Class *p;
    int i;
    p = this;
    ofstream ofile("E:\\Student_data.txt",ios::out);
    if (ofile.good())
        cout << "文件已被成功打开!" << endl;
    else
        exit(0);
    //p1 = p->S[0];
    i = 0;
    while (p != NULL)
    {
        ofile << p->c_id;
        ofile << "\n";
        ofile << p->c_name;
        ofile << "\n";
        ofile << p->nature;
        ofile << "\n";
        ofile << p->total_time;
        ofile << "\n";
        ofile << p->xuefen;
        ofile << "\n";
        while (1)
        {
            if (i == 5)
                break;
            if (p->S[i] != NULL)
            {
                ofile << p->S[i];
                ofile << "\n";
                i++;
            }
            else
            {
                ofile << "\n";
                i++;
            }
        }
        p = p->next;
    }
}

void Class::load()
{
    string ch;
    int i ;
    char *temp;
    temp = new char[20];
    Class *p;
    p = this;
    ifstream ifile("E:\\Student_data.txt", ios::out);
    getline(ifile, ch);
    p->c_id = ch;
    getline(ifile, ch);
    p->c_name = ch;
    getline(ifile, ch);
    p->nature = ch;
    getline(ifile, ch);
    p->total_time = ch;
    getline(ifile, ch);
    p->xuefen = ch;
    getline(ifile, ch);
    p->nos = ch;
    int j;
    for (i = 0; i < 5; i++)
    {
        getline(ifile, ch);
        if (ch.size() < 2)
        {
            for (j = 0; j < ch.size(); j++)
                temp[j] = ch[j];
            temp[j] = '\0';
            strcpy(p->S[i], temp);
        }
        else
        {
            p->S[i] = NULL;
        }
    }
    while (1)
    {
        p = new Class;
        getline(ifile,ch);
        if (ch.size()<2)
            break;
        p->c_id = ch;
        getline(ifile, ch);
        p->c_name = ch;
        getline(ifile, ch);
        p->nature = ch;
        getline(ifile, ch);
        p->total_time = ch;
        getline(ifile, ch);
        p->xuefen = ch;
        getline(ifile, ch);
        p->nos = ch;
        for (i = 0; i < 5; i++)
        {
            getline(ifile, ch);
            if (ch.size() < 2)
            {
                for (j = 0; j < ch.size(); j++)
                    temp[j] = ch[j];
                temp[j] = '\0';
                strcpy(p->S[i], temp);
            }
            else
            {
                p->S[i] = NULL;
            }
        }
        p->next = this->next;
        this->next = p;
    }
}

void main()
{
    string s, s1, s2, s3, s4, s5;
    Class C,*p;
    int i,n;
    cout << "请输入数目:";
    cin >> n;
    cout << "请输入一系列:";
    cin >> s>>s1>>s2>>s3>>s4>>s5;
    C.set(s,s1,s2,s3,s4,s5);
    for (i = 0; i < n - 1; i++)
    {
        cout << "请输入一系列:";
        cin >> s >> s1 >> s2 >> s3 >> s4 >> s5;
        Class C1(s,s1,s2,s3,s4,s5);
        C.add_c(C,C1);
    }
    cout << "请输入搜索的名字:";
    cin >> s;
    C.search1(s);
    cout << "显示第一项:";
    C.display(C);
    cout << "显示所有项";
    C.display_ALL();
    cout << "输入待编辑的人员姓名";
    cin >> s;
    C.edit(s);
    C.display_ALL();
    cout << "请输入要删除的项:";
    cin >> s;
    C.del(s);
    C.display_ALL();
    cout << "显示课程数目:";
    C.count();
    cout << "开始保存";
    C.save();
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 2年前

    说清楚程序要干什么
    你的输入是什么
    预期的输出是什么
    E:\Student_data.txt文件的内容是什么

    // Q767593.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h> 
    #pragma warning(disable:4996)
    using namespace std;
    class Class
    {
    private:
        std::string c_id;
        std::string c_name;
        std::string nature;
        std::string total_time;
        std::string xuefen;
        std::string nos;  //选修该课程学生的人数
        char **S;
    public:
        Class();
        Class(std::string, std::string, std::string ,std::string, std::string, std::string);
        friend class Student;
        friend class Manage;
        Class *me;
        Class *next;
        void set(std::string, std::string, std::string, std::string, std::string, std::string);
        void add_c(Class *c,Class *c1);
        void search1(std::string);
        void display(Class);
        void display_ALL();
        void edit(std::string);
        void del(std::string);
        void count();
        void count_s();
        void save();
        void load();
    };
    
    class Manage
    {
    public:
        Student *s1;
        Class *c1;
        Manage();
        void choose_c();
    };
    
    class Student
    {
    private:
        std::string s_id;
        std::string name;
        char sex;
        int age;
        std::string xi;
        std::string class1;
        std::string tel;
        char **C;
    public:
        Manage m;
        Class *p;
        Student *me;
        Student *next;
        Student();
        Student(std::string, std::string, char, int, std::string, std::string, std::string, char *);
        friend class Manage;
        void add_c(Class *c,Class *c1);
        void search(std::string);
        void display();
        void display_ALL();
        void edit(std::string);
        void del(std::string);
        void count();
        void save();
        void load();
    };
    
    
    //class Manage
    //{
    //public:
    //  Student *s1;
    //  Class *c1;
    //  Manage();
    //  void choose_c();
    //};
    
    
    Class::Class()
    {
        int i;
        this->me =this;
        this->next = NULL;
        this->S = new char*[5];
        for (i = 0; i < 5; i++)
        {
            *(S + i) = new char[50];
            *(S + i) = NULL;
        }
    }
    
    Class::Class(std::string s, std::string s1,std::string c, std::string a, std::string b, std::string c1)
    {
        int i = 0;
        this->c_id = s;
        this->c_name = s1;
        this->nature = c;
        this->total_time = a;
        this->xuefen = b;
        this->nos = c1;
        this->S = new char*[5];
        for (i = 0; i < 5; i++)
        {
            *(S + i) = new char[50];
            *(S + i) = NULL;
        }
        me = this;
        this->next = NULL;
    }
    
    void Class::set(std::string s, std::string s1, std::string s2, std::string s3, std::string s4, std::string s5)
    {
        this->c_id = s;
        this->c_name = s1;
        this->nature = s2;
        this->total_time = s3;
        this->xuefen = s4;
        this->nos = s5;
    }
    
    void Class::add_c(Class *c,Class *c1)
    {
        c1->next = this->next;
        this->next = c1;
    }
    
    void Class::search1(std::string s)
    {
        int i = 0;
        Class *p;
        p = this->me;
        while (p != NULL)
        {
            if (p->c_name == s)
            {
                std::cout << p->c_id << endl;
                std::cout << p->c_name << endl;
                std::cout << p->nature << endl;
                cout << p->total_time << endl;
                cout << p->xuefen << endl;
                cout << p->nos << endl;
                if (p->S == NULL)
                    cout << "还未选课,无选课信息." << endl;
                else
                {
                    for (i = 0; i < 5; i++)
                        cout << p->S[i] << endl;
                }
                break;
            }
            else
                p = p->next;
        }
        if (p == NULL)
            cout << "未找到指定信息" << endl;
    }
    
    
    void Class::display(Class c)
    {
        int i = 0;
        cout << c.c_id << endl;
        cout << c.c_name << endl;
        cout << c.nature << endl;
        cout << c.total_time << endl;
        cout << c.xuefen << endl;
        cout << c.nos << endl;
        if (c.S == NULL)
            cout << "还未选课,无选课信息." << endl;
        else
        {
            for (i = 0; i < 5; i++)
                cout << c.S[i] << endl;
        }
    }
    
    
    void Class::display_ALL()
    {
        int i = 0;
        Class *p;
        p = this;
        if (p == NULL)
            cout << "表中无信息" << endl;
        while (p != NULL)
        {
            cout << p->c_id << endl;
            cout << p->c_name << endl;
            cout << p->nature << endl;
            cout << p->total_time << endl;
            cout << p->xuefen << endl;
            cout << p->nos << endl;
            if (p->S == NULL)
                cout << "还未选课,无选课信息." << endl;
            else
            {
                for (i = 0; i < 5; i++)
                    cout << p->S[i] << endl;
            }
            i = 0;
            p = p->next;
        }
    }
    
    void Class::edit(std::string s)
    {
        Class *p;
        p = this;
        while (p != NULL)
        {
            if (p->c_name == s)
            {
                cout << "请输入想要修改的学号:" << endl;
                cin >> p->c_id;
            }
            else
                p = p->next;
        }
        if (p == NULL)
            cout << "未找到修改位置." << endl;
    }
    
    void Class::del(std::string s)
    {
        Class *p,*p1;
        p = this;
        while (p != NULL)
        {
            if (p->next->c_name == s)
            {
                p1 = p->next;
                p->next = p->next->next;
                delete p1;
                break;
            }
            else
                p = p->next;
        }
        if (p == NULL)
            cout << "未找到想删除的元素." << endl;
    }
    
    void Class::count()
    {
        int count = 0;
        Class *p;
        p = this;
        while (p != NULL)
            count++;
        cout << "一共" << count << "门课程" << endl;
    }
    
    
    void Class::save()
    {
        //char *p1;
        Class *p;
        int i;
        p = this;
        ofstream ofile("E:\\Student_data.txt",ios::out);
        if (ofile.good())
            cout << "文件已被成功打开!" << endl;
        else
            exit(0);
        //p1 = p->S[0];
        i = 0;
        while (p != NULL)
        {
            ofile << p->c_id;
            ofile << "\n";
            ofile << p->c_name;
            ofile << "\n";
            ofile << p->nature;
            ofile << "\n";
            ofile << p->total_time;
            ofile << "\n";
            ofile << p->xuefen;
            ofile << "\n";
            while (1)
            {
                if (i == 5)
                    break;
                if (p->S[i] != NULL)
                {
                    ofile << p->S[i];
                    ofile << "\n";
                    i++;
                }
                else
                {
                    ofile << "\n";
                    i++;
                }
            }
            p = p->next;
        }
    }
    
    void Class::load()
    {
        string ch;
        int i ;
        char *temp;
        temp = new char[20];
        Class *p;
        p = this;
        ifstream ifile("E:\\Student_data.txt", ios::out);
        getline(ifile, ch);
        p->c_id = ch;
        getline(ifile, ch);
        p->c_name = ch;
        getline(ifile, ch);
        p->nature = ch;
        getline(ifile, ch);
        p->total_time = ch;
        getline(ifile, ch);
        p->xuefen = ch;
        getline(ifile, ch);
        p->nos = ch;
        int j;
        for (i = 0; i < 5; i++)
        {
            getline(ifile, ch);
            if (ch.size() < 2)
            {
                for (j = 0; j < ch.size(); j++)
                    temp[j] = ch[j];
                temp[j] = '\0';
                strcpy(p->S[i], temp);
            }
            else
            {
                p->S[i] = NULL;
            }
        }
        while (1)
        {
            p = new Class;
            getline(ifile,ch);
            if (ch.size()<2)
                break;
            p->c_id = ch;
            getline(ifile, ch);
            p->c_name = ch;
            getline(ifile, ch);
            p->nature = ch;
            getline(ifile, ch);
            p->total_time = ch;
            getline(ifile, ch);
            p->xuefen = ch;
            getline(ifile, ch);
            p->nos = ch;
            for (i = 0; i < 5; i++)
            {
                getline(ifile, ch);
                if (ch.size() < 2)
                {
                    for (j = 0; j < ch.size(); j++)
                        temp[j] = ch[j];
                    temp[j] = '\0';
                    strcpy(p->S[i], temp);
                }
                else
                {
                    p->S[i] = NULL;
                }
            }
            p->next = this->next;
            this->next = p;
        }
    }
    
    void main()
    {
        string s, s1, s2, s3, s4, s5;
        Class C,*p;
        int i,n;
        cout << "请输入数目:";
        cin >> n;
        cout << "请输入一系列:";
        cin >> s>>s1>>s2>>s3>>s4>>s5;
        C.set(s,s1,s2,s3,s4,s5);
        for (i = 0; i < n - 1; i++)
        {
            cout << "请输入一系列:";
            cin >> s >> s1 >> s2 >> s3 >> s4 >> s5;
            Class *pC1 = new Class(s,s1,s2,s3,s4,s5);
            C.add_c(&C,pC1);
        }
        cout << "请输入搜索的名字:";
        cin >> s;
        C.search1(s);
        cout << "显示第一项:";
        C.display(C);
        cout << "显示所有项";
        C.display_ALL();
        cout << "输入待编辑的人员姓名";
        cin >> s;
        C.edit(s);
        C.display_ALL();
        cout << "请输入要删除的项:";
        cin >> s;
        C.del(s);
        C.display_ALL();
        cout << "显示课程数目:";
        C.count();
        cout << "开始保存";
        C.save();
    }
    
    

    图片说明

    你程序不合理的地方很多
    比如说你既然是成员函数了,那么this就是相当于C了
    为什么要c1 c2两个参数,完全是矛盾的。

    // Q767593.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    // Q767593.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h> 
    #pragma warning(disable:4996)
    using namespace std;
    class Class
    {
    private:
        std::string c_id;
        std::string c_name;
        std::string nature;
        std::string total_time;
        std::string xuefen;
        std::string nos;  //选修该课程学生的人数
        char **S;
    public:
        Class();
        Class(std::string, std::string, std::string ,std::string, std::string, std::string);
        friend class Student;
        friend class Manage;
        Class *me;
        Class *next;
        void set(std::string, std::string, std::string, std::string, std::string, std::string);
        void add_c(Class *c,Class *c1);
        void search1(std::string);
        void display(Class);
        void display_ALL();
        void edit(std::string);
        void del(std::string);
        void count();
        void count_s();
        void save();
        void load();
    };
    
    class Manage
    {
    public:
        Student *s1;
        Class *c1;
        Manage();
        void choose_c();
    };
    
    class Student
    {
    private:
        std::string s_id;
        std::string name;
        char sex;
        int age;
        std::string xi;
        std::string class1;
        std::string tel;
        char **C;
    public:
        Manage m;
        Class *p;
        Student *me;
        Student *next;
        Student();
        Student(std::string, std::string, char, int, std::string, std::string, std::string, char *);
        friend class Manage;
        void add_c(Class *c,Class *c1);
        void search(std::string);
        void display();
        void display_ALL();
        void edit(std::string);
        void del(std::string);
        void count();
        void save();
        void load();
    };
    
    
    //class Manage
    //{
    //public:
    //  Student *s1;
    //  Class *c1;
    //  Manage();
    //  void choose_c();
    //};
    
    
    Class::Class()
    {
        int i;
        this->me =this;
        this->next = NULL;
        this->S = new char*[5];
        for (i = 0; i < 5; i++)
        {
            //*(S + i) = new char[50]; //这一行多此一举
            *(S + i) = NULL;
        }
    }
    
    Class::Class(std::string s, std::string s1,std::string c, std::string a, std::string b, std::string c1)
    {
        int i = 0;
        this->c_id = s;
        this->c_name = s1;
        this->nature = c;
        this->total_time = a;
        this->xuefen = b;
        this->nos = c1;
        this->S = new char*[5];
        for (i = 0; i < 5; i++)
        {
            //*(S + i) = new char[50];
            *(S + i) = NULL;
        }
        me = this;
        this->next = NULL;
    }
    
    void Class::set(std::string s, std::string s1, std::string s2, std::string s3, std::string s4, std::string s5)
    {
        this->c_id = s;
        this->c_name = s1;
        this->nature = s2;
        this->total_time = s3;
        this->xuefen = s4;
        this->nos = s5;
    }
    
    void Class::add_c(Class *c,Class *c1)
    {
        c1->next = this->next;
        this->next = c1;
    }
    
    void Class::search1(std::string s)
    {
        int i = 0;
        Class *p;
        p = this->me;
        while (p != NULL)
        {
            if (p->c_name == s)
            {
                std::cout << p->c_id << endl;
                std::cout << p->c_name << endl;
                std::cout << p->nature << endl;
                cout << p->total_time << endl;
                cout << p->xuefen << endl;
                cout << p->nos << endl;
                if (p->S == NULL)
                    cout << "还未选课,无选课信息." << endl;
                else
                {
                    for (i = 0; i < 5; i++)
                        if (p->S[i] != NULL)
                            cout << p->S[i] << endl;
                }
                break;
            }
            else
                p = p->next;
        }
        if (p == NULL)
            cout << "未找到指定信息" << endl;
    }
    
    
    void Class::display(Class c)
    {
        int i = 0;
        cout << c.c_id << endl;
        cout << c.c_name << endl;
        cout << c.nature << endl;
        cout << c.total_time << endl;
        cout << c.xuefen << endl;
        cout << c.nos << endl;
        if (c.S == NULL)
            cout << "还未选课,无选课信息." << endl;
        else
        {
            for (i = 0; i < 5; i++)
                if (c.S[i] != NULL) //这里加上判断,否则遇到null就出错
                    cout << c.S[i] << endl;
        }
    }
    
    
    void Class::display_ALL()
    {
        int i = 0;
        Class *p;
        p = this;
        if (p == NULL)
            cout << "表中无信息" << endl;
        while (p != NULL)
        {
            cout << p->c_id << endl;
            cout << p->c_name << endl;
            cout << p->nature << endl;
            cout << p->total_time << endl;
            cout << p->xuefen << endl;
            cout << p->nos << endl;
            if (p->S == NULL)
                cout << "还未选课,无选课信息." << endl;
            else
            {
                for (i = 0; i < 5; i++)
                    if (p->S[i] != NULL)
                        cout << p->S[i] << endl;
            }
            i = 0;
            p = p->next;
        }
    }
    
    void Class::edit(std::string s)
    {
        Class *p;
        p = this;
        while (p != NULL)
        {
            if (p->c_name == s)
            {
                cout << "请输入想要修改的学号:" << endl;
                cin >> p->c_id;
            }
            else
                p = p->next;
        }
        if (p == NULL)
            cout << "未找到修改位置." << endl;
    }
    
    void Class::del(std::string s)
    {
        Class *p,*p1;
        p = this;
        while (p != NULL)
        {
            if (p->next->c_name == s)
            {
                p1 = p->next;
                p->next = p->next->next;
                delete p1;
                break;
            }
            else
                p = p->next;
        }
        if (p == NULL)
            cout << "未找到想删除的元素." << endl;
    }
    
    void Class::count()
    {
        int count = 0;
        Class *p;
        p = this;
        while (p != NULL)
            count++;
        cout << "一共" << count << "门课程" << endl;
    }
    
    
    void Class::save()
    {
        //char *p1;
        Class *p;
        int i;
        p = this;
        ofstream ofile("E:\\Student_data.txt",ios::out);
        if (ofile.good())
            cout << "文件已被成功打开!" << endl;
        else
            exit(0);
        //p1 = p->S[0];
        i = 0;
        while (p != NULL)
        {
            ofile << p->c_id;
            ofile << "\n";
            ofile << p->c_name;
            ofile << "\n";
            ofile << p->nature;
            ofile << "\n";
            ofile << p->total_time;
            ofile << "\n";
            ofile << p->xuefen;
            ofile << "\n";
            while (1)
            {
                if (i == 5)
                    break;
                if (p->S[i] != NULL)
                {
                    ofile << p->S[i];
                    ofile << "\n";
                    i++;
                }
                else
                {
                    ofile << "\n";
                    i++;
                }
            }
            p = p->next;
        }
    }
    
    void Class::load()
    {
        string ch;
        int i ;
        char *temp;
        temp = new char[20];
        Class *p;
        p = this;
        ifstream ifile("E:\\Student_data.txt", ios::out);
        getline(ifile, ch);
        p->c_id = ch;
        getline(ifile, ch);
        p->c_name = ch;
        getline(ifile, ch);
        p->nature = ch;
        getline(ifile, ch);
        p->total_time = ch;
        getline(ifile, ch);
        p->xuefen = ch;
        getline(ifile, ch);
        p->nos = ch;
        int j;
        for (i = 0; i < 5; i++)
        {
            getline(ifile, ch);
            if (ch.size() < 2)
            {
                for (j = 0; j < ch.size(); j++)
                    temp[j] = ch[j];
                temp[j] = '\0';
                strcpy(p->S[i], temp);
            }
            else
            {
                p->S[i] = NULL;
            }
        }
        while (1)
        {
            p = new Class;
            getline(ifile,ch);
            if (ch.size()<2)
                break;
            p->c_id = ch;
            getline(ifile, ch);
            p->c_name = ch;
            getline(ifile, ch);
            p->nature = ch;
            getline(ifile, ch);
            p->total_time = ch;
            getline(ifile, ch);
            p->xuefen = ch;
            getline(ifile, ch);
            p->nos = ch;
            for (i = 0; i < 5; i++)
            {
                getline(ifile, ch);
                if (ch.size() < 2)
                {
                    for (j = 0; j < ch.size(); j++)
                        temp[j] = ch[j];
                    temp[j] = '\0';
                    strcpy(p->S[i], temp);
                }
                else
                {
                    p->S[i] = NULL;
                }
            }
            p->next = this->next;
            this->next = p;
        }
    }
    
    void main()
    {
        string s, s1, s2, s3, s4, s5;
        Class C,*p;
        int i,n;
        cout << "请输入数目:";
        cin >> n;
        cout << "请输入一系列:";
        cin >> s>>s1>>s2>>s3>>s4>>s5;
        C.set(s,s1,s2,s3,s4,s5);
        for (i = 0; i < n - 1; i++)
        {
            cout << "请输入一系列:";
            cin >> s >> s1 >> s2 >> s3 >> s4 >> s5;
            Class *pC1 = new Class(s,s1,s2,s3,s4,s5);
            C.add_c(&C,pC1);
        }
        cout << "请输入搜索的名字:";
        cin >> s;
        C.search1(s);
        cout << "显示第一项:";
        C.display(C);
        cout << "显示所有项";
        C.display_ALL();
        cout << "输入待编辑的人员姓名";
        cin >> s;
        C.edit(s);
        C.display_ALL();
        cout << "请输入要删除的项:";
        cin >> s;
        C.del(s);
        C.display_ALL();
        cout << "显示课程数目:";
        C.count();
        cout << "开始保存";
        C.save();
    }
    

    图片说明

    如果问题最终解决,请点下采纳,谢谢

    点赞 2 评论 复制链接分享
  • hhhhyiuxiu hhhhyiuxiu 2年前
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h> 
    #pragma warning(disable:4996)
    using namespace std;
    class Class
    {
    private:
        std::string c_id;
        std::string c_name;
        std::string nature;
        std::string total_time;
        std::string xuefen;
        std::string nos;  //选修该课程学生的人数
        char **S;
    public:
        Class();
        Class(std::string, std::string, std::string, std::string, std::string, std::string);
        friend class Student;
        friend class Manage;
        Class *me;
        Class *next;
        void set(std::string, std::string, std::string, std::string, std::string, std::string);
        void add_c(Class *c, Class *c1);
        void search1(std::string);
        void display(Class);
        void display_ALL();
        void edit(std::string);
        void del(std::string);
        void count();
        void count_s();
        void save();
        void load();
    };
    
    class Manage
    {
    public:
        Student *s1;
        Class *c1;
        Manage();
        void choose_c();
    };
    
    class Student
    {
    private:
        std::string s_id;
        std::string name;
        char sex;
        int age;
        std::string xi;
        std::string class1;
        std::string tel;
        char **C;
    public:
        Manage m;
        Class *p;
        Student *me;
        Student *next;
        Student();
        Student(std::string, std::string, char, int, std::string, std::string, std::string, char *);
        friend class Manage;
        void add_c(Class *c, Class *c1);
        void search(std::string);
        void display();
        void display_ALL();
        void edit(std::string);
        void del(std::string);
        void count();
        void save();
        void load();
    };
    
    
    //class Manage
    //{
    //public:
    //  Student *s1;
    //  Class *c1;
    //  Manage();
    //  void choose_c();
    //};
    
    
    Class::Class()
    {
        int i;
        this->me = this;
        this->next = NULL;
        this->S = new char*[5];
        for (i = 0; i < 5; i++)
        {
            *(S + i) = new char[50];
            *(S + i) = NULL;
        }
    }
    
    Class::Class(std::string s, std::string s1, std::string c, std::string a, std::string b, std::string c1)
    {
        int i = 0;
        this->c_id = s;
        this->c_name = s1;
        this->nature = c;
        this->total_time = a;
        this->xuefen = b;
        this->nos = c1;
        this->S = new char*[5];
        for (i = 0; i < 5; i++)
        {
            *(S + i) = new char[50];
            *(S + i) = NULL;
        }
        me = this;
        this->next = NULL;
    }
    
    void Class::set(std::string s, std::string s1, std::string s2, std::string s3, std::string s4, std::string s5)
    {
        this->c_id = s;
        this->c_name = s1;
        this->nature = s2;
        this->total_time = s3;
        this->xuefen = s4;
        this->nos = s5;
    }
    
    void Class::add_c(Class *c, Class *c1)
    {
        c1->next = this->next;
        this->next = c1;
    }
    
    void Class::search1(std::string s)
    {
        int i = 0;
        Class *p;
        p = this->me;
        while (p != NULL)
        {
            if (p->c_name == s)
            {
                std::cout << p->c_id << endl;
                std::cout << p->c_name << endl;
                std::cout << p->nature << endl;
                cout << p->total_time << endl;
                cout << p->xuefen << endl;
                cout << p->nos << endl;
                if (p->S == NULL)
                    cout << "还未选课,无选课信息." << endl;
                else
                {
                    for (i = 0; i < 5; i++)
                        cout << p->S[i] << endl;
                }
                break;
            }
            else
                p = p->next;
        }
        if (p == NULL)
            cout << "未找到指定信息" << endl;
    }
    
    
    void Class::display(Class c)
    {
        int i = 0;
        cout << c.c_id << endl;
        cout << c.c_name << endl;
        cout << c.nature << endl;
        cout << c.total_time << endl;
        cout << c.xuefen << endl;
        cout << c.nos << endl;
        if (c.S == NULL)
            cout << "还未选课,无选课信息." << endl;
        else
        {
            for (i = 0; i < 5; i++)
                cout << c.S[i] << endl;
        }
    }
    
    
    void Class::display_ALL()
    {
        int i = 0;
        Class *p;
        p = this;
        if (p == NULL)
            cout << "表中无信息" << endl;
        while (p != NULL)
        {
            cout << p->c_id << endl;
            cout << p->c_name << endl;
            cout << p->nature << endl;
            cout << p->total_time << endl;
            cout << p->xuefen << endl;
            cout << p->nos << endl;
            if (p->S == NULL)
                cout << "还未选课,无选课信息." << endl;
            else
            {
                for (i = 0; i < 5; i++)
                    cout << p->S[i] << endl;
            }
            i = 0;
            p = p->next;
        }
    }
    
    void Class::edit(std::string s)
    {
        Class *p;
        p = this;
        while (p != NULL)
        {
            if (p->c_name == s)
            {
                cout << "请输入想要修改的学号:" << endl;
                cin >> p->c_id;
            }
            else
                p = p->next;
        }
        if (p == NULL)
            cout << "未找到修改位置." << endl;
    }
    
    void Class::del(std::string s)
    {
        Class *p, *p1;
        p = this;
        while (p != NULL)
        {
            if (p->next->c_name == s)
            {
                p1 = p->next;
                p->next = p->next->next;
                delete p1;
                break;
            }
            else
                p = p->next;
        }
        if (p == NULL)
            cout << "未找到想删除的元素." << endl;
    }
    
    void Class::count()
    {
        int count = 0;
        Class *p;
        p = this;
        while (p != NULL)
            count++;
        cout << "一共" << count << "门课程" << endl;
    }
    
    
    void Class::save()
    {
        //char *p1;
        Class *p;
        int i;
        p = this;
        ofstream ofile("E:\\Student_data.txt", ios::out);
        if (ofile.good())
            cout << "文件已被成功打开!" << endl;
        else
            exit(0);
        //p1 = p->S[0];
        i = 0;
        while (p != NULL)
        {
            ofile << p->c_id;
            ofile << "\n";
            ofile << p->c_name;
            ofile << "\n";
            ofile << p->nature;
            ofile << "\n";
            ofile << p->total_time;
            ofile << "\n";
            ofile << p->xuefen;
            ofile << "\n";
            while (1)
            {
                if (i == 5)
                    break;
                if (p->S[i] != NULL)
                {
                    ofile << p->S[i];
                    ofile << "\n";
                    i++;
                }
                else
                {
                    ofile << "\n";
                    i++;
                }
            }
            p = p->next;
        }
    }
    
    void Class::load()
    {
        string ch;
        int i;
        char *temp;
        temp = new char[20];
        Class *p;
        p = this;
        ifstream ifile("E:\\Student_data.txt", ios::out);
        getline(ifile, ch);
        p->c_id = ch;
        getline(ifile, ch);
        p->c_name = ch;
        getline(ifile, ch);
        p->nature = ch;
        getline(ifile, ch);
        p->total_time = ch;
        getline(ifile, ch);
        p->xuefen = ch;
        getline(ifile, ch);
        p->nos = ch;
        int j;
        for (i = 0; i < 5; i++)
        {
            getline(ifile, ch);
            if (ch.size() < 2)
            {
                for (j = 0; j < ch.size(); j++)
                    temp[j] = ch[j];
                temp[j] = '\0';
                strcpy(p->S[i], temp);
            }
            else
            {
                p->S[i] = NULL;
            }
        }
        while (1)
        {
            p = new Class;
            getline(ifile, ch);
            if (ch.size()<2)
                break;
            p->c_id = ch;
            getline(ifile, ch);
            p->c_name = ch;
            getline(ifile, ch);
            p->nature = ch;
            getline(ifile, ch);
            p->total_time = ch;
            getline(ifile, ch);
            p->xuefen = ch;
            getline(ifile, ch);
            p->nos = ch;
            for (i = 0; i < 5; i++)
            {
                getline(ifile, ch);
                if (ch.size() < 2)
                {
                    for (j = 0; j < ch.size(); j++)
                        temp[j] = ch[j];
                    temp[j] = '\0';
                    strcpy(p->S[i], temp);
                }
                else
                {
                    p->S[i] = NULL;
                }
            }
            p->next = this->next;
            this->next = p;
        }
    }
    
    void main()
    {
        string s, s1, s2, s3, s4, s5;
        Class C, *p;
        int i, n;
        cout << "请输入数目:";
        cin >> n;
        cout << "请输入一系列:";
        cin >> s >> s1 >> s2 >> s3 >> s4 >> s5;
        C.set(s, s1, s2, s3, s4, s5);
        for (i = 0; i < n - 1; i++)
        {
            cout << "请输入一系列:";
            cin >> s >> s1 >> s2 >> s3 >> s4 >> s5;
            Class *pC1 = new Class(s, s1, s2, s3, s4, s5);
            C.add_c(&C, pC1);
        }
        /*cout << "请输入搜索的名字:";
        cin >> s;
        C.search1(s);
        cout << "显示第一项:";
        C.display(C);*/
        cout << "显示所有项";
        C.display_ALL();
        /*cout << "输入待编辑的人员姓名";
        cin >> s;
        C.edit(s);
        C.display_ALL();
        cout << "请输入要删除的项:";
        cin >> s;
        C.del(s);
        C.display_ALL();
        cout << "显示课程数目:";
        C.count();
        cout << "开始保存";
        C.save();*/
    }
    

    我原样复制后注释了main函数 中的某些功能,我没有复制 #include "stdafx.h",因为我 建立的是一个空项目,包含该头文件会报错,下面是 输入和输出,我 说一下格式,第一行的输入表示插入链表的节点个数,第二行为第一个待插入节点的赋值,一共有6项需要赋值,我每一个都赋值为1,
    第三行为第二个待插入的节点,每一个赋值为2,此时输入完成,之后是输出链表中所有项。但是程序只输出了第一个节点的值,之后没有输出。
    图片如下:
    图片说明

    点赞 评论 复制链接分享