数组升序降序最大值最小值统计的类模板及实例

来源:互联网 发布:青岛seo综合查询工具 编辑:程序博客网 时间:2024/04/28 22:43

/*
sortScore.h
*/
/*已知整型实数组长度的统计类模板及实例
算法:冒泡法
过程:
1.数组原型
2.排序
3.降序
4.分数段
4.最大值
5.最小值
其中最大值,最小值可以在排序函数中实现,
现在用单独函数实现

(c) adengou@foxmail.com 2011.8.19

win7 dev c++5.0 和 vs 2010 编译通过

使用请保留版权
*/
#include <iostream>
#include <string.h>
using namespace std;
#ifndef __sortScore_h_h
#define __sortScore_h_h
//using namespace std;

//typedef T CScore;//定义分数类型

template<class mTemp,class iTemp>
class NumSort
{
private:
 iTemp i;//步长控制
 iTemp j;//步长控制
 iTemp ArrayLength;//数组长度
 mTemp *nArray;//数组指针
 mTemp tempNumber;//用来调换数据

public:
 //学生人数
 iTemp m_stdCount;
 //总分
    mTemp m_stdTotalScore;
 //平均分
 mTemp m_stdAveScore;
 //自定义分数段人数
 iTemp m_stdSectionCount;
 //最大值,最小值
 mTemp Max, Min;
public:
 /*构造函数*/
 NumSort(mTemp arryNum[],iTemp nLen);
 //升序排序
 void UPSort(void);
 //降序排列
 void DESort(void);
 //求数组之和
 mTemp TotalSort(void);
 //最大值
 mTemp MaxSort(void);
 //最小值
 mTemp MinSort(void);
 //自定义分数段函数
 iTemp SectionScoreSelf(mTemp firstNumber,mTemp secondNumber);
};
//构造函数,变量初始化
template<class mTemp,class iTemp>
NumSort<mTemp,iTemp>::NumSort(mTemp arryNum[],iTemp nLen):m_stdCount(0),m_stdTotalScore(0.0),
 m_stdAveScore(0.0),m_stdSectionCount(0),tempNumber(0)
 {
     nArray=arryNum;ArrayLength= nLen;
  m_stdCount=ArrayLength;
 }
//原数组
template<class mTemp,class iTemp>
mTemp NumSort<mTemp,iTemp>::TotalSort(void)
{
 //总分;
  for(i=0;i!=ArrayLength;i++)
 {
  m_stdTotalScore+=nArray[i];
 }
    return m_stdTotalScore;
}
//升序数组
template<class mTemp,class iTemp>
void NumSort<mTemp,iTemp>::UPSort(void)
{
 for(i=0;i!=ArrayLength;i++)
 {
  m_stdTotalScore+=nArray[i];//求总分
  for(j=i+1;j!=ArrayLength;j++)//排序
  {
   if(nArray[i]>nArray[j])
   {
    tempNumber=nArray[i];
    nArray[i]=nArray[j];
    nArray[j]=tempNumber;
   }
  }
 }
 if(ArrayLength!=0){m_stdAveScore=m_stdTotalScore/ArrayLength;}//求平均分
 Min=nArray[i];Max=nArray[ArrayLength-1];//最小值最大值

}
//降序数组
template<class mTemp,class iTemp>
void NumSort<mTemp,iTemp>::DESort(void)
{
 for(i=0;i!=ArrayLength;i++)
 {
  for(j=i+1;j!=ArrayLength;j++)
  {
   if(nArray[i]<nArray[j])
   {
    tempNumber=nArray[i];
    nArray[i]=nArray[j];
    nArray[j]=tempNumber;
   }
  }
 }
}
//数组中最大值
template<class mTemp,class iTemp>
mTemp NumSort<mTemp,iTemp>::MaxSort(void)
{
 Max =nArray[0];
 for (i=0;i!=ArrayLength;i++)
 {
  if(nArray[i] > Max)
   Max =nArray[i];
 }
 
 return Max;
}
//数组中最小值
template<class mTemp,class iTemp>
mTemp NumSort<mTemp,iTemp>::MinSort(void)
{
 Min =nArray[0];
 for (i=0;i!=ArrayLength;i++)
 {
  if(nArray[i] < Min)
   Min =nArray[i];
 }
 return Min;
}
//分数段人数
template<class mTemp,class iTemp>
iTemp NumSort<mTemp,iTemp>::SectionScoreSelf(mTemp firstNumber,mTemp secondNumber)
{
   if(firstNumber>secondNumber){
         tempNumber=firstNumber;
   firstNumber=secondNumber;
         secondNumber=tempNumber;
 }
 m_stdSectionCount=0;
 for (int i=0;i!=ArrayLength;i++)
 {
  if(nArray[i] >= firstNumber && nArray[i]<=secondNumber)
   m_stdSectionCount+=1;
 }
 return m_stdSectionCount;
}
#endif

/*示例:
#include  "sortScore.h"
int main()
{
   long int  nLen;//数组长度
   double nGrade[]={95.0,98.7,46,98,67,58.2,101.5,67};
  
  nLen=sizeof (nGrade)/sizeof (double);
  NumSort<double,lont int> Grade(nGrade, nLen);
  Grade.TotalSort();//总分
  Grade.UPSort();//升序
  Grade.DESort();//降序
  cout<<"最大值: "<<Grade.MaxSort()<<endl;
  //Grade.MaxSort();//最大值
   cout<<"最小值: "<<Grade.MinSort()<<endl;
   Grade.MinSort();//最小值
   cout<<"参考人数: "<< Grade.m_stdCount<<endl;
    cout<<"总分: "<<Grade.m_stdTotalScore<<endl;
   cout<<"平均分: "<<Grade.m_stdAveScore<<endl;
   cout<<"分数段10-59: "<<Grade.SectionScoreSelf(10,59) <<endl;
   cout<<"分数段90-100.5: "<<Grade.SectionScoreSelf(90,100) <<endl;
   cout<<"分数段59-70: "<<Grade.SectionScoreSelf(70,59) <<endl;
   system("pause");
  return 0;
}
*/