2014年8月22日金曜日

開発環境

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

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

all: order_pizza bermuda

order_pizza: order_pizza.o
 $(CC) $(CFLAGS) order_pizza.o -o order_pizza

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

clear:
 rm -rf order_pizza $(OBJ)

入出力結果(Terminal)

$ make
cc -g -Wall -c order_pizza.c -o order_pizza.o
cc -g -Wall order_pizza.o -o order_pizza
$ ./order_pizza -a
./order_pizza: illegal option -- a
Unknown option: '(null)'
$ ./order_pizza cheese
Ingredients:
cheese
$ ./order_pizza -t -d afternoon
Thick crust.
to be delivered afternoon.
Ingredients:
$ ./order_pizza -t -d afternoon tomato cheese
Thick crust.
to be delivered afternoon.
Ingredients:
tomato
cheese
$ ./order_pizza -td afternoon tomato cheese
Thick crust.
to be delivered afternoon.
Ingredients:
tomato
cheese
$

0 コメント:

コメントを投稿