Skip to content

Instantly share code, notes, and snippets.

@chomado
Last active August 29, 2015 14:04
Show Gist options
  • Save chomado/2fd7ad0767846b679889 to your computer and use it in GitHub Desktop.
Save chomado/2fd7ad0767846b679889 to your computer and use it in GitHub Desktop.
#include "Time.h"
// クラスTimeのデフォルトコンストラクタ
Time::Time()
{
h = 12;
m = s = 0;
}
// クラスTimeのコンストラクタ
Time::Time(int hh, int mm, int ss)
{
h = hh;
m = mm;
s = ss;
}
// 1秒前の時刻を返却
Time Time::one_sec_ago() const
{
Time temp = *this;
if (temp.s > 0)
temp.s--;
else {
temp.s = 59;
if (temp.h > 0)
temp.h--;
else {
temp.m = 59;
if (temp.h > 0)
temp.h--;
else
temp.h = 23;
}
}
return temp;
}
// 文字列表現を返却
string Time::to_string() const
{
ostringstream ss;
ss << setfill('0')
<< setw(2) << h << ":" << setw(2) << m << ":" << setw(2) << s;
return ss.str();
}
// 出力ストリームsにxを挿入
ostream& operator<<(ostream& s, const Time& x)
{
return s << x.to_string();
}
//入力ストリームsから時刻を抽出してxに格納
istream& operator>>(istream& s, Time& x)
{
int hh, mm, ss;
char ch;
s >> hh >> ch >> mm >> ch >> ss;
x = Time(hh, mm, ss);
return s;
}
#include <string>
class Time {
int h;
int m;
int s;
public:
Time(); // デフォルトコンストラクタ
Time(int hh, int mm = 0, int ss = 0); // コンストラクタ
int hour() const { return h; }
int minute() const { return m; }
int second() const { return s; }
Time one_sec_ago() const; // 一秒前の時刻を返却
std::string to_string() const; // 文字列表現を返却
};
std::ostream& operator<<(std::ostream& s, const Time& x); // 挿入子
std::istream& operator>>(std::istream& s, Time& x); // 抽出子
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment