首页 > 解决方案 > 如何修复此三角形程序中的 nan 错误?

问题描述

我得到以下输出:

该程序旨在帮助分析三角形。您想指定三角形的坐标 (C),还是它们的边长 (S)?输入 'C' 或 'S: s 输入 a 边的长度: 3 输入 b 边的长度: 4 输入 c 边的长度: 5 坐标为 A(0,0) , B(5,0) , C(nan,nan) 角度为 A = nan、B = nan 和 C = nan

所需的输出是:

三角形的坐标位于 A(0,0)、B(5,0) 和 C(3.2,2.4)。角度为 A = 0.643501、B = 0.927295 和 C = 1.5708

我尝试在 main 的开头声明变量,并将它们初始化为 0。我得到一个 nan 错误或我的输出的值不正确

 #include <iostream>
 #include <iomanip>
 #include <cmath>


 using namespace std;

 const double PI = 3.1415926535897932384626433832795;

 int main() {

char mode = ' ';

cout << "\n\n";
cout << setw(10) << "" << "This program is designed to aid in the analysis of triangles. " << endl;
cout << setw(10) << "" << "Would you like to specify the coordinates of a triangle (C), " << endl;
cout << setw(10) << "" << "or the lengths of their sides(S) ? Enter 'C' or 'S: " ;
while (cin >> mode)
{
    if (mode == 'C' || mode == 'c')
    {
        double Ax, Ay, Bx, By, Cx, Cy;
        cout << "\n";
        cout << setw(10) << "" << "Enter the x coordinate of point A: ";
        cin >> Ax;
        cout << setw(10) << "" << "Enter the y coordinate of point A: ";
        cin >> Ay;
        cout << setw(10) << "" << "Enter the x coordinate of point B: ";
        cin >> Bx;
        cout << setw(10) << "" << "Enter the y coordinate of point B: ";
        cin >> By;
        cout << setw(10) << "" << "Enter the x coordinate of point C: ";
        cin >> Cx;
        cout << setw(10) << "" << "Enter the y coordinate of point C: ";
        cin >> Cy;

        double a = sqrt((Cx - Bx) * (Cx - Bx) + (Cy - By) * (Cy - By));
        double b = sqrt((Ax - Cx) * (Ax - Cx) + (Ay - Cy) * (Ay - Cy));
        double c = sqrt((Ax - Bx) * (Ax - Bx) + (Ay - By) * (Ay - By));
        double A = acos(((b * b) + (c * c) - (a * a)) / 2 * b * c);
        double B = asin(b*sin(A) / a);
        double C = asin(c*sin(A) / a);

        cout << "\n" << setw(10) << "" << "The lengths of the sides are: a = " << a << ", b = " << b << " and c = " << c << endl;
        cout << setw(10) << "" << "The angles are A = " << A << ", B = " << B << " and C = " << C << endl;
    }
    else if (mode == 'S' || mode == 's')
    {
        double a = 0, b = 0, c = 0;
        cout << setw(10) << "" << "Enter the length of side a: ";
        cin >> a;
        cout << setw(10) << "" << "Enter the length of side b: ";
        cin >> b;
        cout << setw(10) << "" << "Enter the length of side c: ";
        cin >> c;
        if (a + b > c && b + c > a && a + c > b)
        {
            double A = acos(((b * b) + (c * c) - (a * a)) / 2 * b * c);
            double B = asin(b*sin(A) / a);
            double C = asin(c*sin(A) / a); 
            double Ax = 0;
            double Ay = 0;
            double Bx = c;
            double By = 0;
            double Cx = b * (cos(A) * PI / 180);
            double Cy = b * (sin(A) * PI / 180);

            cout << setw(10) << "" << "The coordinates are A(" << Ax << "," << Ay << ") , B(" << Bx << "," << By << ") , C(" << Cx << "," << Cy << ")" << endl;
            cout << setw(10) << "" << "The angles are A = " << A << ", B = " << B << " and C = " << C << endl;
        }
        else
            cout << setw(10) << "" << "These lengths don't form a triangle. Try again";
    }
    else
    {
        cout << setw(10) << "" << "Please enter 'S' or'C': ";
        cin >> mode;
    }
}

}

标签: c++trigonometrycmath

解决方案


乘法(*)和除法(/)运算符具有相同的优先级,并且关联性是从左到右的,只需将角度 A 计算更改为

   double A = acos(((b * b) + (c * c) - (a * a)) / (2 * b * c));

推荐阅读