落書きノート

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

C++で問題を解く

Bash on Windowsでやってみたのですが、少々バグがあるものの現時点でも結構使えます。AtomエディタでコーディングしてBash on Windowsのg++コンパイラでやってみました。/mntにCドライブがマウントされていたので、これを利用しました。ホームディレクトリにシンボリックリンクを作成しましたが、なぜか動作が安定しませんね・・・。これは時間が経てば直るのかな。

#include <iostream>

class Time{
  int hour;
  int minute;
  int second;
public:
  Time(const int& h, const int& m, const int& s);

  int getHour() { return this->hour; }
  int getMinute() { return this->minute; }
  int getSecond() { return this->second; }
};

Time::Time(const int& h, const int& m, const int& s){
  this->hour = h;
  this->minute = m;
  this->second = s;
}

using namespace std;

int main(void){
  Time a(12, 35, 43);
  Time b = a;
  Time c = Time(17, 28, 39);

  cout << a.getHour() << "時" << a.getMinute() << "分" << a.getSecond() << "秒" << endl;
  cout << b.getHour() << "時" << b.getMinute() << "分" << b.getSecond() << "秒" << endl;
  cout << c.getHour() << "時" << c.getMinute() << "分" << c.getSecond() << "秒" << endl;

  return 0;
}



#include <iostream>

class Time {
  int h;
  int m;
  int s;
public:
  Time();
  Time(const int& h, const int& m = 0, const int& s = 0);

  int getHour() const { return this->h; }
  int getMinute() const { return this->m; }
  int getSecond() const { return this->s; }

  Time one_sec_ago() const;
};

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

Time::Time(const int& h, const int& m, const int& s){
  this->h = h;
  this->m = m;
  this->s = s;
}

Time Time::one_sec_ago() const {
  Time temp = *this;

  if(temp.s > 0){
    temp.s -= 1;
  }else{
    if(temp.m > 0){
      temp.m -= 1;
      temp.s = 59;
    }else{
      if(temp.h > 0){
        temp.h -= 1;
        temp.m = 59;
        temp.s = 59;
      }else{
        temp.h = 23;
        temp.m = 59;
        temp.s = 59;
      }
    }
  }

  return temp;
}

using namespace std;

int main(void){
  const Time a;
  const Time b(12, 35, 43);
  const Time c = a.one_sec_ago();
  const Time d = b.one_sec_ago();

  cout << a.getHour() << "時" << a.getMinute() << "分" << a.getSecond() << "秒" << endl;
  cout << b.getHour() << "時" << b.getMinute() << "分" << b.getSecond() << "秒" << endl;
  cout << c.getHour() << "時" << c.getMinute() << "分" << c.getSecond() << "秒" << endl;
  cout << d.getHour() << "時" << d.getMinute() << "分" << d.getSecond() << "秒" << endl;

  return 0;
}



#include <iostream>

class Time {
  int h;
  int m;
  int s;
  mutable int counter;
public:
  Time();
  Time(const int& h, const int& m = 0, const int& s = 0);

  int getHour() const { this->counter++; return this->h; }
  int getMinute() const { this->counter++; return this->m; }
  int getSecond() const { this->counter++; return this->s; }
  int getCounter() const { return ++this->counter; }

  Time one_sec_ago() const;
};

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

Time::Time(const int& h, const int& m, const int& s){
  this->h = h;
  this->m = m;
  this->s = s;
}

Time Time::one_sec_ago() const {
  Time temp = *this;

  if(temp.s > 0){
    temp.s -= 1;
  }else{
    if(temp.m > 0){
      temp.m -= 1;
      temp.s = 59;
    }else{
      if(temp.h > 0){
        temp.h -= 1;
        temp.m = 59;
        temp.s = 59;
      }else{
        temp.h = 23;
        temp.m = 59;
        temp.s = 59;
      }
    }
  }

  return temp;
}

using namespace std;

int main(void){
  const Time a;
  const Time b(12, 35, 43);
  const Time c = a.one_sec_ago();
  const Time d = b.one_sec_ago();

  cout << a.getHour() << "時" << a.getMinute() << "分" << a.getSecond() << "秒" << endl;
  cout << b.getHour() << "時" << b.getMinute() << "分" << b.getSecond() << "秒" << endl;
  cout << c.getHour() << "時" << c.getMinute() << "分" << c.getSecond() << "秒" << endl;
  cout << d.getHour() << "時" << d.getMinute() << "分" << d.getSecond() << "秒" << endl;
  cout << "Counter:" << a.getCounter() << endl;

  return 0;
}