2014年10月16日木曜日

開発環境

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

その他参考書籍

エクササイズ(p.165)

コード(BBEdit, Emacs)

sample165.c

#include <stdio.h>

float total = 0.0;
unsigned short count = 0;
int tax_percent = 6;

double add_with_tax(float f)
{
  float tax_rate = 1 + tax_percent / 100.0;
  total = total + (f * tax_rate);
  count++;

  return total;
}

int main(int argc, char *argv[])
{
  float val;
  
  printf("品目の値段:");
  while (scanf("%f", &val) == 1) {
    printf("個々までの合計:%.2f\n", add_with_tax(val));
    printf("品目の値段:");
  }
  printf("\n最終合計:%.2f\n", total);
  printf("品目数:%hd\n", count);

  return (0);
}

Makefile

P=
CC=cc
CFLAGS=-g -Wall # -O3
SRC=
OBJ=
LDLIBS=

$(P): $(OBJ)
 $(CC) $(CFLAGS) $(LDLIBS) $(OBJ) -o $@

入出力結果(Terminal)

$ make sample165
cc -g -Wall     sample165.c   -o sample165
$ ./sample165
品目の値段:1
個々までの合計:1.06
品目の値段:100
個々までの合計:107.06
品目の値段:10
個々までの合計:117.66
品目の値段:5
個々までの合計:122.96
品目の値段:
最終合計:122.96
品目数:4
$ 

0 コメント:

コメントを投稿