開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅡ部(単純なプログラミング)の12章(高度な型)、12.1(構造体)、12.2(共用体)、12.3(typedef)、12.4(enum型)、12.5(キャスト)、12.6(ビットフィールドまたは構造体の圧縮)、12.7(構造体配列)、12.9(プログラミング実習)、実習12-2を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
実習12-2.
コード
sample.c
#include <stdio.h>
struct time_and_day{
int hour;
int minute;
int second;
int month;
int day;
};
int main()
{
struct time_and_day t1 = {
1,
2,
3,
4,
5
};
struct time_and_day t2 = {
6,
7,
8,
9,
10
};
double difference(struct time_and_day t1, struct time_and_day t2);
void display(struct time_and_day t);
display(t1);
display(t2);
printf("時刻の違い(分単位): %.2f分\n", difference(t1, t2));
display(t2);
display(t1);
printf("時刻の違い(分単位): %.2f分\n", difference(t2, t1));
return (0);
}
double difference(struct time_and_day a, struct time_and_day b){
double n = 60 * (a.hour - b.hour) + (a.minute - b.minute) +
(a.second - b.second) / 60;
return n >= 0 ? n : -n;
}
void display(struct time_and_day t)
{
printf("%d月%d日%d時%d分%d秒\n",
t.month, t.day, t.hour, t.minute, t.second);
}
makefile
CC=cc CFLAGS=-g sample: sample.c $(CC) $(CFLAGS) -o sample sample.c clean: rm -f sample
入出力結果(Terminal)
$ make cc -g -o sample sample.c $ ./sample 4月5日1時2分3秒 9月10日6時7分8秒 時刻の違い(分単位): 305.00分 9月10日6時7分8秒 4月5日1時2分3秒 時刻の違い(分単位): 305.00分 $
0 コメント:
コメントを投稿