程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 編程-數組類模板Array,類中包括對數組進行排序、查找和求元素和 然後由此產生模板類Array<Box>

編程-數組類模板Array,類中包括對數組進行排序、查找和求元素和 然後由此產生模板類Array<Box>

編輯:編程綜合問答
數組類模板Array,類中包括對數組進行排序、查找和求元素和 然後由此產生模板類Array<Box>

#include
using namespace std;
class Box
{
private:
int a, b, c;
public:
int V;
Box(int ch,int k,int g)
{
a = ch;
b = k;
c = g;
V = a*b*c;
}
bool operator <(Box &one)
{
int temp = V - one.V;
if (temp < 0)
return true;
else
return false;
}
int operator +(Box &one)
{
return V + one.V;
}
bool operator ==(Box &one)
{
if (V == one.V)
return true;
else
return false;
}
};
template
class Array
{
private:
int size;
T* element;
public:
Array();
~Array(){ delete[]element; }
void sore();
void find();
T add();
int add1()
{
int sum1 = 0;
for (int i = 0; i < size; i++)
{
sum1 = sum1 + element[i];
}
return sum1;
}
};
template
Array::Array()
{
cout << "輸入數組長度:" << endl;
cin >> size;
element = new T[size];
cout << "輸入數組成員:" << endl;
for (int i = 0; i < size; i++)
{
cin >> element[i];
}
}
template
void Array::sore()
{
int i, j;
T temp;
for (i = 0 ; i < size - 1; i++)
for (j = 0; j < size - 1 - i; j++)
{
if (element[j]>element[j + 1])
{
temp = element[j];
element[j] = element[j + 1];
element[j + 1] = temp;
}
}
for (int i2 = 0; i2 < size; i2++)
{
cout << element[i2] ;
}
}
template
void Array::find()
{
int p = 0;
T num;
cout << "輸入要找的數:" << endl;
cin >> num;
for (int i = 0; i < size; i++)
{
if (element[i] == num)
{
cout << "該成員對應的下標為:" << (i + 1) << endl;
p = 1;
}
}
if (p!=1)
cout << "不存在!!" << endl;
}
template
T Array::add()
{
T he=0;
for (int i = 0; i < size; i++)
{
he = he + element[i];
}
return he;
}
int main()
{
ArrayS2;
cout << "排序:";
S2.sore();
cout << endl;
S2.find();
cout << "求和:";
cout << S2.add1();
cout << endl;*/
return 0;
}

最佳回答:


http://zhidao.baidu.com/link?url=7sohJyMWxvDlcpDR3aEyJv8DSTuxB1aLc7p6SsanENCWuCmIBcaY2VY6dVKR-TQl6tjf7gZyWeHgMWnl7yhuX_

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved