没事可以看看,有一些思路还是很好的

链表

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// 那些心酸事: = 写成 == , == 写成 =  一个晚上让人难受。。。。。。 
// 细心啊
/*
//
\\ //
\\ //
##DDDDDDDDDDDDDDDDDDDDDD##
## DDDDDDDDDDDDDDDDDDDD ## __________ ____ _____ ____
## hh hh ## |\ __ \ | \ \ | \ \ | \ \
## hh // \\ hh ## \ \ \|\ /_ \ \ \ \ \ \ \ \ \
## hh // \\ hh ## \ \ __ \ \ \ \ \ \ \ \ \ \
## hh hh ## \ \ \|\ \ \ \ \ \ \ \______ \ \ \
## hh WWWW hh ## \ \________\ \ \__\ \ \_________\ \ \__\
## hh hh ## \|________| \|__| \|_________| \|__|
## MMMMMMMMMMMMMMMMMMMM ##
##MMMMMMMMMMMMMMMMMMMMMM##
\/ \/

__________ ____ _____ ____
|\ __ \ | \ \ | \ \ | \ \
\ \ \|\ /_ \ \ \ \ \ \ \ \ \
\ \ __ \ \ \ \ \ \ \ \ \ \
\ \ \|\ \ \ \ \ \ \ \______ \ \ \
\ \________\ \ \__\ \ \_________\ \ \__\
\|________| \|__| \|_________| \|__|

保佑再无 Bug
*/

#include<iostream>
using namespace std;
// 定义结构体并重命名
typedef struct link
{
int data;
struct link *next;
}Link;
// 主函数
int main()
{
// 函数声明
Link *addNode (Link *head);
Link *deleteNode (Link *head, int nodeData);
Link *insertNode (Link *head, int nodeData);
void displayNode (Link *head);
void freeNode (Link *head);
int menu ();
// 赋初值
Link *head = NULL;
int i = 0, j;
char c;
int nodeData;
while (true)
{
// j 是选择的结果
j = menu();
switch (j)
{
case 1:
head = addNode(head);
// 计算有多少个节点
i++;
break;
case 2:
cout<<"Please input the node you want to delete: ";
cin>>nodeData;
head = deleteNode(head, nodeData);
// 计算有多少个节点
if (i > 0) i--;
break;
case 3:
cout<<"Please input the node you want to insert: ";
cin>>nodeData;
head = insertNode(head, nodeData);
// 计算有多少个节点
i++;
break;
case 4:
displayNode(head);
cout<<i<<" new nodes have been founded !"<<endl;
break;
case 5:
freeNode(head);
cout<<"Quited ......"<<endl<<endl;
system("pause");
exit(0);
break;
default:
cout<<"输入无效!";
break;
}
}
return 0;
}

// 显示菜单
int menu ()
{
int i;
cout<<endl<<"What do you want to do ?"<<endl<<endl;
cout<<"****************************"<<endl;
cout<<" * 1. Add node"<<endl<<" * 2. Delete node"<<endl<<" * 3. Insert node"<<endl<<
" * 4. Display node data"<<endl<<" * 5. Free node"<<endl<<"****************************"<<endl<<endl;
cout<<"Input your choose: ";
cin>>i;
return i;
}

// 添加节点
// 返回的是头节点 head
Link *addNode (Link *head)
{
// pr 先指向头节点,p 初始化
Link *p = NULL, *pr = head;
int data;
// 让 p 指向新创建的节点
p = new Link;
// 如果申请内存失败,则退出程序
if (p == NULL)
{
cout<<"No enough memory to allocate !"<<endl;
exit(0);
}
cout<<"Input node data: ";
cin>>data;
// 如原列表为空
if (head == NULL)
{
// 则将新建节点设置为头节点
head = p;
}
else
{
// 当 pr 不是最后一个节点时
while (pr -> next != NULL)
{
// pr 指向下一个节点
pr = pr -> next;
}
// 将新建的节点放在列表最后面
pr -> next = p;
}
// 新建节点赋值操作
// 放前面也行哈
p -> data = data;
p -> next = NULL;
return head;
}

// 删除指定节点
Link *deleteNode (Link *head, int nodeData)
{
Link *p = head, *pr = head;
// 若链表为空,则退出该操作
if (head == NULL)
{
cout<<"Linked table is empty !"<<endl;
return head;
}
// 当没有找到且未到链表尾
while (nodeData != p -> data && p -> next != NULL)
{
// pr 保存当前节点
pr = p;
// p 指向下一节点
p = p -> next;
}
// 再判断一次,因为上面的判断可能是后一个条件导致循环结束
// 找到要删除的节点
if (nodeData == p -> data)
{
// 要删除的是头节点
// 因为头节点有可能改变,所以函数返回的是 head
if (p == head)
{
// 一步错,不知道会有几步错。。。
head = p -> next;
}
// 要删除的不是头节点
else
{
// pr 就是为此保存当前节点
pr -> next = p -> next;
}
// 删除节点
delete p;
}
// 未找到需删除的数据
else
{
cout<<"This node has not been found !"<<endl;
}
return head;
}

// 插入节点
Link *insertNode (Link *head, int nodeData)
{
Link *p = head, *pr = head, *temp = NULL;
// 先 new 一个
p = new Link;
// 如果申请内存失败,则退出程序
if (p == NULL)
{
cout<<"No enough memory to allocate !"<<endl;
exit(0);
}
// 新节点赋值
p -> data = nodeData;
p -> next = NULL;
// 如果原链表为空
if (head == NULL)
{
// 头节点可能发生改变
// 则将新建节点设置为头节点
head = p;
}
// 如果原链表不为空
else
{
// 未找到插入节点位置,继续
// 这里别弄混了
while (pr -> data < nodeData && pr -> next != NULL)
{
// temp 保存当前节点
temp = pr;
// p 指向下一节点
pr = pr -> next;
}
// 再判断一次,因为上面的判断可能是后一个条件导致循环结束
// 找到插入位置
if (pr -> data >= nodeData)
{
// 在头节点前插入
if (pr == head)
{
p -> next = head;
head = p;
}
else
{
// temp 的用处这就表现出来了
// 相当于要在 temp 和 pr 之间插入一个节点
pr = temp;
// p 是需要插入的节点,对应好
p -> next = pr -> next;
pr -> next = p;
}
}
// 在链表末插入
else
{
pr -> next = p;
}
}
return head;

}

// 显示节点及其中数据
void displayNode (Link *head)
{
Link *p = head;
int j = 1;
while (p != NULL)
{
cout<<" The "<<j<<" node is"<<" "<<p -> data<<endl;
p = p -> next;
j++;
}
}

// 释放节点所占内存,一个一个来
void freeNode (Link *head)
{
Link *p = head, *pr = NULL;
while (p != NULL)
{
pr = p;
p = p -> next;
delete p;
}
}


实验报告一

1-1a

