C++学生管理系统

一、前言

该学生管理系统使用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;
}

三、结果

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值