计算斜边的 C 程序

C Program to Calculate Hypotenuse

我对编码还很陌生,目前正在学习 C。在 class 中,我接到了一项任务,要求我编写一个程序,使用我们自己的函数计算三角形的斜边。但是,我写的代码好像有问题。

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

double hypotenuse(double x, double y, double z);

int main(void) {
    double side1, side2, side3, counter;

    side3 = 1;

    for (counter = 0; counter <= 2; counter++) {
        printf("Enter values for two sides: ");
        scanf_s("%d %d", &side1, &side2);

        printf("%.2f\n", hypotenuse(side1, side2, side3));
    }

    return 0;
}

double hypotenuse(double x, double y, double z) {
    x *= x;
    y *= y;
    z = sqrt(x + y);

    return z;
}

我的老师说我们可以使用数学库的平方根函数 sqrt。我面临的主要错误是:

1) side3 is not defined (这就是为什么我随便将它设置为1,但是有没有其他方法可以防止这个错误发生?)
2)如果我,例如,输入3和4作为side1side2,那么side3应该是5。然而,打印的结果是一个荒谬的长数字。

感谢您的帮助!任何建议都将不胜感激。

您不需要 side3 变量 - 它不用于计算。而你函数hypotenusereturns的结果,所以你可以直接输出sqrt.

的结果

我用UbuntuLinux这样写。喜欢的话请看。

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

double hypotenuse(double x, double y) {
    double z = sqrt(x * x + y * y);
    return z;
}

int main(void) {
    double b1, b2, counter;
    for (counter = 0; counter <= 2; counter++) {
        printf("Enter values for two sides: ");
        scanf("%lf %lf", &b1, &b2);
        printf("%.2f\n", hypotenuse(b1, b2));
    }
    return 0;
}

测试

$ ./a.out 
Enter values for two sides: 1 1.73
2.00

作为查看此问题的任何人的参考:

不需要编写自己的函数。标准 C 提供 functions to calculate the hypotnuse:

7.12.7.3 The hypot functions

Synopsis

#include <math.h>
double hypot(double x, double y);
float hypotf(float x, float y);
long double hypotl(long double x, long double y);

请注意,您可能需要 link 和 -lm,尽管 C 标准的函数文档和 the latest POSIX documentation 中都没有明确列出。它可能记录在标准的其他地方。

(Link to C11 [draft] standard - 可能会长得多。)

OP的代码有一些问题:

关键问题:代码应该生成编译器警告,因为 scanf() 被指示将 &side1 视为 int *。打开所有编译器警告以节省您的时间。代码使用 "%d" 而不是匹配 "%lf" 来读取 double。还应检查 return 值以验证输入。

double side1, side2, side3, counter;
...
// scanf_s("%d %d", &side1, &side2);
if (scanf_s("%lf %lf", &side1, &side2) != 2) puts("Input error");

size3 不需要。使用 2 个参数调用 hypotenuse()

// printf("%.2f\n", hypotenuse(side1, side2, side3));
printf("%.2f\n", hypotenuse(side1, side2));

// double hypotenuse(double x, double y, double z) {
double hypotenuse(double x, double y) {
   double z = ...

Minor:代码使用 "%.2f" 打印斜边的值。对于 OP 代码的 select 输入值,这可能没问题,但通常是一个糟糕的选择。如果输入值变化很小,如 0.001 和 0.002,输出将打印四舍五入的值 0.00。对于非常大的值,结果将显示许多不重要的数字,因为 OP 在 130899030500194208680850288727868915862901750748094271410143‌​232.00.

中找到

对于开发和调试,考虑使用 "%e" ,"%g""%a" 来查看相关的 double.


请注意 x * x + y * y 容易出现 over/under 流动,即使 数学上 sqrt(x * x + y * y)double 范围内也是如此。这是标准函数 hypot(x,y) 的优势之一,因为它通常可以很好地处理这些边缘情况。

使用正确的格式说明符! double 的格式说明符不是 %d!休息很好。

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

double hypotenuse(double x, double y, double z);

int main(void) {
    double side1, side2, side3, counter;

    side3 = 1;

    for (counter = 0; counter <= 2; counter++) {
        printf("Enter values for two sides: ");
        scanf("%lf %lf", &side1, &side2);

        printf("%.2f\n", hypotenuse(side1, side2, side3));
    }

    return 0;
}

double hypotenuse(double x, double y, double z) {
    x *= x;
    y *= y;
    z = sqrt(x + y);

    return z;
}

你也可以修改成这样:

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

double hypotenuse(double x, double y);

int main(void) {
    double side1, side2, counter;



    for (counter = 0; counter <= 2; counter++) {
        printf("Enter values for two sides: ");
        scanf("%lf %lf", &side1, &side2);

        printf("%.2f\n", hypotenuse(side1, side2));
    }

    return 0;
}

double hypotenuse(double x, double y) {
    x *= x;
    y *= y;
    return sqrt(x + y);


}