第六周(项目四)——静态成员应用。

/*
02.*烟台大学计算机学院学生
03.*All right reserved.
04.*文件名称*烟台大学计算机学院学生
05.*All right reserved.
06.*文件名称:静态成员应用
07.*作者:王洪海
08.*完成日期:2013年4月3日
09.*版本号:v1.0
10.*对任务及求解方法的描述部分:静态成员应用
11.*我的程序:
12.*/
#include <iostream>

#include <cstdlib>

using namespace std;

class Time
{
public:
    Time(int=0,int=0,int=0);
    void show_time( ); //根据is_24和from0,输出适合形式-20:23:5/8:23:5 pm/08:23:05 pm
    void add_seconds(int); //增加n秒钟
    void add_minutes(int); //增加n分钟
    void add_hours(int); //增加n小时
    static void change24();  //改变静态成员is_24,在12和24时制之间转换
    static void changefrom0();   //改变静态成员from0,切换是否前导0
private:
    static bool is_24; //为true时,24小时制,如20:23:5;为flase,12小时制,显示为8:23:5 pm
    static bool from0; //为true时,前导0,8:23:5显示为08:23:05
    int hour;
    int minute;
    int sec;
};
//下面写出静态成员的初始化及各成员函数的定义……
bool Time::is_24=true;
bool Time::from0=true;
Time::Time(int a,int b,int c)
{
    hour=a;
    minute=b;
    sec=c;
}
void Time::show_time( )
{
    if(is_24==true&&from0==true)
    {
        if(hour<10)
            cout<<"0"<<hour<<":";
        else
            cout<<hour<<":";
        if(minute<10)
            cout<<"0"<<minute<<":";
        else
            cout<<minute<<":";
        if(sec<10)
            cout<<"0"<<sec<<":";
        else
            cout<<sec<<endl;
    }
    if(is_24==false&&from0==true)
    {
        if(hour>12)
        {
            if(hour<22)
                cout<<"0"<<hour-12<<":";
            else
                cout<<hour-12<<":";
        }
        else
        {
            cout<<"0"<<hour<<":";
        }
        if(minute<10)
            cout<<"0"<<minute<<":";
        else
            cout<<minute<<":";
        if(sec<10)
            cout<<"0"<<sec<<"  pm";
        else
            cout<<sec<<"  pm";
    }
    if(is_24==true&&from0==false)
    {
        cout<<hour<<":"<<minute<<":"<<sec<<endl;
    }
    if(is_24==false&&from0==false)
    {
        if(hour>12)
        {
            cout<<hour-12<<":";
        }
        else
        {
            cout<<hour<<":";
        }
        cout<<minute<<":"<<sec<<"  pm"<<endl;
    }
}
void Time::change24()
{
    int a;
    cout<<"1.选择24时间值"<<"\t"<<"2.选择12时间值"<<endl;
    cout<<"请输入你想选择的时间制: ";
    cin>>a;
    cout<<endl;
    switch(a)
    {
    case 1:
        is_24=true;
        break;
    case 2:
        is_24=false;
        break;
    }
    if(a!=1&&a!=2)
      {
          cout<<"你的输入错误!!"<<endl;
          exit(0);
      }
}
void Time::changefrom0()
{
    int a;
    cout<<"1.前导有零。"<<"\t"<<"2.前导无零。"<<endl;
    cout<<"请输入你选择的代码: ";
    cin>>a;
    cout<<endl;
    switch(a)
    {
      case 1:
        from0=true;
        break;
    case 2:
        from0=false;
        break;
    }
}
void Time::add_seconds(int x)
{
    int a;
    sec+=x;
    a=sec;
    sec=sec%60;
    minute+=a/60;
    minute=minute%60;
    hour+=minute/60;
    hour=hour%24;
    show_time();
}
void Time::add_minutes(int y)
{
    int b;
    minute+=y;
    b=minute;
    minute=minute%60;
    hour+=b/60;
    hour=hour%24;
    show_time();
}
void Time::add_hours(int z)
{
    hour+=z;
    hour=hour%24;
    show_time();
}
int main( )
{
    int s1,s;
    Time t(8,25,30);
    cout<<"初始值为:";
    t.show_time();
    cout<<endl;
    t.change24();
    t.changefrom0();
    cout<<"1.增加秒数"<<"\t"<<"2.增加分钟数。"<<"\t"<<"3.增加小时数。"<<endl;
    cout<<"请选择相应代码: ";
    cin>>s;
    cout<<endl;
    switch(s)
    {
        case 1:
        {
            cout<<"请输入增加的秒数: ";
            cin>>s1;
            t.add_seconds(s1);
            break;
        }
        case 2:
        {
            cout<<"请输入增加的分钟数: ";
            cin>>s1;
            t.add_minutes(s1);
            break;
        }
        case 3:
        {
            cout<<"请输入增加的小时数: ";
            cin>>s1;
            t.add_hours(s1);
            break;
        }
    }
    if(s!=1&&s!=2&&s!=3)

   {
       cout<<"你的输入错误!!"<<endl;
       exit(0);
   }
   return 0;
}

运行结果,如下图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值