用c++编写代码实现 (1)录入每个学生基本信息:支持单个录入、批量录入。学生基本信息包括:学号、姓名、性别、身份证号、院系、班级、专业、籍贯、手机、E_mail、QQ 等信息。
时间: 2024-09-16 15:02:57 浏览: 102
在C++中,我们可以使用结构体定义学生的基本信息,并创建函数来实现单个录入和批量录入的功能。首先,定义一个学生信息结构:
```cpp
#include <iostream>
#include <vector>
using namespace std;
struct StudentInfo {
int student_id;
string name;
char gender; // 'M' or 'F'
string id_card;
string department;
string class_name;
string major;
string origin;
string phone;
string email;
string qq;
};
```
然后,可以创建两个函数,一个用于单个录入,一个用于批量录入:
```cpp
void singleEntry(StudentInfo& student) {
cout << "请输入学生信息:\n";
cin >> student.student_id >> student.name >> student.gender >> student.id_card >> student.department >> student.class_name >> student.major >> student.origin >> student.phone >> student.email >> student.qq;
}
void batchEntry(vector<StudentInfo>& students) {
int numStudents;
cout << "请输入学生人数: ";
cin >> numStudents;
for (int i = 0; i < numStudents; ++i) {
StudentInfo tempStudent;
cout << "请输入第" << i+1 << "位学生的信息:" << endl;
singleEntry(tempStudent);
students.push_back(tempStudent);
}
}
int main() {
vector<StudentInfo> studentList;
string choice;
do {
cout << "请选择录入方式:\n"
<< "1. 单个录入\n"
<< "2. 批量录入\n"
<< "0. 退出\n";
cin >> choice;
if (choice == "1") {
StudentInfo tempStudent;
singleEntry(tempStudent);
studentList.push_back(tempStudent); // 单个录入添加到列表
} else if (choice == "2") {
batchEntry(studentList);
} else if (choice != "0") {
cout << "无效选择,请重新输入。\n";
}
} while (choice != "0");
return 0;
}
```
在这个例子中,用户可以选择单个录入或批量录入,数据会存储在一个`vector<StudentInfo>`中。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)