2012年10月18日木曜日

開発環境

『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) II部(単純なプログラミング)の12章(高度な型)12.9(プログラミング実習)実習12-2を解いてみる。

実習12-2.

コード(TextWrangler)

#include <stdio.h>

struct date{
  int hour;
  int minute;
  int month;
  int day;
};
int f(struct date d1, struct date d2);
void p(struct date d);

int main(){
  int i;
  struct date d1 = {
    1,
    2,
    3,
    4
  };
  struct date d2 = {
    5,
    6,
    7,
    8
  };
  struct date dates[2] = {d1, d2};
  for(i = 0 ; i < 2; i++){
    p(dates[i]);
  }
  printf("2つの時刻の違い(分単位): %d分\n", f(d1, d2));
  return (0);
}

int f(struct date d1, struct date d2){
  int result = (d1.hour - d2.hour) * 60 + (d1.minute - d2.minute);
  if(result < 0) result *= -1;
  return result;
}
void p(struct date d){
  printf("%d月%d日%d時%d分\n", d.month, d.day, d.hour, d.minute);
}

入出力結果(Terminal)

$ cc -g -o sample sample.c
$ ./sample
3月4日1時2分
7月8日5時6分
2つの時刻の違い(分単位): 244分
$

0 コメント:

コメントを投稿