顺序表练习——C++学生信息管理系统
【摘要】 C++顺序表简单实现学生信息管理系统,未加入文件输入输出流,界面较简单,编译通过
#include<stdlib.h>
#include<string>
#include<iostream>
#include<iomanip>
using namespace std;
#define MAXSIZE 100
#defi...
C++顺序表简单实现学生信息管理系统,未加入文件输入输出流,界面较简单,编译通过
#include<stdlib.h>
#include<string>
#include<iostream>
#include<iomanip>
using namespace std;
#define MAXSIZE 100
#define OVERFLOW -2
#define ERROR -1
#define OK 1
typedef int Status;
typedef struct {
string Name; // 姓名
string Num; // 学号
string Sex; // 性别
int Age; // 年龄
string Tele; // 电话
string Addr; // 地址
float Score; //3 成绩
}StudentList;
typedef StudentList Elemtype;
typedef struct {
Elemtype* elem; int length; // 顺序表长度
}SqList;
// 重载输入运算符
istream& operator >>(istream& in, Elemtype& Stu)
{
cout << "姓名:";
in >> Stu.Name;
cout << "学号:";
in >> Stu.Num;
cout << "性别:";
in >> Stu.Sex;
cout << "年龄:";
in >> Stu.Age;
cout << "电话:";
in >> Stu.Tele;
cout << "地址:";
in >> Stu.Addr;
cout << "成绩:";
in >> Stu.Score;
return in;
}
// 重载输出运算符
ostream& operator <<(ostream& out, Elemtype& Stu)
{
out << Stu.Name << Stu.Num << Stu.Sex << Stu.Age << Stu.Tele << Stu.Addr << Stu.Score << endl;
return out;
}
// 初始化顺序表
Status InitList_Sq(SqList& L)
{
L.elem = new Elemtype[MAXSIZE];
if (!L.elem) exit(OVERFLOW);
L.length = 0;
cout << "初始化成功!" << endl;
return OK;
}
// 销毁顺序表
void DestroyList(SqList& L)
{
if (!L.elem)
delete[] L.elem;
cout << "销毁成功!" << endl;
}
// 清空顺序表
void ClearList(SqList& L)
{
L.length = 0;
cout << "清空成功!" << endl;
}
// 返回顺序表的长度
void GetLength(SqList L)
{
cout << "顺序表的长度为:" << L.length << endl;
}
// 判断顺序表是否为空
int IsEmpty(SqList L)
{
if (!L.elem) {
cout << "顺序表为空!" << endl;
return 1;
}
else {
cout << "顺序表非空!" << endl;
return 0;
}
}
// 创建顺序表
Status CreateList(SqList& L, int length)
{ // cout << "请输入顺序表的元素:";
int i;
if (length < 0) return ERROR;
if (length >= MAXSIZE) return OVERFLOW;
for (i = 1; i < length + 1; i++)
{
cout << "请输入第" << i << "个学生的信息:" << endl;
cin >> L.elem[i - 1];
}
L.length = length;
cout << "创建成功!" << endl;
return OK;
}
// 输出顺序表
void PrintList(SqList L)
{
int i;
cout << setw(10) << "姓名" << setw(10) << "学号" << setw(10) << "性别" <<setw(10) << "年龄" << setw(15) << "电话" << setw(28) << "地址" << setw(15) << "成绩" << endl;
for (i = 0; i < L.length; i++)
cout << setw(10) << L.elem[i].Name << setw(10) <<L.elem[i].Num
<< setw(10) << L.elem[i].Sex << setw(10) << L.elem[i].Age << setw(15) << L.elem[i].Tele << setw(28) << L.elem[i].Addr << setw(15) << L.elem[i].Score << endl;
cout << endl;
}
// 按学号排序
void SequenceList(SqList& L)
{
Elemtype Temp;
// 冒泡排序
for (int i = 0; i < L.length - 1; i++)
{
for (int j = 0; j < L.length - 1; j++) if (L.elem[j].Num > L.elem[j + 1].Num) { Temp = L.elem[j]; L.elem[j] = L.elem[j + 1]; L.elem[j + 1] = Temp; }
}
cout << "排序成功!" << endl;
}
// 删除顺序表中的某个元素,并将其保存在e中
Status DeleteList(SqList& L, Elemtype& e)
{
int i;
cout << "要删除元素的位置:";
cin >> i;
if (i < 1 || i > L.length) return ERROR;
e = L.elem[i - 1];
int j;
for (j = i; j < L.length; j++)
L.elem[j - 1] = L.elem[j];
L.length--;
cout << "删除成功!" << endl;
return OK;
}
// 在顺序表中插入元素
Status InsertList(SqList& L)
{
if (L.length == MAXSIZE) {
cout << "溢出!" << endl;
return OVERFLOW;
}
int i;
Elemtype e;
cout << "请输入您要插入的位置:";
cin >> i;
if (i < 1 || i > L.length + 1) {
cout << "输入错误!" << endl;
return ERROR;
}
cout << "请输入学生信息:" << endl;
cout << "姓名:";
cin >> e.Name;
cout << "学号:";
cin >> e.Num;
cout << "性别:";
cin >> e.Sex;
cout << "年龄:";
cin >> e.Age;
cout << "电话:";
cin >> e.Tele;
cout << "地址:";
cin >> e.Addr;
cout << "成绩:";
cin >> e.Score;
Elemtype* p, * q;
q = &(L.elem[i - 1]);
for (p = &(L.elem[L.length - 1]); p >= q; p--)
* (p + 1) = *p;
*q = e;
++L.length;
cout << "插入成功!" << endl;
return OK;
}
// 修改顺序表元素
void ChangeList(SqList& L)
{
string num;
int i;
cout << "请输入要修改的学生的学号:";
cin >> num;
for (i = 0; i < L.length; i++)
{
if (L.elem[i].Num == num)
{ cout << "姓名:"; cin >> L.elem[i].Name; cout << "学号:"; cin >> L.elem[i].Num; cout << "性别:"; cin >> L.elem[i].Sex; cout << "年龄:"; cin >> L.elem[i].Age; cout << "电话:"; cin >> L.elem[i].Tele; cout << "地址:"; cin >> L.elem[i].Addr; cout << "成绩:"; cin >> L.elem[i].Score; cout << "修改成功!" << endl;
}
}
if (i == L.length)
cout << "查无此人!" << endl;
}
// 按学号查找
void SearchList_Num(SqList& L)
{
string Num;
int i;
cout << "请输入要查找的学生的学号:";
cin >> Num;
for (i = 0; i < L.length; i++)
{
if (L.elem[i].Num == Num)
{ cout << setw(10) << "姓名" << setw(10) << "学号" << setw(10) << "性别" << setw(10) << "年龄" << setw(15) << "电话" << setw(28) << "地址" << setw(15) << "成绩" << endl; cout << setw(10) << L.elem[i].Name << setw(10) << L.elem[i].Num << setw(10) << L.elem[i].Sex << setw(10) << L.elem[i].Age << setw(15) << L.elem[i].Tele << setw(28) << L.elem[i].Addr << setw(15) << L.elem[i].Score << endl; break;
}
}
if(i == L.length)
cout << "查无此人!" << endl;
}
// 按姓名查找
void SearchList_Name(SqList& L)
{
string Name;
int i;
cout << "请输入要查找的学生的姓名:";
cin >> Name;
for (i = 0; i < L.length; i++)
{
if (L.elem[i].Name == Name)
{ cout << setw(10) << "姓名" << setw(10) << "学号" << setw(10) << "性别" << setw(10) << "年龄" << setw(15) << "电话" << setw(28) << "地址" << setw(15) << "成绩" << endl; cout << setw(10) << L.elem[i].Name << setw(10) << L.elem[i].Num << setw(10) << L.elem[i].Sex << setw(10) << L.elem[i].Age << setw(15) << L.elem[i].Tele << setw(28) << L.elem[i].Addr << setw(15) << L.elem[i].Score << endl; break;
}
}
if(i == L.length)
cout << "查无此人!" << endl;
}
// 按电话查找
void SearchList_Tele(SqList& L)
{
string Tele;
int i;
cout << "请输入要查找的学生的电话:";
cin >> Tele;
for (i = 0; i < L.length; i++)
{
if (L.elem[i].Tele == Tele)
{ cout << setw(10) << "姓名" << setw(10) << "学号" << setw(10) << "性别" << setw(10) << "年龄" << setw(15) << "电话" << setw(28) << "地址" << setw(10) << "成绩" << endl; cout << setw(10) << L.elem[i].Name << setw(10) << L.elem[i].Num << setw(10) << L.elem[i].Sex << setw(10) << L.elem[i].Age << setw(15) << L.elem[i].Tele << setw(28) << L.elem[i].Addr << setw(10) << L.elem[i].Score << endl; break;
}
}
if(i == L.length)
cout << "查无此人!" << endl;
}
int main()
{
SqList L;
int p = 0, i = 0;
// int n, i;
int Length;
Elemtype e;
cout << "请输入顺序表的长度:";
cin >> Length;
/* // test
// InitList_Sq(L);
// cout << "请输入顺序表的长度:";
// cin >> length;
// CreateList(L, Length);
// 输出测试
// PrintList(L);
// 排序测试
// SequenceList(L);
// PrintList(L);
// 删除测试
// cin >> i;
// DeleteList(L, e);
// cout << e.Name << e.Num << e.Sex << e.Age << e.Tele << e.Addr << e.Score << endl;
// PrintList(L);
// 插入测试
// InsertList(L);
// PrintList(L);
// cout << "顺序表的长度为:" << GetLength(L) << endl; // 修改测试
// ChangeList(L);
// PrintList(L);
// 按学号查找测试
// SearchList_Num(L);
// 按姓名查找测试
// SearchList_Name(L);
// 按电话查找
// SearchList_Tele(L);
*/
system("cls");
while (1)
{
cout << endl << "1. 初始化顺序表" << 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 << "11. 清空顺序表" << endl;
cout << "12. 销毁顺序表" << endl;
cout << "0. 退出" << endl;
cout << endl << "请输入您的选择:";
cin >> p;
system("cls");
switch (p)
{
case 0: exit(-2);
case 1: InitList_Sq(L); break; case 2: CreateList(L, Length);break;
case 3: PrintList(L); break; case 4: SequenceList(L); break;
case 5: InsertList(L); break;
case 6: DeleteList(L, e); break;
case 7: ChangeList(L); break;
case 8: SearchList_Num(L); break;
case 9: SearchList_Name(L); break;
case 10: SearchList_Tele(L); break;
case 11: ClearList(L); break;
case 12: DestroyList(L); break;
default: cout << "输入错误!" << endl;
}
cout << endl << "输入1返回主界面 \n输入0退出" << endl;
cin >> i;
switch (i)
{
case 1: system("cls"); break;
case 0: exit(-2); break;
}
}
cout << endl;
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
- 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
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
文章来源: ruochen.blog.csdn.net,作者:若尘,版权归原作者所有,如需转载,请联系作者。
原文链接:ruochen.blog.csdn.net/article/details/102060439
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)