您的位置:首页 > 其它

SDUST 第四次作业

2017-03-26 17:29 197 查看
/*这周的作业难度整体不大,主要是新学了一种封装类的方法,对于有联系的数字进行封装,
如时间这种东西,同时还有的便是,对一些非法数据的处理,采用这种方法很方便,
再有新学的便是,运算符重载,
运算符重载主要分为: 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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sdust