编写金字塔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;
int main()
{
int i, j, n, k;
cout<<"Please input the row number: ";
cin>>n;
for (i = 1; i <= n; i++)
{
for (j = 20; j - i > 0; j--)
{
cout<<" ";
}
for (k = 0; k < 2 * i - 1; k++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}

1-1b

编写杨辉三角

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
#include<iostream>
using namespace std;
int main()
{
const int N = 100;
// 数组很大
int n, a[N][N], i, j;
cout<<"Input the row: ";
cin>>n;
// 不以0为第一个数,直接用表面意思,a[1][1]即第一行第一个,以0为第一个数会很容易让人弄傻。。。
// 每行的第一个和最后一个都为 1
for (i = 1; i <= n; i++) a[i][1] = a[i][i] = 1;
// 赋值数据
for (i = 3; i <= n; i++)
{
for (j = 2; j <= i - 1; j++)
{
// 中间数的规律
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
// 输出数据
for (i = 1; i <= n; i++)
{
// 显示空格
int k;
for (k = 30; k - i * 2 > 0; k--)
{
cout<<" ";
}
// 显示数据
for (j = 1; j <= i; j++)
{
cout<<a[i][j]<<" ";
}
// 每打印完一行换行
cout<<endl;
}
return 0;
}

2. 改错

对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。
请计算需要经过几步才能将n变到1,具体可见样例
输入:
测试包含多个用例,每个用例包含一个整数n,当n为0 时表示输入结束。(1<=n<=10000)
输出:
对于每组测试用例请输出一个数,表示需要经过的步数,每组输出占一行

样例输入: 3 1 0
样例输出: 5 0

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
#include<iostream>
using namespace std;
int main()
{
int i, n, k = 0;
int a[100], t[100];
cout<<"please input numbers:";
for (i = 0; i < 100; i++)
{
cin>>a[i];
k++;
if (a[i] == 0)
break;
}
for (i = 0, t[i] = 0; i < k; i++)
{
while (a[i] != 1)
{
if (a[i] % 2 == 0)
{
a[i] = a[i] / 2;
}
else
{
a[i] = (a[i] * 3 + 1) / 2;
}
t[i]++;
}
cout<<t[i]<<endl;
}
return 0;

改错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for (i = 0; i < k; i++)
// 错误1:应将t[i]放在for循环里面
t[i] = 0;
// 错误二:应增加一条判断语句,判断何时退出
if (a[i] == 0) break;
while (a[i] != 1)
{
if (a[i] % 2 == 0)
{
a[i] = a[i] / 2;
}
else
{
a[i] = (a[i] * 3 + 1) / 2;
}
t[i]++;
}
// 若需与下图保持一直,应用cout<<t[i]<<”\t”;
cout<<t[i]<<endl;
}

3. 编写顺序查找算法

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
#include<iostream>
using namespace std;
int main()
{
int arr[] = {2, 12, 4, 7, 13, 6, 11, 78, 65, 66};
int i;
// 显示数组
for (i = 0; i < 10; i++)
{
cout<<" "<<arr[i];
}
int n;
cout<<endl<<endl<<"Input the number you want to find: ";
cin>>n;
// 下标
int m;
// 查找
for (i = 0; i < 10; i++)
{
if (arr[i] == n)
{
m = i;
cout<<"the position of the key "<<n;
cout<<" in the array is: "<<m<<endl;
return 0;
}
}
m = -1;
cout<<"the position of the key "<<n;
cout<<" in the array is: "<<m<<endl;
return 0;
}

4. 编写折半查找算法

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
#include<iostream>
using namespace std;
int main()
{
int arr[] = {2, 4, 6, 7, 11, 12, 13, 65, 66, 78};
int low = 0, high = 9, mid;
int i;
// 显示数组
for (i = 0; i < 10; i++)
{
cout<<" "<<arr[i];
}
// 输入需要查找的数
int n;
cout<<endl<<endl<<"Input the number you want to find: ";
cin>>n;
while (low <= high)
{
mid = (low + high) / 2;
if (arr[mid] > n)
{
high = mid - 1;
}
else if (arr[mid] < n)
{
low = mid + 1;
}
else break;
}
if (low <= high)
{
cout<<"the position of the key "<<n<<" in the array is: "<<mid<<endl;
}
else
{
mid = -1;
cout<<"the position of the key "<<n<<" in the array is: "<<mid<<endl;
}

return 0;
}

5. 编写代码

输入10个整数,将这10个整数按升序排列输出,并且奇数在前,偶数在后

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
#include<iostream>
using namespace std;
int main()
{
int n = 10;
// a[n]放所有数据,b[n]放偶数,c[n]放奇数
int a[n], b[n], c[n];
cout<<"Please input ten numbers: ";
// 输入数据
int i, j = 0, k = 0;
for (i = 0; i < 10; i++)
{
cin>>a[i];
}
// 先打印一遍
for (i = 0; i < 10; i++)
{
cout<<" "<<a[i];
}
cout<<endl<<"n="<<n<<endl;
// 奇数 偶数 分开
for (i = 0; i < 10; i++)
{
if (a[i] % 2 == 0)
{
b[j] = a[i];
j++;
}
else
{
c[k] = a[i];
k++;
}
}
// 奇数 偶数 分别排序 冒泡法

// 偶数
int m, temp;
for (i = 0; i < j - 1; i++)
{
for (m = 0; m < j - i - 1; m++)
{
if (b[m] > b[m+1])
{
temp = b[m];
b[m] = b[m+1];
b[m+1] = temp;
}
}
}
// 奇数
for (i = 0; i < k - 1; i++)
{
for (m = 0; m < k - i - 1; m++)
{
if (c[m] > c[m+1])
{
temp = c[m];
c[m] = c[m+1];
c[m+1] = temp;
}
}
}
// 显示数据
// 先奇数
for (i = 0; i < k; i++)
{
cout<<" "<<c[i];
}
// 再偶数
for (i = 0; i < j; i++)
{
cout<<" "<<b[i];
}
cout<<endl;
return 0;
}

6-1a

从键盘输入一个整数,判断该数是几位数,逆向输出该数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;
int main()
{
long n;
// 输入数据
cin>>n;
// 逆向输出
int i, m = 0, count = 0;
for (i = 0; i < 100; i++)
{
if (n > 0)
{
count++;
m = m * 10 + n % 10;
n = n / 10;
}
}
cout<<m<<endl;
cout<<"该数是"<<count<<"位数"<<endl;
return 0;
}

6-1b

将用户输入的一个字符串反向形式输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string str1,str2;
cin>>str1;
// 得知输入字符串的长度
for (i = 0; str1[i] != 0; i++);
// 倒序排列
for (--i; i >= 0; i--){
str2 = str2 + str1[i];
}
// 输出倒序后的字符串
cout<<str2<<endl;
return 0;
}

实验报告二

1. 子字符串出现次数

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
// 作用:子字符串在源字符串中出现的次数
// 注:若字符串含有空格,则必须用 getline/getlines ,再赋值给 string 类型变量,这里就不写了哈

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int findCount (string &s1, string &s2);
// s1 为源字符串, s2 为子字符串
string s1, s2;
cout<<"输入源字符串:"<<endl;
cin>>s1;
cout<<"输入子字符串:"<<endl;
cin>>s2;
// 出现次数
int count;
count = findCount(s1, s2);
cout<<s2<<" 出现的次数为:"<<count<<endl;
system("pause");
return 0;
}
// 找有几次
int findCount (string &s1, string &s2)
{
int n = 0;
int k;
// 得到子字符串的长度
for (k = 0; s2[k] != 0; k++);
for (int i = 0; s1[i] != 0;)
{
bool find = false;
for (int j = 0; s2[j] != 0;)
{
if (s1[i] == s2[j])
{
i++;
j++;
// 因为最后肯定是会退出的,需保证子字符串的最后一个字符相同才设置为 true
if (j == k) find = true;
}
// 有一个字符不同,子字符串再次指向第一个字符
else
{
i++;
j = 0;
}
// 源字符串到末尾了,退出
if (s1[i] == 0)
{
break;
}
}
if (find) n++;
}
return n;
}

2. 电话号码

现有一电话号码簿,其中有姓名、电话号码,当输入电话号码时,查找出姓名和电话号码;输入姓名时,同样查找出姓名与电话号码;还允许不完全输入查找,如输入010时,查找出所有以010开头的号码,输入“李”时,列出所有姓名以“李”开头的号码

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
#include<iostream>
#include<string>
using namespace std;
// 建一个类
class Contact
{
public:
Contact (string s, string n):name(s), num(n) {}
string name;
string num;
};
// 主函数
// 困扰我这么长时间,居然是 main 写成了 mian 。。。。太尴尬了
int main()
{
// 函数声明
bool findName (Contact &c, string f);
bool findNum (Contact &c, string f);
// 对象数组 (电话号码簿)
Contact cs[10] = {
Contact("郑大", "123456"),
Contact("郑中", "123567"),
Contact("郑小", "123678"),
Contact("李大", "234567"),
Contact("李中", "234678"),
Contact("李小", "234789"),
Contact("张大", "345678"),
Contact("张中", "345789"),
Contact("张小", "345890"),
Contact("小双", "987654")
};
// 选择
int c;
cout<<"1. 姓名"<<" "<<"2. 电话号码"<<endl;
cout<<"你想输入的是:";
cin>>c;
int i;
// 输入要查找的字符串
string name, num;
// 节流阀
bool isFind = false;
switch(c)
{
case 1:
cout<<"请输入姓名:";
cin>>name;
for (i = 0; i < 10; i++)
{
// 每个对象都得比较
if (findName(cs[i], name))
{
// 只要有一个符合条件的就设置为 true
isFind = true;
cout<<cs[i].name<<" "<<cs[i].num<<endl;
}
}
// 没有符合条件的
if (!isFind) cout<<"无法找到 !";
// 这个别忘了
break;
case 2:
cout<<"请输入号码:";
cin>>num;
for (i = 0; i < 10; i++)
{
// 每个对象都得比较
if (findNum(cs[i], num))
{
// 只要有一个符合条件的就设置为 true
isFind = true;
cout<<cs[i].num<<" "<<cs[i].name<<endl;
}
}
if (!isFind) cout<<"无法找到 !";
break;
default:
cout<<"无效输入 !"<<endl;
break;
}
system("pause");
return 0;
}
// 输入的是姓名
// 返回的是布尔值
bool findName (Contact &c, string f)
{
int k, j;
// 得到子字符串的长度
for (k = 0; f[k] != 0; k++);
// 注意作用域
bool find = false;
for (j = 0; j < k;)
{
if (c.name[j] == f[j])
{
j++;
if (j == k) find = true;
}
else break;
}
return find;
}
// 输入的是号码
// 返回的是布尔值
bool findNum (Contact &c, string f)
{
int k, j;
// 得到子字符串的长度
for (k = 0; f[k] != 0; k++);
// 注意作用域
bool find = false;
for (j = 0; j < k;)
{
if (c.num[j] == f[j])
{
j++;
if (j == k) find = true;
}
else break;
}
return find;
}


3. 课本习题

这个太琐碎了,就放在 gitee 上了,
点此处跳转


4. 学生链表

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 作用:学生链表

#include<iostream>
#include<string>
using namespace std;
// 定义结构体
typedef struct student
{
string name;
int grade;
struct student *next;
}Student;
// 主函数
int main()
{
Student *sort(Student *head);
Student *del(Student *head);
// Student *search(Student *head);
void display(Student *head);
void free(Student *head);
cout<<"Input name and score(-1 to exit): "<<endl;
// 头节点初始化
Student *head = new Student;
cin>>head -> name>>head -> grade;
head -> next = NULL;
// 起始判断
if (head -> grade >= 0)
{
Student *p = new Student;
cin>>p -> name>>p -> grade;
p -> next = NULL;
// 由于第二个节点是 head -> next 与后面不同,所以不能放进循环里
head -> next = p;
// pr 保存当前节点
Student *pr = p;
// 循环建立节点,直至 p -> grade = -1
while (p -> grade >= 0)
{
p = new Student;
if (p == NULL)
{
cout<<"No enough memory to allocate !"<<endl;
exit(0);
}
cin>>p -> name>>p -> grade;
p -> next = NULL;
pr -> next = p;
// pr 指向新建节点
pr = pr -> next;
}
}
// 如果第一个节点就是 -1
if (head -> grade < 0)
{
cout<<"Linked table is empty !"<<endl;
exit(0);
}
else
{
cout<<endl<<"Before delete:"<<endl;
head = sort(head);
display(head);
cout<<endl<<"After delete:"<<endl;
head = del(head);
// 如果输入的全是 60 以下
if (head == NULL)
{
cout<<"Both fail !"<<endl;
}
else display(head);
}
// 释放节点
free(head);
system("pause");
return 0;
}
// 排序
Student *sort(Student *head)
{
Student *p = head, *pr = NULL, temp;
while (p -> next != NULL)
{
pr = p->next;
while (pr != NULL)
{
// 那个,节点的交换太烧脑了,投机取巧,我交换节点的值
if (pr -> grade > p -> grade)
{
temp.name = pr -> name;
pr -> name = p -> name;
p -> name = temp.name;
temp.grade = pr -> grade;
pr -> grade = p -> grade;
p -> grade = temp.grade;
}
pr = pr -> next;
}
p = p -> next;
}
return head;
}
// 删除
Student *del(Student *head)
{
Student *p = head, *pr = NULL;
while (p != NULL)
{
// 第一个节点就小于 60,则后面全是,因为已经排过序了
if (head -> grade < 60)
{
// 全部 delete
while (p != NULL)
{
pr = p;
p = p -> next;
delete p;
}
// 此处可能改变 head 的值,所以函数是 Student* 型
head = p;
}
else
{
if (p -> grade >= 60)
{
pr = p;
p = p -> next;
}
else
{
pr -> next = p -> next;
delete p;
p = pr -> next;
}
}
}
return head;
}
// 打印链表
void display(Student *head)
{
Student *p = head;
while (p != NULL)
{
// 最后一个 -1 的不需要打印出来
if (p -> grade >= 0)
{
cout<<p -> name<<" "<<p -> grade<<endl;
p = p -> next;
}
else p = p -> next;
}
}
// 释放节点
void free(Student *head)
{
Student *p = head, *pr = NULL;
while (p != NULL)
{
pr = p;
p = p -> next;
delete p;
}
}

实验报告三

1. 课本习题

这个太琐碎了,就放在 gitee 上了
点此处跳转


2. 求两点的距离

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
// 作用:求两点的距离

#include<iostream>
#include<math.h>
using namespace std;
class Point
{
public:
// 定义构造函数并赋初值
Point (double a, double b):x(a), y(b) {}
double distance (Point &p);
// 在类外需要使用,所以定义为 public
double x;
double y;
};
// 成员函数
double Point::distance(Point &p)
{
double dst;
dst = pow((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y), 1.0/2);
return dst;
}
// 主函数
int main()
{
Point p1(1, 1), p2(2, 2);
cout<<p1.distance(p2)<<endl;
system("pause");
return 0;
}

3. 公路收费程序

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
// 作用:公路收费程序

#include<iostream>
using namespace std;
class Free
{
public:
Free () { car = 0; truck = 0; bus = 0; carfee = 0; truckfee = 0; busfee = 0; }
// 统计小汽车的车辆数和收费小计
void cars ();
// 统计卡车的车辆数和收费小计
void trucks ();
// 统计公汽的车辆数和收费小计
void buses ();
// 收费处理
void process ();
// 输出收费统计信息
void dispaly ();
int car;
int truck;
int bus;
int carfee;
int truckfee;
int busfee;
int sum;
};
// 成员函数
void Free::cars()
{
car++;
carfee += 10;
}
void Free::trucks()
{
truck++;
truckfee += 25;
}void Free::buses()
{
bus++;
busfee += 15;
}
void Free::process()
{
sum = carfee + truckfee + busfee;
}
void Free::dispaly()
{
cout<<"收费统计如下:"<<endl;
cout<<"小汽车:"<<car<<"辆"<<" "<<"收费小计:"<<carfee<<endl;
cout<<"卡车:"<<truck<<"辆"<<" "<<"收费小计:"<<truckfee<<endl;
cout<<"公汽:"<<bus<<"辆"<<" "<<"收费小计:"<<busfee<<endl;
cout<<" "<<"合计收费:"<<sum<<endl;
}
// 主函数
int main()
{
Free obj;
cout<<"1. 小汽车 (10元/辆)"<<endl<<"2. 卡车 (25元/辆)"<<endl<<"3. 公汽 (15元/辆)"<<"0 : 退出"<<endl<<endl;
int n, i = 1;
// 定义节流阀
bool active = true;
while (active)
{
cout<<" "<<i<<"-车型:";
cin>>n;
switch (n)
{
case 0:
// 当输入为 0 时退出
active = false;
break;
case 1:
obj.cars();
i++;
break;
case 2:
obj.trucks();
i++;
break;
case 3:
obj.buses();
i++;
break;
default:
cout<<"无效输入!"<<endl;
break;
}
}
obj.process();
obj.dispaly();
system("pause");
return 0;
}

实验报告四

1. 课本习题

这个太琐碎了,就放在 gitee 上了
点此处跳转


计算工资

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
#include<iostream>
#include<string>
using namespace std;
class Salary
{
public:
void setdata ();
void computed ();
void sort ();
void display ();
private:
long num;
string name;
int base_salary;
int post_salary;
int fund;
int tax;
int sum;
// 这几个数据由于在多个成员函数中都有使用,就放在类里了
int i, n;
Salary *salary1;
};
// 成员函数
// 输入数据
void Salary::setdata()
{
cout<<"请输入职工的人数:";
cin>>n;
// 动态创建对象数组
salary1 = new Salary[n];
for (i = 0; i < n; i++)
{
cin>>salary1[i].num>>salary1[i].name>>salary1[i].base_salary>>salary1[i].post_salary>>salary1[i].fund>>salary1[i].tax;
}
}
// 计算实际工资
void Salary::computed()
{
for (int i = 0; i < n; i++)
{
salary1[i].sum = salary1[i].base_salary + salary1[i].post_salary - salary1[i].fund - salary1[i].tax;
}
}
// 降序排序
void Salary::sort()
{
int j;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j< n; j++)
{
Salary temp;
if (salary1[i].sum < salary1[j].sum)
{
temp = salary1[i];
salary1[i] = salary1[j];
salary1[j] = temp;
}
}
}
}
// 输出数据
void Salary::display()
{
cout<<"职工实际发放的工资是:(降序排列)"<<endl;
for (int i = 0; i < n; i++)
{
cout<<salary1[i].num<<" "<<salary1[i].name<<" "<<salary1[i].base_salary<<" "<<salary1[i].post_salary<<" "<<salary1[i].fund<<" "<<salary1[i].tax<<" "<<salary1[i].sum<<endl;
}
// 这个莫忘了
delete [] salary1;
}

