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

写完了一半我觉得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();
}

c++
qhjt888
qhjt888 查一下定义。
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 回复明黔(惟愿生活馆): 你帮我解决这个问题,C币就是你的,毕竟我的C币也不是天上掉下来的。
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu ps
11 个月之前 回复

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();
}

图片说明

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

hhhhyiuxiu
hhhhyiuxiu 回复贵阳挖掘机马善福,自备车辆专业挖游泳池: 哈哈,确实我也有问题,我应该把详细的注释附上再提问的。总而言之谢谢你的帮助!
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复hhhhyiuxiu: 我也是因为之前回答过你一个汇编的问题,要是不认识你,看你这么多代码一贴,要以前我也许会回答下,但是现在可能就未必了。
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 csdn归根结底他不是一个熟人的社区,而是如同菜市场一样,来也匆匆去也匆匆,人们彼此不了解也不信任,所以戾气比较重。
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复hhhhyiuxiu: 你也别那么说,你不了解他,他也不了解你。你要是经常来,你就知道,他其实也回答了很多问题,只是这个问题没回答你罢了。他要是看了你的blog,也就知道你也不是个伸手要答案的
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 回复茂大叔独立游戏开发: 好像谁不是从新手过来一样,你不愿意帮助直接闭上嘴关闭页面,瞎jr说一些没营养的评论搞得大家都很烦,下次建议你直接离开。
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 回复贵阳挖掘机马善福,自备车辆专业挖游泳池: mmp,我链表没有任何问题,主要是输出函数有问题,我已经改了,多谢你的帮助,C币奉上。
11 个月之前 回复
foomow
茂大叔 回复贵阳挖掘机马善福,自备车辆专业挖游泳池: 兄die……你真有耐心,换成是我直接劝他挂科算了……整个一个没头绪……都搞不懂他怎么写了这么多代码出来的……
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复hhhhyiuxiu: 我调试了你的程序,你的程序之所以崩溃是因为没有一个地方对S这个数组赋值。cout << c.S[i] << endl; 这里报错(类似的地方有3个)。我修改了下,现在不报错了。看我更新回答
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 回复贵阳挖掘机马善福,自备车辆专业挖游泳池: 大佬 ,麻烦帮忙 看看
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 回复贵阳挖掘机马善福,自备车辆专业挖游泳池: 请看一下我的回答
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 回复贵阳挖掘机马善福,自备车辆专业挖游泳池: 关于你说的this是相当于C这个我明白,我开始的程序就是 按照这个思路写的,但是很坑爹的是我在调试的时候发现this指针的地址居然和参数中的对象地址相同。。。
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复hhhhyiuxiu: 我代码更新过一次,你确保你粘贴过去的是现在的代码,而不是之前的。如果还有问题,你必须给出你的输入、截图
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复hhhhyiuxiu: 你看到我的图没有,是可以插入的。至于你说的输出不了,也不能排除你输出的代码也不对。而且关键你不愿意解释你的程序怎么输入的
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 回复贵阳挖掘机马善福,自备车辆专业挖游泳池: 还是不对,我拷贝了你修改后的代码,注释了main函数中其他的功能,只留下了add_C功能和display_ALL功能,仍旧是只能输出第一个节点,无法 输出后续节点。
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复hhhhyiuxiu: 你看我更新的回答。实在不知道你的程序怎么输入的,但是就构造链表,我这么写肯定是构造出来了
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 回复贵阳挖掘机马善福,自备车辆专业挖游泳池: 但是我明明在main函数中的每一次for循环中有Class C(),这样的语句,这样不是为Class类新建一个对象吗?
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 我大概说一下我的过程,就是说我先输入的是要添加的内容数目,然后我向链表中循环添加内容,每一项为个内容。我在写了这个功能后进行执行发现明明 输入了要查询的信息,但是在后面的查询函数中 还是无法查询到,于是我向前面看,最终发现链接链表的过程中有问题。
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复hhhhyiuxiu: 你的代码根本就不对,你一共只有一个对象在堆栈上面,你反复添加的都是着一个。你必须用指针和new操作符
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 可以认为C对象是链表头,每次循环里初始化的C1对象是要添加的内容
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 初学者可能有一个误解,就是说代码哪里错,是“看”出来的。其实是调出来的。所以必须在我这里复现你的错误,才好帮你调试
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 回复贵阳挖掘机马善福,自备车辆专业挖游泳池: 在main函数中的第一个for循环 那里是向链表中添加内容的功能,可以认为C
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复hhhhyiuxiu: 你得给出你的输入
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复hhhhyiuxiu: 我运行了你的程序,不明所以,哪里有添加链表
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 所以您只需要看我的add_c函数以及main函数中循环向链表中添加值的那一段就可以了
11 个月之前 回复
hhhhyiuxiu
hhhhyiuxiu 您问的这些跟我问的问题没关系,现在的问题是,我想要循环向链表 中添加东西,但是我没有添加进去,我现在想知道的是为什么没有添加进去和如何修改。
11 个月之前 回复
#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,此时输入完成,之后是输出链表中所有项。但是程序只输出了第一个节点的值,之后没有输出。
图片如下:
图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