zoukankan      html  css  js  c++  java
  • YTU 2642: 填空题:类模板---求数组的最大值

    2642: 填空题:类模板---求数组的最大值

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 646  解决: 446

    题目描述

      类模板---求数组的最大值
       找出一个数组中的元素的最大值,数组大小为10。(用类模板来实现)
       数组元素类型作为类模板的参数。
       在下面的程序段基础上完成设计,只提交begin到end部分的代码
     
    #include <iostream> 
    #include <string> 
    using namespace std; 
    template <class T>   
    class  Array_max         //声明类模板 
    {
     public:                   //以下3行为成员函数原型声明 
         void set_value( );    //对数组元素设置值 
         T  max_value( );    //找出数组中的最大元素 
      private: 
         T array[10];         //T类型数组 
         T max;               //max用来存放数组中的最大值 
    }; 
     
    //将程序需要的其他成份写下来,只提交begin到end部分的代码
    //******************** begin ********************
    ______(1)_______  
    void Array_max<T>::set_value( ) 
    {
    int i;      
        for (i=0; i<10; i++)  
    ______(2)_______; 
     
    ______(3)________  
    T Array_max<T>::max_value( ) 
    {  
    int i; 
            _____(4)________;  //此空可能需要多行才能实现要求的功能
    return max;



    //********************* end ********************
    int main( ) 
    {  
       Array_max<int>   arrmax_int; //定义对象arrmax_int,该对象中数组元素类型为整型 
       arrmax_int.set_value( );      //调用arrmax_int的set_value函数,向数组元素输入数值 
       cout<<arrmax_int.max_value( )<<endl;  //调用arrmax_int的max_value函数,找出数组元素中的最大值 
       
       
       Array_max<double>   arrmax_double; //定义对象arrmax_double,该对象中数组元素类型为双精度型 
       arrmax_double.set_value( );      //调用arrmax_double的set_value函数,向数组元素输入数值 
       cout<<arrmax_double.max_value( )<<endl;  //调用arrmax_double的max_value函数,找出数组元素中的最大值 
       
       
       Array_max<char>   arrmax_char; //定义对象arrmax_char,该对象中数组元素类型为字符型
       arrmax_char.set_value( );      //调用arrmax_char的set_value函数,向数组元素输入数值 
       cout<<arrmax_char.max_value( )<<endl;  //调用arrmax_char的max_value函数,找出数组元素中的最大值   
     
       
       Array_max<string>   arrmax_string; //定义对象arrmax_string,该对象中数组元素类型为字符串型
       arrmax_string.set_value( );      //调用arrmax_string的set_value函数,向数组元素输入数值 
       cout<<arrmax_string.max_value( )<<endl;  //调用arrmax_string,的max_value函数,找出数组元素中的最大值 
          
       return 0; 

    输入

    10个int型数据

    10个double型数据

    10个char型数据

    10gestring型数据

    输出

    10个int型数据的最大值

    10个double型数据的最大值

    10个char型数据的最大值

    10个string型数据的最大值

    样例输入

    1 3 5 7 9 8 6 4 2 0
    1.2 3.4 5.66 7.8 9.9 13.4 -2.5 6.7 0 -10
    a b 1 2 +  - A B p Z
    guo li   zhao sun zhou zhang yang lan zhai wang 
    

    样例输出

    9
    13.4
    p
    zhou

    提示

    只提交begin到end部分的代码


    迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……

    #include <iostream>
    #include <string>
    using namespace std;
    template <class T>
    class  Array_max         //声明类模板
    {
    public:                   //以下3行为成员函数原型声明
        void set_value( );    //对数组元素设置值
        T  max_value( );    //找出数组中的最大元素
    private:
        T array[10];         //T类型数组
        T max;               //max用来存放数组中的最大值
    };
    template<class T>
    void Array_max<T>::set_value( )
    {
        int i;
        for (i=0; i<10; i++)
            cin>>array[i];
    }
    template<class T>
    T Array_max<T>::max_value( )
    {
        int i;
        max=array[0];
        for (i=1; i<10; i++)
            max=max<array[i]?array[i]:max;
        return max;
    }
    int main( )
    {
        Array_max<int>   arrmax_int; //定义对象arrmax_int,该对象中数组元素类型为整型
        arrmax_int.set_value( );      //调用arrmax_int的set_value函数,向数组元素输入数值
        cout<<arrmax_int.max_value( )<<endl;  //调用arrmax_int的max_value函数,找出数组元素中的最大值
    
    
        Array_max<double>   arrmax_double; //定义对象arrmax_double,该对象中数组元素类型为双精度型
        arrmax_double.set_value( );      //调用arrmax_double的set_value函数,向数组元素输入数值
        cout<<arrmax_double.max_value( )<<endl;  //调用arrmax_double的max_value函数,找出数组元素中的最大值
    
    
        Array_max<char>   arrmax_char; //定义对象arrmax_char,该对象中数组元素类型为字符型
        arrmax_char.set_value( );      //调用arrmax_char的set_value函数,向数组元素输入数值
        cout<<arrmax_char.max_value( )<<endl;  //调用arrmax_char的max_value函数,找出数组元素中的最大值
    
    
        Array_max<string>   arrmax_string; //定义对象arrmax_string,该对象中数组元素类型为字符串型
        arrmax_string.set_value( );      //调用arrmax_string的set_value函数,向数组元素输入数值
        cout<<arrmax_string.max_value( )<<endl;  //调用arrmax_string,的max_value函数,找出数组元素中的最大值
    
        return 0;
    }
    

  • 相关阅读:
    MySQL "show users"
    MySQL
    A MySQL 'create table' syntax example
    MySQL backup
    MySQL show status
    Tomcat, pathinfo, and servlets
    Servlet forward example
    Servlet redirect example
    Java servlet example
    How to forward from one JSP to another JSP
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989504.html
Copyright © 2011-2022 走看看