PTA 类&模板(一)

6-1 类模板Point的定义与使用 (10 分)
参考文献:http://c.biancheng.net/view/320.html
template <class T1, class T2>
class Point
{
    public:
        T1 x;
        T2 y;
        Point(T1 x0, T2 y0) {
            x = x0, y = y0;
        };
        T1 getX() {
            return x;
        };
        T2 getY() {
            return y;
        };
};
6-2 类模板Store的定义与使用 (10 分)
抛出异常 
以下代码 WA
template <class T>
class Store
{
    private:
        bool init = false;
    public:
        T dat;
        void putElem(T x) {
            dat = x;
            init = true;
        };
        T getElem() {
            if (init) return dat;
            else {
                 throw "No item present!";
            }
        };
};
7-1 设计一个矩形类Rectangle并创建测试程序(C++) (10 分)
#include <iostream>
using namespace std;
class Rectangle {
private:
    float width,height;
public:
    Rectangle(double a, double b) {
        width = a, height = b;
    }
    double getArea() {
        return width*height;
    }
    double getPerimeter() {
        return 2*(width+height);
    }
};
int main()
{
    float m,n;
    cin>>m>>n;
    Rectangle  r(m,n);
    cout<<r.getArea()<<endl<<r.getPerimeter()<<endl;
}
7-2 复数类的运算 (10 分)
[转载]https://blog.csdn.net/FXH_smile/article/details/105485470

7-3 复数的比较 (10 分)
重载运算符
#include <iostream>
using namespace std;

class A {
public:
    A(int x = 0, int y = 0) : x(x), y(y) {}
    void show() const;
    bool operator>(A& a);
private:
    int x, y;
};
bool A::operator>(A& a){
    return ((x*x+y*y) > (a.x*a.x+a.y*a.y));
}

int main() {
    int x,y,p,q;
    while (cin >> x >> y >> p >> q, x||y||p||q) {
        A a1(x, y);
        A a2(p, q);
        cout << ((a1 > a2) ? "true" : "false" ) << endl;
    }
    return 0;
}
7-4 分数加法运算重载 (10 分)
#include <iostream> 
#include <algorithm>
using namespace std;
class FS
{
private:
    int fz;
    int fm;
    int gcd(int x,int y){
        if (x < y) swap(x, y);
        while(y) {
            int z = x % y;
            x = y;
            y = z;
        }
        return x;
    }
        
public:
    FS(){}   
    FS(int z,int m):fz(z),fm(m){}
 
    void set(int x,int y) {fz=x; fm=y;}
    void show() const {
       cout <<fz<<"z"<<fm<<"m"<<endl;
    }
 
    FS operator + (const FS &f) {
        int y = fm * f.fm;
        int x = fz * f.fm + f.fz * fm;
        int d = gcd(x,y); 
        x = x / d, y = y / d;
        if (y < 0) {
            x = -x, y = -y;         
        }
        return FS(x,y);
    }
};

int main()
{
    int n, p, q;
    char a,b;   
    cin >> n;
    while (n--)
    {
        cin >> q >> a >> p >> b;
        FS f(q, p);
        cin >> q >> a >> p >> b;
        FS g(q, p);
        f = f + g;
        f.show();
    }
    return 0;
}
7-5 分钟秒钟的时间相减 (10 分)
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

class Time 
{
    private:
        int hour, minute;
    public:
        Time() {}
        Time(int h, int m):hour(h),minute(m) {};
        
        Time operator - (const Time& t) {
            if (t.minute <= minute) {
                this->minute -= t.minute;
                this->hour -= t.hour;
            } else {
                this->minute = this->minute + 60 - t.minute;
                this->hour -= (t.hour + 1);
            }
            return *this;
        }
        
        void showMinutes() const {
            cout << hour*60 + minute <<endl;
        }
    
};

int main()
{
    int h1, m1, h2, m2;
    while (scanf("%02d %02d %02d %02d", &h1, &m1, &h2, &m2))
    {
        if (!h1 && !m1 && !h2 && !m2) break;
        Time f(h1, m1);
        Time g(h2, m2);
        g = g - f;
        g.showMinutes();
    }
    return 0;
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值