这个程序后面有时候会stu * p, 那new出来的空间不用delete回收掉吗?
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
/*
Written by 武汉大学国际软件学院—陈志杰 in 2016/12/5
对于这个程序的简要介绍:
1.内存中学生记录的形式为链表结构,链表结构便于增删,容量能自主控制
2.读取txt文本的学生信息会clear链表中的所有值(详见clear函数)
3.增删学生记录函数只会在内存中做操作,而不会自动保存到txt中,需要手动调用保存才会存放到硬盘的文件上
4.所有堆的创建释放应该注意完全了
@All rights reserved.
*/
string course_names[5] = { "高数","英语","计算机","体育","政治" }; //这个全局变量以便某些函数使用
class Course {
public:
char* name;
int courseID;
int credit;
int time;
};
class Score {
public:
double score;
char* courseName;
};
class Student {
public:
Score scores[5];
int stuID;
string name;
public:
Student() {
scores[0].courseName = "高数";
scores[1].courseName = "英语";
scores[2].courseName = "计算机";
scores[3].courseName = "体育";
scores[4].courseName = "政治";
}
};
class stu { //这个类用来作为学生链表的节点类
public:
Student* student;
stu* next; //指向下一个节点
public:
stu() {
student = new Student();
next = nullptr;
}
~stu() {
delete student;
}
};
class controller {
private:
stu* start; //作为一条学生类的链表头来记录学生的信息
stu* temp; //当前指向的节点,用作游标
public:
controller() {
start = new stu();
delete start->student;
start->student = nullptr;
temp = start;
}
public:
//添加学生
void addStudent() { //其中a,b,c,d,e分别是不同的课程成绩
string name;
int id;
double a, b, c, d, e;
cout <<endl<< "请输入学生学号:" << endl;
cin >> id;
cout << endl << "请输入学生姓名:" << endl;
cin >> name;
cout << endl << "请输入高数成绩:" << endl;
cin >> a;
cout << endl << "请输入英语成绩:" << endl;
cin >> b;
cout << endl << "请输入计算机成绩:" << endl;
cin >> c;
cout << endl << "请输入体育成绩:" << endl;
cin >> d;
cout << endl << "请输入政治成绩:" << endl;
cin >> e;
temp->next = new stu();
temp = temp->next;
temp->student->scores[0].score = a;
temp->student->scores[1].score = b;
temp->student->scores[2].score = c;
temp->student->scores[3].score = d;
temp->student->scores[4].score = e;
temp->student->name = name;
temp->student->stuID = id;
system("cls");
cout << "添加成功!" << endl << endl << endl;
}
//显示某个学生信息
void display(int id) {
system("cls");
cout << "学号/姓名/高数/英语/计算机/体育/政治" << endl;
stu* p = start;
while (p != nullptr) {
if (p->student != nullptr) {
if (p->student->stuID == id) {
cout << p->student->stuID << "/" << p->student->name;
for (int i = 0;i < 5;i++) {
cout <<"/"<< p->student->scores[i].score;
}
cout << endl;
}
}
p = p->next;
}
cout << endl << endl;
}
//显示所有学生的信息
void display() {
system("cls");
cout << "学号/姓名/高数/英语/计算机/体育/政治" << endl;
stu* p = start;
while (p != nullptr) {
if (p->student != nullptr) {
cout << p->student->stuID << "/" << p->student->name;
for (int i = 0;i < 5;i++) {
cout << "/" << p->student->scores[i].score;
}
cout << endl;
}
p = p->next;
}
cout << endl << endl;
}
//按某门课程查询所有学生的成绩(i代表课程号,只为便于编程)
void displayAllByCourse() {
cout << "高数:0 英语:1 计算机:2 体育:3 政治:4 " << endl;
cout << "请输入您要查询的课程编号:";
int i = 0;
cin >> i;
system("cls");
cout << "学号/姓名/" << course_names[i]<< endl;
stu* p = start;
while (p != nullptr) {
if (p->student != nullptr) {
cout << p->student->stuID << "/" << p->student->name;
cout << "/" << p->student->scores[i].score;
cout << endl;
}
p = p->next;
}
cout << endl << endl;
}
//按照某门课程的成绩来排序
void sortByCourse() {
cout << "高数:0 英语:1 计算机:2 体育:3 政治:4 " << endl;
cout << "请输入您要排序的课程编号:";
int i = 0;
cin >> i;
stu* p1 = start->next;
stu* p2 = start->next;
while (p1 != nullptr) {
p2 = p1->next;
while (p2 != nullptr) {
if (p1->student != nullptr && p2->student != nullptr) {
if (p1->student->scores[i].score <= p2->student->scores[i].score) {
Student* st = p1->student;
p1->student = p2->student;
p2->student = st;
}
}
p2 = p2->next;
}
p1 = p1->next;
}
system("cls");
cout << "排序成功!" << endl;
cout << endl << endl;
}
//按照总分排序
void sortBySum() {
stu* p1 = start->next;
stu* p2 = start->next;
while (p1 != nullptr) {
p2 = p1->next;
while (p2 != nullptr) {
if (p1->student != nullptr && p2->student != nullptr) {
double s1=0, s2=0;
for (int i = 0; i < 5; i++) {
s1 += p1->student->scores[i].score;
s2 += p2->student->scores[i].score;
}
if (s1<s2) {
Student* st = p1->student;
p1->student = p2->student;
p2->student = st;
}
}
p2 = p2->next;
}
p1 = p1->next;
}
system("cls");
cout << "排序成功!" << endl;
cout << endl << endl;
}
//将某门课不及格的学生显示出来
void displayStuNotPass() {
cout << "高数:0 英语:1 计算机:2 体育:3 政治:4 " << endl;
cout << "请输入您要选择的课程编号:";
int i = 0;
cin >> i;
system("cls");
cout << "学号/姓名/高数/英语/计算机/体育/政治" << endl;
stu* p = start;
while (p != nullptr) {
if (p->student != nullptr && p->student->scores[i].score <60) {
cout << p->student->stuID << "/" << p->student->name;
for (int i = 0;i < 5;i++) {
cout << "/" << p->student->scores[i].score;
}
cout << endl;
}
p = p->next;
}
cout << endl << endl;
}
//清除所有数据
void clear() {
stu* p;
while (start->next != nullptr) {
p = start->next;
start->next = p->next;
delete p;
}
}
//读取信息
void readStu() {
ifstream in;
in.open(".\\student.txt");
bool flag = false;
char buf[255];
clear();
while (!in.eof()) {
if (flag == true) {
string name;
int id;
double a, b, c, d, e;
in >> id >> name >> a >> b >>c >> d >> e;
temp->next = new stu();
temp = temp->next;
temp->student->scores[0].score = a;
temp->student->scores[1].score = b;
temp->student->scores[2].score = c;
temp->student->scores[3].score = d;
temp->student->scores[4].score = e;
temp->student->name = name;
temp->student->stuID = id;
}
else {
in >> buf >> buf >> buf >> buf >> buf >> buf >> buf;
flag = true;
}
}
in.close();
system("cls");
cout << "信息读取成功!" << endl << endl << endl;
}
void deleteStu() {
cout << "请输入你要删除的学生的id:" << endl;
int id;
cin >> id;
stu* p1 = start;
stu* p2 = p1->next;
while (p2 != nullptr) {
if (p2->student != nullptr) {
if (p2->student->stuID == id) {
p1->next = p2->next;
delete p2;
p2 = p1->next;
}
else {
p1 = p1->next;
p2 = p2->next;
}
}
}
system("cls");
cout << "删除操作完毕!" << endl << endl << endl;
}
//保存学生信息
void saveStu() {
ofstream out(".\\student.txt");
out << setw(10) << "学号" << setw(10) << "姓名" << setw(10) << "高数" << setw(10) << "英语" << setw(10) << "计算机" << setw(10) << "体育" << setw(10) << "政治" << endl;
stu* p = start;
while (p != nullptr) {
if (p->student != nullptr) {
out <<setw(10)<< p->student->stuID << setw(10) << p->student->name;
for (int i = 0;i < 5;i++) {
out << setw(10) << p->student->scores[i].score;
}
if (p->next != nullptr) {
out << endl;
}
}
p = p->next;
}
out.flush();
out.close();
system("cls");
cout << "信息保存完毕!" << endl << endl << endl;
}
};
int main() {
controller con;
while (true) {
cout << "******************主菜单******************" << endl;
cout << "1.读取txt学生信息" << 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 << "10.退出系统" << endl;
cout << "******************************************" << endl;
int i = 0;
cin >> i;
switch (i) {
case 1:
con.readStu();
break;
case 2:
con.addStudent();
break;
case 3:
con.display();
break;
case 4:
con.displayAllByCourse();
break;
case 5:
con.displayStuNotPass();
break;
case 6:
con.sortByCourse();
break;
case 7:
con.sortBySum();
break;
case 8:
con.deleteStu();
break;
case 9:
con.saveStu();
break;
case 10:
exit(0);
break;
default:
cout << "请输入1-8的某个数字进行选择!" << endl;
break;
}
}
}
大致看了下,在清空链表和删除节点处都有delete 释放内存空间啊。其他地方new出来的空间就是链表应有的空间,只不过指针指向其他节点而已。new出来的空间依然存在