首页 > 其他分享 >cpp:Double Dimensional Array using vector 2

cpp:Double Dimensional Array using vector 2

时间:2023-04-18 23:33:55浏览次数:44  
标签:sort const int Double Dimensional vector include size

 

// 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

相关文章

  • Map和Vector
    PAT甲级-1016sanpleinput10101010101020202015151515151515203020151510101010CYLL01:01:06:01on-lineCYLL01:28:16:05off-lineCYJJ01:01:07:00off-lineCYLL01:01:08:03off-lineCYJJ01:01:05:59on-lineaaa01:01:01:03on-lineaaa......
  • Receive double value in WndProc from legacy
    ReceivedoublevalueinWndProcfromlegacyAskQuestionAsked 9years,8monthsagoModified 9years,1monthagoViewed 158times I'mtryingtosenddouble/floatvaluesfrommyMFClegacycodetoWPFwindow.WPFWndProcprocedurerece......
  • C++动态数组(vector.h)
    #include<iostream>#include<vector>intmain(){std::vector<std::string>con;con.push_back("9999");std::cout<<con[0];return0;}vector搞了一个多态,你可以随便赋值和数组一样,不过是动态的,读取的话vector有自带的比for更优雅的方式......
  • Java中的long与double的区别
    1.long与double在java中本身都是用64位存储的,但是他们的存储方式不同,导致double可储存的范围比long大很多2.long可以准确存储19位数字,而double只能准备存储16位数字(实际测试,是17位,)。double由于有exp位,可以存16位以上的数字,但是需要以低位的不精确作为代价。如果一个大于17位的lo......
  • Java_Double&BigDecimal
    importjava.math.BigDecimal;importjava.math.MathContext;publicclassBigDecimalTest{/***@paramargs*@referencearchie2010*@function实现将double类型的值转换为BigDecimal类型的值的不同途径以及各途径间的区别*......
  • java学习日记20230411-Vector
    VectorVector底层也是一个对象数组;Vector是线程同步的,即线程安全,Vector类的操作方法带有synchronized在开发中需要线程同步安全的,考虑使用VectorpublicclassVector01{//Vector线程安全publicstaticvoidmain(String[]args){Vector<Object>objects......
  • UVa 113 / POJ 2109 Power of Cryptography (使用double处理大整数&泰勒公式与误差分
    113-PowerofCryptographyTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem&problem=49http://poj.org/problem?id=2109题意:给出n和p,求出 ,但是p可以很大()如何存储p?不用大数可不可以?先看看double......
  • 用find_if查找vector内对象的成员
    用stl的find方法查找一个包含简单类型的vector中的元素是很简单的,例如vector<string>strVec;find(strVec.begin(),strVec.end(),”aa”);假如vector包含一个复合类型的对象呢比如classA{public:A(conststd::stringstr,intid){this->str=str;this->id=id;}private:......
  • vector类的学习和实现4月8日
    vector类不同于string类.他其中的成员变量都为重定义自定义类型指针为迭代器,用指针的位置来代表容量,当前容器大小构造函数:将所有指针都置为空,析构函数:释放空间并将所有成员指针置为空.拷贝构造和重载=运算符:拷贝构造可以开空间并依次尾插被拷贝类的各个数值来达到构造的......
  • C++竞赛常用函数库stl快捷查询手册(vector,map,set,queue,string等)
    1.控制输出流<iomanip>;cout<<setprecision(<span="">int);保留int位有效数字cout<<setprecision(<span="">int)<<fixed;保留int位有效小数为不足4位数的数填充0(如1填充变成0001),cout<<setfill('0')<<setw(4)(一次性效果)......