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; } }
相关文章推荐
- 第四次作业--项目选题报告(团队)
- 第四次团队作业:社团申请App
- RFID第四次作业8-5
- SDN第四次上机作业
- SDN第四次上机作业
- C++第四次上机作业
- c++第四次实验(作业)
- 【c#第四次作业】c#操作MySQL数据库
- 第四次C#作业-MySQL数据库及C#操作MySQL数据库
- C++第四次作业-分数的累加
- C#第四次作业:MySQL数据库及C#操作MySQL数据库
- 第四次作业:《财务决策与分析》——财务报表的逻辑架构(慕课组)
- 2017年软件工程第四次作业-2单元测试
- 软件工程——第四次作业(2)
- 第四次作业(1和2)
- 软件工程-东北师大战-第四次作业(3)
- SDUST第十一次oj作业液晶显示问题
- c#第四次作业:建立数据库和表,并用cmd显示查询结果
- 第四次软工作业-代码规范
- c++第四次作业