0

我在问题的输出部分遇到问题,我在右下角、左上角和尺寸的行上遇到错误。我究竟做错了什么?我已经尝试了很多东西,但我只是不知道如何让它正常工作,而且我们还没有在课堂上讨论过类似这样的输出:

#include <iostream>
#include <cmath>

using namespace std;

class Point
{
private:
double px;
double py;

public:
void setX(const double x);
void setY(const double y);
double getX() const;
double getY() const;
};

class Triangle
{
private:
Point blPoint;
double length, height;

public:
// member functions
void setBottomLeftX(const double x);
void setBottomLeftY(const double y);
void setLength(const double inLength);
void setHeight(const double inHeight);

Point getBottomLeft() const;
Point getBottomRight() const;
Point getTopLeft() const;
double getLength() const;
double getHeight() const;

double perimeter() const;
double hypotenuse() const;
void scaleLength(const double sx);
void scaleHeight(const double sy);
void display() const;
};

// FUNCTION PROTOTYPES GO HERE:
double read_triangle(Triangle & tri);

int main()
{
// Define local variables
Triangle tri;
double sx, sy;

//Prompt the user for triangle information and fill Class Triangle object, tri,
//with this information
read_triangle(tri);

// Display triangle information
tri.display();

// Prompt and read scale factors to change length and height
cout << "Enter scale factor in x direction: ";
cin >> sx;

cout << "Enter scale factor in y direction: ";
cin >> sy;

// Apply scale factors
tri.scaleLength(sx);
tri.scaleHeight(sy);

// Display triangle information
tri.display();

return 0;
}

// FUNCTION DEFINITIONS GO HERE:

// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:

void Point::setX(const double x)
{
px = x;
}

void Point::setY(const double y)
{
py = y;
}

double Point::getX() const
{
return (px);
}

double Point::getY() const
{
return (py);
}

void Triangle::setBottomLeftX(const double x)
{
/* INSERT YOUR CODE */
 blPoint.setX(x);
}

void Triangle::setBottomLeftY(const double y)
{
/* INSERT YOUR CODE */
blPoint.setY(y);
}

void Triangle::setLength(const double inLength)
{
/* INSERT YOUR CODE */
length=inLength;
}

void Triangle::setHeight(const double inHeight)
{
/* INSERT YOUR CODE */
height=inHeight;
}

Point Triangle::getBottomLeft() const
{
/* INSERT YOUR CODE */
return (blPoint);
}

Point Triangle::getBottomRight() const
{
/* INSERT YOUR CODE */
Point getBottomRight;
double mx = (blPoint.getX()+ length);
getBottomRight.setX(mx);
return(getBottomRight);
}

Point Triangle::getTopLeft() const
{
/* INSERT YOUR CODE */
Point getTopLeft;
double my = (blPoint.getY()+ height);
getTopLeft.setY(my);
return (getTopLeft);
}

double Triangle::getLength() const
{
/* INSERT YOUR CODE */
return (length);
}

double Triangle::getHeight() const
{
/* INSERT YOUR CODE */
return (height);
}

double Triangle::hypotenuse() const
{
/* INSERT YOUR CODE */
//hypotenuse = (sqrt((height * height)+(length * length)));
return (sqrt((height * height)+(length * length)));
}

double Triangle::perimeter() const
{
/* INSERT YOUR CODE */
 //perimeter = ((sqrt((height * height)+(length * length)))+ height + length);
 return ((sqrt((height * height)+(length * length)))+ height + length);
}

void Triangle::scaleLength(const double scalefact)
{
/* INSERT YOUR CODE */

 length = scalefact * length;
}

void Triangle::scaleHeight(const double scalefact)
{
/* INSERT YOUR CODE */
height = scalefact * height;
}

void Triangle::display() const
{
/* INSERT YOUR CODE */
cout <<"---------------------------------------" << endl;
cout << "Lower Left Vertex (" << blPoint.getX() << ", " << blPoint.getY() << ')' <<endl;
cout << "Top Left Vertex (" << blPoint.getX() << ", " << getTopLeft.getY() << ')' << endl;
cout << "Bottom Right Vertex (" << getBottomRight.getX() << ", " << blPoint.getY() << ')' << endl;
cout << "Dimensions (" << getBottomRight.getX()- blPoint.getX() << ", " << getTopleft.getY() - blPoint.getY() << ')' << endl;
cout << "Hypotenuse = " << hypotenuse() << endl;
cout << "Perimeter = " << perimeter() << endl;
cout <<"---------------------------------------" << endl;
}

double read_triangle(Triangle & tri)
{
/* INSERT YOUR CODE */
double x, y, inLength, inHeight;
cout << "Enter bottom left x coordinate: ";
cin >> x;
tri.setBottomLeftX(x);

cout << "Enter bottom left y coordinate: ";
cin >> y ;
tri.setBottomLeftY(y);

cout << "Enter length: ";
cin >> inLength;
tri.setLength(inLength);

cout << "Enter Height: ";
cin >> inHeight;
tri.setHeight(inHeight);
}
4

1 回答 1

0

您正在使用函数,就像它们是需要添加()才能正确调用它们的变量一样:

cout << "Top Left Vertex (" << blPoint.getX() << ", " << getTopLeft().getY() << ')' << endl;
                                                                   ^^  
cout << "Bottom Right Vertex (" << getBottomRight().getX() << ", " << blPoint.getY() << ')' << endl;
                                                 ^^
cout << "Dimensions (" << getBottomRight().getX()- blPoint.getX() << ", " << getTopLeft().getY() - blPoint.getY() << ')' << endl;
                                        ^^                                             ^^   

此外,read_triangle没有return 语句,但您声明它返回double从值返回函数的末尾流出是未定义的行为,因此您不能依赖结果。它看起来不像您正在使用结果,因此您可能只想更改要返回的函数void,这将修复它。

于 2013-04-12T03:12:09.173 回答