1 代码
代码:
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdbool.h>
int fabonacci_fun(int );
void debug_print();
void debug_number();
bool check_num(char*);
int main(int argc, char *argv[])
{
int ret = 0;
if(2 != argc)
debug_print();
if (false == check_num(argv[1]))
debug_number();
int num = 0;
num = atoi(argv[1]);
long long sum = 0;
sum = fabonacci_fun(num);
printf("sum = %lld\n", sum);
return ret;
}
void debug_print(void)
{
printf("usage:\n"" ./fabonacci xxx\n");
exit(0);
}
void debug_number()
{
printf("./fabonacci xxx:\n"" please input the integer number xxx \n");
exit(0);
}
bool check_num(char* str)
{
bool flag = false;
if ((strspn(str, "0123456789") == strlen(str)))
flag = true;
else
flag = false;
return flag;
}
int fabonacci_fun(int num)
{
int sum = 0;
if(0 == num)
sum = 0;
else if (1 == num )
sum = 1;
else
sum = fabonacci_fun(num - 1) + fabonacci_fun(num - 2);
return sum;
}
2 实验结果
第一步:编译
第二步:运行