SDUT-2711-->电子时钟中的运算符重载
来源:互联网 发布:淘宝一休老银匠怎么样 编辑:程序博客网 时间:2024/06/02 02:23
4-2 电子时钟中的运算符重载
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
通过本题目的练习可以运算符重载的方法;
设计一个时间类Time,私有数据成员有hour(时)、minute(分)、second(秒);
公有成员函数有:setHour(int)设置数据成员hour的值,非法的输入默认为12;setMinue(int)设置数据成员minute的值,非法输入默认为0;setSecond(int)设置数据成员second的值,非法输入默认为0;setTime(int,int,int)设置时、分、秒三个数据成员的值;三个成员函数int getHour(); int getMinute(); int getSecond();分别用于获取时间对象的属性值。
定义两个构造函数Time(); 和 Time(int,int,int);
定义一个成员函数void displayTime(); 用于显示时间,注意格式为 hh:mm:ss,位数不够用0填充;
定义一个时钟增加1的成员函数 void tick(); 把second的值加1,并注意是否到60;
定义一个成员或友元函数 bool operator= =(….); 判断两个时间对象的值是否相等;
定义一个成员或友元函数bool operator>(…..); 判断第一个时间对象的值是否大于第二个时间对象的值。
在主函数main()中指定开始时间和结束时间,并调用相应成员函数,显示从开始时间到结束时间之间所有时间对象的值,其格式见示例输出。
Input
输入6个整数,之间用一个空格间隔;分别表示开始时间的时、分、秒和结束时间的时、分、秒的值
Output
从开始时间到结束时间之间所有时间对象的值;每个值占一行,格式为hh:mm:ss
Example Input
01 01 01 01 01 10
Example Output
01:01:0101:01:0201:01:0301:01:0401:01:0501:01:0601:01:0701:01:0801:01:0901:01:10
Hint
输入
11 10 12 10 12 56
输出
The begin time is not earlier than the end time!
Author
黄晶晶
正确代码:转载出处:http://blog.csdn.net/u013486414/article/details/39956903
#include <iostream>using namespace std;class Time{private: int hour; int minute; int second;public: Time(int h,int m,int s) { hour=h; minute=m; second=s; } Time operator ++ (); void display(); int cmp(Time &t);};Time Time::operator ++ ( ){ if(++second>= 60) { second-= 60; if(++minute>= 60) { minute-=60; ++hour; } }}void Time::display(){ if(hour<10) cout<<"0"<<hour<<":"; else cout<<hour<<":"; if(minute<10) cout<<"0"<<minute<<":"; else cout<<minute<<":"; if(second<10) cout<<"0"<<second; else cout<<second; cout<<endl;}int Time::cmp(Time & t){ if(hour>t.hour) return 0; else if(hour== t.hour&& minute > t.minute) return 0; else if(hour == t.hour && minute == t.minute && second > t.second) return 0; else return 1;}int main(){ int h1,h2,m1,m2,s1,s2; cin>>h1>>m1>>s1; cin>>h2>>m2>>s2; Time t1(h1,m1,s1); Time t2(h2,m2,s2); if(!t1.cmp(t2) ) { cout<<"The begin time is not earlier than the end time!"<<endl; } else { while(t1.cmp(t2)) { t1.display(); ++t1; } } return 0;}
菜鸡本人的错误代码 ,一直找不出错误在哪 希望大佬帮忙找一下。。
#include <iostream>#include <stdio.h>using namespace std;class Time{public: Time();//定义无参构造函数 Time(int h=12, int m=0, int s=0);//定义有参构造函数 void setTime(int h=12,int m=0, int s=0); void displayTime();// void tick();// bool operator==(Time &n); bool operator>(Time &n);private: int hour,minute,second;};Time::Time(){ hour = 12; minute = 0; second = 0;}Time::Time(int h,int m, int s){ hour = h; minute = m; second = s;}void Time::setTime(int h,int m,int s){ hour = h; minute = m; second = s;}void Time::displayTime(){ printf("%02d:%02d:%02d\n",hour,minute,second);}void Time::tick(){ if(second+1 < 60) { second++; return ; } else if(second+1 == 60) { second = 0; if(minute+1<60) { minute++; return ; } else if(minute+1 == 60) { minute = 0; hour++; return ; } }}bool Time::operator==(Time &n){ if(hour == n.hour && minute == n.minute && second == n.second) return 1; else return 0;}bool Time::operator>(Time &n){ if(hour>n.hour) return 1; else if(hour == n.hour && minute>n.minute) return 1; else if(hour == n.hour && minute==n.minute && second>n.second) return 1; return 0;}int main(){ int h1,m1,s1,h2,m2,s2; cin>>h1>>m1>>s1>>h2>>m2>>s2; Time t1(h1,m1,s1),t2(h2,m2,s2); if(t1>t2) cout<<"The begin time is not earlier than the end time!"<<endl; else if(t1 == t2) cout<<"The begin time is not earlier than the end time!"<<endl; else { while(t2>t1) { t1.displayTime(); t1.tick(); } t2.displayTime(); } return 0;}
阅读全文
0 0
- SDUT-2711 4-2 电子时钟中的运算符重载
- SDUT 2711 4-2 电子时钟中的运算符重载
- SDUT-2711-->电子时钟中的运算符重载
- SDUT OJ4-2 电子时钟中的运算符重载
- sdut oj4-2 电子时钟中的运算符重载
- 电子时钟中的运算符重载
- [2711]4-2 电子时钟中的运算符重载
- SDUTOJ 2711 4-2 电子时钟中的运算符重载
- 2711 4-2 电子时钟中的运算符重载
- 4-2电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 4-2 电子时钟中的运算符重载
- 关于彩票
- Java NIO1:浅谈I/O模型
- 1
- 大型网站技术架构演化之路
- 1
- SDUT-2711-->电子时钟中的运算符重载
- 222
- pageoffice在网站上部署
- 使用ngrok工具映射到外网可访问
- 裁剪
- HTML/HTML5中的download属性
- 用物联网技术将城市带入智慧时代
- 基于Maven的SSM框架容易出现问题
- [Steam]怎么喜加1获得免费正版游戏