C++ 确定三角形的C++程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6486888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ program on determining triangle
提问by jason
I need help with the following code that requires me to:
我需要以下代码的帮助,这些代码要求我:
Declare 3
double
type variables, each representing one of three sides of a triangle.Prompt the user to input a value for the first side, then
Set the user's input to the variable you created representing the first side of the triangle.
Repeat the last 2 steps twice more, once for each of the remaining 2 sides of the triangle.
Use a series of nested
if
/else
statements to determine if the triangle having side-lengths as set by the user is an EQUILATERAL, ISOSCELES, or SCALENE triangle. [Note: look to the Wikipedia page on ‘triangle' for definitions of these three types of triangles.]Print the resulting triangle type to the console.
Ensure your Triangle detector works by running it 5 times as in the example above. You may use the same values as in the example.
声明 3 个
double
类型变量,每个变量代表三角形的三个边之一。提示用户为第一边输入一个值,然后
将用户的输入设置为您创建的代表三角形第一边的变量。
将最后 2 个步骤再重复两次,三角形的其余 2 边各一次。
使用一系列嵌套的
if
/else
语句来确定具有用户设置的边长的三角形是 EQUILATERAL、ISOSCELES 或 SCALENE 三角形。[注意:查看关于“三角形”的维基百科页面,了解这三种三角形的定义。]将生成的三角形类型打印到控制台。
如上例所示,运行 5 次以确保您的 Triangle 检测器正常工作。您可以使用与示例中相同的值。
I currently have:
我目前有:
//lab eleven program code on triangles
#include <iostream.h>
main()
{
//variables
float aside, bside, cside;
//enter side a
cout<<"enter the length of side a "<<endl;
cin>>aside;
//enter side b
cout<<"enter the length of side b "<<endl;
cin>>bside;
//enter side c
cout<<"enter the length of side c "<<endl;
cin>>cside;
// all sides equal
if(aside==bside && bside==cside)
cout << "Equilateral triangle\n";
// at least 2 sides equal
else if(aside==bside || aside==cside || bside==cside)
cout << "Isosceles triangle\n";
// no sides equal
else
cout << "Scalene triangle\n";
}
But I need help with the if
and else if
statements to determine the type triangle. Our professor has not covered this topic in class.
但我需要帮助if
和else if
语句来确定三角形类型。我们的教授在课堂上没有涉及这个话题。
We use the program Ch 6.3 on Windows.
我们在 Windows 上使用程序 Ch 6.3。
回答by Blindy
if(a==b && b==c) // all sides equal
cout << "Equilateral triangle\n";
else if(a==b || a==c || b==c) // at least 2 sides equal
cout << "Isosceles triangle\n";
else // no sides equal
cout << "Scalene triangle\n";
回答by Merlyn Morgan-Graham
As your professor suggested, you should look at:
正如你的教授所建议的,你应该看看:
http://en.wikipedia.org/wiki/Triangle#Types_of_triangles
http://en.wikipedia.org/wiki/Triangle#Types_of_triangles
You should also look at:
你还应该看看:
http://www.teacherschoice.com.au/maths_library/trigonometry/solve_trig_sss.htm
http://www.teacherschoice.com.au/maths_library/trigonometry/solve_trig_sss.htm
Algorithm:
算法:
Solve for all angles, a1, a2, a3 (see the article above)
If you can't find a solution:
Output "Error: Not a valid triangle"
Else:
If (a1 == a2) && (a2 == a3):
Output "EQUILATERAL" and stop
If (a1 == a2) || (a2 == a3) || (a1 == a3):
Output "ISOSCELES" and stop
Output "SCALENE" and stop
Also note: Be careful about "equality" with floating point (float
/double
) values (such as angles). If you are doing such a comparison, you should usually use this instead:
另请注意:请注意浮点 ( float
/ double
) 值(例如角度)的“相等” 。如果您正在进行这样的比较,您通常应该使用它来代替:
abs(x - y) < epsilon
Where epsilon
is a "sufficiently small value".
哪里epsilon
是“足够小的值”。
回答by Owen S.
The logic falls out neatly from the definition of these different types of triangles, which as the professor notes, is information readily obtained from Wikipedia. It just involves a simple comparison of side lengths; you don't have to go as far as angles. But I'll give you some help with the "not a triangle" condition. Don't be afraid to put on your math hat here and go wandering in, a little logic isn't a bad thing for a poli sci student to endure every now and then. :-)
从这些不同类型的三角形的定义中可以清楚地看出逻辑,正如教授所指出的,这是很容易从维基百科中获得的信息。它只涉及边长的简单比较;你不必走得那么远。但我会给你一些关于“非三角形”条件的帮助。不要害怕在这里戴上你的数学帽子并四处游荡,对于一个科学学生来说,时不时地忍受一点逻辑并不是一件坏事。:-)
For the sides to make a proper triangle, for each pair of sides (I'll call them f
and g
), they must add up to greater than the third side's length (I'll call it h
). If you're dealing with equilateral triangles, you automatically know this condition is met (why?). If you're dealing with isosceles or scalene triangles, you technically only need to check the smaller two sides against the largest side, and if it's true for them, it's true for the other two cases as well (why?). However, it may be just as convenient for you to check all three cases.
为了使边形成适当的三角形,对于每对边(我将它们称为f
和g
),它们的总和必须大于第三条边的长度(我将其称为h
)。如果您正在处理等边三角形,您会自动知道满足此条件(为什么?)。如果您正在处理等腰三角形或不等边三角形,从技术上讲,您只需要检查较小的两条边和最大的边,如果对它们来说是真的,那么其他两种情况也是如此(为什么?)。但是,检查所有三种情况对您来说可能同样方便。
Looking at why this inequality has to hold: if the sum of two sides was exactly equal to the third side's length, you'd have a "degenerate" triangle where sides f
and g
could only lay on top of h
! If they added up to less, the two sides could connect to the endpoints of h
but then would never meet at a third point! You can test this yourself by cutting lengths of string or strips of paper and trying it out.
看看为什么这个不等式必须成立:如果两条边的总和正好等于第三条边的长度,你就会有一个“退化”的三角形,其中边f
和g
只能放在h
! 如果它们加起来更少,则两侧可以连接到 的端点,h
但永远不会在第三个点相遇!您可以通过剪下一定长度的绳子或纸条并试用来自己测试。
Three other things to think about:
其他需要考虑的三件事:
- In C++,
double
andfloat
are not the same thing. One has less precision than the other. Make sure you use the one the professor asks for. - Checking to make sure the sides are non-negative is a great idea. You could probably reasonably rule out lengths of 0 as well, to eliminate the possibility of degenerate triangles that just look like line segments or points.
- When comparing floating-point numbers, you should always be careful to consider whether a strict equality is going to get you what you want. For checking the equilateral/isosceles/scalene conditions, you're fine because the user is directly entering in the floating-point numbers and you're not manipulating them, so there's no chance for you to introduce error. But when checking the "not a triangle" condition, it's relatively easy to set up a situation where adding the two sides rounds off (because of the vicissitudes of floating-point arithmetic in the CPU) to something that's very close to, but not quite exactly, the third side. In those cases, if you want to catch degenerate triangles, what you usually do is pick an "epsilon" value (some very small value relative to the numbers you're dealing with) that represents the maximum amount of roundoff you're willing to tolerate. You then check whether the sum of
f
andg
is somewhere betweenh - epsilon
andh + epsilon
– or put another way, whether the absolute valueoff + g - h
is less than or equal toepsilon
. If it is, you claim thatf + g = h
(as best as you can tell) and deal with the degenerate case.
- 在C++中,
double
和float
是不是一回事。一种比另一种精度低。确保你使用教授要求的那个。 - 检查以确保双方是非负是一个好主意。您也可以合理地排除长度为 0,以消除看起来像线段或点的退化三角形的可能性。
- 在比较浮点数时,您应该始终小心考虑严格相等是否会满足您的要求。为了检查等边/等腰/不等边线条件,您没问题,因为用户直接输入浮点数并且您没有操作它们,因此您没有机会引入错误。但是在检查“非三角形”条件时,比较容易设置这样一种情况,即将两侧相加四舍五入(因为 CPU 中浮点运算的变迁)非常接近但不完全的情况确切地说,是第三面。在这些情况下,如果你想捕捉退化三角形,你通常做的是选择一个“epsilon”值(相对于你的数字来说,一些非常小的值)重新处理)代表您愿意容忍的最大舍入量。然后你检查是否总和
f
并且g
是介于h - epsilon
和h + epsilon
-或者换一种方式,无论是绝对值的f + g - h
小于或等于epsilon
。如果是这样,你会声称f + g = h
(尽你所能)并处理退化的情况。
回答by mattygs
To complete this program you will need the following:
要完成此程序,您将需要以下内容:
Make sure input is valid. In this case, input must be greater than 0. You could catch your input using a loop like
确保输入有效。在这种情况下,输入必须大于 0。您可以使用类似的循环来捕获输入
while (invar <= 0)
{
cout<<"Enter length"<<endl;
cin>>invar;
if (invar <= 0)
{
cout<<"invalid input"<<endl;
}
}
I am not sure if this is proper c++ syntax, I haven't used it in about 8 years. you can do this for all 3 inputs. I would probably make a function to determine the triangle using 3 input variables and 1 return variable. The following is pseudo-code
我不确定这是否是正确的 C++ 语法,我已经有大约 8 年没有使用过它了。您可以对所有 3 个输入执行此操作。我可能会创建一个函数来使用 3 个输入变量和 1 个返回变量来确定三角形。以下是伪代码
if (a + b <= c) or (a + c <= b) or (b + c <= a)
{
return "you don't have a triangle."
}
else
{
if (a == b) or (a == c) or (b == c)
{
if (a == b and b == c)
{
return "equilateral"
}
return "isosceles"
}
return "scalene"
}
return -1
回答by MR SHINE AIYYAPPAN
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<math.h>
int main()
{
float Side1,Side2,Side3;
float Flag1,Flag2,Sum_of_sq1,Sum_of_sq2,Sum_of_sq3;
clrscr();
printf("Enter Three Sides Side1 Side2 Side3 :");
scanf("%f %f %f", &Side1 , &Side2 , &Side3);
Flag1=(Side1==Side2)?(Side2==Side3?1:0):((Side2==Side3)?0:-1);
if(Flag1==0)
{ printf("Triangle is Isoceles\n");
}
if (Flag1==1)
{ printf("Equilateral Triangle");
}
Sum_of_sq1=pow(Side1,2)+pow(Side2,2);
Sum_of_sq2=pow(Side1,2)+pow(Side3,2);
Sum_of_sq3=pow(Side2,2)+pow(Side3,2);
if (sqrt(Sum_of_sq1)==Side3 ||sqrt(Sum_of_sq2)==Side2 || sqrt(Sum_of_sq3)==Side1)
printf("The Triangle is Right Angled Triangle");
getch();
return(0);
}