在C编程中通过给定边找到三角形的角度

时间:2015-12-05 19:23:12

标签: c math

我在我的代码:: blocks中运行它以显示3个角度的三角形,其中3个边由用户给出。但是如果我运行并且给出3,4,5输出的3个边将是-1。#J -1。#J -1。#J。我的代码出了什么问题?

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

int main()
{
    float a, b, c, A, B, C, R, s, pi, area;
    pi = acos(-1);

    scanf("%f %f %f", &a, &b, &c);
    s = (a+b+c)/2;
    area = sqrt(s*(s-a)*(s-b)*(s-c));

    R = (a*b*c)/(4*area);

    A = (180/pi)*asin(a/2*R);
    B = (180/pi)*asin(b/2*R);
    C =  (180/pi)*asin(c/2*R);

    printf("%.2f %.2f %.2f", A, B, C);


    return 0;
}

4 个答案:

答案 0 :(得分:4)

您需要更多括号。你有:

A = (180/pi)*asin(a/2*R);

你需要:

A = (180/pi)*asin(a/(2*R));

你写的内容相当于:

A = (180 / pi) * asin((R * a) / 2);

例如,您还应检查输入,以便拒绝侧面为1,1,3的“三角形”。负长度和零长度也应该被拒绝。

修订代码

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

int main(void)
{
    float a, b, c, A, B, C, R, s, pi, area;
    pi = acos(-1);

    if (scanf("%f %f %f", &a, &b, &c) != 3)
    {
        fprintf(stderr, "Failed to read 3 numbers\n");
        return 1;
    }
    if (a <= 0 || b <= 0 || c <= 0)
    {
        fprintf(stderr, "Sides must be strictly positive\n");
        return 1;
    }
    s = (a + b + c) / 2;
    if (a > s || b > s || c > s)
    {
        fprintf(stderr, "The three sides %.2f, %.2f, %.2f do not form a triangle\n",
                a, b, c);
        return 1;
    }

    area = sqrt(s * (s - a) * (s - b) * (s - c));

    R = (a * b * c) / (4 * area);

    A = (180 / pi) * asin(a / (2 * R));
    B = (180 / pi) * asin(b / (2 * R));
    C = (180 / pi) * asin(c / (2 * R));

    printf("Sides:  %6.2f %6.2f %6.2f\n", a, b, c);
    printf("Angles: %6.2f %6.2f %6.2f\n", A, B, C);

    return 0;
}

请注意,标准错误会报告错误消息。所有输出行以格式换行结束。输入数据被回显。所有这些都是良好的做法。

示例运行

$ triangle <<< "0 1 3"
Sides must be strictly positive
$ triangle <<< "-1 1 3"
Sides must be strictly positive
$ triangle <<< "1 1 3"
The three sides 1.00, 1.00, 3.00 do not form a triangle
$ triangle <<< "3 4 5"
Sides:    3.00   4.00   5.00
Angles:  36.87  53.13  90.00
$ triangle <<< "3 3 3"
Sides:    3.00   3.00   3.00
Angles:  60.00  60.00  60.00
$ triangle <<< "1 1.4141 1"
Sides:    1.00   1.41   1.00
Angles:  45.00    nan  45.00
$ triangle <<< "1 1 1.4141"
Sides:    1.00   1.00   1.41
Angles:  45.00  45.00    nan
$ triangle <<< "1 1 1.414"
Sides:    1.00   1.00   1.41
Angles:  45.01  45.01  89.98
$ triangle <<< "1 1 1.41421356237309504880"
Sides:    1.00   1.00   1.41
Angles:  45.00  45.00    nan
$

我对nan值感到有些困惑。但是,将数据类型从float更改为double并相应地调整scanf()格式(%lf而不是%f)似乎可以“解决”(可能是'逃避'是一个更好的词'这个问题。

答案 1 :(得分:1)

应该是

A = (180/pi)*asin(a/(2*R));

而不是

A = (180/pi)*asin(a/2*R);

括号不足。

答案 2 :(得分:1)

其他细节:

my $sth = $dbh->column_info(undef, $schema, $Table, undef); while (my $Column = $sth && $sth->fetchrow_hashref) { my $ColumnName = $Column->{COLUMN_NAME}; my $data_type = $Column->{DATA_TYPE}; # SQL_DATA_TYPE does not work too my $type_info = $dbh->type_info($data_type); my $Unsigned = $type_info->{UNSIGNED_ATTRIBUTE}; my $UnsignedInfo = defined($Unsigned) ? ", unsigned=>$Unsigned" : ""; print $fh " '$ColumnName' => {type=>'$Column->{TYPE_NAME}', nullable=>$Column->{NULLABLE}$UnsignedInfo},\n"; } 仅针对值asin(x)定义。使用选择输入时,由于浮点计算问题,更正后的代码可能会得到一个略大于1.0的[-1.0 ... 1.0]

a/(2*R)

答案 3 :(得分:0)

即使给出了所有评论和先前的答案,我也无法使您发布的代码能够正常运行。

然而,这非常有效。

注意:我没有包含错误检查scanf()调用以保持演示文稿简单的代码

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

#define RAD_2_DEG  (57.2958)
#define TOTAL_DEG  (180.0)

int main( void )
{

    int  a, b, c;   // user entered lengths of sides of triangle
    double A, B, C; // calculated angle values in degrees

    scanf( "%d %d %d", &a, &b, &c );


    // note: acos returns the angle in radians
    //       and one radian is (approx) 57.2958 degrees
    A = RAD_2_DEG * acos((double)(b*b + c*c - a*a)/(2.0*b*c));

    B = RAD_2_DEG * acos((double)(c*c + a*a - b*b)/(2.0*a*c));

    // third angle done this way to absorb fractional degree errors
    C = TOTAL_DEG -(A + B);

    printf("%.2f %.2f %.2f", A, B, C);
    return 0;
}