陣列與字串

2009-11-17 2:00 am
試寫一程式將各有5個整數的陣列,
合並成一個由大到小排列新陣列陣列長度為10

dev c++程式撰寫

回答 (3)

2009-11-17 2:46 am
✔ 最佳答案
// Headers and Macros
#ifndef _MSC_VER
#undef NULL
#define NULL 0
#endif
#include <iostream>
#include <cstdlib>
using namespace std;
//
#define ARRAY_NUMBER 5
#define ELE_PER_ARRAY 10
#include <vector>
#include <algorithm>
#include <functional>
typedef vector<int> Vint;
class CHW
{
public:
CHW(void)
{
this->BuildArray();
this->Process();
}
// Rule of Three in C++
// 1. Copy Constructor
CHW(CHW& clone){}
// 2. Copy Assignment Operator
CHW& operator=(CHW& clone)
{
if ( this != &clone ){}
return *this;
}
// 3. Destructor
~CHW()
{
if ( ! this->m_vint.empty() )
{
this->m_vint.clear();
}
}
void BuildArray(void)
{
for ( int I = 0; I < ARRAY_NUMBER; I ++)
{
for ( int J = 0; J < ELE_PER_ARRAY; J ++)
{
this->m_pNum[I][J] = I * 10 + J;
}
}
for ( int I = 0; I < ARRAY_NUMBER; I ++)
{
for ( int J = 0; J < ELE_PER_ARRAY; J ++)
{
cout<<" "<<this->m_pNum[I][J];
}
cout<<endl;
}
}
void Process(void)
{
for ( int I = 0; I < ARRAY_NUMBER; I ++)
{
for ( int J = 0; J < ELE_PER_ARRAY; J ++)
{
this->m_vint.push_back(this->m_pNum[I][J]);
}
}
cout<<endl<<"Before sort..."<<endl;
for ( Vint::iterator it = this->m_vint.begin(); it != this->m_vint.end(); it ++)
{
cout<<" "<<(*it);
}
cout<<endl<<"After sort..."<<endl;
sort( this->m_vint.begin(), this->m_vint.end(), greater<int>());
for ( Vint::iterator it = this->m_vint.begin(); it != this->m_vint.end(); it ++)
{
cout<<" "<<(*it);
}
cout<<endl;
}
private:
int m_pNum[ARRAY_NUMBER][ELE_PER_ARRAY];
Vint m_vint;
};
//Main Function
#ifndef _MSC_VER
int
#else
void
#endif
main(int argc, char** argv)
{
//==START==//
CHW a;
//==END==//
system("PAUSE");
return
#ifndef _MSC_VER
EXIT_SUCCESS
#endif
;
}



2009-11-19 18:35:30 補充:
哪邊出錯了?
動態陣列、STL 應該沒錯吧!

2009-11-20 16:22:27 補充:
=__=!
又不是大型程式…,可以執行就好啦!
參考: 這是 Cpp 原始碼
2009-11-21 3:11 am
馬步不穩,練不好拳!

你最近一直用來回答的架構,
照示你對 class, NULL, constructor, compiler compatiblilty, OS 等的基本觀念根本有問題!

用這樣有多重問題的程式,回答一。二次,可謂好玩;
一直使用,被人指出有問題,還找藉口,
只能說,你不是不知它有問題,就是存心誤人子弟!
2009-11-20 8:43 pm
參考 http://tw.knowledge.yahoo.com/question/question?qid=1509111905902 兩位高手對你的程式的評語,好好反省一下吧!


收錄日期: 2021-04-27 17:16:14
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20091116000016KK05777

檢視 Wayback Machine 備份