読者です 読者をやめる 読者になる 読者になる

個人的自由ノート

ふと自分が気になった事を書いてます

C++で問題を解く

今回は問題の形式上、全部写経しました。後2問で全部終わりです。

#ifndef __Class_Time
#define __Class_Time

#include <string>
#include <iostream>

class Time {
  int h;
  int m;
  int s;
public:
  Time();
  Time(const int& hh, const int& mm = 0, const int& ss = 0);
  const int hour() const { return h; }
  const int minute() const { return m; }
  const int second() const { return s; }
  Time one_sec_ago() const;
  std::string to_string() const;

  Time& operator++();
  Time operator++(int);
  Time& operator--();
  Time operator--(int);
  Time& operator+=(int ds);
  Time& operator-=(int ds);
  Time operator+(int ds) const;
  friend Time operator+(int ds, const Time& t);
  Time operator-(int ds) const;
  bool operator==(const Time& t) const;
  bool operator!=(const Time& t) const;
  bool operator< (const Time& t) const;
  bool operator> (const Time& t) const;
  bool operator<=(const Time& t) const;
  bool operator>=(const Time& t) const;
};

std::ostream& operator<<(std::ostream& s, const Time& x);
std::istream& operator>>(std::istream& s, Time& x);
#endif
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
#include "q12_5.h"

using namespace std;

Time::Time(){
  h = 12;
  m = s = 0;
}

Time::Time(const int& hh, const int& mm, const int& ss){
  h = hh % 24;
  m = mm % 60;
  s = ss % 60;
}

Time Time::one_sec_ago() const {
  Time temp = *this;
  if(temp.s > 0)
    temp.s--;
  else {
    temp.s = 59;
    if(temp.m > 0)
      temp.m--;
    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();
}

Time& Time::operator++() {
  return *this += 1;
}

Time Time::operator++(int) {
  Time temp = *this;
  ++(*this);
  return temp;
}

Time& Time::operator--() {
  return *this -= 1;
}

Time Time::operator--(int) {
  Time temp = *this;
  --(*this);
  return temp;
}

Time& Time::operator+=(int ds) {
  if(ds < 0)
    return *this -= -ds;
  if(ds) {
    s += ds % 60; ds /= 60; if(s >= 60) { ds++; s -= 60; }
    if(ds) {
      m += ds % 60; ds /= 60; if(m >= 60) { ds++; m -= 60; }
      if(ds) {
        h += ds % 24; if(h >= 24) { h -= 24; }
      }
    }
  }
  return *this;
}

Time& Time::operator-=(int ds) {
  if(ds < 0)
    return *this += -ds;
    if(ds) {
      s -= ds % 60; ds /= 60; if(s < 0) { ds++; s += 60; }
      if(ds){
        m -= ds % 60; ds /= 60; if(m < 0) { ds++; m += 60; }
      if(ds) {
        h -= ds % 60; if(h < 0) { h += 24; }
      }
    }
  }
  return *this;
}

Time Time::operator+(int ds) const {
  Time temp = *this;
  return temp += ds;
}

Time operator+(int ds, const Time& t) {
  return t + ds;
}

Time Time::operator-(int ds) const {
  Time temp = *this;
  return temp -= ds;
}

bool Time::operator==(const Time& t) const {
  return h == t.h && m == t.m && s == t.s;
}

bool Time::operator!=(const Time& t) const {
  return !(*this == t);
}

bool Time::operator<(const Time& t) const {
  if(h < t.h) return true;
  if(h > t.h) return false;

  if(m < t.m) return true;
  if(m > t.m) return false;

  return s < t.s;
}

bool Time::operator>(const Time& t) const {
  if(h > t.h) return true;
  if(h < t.h) return false;

  if(m > t.m) return true;
  if(m < t.m) return false;

  return s > t.s;
}

bool Time::operator<=(const Time& t) const {
  return !(*this > t);
}

bool Time::operator>=(const Time& t) const {
  return !(*this < t);
}

ostream& operator<<(ostream& s, const Time& x) {
  return s << x.to_string();
}

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;
}

int main(void){
  int ds;
  Time t1, t2;

  cout << "時刻t1 : "; cin >> t1;
  cout << "時刻t2 : "; cin >> t2;
  cout << "整数ds : "; cin >> ds;

  cout << "t1++  " << t1++ << endl;
  cout << "++t1  " << ++t1 << endl;

  cout << "t2--  " << t2-- << endl;
  cout << "--t2  " << --t2 << endl;

  cout << "t1 += " << ds << "  " << (t1 += ds) << endl;
  cout << "t2 -= " << ds << "  " << (t2 -= ds) << endl;

  cout << "t1 + " << ds << "  " << t1 + ds << endl;
  cout << ds << " + t1 " << ds + t1 << endl;
  cout << "t2 - " << ds << "  " << t2 - ds << endl;

  cout << boolalpha;

  cout << "t1 == t2  " << (t1 == t2) << endl;
  cout << "t1 != t2  " << (t1 != t2) << endl;
  cout << "t1 >  t2  " << (t1 >  t2) << endl;
  cout << "t1 <  t2  " << (t1 <  t2) << endl;
  cout << "t1 >= t2  " << (t1 >= t2) << endl;
  cout << "t1 <= t2  " << (t1 <= t2) << endl;

  return 0;
}