2014年5月11日日曜日

開発環境

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) -o sample150 $(OBJ)

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

clean:
 rm -rf sample150 sample150.o

入出力結果(Terminal)

$ make
cc -g -Wall -c sample150.c
cc -g -Wall -o sample150 sample150.o
$ ./sample150
Ingredients:
$ ./sample150 -d
./sample150: option requires an argument -- d
Unknown option: '(null)'
$ ./sample150 -d now
To be delivered now.
Ingredients:
$ ./sample150 -d now -t
Thick crust.
To be delivered now.
Ingredients:
$ ./sample150 -d now -t cheese
Thick crust.
To be delivered now.
Ingredients:
cheese
$ ./sample150 -d now -t cheese tomato
Thick crust.
To be delivered now.
Ingredients:
cheese
tomato
$ ./sample150 -d 'after 5' -t cheese tomato
Thick crust.
To be delivered after 5.
Ingredients:
cheese
tomato
$ ./sample150 -a
./sample150: illegal option -- a
Unknown option: '(null)'
$ ./sample150  -t
Thick crust.
Ingredients:
$ 

0 コメント:

コメントを投稿