简体   繁体   中英

Triangle Class C++

I would appreciate any help that can be provided. I'm working on a class that evaluates a triangle based on three sides input. I'm fairly lost on some of the algorithms for the analysis, though. For example, I feel like my largest , smallest , and type of triangle algorithms are correct, but the first two are returning zeros and the type is returning that equilaterals are isosceles. Finally, I'm pretty much entirely lost on the isTriangle and isRight . I know they're wrong, but I can't figure out what adjustments to make. :/

#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";



}

If S1 == s2 == s3 is true that does not imply the sides are equal.

You need to write s1 == s2 && s1 == s3

To see this, rewrite to booleans. Set b1 false and b2 not equal to b3.

B2 == b3 is false and that will compare true with b1. Oops.

Your triangle has a right angle if and only if the square of the longer side is the sum of the squares of the shorter sides.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM