-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
390 lines (346 loc) · 6.78 KB
/
test.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <array>
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <ostream>
#include <string>
#include <tuple>
using namespace std;
#define PI 3.1415
/* 1. C++ 基本语法
变量和数据类型
输入输出(cin 和 cout)
条件语句和循环 */
/* int main()
{
MyClass obj;
// obj.privateVar = 10; // 错误:private 成员不能在类外部访问
// obj.privateMethod(); // 错误:private 成员不能在类外部访问
obj.setPrivateVar(10); // 通过公共方法访问 private 成员
std::cout << "PrivateVar: " << obj.getPrivateVar() << std::endl;
// 计算Myclass对象的大小
std::cout << "Size of MyClass: " << sizeof(MyClass) << std::endl;
return 0;
}
*/
// 字符串链表
/* class Node
{
public:
string data;
Node *next;
Node(string data)
{
this->data = data;
this->next = NULL;
}
};
class LinkedList
{
public:
Node *head;
LinkedList()
{
this->head = NULL;
}
// 添加节点
void addNode(string data)
{
Node *newNode = new Node(data);
if (this->head == NULL)
{
this->head = newNode;
}
else
{
Node *temp = this->head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
}
// 打印链表
void display()
{
Node *temp = this->head;
while (temp != NULL)
{
cout << temp->data << "->";
temp = temp->next;
}
cout << "NULL";
cout << endl;
}
// 删除节点
void deleteNode(string data)
{
Node *temp = this->head;
Node *prev = NULL;
while (temp != NULL)
{
if (temp->data == data)
{
if (prev == NULL)
{
this->head = temp->next;
}
else
{
prev->next = temp->next;
}
delete temp;
return;
}
prev = temp;
temp = temp->next;
}
cout << "Node not found" << endl;
}
};
// 菜单
// 1.添加节点 --> 输入字符串
// 2.打印链表
// 0.退出
// 打印菜单
void menu()
{
cout << "1.Add Node" << endl;
cout << "2.Print List" << endl;
cout << "3.Delete Node" << endl;
cout << "0.Exit" << endl;
}
int main()
{
int choice;
LinkedList linkedList;
do
{
menu();
cin >> choice;
switch (choice)
{
case 1:
{
string data;
cout << "please enter a string" << endl;
cin >> data;
linkedList.addNode(data);
break;
}
case 2:
{
linkedList.display();
break;
}
case 3:
{
string data;
cout << "please enter a string" << endl;
cin >> data;
linkedList.deleteNode(data);
break;
}
case 0:
{
cout << "Exit" << endl;
break;
}
default:
{
cout << "invalid input" << endl;
break;
}
}
} while (choice != 0);
return 0;
}
*/
// 模板练习 不同游戏角色
// 返回最大值 请不要进行代码补全
template <typename T>
T Max(T a, T b)
{
return a > b ? a : b;
}
template <typename T>
void Swap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
// 模板类
template <typename T>
class MyContainer
{
public:
T data;
MyContainer(T d) : data(d) {}
void display()
{
cout << "Data: " << this->data << endl;
}
};
// 模板类数组
template <typename T, int SIZE>
class FixedArray
{
public:
T arr[SIZE];
void Set(int index, T value)
{
if (index >= 0 && index < SIZE)
{
arr[index] = value;
}
else
{
cout << "Index out of range" << endl;
}
}
T Get(int index)
{
if (index >= 0 && index < SIZE)
{
return arr[index];
}
return T();
}
int Size()
{
return SIZE;
}
};
// 模板异化
template <int SIZE>
class FixedArray<bool, SIZE>
{
public:
bool arr[SIZE];
void Set(int index, bool value)
{
if (index >= 0 && index < SIZE)
{
arr[index] = value;
}
else
{
cout << "Index out of range" << endl;
}
}
bool Get(int index)
{
if (index >= 0 && index < SIZE)
{
return arr[index];
}
return true;
}
int Size()
{
return SIZE;
}
};
// 模板异化
template <int SIZE>
class FixedArray<int, SIZE>
{
public:
int arr[SIZE];
void Set(int index, int value)
{
if (index >= 0 && index < SIZE)
{
arr[index] = value;
}
else
{
cout << "Index out of range" << endl;
}
}
int Get(int index)
{
if (index >= 0 && index < SIZE)
{
return arr[index];
}
return 1;
}
int Size()
{
return SIZE;
}
};
// 斐波那契数列 模板元编程练习
template <int N>
struct Fibonacci
{
enum {value = Fibonacci<N - 1>::value + Fibonacci<N - 2>::value};
};
// 模板异化 递归终止条件
template <>
struct Fibonacci<0>
{
enum {value = 0};
};
template <>
struct Fibonacci<1>
{
enum {value = 1};
};
// 类里面的static成员变量
class Entity
{
public:
static int x;
static int y;
static void Print()
{
cout << x << ", " << y << endl;
}
};
int Entity::x = 0;
int Entity::y = 0;
inline void Print(const string& str)
{
cout << str << endl;
}
class Player
{
private:
string name;
int hp;
int xp;
// 等级
int level;
public:
explicit Player(const string& name) : name(name), hp(100), xp(0), level(1) {}
// 打印个人信息
friend ostream& operator<<(ostream& os, const Player& player);
~Player() {cout << name << " is destroyed" << endl;}
};
ostream& operator<<(ostream& os, const Player& player)
{
os << "Name: " << player.name << endl;
os << "HP: " << player.hp << endl;
os << "XP: " << player.xp << endl;
os << "Level: " << player.level << endl;
return os;
}
string GiveName(const string& name)
{
string newName = "Mr." + name;
return newName;
}
int main()
{
Player player1("Shibo");
cout << player1 << endl;
//Entity e1;
//e1.x = 2;
//e1.y = 3;
//e1.Print();
//Entity::Print();
}