1

我想编写一个程序来计算斐波那契数列的第 n 个数字,我已经使用 printf 和 scanf 完成了。但我希望更改我的程序,以便在命令行输入序列号,而不是在程序提示时输入。这就是我想出的。它可以编译,但是当我运行它时它崩溃了……不知道为什么。任何建议,将不胜感激。

这是一个使用迭代计算斐波那契代码的第 n 个数字的程序。我已经这样写了:您必须在命令行 argv[1] 中输入您希望计算的序列号。然后程序接受这个命令行参数并在 while 循环中使用它,并打印这个数字。

#include <stdio.h>


int main( int argc, char**argv ) {
int fib[3] = {0,1};
int counter = 0;
  printf("The %dth Fibonacci number is:\n", atoi(argv[1]));
while ( counter < atoi(argv[1]) ) {

    fib[2] = fib[0] + fib[1];
    fib[0] = fib[1];
    fib[1] = fib[2];
    counter++;
}
printf("%d\n", fib[0]);
getchar();
  return 0;
}
4

5 回答 5

4

检查用户是否真的传递了一个参数:

int main( int argc, char**argv ) {
    if (argc < 2) {
        printf("Usage: %s number\n", argv[0]);
        return 1;
    }
    ...
}

如果他没有,argv[1]则为空,您将崩溃

于 2011-03-15T16:33:51.030 回答
3

程序应检查是否存在命令行参数:

if (argc < 2)
{
    printf ("usage:  %s n\n  where n is a positive integer\n", argv[0]);
    return 1;
}

如果没有提供参数,它可能会崩溃。

= = = = = 编辑 = = = = =

修复上述错误后,我看不到任何导致崩溃的原因。这工作正常:

#include <stdio.h>
int main( int argc, char**argv )
{
        int fib[3] = {0,1};
        int counter = 0;
        if (argc < 2)
        {
                printf ("usage: %s number\n", argv[0]);
                return 1;
        }

        printf("The %dth Fibonacci number is:\n", atoi(argv[1]));
        while ( counter < atoi(argv[1]) )
        {

                fib[2] = fib[0] + fib[1];
                fib[0] = fib[1];
                fib[1] = fib[2];
                counter++;
        }
        printf("%d\n", fib[0]);
        return 0;
}


[wally@zenetfedora ~]$ ./a.out 
usage: ./a.out number
[wally@zenetfedora ~]$ ./a.out 3
The 3th Fibonacci number is:
2
[wally@zenetfedora ~]$ ./a.out 4
The 4th Fibonacci number is:
3
[wally@zenetfedora ~]$ ./a.out 5
The 5th Fibonacci number is:
5
[wally@zenetfedora ~]$ ./a.out 6
The 6th Fibonacci number is:
8
[wally@zenetfedora ~]$ ./a.out 7
The 7th Fibonacci number is:
13
[wally@zenetfedora ~]$ ./a.out 8
The 8th Fibonacci number is:
21
[wally@zenetfedora ~]$ ./a.out 9
The 9th Fibonacci number is:
34
[wally@zenetfedora ~]$ ./a.out 10
The 10th Fibonacci number is:
55
于 2011-03-15T16:34:00.917 回答
0

你用什么号码测试的?因为一个 int 将无法保存高于第 50 个斐波那契数的任何东西。我必须在我的算法课上做这样的事情,我们必须找到第 239 个斐波那契数,64202014863723094126901777428873111802307548623680 这是一个 int 和 long 都不能正确持有的东西。我认为这不会让它崩溃,只是提醒一下如果你需要找到一个大的斐波那契数。

于 2011-03-15T16:42:15.193 回答
0
#include<stdio.h>

#include<stdlib.h>

int main(int c,char *v[])

{

int *numberOfTerms;

int x,y,z,i,count;

if(c==1)

{

printf("provide number of terms as command line argument");

return 0;

}

numberOfTerms=(int *)malloc(sizeof(int));

*numberOfTerms=atoi(v[1]);

x=0;

y=1;

if(*numberOfTerms==0) return 0;

if(*numberOfTerms<=2)

{

for(i=0;i<*numberOfTerms;i++) printf("%d\n",i);

return 0;

}

printf("%d\n",x);

printf("%d\n",y);

count=2;

while(count<*numberOfTerms)

{

z=x+y;

printf("%d\n",z);

x=y;

y=z;

count++;

}

}
于 2014-08-10T04:24:10.390 回答
0

如果要打印指定为命令行参数的特定数量的系列,请使用此程序:

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

int main(int c,char *v[])
{
    int *number;
    int x,y,z;
    if(c==1)
    {
        printf("provide number upto which febonacci is to be printed (greater than 2)");
        return 0;
    }
    number=(int *)malloc(sizeof(int));
    *number=atoi(v[1]);
    x=0;
    y=1;
    printf("%d\n",x);
    printf("%d\n",y);
    z=x+y;
    while(z<=*number)
    {
        printf("%d\n",z);
        x=y;
        y=z;
        z=x+y;
    }
}
于 2014-08-10T04:28:20.263 回答