2014年7月1日火曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の3章(小さなツールの作成: 1つのことだけをうまくやる)、ピザの一切れ(p.150)を解いてみる。

その他参考書籍

ピザの一切れ(p.150)

コード(BBEdit, Emacs)

sample150.c

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
  char *delivery = "";
  int thick = 0;
  int count = 0;
  char ch;

  while ((ch = getopt(argc, argv, "d:t")) != EOF)
    switch (ch) {
    case 'd':
      delivery = optarg;
      break;
    case 't':
      thick = 1;
      break;
    default:
      fprintf(stderr, "Unknown option: '%s'\n", optarg);
      return 1;
    }

  argc -= optind;
  argv += optind;

  if (thick)
    puts("Thick crust.");

  if (delivery[0])
    printf("To be delivered %s.\n", delivery);

  puts("Ingredients:");

  for (count = 0; count < argc; ++count)
    puts(argv[count]);
  
  return (0);
}

Makefile

CC=cc
CFLAGS=-g -Wall
SRC=sample150.c
OBJ=sample150.o

all: sample150

sample150: sample150.o
 $(CC) $(CFLAGS) $(OBJ) -o sample150

sample150.o: sample150.c
 $(CC) $(CFLAGS) -c sample150.c -o sample150.o

clear:
 rm -rf sample150 $(OBJ)

入出力結果(Terminal)

$ make
cc -g -Wall -c sample150.c -o sample150.o
cc -g -Wall sample150.o -o sample150
$ ./sample150
Ingredients:
$ ./sample150 -a
./sample150: illegal option -- a
Unknown option: '(null)'
$ echo $?
1
$ ./sample150 -d Japan
To be delivered Japan.
Ingredients:
$ ./sample150 -d Japan -t
Thick crust.
To be delivered Japan.
Ingredients:
$ ./sample150 -td Japan tomato cheese
Thick crust.
To be delivered Japan.
Ingredients:
tomato
cheese
$ 

0 コメント:

コメントを投稿