今天是个好日子,
我的程序遇到了困难。它应该计算直角三角形的所有可能边并将它们推入向量中,例如 [3,4,5] 和 [5,12,13]。我尝试运行这个程序,但它总是列出 3、4、5。这是标题和功能。任何帮助,将不胜感激。
Header:
/*
* 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);
功能:
#include "triangles.h" // import the prototypes for our triangle class.
#include <iostream>
using std::cout;
#include <vector>
using std::vector;
#include <algorithm>
using std::sort;
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;
}