-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudentList.cpp
138 lines (134 loc) · 3.19 KB
/
studentList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include"studentList.h"
#include<QDebug>
void studentList::insertNode(studentNode*& obj)
{
if (!students)
{
students = obj;
students->next = obj;
obj->next = NULL;
return;
}
studentNode* p = students;
while (p->next)p = p->next;
p->next = obj;
obj->next = NULL;
}
void studentList::deleteNode(studentNode*& obj)
{
if(!haveStudent(obj->id))return;
studentNode* s = students;
if (s == obj)
{
students = (studentNode*)s->next;
delete s;
s = NULL;
return;
}
else
{
while (s->next != obj)
{
s = (studentNode*)s->next;
}
studentNode* p = (studentNode*)obj->next;
s->next = p;
delete obj;
obj = NULL;
}
}
void studentList::initList()
{
QFile fi("D:\\Qt\\projects\\classSelectionProgram\\studentList.txt");
fi.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream st(&fi);
int tmpId;
QString tmpName;
QString tmpPassword;
int tmpNOC;
QString str;
QVector<QString> tmpChosen;
while(!st.atEnd())
{
st>>tmpId>>tmpName>>tmpPassword>>tmpNOC;
for(int i=0;i<tmpNOC;i++){
st>>str;
tmpChosen.push_back(str);
}
studentNode*stu=new studentNode(tmpId,tmpName,tmpPassword,tmpNOC,tmpChosen);
insertNode(stu);
tmpChosen.clear();
}
if(haveStudent(0))
deleteNode(findStudent(0));//最后会多读一个无用数据,需要删除
}
void studentList::traverseStudent()
{
studentNode* p = students;
while (p)
{
qDebug() << p->id << ' ' << p->name << ' ' << p->numOfChosen << ' ';
for (int i = 0; i < p->numOfChosen; i++)
qDebug() << p->chosen[i] << ' ';
p = p->next;
}
}
studentNode*& studentList::findStudent(int id)
{
tmpPtr=students;
while(tmpPtr)
{
if(tmpPtr->id==id)return tmpPtr;
tmpPtr=tmpPtr->next;
}
}
int studentList::haveStudent(int id)
{
tmpPtr=students;
while(tmpPtr)
{
if(tmpPtr->id==id)return 1;
tmpPtr=tmpPtr->next;
}
return 0;
}
void studentList::selectClass(int id,QString& className)
{
findStudent(id)->chosen.push_back(className);
findStudent(id)->numOfChosen++;
}
void studentList::deleteClass(int id,QString& className)
{
for(int i=0;i<findStudent(id)->chosen.size();i++)
if(findStudent(id)->chosen.at(i)==className)
{
findStudent(id)->chosen.remove(i);
findStudent(id)->numOfChosen--;
return;
}
}
int studentList::studentHaveClass(int id, QString& className)
{
for(int i=0;i<findStudent(id)->chosen.size();i++)
if(findStudent(id)->chosen.at(i)==className)
{
return 1;
}
return 0;
}
void studentList::save()
{
QFile fi("D:\\Qt\\projects\\classSelectionProgram\\studentList.txt");
fi.open(QIODevice::WriteOnly|QIODevice::Text);
QTextStream st(&fi);
studentNode*p=students;
while(p)
{
st<<p->id<<' '<<p->name<<' '<<p->getPassword()<<' '<<p->numOfChosen<<' ';
for(int i=0;i<p->numOfChosen;i++)
st<<p->chosen[i]<<' ';
st<<Qt::endl;
p=p->next;
}
fi.close();
}