SDUST 第四次作业

/*这周的作业难度整体不大,主要是新学了一种封装类的方法,对于有联系的数字进行封装,
如时间这种东西,同时还有的便是,对一些非法数据的处理,采用这种方法很方便,
再有新学的便是,运算符重载,
运算符重载主要分为: 1对于简单二元运算符的重载参照原来的运算法则进行重载,不可随意改变;
2对赋值运算符()的重载,这个在类里面很常用,因为自定义数据类型进容器有时需要排序,所以这就需要有一个
自定义的排序方式
3.要牢记:(因为这个让在回忆时记不起来了,盲点):对普通一元运算符的重载
4.对【】的重载
5.对输入输出流的重载。
运算符重载的简单形式:
*/
/*  int operator + (const Point &p, const Point &q)
    {
       return p.x_ + q.x_;
    }
注意:如果传入的参数t为const类型,则连t.gets()这个操作都不被允许
*/
/*附上最后一道题的代码,包含的知识点挺全的有输入输出操作符的重载。*/


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

class Time
{
public:
    int flag;
public:
    Time():hh(0), mm(0), ss(0), s_(0), flag(0) {}
    Time(Time & t) : hh(t.getH()), mm(t.getM()), ss(t.getS()), s_(t.getS_()), flag(t.getF()) {}
    int getF() {return flag;}
    int getH() {hh = s_/3600; return hh;}
    int getM() {mm = (s_%3600)/60; return mm;}
    int getS() {ss = s_ % 60; return ss; }
    int getS_(){s_ = hh * 3600 + mm * 60 + ss; return s_;}
    Time& operator++() // 前增
    {
        if(flag == 0)
            {s_ = s_ + 1 + 86400; s_ = s_ % 86400;}
        return *this;
    }
    Time& operator++(int)
    {
        Time tt(*this);
        if(flag == 0)
        {
            s_ = s_ + 1 + 86400;
            s_ = s_ % 86400;
        }
        return tt;
    }
    Time& operator--()
    {
        if(flag == 0)
        {
            s_ = s_ - 1 + 86400;
            s_ = s_ % 86400;
        }
        return *this;
    }
    Time& operator--(int)
    {
        Time tt(*this);
        if(flag == 0)
        {
            s_ = s_ - 1 + 86400;
            s_ = s_ % 86400;
        }
        return tt;
    }
    friend ostream &operator<<(ostream &os, Time &t);
    friend istream &operator>>(istream &is, Time &t);
private:
    int hh, mm, ss, s_;
};


istream &operator>> (istream &is, Time &t)
{
    is >> t.hh >> t.mm >> t.ss;
    if(t.hh >= 24 || t.hh < 0 || t.mm >= 60 || t.mm  < 0 || t.ss >= 60 || t.ss < 0 || t.s_ > 86400)
        {
            t.flag = 1;
            if(t.hh == 24 && t.mm == 60 && t.ss == 60)
                t.flag = 2;
        }
    else t.flag = 0;
    t.s_ = t.hh * 3600 + t.mm * 60 + t.ss;
    return is;
}


ostream &operator<< (ostream &os, Time & t)
{
    if(t.flag == 0)
    {os << setw(2) << setfill('0') << t.getH() << ":";
     os << setw(2) << setfill('0') << t.getM() << ":";
     os << setw(2) << setfill('0') << t.getS();
    }
    else if(t.flag == 2)
    {
        os << "24:60:60";
    }
    else
    os << "error!!!" ;
    return os;
}

int main()
{
    Time t;
    int cases;
    cin>>cases;
    cout<<setw(8)<<left<<"++t"<<" ";
    cout<<setw(8)<<left<<"--t"<<" ";
    cout<<setw(8)<<left<<"t"<<" ";
    cout<<setw(8)<<left<<"t--"<<" ";
    cout<<setw(8)<<left<<"t++"<<" ";
    cout<<setw(8)<<left<<"t"<<right<<endl;
    for(int i = 1; i <= cases; ++i)
    {
        cin>>t;
        cout<<(++t)<<" ";
        cout<<(--t)<<" ";
        cout<<t<<" ";
        cout<<t--<<" ";
        cout<<t++<<" ";
        cout<<t<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值