我如何使用 3 个整数之间的关系来确定三角形 and/or 我可以使用什么声明来使该程序运行?
How do I use relationships btwn 3 integers to determine a triangle and/or what declaration can I use to make this program work?
我正在尝试创建一个可以使用关系来确定正在计算的三角形类型的程序。我尝试过 float 和 double 声明,但无论哪种方式,最终结果都不正确,让我发疯。基本上程序不会将 "c" 重新识别为等于 a/b 对于每个类似的值。(例如,为边 a 和 b 输入 7 7,角度为 60,应该产生等边,但不.) 关于如何解决这个问题的任何想法?太感谢了!
#include <iostream>
#include <cmath>
#define pi 3.14159265358989
using namespace std;
int main()
{
float a, b, c, d, C;
cout << "Hello. I can calculate the missing length of a triangle" << endl;
cout << "by using using the information you provide." << endl;
cout << "Please enter two positive integers between 1-99 and separate them with a space. " << endl;
cin >> a >> b;
cout << "Side 1 equals: " << a << endl;
cout << "Side 2 equals: " << b << endl;
cout << "Please enter the angle between the two sides you just entered. " << endl;
cin >> d;
C = d * (pi / 180); // convert rad to deg
//Compute missing side
c = sqrt((a*a) + (b*b) - 2 * a * b * cos(C));
cout << "The length of the third side of the triangle is: " << c << endl;
if (a == b && b == c) //all sides are equal
cout << "This is an Equilateral Triangle." << endl;
else if (a == b && b != c) // if 2 sides are equal
cout << "This is an Isosceles Triangle. " << endl;
else if (a != b && b != c) // if no sides are equal
cout << "This is a Scalene Triangle. " << endl;
system ("pause");
return 0;
}
您不能真正使用 ==
来比较 float 或 double,至少对于您的示例没有任何有意义的方式。
您必须指定 2 个 float 彼此相等的含义。
即:2 个小到足以让您认为它们相等的浮点数之间的 epsilon 差异是多少。
看到这个。
What is the most effective way for float and double comparison?
我正在尝试创建一个可以使用关系来确定正在计算的三角形类型的程序。我尝试过 float 和 double 声明,但无论哪种方式,最终结果都不正确,让我发疯。基本上程序不会将 "c" 重新识别为等于 a/b 对于每个类似的值。(例如,为边 a 和 b 输入 7 7,角度为 60,应该产生等边,但不.) 关于如何解决这个问题的任何想法?太感谢了!
#include <iostream>
#include <cmath>
#define pi 3.14159265358989
using namespace std;
int main()
{
float a, b, c, d, C;
cout << "Hello. I can calculate the missing length of a triangle" << endl;
cout << "by using using the information you provide." << endl;
cout << "Please enter two positive integers between 1-99 and separate them with a space. " << endl;
cin >> a >> b;
cout << "Side 1 equals: " << a << endl;
cout << "Side 2 equals: " << b << endl;
cout << "Please enter the angle between the two sides you just entered. " << endl;
cin >> d;
C = d * (pi / 180); // convert rad to deg
//Compute missing side
c = sqrt((a*a) + (b*b) - 2 * a * b * cos(C));
cout << "The length of the third side of the triangle is: " << c << endl;
if (a == b && b == c) //all sides are equal
cout << "This is an Equilateral Triangle." << endl;
else if (a == b && b != c) // if 2 sides are equal
cout << "This is an Isosceles Triangle. " << endl;
else if (a != b && b != c) // if no sides are equal
cout << "This is a Scalene Triangle. " << endl;
system ("pause");
return 0;
}
您不能真正使用 ==
来比较 float 或 double,至少对于您的示例没有任何有意义的方式。
您必须指定 2 个 float 彼此相等的含义。 即:2 个小到足以让您认为它们相等的浮点数之间的 epsilon 差异是多少。
看到这个。
What is the most effective way for float and double comparison?