C程序来计算斜边

时间:2017-04-08 10:58:50

标签: c function hypotenuse

我对编码很新,目前正在学习C.在课堂上我被赋予编写程序的任务,该程序通过使用我们自己的函数来计算三角形的斜边。但是,我编写的代码似乎有问题。

#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未定义(这就是为什么我只是随意将其设置为1,但还有其他方法可以防止此错误发生吗?)
2)例如,如果我输入3和4为side1side2,则side3应为5.但是,打印结果是一个荒谬的长数。

谢谢你的帮助!任何建议的话都表示赞赏。

5 个答案:

答案 0 :(得分:3)

您不需要side3变量 - 它不用于计算。函数hypotenuse返回结果,因此您可以直接输出sqrt的结果。

答案 1 :(得分:3)

我使用Ubuntu Linux并以这种方式编写它。如果你喜欢它,请看看。

#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

答案 2 :(得分:2)

OP的代码存在一些问题:

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

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()@ghostprgmr

// 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代码的选择输入值,这可能没问题,但一般情况下选择不当。如果输入值变小(如0.001和0.002),则输出将打印舍入值0.00。如果值非常大,则结果将显示许多非重要数字,如130899030500194208680850288727868915862901750748094271410143‌​232.00所示。

要进行开发和调试,请考虑使用"%e""%g""%a"查看相关的double

请注意,即使数学 x * x + y * y位于sqrt(x * x + y * y)范围内,double也容易出现过度/不足流量。这是标准函数hypot(x,y)的一个优点,因为它通常可以很好地处理这些边缘情况。

答案 3 :(得分:1)

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

你不需要来编写自己的功能。标准C提供functions to calculate the hypotnuse

  

7.12.7.3 hypot函数

     

<强>概要

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

请注意,您可能需要与-lm链接,尽管在C标准的函数文档中未明确列出,the latest POSIX documentation。它可能会在标准的其他地方记录。

Link to C11 [draft] standard - 可能寿命更长。)

答案 4 :(得分:1)

使用正确的格式说明符! double的Format Specifier不是%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);


}