C++编写学生信息管理系统

C++编写学生信息管理系统

Zephyr

前言

这个是我的一个大作业,记录下这个大作业的源代码。

题目要求

1.功能内容必须包含以下内容:

​ 对记录进行增加、删除、查询、修改、浏览等操作。

2.编程使用的技术必须包含以下C++语言知识点:

​ (1) 变量的使用

​ (2) if语句

​ (3) switch case分支语句的使用

​ (4) 循环结构

​ (5) 函数定义及调用

​ (6) 类的定义及用法

代码缺点:

因为本人技术问题,添加学生信息后就只是一次性的,并没有储存到本地,所以一旦关闭系统后所添加的信息都会消失。

交互界面截图:

1.主界面:

2.添加学生信息:

3.查询学生信息:

(1)查询结果:

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
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
#include "iostream"
#include "string"

#define M 60
class Student
{
private:
std::string num;
std::string name;
int age;
std::string sex;
public:
explicit Student(std::string ="",std::string ="",int = 10,std::string = "男");
std::string getNum();
std::string getName();
void setName(std::string n);
void setSex(char s);
void setAge(int a);
void setNum(std::string u);
void print();
};

void addStudent(Student *s ,int &count);
void deleteStudent(Student *s ,int &count);
void updateStudent(Student *s ,int count);
void searchStudent(Student *s ,int count);
int menu();

Student::Student(std::string n1,std::string n2,int a,std::string s)
{
age = a;
num = std::move(n1);
name = std::move(n2);
sex = std::move(s);
}

void Student::print() {
std::cout << "学生名字:" << name << " " << "学号:" << num << " " << "年龄:" << age << " " << "性别:" << sex << " " << std::endl;
}

void Student::setNum(std::string u) {num = std::move(u);}
void Student::setName(std::string n) {name=std::move(n);}
void Student::setAge(int a) {age = a;}
void Student::setSex(char s) {sex=s;}
std::string Student::getName() {return name;}
std::string Student::getNum() {return num;}

int menu()
{
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::endl;
std::cout<< " 六年级二班学生信息系统 " << std::endl;
std::cout << " 1.添加学生信息 2.查询学生信息 " << std::endl;
std::cout << " 3.修改学生信息 4.删除学生信息 " << std::endl;
std::cout << " 0.退出系统 " << std::endl;
std::cout << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cout << " 请输入0-4进行相应操作 " << std::endl;
int n;
std::cin >> n;
return n;
}

void addStudent(Student *s,int &count)
{
int age;
std::string num,name;
std::string sex;
system("cls");
std::cout << "请输入学生信息:" << std::endl;
std::cout << "学生姓名:";
std::cin >> name;
std::cout << "学号:";
std::cin >> num;
std::cout << "性别:";
std::cin >> sex;
std::cout << "年龄:";
std::cin >> age;
s[count]=Student(num,name,age,sex);
count++;
std::cout << "添加成功" << std::endl;
}

void deleteStudent(Student *s,int &count)
{
int i, j, flag;
char ch;
flag = 0;
std::string newname;
system("cls");
std::cout << "请输入删除姓名:";
std::cin >> newname;
for (i = 0; i < count; i++) {
if (s[i].getName() == newname) {
flag = 1;
std::cout << "学号:" << i+1 << std::endl;
s[i].print();
std::cout << " 确认是否进行删除,请输入y/n.";
std::cin >> ch;
if (ch == 'y' || ch == 'Y')
{
for (j=i+1; j<count; j++)
s[j-1] = s[j];
count--;
std::cout << "删除成功" << std::endl;
}
else
{
std::cout << "放弃本次删除操作" << std::endl;
}

}
if (flag == 0) {
std::cout << "不存在符合条件的学生信息!" << std::endl;
break;
}
}
}

void updateStudent(Student *s,int count)
{
int i,type;
std::string num;
system("cls");
std::cout << "请输入修改学生学号:" << std::endl;
std::cin >> num;
for ( i = 0; i < count && (s[i].getNum()!= num); i++);
if (i < count )
{
s[i].print();
std::cout << "-----------------------------------------------" << std::endl;
std::cout << " " << std::endl;
std::cout << " 1.姓名 2.学号 " << std::endl;
std::cout << " 3.性别 4.年龄 " << std::endl;
std::cout << " 0.返回 " << std::endl;
std::cout << " 请输入相应编号做相应修改。 " << std::endl;
std::cout << " " << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cin >> type;
switch (type)
{
case 1:
{
std::string newname;
std::cout << "请输入新的姓名:" << std::endl;
std::cin >> newname;
s[i].setName(newname);
std::cout << "修改成功" << std::endl;
break;
}
case 2:
{
std::string newnum;
std::cout << "请输入新的学号:" << std::endl;
std::cin >> newnum;
s[i].setNum(newnum);
std::cout << "修改成功" << std::endl;
break;
}
case 3:
{
char newsex;
std::cout << "请输入新的性别(F:女 f:男):" << std::endl;
std::cin >> newsex;
s[i].setSex(newsex);
std::cout << "修改成功" << std::endl;
break;
}
case 4:
{
int newage;
std::cout << "请输入新的年龄:" << std::endl;
std::cin >> newage;
s[i].setAge(newage);
std::cout << "修改成功" << std::endl;
break;
}
case 0:
{
return;
}
default:
std::cout << "输入错误" << std::endl;
}
}
else std::cout << "不存在符合条件的学生记录" << std::endl;
}

void searchStudent(Student *s,int count)
{
int type,flag;
system("cls");
while (1)
{
std::cout << "-----------------------------------------------" << std::endl;
std::cout << std::endl;
std::cout << " 1.按姓名查询 2.按学号查询 " << std::endl;
std::cout << " 0.返回 " << std::endl;
std::cout << std::endl;
std::cout << " 请输入0至2 " << std::endl;
std::cout << "-----------------------------------------------" << std::endl;
std::cin >> type;
switch (type)
{
case 1:
{
std::string newname;
std::cout << "请查询输入姓名:";
std::cin >> newname;
flag = 0;
for (int i = 0; i < count; i++)
{
if (s[i].getName() == newname)
{
s[i].print();
flag = 1;
}
}
if (flag == 0)
std::cout << "不存在符合条件的学生" << std::endl;
break;
}
case 2:
{
std::string newnum;
std::cout << "请输入学号:";
std::cin >> newnum;
flag = 0;
for (int i = 0; i < count ;i++)
{
if (s[i].getNum() == newnum)
{
s[i].print();
flag = 1;
}
}
if (flag == 0)
std::cout << "不存在符合条件的学生" << std::endl;
break;
}
case 0:
return;

default: std::cout << "输入错误 " << std::endl;
break;
}
}
}

int main()
{
Student s[M];
int count = 0;
while (1)
{
switch(menu())
{
case 1:
addStudent(s,count);
break;
case 2:
searchStudent(s,count);
break;
case 3:
updateStudent(s,count);
break;
case 4:
deleteStudent(s,count);
break;
case 0:
exit(0);
default:
std::cout << "输入错误,请重新输入!" << std::endl;
}
}
}

文章更新:

时间:2023年2月13日18点10分

卧槽,刚才让chatGPT写我的C++大作业。代码写的比我好几百倍 ,代码原本是用结构体,我把结构体换成类再作修改就符合题目要求,且使用效果更好。代码写的比我好几百倍,看的我人都傻了,让我大受震撼。主要是代码里对vector和Range-based for loop的使用能够大大简化代码(相对于我写的)。简化内存管理、提高代码效率、提供高效的插入和删除操作,并且易于使用和理解。

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;
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){
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;
}
}
}