一、前言
该学生管理系统使用C++语言实现,重点使用了sort函数、迭代器、auto、基于范围的 for 循环等C++独有的特性,实现了创建学生管理系统、增删学生、查找学生、按照成绩、姓名进行排序的功能。
二、代码
Student.h
#pragma once
#include <string>
class Student
{
private:
std::string name;
std::string id;
std::string sex;
int age;
int math;
int chinese;
int english;
int avg;
public:
Student();
Student(std::string name, std::string id, std::string sex, int age, int math, int chinese, int english);
void showInfo();
std::string getName();
std::string getId();
int getMath();
int getChinese();
int getEnglish();
int getAvg();
char getNameFirst();
};
Student.cpp
#include "Student.h"
#include <iostream>
Student::Student() {
this->name = "unknown";
this->id = "unknown";
this->sex = "unknown";
this->age = 0;
this->math = 0;
this->chinese = 0;
this->english = 0;
this->avg = 0;
}
Student::Student(std::string name, std::string id, std::string sex, int age, int math, int chinese, int english) {
this->name = name;
this->id = id;
this->sex = sex;
this->age = age;
this->math = math;
this->chinese = chinese;
this->english = english;
this->avg = (math + chinese + english) / 3;
}
void Student::showInfo() {
std::cout << "name: " << this->name << " ";
std::cout << "id: " << this->id << " ";
std::cout << "sex: " << this->sex << " ";
std::cout << "age: " << this->age << " ";
std::cout << "math score: " << this->math << " ";
std::cout << "chinese score: " << this->chinese << " ";
std::cout << "english score: " << this->english << " ";
std::cout << "avg score:" << this->avg << " ";
std::cout << std::endl;
}
std::string Student::getName() {
return this->name;
}
std::string Student::getId() {
return this->id;
}
int Student::getMath() {
return this->math;
}
int Student::getChinese() {
return this->chinese;
}
int Student::getEnglish() {
return this->english;
}
int Student::getAvg() {
return this->avg;
}
char Student::getNameFirst() {
std::string str = this->getName();
return str[0];
}
studentSystem.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include "Student.h"
class stuSystem {
private:
std::vector<Student> stus;
static bool compareByMathScore(Student s1, Student s2);
static bool compareByChineseScore(Student s1, Student s2);
static bool compareByEnglishScore(Student s1, Student s2);
static bool compareByAvgScore(Student s1, Student s2);
static bool compareByName(Student s1, Student s2);
public:
void addStu(Student stu);
void delByName(std::string name);
void delById(std::string id);
void showAll();
void sortByMath();
void sortByChinese();
void sortByEnglish();
void sortByAvg();
void sortByName();
void findByName(std::string nameToFind);
};
void stuSystem::addStu(Student stu) {
stus.push_back(stu);
}
void stuSystem::delByName(std::string name) {
auto it = stus.begin();
while (it != stus.end()) {
if (it->getName() == name) {
it = stus.erase(it);
}
else {
++it;
}
}
}
void stuSystem::delById(std::string id) {
auto it = stus.begin();
while (it != stus.end()) {
if (it->getId() == id) {
it = stus.erase(it);
}
else {
++it;
}
}
}
void stuSystem::showAll() {
int nums = 0;
for (auto elem : stus) {
++nums;
std::cout << nums << ". ";
elem.showInfo();
}
std::cout << std::endl;
}
bool stuSystem::compareByMathScore(Student s1, Student s2) {
return s1.getMath() > s2.getMath();
}
bool stuSystem::compareByChineseScore(Student s1, Student s2) {
return s1.getChinese() > s2.getChinese();
}
bool stuSystem::compareByEnglishScore(Student s1, Student s2) {
return s1.getEnglish() > s2.getEnglish();
}
bool stuSystem::compareByAvgScore(Student s1, Student s2) {
return s1.getAvg() > s2.getAvg();
}
bool stuSystem::compareByName(Student s1, Student s2) {
return s1.getNameFirst() < s2.getNameFirst();
}
void stuSystem::sortByMath() {
std::sort(stus.begin(), stus.end(), compareByMathScore);
}
void stuSystem::sortByChinese() {
std::sort(stus.begin(), stus.end(), compareByChineseScore);
}
void stuSystem::sortByEnglish() {
std::sort(stus.begin(), stus.end(), compareByEnglishScore);
}
void stuSystem::sortByAvg() {
std::sort(stus.begin(), stus.end(), compareByAvgScore);
}
void stuSystem::sortByName() {
std::sort(stus.begin(), stus.end(), compareByName);
}
void stuSystem::findByName(std::string nameToFind) {
int findFlag = 0;
for (auto elem : stus) {
if (elem.getName() == nameToFind) {
std::cout << "find!" << std::endl;
elem.showInfo();
findFlag = 1;
}
}
if (findFlag == 1) {
std::cout << std::endl;
return;
}
else {
std::cout << nameToFind << " Not Found!" << std::endl;
}
}
int main(void) {
stuSystem SMS;
SMS.addStu(Student("jack", "0001", "male", 18, 80, 85, 90));
SMS.addStu(Student("mike", "0002", "male", 19, 90, 90, 90));
SMS.addStu(Student("emma", "0003", "female", 20, 85, 88, 92));
SMS.addStu(Student("lisa", "0004", "female", 19, 92, 85, 90));
SMS.addStu(Student("chris", "0005", "male", 21, 78, 82, 88));
SMS.addStu(Student("jenny", "0006", "female", 20, 86, 90, 85));
SMS.addStu(Student("david", "0007", "male", 19, 85, 88, 85));
SMS.addStu(Student("sophia", "0008", "female", 22, 90, 92, 88));
SMS.addStu(Student("kevin", "0009", "male", 20, 82, 85, 80));
SMS.addStu(Student("sara", "0010", "female", 21, 88, 90, 92));
SMS.showAll();
//删除模块
SMS.delByName("lisa");
SMS.showAll();
SMS.delById("0002");
SMS.showAll();
//排序模块
SMS.sortByAvg();
SMS.showAll();
SMS.sortByChinese();
SMS.showAll();
SMS.sortByEnglish();
SMS.showAll();
SMS.sortByMath();
SMS.showAll();
SMS.sortByName();
SMS.showAll();
SMS.findByName("david");
return 0;
}