#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <windows.h>
// 本科生
struct Undergraduate
{
int num; // 学号
std::string name; // 姓名
std::string sex1; // 性别
int Undergraduate_math; // 本科生 高数
int Undergraduate_English; // 本科生 英语
int Undergraduate_Cscore; // 本科生 c语言
int TotalScore; // 总成绩
int Classrank; // 班级排名
int Schoolrank; // 校排名
};
// 班级
struct Classroom
{
std::string classroom_name; // 班级名
std::vector<Undergraduate> u; // 本科生
};
// 专业
struct Profession
{
std::string profession_name; // 专业名
std::vector<Classroom> c; // 班级
};
// 学校
struct School
{
std::string school_name; // 校名
std::vector<Profession> pro; // 专业
};
//// 录入数据
//void InputDataDebug(School &dest, const std::vector<UndergraduateDebug> &src) // debug版本
//{
// for(auto iterator = src.begin(); iterator != src.end(); ++iterator)
// {
// // 确定专业
// std::vector<Profession>::iterator i = dest.pro.end();
// for(auto iter = dest.pro.begin(); iter != dest.pro.end(); ++iter)
// {
// if(iterator->profession == iter->profession_name)
// {
// i = iter;
// break;
// }
// }
//
// // 没有就加一个
// if(i == dest.pro.end())
// {
// // 在添加时确保专业是有序的
// for(auto iter = dest.pro.begin(); iter != dest.pro.end(); ++iter)
// {
// if(iter->profession_name > iterator->profession)
// {
// i = dest.pro.insert(iter, Profession({iterator->profession, std::vector<Classroom>()}));
// break;
// }
// }
//
// // i 任然等于dest.pro.end(),添加到最后
// if(i == dest.pro.end())
// {
// dest.pro.push_back(Profession({iterator->profession, std::vector<Classroom>()}));
// i = dest.pro.end() - 1;
// }
// }
//
// // 确定班级
// std::vector<Classroom>::iterator j = i->c.end();
// for(auto iter = i->c.begin(); iter != i->c.end(); ++iter)
// {
// if(iterator->classroom == iter->classroom_name)
// {
// j = iter;
// break;
// }
// }
//
// // 没有就加一个
// if(j == i->c.end())
// {
// // 在添加时确保班级是有序的
// for(auto iter = i->c.begin(); iter != i->c.end(); ++iter)
// {
// if(iter->classroom_name > iterator->classroom)
// {
// j = i->c.insert(iter, Classroom({iterator->classroom, std::vector<Undergraduate>()}));
// break;
// }
// }
//
// // j 任然等于i->c.end(),添加到最后
// if(j == i->c.end())
// {
// i->c.push_back(Classroom({iterator->classroom, std::vector<Undergraduate>()}));
// j = i->c.end() - 1;
// }
// }
//
// // 确定学生在班级中的位置
// std::vector<Undergraduate>::iterator k = j->u.end();
// int TotalScore = iterator->Undergraduate_math + iterator->Undergraduate_English + iterator->Undergraduate_Cscore;
// for(auto iter = j->u.begin(); iter != j->u.end(); ++iter)
// {
// if(iter->TotalScore < TotalScore)
// {
// k = iter;
// break;
// }
// }
//
// Undergraduate u;
// u.num = iterator->num;
// u.name = iterator->name;
// u.sex1 = iterator->sex1;
// u.Undergraduate_math = iterator->Undergraduate_math;
// u.Undergraduate_English = iterator->Undergraduate_English;
// u.Undergraduate_Cscore = iterator->Undergraduate_Cscore;
// u.TotalScore = TotalScore; // 上面已经计算过了
// u.Classrank = 0; // 暂时填0
// u.Schoolrank = 0; // 暂时填0
//
// j->u.insert(k, u);
// }
//}
// 录入数据
void InputData(School &s, const std::string profession_name, const std::string classroom_name, const Undergraduate &u)
{
// 确定专业
std::vector<Profession>::iterator i = s.pro.end();
for(auto iter = s.pro.begin(); iter != s.pro.end(); ++iter)
{
if(profession_name == iter->profession_name)
{
i = iter;
break;
}
}
// 没有就加一个
if(i == s.pro.end())
{
// 在添加时确保专业是有序的
for(auto iter = s.pro.begin(); iter != s.pro.end(); ++iter)
{
if(iter->profession_name > profession_name)
{
i = s.pro.insert(iter, Profession{profession_name, std::vector<Classroom>()});
break;
}
}
// i 任然等于dest.pro.end(),添加到最后
if(i == s.pro.end())
{
s.pro.push_back(Profession{profession_name, std::vector<Classroom>()});
i = s.pro.end() - 1;
}
}
// 确定班级
std::vector<Classroom>::iterator j = i->c.end();
for(auto iter = i->c.begin(); iter != i->c.end(); ++iter)
{
if(classroom_name == iter->classroom_name)
{
j = iter;
break;
}
}
// 没有就加一个
if(j == i->c.end())
{
// 在添加时确保班级是有序的
for(auto iter = i->c.begin(); iter != i->c.end(); ++iter)
{
if(iter->classroom_name > classroom_name)
{
j = i->c.insert(iter, Classroom{classroom_name, std::vector<Undergraduate>()});
break;
}
}
// j 任然等于i->c.end(),添加到最后
if(j == i->c.end())
{
i->c.push_back(Classroom{classroom_name, std::vector<Undergraduate>()});
j = i->c.end() - 1;
}
}
// 确定学生在班级中的位置
std::vector<Undergraduate>::iterator k = j->u.end();
for(auto iter = j->u.begin(); iter != j->u.end(); ++iter)
{
if(iter->TotalScore < u.TotalScore)
{
k = iter;
break;
}
}
j->u.insert(k, u);
}
bool Compare(const std::vector<Undergraduate>::iterator &a, const std::vector<Undergraduate>::iterator &b)
{
return a->TotalScore > b->TotalScore;
}
std::string AlignString(std::string s, int length)
{
while(s.length() != length)
s.push_back(' ');
return s;
}
void DebugPrint(School &s)
{
#if 1
int length = 15;
std::cout << AlignString("校名:", length) << s.school_name << std::endl;
for(auto i = s.pro.begin(); i != s.pro.end(); ++i)
{
std::cout << "-------------------------" << std::endl;
std::cout << AlignString("专业名:", length) << i->profession_name << std::endl;
std::cout << "-------------------------" << std::endl;
for(auto j = i->c.begin(); j != i->c.end(); ++j)
{
std::cout << "*************************" << std::endl;
std::cout << AlignString("班级名:", length) << j->classroom_name << std::endl;
for(auto k = j->u.begin(); k != j->u.end(); ++k)
{
std::cout << AlignString("学号:", length) << k->num << std::endl;
std::cout << AlignString("姓名:", length) << k->name << std::endl;
std::cout << AlignString("性别:", length) << k->sex1 << std::endl;
std::cout << AlignString("本科生 高数:", length) << k->Undergraduate_math << std::endl;
std::cout << AlignString("本科生 英语:", length) << k->Undergraduate_English << std::endl;
std::cout << AlignString("本科生 c语言:", length) << k->Undergraduate_Cscore << std::endl;
std::cout << AlignString("总成绩:", length) << k->TotalScore << std::endl;
std::cout << AlignString("班级排名:", length) << k->Classrank << std::endl;
std::cout << AlignString("校排名:", length) << k->Schoolrank << std::endl;
std::cout << std::endl;
}
std::cout << std::endl;
}
}
#else
// 另一种输出,按校级排名输出
std::vector<std::vector<Undergraduate>::iterator> tmp;
for(auto i = s.pro.begin(); i != s.pro.end(); ++i)
{
for(auto j = i->c.begin(); j != i->c.end(); ++j)
{
for(auto k = j->u.begin(); k != j->u.end(); ++k)
{
tmp.push_back(k);
}
}
}
// 排序
std::sort(tmp.begin(), tmp.end(), Compare);
int length = 15;
for(auto iter = tmp.begin(); iter != tmp.end(); ++iter)
{
std::cout << AlignString("学号:", length) << (*iter)->num << std::endl;
std::cout << AlignString("姓名:", length) << (*iter)->name << std::endl;
std::cout << AlignString("性别:", length) << (*iter)->sex1 << std::endl;
std::cout << AlignString("本科生 高数:", length) << (*iter)->Undergraduate_math << std::endl;
std::cout << AlignString("本科生 英语:", length) << (*iter)->Undergraduate_English << std::endl;
std::cout << AlignString("本科生 c语言:", length) << (*iter)->Undergraduate_Cscore << std::endl;
std::cout << AlignString("总成绩:", length) << (*iter)->TotalScore << std::endl;
std::cout << AlignString("班级排名:", length) << (*iter)->Classrank << std::endl;
std::cout << AlignString("校排名:", length) << (*iter)->Schoolrank << std::endl;
std::cout << std::endl;
}
#endif
}
// ********************************************************************************************
// 排名
void Rank(School &s)
{
// 处理班级排名
// 学生在班级中已经是有序的
for(auto i = s.pro.begin(); i != s.pro.end(); ++i)
{
for(auto j = i->c.begin(); j != i->c.end(); ++j)
{
int count = 0;
for(auto k = j->u.begin(); k != j->u.end(); ++k)
{
k->Classrank = ++count;
}
}
}
// 处理校级排名
// 我也没有什么好办法^_^
std::vector<std::vector<Undergraduate>::iterator> tmp;
for(auto i = s.pro.begin(); i != s.pro.end(); ++i)
{
for(auto j = i->c.begin(); j != i->c.end(); ++j)
{
for(auto k = j->u.begin(); k != j->u.end(); ++k)
{
tmp.push_back(k);
}
}
}
// 排序
std::sort(tmp.begin(), tmp.end(), Compare);
// 填序号就可以了
int count = 0;
for(auto iter = tmp.begin(); iter != tmp.end(); ++iter)
{
(*iter)->Schoolrank = ++count;
}
}
// 为了方便,就把所有函数挤在一个cpp文件了
// 这样好像并不“方便”,目前就这样了
//static void SetXY(int x, int y)
//{
// SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD({(short)x, (short)y}));
//}
//
//static COORD GetXY()
//{
// CONSOLE_SCREEN_BUFFER_INFO csbi;
// GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
// return COORD{csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y};
//}
//
//static void HideCursor()
//{
// HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
// CONSOLE_CURSOR_INFO cci;
// GetConsoleCursorInfo(hOut, &cci);
// cci.bVisible = false;
// SetConsoleCursorInfo(hOut, &cci);
//}
//void DrawMenu()
//{
// const int WINDOW_X = 180;
// const int WINDOW_Y = 45;
// const std::string menu_select[] =
// {
// "1.添加 --- 添加学生信息",
// "2.删除 --- 删除学生信息",
// "3.修改 --- 修改学生信息",
// "4.查看 --- 查看学生信息"
// };
//
// for(int y = 0; y < WINDOW_Y - 1; ++y)
// {
// for(int x = 0; x < WINDOW_X; ++x)
// {
// if(((y == 0) || (y == WINDOW_Y - 2)) || ((x == 0) || (x == WINDOW_X - 1)))
// {
// SetXY(x, y);
// std::cout.put('*');
// }
// }
// }
//
//
//}
int main()
{
School s = {"校名", std::vector<Profession>()};
int count = 5;
while(count--)
{
std::string profession_name;
std::string classroom_name;
Undergraduate u;
std::cout << "学号:";
std::cin >> u.num;
std::cout << "姓名:";
std::cin >> u.name;
std::cout << "性别:";
std::cin >> u.sex1;
std::cout << "专业:";
std::cin >> profession_name;
std::cout << "班级:";
std::cin >> classroom_name;
std::cout << "本科生 高数:";
std::cin >> u.Undergraduate_math;
std::cout << "本科生 英语:";
std::cin >> u.Undergraduate_English;
std::cout << "本科生 c语言:";
std::cin >> u.Undergraduate_Cscore;
u.TotalScore = u.Undergraduate_math + u.Undergraduate_English + u.Undergraduate_Cscore;
u.Classrank = 0;
u.Schoolrank = 0;
InputData(s, profession_name, classroom_name, u);
}
Rank(s);
DebugPrint(s);
//HideCursor();
//DrawMenu();
//// 学号我就不管了,有重复也不管了,^_^
//std::vector<UndergraduateDebug> info_tab =
//{
// {0, "学生1", "女", "专业3", "班级2", 70, 50, 20, 0, 0, 0},
// {1, "学生2", "男", "专业1", "班级3", 99, 45, 80, 0, 0, 0},
// {2, "学生3", "男", "专业2", "班级1", 30, 20, 80, 0, 0, 0},
// {3, "学生4", "男", "专业2", "班级2", 20, 40, 60, 0, 0, 0},
// {4, "学生5", "女", "专业1", "班级1", 10, 58, 70, 0, 0, 0},
// {5, "学生6", "女", "专业3", "班级3", 55, 60, 90, 0, 0, 0},
// {6, "学生7", "男", "专业1", "班级1", 75, 57, 62, 0, 0, 0},
// {7, "学生8", "女", "专业3", "班级2", 75, 52, 18, 0, 0, 0},
// {8, "学生9", "男", "专业1", "班级3", 97, 89, 55, 0, 0, 0},
// {9, "学生10", "男", "专业2", "班级1", 35, 21, 88, 0, 0, 0},
// {10, "学生11", "男", "专业2", "班级2", 13, 18, 64, 0, 0, 0},
// {11, "学生12", "女", "专业1", "班级1", 19, 59, 53, 0, 0, 0},
// {12, "学生13", "女", "专业3", "班级3", 59, 51, 52, 0, 0, 0},
// {13, "学生14", "男", "专业1", "班级1", 70, 68, 30, 0, 0, 0}
//};
//
//School s = {"校名", std::vector<Profession>()};
//InputDataDebug(s, info_tab);
//Rank(s);
//DebugPrint(s);
return 0;
}