HW 13

我把时间荒废了,时间便把我荒废了

            —— 莎士比亚

Problem A: 建筑物

题面:

main函数:

int main()
{
    Building b;
    int i;
    cin>>i;
    b.setCnt(i);
    cout<<"The building has "<<b.getCnt()<<" floors."<<endl;
    return 0;
}

考点:类的使用

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Building
{
public:
    int n;
    void setCnt(int x)
    {
        n = x;
    }
    int getCnt(){return n;}
};

Problem B: 一个人的生日

题面:

main函数:

int main()
{
    string name;
    int year, month, day;
    cin>>year>>month>>day;
    Date date(year, month, day);
    cin>>name>>year>>month>>day;
    Person person(year, month, day, name);
    return 0;
}

考点:类的使用

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Date
{
public:
   int y,m,d;
   Date(int _y,int _m,int _d):y(_y),m(_m),d(_d)
   {
       printf("Date %d-%d-%d is created.\n",y,m,d);
   }
   ~Date()
   {
       printf("Date %d-%d-%d is erased.\n",y,m,d);
   }
};
class Person
{
public:
    Date da;
    string name;
    Person(int y,int m,int d,string _s):da(y,m,d),name(_s)
    {
        printf("Person ");
        cout << name;
        printf(" was born at %d-%d-%d is created.\n",da.y,da.m,da.d);
    }
    ~Person()
    {
        printf("Person ");
        cout << name;
        printf(" was born at %d-%d-%d is erased.\n",da.y,da.m,da.d);
    }
};

Problem C: 类型可变的类

题面:

main函数:

int main()
{
    Data d1;
    int i;
    cin>>i;
    Data d2(i);
    double d;
    cin>>d;
    Data d3(d);
    return 0;
}

考点:重载

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Data
{
public:
    int flag ;
    int i;
    double d;
    Data():flag(0){printf("A default object is created.\n");}
    Data(int _d):flag(1),i(_d){
    printf("An integer object %d is created.\n",i);
    }
    Data(double _d):flag(2),d(_d){
        cout << "A double object ";
        cout << d ;
        cout << " is created." << endl;
    }
    ~Data()
    {
        if(flag==0)
        {
            printf("The default object is erased.\n");
        }
        else if(flag==1)
        {
            printf("The integer object %d is erased.\n",i);
        }
        else if(flag==2)
        {
             cout << "The double object ";
        cout << d ;
        cout << " is erased." << endl;
        }
    }
};

Problem D: 学校的构成(I)

题面:

main函数:

int main()
{
    int g;
    double s;
    cin>>g>>s;
    School sch(g, s);
    cin>>g;
    Student stu(g);
    cin>>s;
    Teacher tea(s);
    School(g, s);
    return 0;
}

考点:类的组合

AC代码:

include <bits/stdc++.h>
using namespace std;
class Student
{
    public:
    int n;
    Student(int _n):n(_n)
    {
        printf("A student grade %d is created.\n",n);
    }
    ~Student()
    {
        printf("A student grade %d is erased.\n",n);
    }
};
class Teacher
{
public:
    double wage;
    Teacher(double _d):wage(_d)
    {
        printf("A teacher with salary ");
        cout << wage ;
        printf(" is created.\n");
    }
    ~Teacher()
    {
        printf("A teacher with salary ");
        cout << wage ;
        printf(" is erased.\n");
    }
};
class School
{
public:
    Student stu;
    Teacher tea;
    School(int _g,double _s):stu(_g),tea(_s)
    {
 
        printf("A school is created.\n");
    }
    ~School()
    {
        printf("A school is erased.\n");
    }
};

Problem E: 简单类定义

题面:

main函数:

int main()
{
    Test t1;
    int i;
    cin>>i;
 
    Test t2(i);
    cout<<"t2 is "<<t2.getMem()<<"."<<endl;
    cin>>i;
    t2.setMem(i);
    cout<<"t2 is "<<t2.getMem()<<"."<<endl;
    return 0;
}

考点:类的使用

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Test
{
public:
    int d;
    Test(int _d=0):d(_d)
    {
        printf("Test %d is created.\n",d);
    }
    void setMem(int x)
    {
        d = x;
    }
    int getMem()
    {
        return d;
    }
    ~Test()
    {
        printf("Test %d is erased.\n",d);
    }
};

