-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteacherList.cpp
104 lines (102 loc) · 2.14 KB
/
teacherList.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
#include"teacherList.h"
QString teacherNode::getPassword()
{
return password;
}
void teacherNode::setPassword(QString str)
{
password=str;
}
void teacherList::insertNode(teacherNode *&obj)
{
if (!teachers)
{
teachers = obj;
teachers->next = obj;
obj->next = NULL;
return;
}
teacherNode* p = teachers;
while (p->next)p = (teacherNode*)p->next;
p->next = obj;
obj->next = NULL;
}
void teacherList::deleteNode(teacherNode *&obj)
{
teacherNode* s = teachers;
if (s == obj)
{
teachers = (teacherNode*)s->next;
delete s;
s = NULL;
return;
}
else
{
while (s->next != obj)
{
s = (teacherNode*)s->next;
}
teacherNode* p = (teacherNode*)obj->next;
s->next = p;
delete obj;
obj = NULL;
}
}
void teacherList::initList()
{
QFile fi("D:\\Qt\\projects\\classSelectionProgram\\teacherList.txt");
fi.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream st(&fi);
int tmpId;
QString tmpName;
QString tmpPassword;
while(!st.atEnd())
{
st>>tmpId>>tmpName>>tmpPassword;
teacherNode*tea=new teacherNode(tmpId,tmpName,tmpPassword);
insertNode(tea);
}
deleteNode(findTeacher(0));//最后会多读一个无用数据,需要删除
}
void teacherList::save()
{
QFile fi("D:\\Qt\\projects\\classSelectionProgram\\teacherList.txt");
fi.open(QIODevice::WriteOnly|QIODevice::Text);
QTextStream st(&fi);
teacherNode*p=teachers;
while(p)
{
st<<p->id<<' '<<p->name<<' '<<p->getPassword()<<Qt::endl;
p=p->next;
}
fi.close();
}
void teacherList::traverseTeacher()
{
teacherNode* p = teachers;
while (p)
{
qDebug() << p->id << ' ' << p->name <<Qt::endl;
p = p->next;
}
}
teacherNode*& teacherList::findTeacher(int id)
{
tmpPtr=teachers;
while(tmpPtr)
{
if(tmpPtr->id==id)return tmpPtr;
tmpPtr=tmpPtr->next;
}
}
int teacherList::haveTeacher(int id)
{
teacherNode*p=teachers;
while(p)
{
if(p->id==id)return 1;
p=p->next;
}
return 0;
}