实现以下事例功能:
$./fibo 12
输出:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
//源码如下:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int fibo(int n)
{
if(n==1) return 1;
return n*fibo(n-1);
}
int main(int argc,char *argv[])
{
/* int x=pow(2.0,3.0); */
/* printf("pow(2.0,3.0)=%d/n",x); */
/* printf("the length of argv[1]=%d/n",strlen(argv[1])); */
if(argc!=2){
printf("usage:fibo x /n(the only parameter x which is a number low than 13!)/n");
exit(EXIT_FAILURE);
} //判断非法参数个数的输入
int i,pos;
int x=0;
int length = strlen(argv[1]); //先求数值参数的位数
for(i=length;i>0;i--){
pos = length-i;
if( argv[1][pos]<'0' || argv[1][0]>'9' ) //判断数值参数的有效性
printf("usage:fibo x /n(the only parameter x which is a number!)/n");
x+=(argv[1][pos]-'0')*pow(10.0,i-1);
// printf("x = %d/n",x); //测使用
}
if(x>13){
printf("num is too larage,input a num low than 13!/n");
exit(EXIT_FAILURE); //如果x大于13计算结果将溢出int范围。
}
for(i=1;i<=x;i++)
printf("%2d! = %d/n",i,fibo(i));
exit(EXIT_SUCCESS);
}