// 主函数
int main()
{
// 创建一个对象
Salary employees;
employees.setdata();
employees.computed();
employees.sort();
employees.display();
system("pause");
return 0;
}

学生管理

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
#include<iostream>
#include<string>
using namespace std;
class Students
{
public:
void setdata ();
void search (long n);
void display ();
void allDisplay ();
private:
string name;
long num;
int mathGrade;
int cplusGrade;
int enGrade;
// 这几个数据由于在多个成员函数中都有使用,就放在类里了
int i, n;
Students *student;
};
// 成员函数
// 输入数据
void Students::setdata()
{
cout<<"请输入学生总数: ";
cin>>n;
// 动态创建对象数组
student = new Students[n];
for (i = 0; i < n; i++)
{
cout<<"请输入学生姓名:";
cin>>student[i].name;
cout<<"请输入学生学号:";
cin>>student[i].num;
cout<<"请输入学生高等数学成绩:";
cin>>student[i].mathGrade;
cout<<"请输入学生C++成绩:";
cin>>student[i].cplusGrade;
cout<<"请输入学生大学英语成绩:";
cin>>student[i].enGrade;
cout<<endl;
}
}
// 查询
void Students::search(long n)
{
for (i = 0; i < n; i++)
{
if (student[i].num == n)
{
student[i].display();
return;
}
}
cout<<"无效学号输入!"<<endl;
}
// 打印
void Students::display()
{
cout<<"姓名:"<<name<<endl;
cout<<"学号:"<<num<<endl;
cout<<"高等数学成绩:"<<mathGrade<<endl;
cout<<"C++成绩:"<<cplusGrade<<endl;
cout<<"大学英语成绩:"<<enGrade<<endl;
}
// 全部打印
void Students::allDisplay ()
{
cout<<"------------------------------"<<endl;
cout<<"现有所有学生数据:"<<endl;
for (i = 0; i < n; i++)
{
cout<<"姓名:"<<student[i].name<<" "<<"学号:"<<student[i].num<<" ";
cout<<"高等数学成绩:"<<student[i].mathGrade<<endl;
cout<<"C++成绩:"<<student[i].cplusGrade<<" "<<"大学英语成绩:"<<student[i].enGrade<<endl;
}
}

// 主函数
int main()
{
Students students;
students.setdata();
char search;
int num;
do
{
cout<<"请输入要查询的学生的学号: ";
cin>>num;
students.search(num);
cout<<"是否继续查询?(y/n): ";
cin>>search;
} while (search == 'y');
students.allDisplay();
system("pause");
return 0;
}

实验报告五

1. 三角形

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
#include<iostream>
#include<math.h>
using namespace std;
class Ctriangle
{
public:
// 构造函数初始化
Ctriangle(int x, int y, int z):a(x), b(y), c(z) {}
void display() {cout<<"Ctriangle: a="<<a<<" b="<<b<<" c="<<c<<endl;}
void length();
void are();
private:
// 三条边的长度
int a;
int b;
int c;
// 周长、面积
int perimeter;
float area;
};
void Ctriangle::length ()
{
perimeter = a + b + c;
cout<<"Perimeter: "<<perimeter<<endl;
}
void Ctriangle::are()
{
float p;
// 这里要注意数据类型哦,若是 / 2,则 p 中存放的会是 int 型数据
p = (a + b + c) / 2.0;
area = pow(p * (p - a) * (p - b) * (p - c), 1.0 / 2);
cout<<"Area: "<<area<<endl;
}
// 主函数
int main()
{
int a, b, c;
cin>>a>>b>>c;
Ctriangle triangle1(a, b, c);
triangle1.display();
triangle1.length();
triangle1.are();
system("pause");
return 0;
}

