C++ 同余、相似和直角三角形
我必须测试两个三角形是否相似、全等,以及给定的边是否是直角三角形。我包含了一个头文件。我试着运行这个程序,但总是得到错误的答案 标题:C++ 同余、相似和直角三角形,c++,C++,我必须测试两个三角形是否相似、全等,以及给定的边是否是直角三角形。我包含了一个头文件。我试着运行这个程序,但总是得到错误的答案 标题: /* * triangles.h * Header file for triangle class. */ // make sure this file is not included multiple times: #pragma once #include <vector> using std::vector; class triangl
/*
* triangles.h
* Header file for triangle class.
*/
// make sure this file is not included multiple times:
#pragma once
#include <vector>
using std::vector;
class triangle {
public:
// member functions:
// constructor:
triangle(unsigned long a=3, unsigned long b=4, unsigned long c=5):
s1(a),s2(b),s3(c) {}
unsigned long perimeter();
unsigned long area();
void print(); // prints to standard output
// member variables:
// integers for the 3 sides:
unsigned long s1;
unsigned long s2;
unsigned long s3;
};
vector<triangle> findRightTriangles(unsigned long l, unsigned long h);
bool congruent(triangle t1, triangle t2);
bool similar(triangle t1, triangle t2);
/*
*三角形
*三角形类的头文件。
*/
//确保未多次包含此文件:
#布拉格语一次
#包括
使用std::vector;
阶级三角{
公众:
//成员职能:
//建造商:
三角形(无符号长a=3,无符号长b=4,无符号长c=5):
s1(a)、s2(b)、s3(c){}
无符号长周长();
无符号长区域();
void print();//打印到标准输出
//成员变量:
//三个边的整数:
无符号长s1;
无符号长s2;
无符号长s3;
};
矢量FindRight三角形(无符号长l、无符号长h);
布尔全等(三角形t1,三角形t2);
布尔相似(三角形t1,三角形t2);
功能
#include <vector>
using std::vector;
#include <algorithm>
using std::sort;
// note the "triangle::" part. We need to specify the function's
// FULL name to avoid confusion. Else, the compiler will think we
// are just defining a new function called "perimeter"
unsigned long triangle::perimeter() {
return s1+s2+s3;
}
unsigned long triangle::area() {
// TODO: write this function.
// Note: why is it okay to return an integer here? Recall that
// all of our triangles have integer sides, and are right triangles...
// put the sides in an array:
unsigned long sides[3] = {s1,s2,s3};
// sort the array:
sort(sides,sides+3);
// at this point, sides[0] <= sides[1] <= sides[2]
unsigned long b = sides[0];
unsigned long h = sides[1];
return (b*h)/2;
}
void triangle::print() {
cout << "[" << s1 << "," << s2 << "," << s3 << "]";
}
bool congruent(triangle t1, triangle t2) {
// TODO: write this function.
int a, b, c;
a = t1.s1;
b = t1.s2;
c = t1.s3;
unsigned long tr1[3] = {a,b,c};
// sort the array:
sort(tr1,tr1+3);
// at this point, tr1[0]<= tr1[1] <= tr1[2]
int d,e,f;
d = t2.s1;
e = t2.s2;
f = t2.s3;
unsigned long tr2[3] = {d,e,f};
// sort the array:
sort(tr2,tr2+3);
// at this point, tr2[0] <= tr2[1] <= tr2[2]
if(tr1[0] == tr2[0] && tr1[1] == tr2[1] && tr1[2] == tr2[2]){
true;
}
else false;
}
bool similar(triangle t1, triangle t2) {
// TODO: write this function.
int a, b, c;
a = t1.s1;
b = t1.s2;
c = t1.s3;
unsigned long tr1[3] = {a,b,c};
// sort the array:
sort(tr1,tr1+3);
// at this point, tr1[0]<= tr1[1] <= tr1[2]
int d,e,f;
d = t2.s1;
e = t2.s2;
f = t2.s3;
unsigned long tr2[3] = {d,e,f};
// sort the array:
sort(tr2,tr2+3);
// at this point, tr2[0] <= tr2[1] <= tr2[2
if(tr1[0]%tr2[0] == 0 && tr1[1]%tr2[1] == 0 && tr1[2]%tr2[2] == 0){
true;
}
else false;
}
vector<triangle> findRightTriangles(unsigned long l, unsigned long h) {
// TODO: find all the right triangles with integer sides,
// subject to the perimeter bigger than l and less than h
vector<triangle> retval; // storage for return value.
triangle t1;
t1.s1=l;
t1.s3=h;
for (unsigned long p = 0; p < t1.s3; p++) {
t1.s2=p;
if ( p >= t1.s1 && p <= t1.s3 && (((t1.s1*t1.s1)+(p*p)) == (t1.s3*t1.s3))){
retval.push_back(t1);
break;
}
}
return retval;
}
#包括
使用std::vector;
#包括
使用std::sort;
//注意“三角形::”部分。我们需要指定函数的
//全名以避免混淆。否则,编译器会认为我们
//我们正在定义一个名为“周长”的新函数
无符号长三角形::周长(){
返回s1+s2+s3;
}
无符号长三角形::面积(){
//TODO:编写此函数。
//注意:为什么这里可以返回一个整数?回想一下
//我们所有的三角形都有整数边,都是直角三角形。。。
//将两侧排列成一列:
无符号长边[3]={s1,s2,s3};
//对数组进行排序:
排序(边,边+3);
//此时,sides[0]主要问题是您没有编写return
。
例如,函数一致
和函数相似
的结尾应如下所示:
return true;
}
else return false;
除此之外,similor
函数还有一个问题。如果三角形的边之间的比率相同,则三角形是相似的。您正在检查一个三角形是否是另一个三角形的倍数,但这不一定是真的。例如,边为(6,8,10)和(9,12,15)的三角形相似,但一个不是另一个的倍数。您应该检查tr1[i]/tr2[i]
是否对所有3个边都相同。如果使用整数除法,这将给您带来麻烦,因此您可以使用以下避免除法的条件:
if(tr1[0]*tr2[1] == tr2[0]*tr1[1] && tr1[2]*tr2[1] == tr2[2]*tr1[1])
检查tr1[0]/tr2[0]
是否与tr1[1]/tr2[1]
相同,然后检查tr1[2]/tr2[2]
是否与tr1[1]/tr2[1]
我没有检查你的其他函数。三角形有三个高度。对于直角三角形,如果底部是一条腿,则其高度是另一条腿,但对于一般三角形,则不是。但是面积公式是a=1/2 bh,而不是bh。我不会只为你调试。你需要坐下来拿一张纸,确保你的算法工作正常s、 然后编写代码。这样你就知道应该得到什么样的答案。然后,如果它不起作用,你可以回到这里说“我在期待,但这正在发生,我该如何修复它?”对于区域,你可能想使用heron的公式: