C 中的命令行参数返回大数

Command Line Arguments in C Returning Large Numbers

我正在编写一个基本程序来使用命令行参数使用 C 计算算术几何平均值。但是该程序似乎无法识别我输入的任何内容。这是我的代码:

/*
*This program will calculate an arithmetic-geometric mean
*provided two numbers (x,y) and an epsilon (e) are entered.
*/

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

int main (int argc, char **argv) {

    //Check for command line argument.
    if (argc != 4) {
        printf ("Please enter two numbers (x,y) and an epsilon (e)\n");
        printf ("as command line arguments for an AGM calculation\n");
        exit(1);
    }

    double e,x,y,an,gn;

    x = atof (argv[1]); //First number x.
    y = atof (argv[2]); //Second number y.
    e = atof (argv[3]); //Number of repetitions e.

    double absoluteAnMinusGn; //Continuation condition.
    double a = (x + y) / 2; //Normal arithmetic mean.
    double g = sqrt (x * y); //Normal geometric mean.

    an = (a + g) / 2; //Iteration 1 for calculation arithmetic mean.
    gn = sqrt (a * g); //Iteration 1 for calculation geometric mean.
    absoluteAnMinusGn = an - gn; //Calculates continuation condition.
       if (absoluteAnMinusGn < 0) {
           absoluteAnMinusGn = absoluteAnMinusGn * (-1); //Ensures absolute value of continuation condition.
        }

    printf ("DEBUG IN: x%d, y%d, e%d, absoulteAnMinusGn%d, a%d, g%d, an%d, gn%d\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE 

    while (absoluteAnMinusGn > e) {
        an = (a + g) / 2;
        gn = sqrt (a * g);
        a = an;
        g = gn;
        absoluteAnMinusGn = an - gn;
        if (absoluteAnMinusGn < 0) {
            absoluteAnMinusGn = absoluteAnMinusGn * (-1);
        }
    }

    //printf ("The arithmetric-geometric mean is (%d,%d) for %d\n", a,g,e); 
            printf ("DEBUG OUT: x%d, y%d, e%d, absoulteAnMinusGn%d, a%d, g%d, an%d, gn%d\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE 

    return 0;
}

我在命令行输入以下内容:agm.exe 3 4 5

我得到以下输出:

DEBUG IN: x0, y10742661112, e0, absoluteANMinusGn1074790400, a0, g1075052544, an-171951648, gn1057505593

DEBUG OUT: x0, y10742661112, e0, absoluteANMinusGn1074790400, a0, g1075052544, an-171951648, gn1057505593

我昨天制作了一个类似的程序,用于使用命令行输入计算积分,完全符合预期。代码在这里:

/*
*This program will calculate a Riemann sum using the 
*left hand rule for sin(x)/x.
*/

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

int main (int argc, char **argv) {

    //Check for command line argument.
    if (argc != 4) {
        printf ("Please enter integral bounds (a,b) and number of intervals (n)\n");
        printf ("as command line arguments for a Riemann sum calculation \n");
        exit(1);
    }

    double a,b,i,n,h,riemann,rectangles,answer;

    a = atof (argv[1]); //Lower bound of integral.
    b = atof (argv[2]); //Upper bound of integral.
    n = atof (argv[3]); //Number of intervals.

    h = (b - a) / n; //Delta X.
    i = 0; //Counts intervals.

    //Calculation of Left Hand Riemann Sum.
    while (i <= (n - 1)) {
        if (a == 0 && i == 0) { //Stops from dividing by zero.
            rectangles = 1;
            i++;
        }
        riemann = (sin(a + (i * h))) / (a + (i * h));
        rectangles += riemann;
        i++;
    }
    //Finalize answer.
    answer = rectangles * h;

    printf ("Sin(x)/x for bounds (%f , %f) with %f intervals is approximately %f \n", a,b,n,answer);

    return 0;
}

上面的左手黎曼和程序输出正确,几乎与我为 AGM 编写的代码相同。有人可以帮我弄清楚这里出了什么问题吗?我到处搜索,找不到解决方案。我知道 AGM 代码可能设置为输出不正确的答案,但我主要关心的是修复我的命令行参数识别。我可以稍后重做我的数学。

双精度打印的格式说明符是%f。如果您没有提供正确的格式说明符来处理双精度,并且将双精度传递给 %d 格式说明符 - 它会导致未定义的行为。(%d 期望整数参数不是双精度)(在你的大小写错误输出)。

来自 §7.21.6.3p9 N1570(c11 标准)

If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

只需将 %d(整数的格式说明符)替换为 %f(双精度的格式说明符),因为你是使用双精度类型的变量,而不是整数。

我没有测试你的算法,但我在你的代码中发现了至少三个问题。我现在列出如下:

  1. 忘记初始化你的变量,这就是为什么你有一个 Large Numbers 这是内存未定义的数据!
  2. 没有将您的 argv 转换为整数类型。
  3. printf 的格式错误。

我把我的固定代码放在这里,你可以测试一下。如果它能运行如您所愿!

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

int main (int argc, char *argv[]) {
    if (argc != 4) {
        printf ("Please enter two numbers (x,y) and an epsilon (e)\n");
        printf ("as command line arguments for an AGM calculation\n");
        exit(1);
    }

    //  argv is pointer which pointer to pointer 
    // and argv[1] is a pointer to a string.
    // so we need to covert string to int
    char *p=NULL;
    int x = strtol(argv[1], &p, 10);
    int y = strtol(argv[2], &p, 10);
    int e = strtol(argv[3], &p, 10);

    //Check for command line argument.

    double an = 0.0,gn =0.0;


    double absoluteAnMinusGn=0.0; //Continuation condition.
    double a = (x + y) / 2; //Normal arithmetic mean.
    double g = sqrt (x * y); //Normal geometric mean.

    an = (a + g) / 2; //Iteration 1 for calculation arithmetic mean.
    gn = sqrt (a * g); //Iteration 1 for calculation geometric mean.
    absoluteAnMinusGn = an - gn; //Calculates continuation condition.
       if (absoluteAnMinusGn < 0) {
           absoluteAnMinusGn = absoluteAnMinusGn * (-1); //Ensures absolute value of continuation condition.
        }

    printf ("DEBUG IN: x%d, y%d, e%d, absoulteAnMinusGn=%f, a=%f, g=%f, an=%f, gn=%f\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE 

    while (absoluteAnMinusGn > e) {
        an = (a + g) / 2;
        gn = sqrt (a * g);
        a = an;
        g = gn;
        absoluteAnMinusGn = an - gn;
        if (absoluteAnMinusGn < 0) {
            absoluteAnMinusGn = absoluteAnMinusGn * (-1);
        }
    }

    printf ("DEBUG OUT: x=%d, y=%d, e=%d, absoulteAnMinusGn=%f, a=%f, g=%f, an=%f, gn=%f\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE 
    return 0;
}

然后当我 运行 它带有如下所示的一些样本编号时,我得到了结果。