開発環境
- 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-4を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
実習 7-4.
コード
sample.c
#include <stdio.h> #include <math.h> int main(int argc, char *argv[]) { int price; double vat_price; char line[100]; printf("料金を入力: "); fgets(line, sizeof(line), stdin); sscanf(line, "%d", &price); vat_price = price * 1.05; /* 1円単位に丸める(小数点以下を四捨五入する)が上手くいってるかを確認 * するために、丸め前と後を出力 */ printf("消費税込みの料金: %d円(%lf)\n", (int) round(vat_price), vat_price); 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 料金を入力: 10000 消費税込みの料金: 10500円(10500.000000) $ ./sample 料金を入力: 11111 消費税込みの料金: 11667円(11666.550000) $
0 コメント:
コメントを投稿