// StudentArry.h : 此文件包含 "StudentArry" 类。学生数组成绩显示方法 C++ 14 // 2023年4月9日 涂聚文 Geovin Du edit. //(1)vec1.size() 就是”二维数组”的行数 //(2)vec1[0].size() 就是”二维数组”的列数 //vector<vector<double>> geovindu #pragma once #ifndef STUDENTARRY_H #define STUDENTARRY_H #include <iostream> #include <string> #include <vector> #include <memory> #include <iostream> #include <string> #include <ctime> #include <cstdlib> #include <list> #include<algorithm> using namespace std; /** * */ namespace geovindu { /// <summary> /// 学生数组成绩显示方法 /// </summary> class StudentArry { private: public: /// <summary> /// 学生成绩 /// </summary> void displayStudent(); /// <summary> /// 学生成绩 无效 /// </summary> /// <param name="name">学生姓名列表</param> /// <param name="Subject">课程列表</param> /// <param name="ScoreList">分数列表</param> void displayStudent(string name[5], string Subject[5], int ScoreList[5][4]); /// <summary> /// 学生成绩 /// </summary> /// <param name="name">学生姓名列表</param> /// <param name="Subject">课程列表</param> /// <param name="ScoreList">分数列表</param> void displayStudentVector(vector<string> name, vector<string>Subject, vector<vector<int>> ScoreList); /// <summary> /// 获取数组排序的索引列表 /// </summary> /// <param name="sortvecotrs"></param> /// <returns></returns> //vector<int> sort(vector<int> sortvecotrs); /// <summary> /// /// </summary> /// <param name="source"></param> /// <returns></returns> vector<int> setSort(vector<int> source); int getIndex(vector<int> sortvecotrs, int index); }; } #endif
// StudentArry.cpp : //练习案例:学生数组成绩显示方法 StudentArry //案例描述:学生数组成绩显示方法 #include "StudentArry.h" #include <iostream> #include <string> #include <vector> #include <memory> #include <iostream> #include <list> #include<algorithm> using namespace std; /** * @brief * \author geovindu. * \date 20230-4-10 */ namespace geovindu { /// <summary> /// /// </summary> typedef struct { int index; //原索引值 int value; //值 }sort_st; /// <summary> /// /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> bool compare(sort_st a, sort_st b) { return a.value > b.value; //升序排列,如果改为return a.value<b.value,则为降序 } struct node { double data; int No; }; int comp(const void* a, const void* b) { return (*(struct node*)a).data > (*(struct node*)b).data ? 1 : -1; } /// <summary> /// /// </summary> /// <param name="sortvecotrs"></param> /// <returns></returns> //vector<int> sort(vector<int> sortvecotrs) //{ // //vector <int> temp = sortvecotrs;// { 11, 3, 2, 14 }; // vector <sort_st> sort_array(sortvecotrs.size()); // for (int i = 0; i < sortvecotrs.size(); ++i) { // sort_array[i].index = i; // sort_array[i].value = sortvecotrs[i]; // } // //从小至大 // sort(sort_array.begin(), sort_array.end(), compare); // return sort_array; //} /// <summary> /// 学生成绩 /// 数组用法 /// </summary> void StudentArry::displayStudent() { /// int SubjectScore = 0; // int grossScore = 0; string name[5] = { "张三","李四","王五","涂聚文","Geovin" }; const int nameCount = size(name); string Subject[4] = { "语文","数学","英语","物理" }; const int SubjectCount = size(Subject); string strname = ""; //前人个数nameCount 后分数个数SubjectCount int ScoreList[nameCount][SubjectCount] = { { 100,100,100,99 },{ 90,50,100,60 },{ 60,70,80,77 },{ 10,20,50,17 },{ 60,90,40,87 } }; //表标题 cout << "\t"; for (int i = 0; i < SubjectCount; i++) { cout << Subject[i] + "\t"; } cout << "总分\t名次"; cout << endl; //求总成绩的数组及排名次 vector <int> temp; //vector<size_t> idx; std::vector<int> du; for (int i = 0; i < nameCount; i++) { grossScore = 0; //cout << name[i] << "\t"; for (int j = 0; j < SubjectCount; j++) { //显示行各人成绩 //cout << ScoreList[i][j] << "\t"; grossScore = grossScore + ScoreList[i][j]; } //cout << grossScore << "\t"; //cout << endl; temp.push_back(grossScore); } //du=sort(temp); du=setSort(temp); //内容列表 for (int i = 0; i < nameCount; i++) { grossScore = 0; cout << name[i] << "\t"; for (int j = 0; j < SubjectCount; j++) { cout << ScoreList[i][j] << "\t"; grossScore = grossScore + ScoreList[i][j]; } cout << grossScore << "\t";; int ii = getIndex(du,i); cout << ii; cout << endl; } //求总分和平均分 int sum = 0; int ver[SubjectCount]; cout << "科总分 "; for (int i = 0; i < SubjectCount; i++) { SubjectScore = 0; for (int j = 0; j < nameCount; j++) { SubjectScore = SubjectScore + ScoreList[j][i]; } sum = sum + SubjectScore; ver[i] = SubjectScore / nameCount; cout << SubjectScore << "\t"; } cout << sum; cout << endl; //显示平均分行 cout << "平均分 "; for (int i = 0; i < SubjectCount; i++) { cout << ver[i] << "\t"; } cout << endl; cout << "***********************************" << endl; /* for (int i = 0; i < 3; i++) { ge = 0; for (int j = 0; j < 3; j++) { ge = ge + chengji[i][j]; } strname = name[i] + to_string(ge); cout << name[i] << "个人总分:" << ge << endl; //cout <<"个人总分:"<< strname << endl; yuwen = 0; for (int j = 0; j < 3; j++) { yuwen = yuwen + chengji[j][i]; } cout << cheng[i] << "课程总分数:" << yuwen << endl; } */ } /// <summary> /// 学生成绩 无效 /// </summary> /// <param name="name">学生姓名列表</param> /// <param name="Subject">课程列表</param> /// <param name="ScoreList">分数列表</param> void StudentArry::displayStudent(string name[5], string Subject[4], int ScoreList[5][4]) { // int SubjectScore = 0; // int grossScore = 0; const int nameCount = sizeof(name); const int SubjectCount = sizeof(Subject); string strname = ""; //表标题 cout << "\t"; for (int i = 0; i < SubjectCount; i++) { cout << Subject[i] + "\t"; } cout << "总分"; cout << endl; //内容列表 for (int i = 0; i < nameCount; i++) { grossScore = 0; cout << name[i] << "\t"; for (int j = 0; j < SubjectCount; j++) { cout << ScoreList[i][j] << "\t"; grossScore = grossScore + ScoreList[i][j]; } cout << grossScore; cout << endl; } //求总分和平均分 int sum = 0; int ver[SubjectCount]; cout << "科总分 "; for (int i = 0; i < SubjectCount; i++) { SubjectScore = 0; for (int j = 0; j < nameCount; j++) { SubjectScore = SubjectScore + ScoreList[j][i]; } sum = sum + SubjectScore; ver[i] = SubjectScore / nameCount; cout << SubjectScore << "\t"; } cout << sum; cout << endl; //显示分均分行 cout << "平均分 "; for (int i = 0; i < SubjectCount; i++) { cout << ver[i] << "\t"; } cout << endl; cout << "***********************************" << endl; } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="v"></param> /// <returns></returns> template < typename T> vector< size_t> sort_indexes(const vector< T>& v) { // initialize original index locations vector< size_t> idx(v.size()); for (size_t i = 0; i != idx.size(); ++i) idx[i] = i; // sort indexes based on comparing values in v sort(idx.begin(), idx.end(), [&v](size_t i1, size_t i2) {return v[i1] < v[i2]; }); return idx; } /// <summary> /// /// </summary> /// <param name="sortvecotrs"></param> /// <returns></returns> vector<int> sort(vector<int> sortvecotrs) { vector<int> data = sortvecotrs; vector<int> indexdu(sortvecotrs.size(), 0); for (int i = 0; i != indexdu.size(); i++) { indexdu[i] = i; } sort(indexdu.begin(), indexdu.end(),[&](const int& a, const int& b) { return (data[a] < data[b]); } ); //反向排序 //sort(indexdu.rbegin(), indexdu.rend()); //qsort() return indexdu; } /// <summary> /// /// </summary> /// <param name="source"></param> /// <returns></returns> vector<int> StudentArry::setSort(vector<int> source) { //double a[] = { 3.0, 4.0, 2.5,1.1, 5.6 }; /* int n = source.size();// sizeof(a) / sizeof(double); struct node arry[sizeof(source) / sizeof(int)]; int i; for (i = 0; i < n; ++i) { arry[i].data = source[i]; arry[i].No = i; } qsort(arry, n, sizeof(struct node), comp); */ /** * * printf("(当前值,原索引)\n"); for(i = 0; i < n;++i) { cout<<arry[i].data<<"\t"<<a[i]<<"\t"<<arry[i].No<<endl; }. * * \param source * \return */ vector<int> index(source.size(), 0); for (int i = 0; i != index.size(); i++) { index[i] = i; } sort(index.begin(), index.end(), [&](const int& a, const int& b) { return (source[a] >source[b]); //< 升 //降 } ); return index; } /// <summary> /// 根据原索引值得到排名次 /// </summary> /// <param name="sortvecotrs"></param> /// <param name=""></param> /// <returns></returns> int StudentArry::getIndex(vector<int> sortvecotrs,int index) { int ii = 0; for (int i = 0; i != sortvecotrs.size(); i++) { if (sortvecotrs[i] == index) { ii = i + 1; } //cout <<"索引值:" << sortvecotrs[i] << " 名次:" << i + 1 << endl; } return ii; } /// <summary> /// 学生成绩 vector容器 /// </summary> /// <param name="name">学生姓名列表</param> /// <param name="Subject">课程列表</param> /// <param name="ScoreList">分数列表</param> void StudentArry::displayStudentVector(vector<string> name, vector<string>Subject, vector<vector<int>> ScoreList) { // int SubjectScore = 0; // int grossScore = 0; const int nameCount = name.size(); const int SubjectCount = Subject.size(); //const int vCout = SubjectCount;// Subject.size(); const int geovindu= Subject.size(); string strname = ""; //表标题 cout << "\t"; for (int i = 0; i < SubjectCount; i++) { cout << Subject[i] + "\t"; } cout << "总分\t名次"; cout << endl; //求总成绩的数组及排名次 vector <int> temp; //vector<size_t> idx; std::vector<int> du; for (int i = 0; i < nameCount; i++) { grossScore = 0; //cout << name[i] << "\t"; for (int j = 0; j < SubjectCount; j++) { //显示行各人成绩 //cout << ScoreList[i][j] << "\t"; grossScore = grossScore + ScoreList[i][j]; } //cout << grossScore << "\t"; //cout << endl; temp.push_back(grossScore); } //du=sort(temp); du = setSort(temp); //内容列表 for (int i = 0; i < nameCount; i++) { grossScore = 0; cout << name[i] << "\t"; for (int j = 0; j < SubjectCount; j++) { //显示行各人成绩 cout << ScoreList[i][j] << "\t"; grossScore = grossScore + ScoreList[i][j]; } cout << grossScore << "\t"; int ii = getIndex(du, i); cout << ii; cout << endl; } //增加行 //求总分和平均分 int sum = 0; const int vcout = Subject.size(); int* verScore =new int[vcout]; cout << "科总分 "; for (int i = 0; i < SubjectCount; i++) { SubjectScore = 0; for (int j = 0; j < nameCount; j++) { SubjectScore = SubjectScore + ScoreList[j][i]; } sum = sum + SubjectScore; verScore[i] = SubjectScore / nameCount; cout << SubjectScore << "\t"; } cout << sum; cout << endl; //显示分均分行 cout << "平均分 "; for (int i = 0; i < SubjectCount; i++) { cout << verScore[i] << "\t"; } cout << endl; cout << "**vector容器*********************************" << endl; } }
标签:sort,const,int,Double,Dimensional,vector,include,size From: https://www.cnblogs.com/geovindu/p/17331649.html