zoukankan      html  css  js  c++  java
  • 18.03.31 模板作业

    A:这个模板并不难

    描述

    程序填空,输出指定结果

    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    template <class T>  
    class myclass {
    // 在此处补充你的代码
    ~myclass( ) {
            delete [] p;
        }
        void Show()
        {
            for( int i = 0;i < size;i ++ ) {
                cout << p[i] << ",";
            }
            cout << endl;
        }
    };
    int a[100];
    int main() {
        char line[100];
        while( cin >> line ) {
            myclass<char> obj(line,strlen(line));;
            obj.Show();
            int n;
            cin >> n;
            for(int i = 0;i < n; ++i)
                cin >> a[i];
            myclass<int> obj2(a,n);
            obj2.Show();
        }
        return 0;
    }

    输入

    多组数据。每组第一行是一个不含空格的字符串
    第二行是整数n
    第三行是n个整数输出对每组数据,先依次输出输入字符串的每个字母,并且在每个字母后面加逗号
    然后依次再输出输入的n个整数 ,在每个整数后面加逗号样例输入

    Tom 
    3
    3 4 5
    Jack
    4
    1 2 3 4

    样例输出

    T,o,m,
    3,4,5,
    J,a,c,k,
    1,2,3,4,

    来源Guo Wei

     1 #include <iostream>
     2 #include <string>
     3 #include <cstring>
     4 using namespace std;
     5 template <class T>  
     6 class myclass {
     7     //s
     8 public:
     9     T *p;
    10     int size;
    11     myclass(T *line, int len) {
    12         p = new T[len+1];
    13         size = len;
    14         for (int i = 0; i<size; i++)
    15             p[i] = line[i];
    16         p[len] = 0;
    17     }
    18     //e
    19 ~myclass( ) {
    20         delete [] p;
    21     }
    22     void Show()
    23     {
    24         for( int i = 0;i < size;i ++ ) {
    25             cout << p[i] << ",";
    26         }
    27         cout << endl;
    28     }
    29 };
    30 int a[100];
    31 int main() {
    32     char line[100];
    33     while( cin >> line ) {
    34         myclass<char> obj(line,strlen(line));;
    35         obj.Show();
    36         int n;
    37         cin >> n;
    38         for(int i = 0;i < n; ++i)
    39             cin >> a[i];
    40         myclass<int> obj2(a,n);
    41         obj2.Show();
    42     }
    43     return 0;
    44 }
    View Code

    构造函数不能加 if(p)delete[]p 

    B:排序,又见排序!

    描述

    自己编写一个能对任何类型的数组进行排序的mysort函数模版。只能写一个mysort模板,不能写mysort函数!

    #include <iostream>
    using namespace std;
    
    bool Greater2(int n1,int n2) 
    {
        return n1 > n2;
    }
    bool Greater1(int n1,int n2) 
    {
        return n1 < n2;
    }
    bool Greater3(double d1,double d2)
    {
        return d1 < d2;
    }
    
    template <class T1,class T2>
    void mysort(
    // 在此处补充你的代码
    #define NUM 5
    int main()
    {
        int an[NUM] = { 8,123,11,10,4 };
        mysort(an,an+NUM,Greater1); //从小到大排序 
        for( int i = 0;i < NUM; i ++ )
           cout << an[i] << ",";
        mysort(an,an+NUM,Greater2); //从大到小排序 
        cout << endl;
        for( int i = 0;i < NUM; i ++ )
            cout << an[i] << ","; 
        cout << endl;
        double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1};
        mysort(d+1,d+5,Greater3); //将数组从下标1到下标4从小到大排序 
        for( int i = 0;i < 6; i ++ )
             cout << d[i] << ","; 
        return 0;
    }

    输入

    输出4,8,10,11,123,
    123,11,10,8,4,
    1.4,1.2,1.8,3.1,3.2,2.1,样例输入

    样例输出

    4,8,10,11,123,
    123,11,10,8,4,
    1.4,1.2,1.8,3.1,3.2,2.1,

    来源Guo Wei

     1 #include <iostream>
     2 using namespace std;
     3 
     4 bool Greater2(int n1,int n2) 
     5 {
     6     return n1 > n2;
     7 }
     8 bool Greater1(int n1,int n2) 
     9 {
    10     return n1 < n2;
    11 }
    12 bool Greater3(double d1,double d2)
    13 {
    14     return d1 < d2;
    15 }
    16 
    17 template <class T1,class T2>
    18 void mysort(
    19 T1*s, T1*e, T2(*h)(T1 a, T1 b)) {
    20     int gap = e - s;
    21     for(int i=0;i<gap-1;i++)
    22         for (int j = 0; j < gap -1- i; j++)
    23         {
    24             T1 swap;
    25             if (!(*h)(*(s+j),*(s+j+1)))
    26             {
    27                 swap = *(s + j);
    28                 *(s + j) = *(s + j + 1);
    29                 *(s + j + 1) = swap;
    30             }
    31         }
    32 }
    33 #define NUM 5
    34 int main()
    35 {
    36     int an[NUM] = { 8,123,11,10,4 };
    37     mysort(an,an+NUM,Greater1); //从小到大排序 
    38     for( int i = 0;i < NUM; i ++ )
    39        cout << an[i] << ",";
    40     mysort(an,an+NUM,Greater2); //从大到小排序 
    41     cout << endl;
    42     for( int i = 0;i < NUM; i ++ )
    43         cout << an[i] << ","; 
    44     cout << endl;
    45     double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1};
    46     mysort(d+1,d+5,Greater3); //将数组从下标1到下标4从小到大排序 
    47     for( int i = 0;i < 6; i ++ )
    48          cout << d[i] << ","; 
    49     return 0;
    50 }
    View Code

    C:山寨版istream_iterator

    描述

    模仿C++标准模板库istream_iterator用法,实现CMyistream_iterator使得程序按要求输出

    #include <iostream>
    #include <string>
    
    using namespace std;
    template <class T>
    class CMyistream_iterator
    {
    // 在此处补充你的代码
    };
    
    
    
    int main()  
    { 
        int t;
        cin >> t;
        while( t -- ) {
             CMyistream_iterator<int> inputInt(cin);
             int n1,n2,n3;
             n1 = * inputInt; //读入 n1
             int tmp = * inputInt;
             cout << tmp << endl;
             inputInt ++;   
             n2 = * inputInt; //读入 n2
             inputInt ++;
             n3 = * inputInt; //读入 n3
             cout << n1 << " " << n2<< " " << n3 << " ";
             CMyistream_iterator<string> inputStr(cin);
             string s1,s2;
             s1 = * inputStr;
             inputStr ++;
             s2 = * inputStr;
             cout << s1 << " " << s2 << endl;
        }
         return 0;  
    }

    输入

    第一行是整数t,表示有t组数据
    每组数据一行,三个整数加两个字符串。字符串是不含空格的

    输出

    对每组数据,输出二行 
    在第一行输出第一个数
    第二行原样输出输入的内容

    样例输入

    2
    79 90 20 hello me
    12 34 19 take up

    样例输出

    79
    79 90 20 hello me
    12
    12 34 19 take up
    

    提示C++标准模板库 istream_iterator模版使用说明:

    其构造函数执行过程中就会要求输入,然后每次执行++,则读取输入流中的下一个项目,执行 * 则返回上次从输入流中读取的项目。例如,下面程序运行时,就会等待用户输入数据,输入数据后程序才会结束:
    #include 
    #include 
    using namespace std;
    int main() { 
    istream_iterator inputInt(cin);
    return 0; 
    }

    下面程序运行时,如果输入 12 34 程序输出结果是: 12,12
    #include 
    #include 
    using namespace std;
    int main() 

    istream_iterator inputInt(cin);
    cout << * inputInt << "," << * inputInt << endl;
    return 0; 
    }

    下面程序运行时,如果输入 12 34 56程序输出结果是: 12,56
    #include 
    #include 
    using namespace std;
    int main() 

    istream_iterator inputInt(cin);
    cout << * inputInt << "," ;
    inputInt ++;
    inputInt ++;
    cout << * inputInt;
    return 0; 
    }

    来源Guo Wei

     1 #include <iostream>
     2 #include <string>
     3 
     4 using namespace std;
     5 template <class T>
     6 class CMyistream_iterator
     7 {
     8     // 在此处补充你的代码
     9 public:
    10     istream &in;
    11     T now;
    12     CMyistream_iterator(istream &c):in(c) {
    13         cin >> now;
    14     }
    15     T operator*() {
    16         return now;
    17     }
    18     void operator++(int) {
    19         cin >> now;
    20     }
    21 };
    22 
    23 
    24 
    25 int main()  
    26 { 
    27     int t;
    28     cin >> t;
    29     while( t -- ) {
    30          CMyistream_iterator<int> inputInt(cin);
    31          int n1,n2,n3;
    32          n1 = * inputInt; //读入 n1
    33          int tmp = * inputInt;
    34          cout << tmp << endl;
    35          inputInt ++;   
    36          n2 = * inputInt; //读入 n2
    37          inputInt ++;
    38          n3 = * inputInt; //读入 n3
    39          cout << n1 << " " << n2<< " " << n3 << " ";
    40          CMyistream_iterator<string> inputStr(cin);
    41          string s1,s2;
    42          s1 = * inputStr;
    43          inputStr ++;
    44          s2 = * inputStr;
    45          cout << s1 << " " << s2 << endl;
    46     }
    47      return 0;  
    48 }
    View Code

    D:简单的SumArray

    描述

    填写模板 PrintArray,使得程序输出结果是: TomJackMaryJohn 10 不得编写SumArray函数

    #include <iostream>
    #include <string>
    using namespace std;
    template <class T>
    T SumArray(
    // 在此处补充你的代码
    }
    int main() {
        string array[4] = { "Tom","Jack","Mary","John"};
        cout << SumArray(array,array+4) << endl;
        int a[4] = { 1, 2, 3, 4};  //提示:1+2+3+4 = 10
        cout << SumArray(a,a+4) << endl;
        return 0;
    }

    输入

    输出

    TomJackMaryJohn
    10

    样例输入

    样例输出

    TomJackMaryJohn
    10
    

    来源Guo Wei

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 template <class T>
     5 T SumArray(
     6 T*s, T*e){
     7     int gap = e - s;
     8     T sum=*s;
     9     for (int i = 1; i < gap; i++) {
    10         sum += *(s + i);
    11     }
    12     return sum;
    13 }
    14 int main() {
    15     string array[4] = { "Tom","Jack","Mary","John"};
    16     cout << SumArray(array,array+4) << endl;
    17     int a[4] = { 1, 2, 3, 4};  //提示:1+2+3+4 = 10
    18     cout << SumArray(a,a+4) << endl;
    19     return 0;
    20 }
    View Code

    E:简单的foreach

    描述

    编写MyForeach模板,使程序按要求输出 不得编写 MyForeach函数

    #include <iostream>
    #include <string>
    using namespace std;
    // 在此处补充你的代码
    void Print(string s)
    {
        cout << s;
    }
    void Inc(int & n)
    {
        ++ n;
    }
    string array[100];
    int a[100];
    int main() {
        int m,n;
        while(cin >> m >> n) {
            for(int i = 0;i < m; ++i)
                cin >> array[i];
            for(int j = 0; j < n; ++j)
                cin >> a[j];
            MyForeach(array,array+m,Print);         
            cout << endl;
            MyForeach(a,a+n,Inc);         
            for(int i = 0;i < n; ++i)
                cout << a[i] << ",";
            cout << endl;
        }
        return 0;
    }

    输入

    多组数据

    每组数据第一行是两个整数 m 和 n ,都不超过 50

    第二行是m个不带空格的字符串
    第三行是 n个整数

    输出

    对每组数据
    第一行输出所有输入字符串连在一起的结果
    第二行输出输入中的每个整数加1的结果样例输入

    3 4
    Tom Mike Jack
    1 2 3 4
    1 2
    Peking
    100 200

    样例输出

    TomMikeJack
    2,3,4,5,
    Peking
    101,201,

    来源Guo Wei

    函数可以这么传 所以指针也可以缩成一个 type 吗

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 //s
     5 template<class T1,class T2>
     6 void MyForeach(T1* s, T1* e,T2 h ) {
     7     int gap = e - s;
     8     for (int i = 0; i < gap; i++)
     9          h(*(s + i));
    10 }
    11 //e
    12 void Print(string s)
    13 {
    14     cout << s;
    15 }
    16 void Inc(int & n)
    17 {
    18     ++ n;
    19 }
    20 string array[100];
    21 int a[100];
    22 int main() {
    23     int m,n;
    24     while(cin >> m >> n) {
    25         for(int i = 0;i < m; ++i)
    26             cin >> array[i];
    27         for(int j = 0; j < n; ++j)
    28             cin >> a[j];
    29         MyForeach(array,array+m,Print);         
    30         cout << endl;
    31         MyForeach(a,a+n,Inc);         
    32         for(int i = 0;i < n; ++i)
    33             cout << a[i] << ",";
    34         cout << endl;
    35     }
    36     return 0;
    37 }
    View Code

    F:简单的Filter

    描述

    编写Filter模板,使得程序产生指定输出 不得编写 Filter函数

    #include <iostream>
    #include <string>
    using namespace std;
    // 在此处补充你的代码
    bool LargerThan2(int n)
    {
        return n > 2;
    }
    bool LongerThan3(string s) 
    {
        return s.length() > 3;
    }
    
    string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"};
    string as2[5];
    int  a1[5] = { 1,2,3,4,5};
    int a2[5];
    int main() {
        string * p = Filter(as1,as1+5,as2,LongerThan3);
        for(int i = 0;i < p - as2; ++i)
            cout << as2[i];
        cout << endl; 
        int * p2 = Filter(a1,a1+5,a2,LargerThan2);
        for(int i = 0;i < p2-a2; ++i)
            cout << a2[i] << ",";
        return 0;
    }

    输入

    输出

    MikeJackLucy
    3,4,5,样例输入

    样例输出

    MikeJackLucy
    3,4,5,

    来源Guo Wei

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 //s
     5 template <class T1,class T2>
     6 T1* Filter(T1* s, T1* e, T1* to, T2 h) {
     7     int p = -1;
     8     int gap = e - s;
     9     for (int i = 0; i < gap; i++) {
    10         if (h(*(s+i)))
    11             *(to + (++p)) = *(s+i);
    12     }
    13     return (to + p+1);
    14 }
    15 //e
    16 bool LargerThan2(int n)
    17 {
    18     return n > 2;
    19 }
    20 bool LongerThan3(string s) 
    21 {
    22     return s.length() > 3;
    23 }
    24 
    25 string as1[5] = {"Tom","Mike","Jack","Ted","Lucy"};
    26 string as2[5];
    27 int  a1[5] = { 1,2,3,4,5};
    28 int a2[5];
    29 int main() {
    30     string * p = Filter(as1,as1+5,as2,LongerThan3);
    31     for(int i = 0;i < p - as2; ++i)
    32         cout << as2[i];
    33     cout << endl; 
    34     int * p2 = Filter(a1,a1+5,a2,LargerThan2);
    35     for(int i = 0;i < p2-a2; ++i)
    36         cout << a2[i] << ",";
    37     return 0;
    38 }
    View Code

    G:你真的搞清楚为啥 while(cin >> n) 能成立了吗?

    描述

    读入两个整数,输出两个整数 ,直到碰到-1

    #include <iostream>
    using namespace std;
    class MyCin
    {
    // 在此处补充你的代码
    };
    int main()
    {
        MyCin m;
        int n1,n2;
        while( m >> n1 >> n2) 
            cout  << n1 << " " << n2 << endl;
        return 0;
    }

    输入

    多组数据,每组一行,是两个整数

    输出

    对每组数据,原样输出 
    当碰到输入中出现-1 时,程序结束 
    输入中保证会有 -1

    样例输入

    12 44
    344 555
    -1
    2 3

    样例输出

    12 44
    344 555

    来源Guo Wei

     1 #include <iostream>
     2 using namespace std;
     3 class MyCin
     4 {
     5 //s
     6 public:
     7     bool n;
     8     MyCin() { n = 1; }
     9     MyCin&operator>>(int &a) {
    10         cin >> a;
    11         if(a==-1)n=0;
    12         return *this;
    13     }
    14     operator bool() {
    15         return n;
    16     }
    17 //e
    18 };
    19 int main()
    20 {
    21     MyCin m;
    22     int n1,n2;
    23     while( m >> n1 >> n2) 
    24         cout  << n1 << " " << n2 << endl;
    25     return 0;
    26 }
    View Code

    其实 operator bool 还是没清楚原理

    もう、疲れだ(*´・ω・`)

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    微软SQL Server 2005的30项顶尖特性
    UML统一建模语言 类关系
    设计模式 建造者模式
    设计模式 原型模式
    设计模式 简单工厂模式
    设计模式 适配器模式
    设计模式 适配器模式
    设计模式 单例模式
    设计模式 抽象工厂模式
    设计模式 桥接模式
  • 原文地址:https://www.cnblogs.com/yalphait/p/8681297.html
Copyright © 2011-2022 走看看