1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class POINT
{
private: int x, y;
public:
POINT(); //set vertex to zero
void ReadData(char vertex); //read in coordinates
void Display(char vertex); //display coordinates for rectangle
friend float Distance(POINT p, POINT q); //calculate distance of 2 points
friend void DisplayPer(float AB, float AC, float CD, float BD); // calculate the perimeter
~POINT();
};
POINT::POINT()
{
x = 0;
y = 0;
}
void POINT :: ReadData(char vertex)
{
cout << "Enter the coordinates for " << vertex << endl;
cin >> x, y;
}
void POINT::Display(char vertex)
{
cout << fixed << showpoint << setprecision(2);
cout << vertex << "(" << float(x) << "," << float(y) << ")";
}
float Distance(POINT p, POINT q)
{
return sqrt(pow(p.x - q.x, 2) + pow(p.y + q.y, 2));
}
void DisplayPer(float AB, float AC, float CD, float BD)
{
cout << "The perimeter is = " << AB + AC + CD + BD << endl;
}
POINT :: ~POINT()
{
}
int main()
{
POINT A, B, C, D;
float AB, AC, CD, BD;
cout << "For rectangle ABCD: " << endl;
A.ReadData('A');
B.ReadData('B');
C.ReadData('C');
D.ReadData('D');
A.Display('A');
for (int i = 0; i < 20; i++)
{
cout << char(220);
}
B.Display('B'); cout << endl;
for (int i = 0; i < 3; i++)
{
cout << char(179) << endl;
}
C.Display('C');
for (int i = 0; i < 20; i++)
{
cout << char(220);
}
D.Display('D');
float AB = Distance(A, B);
cout << "\nAB = " << AB << endl;
float AC = Distance(A, C);
cout << "AC = " << AC << endl;
float CD = Distance(C, D);
cout << "CD = " << CD << endl;
float BD = Distance(B, D);
cout << "BD = " << BD << endl;
DisplayPer(AB, AC, CD, BD);
system("PAUSE");
return 0;
}
|