用C语言创建Fibonacci序列

用C语言创建Fibonacci序列,c,C,我正在为一所学校的实验室用C语言创建斐波那契序列生成器,但它不可能是0 1 2 3 5等的常规方法。。。程序应该向用户请求一个限制和两个数字,然后从那里生成序列 以下是我到目前为止的情况: #include <stdio.h> #include <stdlib.h> int main(void) { int num1, num2, num3, limit; int Fibarray[10]; //Ask the user for a limit

我正在为一所学校的实验室用C语言创建斐波那契序列生成器,但它不可能是0 1 2 3 5等的常规方法。。。程序应该向用户请求一个限制和两个数字,然后从那里生成序列

以下是我到目前为止的情况:

#include <stdio.h>
#include <stdlib.h>
int main(void) 
{
    int num1, num2, num3, limit;
    int Fibarray[10];
    //Ask the user for a limit to the number of values displayed, checks to see if value is greater
    // than 10 or less than 0, if so displays an error message  
    printf("Please enter limit of values generated, the limit must be 10 or less, but greater than 0\n");
    scanf ("%i", &limit);
    if (limit > 10)
    {
        printf("Please enter a valid integer greater than 0, and less than or equal to 10");
    }
    else    
        printf("Please enter 2 numbers separated by a space, the second number must be larger than the first"); 
    scanf ("%i", &num1, &num2);
    if (num1>num2||num1<0)
    {
        puts("Please re enter your numbers, make sure they are in ascending order & the second number is greater than 0");
        return(15); 
    }
    else
    {
        // ...
    }
}
#包括
#包括
内部主(空)
{
int num1,num2,num3,极限;
int-Fibarray[10];
//要求用户限制显示的值的数量,检查值是否更大
//小于10或小于0,如果是,则显示错误消息
printf(“请输入生成值的限制,限制必须小于等于10,但大于0\n”);
scanf(“%i”、&limit);
如果(限值>10)
{
printf(“请输入一个大于0且小于或等于10的有效整数”);
}
其他的
printf(“请输入两个空格分隔的数字,第二个数字必须大于第一个”);
scanf(“%i”、&num1、&num2);
如果(num1>num2 | | num1关于:

scanf ("%i", &num1, &num2); 
这将导致编译器输出警告消息,并且永远不会设置变量num2。建议:

size_t num1;
size_t num2;

....

if( 2 != scanf( "%lu %lu", &num1, &num2 ) ) 
{ 
    fprintf( stderr, "scanf for num1, num2 failed\n" );
    exit( EXIT_FAILURE ); 
} 

要打印序列,请执行以下步骤:

  • 输出
    a
    ,与
    printf
  • 将下一个数字
    c
    计算为
    a
    b
  • 更新配对:
    a=b
    b=c
  • 迭代
    limit
关于您的代码,这里有一个改进版本,具有更严格的输入检查:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int num1, num2, num3, limit;

    //Ask the user for a limit to the number of values displayed, checks to see if value is greater
    // than 10 or less than 0, if so displays an error message  
    printf("Please enter limit of values generated, the limit must be 10 or less, but greater than 0:\n");
    if (scanf("%i", &limit) != 1 || limit <= 0 || limit > 10) {
        printf("Please enter a valid integer greater than 0, and less than or equal to 10\n");
        return 1;
    }
    printf("Please enter 2 numbers separated by a space, the second number must be larger than the first:\n");
    if (scanf("%i%i", &num1, &num2) != 2 || num1 > num2) {
        puts("Please re enter your numbers, make sure they are in ascending order\n");
        return 2; 
    }
    // ...
}
#包括
#包括
内部主(空){
int num1,num2,num3,极限;
//要求用户限制显示的值的数量,检查值是否更大
//小于10或小于0,如果是,则显示错误消息
printf(“请输入生成值的限制,限制必须小于等于10,但大于0:\n”);
如果(扫描频率(“%i”,&limit)!=1 | | limit 10){
printf(“请输入一个大于0且小于或等于10的有效整数\n”);
返回1;
}
printf(“请输入两个空格分隔的数字,第二个数字必须大于第一个:\n”);
如果(scanf(“%i%i”、&num1、&num2)!=2 | | num1>num2){
puts(“请重新输入您的号码,确保它们按升序排列\n”);
返回2;
}
// ...
}

您不需要数组(除非您以后要打印结果)。与通常的斐波那契序列一样,您需要在每个循环中洗牌三个变量。唯一的区别是两个起始值。MSVC的可能副本会给出两个关于
scanf(“%i”、&num1和&num2)的警告;
-警告C4474:“scanf”:为格式字符串传递的参数太多,注意:占位符及其参数需要1个可变参数side:不清楚要求的限制是术语的数量还是达到的值。问题更多的是输入值,而不是生成斐波那契序列,而您不知道show.@coderredoc My bad,我已经删除了注释。仍然存在未定义的行为,因为
num2
从未初始化。