C++习题与解析-模板

01.分析以下程序的执行结果 
  #include 
  template  
  T max(T x,T y) 
  { 
  return (x>y?x:y); 
  } 
  void main() 
  { 
  cout<
  } 
  解: 
  本题说明函数模板的使用方法。max()函数是一个函数模板,它返回两个参数中的较大者。在调用时自动联编相应的max()函数。所以输出为:5,3.5 
  注重:T为类型形式参数,可包含基本数据类型,也可以包含类类型,不能是普通的数据变量。 
  
  ------------------------------------------------------- 
  
  02.分析以下程序的执行结果 
  #include 
  template  
  class Sample 
  { 
  T n; 
  public: 
  Sample(T i){n=i;} 
  void operator++(); 
  void disp(){cout<<"n="<
  }; 
  template  
  void Sample::operator++() 
  { 
  n+=1; // 不能用n++;因为double型不能用++ 
  } 
  void main() 
  { 
  Sample s('a'); 
  s++; 
  s.disp(); 
  } 
  解: 
  本题说明类模板的使用方法。Sample是一个类模板,由它产生模板类Sample,通过构造函数给n赋初值,通过重载++运算符使n增1,这里n由'a'增1变成'b'。 
  所以输出为:n=b 
  
  ------------------------------------------------------- 
  
  03.编写一个对具有n 个元素的数组x[]求最大值的程序,要求将求最大值的函数设计成函数模板。 
  解: 
  将max()函数设计成一个函数模板。 
  本题程序如下: 
  #include 
  template  
  T max(T x[],int n) 
  { 
  int i; 
  T maxv=x[0]; 
  for(i=1;i
  if(maxv
  maxv=x[i]; 
  return maxv; 
  } 
  void main() 
  { 
  int a[]={4,5,2,8,9,3}; 
  double b[]={3.5,6.7,2,5.2,9.2}; 
  cout<<"a数组最大值:"<
  cout<<"b数组最大值:"<
  } 
  本程序的执行结果如下: 
  a 数组最大值:9 
  b 数组最大值:9.2 
  
  ------------------------------------------------- 
  
  04.编写一个使用类模板对数组进行排序、查找和求元素和的程序。 
  解: 
  设计一个类模板templateclass Array,用于对T类型的数组进行排序、查找和求元素和,然后由此产生模板类Array和Array。 
  本题程序如下: 
  #include 
  #include 
  template  
  class Array 
  { 
  T *set; 
  int n; 
  public: 
  Array(T *data,int i){set=data;n=i;} 
  ~Array(){} 
  void sort(); // 排序 
  int seek(T key); // 查找指定的元素 
  T sum(); // 求和 
  void disp(); // 显示所有的元素 
  }; 
  template 
  void Array::sort() 
  { 
  int i,j; 
  T temp; 
  for(i=1;i
  for(j=n-1;j>=i;j--) 
  if(set[j-1]>set[j]) 
  { 
  temp=set[j-1];set[j-1]=set[j];set[j]=temp; 
  } 
  } 
  template  
  int Array::seek(T key) 
  { 
  int i; 
  for(i=0;i
  if(set[i]==key) 
  return i; 
  return -1; 
  } 
  template 
  T Array::sum() 
  { 
  T s=0;int i; 
  for(i=0;i
  s+=set[i]; 
  return s; 
  } 
  template 
  void Array::disp() 
  { 
  int i; 
  for(i=0;i
  cout<
  cout<
  } 
  void main() 
  { 
  int a[]={6,3,8,1,9,4,7,5,2}; 
  double b[]={2.3,6.1,1.5,8.4,6.7,3.8}; 
  Arrayarr1(a,9); 
  Arrayarr2(b,6); 
  cout<<" arr1:"<
  cout<<" 原序列:"; arr1.disp(); 
  cout<<" 8在arr1中的位置:"<
  arr1.sort(); 
  cout<<" 排序后:"; arr1.disp(); 
  cout<<"arr2:"<
  cout<<" 原序列:"; arr2.disp(); 
  cout<<" 8.4在arr2中的位置:"<
  arr2.sort(); 
  cout<<" 排序后:"; arr2.disp(); 
  } 
  本程序的执行结果如下: 
  arr1: 
  原序列:6 3 8 1 9 4 7 5 2 
  8在arr1中的位置:2 
  排序后:1 2 3 4 5 6 7 8 9 
  arr2: 
  原序列:2.3 6.1 1.5 8.4 6.7 3.8 
  8.4在arr2中的位置:3 
  排序后:1.5 2.3 3.8 6.1 6.7 8.4 
   
   题1.分析以下程序的执行结果 
  #include 
  template  
  T abs(T x) 
  { 
  return (x>0?x:-x); 
  } 
  void main() 
  { 
  cout<
  } 
  解: 
  abs()是一个函数模板,它返回参数的绝对值。在调用时自动联编相应的abs()函数。 
  所以输出为:3,2.6 
  
  ---------------------------------------------- 
  
  題2.分析以下程序的执行结果 
  #include 
  template 
  class Sample 
  { 
  T n; 
  public: 
  Sample(){} 
  Sample(T i){n=i;} 
  Sample&operator+(consta Sample&); 
  void disp(){cout<<"n="<
  }; 
  template 
  Sample&Sample::operator+(const Sample&s) 
  { 
  static Sample temp; 
  temp.n=n+s.n; 
  return temp; 
  } 
  void main() 
  { 
  Samples1(10),s2(20),s3; 
  s3=s1+s2; 
  s3.disp(); 
  } 
  解: 
  Sample为一个类模板,产生一个模板类Sample,并建立它的三个对象,调用重载运算符+实现s1与s2的加法运算,将结果赋给s3。 
  所以输出为:n=30 
  
  ---------------------------------------------------- 
  
  题3.编写一个函数模板,它返回两个值中的较小者,同时要求能正确处理字符串。 
  解: 
  这里设计一个函数模板template T min(T a,T b),可以处理int、float和char 等数据类型,为了能正确处理字符串,添加一个重载函数专门处理字符串比较,即char *min(char *a,char *b)。
  本题程序如下: 
  #include 
  #include 
  template 
  T min(T a,T b) 
  { 
  return (a
  } 
  char *min(char *a,char *b) 
  { 
  return (strcmp(a,b)<0?a:b); 
  } 
  void main() 
  { 
  double a=3.56,b=8.23; 
  char s1[]="Hello",s2[]="Good"; 
  cout<<"输出结果:"<
  cout<<" "<
  cout<<" "<
  } 
  输出结果: 
  3.56,8.23中较小者:3.56 
  Hello,Good中较小者:Good 
  
  ---------------------------------------------------- 
  
  题4.设计一个数组类模板Array,其中包含重载下标运算符函数,并由此产生模板类Array和Array,使用一些测试数据对其进行测试。 
  解: 
  本题程序如下: 
  #include 
  #include 
  template  
  class Array 
  { 
  T *elems; 
  int size; 
  public: 
  Array(int s); // 构造函数 
  ~Array(); 
  T& operator[](int); // 重载下标运算符 
  void operator=(T); // 重载等号运算符 
  }; 
  template  
  Array::Array(int s) 
  { 
  size=s; 
  elems=new T[size]; 
  for(int i=0;i
  elems[i]=0; 
  } 
  template 
  Array::~Array() 
  { 
  delete elems; 
  } 
  template 
  T& Array::operator[](int index) 
  { 
  return elems[index]; 
  } 
  template 
  void Array::operator=(T temp) 
  { 
  for(int i=0;i
  elems[i]=temp; 
  } 
  void main() 
  { 
  int i,n=10; 
  Array arr1(n); // 产生整型模板类及其对象arr1 
  Array arr2(n); // 产生字符型模板类及其对象arr2 
  for(i=0;i
  { 
  arr1[i]='a'+i; // 调用重载运算符 
  arr2[i]='a'+i; 
  } 
  cout<<" ASCII码 字符"<
  for(i=0;i
  cout<
  } 
  本程序的执行结果如下: 
  ASCII码 字符 
  97 a 
  98 b 
  99 c 
  100 d 
  101 e 
  102 f 
  103 g 
  104 h 
  105 i 
  106 j 
   三层交换技术 交换机与路由器密码恢复 交换机的选购 路由器设置专题 路由故障处理手册 数字化校园网解决方案 
   题 5. 一个Sample类模板的私有数据成员为T n,在该类模板中设计一个operator==重载运算符函数 ,用于比较各对象的n数据是否相等。 
  解: 
  本题程序如下: 
  #include 
  template  
  class Sample 
  { 
  T n; 
  public: 
  Sample(T i){n=i;} 
  int operator==(Sample &); 
  }; 
  template  
  int Sample::operator==(Sample &s) 
  { 
  if(n==s.n) 
  return 1; 
  else 
  return 0; 
  } 
  void main() 
  { 
  Sample s1(2),s2(3); 
  cout<<"s1与s2的数据成员"<<(s1==s2?"相等":"不相等")<
  Samples3(2.5),s4(2.5); 
  cout<<"s3与s4的数据成员"<<(s3==s4?"相等":"不相等")<
  } 
  本程序的运行结果如下: 
  s1与s2的数据成员不相等 
  s3与S4的数据成员相等 
  
  ---------------------------------------------------- 
  
  题 6. 对第3章的例3.5进行修改,只设计一个Sample类,其数据和方法均包含在该类中,而且使用类模板的方式实现。 
  #include 
  #define Max 100 
  template  
  class Sample 
  { 
  T A[Max]; 
  int n; 
  void qsort(int l,int h); // 私有成员,由quicksort()成员调用 
  public: 
  Sample(){n=0;} 
  void getdata(); // 获取数据 
  void insertsort(); // 插入排序 
  void Shellsort(); // 希尔排序 
  void bubblesort(); // 冒泡排序 
  void quicksort(); // 快速排序 
  void selectsort(); // 选择排序 
  void disp(); 
  }; 
  template  
  void Sample::getdata() 
  { 
  cout<<"元素个数:"; 
  cin>>n; 
  for(int i=0;i
  { 
  cout<<"输入第"<
  cin>>A[i]; 
  } 
  } 
  template  
  void Sample::insertsort() // 插入排序 
  { 
  int i,j; 
  T temp; 
  for(i=1;i
  { 
  temp=A[i]; 
  j=i-1; 
  while(temp
  { 
  A[j+1]=A[j]; 
  j--; 
  } 
  A[j+1]=temp; 
  } 
  } 
  template  
  void Sample::Shellsort() // 希尔排序 
  { 
  int i,j,gap; 
  T temp; 
  gap=n/2; 
  while(gap>0) 
  { 
  for(i=gap;i
  { 
  j=i-gap; 
  while(j>=gap) 
  if(A[j]>A[j+gap]) 
  { 
  temp=A[j]; 
  A[j]=A[j+gap]; 
  A[j+gap]=temp; 
  j=j-gap; 
  } 
  else j=0; 
  } 
  gap=gap/2; 
  } 
  } 
  template  
  void Sample::bubblesort() // 冒泡排序 
  { 
  int i,j; 
  T temp; 
  for(i=0;i
  for(j=n-1;j>=i+1;j--) 
  if(A[j]
  { 
  temp=A[j]; 
  A[j]=A[j-1]; 
  A[j-1]=temp; 
  } 
  } 
  template  
  void Sample::quicksort() // 快速排序 
  { 
  qsort(0,n-1); 
  } 
  template 
  void Sample::qsort(int l,int h) 
  { 
  int i=l,j=h; 
  T temp; 
  if(l
  { 
  temp=A[l]; 
  do{ 
  while(j>i&&A[j]>=temp) 
  j--; 
  if(i
  { 
  A[i]=A[j]; 
  i++; 
  } 
  while(i
  i++; 
  if(i
  { 
  A[j]=A[i]; 
  j--; 
  } 
  }while(i
  A[i]=temp; 
  qsort(1,j-1); 
  qsort(j+1,h); 
  } 
  } 
  template  
  void Sample::selectsort() // 选择排序 
  { 
  int i,j,k; 
  T temp; 
  for(i=0;i
  { 
  k=i; 
  for(j=i+1;j<=n-1;j++) 
  if(A[j]
  k=j; 
  temp=A[i]; 
  A[i]=A[k]; 
  A[k]=temp; 
  } 
  } 
  template  
  void Sample::disp() 
  { 
  for(int i=0;i
  cout<
  cout<
  } 
  void main() 
  { 
  int sel=0; 
  Sample s; // 由类模板产生char型的模板类 
  s.getdata(); 
  cout<<"原来序列:"; 
  s.disp(); 
  cout<<"0:插入排序 1:希尔排序 2:冒泡排序 3:快速排序 4:选择排序 其它退出"<
  cout<<"选择排序方法:"; 
  cin>>sel; 
  switch(sel) 
  { 
  case 0: 
  s.insertsort(); 
  cout<<"插入排序结果"; 
  break; 
  case 1: 
  s.Shellsort(); 
  cout<<"希尔排序结果:"; 
  break; 
  case 2: 
  s.bubblesort(); 
  cout<<"冒泡排序结果:"; 
  break; 
  case 3: 
  s.quicksort(); 
  cout<<"快速排序结果:"; 
  break; 
  case 4: 
  s.selectsort(); 
  cout<<"选择排序结果:"; 
  break; 
  } 
  s.disp(); 
  } 
  程序运行结果如下: 
   
   
  ---------------------------------------------------- 
  
  题 7. 设计一个模板类Sample,用于对一个有序数组采用二分法查找元素下标。 
  解: 
  #include 
  #define Max 100 
  template  
  class Sample 
  { 
  T A[Max]; 
  int n; 
  public: 
  Sample(){} 
  Sample(T a[],int i); 
  int seek(T c); 
  void disp() 
  { 
  for(int i=0;i
  cout<
  cout<
  } 
  }; 
  template  
  Sample::Sample(T a[],int i) 
  { 
  n=i; 
  for(int j=0;j
  A[j]=a[j]; 
  } 
  template  
  int Sample::seek(T c) 
  { 
  int low=0,high=n-1,mid; 
  while(low<=high) 
  { 
  mid=(low+high)/2; 
  if(A[mid]==c) 
  return mid; 
  else if(A[mid]
  else high=mid-1; 
  } 
  return -1; 
  } 
  void main() 
  { 
  char a[]="acegkmpwxz"; 
  Samples(a,10); 
  cout<<"元素序列:"; s.disp(); 
  cout<<"'g'的下标:"<
  } 
  程序运行结果如下: 
  元素序列:a c e g k m p w x z 
  元素'g'的下标: 3 
   



FROM:  http://www.warting.com/program/201109/33601.html

你可能感兴趣的:(C++/STL)