C语言实现Fibonacci sequence(斐波那切数列)

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 实验结果

第一步:编译

                                  

第二步:运行

                                             

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值