2014年3月20日木曜日

開発環境

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

その他参考書籍

ピザパズル(ピザの一切れ(p.150))

コード(BBEdit, Emacs)

order_pizza.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

all: order_pizza

order_pizza: order_pizza.c
 cc -g -o order_pizza order_pizza.c

clean:
 rm order_pizza

入出力結果(Terminal)

$ make && ./order_pizza
cc -g -o order_pizza order_pizza.c
ingredients:
$ make && ./order_pizza
cc -g -o order_pizza order_pizza.c
ingredients:
$ ./order_pizza cheese
ingredients:
cheese
$ ./order_pizza cheese tomato
ingredients:
cheese
tomato
$ ./order_pizza -d
./order_pizza: option requires an argument -- d
Unknown option: '(null)'
$ ./order_pizza -d now
to be delivered now.
ingredients:
$ ./order_pizza -d now cheese
to be delivered now.
ingredients:
cheese
$ ./order_pizza -d now cheese tomato
to be delivered now.
ingredients:
cheese
tomato
$ ./order_pizza t
ingredients:
t
$ ./order_pizza -t
Thick crust.
ingredients:
$ ./order_pizza -t cheese
Thick crust.
ingredients:
cheese
$ ./order_pizza -t cheese tomato
Thick crust.
ingredients:
cheese
tomato
$ ./order_pizza -d now -t
Thick crust.
to be delivered now.
ingredients:
$ ./order_pizza -d now -t cheese
Thick crust.
to be delivered now.
ingredients:
cheese
$ ./order_pizza -d now -t cheese tomato
Thick crust.
to be delivered now.
ingredients:
cheese
tomato
$

0 コメント:

コメントを投稿