2013-04-12 139 views
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); 
} 
+0

請發佈*確切的*錯誤消息,並指出哪些行導致他們。 –

回答

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語句但你聲明它返回。在返回函數的值結尾處流動的結果是undefined behavior,因此您不能依賴結果。它看起來並不像你使用的結果,所以你可能只想改變函數返回void,這將解決它。