代码之家  ›  专栏  ›  技术社区  ›  Raoul Duke

如何获取用户在'之后输入的命令行参数整数的GCD/a、 有什么顺序吗?

  •  1
  • Raoul Duke  · 技术社区  · 11 年前

    此程序返回命令行的GCD 用户仅从至少到润滑测试输入的参数。 例如:

    用户输入:'/a、 外5 10 15 20 25'

    此程序返回:“命令行参数的GCD为5”

    然而,我遇到的问题是,如果用户键入例如:

    用户输入:'/a、 外15 10 5 25 20'

    此程序返回:15

    有人能告诉我如何解决这个问题吗?

    这就是我的目标:

    如果用户输入:'/a、 外15 10 5 25 20'

    此程序应返回:5

    //Header Files
    #include<stdio.h>
    #include<string.h>
    
    //Main Method
    int main(int argc, char *argv[]){
    
    //Declared variables here and print statements
     int i,x,y,min;
     printf("Number of command line args is %d\n", argc);
     printf("The GCD is:\t");
    
     //This is the main while loop
     while( x !=0 && y !=0 && y != x){
    
     if(x<y){
      y=y-x;
     }//End first IF statement
     if(y<x){
      x-x-y;
     }//End second IF statement
     }//End while loop
    
     //This function returns the converted integral number as an int value
      x=atoi(argv[i]);
      for(i=2;i<argc;i++){
      y=atoi(argv[i]);
       }
    
       //The following code gets the GCD and prints from the command line
       min = (x>y)?x:y;
      for(i=min;i>=1;--i){
      if(x%i==0 && y%i==0){
      for(i=1;i<argc;i++){
       printf("%s\n", argv[i]);
        break;}//End for loop
        }//End IF statement
       }//End For loop
      }//End of MAIN
    
    2 回复  |  直到 11 年前
        1
  •  1
  •   Manish Kothari    11 年前

    我已经清理干净了,这应该对你有用:

    在考虑在代码中计算之前初始化每个变量。

    #include<stdio.h>
    #include<string.h>
    
    //Main Method
    int main(int argc, char *argv[]){
    
    //Declared variables here and print statements
     int i,x,y,gcd;
     printf("Number of command line args is %d\n", argc);
     printf("The GCD is:\t");
     x=atoi(argv[1]);
     for(i=2;i<argc;i++){
        y=atoi(argv[i]);
        while( x !=0 && y !=0 && y != x){
            if(x<y){
                y=y-x;
            }else if(y<x){
                x=x-y;
            }
        }
        gcd=x;
     }
     printf("%d",gcd);
    }//End of MAIN
    
        2
  •  1
  •   btevfik    11 年前

    如果您想使用递归。

    #include<stdio.h>
    #include<string.h>
    
    int GCD(int numbers[], int count)
    {
        if (count==1)
            return numbers[0];
        int i, j, min, x, y;
        x = numbers[0];
        y = numbers[1];
        min = (x>y)?x:y;
        for(i=min;i>=1;--i){
            if(x%i==0 && y%i==0){
                int newNumbers[count-1];
                newNumbers[0]=i;
                for (j=1; j<count-1; j++) {
                    newNumbers[j] = numbers[j+1];
                }
                return GCD(newNumbers, count-1);
            }
        }
        return 0;
    }
    
    int main(int argc, const char * argv[]) {
        //Declared variables here and print statements
        int i;
        printf("Number of command line args is %d\n", argc);
        printf("The GCD is:\t");
    
        int numbers[argc];
    
        for(i=1;i<argc;i++){
            numbers[i]=atoi(argv[i]);
        }
    
        int result = GCD(numbers, argc);
        printf("%d\n", result);
    
    }