查找学生信息
描述
请设计一个简单的学生成绩管理系统,要求系统实现以下功能:
插入学生信息: Insert id name sex year month day x y z, 其中的参数分别为学号、姓名、性别、 出生日期(年、月、日)、三门课的成绩,成绩为浮点数。
按输入顺序输出学生信息 List
查找学生信息: Find id 查找学号为id的学生信息。
退出程序: Quit或者Exit
要求:(1)编写查找学生信息函数 请注意:姓名的长度不超过20。
输入
输入有多行,每行一条指令,指令格式如下:
Insert id name sex year month day x y z 其中的参数分别为学号、姓名、性别、 出生日期(年、月、日)三门课的成绩,成绩为浮点数。
List 按输入顺序输出所有学信息。
Find id 查找学号为 id 的学生信息。
Quit或者Exit 输出"Good bye!"后结束程序。
请注意:姓名的长度不超过20。
输出
输出有多行,对应命令的输出如下:
Insert id name sex year month day x y z 插入后在单独的一行中输出"Insert:", 如果链表中不存在相同学号的学生信息,在第二行中显示学生信息, 格式: id name sex year month day x y z ave sum,分别为学号、姓名、性别、 出生日期(年、月、日)和三门课(语文、数学、英语)的成绩,平均成绩和总成绩, 数据之间用一个空格分开,成绩保留1位小数。 如果已经有相同学号的学生信息,插入不成功,输出"Failed"
List 输出"List:"后,按输入顺序输出所有学生的信息,格式与插入学生信息后输出的格式相同。
Find id 第一行显示”Find:",第二行显示格式如下: 如果找到学号为id的学生,则在 单独一行中显示学生信息,格式如List。否则在单独一行显示"Failed"。
Quit或者Exit 在单独一行中输出"Good bye!"后结束程序。
输入样例 1
Insert 0911001 zhangsan F 1992 3 24 87 78 65 Insert 0911003 Lisi F 1992 5 3 77 72 55 List Find 0911002 Find 0911003 Insert 0911001 zhangou M 1992 3 24 98 78 65 Insert 0911002 zhaoliu F 1993 8 8 97 90 55 Insert 0911005 Wangrong F 1990 12 12 68 56 100 Quit
输出样例 1
Insert: 0911001 zhangsan F 1992 3 24 87.0 78.0 65.0 76.7 230.0 Insert: 0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0 List: 0911001 zhangsan F 1992 3 24 87.0 78.0 65.0 76.7 230.0 0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0 Find: Failed Find: 0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0 Insert: Failed Insert: 0911002 zhaoliu F 1993 8 8 97.0 90.0 55.0 80.7 242.0 Insert: 0911005 Wangrong F 1990 12 12 68.0 56.0 100.0 74.7 224.0 Good bye!
提示
HINT 时间限制:200ms
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
struct Student {
std::string id, name, sex;
int year, month, day;
double x, y, z, ave, sum;
};
int main() {
std::vector<Student> students;
std::string command;
while (std::cin >> command) {
if (command == "Insert") {
Student student;
std::cin >> student.id >> student.name >> student.sex >> student.year >> student.month >> student.day >> student.x >> student.y >> student.z;
student.sum = student.x + student.y + student.z;
student.ave = student.sum / 3;
bool exists = false;
for (const auto& s : students) {
if (s.id == student.id) {
exists = true;
break;
}
}
if (!exists) {
students.push_back(student);
std::cout << "Insert:" << std::endl;
std::cout << std::fixed << std::setprecision(1)
<< student.id << " " << student.name << " " << student.sex << " "
<< student.year << " " << student.month << " " << student.day << " "
<< student.x << " " << student.y << " " << student.z << " "
<< student.ave << " " << student.sum << std::endl;
} else {
std::cout << "Insert:" << std::endl << "Failed" << std::endl;
}
} else if (command == "List") {
std::cout << "List:" << std::endl;
for (const auto& s : students) {
std::cout << std::fixed << std::setprecision(1)
<< s.id << " " << s.name << " " << s.sex << " "
<< s.year << " " << s.month << " " << s.day << " "
<< s.x << " " << s.y << " " << s.z << " "
<< s.ave << " " << s.sum << std::endl;
}
} else if (command == "Find") {
std::string id;
std::cin >> id;
std::cout << "Find:" << std::endl;
bool found = false;
for (const auto& s : students) {
if (s.id == id) {
std::cout << std::fixed << std::setprecision(1)
<< s.id << " " << s.name << " " << s.sex << " "
<< s.year << " " << s.month << " " << s.day << " "
<< s.x << " " << s.y << " " << s.z << " "
<< s.ave << " " << s.sum << std::endl;
found = true;
break;
}
}
if (!found) std::cout << "Failed" << std::endl;
} else if (command == "Quit" || command == "Exit") {
std::cout << "Good bye!" << std::endl;
break;
}
}
return 0;
}
如有侵权,联系删除
1万+

被折叠的 条评论
为什么被折叠?



