学生成绩管理系统

时间:2022-11-13 22:14:28
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<map>
#include<cstdlib>
#include<sstream>
#include<algorithm>
using namespace std;
struct student{
long long Id;
string Name;
double Dorm;
int Rank;
};
class Student{
private:
vector<student> S;
static int count;
public:
Student();
void insert();
void Sort(char X);
void Print();
void Search();
void Edit();
void Erase();
void Num();
};
int Student::count = 0;


Student::Student()
{
ifstream File("C:\\Users\\DELL\\Desktop\\Student.txt", ios::in);
string line;
while (getline(File, line)){
stringstream input(line);
student s;
input >> s.Id >> s.Name >> s.Dorm;
this->S.push_back(s);
Student::count++;
}
this->Sort('C');
File.close();
}



void Student::insert()
{
cout << "请依次输入该生的学号,姓名,分数" << endl;
student s;
cin >> s.Id >> s.Name >> s.Dorm;
this->S.push_back(s);
Student::count++;
}
struct RuleId{
bool operator ()(const student& s1, const student & s2){
return s1.Id < s2.Id;
}
};
struct RuleName{
bool operator ()(const student& s1, const student & s2){
return s1.Name < s2.Name ;
}
};
struct RuleDorm{
bool operator ()(const student& s1, const student & s2){
return s1.Dorm > s2.Dorm ;
}
};
void Student::Sort(char X='0')
{


char G;
if (X == '0'){
cout << "请选择排序的关键字" << endl;
cout << "A:学号" << endl;
cout << "B:姓名" << endl;
cout << "C:分数" << endl;
cin >> G;
}

else{
G = X;
}
switch (G){
case('A') : {
sort((this->S).begin(), (this->S).end(), RuleId());
break;
}
case('B'):{
sort((this->S).begin(), (this->S).end(), RuleName());
break;
}
case('C') : {
sort((this->S).begin(), (this->S).end(), RuleDorm());
int rank = 0;
for (auto it = (this->S).begin(); it != (this->S).end(); ++it){
rank++;
it->Rank = rank;
}

break;
}
}
}

void Student::Print()
{
ofstream File("C:\\Users\\DELL\\Desktop\\Student.txt", ios::out);
for (vector<student>::iterator it = (this->S).begin(); it != (this->S).end(); ++it){
cout << it->Id << "" << it->Name << "" << it->Dorm <<""<<it->Rank << endl;
File << it->Id << "" << it->Name << "" << it->Dorm << "" << it->Rank << endl;
}
File.close();
}

bool Rule(const student& s1, const student & s2)
{
return s1.Id < s2.Id;
}
void Student::Search()
{
cout << "请选择查找的关键字" << endl;
cout << "A:学号" << endl;
cout << "B:姓名" << endl;
cout << "C:分数" << endl;
char G;
cin >> G;
this->Sort(G);
switch(G){
case('A') : {
cout << "请输入要查询的学号:";
student SId;
cin >> SId.Id ;
auto it1 = lower_bound((this->S).begin(), (this->S).end(),SId,RuleId());
auto it2 = upper_bound((this->S).begin(), (this->S).end(),SId, RuleId());
if (it1 == it2){
cout << "对不起!您查询的人不存在" << endl;
}
else{
for (; it1 != it2; ++it1){
cout << it1->Id << "" << it1->Name << "" << it1->Dorm << "" << it1->Rank << endl;
}
}
break;
}case('B'):{
cout << "请输入要查询的姓名:";
student SId;
cin >> SId.Name ;
auto it1 = lower_bound((this->S).begin(), (this->S).end(), SId, RuleName());
auto it2 = upper_bound((this->S).begin(), (this->S).end(), SId, RuleName());
if (it1 == it2){
cout << "对不起!您查询的人不存在" << endl;
}
else{
for (; it1 != it2; ++it1){
cout << it1->Id << "" << it1->Name << "" << it1->Dorm << "" << it1->Rank << endl;
}
}
break;
}case('C') : {
cout << "请输入成绩:";
student SId;
cin >> SId.Dorm ;
auto it1 = lower_bound((this->S).begin(), (this->S).end(), SId, RuleDorm());
auto it2 = upper_bound((this->S).begin(), (this->S).end(), SId, RuleDorm());
if (it1 == it2){
cout << "对不起!您查询的人不存在" << endl;
}
else{
for (; it1 != it2; ++it1){
cout << it1->Id << "" << it1->Name << "" << it1->Dorm << "" << it1->Rank << endl;
}
}

break;
}
}
}



void Student::Edit()
{
this->Sort('A');
cout << "请输入要修改信息的学生的学号:";
student SId;
cin >> SId.Id;
auto it1 = lower_bound((this->S).begin(), (this->S).end(), SId, RuleId());
auto it2 = upper_bound((this->S).begin(), (this->S).end(), SId, RuleId());
if (it1 == it2){
cout << "对不起!您查询的人不存在" << endl;
}
else{
cout << "找到该生,请输入该生修改后的信息:";
cin >> it1->Id >> it1->Name >> it1->Dorm;
}
}


void Student::Erase()
{
this->Sort('A');
cout << "请输入要删除信息的学生的学号:";
student SId;
cin >> SId.Id;
auto it1 = lower_bound((this->S).begin(), (this->S).end(), SId, RuleId());
auto it2 = upper_bound((this->S).begin(), (this->S).end(), SId, RuleId());
if (it1 == it2){
cout << "对不起!您查询的人不存在" << endl;
}
else{
this->S.erase(it1);
}
Student::count--;
}


void Student::Num()
{
cout << "学生的总人数是:" << Student::count << endl;
}
void Show()
{
cout << "请选择您的操作" << endl;
cout << "0.结束程序" << endl;
cout << "1.添加学生成绩记录信息" << endl;
cout << "2.排序" << endl;
cout << "3.查找学生的信息" << endl;
cout << "4.编辑学生住宿记录" << endl;
cout << "5.删除学生住宿记录" << endl;
cout << "6.查询学生总数" << endl;
}

int main()
{
Student S;
int m;
Show();
cin >> m;
while (m != 0){
switch (m){
case(1) : {
S.insert();
break;
}case(2) : {
S.Sort();
break;
}case(3) : {
S.Search();
break;
}case(4) : {
S.Edit();
break;
}case(5):{
S.Erase();
break;
}case(6) : {
S.Num();
}
}
Show();
cin >> m;
}
S.Print();
cout << "谢谢使用!" << endl;
system("pause");
return 0;
}