diff factorial.c @ 0:dee9711aeb06

the first commit
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Tue, 12 Nov 2013 11:32:39 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/factorial.c	Tue Nov 12 11:32:39 2013 +0900
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <setjmp.h>
+
+__code print_factorial(int prod)
+{
+  printf("factorial = %d\n",prod);
+  exit(0);
+}
+
+__code factorial0(int prod, int x)
+{
+  if ( x >= 1) {
+    goto factorial0(prod*x, x-1);
+  }else{
+    goto print_factorial(prod);
+  }
+
+}
+
+int factorial(int x)
+{
+  void *__return;
+  void *__enviroment;
+  printf("factorial entry\n");
+  __enviroment = (void*)malloc(sizeof(jmp_buf));
+  if (setjmp(__enviroment)){
+    free(__enviroment);
+    printf("factorial return\n");
+    return 0;
+  }
+  __return = (void*)return1;
+  goto factorial0(1, x);
+  return -1;
+}
+
+int
+main(int argc, char **argv)
+{
+  int i,ans;
+  i = atoi(argv[1]);
+
+  ans = factorial(i);
+  printf("answer = %d\n",ans);
+}