直角三角形程序错误

时间:2013-10-16 18:04:06

标签: c++

大家好日子,

我在使用我的程序时遇到困难。它应该计算右三角形的所有可能的边并将它们推入矢量例如[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;
}

0 个答案:

没有答案