2. 时钟

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
#include<iostream>
using namespace std;
class Clock
{
public:
// 构造函数初始化
Clock(int h, int m, int s):_hour(h), _minute(m), _second(s) {}
void setAlarm (int hour, int minute, int second)
{ h = hour; m = minute; s = second; }
void run();
void bell();
void display();
private:
// 当前时间
int _hour;
int _minute;
int _second;
// 闹钟时间
int h;
int m;
int s;
};
// 成员函数
void Clock::run()
{
while (true)
{
if (_hour < h)
{
_second++;
if (_second >= 60)
{
_second -= 60;
_minute++;
if (_minute >= 60)
{
_minute -= 60;
_hour++;
}
}
}
else if (_hour == h)
{
if (_minute < m)
{
_second++;
if (_second >= 60)
{
_second -= 60;
_minute++;
}
}
else if (_minute == m)
{
if (_second < s)
{
_second++;
}
else
{
bell();
break;
}
}
}
}
}
void Clock::bell()
{
cout<<"Plink!Plink!Plink!..."<<endl;
}
void Clock::display()
{
cout<<"Now: "<<_hour<<":"<<_minute<<":"<<_second<<endl;
}
// 主函数
int main()
{
Clock clock(7, 59, 57);
clock.display();
clock.setAlarm(8, 59, 57);
clock.run();
clock.display();
system("pause");
return 0;
}

3. 最长公共子串

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 上级5 - 3
// 困扰了我几小时的BUG: (分配内存这一块还是难点)
// - char *s 一开始没有具体指向,不能直接赋值,所以直接 gets(s) 是会出错的,先得动态申请内存
// - 数组的定义中不能使用变量,应该动态分配内存 (虽说也不会报错,但还是规范点好)
// - 二维数组中 i, j 位置别弄混了
// - s1[j] = '\0' 字符串结尾应加上结束符 (虽说一开始没加也没问题。。。)
// - strcpy(str.s, s1) 不是直接让 str.s = s1 ,这样在析构时会有问题
// - new 完是赋值的,不是让你改变指向的 (想着就好笑,刚 new 完就没了。。。)
// - String str3(maxsubstring(str1, str2)) 一定要调用复制构造函数的重载!默认是浅拷贝,类里有指针,需要深拷贝

