開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C++ (プログラミング言語)
- g++(コンパイラ)
C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅠ部(基礎)の7章(プログラミングの手順)、7.15(プログラミング実習)、実習 7-4.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 7-4.
コード(Emacs (CUI)、BBEdit, Emacs)
sample.cpp
/* * 消費税を加算した金額を求めるプログラム */ #include <iostream> int main() { int price; // 金額 float vat; // 羊皮税 float total; // 合計(結果を1円単位に丸める前 int result; // 合計(小数点以下を四捨五入後) std::cout << "金額を入力: "; std::cin >> price; vat = price * 0.05; total = price + vat; total *= 10; result = total; if ((result % 10) < 5){ result /= 10; } else { result = (result + 10) / 10; } std::cout << result << "円\n"; return (0); }
Makefile
# # FSFのg++コンパイラ用のMakefile # CC=g++ CFLAGS=-g -Wall all: sample sample: sample.cpp $(CC) $(CFLAGS) -o sample sample.cpp clean: rm sample
入出力結果(Terminal)
$ make g++ -g -Wall -o sample sample.cpp $ ./sample 金額を入力: 9 9円 $ ./sample 金額を入力: 10 11円 $ ./sample 金額を入力: 100 105円 $
0 コメント:
コメントを投稿