C++模板類練習題


題目說明:

1 編寫一個程序,使用類模板對數組元素進行排序,倒置、查找和求和
2 具有對數組元素進行排序,倒置、查找和求和功能,
3 然后產生類型實參分別為int型和double型的兩個模板類,
4 分別對整型數組與雙精度數組完成所要求的操作

 

實現代碼:

  1 /*
  2 編寫一個程序,使用類模板對數組元素進行排序,倒置、查找和求和
  3 具有對數組元素進行排序,倒置、查找和求和功能,
  4 然后產生類型實參分別為int型和double型的兩個模板類,
  5 分別對整型數組與雙精度數組完成所要求的操作
  6 */
  7 
  8 // 以下代碼 中標注了是用來debug的 都可以在debug完成后 將用來debug的代碼刪除 
  9 
 10 #include<iostream>
 11 using namespace std;
 12 const int SIZE=100;
 13 
 14 // Array -> 模板類(類模板)  
 15 template <class Type>
 16 class Array{
 17 private:
 18     int l, z;
 19     Type a[SIZE];
 20 public:
 21     Array(Type *b, int n)
 22     {
 23         int i;
 24         l = n;
 25         cout << "Array構造函數: " << endl;         // debug
 26         for(i=0;i<l;i++)
 27         {
 28             a[i] = b[i];
 29             // 接下來兩個cout 是用來debug
 30             cout << a[i] << " "; 
 31         } 
 32         cout << endl;
 33     }
 34     
 35     /*
 36         void paixu();
 37         void daozhi();
 38         void chazhao(Type t);
 39         void add();     // 這個是正確的命名,但是求和還是用sum比較好 add表示加 
 40     */
 41     //以上函數的正確命名方法 
 42     void sort();
 43     void reverse();
 44     void find(Type t);
 45     void sum();    
 46         
 47 };
 48 
 49 template <class Type>
 50 void Array<Type>::sort(){ 
 51     Type c[SIZE];
 52     int i, j, m;
 53     for(i=0;i<l;i++)
 54     {
 55         c[i] = a[i];    
 56     }
 57         
 58     // 這下面的{}最好一個都不要省 
 59     // 一個都不省的話 首先方便后期添加代碼 其次看起來舒服 
 60     // 排序 升序排序 
 61     for(j=0;j<l;j++)
 62     {
 63         for(j=i;j<l;j++)
 64         {
 65             if(c[i]>c[j])
 66             {
 67                 m = c[i];
 68                 c[i] = c[j];
 69                 c[j] = m;
 70             }
 71         }
 72     }
 73     
 74     // 輸出排序后的數據 -> a b c 
 75     for(i=0;i<l;i++)
 76     {
 77         cout << c[i] << " ";    
 78     }
 79     cout << endl;
 80     
 81 }
 82         
 83 template <class Type>
 84 void Array<Type>::reverse(){
 85     int i;
 86     Type d[l];                // 聲明數組 
 87     for(i=0;i<l;i++)
 88     {
 89         // int d[i] = a[l-1-i];
 90         d[i] = a[l-1-i];
 91         cout << d[i] << " ";
 92     }
 93     cout << endl;
 94 }
 95 
 96 template <class Type>
 97 void Array<Type>::find(Type t){
 98     int e = 0;
 99     int flag = 0;            // 0表示未找到 
100     for(int i=0;i<l;i++)
101     {
102         if(a[i]==t)
103         {
104             flag = 1;
105             cout << "是第" << i+1 << "個元素" << endl;
106             e = i; 
107             break;    
108             // 找到了就應該直接退出 
109             // 要不然如果不退出循環 后面有一樣的值 就會又輸出一遍 
110             // 當然如果你想將每個值的位置都輸出也可以 但是一般常理是找第一個出現的值
111             
112             // 另外可能沒找到 你應該對這種情況做一個判斷 如果沒找到 輸出適當提示信息
113             // 判斷沒找到的方法很簡單 就是用一個flag變量來標識是否找到 
114         }
115     }
116     
117     // 判斷是否未找到該元素 
118     if(flag==0)
119     {
120         cout << "未找到該元素" << endl;
121     }
122     
123 }
124 
125 template <class Type>
126 void Array<Type>::sum(){
127     int i;
128     Type res = 0;            // res應該初始化        
129     for(i=0;i<l;i++)
130     {
131         res += a[i];
132     }
133     
134     cout << "數組和為:" << res <<endl;
135 }
136 
137 int main()
138 {
139     int i, x, y, q;
140     double p;
141     
142     // 構建初始數組 
143     cout << "請輸入兩種類型數組元素個數: " << endl;
144     cin >> x >> y;
145     int *a;
146     double *b;
147     a = new int[x];
148     b = new double[y];
149     cout<<"請輸入int型數組元素"<<endl;
150     for(i=0;i<x;i++)
151     {
152         cin >> a[i];
153     }
154     cout << "請輸入double型數組元素" << endl;
155     for(i=0;i<y;i++)
156     {
157         cin >> b[i];
158     }
159     
160     // 用初始數組初始化 模板類數組 
161     Array<int> c(a,x);
162     Array<double> d(b,y);
163     
164     // int型數組功能展示 
165     cout << "int 型數組:" << endl;
166     cout << "排序:" << endl;
167     c.sort();
168     cout << "倒置:" << endl;
169     c.reverse();
170     cout << "請輸入要查找的元素: ";
171     cin >> q;
172     c.find(q);
173     cout << "求和:" << endl;
174     c.sum();
175     
176     // double型數組功能展示 
177     cout << "double 型數組:" << endl;
178     cout << "排序:" << endl;
179     d.sort();
180     cout << "倒置:" << endl;
181     d.reverse();
182     cout << "請輸入要查找的元素: ";
183     cin >> p;
184     c.find(p);
185     cout << "求和:" << endl;
186     d.sum();
187     
188     // 清除數據 
189     delete []a;
190     delete []b;
191     
192     return 0;
193     
194 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM