使用c++编写程序一、定义结构体类型: 每个同学的信息:学号、姓名(英文、无重名)、性别(英文)、三门课程成绩(语文、数学、英语) 二、功能要求: 运行后,显示如下信息,提示输入数字,并运行数字所对应的功能,如:按1,执行“显示所有同学的信息”;按6,执行“删除一个同学”。 1.显示所有同学的信息 2.查找指定姓名的信息 (模糊查找:输入姓名中任何一个或多个文字,比如:输入“zhang”,就显示所有姓名中含“zhang”的信息) 3.显示有不及格同学的信息 4.按指定课程排序输出 5.增加一个新同学 6.修改指定姓名的信息 7.删除指定姓名的信息 0.退出
时间: 2024-02-12 14:09:00 浏览: 75
好的,以下是使用 C++ 编写的实现代码:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义学生信息结构体类型
struct Student {
int id; // 学号
string name; // 姓名
string gender; // 性别
double chinese; // 语文成绩
double math; // 数学成绩
double english; // 英语成绩
};
// 定义学生信息数组类型
typedef vector<Student> StudentArray;
// 显示学生信息
void printStudent(const Student& student) {
cout << "学号:" << student.id << "\t姓名:" << student.name << "\t性别:" << student.gender << "\t语文成绩:" << student.chinese << "\t数学成绩:" << student.math << "\t英语成绩:" << student.english << endl;
}
// 显示所有学生信息
void printAllStudents(const StudentArray& students) {
for (const auto& student : students) {
printStudent(student);
}
}
// 查找学生信息
StudentArray findStudents(const StudentArray& students, const string& name) {
StudentArray result;
for (const auto& student : students) {
if (student.name.find(name) != string::npos) {
result.push_back(student);
}
}
return result;
}
// 显示不及格学生信息
void printFailedStudents(const StudentArray& students) {
for (const auto& student : students) {
if (student.chinese < 60 || student.math < 60 || student.english < 60) {
printStudent(student);
}
}
}
// 按指定课程排序输出
void sortByCourse(const StudentArray& students, int course) {
StudentArray sortedStudents = students;
switch (course) {
case 1:
sort(sortedStudents.begin(), sortedStudents.end(), [](const Student& a, const Student& b) { return a.chinese > b.chinese; });
break;
case 2:
sort(sortedStudents.begin(), sortedStudents.end(), [](const Student& a, const Student& b) { return a.math > b.math; });
break;
case 3:
sort(sortedStudents.begin(), sortedStudents.end(), [](const Student& a, const Student& b) { return a.english > b.english; });
break;
default:
cout << "无效的课程号" << endl;
return;
}
cout << "按课程" << course << "排序后的学生信息:" << endl;
printAllStudents(sortedStudents);
}
// 增加一个新学生
void addStudent(StudentArray& students) {
Student student;
cout << "请输入新学生信息:" << endl;
cout << "学号:";
cin >> student.id;
cout << "姓名:";
cin >> student.name;
cout << "性别:";
cin >> student.gender;
cout << "语文成绩:";
cin >> student.chinese;
cout << "数学成绩:";
cin >> student.math;
cout << "英语成绩:";
cin >> student.english;
students.push_back(student);
cout << "添加成功" << endl;
}
// 修改学生信息
void modifyStudent(StudentArray& students, const string& name) {
int count = 0;
for (auto& student : students) {
if (student.name == name) {
cout << "请输入新信息:" << endl;
cout << "学号:";
cin >> student.id;
cout << "姓名:";
cin >> student.name;
cout << "性别:";
cin >> student.gender;
cout << "语文成绩:";
cin >> student.chinese;
cout << "数学成绩:";
cin >> student.math;
cout << "英语成绩:";
cin >> student.english;
cout << "修改成功" << endl;
count++;
}
}
if (count == 0) {
cout << "未找到该学生信息" << endl;
}
}
// 删除学生信息
void deleteStudent(StudentArray& students, const string& name) {
int count = 0;
for (auto it = students.begin(); it != students.end(); ++it) {
if (it->name == name) {
students.erase(it);
cout << "删除成功" << endl;
count++;
break;
}
}
if (count == 0) {
cout << "未找到该学生信息" << endl;
}
}
int main() {
StudentArray students = {
{ 10001, "张三", "男", 90, 80, 70 },
{ 10002, "李四", "女", 85, 75, 95 },
{ 10003, "王五", "男", 70, 60, 80 },
{ 10004, "赵六", "女", 80, 90, 95 },
{ 10005, "钱七", "男", 65, 70, 75 }
};
int choice;
string name;
while (true) {
cout << "请选择功能:" << endl;
cout << "1.显示所有同学的信息" << endl;
cout << "2.查找指定姓名的信息" << endl;
cout << "3.显示有不及格同学的信息" << endl;
cout << "4.按指定课程排序输出" << endl;
cout << "5.增加一个新同学" << endl;
cout << "6.修改指定姓名的信息" << endl;
cout << "7.删除指定姓名的信息" << endl;
cout << "0.退出" << endl;
cin >> choice;
switch (choice) {
case 1:
printAllStudents(students);
break;
case 2:
cout << "请输入姓名:";
cin >> name;
printAllStudents(findStudents(students, name));
break;
case 3:
printFailedStudents(students);
break;
case 4:
int course;
cout << "请选择课程:" << endl;
cout << "1.语文" << endl;
cout << "2.数学" << endl;
cout << "3.英语" << endl;
cin >> course;
sortByCourse(students, course);
break;
case 5:
addStudent(students);
break;
case 6:
cout << "请输入姓名:";
cin >> name;
modifyStudent(students, name);
break;
case 7:
cout << "请输入姓名:";
cin >> name;
deleteStudent(students, name);
break;
case 0:
return 0;
default:
cout << "无效的选项" << endl;
break;
}
}
return 0;
}
```
运行程序后,按照提示输入数字即可执行不同的功能。
阅读全文