#include<iostream>
#include<string.h>
using namespace std;
class String
{
public:
// 构造函数初始化
String() { len = 0; s = NULL; }
String(const String &s1) {
// 深拷贝
len = s1.len;
s = new char[s1.len + 1];
strcpy(s, s1.s);
}
// 析构函数
~String() { delete [] s; }
// 获取字符串
void getstring();
void display();
// 得最长公共子串对象
friend String maxsubstring(String &, String &);
private:
int len;
char *s;
};
// 成员函数
void String::getstring()
{
// 首先给指针 s 初始化地址
s = new char[200];
cout<<"输入字符串:";
// 读入一行
gets(s);
// 得到字符串长度
len = strlen(s);
}
void String::display()
{
// 定义为静态,方便计个数
static int n = 0;
n++;
// 即 s1,s2
if (n < 3) { cout<<" s"<<n<<"字符串="<<s<<" (长度为"<<len<<")"<<endl; }
// 即 s3
else
{
int i, j;
// 打印时两边的空格不要
for (i = 0, j = len - 1; i <= j;)
{
// 空格的 ASCII 码为 32
if (s[i] == 32) i++;
if (s[j] == 32) j--;
if (s[i] != 32 && s[j] != 32) break;
}
// 若只有空格,则打印长度为 0
if (i > j) cout<<" s1和s2最长公共字符串= (长度为0)"<<endl;
else
{
// 由于可能去掉了空格,所以字符串长度不能直接使用 len
cout<<" s1和s2最长公共字符串="<<s<<" (长度为"<<j - i + 1<<")"<<endl;
}
}
}
// 关键函数
String maxsubstring(String & str1, String & str2)
{
int x;
// 动态创建一个二维数组记录相同字母
// key: 利用矩阵算法,str1 为行,str2 为列
int **record = new int* [str1.len];
for (x = 0; x < str1.len; x++) record[x] = new int[str2.len];
int i, j;
// 最长相同字符串长度,初始为0
int max = 0;
// 最长相同字符串的最后一个字母位置
int last;
for (i = 0; i < str2.len; i++) // 遍历 str2
{
for (j = 0; j < str1.len; j++) // 遍历 str1
{
// 若找到相同字母
if (str2.s[i] == str1.s[j])
{
// 判断是否为第一行或列
if (i == 0 || j == 0)
{
record[j][i] = 1;
}
else
{
// 同时记录长度
record[j][i] = record[j - 1][i - 1] + 1;
}
}
// 若字母不同
else record[j][i] = 0;
// 最长相同字符串更新
if (max < record[j][i])
{
max = record[j][i];
last = j;
}
}
}
// 关键是得到 max 和 last,所以可以直接释放二维数组空间
for (x = 0; x < str1.len; x++) delete [] record[x];
delete [] record;
// 若没有相同字符串,则直接退出程序
if (max == 0)
{
cout<<"没有相同字符串!"<<endl;
exit(0);
}
// 动态创建一个指向数组的指针来获取最长相同字符串
// 因为对象里是 char* ,所以这里用 string 接收虽简单,但赋值又有问题,所以创建
char *s1 = new char[max + 1];
j = 0;
// 获取最长相同字符串
for (i = last - max + 1; i <= last; i++)
{
s1[j] = str1.s[i];
j++;
}
// 字符串最后加上 \0 表结束
s1[j] = '\0';
// 建立临时对象 (即返回的对象)
String str;
str.len = max;
str.s = new char[max + 1];
strcpy(str.s, s1);
// 复制完 s1 就没用了,要及时释放
delete [] s1;
// 返回 str 对象
return str;
}

// 主函数
int main()
{
String str1, str2;
str1.getstring();
str2.getstring();
str1.display();
str2.display();
// 调用复制构造函数的重载
String str3(maxsubstring(str1, str2));
str3.display();
system("pause");
return 0;
}

4. 信息管理

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
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
// 定义两个数组作为数据
// 姓名与密码一一对应
string user[] = {"xiao", "shuang", "Wangming", "Xiaoming"};
long pass[] = {20170000, 20170001, 20170002, 20170003};
string name;
cout<<"Input username: ";
cin>>name;
// 得到用户数 (毕竟是随机的)
int i, n;
for (i = 0; user[i] != ""; i++);
// 得到输入姓名在数组中的位置
for (int j = 0; j < i; j++)
{
if (user[j] == name)
{
n = j;
break;
}
}
long password;
cout<<"Input password: ";
cin>>password;
// 验证密码
if (password == pass[n])
{
cout<<"Success login!"<<endl;
}
else
{
cout<<"Default password!"<<endl;
}
system("pause");
return 0;
}

5. 课后习题

这个太琐碎了,就放在 gitee 上了
点此处跳转


实验报告六

1. 课后习题

这个太琐碎了,就放在 gitee 上了
点此处跳转


2. 建筑物

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
// 上级 6 - B

#include<iostream>
#include<string>
using namespace std;
class Building
{
public:
Building(string n, int f, int r, float a): name(n), floor(f), room(r), area(a) {}
void display()
{
cout<<"建筑物名称:"<<name<<endl;
cout<<"层数:"<<floor<<endl;
cout<<"房间数:"<<room<<endl;
cout<<"面积:"<<area<<endl;
}
private:
string name;
int floor;
int room;
float area;
};

class Teach_Building: public Building
{
public:
Teach_Building(string n, int f, int r, float a, string fun): Building(n, f, r, a), function(fun) {}
void display()
{
Building::display();
cout<<"建筑物功能:"<<function<<endl<<endl;
}
private:
string function;
};

class Dorm_Building: Building
{
public:
Dorm_Building(string n, int f, int r, float a, int p): Building(n, f, r, a), peoples(p) {}
void display()
{
Building::display();
cout<<"容纳人数:"<<peoples<<endl;
}
private:
int peoples;
};

int main()
{
Teach_Building t("4号楼", 5, 40, 4000, "计信教学楼");
Dorm_Building d("宿舍1栋", 7, 140, 1200, 500);
// 静态多态
t.display();
d.display();
return 0;
}

实验报告七

课后习题

难得全是课后习题
点此处跳转

实验报告八

1. 课后习题

这个太琐碎了,就放在 gitee 上了
点此处跳转

2. 工资管理系统

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// 老师的思维导图有bug。。。。。。幸好我有百度

