2017 程序设计实习之C++部分作业题汇总 - F:模板 template

题目来源:2017 程序设计实习之C++部分作业题汇总

1、F01:这个模板并不难

总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空,输出指定结果

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
template <class T>  
class myclass {
// 在此处补充你的代码
private:
    int size;
    T * p;
public:
    myclass(T *ptr, int len) :size(len)
    {
        p = new T[len + 1];
        memcpy(p, ptr, len * sizeof(T));
    }
// 在此结束补充的代码
~myclass( ) {
        delete [] p;
    }
    void Show()
    {
        //size需要作为一个成员变量
        for( int i = 0;i < size;i ++ ) {
            cout << p[i] << ",";
        }
        cout << endl;
    }
};
int a[100];
int main() {
    char line[100];
    while( cin >> line ) {
        //myclass<T> obj(T*,int),其中T = char
        myclass<char> obj(line,strlen(line));
        obj.Show();
        int n;
        cin >> n;
        for(int i = 0;i < n; ++i)
            cin >> a[i];
        //myclass<T> obj(T*,int),其中T = int
        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

2、F02:排序,又见排序!

总时间限制: 1000ms 内存限制: 65536kB
描述
自己编写一个能对任何类型的数组进行排序的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>
//需要补全mysort的参数,然后根据函数指针定义的比较规则在指定的左闭右开的区间上排序
void mysort(T1 b,T1 e,bool op(T2,T2))
// 在此处补充你的代码
// int与int *是不同的类型
{
    //miss algorithm,无法使用sort函数
    //qsort(T* base,int size,int wide,int cmp(const void *a,const void *b))
    //qsort的函数指针的返回值类型也不是bool而是int,故也不好调用qsort
    //自己写个排序函数,选insert sort
    int size = e - b;
    for(int i = 1;i<size;++i)
    {
        int j = i - 1;
        T2 tmp = b[i];
        //根据op定义的比较规则:Greater1与Greater3都将小的排前面,Greater2将大的排前面
        while(j>=0 && op(tmp,b[j]))
        {
            b[j+1] = b[j];
            --j;
        }
        b[j+1] = tmp;
    }
}
// 在此结束补充的代码
#define NUM 5
int main()
{
    int an[NUM] = { 8,123,11,10,4 };
    //mysort(T,T,函数指针op),其中T = int*,op = Greater1
    mysort(an,an+NUM,Greater1); //从小到大排序 
    for( int i = 0;i < NUM; i ++ )
       cout << an[i] << ",";
    //mysort(T,T,函数指针op),其中T = int*,op = Greater2
    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(T,T,函数指针op),其中T = double*,op = Greater3
    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

3、F03:山寨版istream_iterator

总时间限制: 1000ms 内存限制: 65536kB
描述
模仿C++标准模板库istream_iterator用法,实现CMyistream_iterator使得程序按要求输出

#include <iostream>
#include <string>

using namespace std;
template <class T>
class CMyistream_iterator
{
// 在此处补充你的代码
private:
    T * ptr;
    istream & is;
public:
    CMyistream_iterator(istream &is_):is(is_)
    {
        ptr = nullptr;
    }
    T & operator *()
    {
        if(ptr)
            return *ptr;
        ptr = new T;
        is >> *ptr;
        return *ptr;
    }
    void operator++(int)//需要重载的后置的++运算符
    {
        delete ptr;
        ptr = nullptr;
    }
    ~CMyistream_iterator()
    {
        if(ptr)
            delete ptr;
    }
// 在此结束补充的代码
};

int main()  
{ 
    int t;
    cin >> t;
    while( t -- ) {
         //T = int,实参cin,参数类型为&
         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

4、F04:简单的SumArray

总时间限制: 1000ms 内存限制: 65536kB
描述
填写模板 PrintArray,使得程序输出结果是: TomJackMaryJohn 10 不得编写SumArray函数

#include <iostream>
#include <string>
using namespace std;
template <class T>
//返回值类型为T,形参的类型只能为T*
T SumArray(T * b,T * e)
// 在此处补充你的代码
{
    T re = *b++;
    while(b!=e)
        re += *b++;
    return re;
}
// 在此结束补充的代码
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

5、F05:简单的foreach

总时间限制: 1000ms 内存限制: 65536kB
描述
编写MyForeach模板,使程序按要求输出 不得编写 MyForeach函数

#include <iostream>
#include <string>
using namespace std;
// 在此处补充你的代码
// 根据MyForeach(array,array+m,Print)与MyForeach(a,a+n,Inc)分析
// MyForeach()的参数,前两个是T1型的参数,第三个是函数指针op
// 函数指针返回值相同,参数的类型不同,且不同于T1,设定为T2
template<class T1,class T2>
void MyForeach(T1 b,T1 e,void op(T2 rhs))
{
    while(b!=e)
        op(*b++);
}
// 在此结束补充的代码
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

6、F06:简单的Filter

总时间限制: 1000ms 内存限制: 65536kB
描述
编写Filter模板,使得程序产生指定输出 不得编写 Filter函数

#include <iostream>
#include <string>
using namespace std;
// 在此处补充你的代码
// 根据
// string * p = Filter(as1,as1+5,as2,LongerThan3)
// int * p2 = Filter(a1,a1+5,a2,LargerThan2)
// Filter函数的前3个参数的类型一致,不妨设为T1,最后一个参数为函数指针op
// op的返回值确定为bool,参数为基本类型,不妨设为T2,返回值类型为T1
// 返回值指向第3个参数所在的空间的有效值结束位置的下一个位置的指针
template<class T1,class T2>
T1 Filter(T1 bt1,T1 et1,T1 bt2,bool op(T2 rhs))
{
    while(bt1!=et1)
    {
        if(op(*bt1))
            *bt2++ = *bt1;
        ++bt1; 
    }
    return bt2;
}
// 在此处结束补充的代码
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
查看

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

总时间限制: 1000ms 内存限制: 65536kB
描述
读入两个整数,输出两个整数 ,直到碰到-1

#include <iostream>
using namespace std;
class MyCin
{
// 在此处补充你的代码
private:
    bool status;
public:
    MyCin():status(true){}
    MyCin & operator >> (int & n)
    {
        if (!status)
            return *this;
        cin >> n;
        if (n == -1)
            status = false;
    }
    operator bool()
    {
        return status;
    }
// 在此结束补充的代码
};
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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值