2015年1月28日水曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の4章(複数のソースファイルの使用: 分割して構築する)、エクササイズ(p. 165)を解いてみる。

その他参考書籍

エクササイズ(p. 165)

コード(BBEdit, Emacs)

sample165.c

#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(){
  float val;
  printf("品目の値段: ");
  while (scanf("%f", &val) == 1) {
    printf("ここまでの合計: %.2f\n", add_with_tax(val));
    printf("品目の値段: ");
  }
  printf("\n最終合計: %.2f\n", total);
  printf("品目数: %hi\n", count);
}

入出力結果(Terminal)

$ crun.sh sample165
clang ...
品目の値段: 5
ここまでの合計: 5.30
品目の値段: 95
ここまでの合計: 106.00
品目の値段: 
最終合計: 106.00
品目数: 2
$

0 コメント:

コメントを投稿