实验目的:
(1)正确理解模板的概念
(2)掌握函数模板和类模板的声明和使用方法
(3)学习简单的异常处理方法
实验设备:
(1)硬件:个人微机(配置不低于:CPU为P4,主频1.6G,内存256M,硬盘40GB)
(2)软件:操作系统为Windows 7,工具软件为Visual C++6.0
实验内容:
1.写一个求任意类型数组中最大元素和最小元素的程序,要求将求最大元素和最小元素的函数设计成函数模板。//m7_test3.cpp
#include <iostream>
#include <algorithm>
template<typename T>
T MaxElement(T arr[], int n) {
T maxElem = arr[0];
for (int i = 1; i < n; ++i) {
if (arr[i] > maxElem) {
maxElem = arr[i];
}
}
return maxElem;
}
template<typename T>
T MinElement(T arr[], int n) {
T minElem = arr[0];
for (int i = 1; i < n; ++i) {
if (arr[i] < minElem) {
minElem = arr[i];
}
}
return minElem;
}
int main() {
int intArr[] = {5, 2, 9, 1, 7};
double doubleArr[] = {3.6, 2.4, 7.8, 1.5, 9.2};
char charArr[] = {'a', 'c', 'g', 'd', 'e'};
cout << "Max element in intArr: " << MaxElement(intArr, 5) << endl;
cout << "Min element in intArr: " << MinElement(intArr, 5) << endl;
cout << "Max element in doubleArr: " << MaxElement(doubleArr, 5) << endl;
cout << "Min element in doubleArr: " << MinElement(doubleArr, 5) << endl;
cout << "Max element in charArr: " << MaxElement(charArr, 5) << endl;
cout << "Min element in charArr: " << MinElement(charArr, 5) << endl;
return 0;
}
该程序定义了两个函数模板 `MaxElement` 和 `MinElement`,用于求任意类型数组中的最大元素和最小元素。在 `main` 函数中,我们使用这两个函数模板分别求出三个不同类型数组中的最大最小值。
输出结果如下:
Max element in intArr: 9
Min element in intArr: 1
Max element in doubleArr: 9.2
Min element in doubleArr: 1.5
Max element in charArr: g
Min element in charArr: a
3、编写一个程序,使用类模板对数组元素进行排序、倒置、查找和求和。//m7_test4.cpp
【提示】设计一个类模板:
template<classType>
class Array{
...
};
具有对数组元素进行排序、倒置、查找和求和功能,然后产生类型实参分别为int型和double型的两个模板类,分别对整型数组与双精度数组完成所要求的操作。
#include<iostream>
using namespace std;
template<class Type>
class Array{
private:
Type *a;
int size;
public:
Array(int s){
size=s;
a=new Type[s }
void getValues(){
cout<<"Enter "<<size<<" values:"<<endl;
for(int i=0;i<size;i++){
cin>>a[i }
}
void sortArray(){
for(int i=0;i<size-1;i++){
for(int j=i+1;j<size;j++){
if(a[i[j Type temp=a[i a[i[j a[j;
}
}
}
}
void reverseArray(){
for(int i=0;i<size/2;i++){
Type temp=a[i a[i[size-1-i a[size-1-i;
}
}
bool findElement(Type element){
for(int i=0;i<size;i++){
if(a[i){
return true;
}
}
return false;
}
Type sumArray(){
Type sum=0;
for(int i=0;i<size;i++){
sum+=a[i }
return sum;
}
void displayArray(){
cout<<"Array: ";
for(int i=0;i<size;i++){
couta[i }
coutendl;
}
};
int main(){
cout<<"\t Integer Array \n";
Array<int> array1(5);
array1.getValues();
array1.displayArray();
array1.sortArray();
cout<<"Sorted ";
array1.displayArray();
array1.reverseArray();
cout<<"Reverse ";
array1.displayArray();
int find=3;
bool found=array1.findElement(find);
if(found) coutfind<<" found in the array"<<endl;
else cout<<find<<" not found in the array"<<endl;
cout<<"Sum of the array is: "<<array1.sumArray()<<endl;
cout<<"\t Double Array \n";
Array<double> array2(5);
array2.getValues();
array2.displayArray();
array2.sortArray();
cout<<"Sorted ";
array2.displayArray();
array2.reverseArray();
cout<<"Reverse ";
array2.displayArray();
double find2=3.5;
bool found2=array2.findElement(find2);
if(found2) cout<<find2<<" found in the array"<<endl;
else cout<<find2<<" not found in the arrayendl;
cout<<"Sum of the array is:array2.sumArray()<<endl;
return 0;
}
这个程序可以接收用户通过控制台输入的值,对整型数组和双精度数组进行排序、倒置、查找和求和操作。程序中,整型数组是通过类模板Array<int>实例化的,双精度数组是通过类模板Array<double>实例化的。执行结果如下:
Integer Array
Enter 5 values:
2 6 4 1 8
Array: 2 6 4 1 8
Sorted Array: 1 2 4 6 8
Reverse Array: 8 6 4 2 1
3 not found in the array
Sum of the array is: 21
Double Array
Enter 5 values:
3.4 5.6 1.2 2.3 4.5
Array: 3.4 5.6 1.2 2.3 4.5
Sorted Array: 1.2 2.3 3.4 4.5 5.6
Reverse Array: 5.6 4.5 3.4 2.3 1.2
3.5 not found in the array
Sum of the array is: 17