/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:刘晓晓
*完成日期:2014年 04月4号
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述: 无
*问题描述:静态时间类
*程序输出:无
*问题分析:
*算法设计:
*/
#include <iostream>
using namespace std;
class Time
{
public:
Time(int a=0,int b =0,int c=0):hour(a),minute(b),sec(c) {}
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;
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<<":";
}
else if (is_24==false &&from0==true)
{
if (hour>12)
cout<<24-hour<<":";
else 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<<":";
}
else if (is_24==true&&from0==false )
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
else
{
if (hour>12)
cout<<24-hour<<":"<<minute<<sec;
else if (hour<10)
cout<<hour<<minute<<sec;
}
}
void Time ::add_hours(int n)
{
if (hour+n<=24)
{
hour=hour+n;
}
else
{
cout<<"格式不对!"<<endl;
}
show_time();
}
void Time ::add_minutes(int n)
{
if (minute+n<60)
{
minute=minute+n;
if (minute>60)
{
hour++;
minute =minute-60;
}
}
else
{
cout<<" 输入错误。";
}
show_time();
}
void Time ::add_seconds(int n )
{
if(sec+n<60)
{
sec=sec+n;
if (sec>60)
{
minute ++;
if (minute>60)
{
hour++;
minute=minute-60;
sec=sec-60;
}
}
}
else
{
cout<<"输入错误";
}
show_time();
}
void Time:: changefrom0()
{
int a;
cout<<"1 需要前导0 如08:23:06 ;2 不需要前导0 如8:23:6"<<endl;
cin>>a;
if (a==1)
{
is_24=true;
void show_time();
}
else if (a==2)
{
is_24=false;
void show_time();
}
else
cout<<"输入错误!"<<endl;
}
void Time :: change24()
{
int a;
cout<<"1. 24小时制 13:00:12;2. 12小时制 1:00:12 pm"<<endl;
cin>>a;
if(a==1)
is_24=true;
else if(a==2)
is_24=false;
else
cout<<"输入错误!"<<endl;
}
int main()
{
Time t1(14,15,16);
int a1,a2,a3;
t1.change24();
t1.changefrom0();
cout<<"输入要增加的秒数:"<<endl;
cin>>a1;
t1.add_seconds(a1);
cout<<"输入要增加的分数"<<endl;
cin>>a2;
t1.add_minutes(a2);
cout<<"输入要增加的小时:"<<endl;
cin>>a3;
t1.add_hours(a3);
return 0;
}
还是没有达到想要的结果。。。