Educator:C++面向对象-STL实训

第1关:病毒复制


任务描述

本关任务:设计一个病毒类。

相关知识

本关涉及到的内容如下:

  • 拷贝构造函数
  • 重载!===运算符
拷贝构造函数

当一个构造函数的唯一一个参数就是它所在类的引用时,这个构造函数就是一个拷贝构造函数

编程要求

设计一个病毒Virus 类,它的内部有一个Gen变量,代表当前病毒对象的年龄默认值为0

当一个病毒对象通过拷贝构造函数拷贝到一个新的病毒对象时,它就将新对象的的Gen变量设为它的Gen值加1的值。

评测代码会在Begin-End区域创建一些病毒对象,把它们拷贝多份存在数组里,然后会将数组中的元素与一个int整型数据g进行g==virus[i]形式的比较,以找出Gen g 的病毒,并进行 计数

学员还需要添加适当的内容以支持声明对象数组和与一个整型变量进行比较的功能。

 
class Virus
{
/********* Begin *********/
public:
int Gen;
//病毒类的声明
Virus(){Virus(const Virus&v)
{
    Gen=v.Gen+1;
}
Virus(int g)
{
    Gen=Gen+1;
}
friend bool operator==(const int g,const Virus &t1)

    Gen =0;
}
Virus(const Virus&v)
{
    Gen=v.Gen+1;
}
Virus(int g)
{
    Gen=Gen+1;
}
friend bool operator==(const int g,const Virus &t1)

{
    return g==t1.Gen;
}
/********* End *********/
};
 

第2关:动态学生信息管理


任务描述

本关任务:编写一个能动态管理学生信息的程序。

相关知识

本关涉及的内容如下:

  • vector容器的使用
  • 容器的迭代器
  • sort函数的使用
  • find函数的使用
vector容器的使用

vector位于头文件<vector>,是C++提供的一种顺序存储的容器,能通过位置索引高效的访问容器中的任意元素,可以看做是一个长度能动态变化数组

vector类是一个模板类,使用时需要指定模板参数。注意:作为模板参数的类型要有无参数的构造函数(如果不使用Allocator)。

其具体代码如下:
 

#include <iostream>
#include <vector>
#include <string>
#include<stdio.h>
#include <algorithm>
using namespace std;
 
/********* Begin *********/
 
//自定义的类或者其他内容
class student
{
 
public:
    string name;
    int score;
    student(string na = "", int sco = 0) :name(na), score(sco)
    {
 
    }
    bool operator ==(const student& other)const
    {
        return other.name == name;
 
    }
 
};
bool cmp(student  a, student b)
{
    return a.name < b.name;
 
}
/********* End *********/
 
int main()
{
    /********* Begin *********/
    vector<student> stu1;
    int i = 0;
    char m;
    while (scanf("%c", &m) != EOF)
    {
        switch (m)
        {
        case 'A':
        {
 
            string name;
            int score;
 
            cin >> name >> score;
            student stu(name, score);
            if (stu1.empty())
            {
                stu1.push_back(stu);
            }
            else
            {
                auto it = find(stu1.begin(), stu1.end(), name);
                if (it != stu1.end())
                {
                    it->score = score;
                    break;
                }
                else
                {
                    stu1.push_back(stu);
                }
 
            }
 
            /* for (auto it = stu1.begin(); it != stu1.end(); it++)
             {
                 if (it->name == name)
                 {
                     it->score = score;
                     flag = 1;
                 }
             }
             if (flag == 1)
             {
             }
             else
             {
                stu1.push_back(stu);
             }*/
 
        }break;
        case 'P':
        {
            if (stu1.empty())
            {
                cout << "[空]" << endl;
            }
            else
 
            {
                for (auto it = stu1.begin(); it != stu1.end(); it++)
                {
                    cout << it->name << " " << it->score << endl;
                }
 
            }
        }break;
        case 'R':
        {
            string name;
            cin >> name;
            auto it = find(stu1.begin(), stu1.end(), name);
            if (it!=stu1.end())
            {
                stu1.erase(it);
            }
 
 
        }break;
        case 'S':
        {
            sort(stu1.begin(), stu1.end(), cmp);
 
        }break;
        }
    }
    /********* End *********/
}
 

第3关:还原键盘输入


任务描述

本关任务:编写一个能根据键盘操作记录还原实际输入的小程序。

相关知识

本关涉及到的内容如下:

  • list容器的使用
list容器的使用

list位于头文件<list>,是 STL 提供的一种顺序存储的容器,它不支持通过位置索引访问,但能高效的进行插入、删除元素操作,通常由双向链表实现。

list是一个模板类,使用时需要指定模板参数,而且作为参数的类型需要有无参数的构造函数(如果不使用Allocator)。

#include <iostream>
#include <string>
#include <list>
using namespace std;
 
int main()
{
    /********* Begin *********/
    //读取输入,解析并输出复原后的输出
    string s;
    list<char>l;
    list<char>::iterator it=l.begin();
    while(cin>>s){
        int i=0;
        while(i<s.size()+1){
            if(s[i]=='\0'){
                it=l.begin();
                while(it!=l.end()){cout<<*it;it++;}
                cout<<endl;
                l.clear();
                it=l.begin();
            }
            if(s[i]>='A'&&s[i]<='Z'||s[i]>='a'&&s[i]<='z'||s[i]>='0'&&s[i]<='9')
                l.insert(it,s[i]);
                    //**!为何迭代器it不用it++,不然不就一直指向l.begin()了吗?!**//
            else if(s[i]=='>')it++;
            else if(s[i]=='<')it--;
            else if(s[i]=='[')it=l.begin();
            else if(s[i]==']')it=l.end();
 
            i++;
        }
    }
    
    
    /********* End *********/
}

  • 19
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值