annotate factorial.c @ 7:c49bf9e28ea8

remove jmp_buf (replaced by integer array)
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Sun, 01 Dec 2013 14:59:18 +0900
parents dee9711aeb06
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include <stdio.h>
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 #include <stdlib.h>
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 #include <setjmp.h>
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 __code print_factorial(int prod)
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 {
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 printf("factorial = %d\n",prod);
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 exit(0);
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 }
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 __code factorial0(int prod, int x)
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 {
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 if ( x >= 1) {
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 goto factorial0(prod*x, x-1);
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 }else{
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 goto print_factorial(prod);
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 }
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 }
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 int factorial(int x)
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 {
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 void *__return;
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 void *__enviroment;
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 printf("factorial entry\n");
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 __enviroment = (void*)malloc(sizeof(jmp_buf));
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 if (setjmp(__enviroment)){
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 free(__enviroment);
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 printf("factorial return\n");
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 return 0;
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 }
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 __return = (void*)return1;
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 goto factorial0(1, x);
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 return -1;
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 }
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 int
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 main(int argc, char **argv)
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 {
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 int i,ans;
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 i = atoi(argv[1]);
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 ans = factorial(i);
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 printf("answer = %d\n",ans);
dee9711aeb06 the first commit
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 }