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