c++面向对象整合练习

郭炜老师的程序设计与算法(三)期末考试,填空部分
时间:2019国庆
题目网址:http://cxsjsxmooc.openjudge.cn/2019t3summerfinal/
交流可以联系:[email protected]

——————————————————————————————————

掐点截图

1.进制转换

#include 
#include 
using namespace std;
string dec2bin(int x)
{
    string s="";
    string s1;
    while(x)
    {
        if(x%2)    s1="1";
        else    s1="0";
        x/=2;
        s=s1+s;
    }
    int l=s.size();
    string s2="";
    s2.append(31-l,'0');
    return s2+s;
}

int main(){
    int n;
    cin >> n;
    while(n--) {
        int x;
        cin >> x;
        cout << dec2bin(x) << endl;
    }
    return 0;
}

2.统计动物数量

#include 
using namespace std;
class Animal{
    public:
    static int number;
    Animal(){number++;};
    virtual ~Animal(){--number;};//使用虚析构,为了删除c2调用cat的析构
};
int Animal::number=0;

class Dog:public Animal{
public:
    static int number;//为什么静态数据成员设为私有就错了
    Dog(){++number;}
    ~Dog(){--number;}
};
int Dog::number=0;

class Cat:public Animal{
public:
    static int number;
    Cat(){++number;}
    ~Cat(){--number;}
};
int Cat::number=0;
void print() {//这里使用类内作用域限定符不能访问私有数据吗?
    cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}

int main() {
    print();
    Dog d1, d2;
    Cat c1;
    print();
    Dog* d3 = new Dog();
    Animal* c2 = new Cat;
    Cat* c3 = new Cat;
    print();
    delete c3;
    delete c2;
    delete d3;
    print();
}

3.简单的计算(模板+重载了圆括号)

#include 
using namespace std;
template 
class Add{
public:
    T m;
    Add(T g):m(g){
    };
    T operator()(T a1,T a2){
        return a1+a2-m;
    }
};

int main(){
    double f;
    int n;
    while( cin >> f >> n) {

        Add a1(f);
        Add a2(n);
        double x,y;
        int p,q;
        cin >> x >> y >> p >> q;
        cout << a1(x, y) << endl;
        cout << a2(p, q) << endl;
    }
    return 0;
}

4.MyClass(复制构造函数,不过乘了个倍数是最佳方法吗?)

#include 
using namespace std;
class CMyClassA {
    int val;
public:
    CMyClassA(int);
    void virtual print();
};
CMyClassA::CMyClassA(int arg) {
    val = arg;
    printf("A:%d\n", val);
}
void CMyClassA::print() {
    printf("%d\n", val);
    return;
}
class CMyClassB:public CMyClassA{
    int val;
public:
    CMyClassB(int a):CMyClassA(3*a){
        val=a;printf("B:%d\n",val);
    }
    void print(){printf("%d\n",val);
    }
};
int main(int argc, char** argv) {
    CMyClassA a(3), *ptr;
    CMyClassB b(5);
    ptr = &a; ptr->print();
    a = b;
    a.print();
    ptr = &b; ptr->print();
    return 0;
}

5.又见MyClass

