開発環境
- OS: macOS High Sierra - Apple
- IDE(統合開発環境): Xcode - Apple
- プログラミング言語: C
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の4章(複数のソースファイルの使用 - 分割して構築する)、エクササイズ(p. 165)を取り組んでみる。
エクササイズ(p. 165)
//
// main.c
// sample
//
// Created by kamimura on 2018/02/02.
// Copyright © 2018 kamimura. All rights reserved.
//
#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.0;
total += f * tax_rate;
count++;
return total;
}
int main(int argc, const char * argv[]) {
float val;
printf("品目の値段: ");
for (; scanf("%f", &val) == 1; ) {
printf("ここまでの合計: %.2f\n", add_with_tax(val));
printf("品目の値段: ");
}
printf("\n最終合計: %.2f\n", total);
printf("品目数: %hi\n", count);
return 0;
}
入出力結果(Terminal)
品目の値段: 1.2 ここまでの合計: 1.27 品目の値段: 3.4 ここまでの合計: 4.88 品目の値段: 最終合計: 4.88 品目数: 2 Program ended with exit code: 0
0 コメント:
コメントを投稿