第八章习题

【8.14】已知下列主函数:

  1. int main()  
  2. {  
  3.     cout << min(10, 5, 3) << endl;  
  4.     cout << min(10.0,5.0,3.0) << endl;  
  5.     cout << min('a','b','c') << endl;  
  6.     return 0;  
  7. }  
设计一个求3个数中最小值的函数模版,并写出调用此函数模版的完整程序。

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. template<typename T>              
  5. T min(T a, T b, T c)               
  6. {  
  7.     T minus;  
  8.     if (a < b&&a < c)  
  9.         minus = a;  
  10.     else if (b < a&&b < c)  
  11.         minus = b;  
  12.     else  
  13.         minus = c;  
  14.     return minus;  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     cout << min(10, 5, 3) << endl;  
  20.     cout << min(10.0,5.0,3.0) << endl;  
  21.     cout << min('a','b','c') << endl;  
  22.     system("pause");  
  23.     return 0;  
  24. }  

【8.15】编写一个函数模版,求数组中的最大元素,并写出调用此函数模版的完整程序,使得函数调用时,数组的类型可以是整数也可以双精度类型

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. template<typename T>  
  5. T bigest(T *p)  
  6. {  
  7.     int s;   
  8.     cout << "请输入元素个数s" << endl;  
  9.     cin >> s;  
  10.      cout <<endl;  
  11.     for (int i = 0; i < s;i++)  
  12.         cin >> p[i];  
  13.     T big = p[0];  
  14.     for (int i = 1; i < s; i++)  
  15.     {  
  16.         if (p[i] > big)  
  17.             big = p[i];  
  18.     }  
  19.     return big;  
  20.   
  21. }  
  22.   
  23. int main()  
  24. {  
  25.     int s;  
  26.     cout << "请输入整型的一维数组的元素个数" << endl;  
  27.     cin >> s;  
  28.     int *pi = new int[s];  
  29.     cout << "数组中最大的数为: " << bigest(pi) << endl;  
  30.     cout << endl;  
  31.       
  32.     int s2;  
  33.     cout << "请输入双精度类型的一维数组的元素个数" << endl;  
  34.     cin >> s2;  
  35.     double *pi1 = new double[s2];  
  36.     cout << "数组中最大的数为: " << bigest(pi1) << endl;  
  37.     system("pause");  
  38.     return 0;  
  39. }  
  40. 【8.16】编写一个函数模版,使用冒泡函数排序法将数组内容由小到大排列并打印出来,并写出调用此函数模版的完整程序,使得函数调用时,数组的类型可以是整形也可以是双精度型。

    1. #include<iostream>  
    2. using namespace std;  
    3.   
    4. template<typename T, typename T1>  
    5. void sort(T *p,T1 n)             //冒泡法排序  
    6. {  
    7.     T temp;  
    8.     for (int i = 1; i < n; i++)  
    9.     {  
    10.         for (int j = n - 1; j >= i; j--)  
    11.             if (p[j - 1] > p[j])  
    12.             {  
    13.                 temp = p[j - 1];  
    14.                 p[j - 1] = p[j];  
    15.                 p[j] = temp;  
    16.             }  
    17.     }  
    18. }  
    19.   
    20. template<typename T,typename T1>  
    21. void  print(T *p, T1 n)               
    22. {  
    23.     cout << "排序后的数组如下:" << endl;  
    24.     for ( int i = 0; i < n; i++)  
    25.     {  
    26.         cout << p[i]<<"  ";  
    27.     }  
    28.     cout << endl;  
    29. }  
    30.   
    31. int main()  
    32. {  
    33.    int a[9],i,j;
    34. double b[4];
    35. cout<<"please input nine int number";
    36.  for(i=0;i<9;i++)
    37.  cin>>a[9];
    38. cout<<"please input 4 double number:"
    39. for(j=0;j<4;j++)
    40. cin>>b[j];
    41.     sort(a,9);  
    42.     print(a, 9);  
    43.     sort(d,4);  
    44.     print(d, 4);  
    45.     system("pause");  
    46.     return 0;  
    47. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值