- 最後登錄
- 2023-10-5
- 在線時間
- 0 小時
- 註冊時間
- 2013-4-17
- 閱讀權限
- 20
- 精華
- 0
- UID
- 12937946
- 帖子
- 300
- 積分
- 193 點
- 潛水值
- 11150 米
| 若瀏覽伊莉的時侯發生問題或不正常情況,請使用Internet Explorer(I.E)。 本帖最後由 andrew33673367 於 2014-5-16 01:16 AM 編輯
這是我用樣板做的一個練習(輸入陣列排序後再輸出出來)我用devC++跑到一辦電腦就說運作終止,請問問題出在哪??
- #include <iostream>
- using namespace std;
- template<class T1> class Test;
- template<class T1>
- class Test{
- public:
- Test(int=0);
- void input();
- void output();
- void sort();
- private:
- T1 *array;
- int index;
- };
- template<class T1>Test<T1>::Test(int a):index(a){
- cout<<"construct complete....."<<"\n";
- array=new T1[index];
- }
- template<class T1>void Test<T1>::input(){
- for(int i=0;i<index;i++){
- cout<<"please type array["<<i<<"]:";
- cin>>*(array+i);
- cout<<"\n";
- }
- }
- template<class T1>void Test<T1>::output(){
- for(int i=0;i<index;i++){
- cout<<"your array["<<i<<"]:"<<*(array+i)<<"\n";
- }
- }
- template<class T1>void Test<T1>::sort(){
- cout<<"sort function open success\n"<<"sort running.....\n";
- int k=index-1,p,j;
- while(k!=0){
- p=0;
- for(int i=0;i<=k-1;i++){
- if(*(array+i)>*(array+i+1)){
- T1 swap=*(array+i);*(array+i)=*(array+i+1);*(array+i+1)=swap;
- p=i;
- }
- }
- k=p;
- }
- }
- int main(){
- int a;
- cout<<"please type index:";
- cin>>a;cout<<"\n";
- Test<int> A(a); //小心別寫成Test<T1>
- A.input();
- A.sort();
- A.output();
- }
複製代碼
... |
|