From 815ab7fd754d9d93d7a2bfb9369d64721a2b4e4e Mon Sep 17 00:00:00 2001 From: j8takagi Date: Sun, 19 Dec 2010 23:41:31 +0900 Subject: [PATCH] =?utf8?q?=E3=82=B5=E3=83=B3=E3=83=97=E3=83=AB=E3=82=92?= =?utf8?q?=E5=BE=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- sample/triangle/.gitignore | 1 + sample/triangle/Makefile | 6 +++++- sample/triangle/test/.gitignore | 1 + sample/triangle/triangle.c | 38 ++++++++++++--------------------- 4 files changed, 21 insertions(+), 25 deletions(-) create mode 100644 sample/triangle/.gitignore diff --git a/sample/triangle/.gitignore b/sample/triangle/.gitignore new file mode 100644 index 0000000..810d36e --- /dev/null +++ b/sample/triangle/.gitignore @@ -0,0 +1 @@ +triangle diff --git a/sample/triangle/Makefile b/sample/triangle/Makefile index 19f8308..c863de4 100644 --- a/sample/triangle/Makefile +++ b/sample/triangle/Makefile @@ -1,11 +1,15 @@ CC = gcc -.PHONY: all clean +.PHONY: all check clean all: triangle triangle: triangle.c $(CC) -o triangle triangle.c +check: + @$(MAKE) -sC test + clean: rm -f triangle + $(MAKE) -sC test clean diff --git a/sample/triangle/test/.gitignore b/sample/triangle/test/.gitignore index c4affcf..c14598e 100644 --- a/sample/triangle/test/.gitignore +++ b/sample/triangle/test/.gitignore @@ -1,3 +1,4 @@ +*.log */1.txt */diff.txt */err.txt diff --git a/sample/triangle/triangle.c b/sample/triangle/triangle.c index c38f9a0..9065bfa 100644 --- a/sample/triangle/triangle.c +++ b/sample/triangle/triangle.c @@ -13,7 +13,6 @@ http://www.amazon.co.jp/dp/4764900599 #include #include -/* #define DEBUG */ int main(int argc, char *argv[]) { @@ -30,39 +29,30 @@ int main(int argc, char *argv[]) } return -1; } - /* 引数が整数かチェック。整数以外の場合はエラー終了 */ + /* 引数が自然数かチェック。自然数以外の場合はエラー終了 */ for(i = 0; i < 3; i++) { l[i] = strtol(argv[i+1], &check, 10); if(*check != '\0' || l[i] <= 0) { fprintf(stderr, "%s: invalid argument.\n", argv[i+1]); - #ifdef DEBUG - printf("i:%d argv[i+1]:%s check:%s l[%d]:%d\n", i, argv[i+1], check, i, l[i]); - #endif return -1; } } - #ifdef DEBUG - printf("l[0]:%d l[1]:%d l[2]:%d\n", l[0], l[1], l[2]); - #endif /* 三角不等式により、三角形になるかを判定 */ if(l[0] + l[1] <= l[2] || l[1] + l[2] <= l[0] || l[2] + l[0] <= l[1]) { puts("not triangle"); - } else { - /* 3辺の長さが等しい場合は、正三角形 */ - if(l[0] == l[1] && l[1] == l[2] && l[2] == l[0]) { - puts("equilateral triangle"); - } - /* 2辺の長さが等しい場合は、二等辺三角形 */ - else if (l[0] == l[1] || l[1] == l[2] || l[2] == l[0]) { - puts("isosceles triangle"); - } - /* それ以外の場合は、不等辺三角形 */ - else { - puts("scalene triangle"); - } + return -1; + } + /* 3辺の長さが等しい場合は、正三角形 */ + if(l[0] == l[1] && l[1] == l[2] && l[2] == l[0]) { + puts("equilateral triangle"); + } + /* 2辺の長さが等しい場合は、二等辺三角形 */ + else if (l[0] == l[1] || l[1] == l[2] || l[2] == l[0]) { + puts("isosceles triangle"); + } + /* それ以外の場合は、不等辺三角形 */ + else { + puts("scalene triangle"); } - #ifdef DEBUG - printf("l[0]:%d l[1]:%d l[2]:%d cosa:%f\n", l[0], l[1], l[2], cosa); - #endif return 0; } -- 2.18.0