開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio 2017
- プログラミング言語: C(Visual C): Visual Studio 2017
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の4章(複数のソースファイルの使用 - 分割して構築する)、エクササイズ(p. 165)を取り組んでみる。
エクササイズ(p. 165)
コード
#include <stdio.h>
float total = 0.0;
short count = 0;
short tax_percent = 6;
float add_with_tax(float f)
{
float tax_rate = 1 + tax_percent / 100.0f;
total = total + (f * tax_rate);
count = count + 1;
return total;
}
int main()
{
float val;
printf("品目の値段: ");
while (scanf_s("%f", &val) == 1) {
printf("ここまでの合計: %.2f\n", add_with_tax(val));
printf("品目の値段: ");
}
printf("\n最終合計: %.2f\n", total);
printf("品目数: %hi\n", count);
return 0;
}
入出力結果(コマンドプロンプト)
C:>Project2.exe 品目の値段: 12.34 ここまでの合計: 13.08 品目の値段: 45.67 ここまでの合計: 61.49 品目の値段: q 最終合計: 61.49 品目数: 2 C:>
0 コメント:
コメントを投稿