有一学生成绩表,包括学号、姓名、3门课程成绩。请实现如下删除功能:输入一个学生的学号,删除该学生的所有信息。
时间: 2023-04-23 22:02:56 浏览: 231
可以先将学生成绩表存储在一个二维数组中,每一行代表一个学生的信息,包括学号、姓名、3门课程成绩。然后,输入要删除的学生的学号,遍历整个数组,找到该学生的信息所在的行,将该行从数组中删除即可。最后,输出删除后的学生成绩表。
相关问题
C++编译有一学生成绩表,包括学号、姓名、3门课程成绩。请实现如下删除功能:输入一个学生的学号,删除该学生的所有信息。
以下是一个简单的实现:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Student {
string id;
string name;
int score1;
int score2;
int score3;
};
int main() {
vector<Student> students;
while (true) {
cout << "请输入学生的学号、姓名、3门课程成绩(用空格隔开),输入exit结束输入:" << endl;
string id, name;
int score1, score2, score3;
cin >> id;
if (id == "exit") {
break;
}
cin >> name >> score1 >> score2 >> score3;
students.push_back({id, name, score1, score2, score3});
}
cout << "请输入要删除的学生的学号:" << endl;
string delete_id;
cin >> delete_id;
bool found = false;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == delete_id) {
students.erase(students.begin() + i);
found = true;
break;
}
}
if (found) {
cout << "删除成功!" << endl;
} else {
cout << "没有找到该学生的信息。" << endl;
}
cout << "学生信息如下:" << endl;
for (const auto& student : students) {
cout << student.id << " " << student.name << " " << student.score1 << " " << student.score2 << " " << student.score3 << endl;
}
return 0;
}
```
在这个程序中,我们使用了一个 `vector` 来存储多个学生的信息。在输入学生信息时,我们使用 `while` 循环不断读入学生信息,并将每个学生的信息存储在一个 `Student` 结构体中,然后将该结构体保存在 `students` 向量中。
当用户输入要删除的学生的学号后,我们遍历 `students` 向量,找到该学生的信息并将其删除。我们使用一个布尔变量 `found` 来表示是否找到了该学生的信息。如果找到了,我们输出删除信息成功的提示;否则,输出没有找到该学生的信息的提示。
最后,我们遍历 `students` 向量,输出所有学生的信息。
有一学生成绩表,包括学号、姓名、3门课程成绩。请实现如下查找功能:输入一个学生的学号,输出该学生学号、姓名、3门课程成绩
可以使用Python语言编写一个程序来实现这个功能。具体步骤如下:
1. 定义一个字典,用来存储学生的信息。字典的键为学号,值为一个列表,包含学号、姓名、3门课程成绩。
2. 通过输入函数获取用户输入的学号。
3. 在字典中查找该学号对应的信息,如果找到了,就输出该学生的学号、姓名、3门课程成绩;如果没有找到,就输出“未找到该学生”的提示信息。
下面是完整的Python代码实现:
```python
# 定义学生信息字典
students = {
'001': ['001', '张三', [80, 85, 90]],
'002': ['002', '李四', [75, 85, 95]],
'003': ['003', '王五', [90, 80, 85]],
'004': ['004', '赵六', [85, 90, 95]],
}
# 获取用户输入的学号
stu_id = input('请输入学生学号:')
# 在字典中查找学生信息
if stu_id in students:
stu_info = students[stu_id]
print('学号:', stu_info[])
print('姓名:', stu_info[1])
print('成绩:', stu_info[2])
else:
print('未找到该学生')
```
运行程序,输入学号001,输出结果如下:
```
请输入学生学号:001
学号: 001
姓名: 张三
成绩: [80, 85, 90]
```
输入学号005,输出结果如下:
```
请输入学生学号:005
未找到该学生
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)