鱼C论坛

 找回密码
 立即注册
查看: 865|回复: 2

[已解决]写了一个时钟程序,如何在输出的时候在不足两位的数上补0?

[复制链接]
发表于 2020-4-4 12:08:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问题:写了一个时钟程序,如何在输出的时候在不足两位的数上补0?

  1. #include<iostream>
  2. using namespace std;
  3. class Time{
  4. private:
  5.         int hour;           
  6.         int min;           
  7.         int sec;
  8. public:
  9.         friend ostream & operator<<(ostream &output,Time&);
  10.         friend istream & operator>>(istream &input,Time&);
  11.         Time operator+(Time &c);
  12.         Time operator-(Time &c);
  13.         friend Time operator++(Time &);
  14.         friend Time operator--(Time &,int);
  15.         bool operator> (Time &c);
  16.         bool operator< (Time &c);
  17.         bool operator== (Time &c);
  18. };

  19. istream & operator>>(istream &input,Time &c){ //输入
  20.         cout<<"请输入时分秒:"<<endl;
  21.         input>>c.hour>>c.min>>c.sec;
  22.         if((c.hour<0||c.hour>=60)||(c.min<0||c.min>=60)||(c.sec<0||c.sec>=60)){
  23.                 c.hour=0;
  24.                 c.min=0;
  25.                 c.sec=0;
  26.                 cout<<"输入错误"<<endl;
  27.                 exit(0);
  28.         }
  29.         return input;
  30. }

  31. ostream& operator<<(ostream &output,Time &c){ //输出
  32.         output<<c.hour<<':'<<c.min<<':'<<c.sec;
  33.         return output;
  34. }

  35. Time Time::operator+(Time &c){  //加
  36.         Time t(*this);
  37.         if(t.sec+c.sec>=60){
  38.                 t.sec=t.sec+c.sec-60;
  39.                 ++t.min;
  40.         }else{
  41.                 t.sec=t.sec+c.sec;
  42.         }
  43.         if(t.min+c.min>=60){
  44.                 t.min=t.min+c.min-60;
  45.                 ++t.hour;
  46.         }else{
  47.                 t.min=t.min+c.min;
  48.         }
  49.         if(t.hour+c.hour>=24){
  50.                 t.hour=t.hour+c.hour-24;
  51.         }else{
  52.                 t.hour=t.hour+c.hour;
  53.         }
  54.         return t;
  55. }

  56. Time Time::operator-(Time &c){  //减
  57.         Time t(*this);
  58.         if(t.sec<c.sec){
  59.                 t.sec=t.sec+60-c.sec;
  60.                 --t.min;
  61.         }else{
  62.                 t.sec=t.sec-c.sec;
  63.         }
  64.         if(t.min<c.min){
  65.                 t.min=t.min+60-c.min;
  66.                 --t.hour;
  67.         }else{
  68.                 t.min=t.min-c.min;
  69.         }
  70.         if(t.hour<c.hour){
  71.                 t.hour=t.hour+24-c.hour;
  72.         }else{
  73.                 t.hour=t.hour+-c.hour;
  74.         }
  75.         return t;
  76. }

  77. Time operator++(Time &c){
  78.         c.hour++;
  79.         return c;
  80. }

  81. Time operator--(Time &c,int){
  82.         Time temp(c);
  83.         c.hour--;
  84.         return c;
  85. }

  86. bool Time::operator> (Time &c){
  87.         if(hour>c.hour) return true;
  88.         else if((hour==c.hour) && (min>c.min)) return true;
  89.         else if((hour==c.hour) && (min==c.min) && (sec > c.sec)) return true;
  90.         return false;
  91. }

  92. bool Time::operator< (Time &c){
  93.         if(hour<c.hour) return true;
  94.         else if((hour==c.hour) && (min<c.min)) return true;
  95.         else if((hour==c.hour) && (min==c.min) && (sec < c.sec)) return true;
  96.         return false;
  97. }

  98. bool Time::operator== (Time &c){
  99.         if((hour==c.hour) && (min==c.min) && (sec == c.sec)) return true;
  100.         return false;
  101. }

  102. int main(){
  103.         Time t1, t2, t;
  104.         cin >> t1 >> t2;
  105.         cout << "t1为:" << t1 << endl;
  106.         cout << "t2为:" << t2 << endl;
  107.         t = t1 + t2;
  108.         cout << "t1+t2=" << t << endl;
  109.         t = t1 - t2;
  110.         cout << "t1-t2=" << t << endl;
  111.         if (t1 > t2) cout << "t1>t2" << endl;
  112.         if (t1 < t2) cout << "t1<t2" << endl;
  113.         if (t1 == t2) cout << "t1==t2" << endl;
  114.         ++t1;
  115.         cout<<"++t1= ";
  116.         cout<<t1<<endl;
  117.         cout<<"t1--= ";
  118.         t1--;
  119.         cout<<t1<<endl;
  120. }
复制代码

输出如图所示

输出如图所示

最佳答案
2020-4-4 12:16:48
栗粒粒 发表于 2020-4-4 12:15
试试用setfill()函数

先用setw(2)保证两位的输出,再用setfill(0)补0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-4 12:15:17 | 显示全部楼层
试试用setfill()函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 12:16:48 | 显示全部楼层    本楼为最佳答案   
栗粒粒 发表于 2020-4-4 12:15
试试用setfill()函数

先用setw(2)保证两位的输出,再用setfill(0)补0
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 17:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表