2012年11月9日金曜日

開発環境

『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第16章(デリゲートとイベント)17.6(練習問題)問題17-1を解いてみる。

その他参考書籍

問題17-1.

コード

using System;
using System.Threading;

class TimeInfoEventArgs : EventArgs
{
    public TimeInfoEventArgs(string msg)
    {
        this.msg = msg;
    }
    public readonly string msg;
}

class Clock
{
    private string msg;
    private DateTime start;
    private DateTime stop;
    public Clock(string msg, int hours, int minutes, int seconds)
    {
        this.msg = msg;
        start = DateTime.Now;
        TimeSpan duration = new TimeSpan(hours, minutes, seconds);
        stop = start + duration;
    }

    public delegate void SecondChangedHandler(object clock, TimeInfoEventArgs ti);
    public SecondChangedHandler SecondChanged;
    public void Run()
    {
        for (int i = 0; ; i++)
        {
            Console.WriteLine("{0}秒経過", i);
            DateTime now = DateTime.Now;
            if (now >= this.stop)
            {
                TimeInfoEventArgs ti =
                    new TimeInfoEventArgs(msg);
                if (SecondChanged != null)
                {
                    SecondChanged(this, ti);
                    break;
                }
            }
            Thread.Sleep(1000);
        }
    }
}

class DisplayClock
{
    public void Subscribe(Clock clock)
    {
        clock.SecondChanged +=
            new Clock.SecondChangedHandler(TimeHasChanged);
    }
    public void TimeHasChanged(object clock, TimeInfoEventArgs ti)
    {
        Console.WriteLine("{0}", ti.msg);
    }
}

class Tester
{
    public void Run()
    {
        Console.WriteLine("通知してほしい経過時間を入力");
        Console.Write("時間: ");
        int hours = Convert.ToInt16(Console.ReadLine());
        Console.Write("分: ");
        int minutes = Convert.ToInt16(Console.ReadLine());
        Console.Write("秒: ");
        int seconds = Convert.ToInt16(Console.ReadLine());
        Console.Write("経過したときのメッセージ: ");
        string msg = Console.ReadLine();

        Clock clock = new Clock(msg, hours, minutes, seconds);
        DisplayClock dc = new DisplayClock();
        dc.Subscribe(clock);
        clock.Run();
    }
    static void Main()
    {
        Tester t = new Tester();
        t.Run();
    }
}

入出力結果(Console Window)

通知してほしい経過時間を入力
時間: 0
分: 0
秒: 10
経過したときのメッセージ: 10秒経過!
0秒経過
1秒経過
2秒経過
3秒経過
4秒経過
5秒経過
6秒経過
7秒経過
8秒経過
9秒経過
10秒経過
10秒経過!
続行するには何かキーを押してください . . .

ちなみにJavaScriptの場合。

通知して欲しい経過時間




コード(TextWrangler)

var msg = $('#t0').val();
var hours = parseInt($('#t1').val());
var minutes = parseInt($('#t2').val());
var seconds = parseInt($('#t3').val());

var start = new Date();
var stop = new Date();
stop.setHours(start.getHours() + hours);
stop.setMinutes(start.getMinutes() + minutes);
stop.setSeconds(start.getSeconds() + seconds);
var i = 0;
var timer = setInterval(function(){
  $('#pre0').append(i + "秒経過\n");
  if(start >= stop){
    $('#pre0').append(msg);
    clearInterval(timer);
  } else {
    i++;
    start.setSeconds(start.getSeconds() + 1);
  }
}, 1000);


pythonの場合。

sample.py

コード(TextWrangler)

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

import datetime
import time

print("通知して欲しい経過時間を入力")
print("時: ", end="")
hours = int(input())
print("分: ", end="")
minutes= int(input())
print("秒: ", end="")
seconds = int(input())
print("経過したときのメッセージ: ", end="")
msg = input()

start = datetime.datetime.today()
stop = datetime.datetime(start.year, start.month, start.day, start.hour + hours, start.minute + minutes, start.second + seconds)
i = 0
while True:
  print("{0}秒経過".format(i))
  i += 1
  now = datetime.datetime.today()
  if now >= stop:
    print(msg)
    break
  time.sleep(1)

入出力結果(Terminal)

$ ./sample.py
通知して欲しい経過時間を入力
時: 0
分: 0
秒: 10
経過したときのメッセージ: 10秒経過!
0秒経過
1秒経過
2秒経過
3秒経過
4秒経過
5秒経過
6秒経過
7秒経過
8秒経過
9秒経過
10秒経過
10秒経過!
$

0 コメント:

コメントを投稿