三角类C ++

时间:2015-07-12 13:54:50

标签: c++ algorithm function

感谢您提供的任何帮助。我正在研究一个基于三边输入来评估三角形的类。不过,我对一些分析算法相当失败。例如,我觉得我的最大最小类型三角形算法是正确的,但前两个是返回零和类型正在回归的是等边是等腰。最后,我完全迷失在 isTriangle isRight 上。我知道他们错了,但我无法弄清楚要做出哪些调整。 :/

#include "Triangle.h"
#include<iostream>
#include<string>
using namespace std;

Triangle::Triangle()
{
    s1 = 1;
    s2 = 1;
    s3 = 1;
}

Triangle::Triangle(int x)
{
    s1 = x;
    s2 = x;
    s3 = x;
}

Triangle::Triangle(int x, int y, int z)
{
    s1 = x;
    s2 = y;
    s3 = z;
}

void Triangle::setSide1(int x)
{
    s1 = x;
}

void Triangle::setSide2(int x)
{
    s2 = x;
}

void Triangle::setSide3(int x)
{ 
    s3 = x;
}

int Triangle::getSide1()
{
    return s1;
}

int Triangle::getSide2()
{
    return s2;
}

int Triangle::getSide3()
{ 
    return s3;
}

int Triangle::perimeter()
{
    int perim = s1 + s2 + s3;

    return perim;

}

int Triangle::largestSide()
{
    int largest = 0;

    if (s1 >= s2 && s3)
        s1 = largest;
    else if (s2 >= s1 && s3)
        s2 = largest;
    else 
        s3 = largest;

    return largest;

}

int Triangle::smallestSide()
{
    int smallest = 0;

    if (s1 <= s2 && s1 <= 3)
        smallest = s1; 
    else if (s2 <= s1 && s2 <= s3)
        smallest = s2;
    else
        smallest = s3;

    return smallest;
}

bool Triangle::isRight()
{
    if (s3 == s1 + s2 || s2 == s1 + s3 || s1 == s2 + s3)

        return true;
    else
        return false;


}

bool Triangle::isTriangle()
{
    if (s1 + s2 < s3 || s1 + s3 < s2 || s2 + s3 < s1)

        return false;
    else
        return true;

}

string Triangle::typeTriangle()
{

    if (&Triangle::isTriangle == false)
        return "This is not a triangle";
    else if (s1 == s2 == s3)
        return "Equilateral";
    else if ((s1 == s2 && s1 != s3 && s2 != s3) || (s2 == s3 && s2 != s1 && s3 != s1))
    return "Isosceles";
    else if (s1 != s2 && s2 != s3)
        return "Scalene";



}

1 个答案:

答案 0 :(得分:2)

如果S1 == s2 == s3为真,则并不意味着双方是平等的。

你需要写s1 == s2&amp;&amp; s1 == s3

要看到这一点,请改写为布尔值。设置b1为假,b2设为不等于b3。

B2 == b3为假,将与b1进行比较。糟糕。

当且仅当长边的正方形是短边的平方和时,你的三角形才有直角。