Problem F: 圆的面积

题面:

main函数:

int main()
{
    double radius;
    cout<<"PI="<<Circle::PI<<endl;
    const Circle c1(2);
    cout<<"radius="<<c1.getRadius();
    cout<<",area="<<c1.getArea()<<endl;
    cin>>radius;
    Circle c2(radius);
    cout<<"radius="<<c2.getRadius();
    cout<<",area="<<c2.getArea()<<endl;
    return 0;
}

考点:类的使用

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Circle
{
public:
    double r;
    static double PI;
    Circle(double _r):r(_r)
    {
    }
    double getArea()const
    {
        return PI*r*r;
    }
    double getRadius()const
    {
        return r;
    }
};

Problem G: 新型乘法运算

题面:

main函数:

int main()
{
    Integer M, N;
    int a, n, m;
    cin>>a;
    M.setValue(a);
    cin>>n;
    while (n--)
    {
        cin>>m;
        N = M * m;
        cout<<N.getValue()<<endl;
    }
    return 0;
}

考点:先求位数,然后乘10的位数方然后再加上原来的数

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Integer
{
public:
    int v;
    Integer(int _v=0):v(_v){}
    void setValue(int _v)
    {
        v = _v;
    }
    int getValue()
    {
        return v;
    }
    Integer operator *(int d)
    {
        int x = 1,k = v;
        while(k)
        {
            //cout << 2 << endl;
            x*=10;
            k/=10;
        }
        k = 0;
        for(int i = 0;i<d;i++)
            k= k*x+v;
        //cout << k << endl;
        return Integer(k);
 
    }
    Integer operator =(Integer b)
    {
        v = b.v;
        return *this;
    }
};

Problem H: 高阶多项式(IV)

题面:

 

 

main函数:

int main()
{
    int n, i;
    cin>>n;
    for (i = 0; i < n; i++)
    {
        Equation eq;
        cin>>eq;
        cout<<eq;
    }
    return 0;
}

 

 考点:运算符的重载,输出格式控制

AC代码:

int main()
{
    int n, i;
    cin>>n;
    for (i = 0; i < n; i++)
    {
        Equation eq;
        cin>>eq;
        cout<<eq;
    }
    return 0;
}

Problem I: 老师的工资

题面:

main函数:

int main()
{
    int N, i, m;
    char ch;
    double basis;
    Teacher *t;
    cin>>N;
    for (i = 0; i < N; i++)
    {
        cin>>ch>>basis>>m;
        if (ch == 'h')
            t = new HighSchoolTeacher(basis, m);
        else if (ch == 'u')
            t  = new UniversityTeacher(basis, m);
        cout<<setprecision(2)<<setiosflags(ios::fixed)<<t->getSalary()<<endl;
        delete t;
    }
    return 0;
}

考点:类的继承,虚基类

AC代码:

#include <bits/stdc++.h>
using namespace std;
class Teacher
{
public:
    double b;
    int m;
    Teacher(double _b,int _m):b(_b),m(_m)
    {
        printf("Teacher's constructor.\n");
    }
    virtual double getSalary()=0;
    virtual ~Teacher()
    {
        printf("Teacher's deconstructor.\n");
    }
};
class HighSchoolTeacher:public Teacher
{
public:
    HighSchoolTeacher(double _b,int _m):Teacher(_b,_m)
    {
        printf("HighSchoolTeacher's constructor.\n");
    }
    double getSalary()
    {
        //cout << b << " " << m << endl;
        return b+m*100.0;
    }
    ~HighSchoolTeacher()
    {
        printf("HighSchoolTeacher's deconstructor.\n");
    }
 
};
class UniversityTeacher:public Teacher
{
public:
    UniversityTeacher(double _d,int m):Teacher(_d,m)
    {
        printf("UniversityTeacher's constructor.\n");
    }
    double getSalary()
    {
        if(m<240)
        return b + (m-240)*20;
        else if(m>240)
            return b+(m-240)*40;
    }
    ~UniversityTeacher()
    {
        printf("UniversityTeacher's deconstructor.\n");
    }
};

 

posted @ 2019-06-27 20:13  浅花迷人  阅读(...)  评论(...编辑  收藏