#include 
#include 
#include 
#include 
using namespace std;
// 在此处补充你的代码
template
class CMyClass{
    T* p;//*位置一开始打错了。。
    int m;
public:
    CMyClass(T* q,int i){
        p=new T[i+1];
        for(int j=0;j b(a, m);
        CMyClass c(s, strlen(s));
        printf("%d %c\n", b[5], c[7]);
    }
    return 0;
}

6.去除重复元素并排序(给出了set容器直接就能完成了。。不知道copy是干什么)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main() {
    int t;
    int  a[100];
    cin >> t;
    while(t--) {
        for(int i = 0;i < 12; ++i)
            cin >> a[i];
// 在此处补充你的代码
    set b;
    int c[100];
    for(int i=0;i<12;++i)b.insert(a[i]);
    for(set::iterator it=b.begin();it!=b.end();++it)cout<<*it<<" ";
std::copy(b.begin(), b.end(), c);
        cout << endl;

    }
    return 0;
}

8.还是Fun和Do(理顺继承关系以及函数的调用)

#include 
using namespace std;

class A {
    public:
        virtual void Fun() {
            cout << "A::Fun" << endl;
        };
        virtual void Do() {
            cout << "A::Do" << endl;
        }
};
class B:public A{
public:
//  void Fun(){
//      cout<<"B::Fun"<
void Call1(T p)
{
    p.Fun();
    p.Do();
}
void Call2(B p) {
    p.Fun();
    p.Do();
}



int main() {
    C c;
    B b;
    Call1(b);
    Call1(c);
    Call2(c);
    return 0;
}

9.简单 的对象(常对象只能调用常成员函数)

#include 
using namespace std;
class A
{
    static int num;
public:
    A(){num+=1;}
    void func()
    {
        cout<< num <

10.回调函数(这几题挺简单。。库给的好多啊)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
class MyFunc
{
int c;

public:
MyFunc(int i):c(i){};
int operator()(int m){
return pow(m,c);
};
};
int main()
{
    int n;
    cin >> n;
    while(n--) {
        vector v;
        for (int i = 0; i < 5; ++i)
            v.push_back(MyFunc(i+1));
        int ans = 1;
        for (int i = 0; i < 5; ++i)
        {
            int m;
            cin >> m;
            ans += v[i](m);
        }
        cout << ans <

11.前k大的偶数(熟练掌握容器真的方便)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
class MyQueue
{
// 在此处补充你的代码
int k;
public:
    vector s;
    MyQueue(int m):k(m){
    };
    friend void operator>>(istream &i,MyQueue &q){
        int t;i>>t;q.s.push_back(t);
    }
    friend void operator<<(ostream &o,MyQueue &q){
        sort(q.s.begin(),q.s.end());
        int l=q.s.size();
        typename vector::iterator it=q.s.end();
        for(int i=0;i> t;
    while(t--) {
        int n, k;
        cin >> n >> k;
        MyQueue q(k);
        for (int i = 0; i < n; ++i)
            cin >> q;
        cout<

12.Printer(遍历执行遍函数)

#include
#include
#include
#include

using namespace std;


class Printer{
public:
    int x;
    Printer(int _x):x(_x){}
    void operator()(int a) {
        if (a > x)
            cout << a << ",";
    }
    void operator()(string a) {
        if (a.length() > x)
            cout << a << ",";
    }
};
int main(){

    int t;
    cin >> t;
    while(t--) {
        int n,x;
        cin>>x>>n;
        
        vector intVec;
        for(int i = 0;i < n; ++i) {
            int y;
            cin >> y;
            intVec.push_back(y);
        }
        for_each(intVec.begin(), intVec.end(), Printer(x));
        cout< strVec;
        for(int i = 0;i < n; ++i) {
            string str;
            cin >> str;
            strVec.push_back(str);
        }
        for_each(strVec.begin(), strVec.end(), Printer(x));
        cout<

13.三生三世(这题很像奥运排序,可以改进奥运排序,有函数类)

#include
#include
#include
#include
using namespace std;

class TV_Drama{
    public:
    char name[100];
    int actor;
    int story;
    int acting_skill;
TV_Drama(char *_name, int _actor, int _story, int _ac) :actor(_actor), story(_story), acting_skill(_ac) {
        int len = 0;
        for (int i = 0; _name[i] != '\0'; i++) {
            name[i] = _name[i];
            len++;
        }
        name[len] = '\0';
    }
    bool operator<(TV_Drama&l) {
        return actor > l.actor;
    }
};
void Printer(TV_Drama x) {
    cout << x.name << ";";
}
bool comparator_1(TV_Drama &x1,TV_Drama &x2) {
    return x1.story > x2.story;
}
class comparator_2{
public:
    comparator_2() {}
    bool operator() (TV_Drama &x1, TV_Drama &x2) {
        return x1.acting_skill > x2.acting_skill;
    }
};
int main(){
    list lst;
    int n;
    
    cin>>n;
    char  _name[100];
    int _actor, _story, _acting_skill;
    for (int i=0; i>_actor>>_story>>_acting_skill;
        lst.push_back(TV_Drama(_name, _actor, _story, _acting_skill));
    }

    lst.sort();
    for_each(lst.begin(), lst.end(), Printer);    
    cout<

14.又见模板

#include 
#include 
using namespace std;
template
class A{
T* p;
public:
A(T* b){
    p=new T[n+1];//一开始遗漏这句开辟空间,使得后面虽然可以通过地址访问,但是sum出错
    for(int i=0;i> t;
    while( t -- ) {
        int b1[10];
        for(int i = 0;i < 10; ++i)

            cin >> b1[i];
        A a1 = b1;//这里的=调用的还是构造函数
        cout << a1[2] << endl;


        double b2[5] ;
        for(int i = 0;i < 5; ++i)
            cin >> b2[i];

        A a2 = b2;
        cout << a2.sum() << endl;


        string b3[4] ;
        for(int i = 0;i < 4; ++i)
            cin >> b3[i];

        A a3 = b3;
        cout << a3.sum() << endl;
    }
    return 0;
}

15.矩形排序(自定义容器的比较函数,要使用友元<,友元函数类)

#include 
#include 
using namespace std;
// 在此处补充你的代码
class Rectangle{
    int a,b;
    
public:
    int m,c;
    Rectangle(int _a,int _b):a(_a),b(_b){
        m=a*b;c=2*(a+b);
    };
    friend bool operator<(const Rectangle& u,const Rectangle& r){
        if(u.m==r.m)return u.c>r.c;
        else
            return u.m>r.m;
    }
    //friend class Comp;
    friend ostream& operator<<(ostream &o,const Rectangle &a){
        o< m1;
    multiset m2;
    int n, a, b;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a >> b;
        m1.insert(Rectangle(a, b));
        m2.insert(Rectangle(a, b));
    }
    for (multiset::iterator it = m1.begin(); it != m1.end(); it++) {
        cout << *it << endl;
    }
    cout << endl;
    for (multiset::iterator it = m2.begin(); it != m2.end(); it++) {
        cout << *it << endl;
    }
    return 0;
}

16.维护平面点
这题放在最后一题,应该是新题,毫无资料。果然在思考怎样给pair排序时想了很久。
最后选择将以右下为小方向,分开判断是正常情况,还是带-1的查找情况

#include 
#include 
#include 
using namespace std;
// 在此处补充你的代码
struct myComp{
    bool operator()(const pair &a,const pair &b){
        //小于判断谁在右下方,因为数据保证,此处可以简单点,只判断x
        //这样不行,查询的时候,数据显然不满足,因为-1的干扰
        if(a.first>0&&a.second>0&&b.first>0&&b.second>0){
            return a.first>b.first;
        }
        else{
            if(a.first<0||b.first<0){
                return a.secondb.first;
            }
        }
    }
};
int main() {
    string cmd;
    set, myComp> S;
    while (cin >> cmd) {
        if (cmd == "A") {
            int x, y;
            cin >> x >> y;
            S.insert(make_pair(x, y));
        } else if (cmd == "Qx") {
            int x;
            cin >> x;
            cout << S.lower_bound(make_pair(x, -1))->second << endl;
        } else if (cmd == "Qy") {
            int y;
            cin >> y;
            cout << S.lower_bound(make_pair(-1, y))->first << endl;
        } else {
            int x, y;
            cin >> x >> y;
            S.erase(make_pair(x, y));
        }
    }
    return 0;
}

7.按要求输出(选对容器map即完,话说。。map的迭代器在元素更改之后仍然有效,真不错)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int  a[10] = {0, 6, 7, 3, 9, 5, 8, 6, 4, 9};
int  b[10] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
int main(int argc, char** argv) {

    mapc;
    map::iterator it;
for(int i=0; i<10; i++) 
        c[a[i]] = b[i];
    for(it=c.begin(); it!=c.end(); it++) 
        cout<second<<" ";
    return 0;
}

你可能感兴趣的:(c++面向对象整合练习)