2014年4月15日火曜日

開発環境

C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅣ部(高度なプログラミング概念)の18章(演算子のオーバーロード)、18.6(プログラミング実習)、実習 18-3.を解いてみる。

その他参考書籍

実習 18-3.

コード(BBEdit, Emacs)

my_time.h

#ifndef __my_time_h__
#define __my_time_h__
#include <iostream>
#include <iomanip>

namespace my_time {
  const int SEC_PER_DAY = 24 * 60 * 60;
  class time {
  private:
    int hour;
    int min;
    int sec;
    int time_to_sec;
    static time sec_to_time(int sec);
  public:
    time () {
      hour = 0;
      min = 0;
      sec = 0;
      time_to_sec = 60 * 60 * hour + 60 * min + sec;
    }
    time (int h, int m, int s) {
      hour = h;
      min = m;
      sec = s;
      time_to_sec = 60 * 60 * hour + 60 * min + sec;
    }
    // time (cons time& old_time);
    // ~time () {}
    // time operator = (const time& old_time);

    time operator += (const time& op2) {
      (*this) = (*this) + op2;
      return (*this);
    }
    time operator -= (const time& op2) {
      (*this) = (*this) - op2;
      return (*this);
    }
    friend time operator + (const time& op1, const time& op2);
    friend time operator - (const time& op1, const time& op2);
    friend std::ostream& operator << (std::ostream& out_file, const time& t);
    friend std::istream& operator >> (std::istream& in_file, time& t);
  };

  inline time time::sec_to_time(int s)
  {
    int hour;
    int min;
    s = s < 0 ? SEC_PER_DAY + s :
      s >= SEC_PER_DAY ? s - SEC_PER_DAY : s;
    hour = s / (60 * 60);
    s -= (60 * 60 * hour);
    min = s / 60;
    s -= (60 * min);
    return time(hour, min, s);
  }

  inline time operator + (const time& op1, const time& op2)
  {
    return time::sec_to_time(op1.time_to_sec + op2.time_to_sec);
  }

  inline time operator - (const time& op1, const time& op2)
  {
    return time::sec_to_time(op1.time_to_sec - op2.time_to_sec);
  }

  inline std::ostream& operator << (std::ostream& out_file, const time& t)
  {
    out_file << std::setw(2) << std::setfill('0') << t.hour << ':' <<
      std::setw(2) << std::setfill('0') << t.min << ':' <<
      std::setw(2) << std::setfill('0') << t.sec;
    return (out_file);
  }

  extern std::istream& operator >> (std::istream& in_file, time& t);
}
#endif /*__my_time_h__*/

my_time.cpp

#include <iostream>

#include "my_time.h"

namespace my_time {
  std::istream& operator >> (std::istream& in_file, time& t)
  {
    int h;
    int m;
    int s;
    char ch;

    std::istream::sentry the_sentry(in_file, true);
    if (the_sentry) {
      if (in_file.fail()) return (in_file);

      in_file >> h;

      if (in_file.fail()) return (in_file);

      in_file >> ch;

      if (in_file.fail()) return (in_file);

      if (ch != ':') {
        in_file.setstate(std::ios::failbit);
        return (in_file);
      }

      in_file >> m;

      if (in_file.fail()) return (in_file);

      in_file >> ch;

      if (ch != ':') {
        in_file.setstate(std::ios::failbit);
        return (in_file);
      }

      in_file >> s;

      if (in_file.fail()) return (in_file);

      t.hour = h;
      t.min = m;
      t.sec = s;
    }
    else
      in_file.setstate(std::ios::failbit);

    return (in_file);
  }
}

test_my_time.cpp

#include <iostream>
#include "my_time.h"

int main(int argc, char *argv[])
{
  my_time::time t0;
  my_time::time t1(1, 2, 3);
  my_time::time t2(4, 5, 6);
  my_time::time t3(21, 5, 10);

  std::cout << t0 << "(00:00:00)\n";
  std::cout << t1 << "(01:02:03)\n";
  std::cout << t2 << "(04:05:06)\n";

  std::cout << "時刻を入力(時:分:秒)>> ";
  std::cin >> t3;
  std::cout << t3 << '\n';

  std::cout << t0 << " + " << t1 << " = " << t0 + t1 << std::endl;
  std::cout << t1 << " + " << t0 << " = " << t1 + t0 << std::endl;
  std::cout << t1 << " + " << t2 << " = " << t1 + t2 << std::endl;
  std::cout << t2 << " + " << t1 << " = " << t2 + t1 << std::endl;
  std::cout << t2 << " + " << t3 << " = " << t2 + t3 << std::endl;
  std::cout << t3 << " + " << t2 << " = " << t3 + t2 << std::endl;

  std::cout << t0 << " - " << t1 << " = " << t0 - t1 << std::endl;
  std::cout << t1 << " - " << t0 << " = " << t1 - t0 << std::endl;
  std::cout << t1 << " - " << t2 << " = " << t1 - t2 << std::endl;
  std::cout << t2 << " - " << t1 << " = " << t2 - t1 << std::endl;
  std::cout << t2 << " - " << t3 << " = " << t2 - t3 << std::endl;
  std::cout << t3 << " - " << t2 << " = " << t3 - t2 << std::endl;

  return (0);
}

Makefile

#
# FSFのg++コンパイラ用のMakefile
#
CC=g++
CFLAGS=-g -Wall
all: test_my_time

test_my_time: test_my_time.o my_time.o
 ${CC} ${CFLAGS} -o test_my_time test_my_time.o my_time.o

test_my_time.o: test_my_time.cpp my_time.h
 ${CC} -c -o test_my_time.o test_my_time.cpp

my_time.o: my_time.cpp my_time.h
 ${CC} -c -o my_time.o my_time.cpp

clean:
 rm test_my_time

入出力結果(Terminal)

$ make && ./test_my_time
g++ -c -o test_my_time.o test_my_time.cpp
g++ -c -o my_time.o my_time.cpp
g++ -g -Wall -o test_my_time test_my_time.o my_time.o
00:00:00(00:00:00)
01:02:03(01:02:03)
04:05:06(04:05:06)
時刻を入力(時:分:秒)>> 21:5:10
21:05:10
00:00:00 + 01:02:03 = 01:02:03
01:02:03 + 00:00:00 = 01:02:03
01:02:03 + 04:05:06 = 05:07:09
04:05:06 + 01:02:03 = 05:07:09
04:05:06 + 21:05:10 = 01:10:16
21:05:10 + 04:05:06 = 01:10:16
00:00:00 - 01:02:03 = 22:57:57
01:02:03 - 00:00:00 = 01:02:03
01:02:03 - 04:05:06 = 20:56:57
04:05:06 - 01:02:03 = 03:03:03
04:05:06 - 21:05:10 = 06:59:56
21:05:10 - 04:05:06 = 17:00:04
$

0 コメント:

コメントを投稿