-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
156 lines (156 loc) · 4.96 KB
/
main.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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student{
public:
string name;
int age{'0'},num{'0'};
string gender;
string address;
};
vector<Student> students; // 创建一个名为students的vector容器,其中存储的元素类型是Student类的对象
void addStudent() {
Student student;
cout << "请输入学生姓名:";
cin >> student.name;
cout << "请输入学生学号:";
cin >> student.num;
cout << "请输入学生年龄:";
cin >> student.age;
cout << "请输入学生性别:";
cin >> student.gender;
cout << "请输入学生地址:";
cin >> student.address;
students.push_back(student);
cout << "添加成功!" << endl;
}
void deleteStudent(){
bool flag = false;
char ch;
string name;
cout << "请输入要删除的学生姓名:";
cin >> name;
for (int i = 0; i < students.size(); i++){
if (students[i].name == name){
flag = true;
cout << "确认是否进行删除,请输入y/n.";
cin >> ch;
if (ch == 'y' || ch == 'Y'){
students.erase(students.begin() + i);
cout << "删除成功!" << endl;
}
else{
cout << "放弃本次删除操作" << endl;
}
}
if (!flag){
cout << "未找到该学生!" << endl;
break;
}
}
}
void modifyStudent(){
int type;
string name;
system("cls");
cout << "请输入要修改的学生姓名:";
cin >> name;
for (auto & student : students){ //遍历student容器中的元素 &表示引用传递,在遍历的过程中直接修改容器中元素的值
if (student.name == name){
cout << "-----------------------------------------------" << endl;
cout << " 1.姓名 2.学号 " << endl;
cout << " 3.性别 4.年龄 " << endl;
cout << " 5.地址 0.返回 " << endl;
cout << " 请输入相应编号做相应修改。 " << endl;
cout << "-----------------------------------------------" << endl;
cin >> type;
switch (type){
case 1:{
cout << "请输入学生新的姓名:";
cin >> student.name;
cout << "修改成功!" << endl;
break;
}
case 2:{
cout << "请输入学生新的学号:";
cin >>student.num;
cout << "修改成功!" << endl;
break;
}
case 3:{
cout << "请输入学生新的性别:";
cin >> student.gender;
cout << "修改成功!" << endl;
break;
}
case 4:{
cout << "请输入学生新的年龄:";
cin >> student.age;
cout << "修改成功!" << endl;
break;
}
case 5:{
cout << "请输入学生新的地址:";
cin >> student.address;
cout << "修改成功!" << endl;
break;
}
case 0:{
return;
}
default:
cout << "输入错误" << endl;
}
}
else cout << "未找到您所输入的学生。" << endl;
}
}
void queryStudent(){
string name;
system("cls");
cout << "请输入要查询的学生姓名:";
cin >> name;
for (auto & student : students){
if (student.name == name){
cout << "姓名:" << student.name << endl;
cout << "年龄:" << student.age << endl;
cout << "学号:" << student.num << endl;
cout << "性别:" << student.gender << endl;
cout << "地址:" << student.address << endl;
break;
} else cout << "未找到该学生!" << endl;
}
}
int main(){
while (true){
cout << "-----------------------------------------------" << endl;
cout << " 六年级二班学生信息系统 " << endl;
cout << " 1.添加学生信息 2.删除学生信息 " << endl;
cout << " 3.修改学生信息 4.查询学生信息 " << endl;
cout << " 0.退出系统 " << endl;
cout << " 请输入相应编号做相应修改。 " << endl;
cout << "-----------------------------------------------" << endl;
int n;
cin >> n;
switch (n){
case 0:
exit(0);
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
queryStudent();
break;
default:
cout << "输入错误!" << endl;
break;
}
}
}