【C++ | PTA】数据的间距问题(重载+函数模板)

文章目录

题目要求

三个类如下设计:类cTime有三个数据成员,hh,mm,ss,分别代表时,分和秒,并有若干构造函数和一个重载-(减号)的成员函数。类point有两个数据成员,x,y分别坐标,并有若干构造函数和一个重载-(减号)的成员函数。类date有三个数据成员,year,month,day分别代表年月日,并有若干构造函数和一个重载-(减号)的成员函数。
要求设计一个函数模板template <\class T>\ double dist(T a, T b) 对int,float,cTime,point和date或者其他类型的数据,返回间距。

其中,hh = 3600 * ss, mm = 60 * ss, year = 365 * day, month = 30 * day,对于cTime和date类型,数据在转换成ss或者day后进行运算。

输入格式:
每一行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为point类型,4,为time类型,5为date类型,若为整型元素,接着输入两个整型数据,

若为浮点型元素,接着输入两个浮点型数据,若为point型元素,输入两个point型数据(x1 y1 x2 y2),若为time型元素, 输入两个cTime型数据(hh1 mm1 ss1 hh2 mm2 ss2),若为date型数据,输入两个date型数据(year1 month1 day1 year2 month2 day2)。输入0时标志输入结束。

输出格式:
对每个输入,每行输出一个间距值。

样例输入:
1 2 5
4 18 21 22 18 20 31
3 2 4 5 9
5 2013 5 14 2013 5 15
2 2.2 9.9
0

样例输出:
3
51
5.83095
1
7.7

代码

#include<iostream>
#include<cmath>
using namespace std;

// 定义Time类
class Time {
private:
    int hh; // 时
    int mm; // 分
    int ss; // 秒
public:
    Time() { // 构造函数
        hh = 0;
        mm = 0;
        ss = 0;
    }
    void set(int h, int m, int s) { // 设置 hh,mm,ss 的值
        hh = h;
        mm = m;
        ss = s;
    }
    friend int operator-(Time, Time); // 友元函数,重载 - 运算符
    friend istream& operator>>(istream&, Time&); // 友元函数,重载输入运算符 >>
};

// 重载输入运算符 >>
istream& operator>>(istream& set, Time& t) {
    set >> t.hh >> t.mm >> t.ss; // 从标准输入中读入数据
    return set;
}

// 重载 - 运算符 
int operator-(Time t1, Time t2) {
    int totalSecond1 = t1.hh * 3600 + t1.mm * 60 + t1.ss; // 计算总秒数
    int totalSecond2 = t2.hh * 3600 + t2.mm * 60 + t2.ss;
    return totalSecond2 - totalSecond1; // 返回时间间隔
}

// 定义Date类
class Date {
private:
    int year; // 年
    int month; // 月
    int day; // 日
public:
    Date() { // 构造函数
        year = 0;
        month = 0;
        day = 0;
    }
    void set(int y, int m, int d) { // 设置 year,month,day 的值
        year = y;
        month = m;
        day = d;
    }
    friend int operator-(Date, Date); // 友元函数,重载 - 运算符
    friend istream& operator>>(istream&, Date&); // 友元函数,重载输入运算符 >>
};

// 重载输入运算符 >>
istream& operator>>(istream& set, Date& d) {
    set >> d.year >> d.month >> d.day; // 从标准输入中读入数据
    return set;
}

// 重载 - 运算符 
int operator-(Date d1, Date d2) {
    int totalDay1 = d1.year * 365 + d1.month * 30 + d1.day; // 计算总天数
    int totalDay2 = d2.year * 365 + d2.month * 30 + d2.day;
    return totalDay1 - totalDay2; // 返回日期间隔天数
}

// 定义 Point 类
class Point {
private:
    int x;
    int y;
public:
    Point() { // 构造函数
        x = y = 0;
    }
    void set(int a, int b) { // 设置 x 和 y 的值
        x = a;
        y = b;
    }
    friend double operator-(Point, Point); // 友元函数,重载 - 运算符
    friend istream& operator>>(istream&, Point&); // 友元函数,重载输入运算符 >>
};

// 重载输入运算符 >>
istream& operator>>(istream& set, Point& p) {
    set >> p.x >> p.y; // 从标准输入中读入数据
    return set;
}

// 重载 - 运算符 
double operator-(Point p1, Point p2) {
    return sqrt((1.0 * p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); // 返回点间距离
}

// 主函数
int main() {
    int classType;
    while (cin >> classType) { // 循环读入类别
        if (classType == 0) break; // 若为 0,则退出循环
        switch (classType) { // 根据类别选择不同的操作
            case 1: { // 整型元素
                int a, b;
                cin >> a >> b;
                cout << abs(b - a) << endl; // 输出 a 和 b 之间的距离
                break;
            }
            case 2: { // 浮点型元素
                double a, b;
                cin >> a >> b;
                cout << abs(b - a) << endl; // 输出 a 和 b 之间的距离
                break;
            }
            case 3: { // Point 类型
                Point a, b;
                cin >> a >> b;
                cout << abs(b - a) << endl; // 输出 a 和 b 之间的距离
                break;
            }
            case 4: { // Time 类型
                Time a, b;
                cin >> a >> b;
                cout << abs(b - a) << endl; // 输出 a 和 b 之间的时间间隔
                break;
            }
            case 5: { // Date 类型
                Date a, b;
                cin >> a >> b;
                cout << abs(b - a) << endl; // 输出 a 和 b 之间的日期间隔天数
                break;
            }
        }
    }
    return 0;
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋说

感谢打赏,祝你平安喜乐。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值