#include<fstream>
#include<iostream>
#include<string>
using namespace std;
// 基类
class Person
{
public:
// 定义为虚函数
virtual void input() {}
friend void display(Person *p);
protected:
long number;
string name;
char sex;
int salary;
};
// 高校教师类
class Teacher:virtual public Person
{
public:
virtual void input();
void pay();
protected:
// 授课的时数
int teaching_hours;
// 职称
string title;
};
void Teacher::input()
{
cout<<"职工号:";
cin>>number;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"授课时长:";
cin>>teaching_hours;
int n;
cout<<"1-----------教授"<<endl;
cout<<"2-----------副教授"<<endl;
cout<<"3-----------讲师"<<endl;
cout<<"4-----------助教"<<endl;
cout<<"请选择职称:";
cin>>n;
if (n == 1) title = "教授";
if (n == 2) title = "副教授";
if (n == 3) title = "讲师";
if (n == 4) title = "助教";
// 要调用函数才能得到 salary 的值
}
void Teacher::pay()
{
if (title == "教授")
salary = teaching_hours * 80 + 6000;
else if (title == "副教授")
salary = teaching_hours * 70 + 5000;
else if (title == "讲师")
salary = teaching_hours * 60 + 4000;
else
salary = teaching_hours * 50 + 3000; //助教工资
}
// 行政员工类
class Staff:virtual public Person
{
public:
void input();
void pay();
protected:
// 职位
string position;
};
void Staff::input()
{
cout<<"职工号:";
cin>>number;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
int n;
cout<<"1-----------院级员工"<<endl;
cout<<"2-----------处级员工"<<endl;
cout<<"3-----------科级员工"<<endl;
cout<<"4-----------一般工作人员"<<endl;
cout<<"请选择职称:";
cin>>n;
if (n == 1) position = "院级员工";
if (n == 2) position = "处级员工";
if (n == 3) position = "科级员工";
if (n == 4) position = "一般工作人员";
}
void Staff::pay()
{
if (position == "院级员工")
salary = 6000;
else if (position == "处级员工")
salary = 5000;
else if (position == "科级员工")
salary = 4000;
else if (position == "一般工作人员")
salary = 3000;
}
// 临时工类
class Temporary_workers:public Person
{
public:
void input();
void pay();
protected:
// 时薪
int hourlyPay;
// 工作的时长
int workingHours;
};
void Temporary_workers::input()
{
cout<<"职工号:";
cin>>number;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"时薪:";
cin>>hourlyPay;
cout<<"工作时长:";
cin>>workingHours;
}
void Temporary_workers::pay()
{
salary = workingHours * hourlyPay;
}
// 双肩挑类
class Staff_Teacher:public Teacher, public Staff
{
public:
void input();
void pay();
};
void Staff_Teacher::input()
{
cout<<"职工号:";
cin>>number;
cout<<"姓名:";
cin>>name;
cout<<"性别:";
cin>>sex;
cout<<"授课时长:";
cin>>teaching_hours;
int n;
cout<<"1-----------教授"<<endl;
cout<<"2-----------副教授"<<endl;
cout<<"3-----------讲师"<<endl;
cout<<"4-----------助教"<<endl;
cout<<"请选择教师职称:";
cin>>n;
if (n == 1) title = "教授";
if (n == 2) title = "副教授";
if (n == 3) title = "讲师";
if (n == 4) title = "助教";
cout<<"1-----------院级员工"<<endl;
cout<<"2-----------处级员工"<<endl;
cout<<"3-----------科级员工"<<endl;
cout<<"4-----------一般工作人员"<<endl;
cout<<"请选择行政员工职称:";
cin>>n;
if (n == 1) position = "院级员工";
if (n == 2) position = "处级员工";
if (n == 3) position = "科级员工";
if (n == 4) position = "一般工作人员";
}
void Staff_Teacher::pay()
{
Staff::pay( );
if (title == "教授")
salary = teaching_hours * 80 + 6000 + salary * 0.5;
else if (title == "副教授")
salary = teaching_hours * 70 + 5000 + salary * 0.5;
else if (title == "讲师")
salary = teaching_hours * 60 + 4000 + salary * 0.5;
else
salary = teaching_hours *50 +3000 + salary * 0.5; //职称为助教的双肩挑人员
}
// 菜单
int menu()
{
int n;
cout<<"***************欢迎使用工资管理系统********************"<<endl;
cout<<"*** 1、教师 ***"<<endl;
cout<<"*** 2、一般职工 *** "<<endl;
cout<<"*** 3、临时工 *** "<<endl;
cout<<"*** 4、双肩挑教师 *** "<<endl;
cout<<"*** 0、退出 *** "<<endl;
cout<<"请选择:";
cin>>n;
return n;
}
// 打印
void display(Person *p)
{
// 打开文件
ofstream outfile("demo23.dat", ios::out |ios::app);
// 写入文件
outfile<<" 工号 姓名 性别 收入\n";
outfile<<" "<<p -> number<<" "<<p -> name<<" "<<p -> sex<<" "<<p -> salary<<"\n";
outfile<<" "<<"-------------------------------------------------------------\n";
// 在屏幕上显示
cout<<" 工号 姓名 性别 收入"<<endl;
cout<<" "<<p -> number<<" "<<p -> name<<" "<<p -> sex<<" "<<p -> salary<<endl;
cout<<" "<<"-------------------------------------------------------------"<<endl;
outfile.close();
}
// 主函数
int main()
{
int n;
// 定义不能在 switch 里面
Teacher t;
Staff s;
Temporary_workers t_w;
Staff_Teacher s_t;
do
{
n = menu();
switch(n)
{
case 1:
t.input();
t.pay();
// 基类指针指向派生类
display(&t);
break;
case 2:
s.input();
s.pay();
display(&s);
break;
case 3:
t_w.input();
t_w.pay();
display(&t_w);
break;
case 4:
s_t.input();
s_t.pay();
display(&s_t);;
break;
case 0:
cout<<"感谢使用!程序退出中~~~"<<endl;
break;
}
} while (n != 0);
system("pause");
return 0;
}

完结结课!

撒花撒花