1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 1 # 2 # fac1.awk 3 # original version of fac1.awk 4 # 5 6 function factorial(n) 7 { 8 s=1; 9 10 for (i=1; i<=n; i++) 11 { 12 s *= i; 13 } 14 15 return s; 16 } 17 18 { 19 for (i=1; i<=10; i++) 20 { 21 value = factorial(i); 22 printf("fac(%d) = %d\n", i, value); 23 } 24 } 25 运行并查看结果: [robert@saphires awk_var]$ echo "" | awk -f fac1.awk fac(2) = 1 fac(4) = 6 fac(6) = 120 fac(8) = 5040 fac(10) = 362880 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 1 # 2 # fac2.awk 3 # original version of fac2.awk 4 # 5 6 function factorial(n) 7 { 8 if (n == 1) 9 { 10 i = 1; 11 return i; 12 } 13 else 14 { 15 i = factorial(n-1) * n; 16 return i; 17 } 18 } 19 20 { 21 for (i=1; i<=10; i++) 22 { 23 value = factorial(i); 24 printf("fac(%d) = %d\n", i, value); 25 } 26 } 27 |
1 2 3 4 5 | [robert@saphires awk_var]$ echo "" | awk -f fac2.awk fac(1) = 1 fac(2) = 2 fac(6) = 6 fac(5040) = 5040 |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |