学生信息排序
描述
请设计一个简单的学生成绩管理系统,要求系统实现以下功能:
插入一个学生的信息: Insert id name sex year month day x y z,
其中的参数分别为 学号、姓名、性别、出生日期(年、月、日)、三门课的成绩,成绩为浮点数。
List 输出所有学生信息。
查找学生信息:Find id 查找学号为id的学生信息。
Change id newname,newsex,newyear,newmonth,newday,newx,newy,newz
把学号为id的学生信息修改为 newname,newsex,newyear,newmonth,newday,newx,newy,newz(学号保持不变)
删除学生信息 Delete id
删除学号为id的学生信息
按学号从小到大排序
Sort byid
按出生日期从小到大排序
Sort bybirthday
按总成绩从小到大排序
Sort bysum
退出程序: Quit或者Exit
请注意:姓名的长度不超过20。
输入
输入有多行,每行一条指令,指令格式如下:
Insert id name sex year month day x y z
其中的参数分别为学号、姓名、性别、 出生日期(年、月、日)三门课的成绩,成绩为浮点数。
List 输出所有学生信息。
Find id 查找学号为 id 的学生信息。
Change id newname,newsex,newyear,newmonth,newday,newx,newy,newz
把学号为id的学生信息修改为 newname,newsex,newyear,newmonth,newday,newx,newy,newz(学号保持不变)
Delete id 删除学号为id的学生信息
Sort byid 按学号从小到大排序并输出
Sort bybirthday 按出生日期从小到大排序
Sort bysum 按总成绩从小到大排序并输出
Quit或者Exit 输出"Good bye!"后结束程序。
输出
输出有多行,对应命令的输出如下:
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"。
Change id newname,newsex,newyear,newmonth,newday,newx,newy,newz 第一行显示"Change:"。如果链表中不存在学号为id的学生,显示"Failed"。 否则修改该学生信息并在单独一行中显示该生信息,显示格式如List命令。
Delete id
第一行显示"Delete:"。如果链表中不存在学号为id的学生,显示"Failed"。 否则删除该学生信息并在单独一行中显示"Deleted"
Sort byid 按学号从小到大排序并输出,输出格式与List相同
Sort bybirthday 按出生日期从小到大排序,输出格式与List相同
Sort bysum 按总成绩从小到大排序并输出,输出格式与List相同。
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 Find 0911002 Find 0911003 Insert 0911001 zhangou M 1992 3 24 98 78 65 Insert 0911002 zhaoliu F 1993 8 8 97 90 55 Change 0911002 zhaoliu M 1990 9 9 90 91 92 Change 0911005 zhaoliu M 1990 9 9 90 91 92 Delete 0911001 Delete 0911006 Insert 0911005 Wangrong F 1990 12 12 68 56 100 Find 0911002 Sort byid Sort bybirthday Sort bysum 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 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 Change: 0911002 zhaoliu M 1990 9 9 90.0 91.0 92.0 91.0 273.0 Change: Failed Delete: Deleted Delete: Failed Insert: 0911005 Wangrong F 1990 12 12 68.0 56.0 100.0 74.7 224.0 Find: 0911002 zhaoliu M 1990 9 9 90.0 91.0 92.0 91.0 273.0 Sort: 0911002 zhaoliu M 1990 9 9 90.0 91.0 92.0 91.0 273.0 0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0 0911005 Wangrong F 1990 12 12 68.0 56.0 100.0 74.7 224.0 Sort: 0911002 zhaoliu M 1990 9 9 90.0 91.0 92.0 91.0 273.0 0911005 Wangrong F 1990 12 12 68.0 56.0 100.0 74.7 224.0 0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0 Sort: 0911003 Lisi F 1992 5 3 77.0 72.0 55.0 68.0 204.0 0911005 Wangrong F 1990 12 12 68.0 56.0 100.0 74.7 224.0 0911002 zhaoliu M 1990 9 9 90.0 91.0 92.0 91.0 273.0 Good bye!
提示
HINT 时间限制:200ms 内存限制:64MB
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>
struct Student {
std::string id, name, sex;
int year, month, day;
double x, y, z, ave, sum;
};
bool compareById(const Student& a, const Student& b) {
return a.id < b.id;
}
bool compareByBirthday(const Student& a, const Student& b) {
if (a.year != b.year) return a.year < b.year;
if (a.month != b.month) return a.month < b.month;
return a.day < b.day;
}
bool compareBySum(const Student& a, const Student& b) {
return a.sum < b.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 == "Change") {
std::string id;
std::cin >> id;
bool found = false;
for (auto& s : students) {
if (s.id == id) {
std::cin >> s.name >> s.sex >> s.year >> s.month >> s.day >> s.x >> s.y >> s.z;
s.sum = s.x + s.y + s.z;
s.ave = s.sum / 3;
std::cout << "Change:" << std::endl;
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 << "Change:" << std::endl << "Failed" << std::endl;
} else if (command == "Delete") {
std::string id;
std::cin >> id;
bool found = false;
for (auto it = students.begin(); it != students.end(); ++it) {
if (it->id == id) {
students.erase(it);
std::cout << "Delete:" << std::endl << "Deleted" << std::endl;
found = true;
break;
}
}
if (!found) std::cout << "Delete:" << std::endl << "Failed" << std::endl;
} else if (command == "Sort") {
std::string type;
std::cin >> type;
if (type == "byid") {
std::sort(students.begin(), students.end(), compareById);
} else if (type == "bybirthday") {
std::sort(students.begin(), students.end(), compareByBirthday);
} else if (type == "bysum") {
std::sort(students.begin(), students.end(), compareBySum);
}
std::cout << "Sort:" << 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 == "Quit" || command == "Exit") {
std::cout << "Good bye!" << std::endl;
break;
}
}
return 0;
}
307

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



