開発環境
- OS X Lion - Apple (UNIX) (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)の7章(プログラミング手順)、7.15(プログラミング実習)、実習 7-3を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
実習 7-3.
コード
sample.c
#include <stdio.h> int main(int argc, char *argv[]) { int cps; int bpd, bph, bpm, bps; int days, hours, mins; double secs; int s; char line[100]; cps = 960; bps = cps * 1; bpm = bps * 60; bph = bpm * 60; bpd = bph * 24; printf("ファイルサイズを入力(バイト): "); fgets(line, sizeof(line), stdin); sscanf(line, "%d", &s); days = s / bpd; s %= bph; hours = s / bph; s %= bph; mins = s / bpm; s %= bpm; secs = s / (double) bps; printf("ファイルの内容を転送するために要する時間: %d日%d時間%d分%lf秒\n", days, hours, mins, secs); return (0); }
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 ファイルサイズを入力(バイト): 419430400 ファイルの内容を転送するために要する時間: 5日0時間21分46.666667秒 $
0 コメント:
コメントを投稿