#include <iostream.h>
template<class T>
class myArray
{
public:
myArray(int nSize,T Initial);
~myArray()
{
delete[] m_pArray;
}
T &operator[](int nIndex)
{
return m_pArray[nIndex];
}
void Show( int nNumElems, char *pszMsg=" ", bool bOneLine=true);
void Sort(int nNumElems);
protected:
T *m_pArray;
int m_nSize;
};
template<class T>
myArray< T >::myArray(int nSize,T InitVal)
{
m_nSize=(nSize>1)? nSize:1;
m_pArray=new T[m_nSize];
for(int i=0;i<m_nSize;i++)
m_pArray[i]=InitVal;
}
template<class T>
void myArray< T >::Show( int nNumElems, char *pszMsg, bool bOneLine)
{
cout << pszMsg<<endl;
if(bOneLine)
{
for(int i=0;i<nNumElems;i++)
cout << m_pArray[i] << ' ';
cout << endl;
}
else
{
for(int i=0;i<nNumElems;i++)
cout << m_pArray[i]<<endl;
}
}
template<class T>
void myArray< T >::Sort(int nNumElems)
{
int i, j;
T temp;
for (i = 1; i < nNumElems; i++)
{
j = i;
temp = m_pArray[i];
while (j > 0 && temp < m_pArray[j-1])
{
m_pArray[j] = m_pArray[j-1];
j--;
}
m_pArray[j] = temp;
}
/* int nOffset=nNumElems;
bool bSorted;
//check argument of parameter nNumbers
if(nNumElems<2)
return;
do
{
nOffset=(nOffset*8)/11;
nOffset=(nOffset<1)?1:nOffset;
bSorted=true;//set sorted flag
//compare elements
for(int i=0,j=nOffset; i < (nNumElems-nOffset); i++,j++)
{
if(m_pArray[i]>m_pArray[j])
{
//swap elements
T nSwap=m_pArray[i];
m_pArray[i]=m_pArray[j];
m_pArray[j]=nSwap;
bSorted=false;//clear sorted flag
}
}
}while(!bSorted||nOffset!=1);
*/
}
void main()
{
int nArr[10]={89,34,32,47,15,81,78,36,63,83};
char cArr[10]={'C','W','r','Y','k','J','X','Z','y','s'};
myArray<int> IntegerArray(10,0);
myArray<char> CharArray(10,' ');
for(int i=0;i<10;i++)
IntegerArray[i]=nArr[i];
for(i=0;i<10;i++)
CharArray[i]=cArr[i];
IntegerArray.Show(10,"Unsorted array is: ");
IntegerArray.Sort(10);
IntegerArray.Show(10,"Sorted array is: ");
cout << endl ;
CharArray.Show(10,"Unsorted array is: ");
CharArray.Sort(10);
CharArray.Show(10,"Sorted array is: ");
cout << endl;
}
没有合适的资源?快使用搜索试试~ 我知道了~
《C++程序设计教程》-杨国兴-电子教案及例题-3838
共469个文件
cpp:135个
pdb:42个
dsp:41个
需积分: 9 3 下载量 134 浏览量
2008-09-03
22:00:23
上传
评论
收藏 5.33MB RAR 举报
温馨提示
《C++程序设计教程》-杨国兴-电子教案及例题-3838
资源推荐
资源详情
资源评论
收起资源包目录
《C++程序设计教程》-杨国兴-电子教案及例题-3838 (469个子文件)
Cpp_language.chm 202KB
l9_5.cpp 2KB
l6_2.cpp 1KB
l7_12.cpp 1KB
l8_7.cpp 1KB
l7_3.cpp 1KB
l8_10.cpp 1KB
l7_6.cpp 1KB
l6_3.cpp 1KB
l6_11.cpp 1KB
l9_2.cpp 1KB
Rectangle.cpp 1KB
l6_1.cpp 1KB
l7_2.cpp 1KB
l8_2.cpp 1KB
l8_9.cpp 1KB
l10_10.cpp 1KB
l6_7.cpp 1KB
l10_11.cpp 1024B
l7_1.cpp 1017B
l6_4.cpp 969B
l9_7.cpp 946B
l8_6.cpp 922B
l10_6.cpp 877B
l6_8.cpp 876B
l10_9.cpp 819B
l8_1.cpp 809B
l4_17.cpp 801B
l7_11.cpp 783B
l7_7.cpp 771B
l6_10.cpp 733B
l3_7.cpp 708B
l8_5.cpp 705B
l9_6.cpp 676B
l6_9.cpp 670B
l6_5.cpp 627B
l7_8_1.cpp 626B
l7_10.cpp 625B
l7_8.cpp 609B
l8_3.cpp 600B
l4_9.cpp 597B
l3_8.cpp 585B
l7_9.cpp 557B
l7_5.cpp 548B
l4_18.cpp 501B
l9_a.cpp 487B
l4_12.cpp 472B
l5_27.cpp 448B
l5_23.cpp 436B
l5_23a.cpp 434B
l5_3.cpp 429B
l4_15.cpp 429B
l8_4.cpp 427B
l2_1.cpp 422B
l5_24.cpp 420B
l7_4.cpp 408B
l4_11.cpp 406B
l5_28.cpp 405B
l3_4.cpp 404B
l6_6.cpp 388B
l5_25.cpp 387B
l5_6.cpp 379B
l4_6.cpp 378B
l4_14.cpp 377B
l10_8.cpp 374B
l5_18.cpp 368B
l3_5.cpp 365B
l2_11.cpp 365B
l2_12.cpp 363B
l4_10.cpp 362B
l9_4.cpp 358B
l9_1.cpp 334B
l4_2.cpp 330B
l5_21.cpp 327B
l5_17.cpp 326B
l4_13.cpp 321B
l5_5.cpp 319B
l10_5.cpp 319B
l8_8.cpp 315B
l8_8_2.cpp 310B
l4_19.cpp 302B
l5_10.cpp 302B
l10_4.cpp 297B
l3_3.cpp 296B
l4_7.cpp 295B
l4_1.cpp 285B
l4_8.cpp 273B
l2_13.cpp 268B
l5_2.cpp 267B
l5_22.cpp 261B
l5_4.cpp 258B
l3_15.cpp 257B
l5_16.cpp 256B
l4_5.cpp 255B
l4_4.cpp 255B
l3_13.cpp 252B
l4_3.cpp 251B
l5_8.cpp 247B
l2_6.cpp 245B
l3_1.cpp 228B
共 469 条
- 1
- 2
- 3
- 4
- 5
资源评论
tangbing819563
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功