如图
这里是全部代码
#include <cstdlib>
#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>
#include <fstream>
using namespace std;
#define null NULL
class student
{
private:
int flag;
friend class studentMessage;
student* next; //节点指针
string name; //学生姓名
string address; //家庭住址
string age; //出生日期
long long id; //学号
string sex;
char grade;
double chinese, math, english; //语文 , 数学 , 英语
double pe, science; //体育 科学
string major; long long int TL; // 专业 , 电话
public:
static int num_total; //总数
//学生初始化
student(long long _id, string _name, string _sex, string _age,char _science, char _pe, double _chinese, double _math, double _english)
{
name = _name;
pe = _pe;
science = _science;
age = _age;
sex = _sex;
id = _id;
chinese = _chinese;
math = _math;
english = _english;
next = NULL;
}
//构造函数
student() //为studentMessage初始化头结点用
{
name = "null";
sex = "null";
address = "null";
age = "null";
id = 0;
chinese = 0;
math = 0;
english = 0;
grade = '0';
science = 0;
pe = 0;
major = "null";
TL = 0;
next = NULL;
}
~student() {}
void swap(student*);
};
int student::num_total = 0;
void student::swap(student* q)
{
string _name, _sex, _address, _age;
long long _id;
double _chinese, _math, _english; //语文 , 数学 , 英语
double _pe, _science; //体育 科学
string _major; long long int _TL; // 专业 , 电话
_chinese = chinese;
chinese = q->chinese;
q->chinese = _chinese;
_math = math;
math = q->math;
q->math = _math;
_english = english;
english = q->english;
q->english = _english;
_pe = pe;
pe = q->pe;
q->pe = _pe;
_science = science;
science = q->science;
q->science = _science;
_major = major;
major = q->major;
q->major = _major;
_TL = TL;
TL = q->TL;
q->TL = _TL;
_name = name;
name = q->name;
q->name = _name;
_sex = sex;
sex = q->sex;
q->sex = _sex;
_address = address;
address = q->address;
q->address = _address;
_age = age;
age = q->age;
q->age = _age;
_id = id;
id = q->id;
q->id = _id;
}
class studentMessage
{
private:
student* first; //头指针
int num; //信息中的学生人数
public:
studentMessage()
{
num = 0; //初始化学生人数为0
first = new student; //初始化头结点
}
~studentMessage() { delete first; }
/*管理系统常规操作*/
void Insret(void); //插入
void Display(void); //显示
void Delete(void); //删除
void Search(void); //搜索
void Change(void); //改动
void SearchByid(void); //按照学号查找
void SearchByname(void); //按照姓名查找
int menu(void); //初始的菜单
void clear(void); //清空列表
void tongji(void); //统计学生人数
void save(void);
void duplicate(void);
void read(void);
};
int studentMessage::menu(void)
{
system("cls");
int ch;
cout << endl; cout << endl; cout << endl; cout << endl;
cout << "**********************************************************************" << endl;
cout << "======================================================================" << endl;
cout << "***************************学生信息管理系统***************************" << endl; cout << endl;
cout << endl;
cout << " 1.添加学生个人信息" << endl;
cout << " 2.查询学生个人信息" << endl;
cout << " 3.显示学生个人信息" << endl;
cout << " 4.编辑学生个人信息" << endl;
cout << " 5.删除学生个人信息" << endl;
cout << " 6.统计学生数量" << endl;
cout << " 7.保存" << endl;
cout << " 8.清除全部信息" << endl;
cout << " 9.读取已保存信息" << endl;
cout << " 0.退出系统" << endl; cout << endl;
cout << endl;
cout << "***********************************************************************" << endl;
cout << "=======================================================================" << endl;
cout << "***********************************************************************" << endl;
cout << endl; cout << endl; cout << endl; cout << endl; cout << endl; cout << endl;
cin >> ch;
cout << "\n\n\n" << endl;
return ch;
}
void studentMessage::save(void)
{
system("cls");
fstream f("student.txt", ios::out);
int i;
if (!f)
{
cout << endl; cout << endl; cout << endl;
cout << "文件保存失败!!!!按任意键返回" << endl;
if (i = _getch()) return;
}
if (student::num_total == 0)
{
cout << "当前记录中无学生" << endl;
}
else
{
student* p = first->next;
while (p != null)
{
f << "学号:" << p->id << " " << endl;
f << "姓名:" << p->name << endl;
f << "性别(boy/girl):" << p->sex << endl;
f << "出生日期:" << p->age << endl;
f << "科学成绩:" << p->science << endl;
f << "体育成绩:" << p->pe << endl;
f << "语文成绩:" << p->chinese << endl;
f << "数学成绩:" << p->math << endl;
f << "英语成绩:" << p->english << endl;
f << "------------------------------------------------" << endl;
p = p->next;
}
}
f.close();
cout << endl; cout << endl; cout << endl;
cout << "学生信息文件已创建,按任意键继续" << endl;
i = _getch();
}
void studentMessage::duplicate(void)
{
system("cls");
fstream f("duplicate.txt", ios::out);
int i;
if (!f)
{
cout << endl; cout << endl; cout << endl;
cout << "副本保存失败!!!!按任意键返回" << endl;
if (i = _getch()) return;
}
if (student::num_total == 0)
{
f << "当前记录中无学生" << endl;
}
else
{
student* p = first->next;
while (p != null)
{
f << p->id << " " << endl;
f << p->name << endl;
f << p->sex << endl;
f << p->age << endl;
f << p->science << endl;
f << p->pe << endl;
f << p->chinese << endl;
f << p->math << endl;
f << p->english << endl;
p = p->next;
}
}
f.close();
cout << endl; cout << endl; cout << endl;
cout << "学生信息文件已创建,按任意键继续" << endl;
i = _getch();
}
void studentMessage::read(void)
{
system("cls");
string name;
string age;
long long id;
string sex;
double chinese, math, english; //语文 , 数学 , 英语
double pe, science; //体育 科学
int i;
long long ch;
ifstream f("duplicate.txt");
while (1)
{
f >> ch;
if (f.eof())
{
cout << "读取完毕" << endl;
if (i = _getch()) return;
}
id=ch;
f >> name;
f >> sex;
f >> age;
f >> science;
f >> pe;
f >> chinese;
f >> math;
f >> english;
student::num_total++;
student* newstu = new student();
newstu = new student(id,name,sex,age,science,pe, chinese, math, english);
student* p = first;
while (p->next != NULL)
{
p = p->next;
}
p->next = newstu;
newstu->next = null;
}
}
void studentMessage::tongji(void)
{
system("cls");
cout << "学生人数一共为:" << student::num_total << endl;
int i;
cout << endl; cout << endl; cout << endl;
cout << "按任意键继续" << endl;
i = _getch();
}
//插入
void studentMessage::Insret(void)
{
system("cls");//
string name;
string age;
long long id;
char grade;
string sex, address;
double chinese, math, english; //语文 , 数学 , 英语
double pe, science; //体育 科学
string major; long long int TL; // 专业 , 电话
cout << "请输入学生姓名: ";
cin >> name;
cout << "请输入学生性别(boy/girl): ";
cin >> sex;
cout << "请输入学生出生日期: ";
cin >> age;
cout << "请输入学生学号: ";
cin >> id;
cout << "请输入科学成绩:" << endl;
cin >> science;
cout << "请输入体育成绩:" << endl;
cin >> pe;
cout << "请输入语文成绩:" << endl;
cin >> chinese;
cout << "请输入数学成绩:" << endl;
cin >> math;
cout << "请输入英语成绩:" << endl;
cin >> english;
student::num_total++;
student* newstu = new student();
newstu = new student(id, name, sex, age, science, pe,chinese, math ,english);
student* p = first;
while (p->next != NULL)
{
p = p->next;
}
p->next = newstu;
newstu->next = null;
}
//00000000000000000000000/
void studentMessage::Display(void)
{
system("cls");
if (student::num_total == 0)
{
cout << "当前记录中无学生" << endl;
}
else
{
student* p = first->next;
while (p != null)
{
cout << "学号:" << p->id << " " << endl;
cout << "姓名:" << p->name << endl;
cout << "性别(boy/girl):" << p->sex << endl;
cout << "出生日期:" << p->age << endl;
cout << "科学成绩:" << p->science << endl;
cout << "体育成绩:" << p->pe << endl;
cout << "语文成绩:" << p->chinese << endl;
cout << "数学成绩:" << p->math << endl;
cout << "英语成绩:" << p->english << endl;
cout << "------------------------------------------------" << endl;
p = p->next;
}
}
int i;
cout << endl; cout << endl; cout << endl;
cout << "按任意键继续" << endl;
i = _getch();
}
//删除功能~~~~~~~~~~~~~~~~
void studentMessage::Delete(void)
{
string _name;
system("cls");
cout << "请输入需要删除的同学姓名:" << endl;
cin >> _name;
int k = 0;
student* p = first;
student* pre = first;
while (p->next) //这里是报错的位置
{
pre = p->next;
if (pre->name == _name)
{
p->next = pre->next;
k = 1;
delete pre;
}
p = p->next;
}
if (k == 0 && p->name != _name) cout << "记录为空!" << endl;
else
{
student::num_total--;
}
int i;
cout << endl; cout << endl; cout << endl;
cout << "按任意键继续" << endl;
i = _getch();
}
void studentMessage::Search(void)
{
system("cls");
int temp = 0;
cout << "请输入查找的条件,有如下选项" << endl;
cout << "按照学号查找(请输入【1】) 按照姓名查找(请输入【2】) " << endl;
cout << " 退出(请输入【666】)" << endl;
cin >> temp;
switch (temp)
{
case 1: SearchByid(); break;
case 2: SearchByname(); break;
case 666: return;
default: cout << "输入有误" << endl;
}
}
void studentMessage::SearchByid(void)
{
system("cls");//
long long _id;
int flag = 0;
cout << "请输入待查找学生的学号:";
cin >> _id;
student* p = first->next;
while (p != null)
{
if (p->id == _id)
{
flag = 1;
cout << "下面是查找匹配结果:" << endl;
cout << endl; cout << endl; cout << endl;
cout << "学号:" << p->id << " " << endl;
cout << "姓名:" << p->name << endl;
cout << "性别(boy/girl):" << p->sex << endl;
cout << "出生日期:" << p->age << endl;
cout << "科学成绩:" << p->science << endl;
cout << "体育成绩:" << p->pe << endl;
cout << "语文成绩:" << p->chinese << endl;
cout << "数学成绩:" << p->math << endl;
cout << "英语成绩:" << p->english << endl;
}
p = p->next;
}
if (flag == 0)
{
cout << "未找到匹配项" << endl;
}
int i;
cout << endl; cout << endl; cout << endl;
cout << "按任意键继续" << endl;
i = _getch();
}
void studentMessage::SearchByname(void)
{
system("cls");
string _name;
int flag = 0;
cout << "请输入待查找的学生姓名: ";
cin >> _name;
student* p = first->next;
while (p != null)
{
if (p->name == _name)
{
cout << "下面是查找匹配结果:" << endl;
cout << endl; cout << endl; cout << endl;
cout << "学号:" << p->id << " " << endl;
cout << "姓名:" << p->name << endl;
cout << "性别(boy/girl):" << p->sex << endl;
cout << "出生日期:" << p->age << endl;
cout << "科学成绩:" << p->science << endl;
cout << "体育成绩:" << p->pe << endl;
cout << "语文成绩:" << p->chinese << endl;
cout << "数学成绩:" << p->math << endl;
cout << "英语成绩:" << p->english << endl;
}
p = p->next;
}
if (flag == 0)
{
cout << "未找到匹配项" << endl;
}
int i;
cout << endl; cout << endl; cout << endl;
cout << "按任意键继续" << endl;
i = _getch();
}
void studentMessage::Change(void)
{
system("cls");
string _name, _sex, _address, _major, _age;
char _grade; long long int _TL;
double _chinese, _math, _english; //语文 , 数学 , 英语
double _pe, _science; //体育 科学
int flag = 0, temp;
long long _id;
int course = 0;
cout << "请输入需要改动信息的学生的姓名: ";
cin >> _name;
student* p = first->next;
while (p != null)
{
if (p->name == _name)
{
flag = 1;
cout << "该学生当前信息如下:" << endl;
cout << "学号:" << p->id << " " << endl;
cout << "姓名:" << p->name << endl;
cout << "性别(boy/girl):" << p->sex << endl;
cout << "出生日期:" << p->age << endl;
cout << "科学成绩:" << p->science << endl;
cout << "体育成绩:" << p->pe << endl;
cout << "语文成绩:" << p->chinese << endl;
cout << "数学成绩:" << p->math << endl;
cout << "英语成绩:" << p->english << endl;
cout << "请指明哪一项需要修改" << endl;
cout << "修改学号(输入【1】) 修改出生日期(输入【2】)修改性别(输入【3】)修改成绩信息(输入【4】) " << endl;
cout << " 退出(输入【123】)" << endl;
cin >> temp;
switch (temp)
{
case 1:
{
cout << "请输入新的学号:" << endl; cin >> _id;
p->id = _id;
}
break;
case 2:
{
cout << "请输入新的出生日期:" << endl;; cin >> _age;
p->age = _age;
}
break;
case 3:
{
cout << "请输入性别:" << endl;
cin >> _sex;
p->sex = _sex;
}
break;
case 4:
{
cout << "请输入科学成绩:" << endl;
cin >> _science;
p->science = _science;
cout << "请输入体育成绩:" << endl;
cin >> _pe;
p->pe = _pe;
cout << "请输入语文成绩:" << endl;
cin >> _chinese;
p->chinese = _chinese;
cout << "请输入数学成绩:" << endl;
cin >> _math;
p->math = _math;
cout << "请输入英语成绩:" << endl;
cin >> _english;
p->english = _english;
}
break;
case 123: return;
cout << "修改后的信息如下: " << endl;
cout << "姓名:" << p->name << " " << endl;
cout << "性别:" << p->sex << " " << endl;
cout << "出生日期:" << p->age << " " << endl;
cout << "学号:" << p->id << " " << endl;
cout << "科学成绩:" << p->science << " " << endl;
cout << "体育成绩:" << p->pe << " " << endl;
cout << "语文成绩:" << p->chinese << " " << endl;
cout << "数学成绩:" << p->math << " " << endl;
cout << "英语成绩:" << p->english << " " << endl;
default: cout << "输入有误" << endl;
}
}
p = p->next;
}
if (flag == 0)
cout << "当前记录中没有此学生" << endl;
}
void studentMessage::clear(void)
{
student::num_total = 0;
student* p = first->next;
while (p != null)
{
first->next = p->next;
p->next = null;
delete p;
p = first->next;
}
}
int main()
{
studentMessage stulist;
int ch;
while (ch = stulist.menu())
{
switch (ch)
{
case 1: stulist.Insret(); break;
case 2: stulist.Search(); break;
case 3: stulist.Display(); break;
case 4: stulist.Change(); break;
case 5: stulist.Delete(); break;
case 6: stulist.tongji(); break;
case 7:
stulist.save();
stulist.duplicate();
break;
case 8: stulist.clear(); break;
case 9: stulist.read(); break;
case 0: return 0;
default: cout << "请按要求输入" << endl;
}
}
return 0;
}
究竟是哪里出了问题?