2015年4月3日金曜日

開発環境

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

その他参考書籍

ピザの一切れ(p.150)

コード(BBEdit, Emacs)

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

int main(int argc, char **argv) {
  char *delivery = "";
  int thick = 0;
  char ch;
  while ((ch = getopt(argc, argv, "d:t")) != EOF)
    if (ch == 'd') delivery = optarg;
    else if (ch == 't') thick = 1;
    else {
      fprintf(stderr, "Unknown option: '%s'\n", optarg);
      return 1;
    }

  argc -= optind;
  argv += optind;

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

  printf("Ingredients:\n");

  for (int count = 0; count < argc; count++)
    printf("%s\n", argv[count]);
}
π

入出力結果(Terminal)

p
$ crun.sh sample150
clang ...
Ingredients:
$ ./sample150 cheese tomato
Ingredients:
cheese
tomato
$ ./sample150 -d '15:00' cheese tomato
To be delivered 15:00.
Ingredients:
cheese
tomato
$ ./sample150 -d '15:00' -t cheese tomato
Thick crust.
To be delivered 15:00.
Ingredients:
cheese
tomato
$ ./sample150 -td '15:00'  cheese tomato
Thick crust.
To be delivered 15:00.
Ingredients:
cheese
tomato
$

0 コメント:

コメントを投稿