题目
分析
定义Interval类
这中间定义了友元使得Rect类可以调用Interval类的私有元素,坐标因为要返回两个值 ,我采用返回数组,其中我反复修改的部分部分是交集和并集部分,因为其中取最大值,最小值,但是前提条件在a<b,而y的部分则a>b,导致一直返回有问题
class Interval
{
friend class Rect;
public:
Interval(int x1=0, int x2=0)//构造函数
:a(x1),
b(x2)
{}
Interval operator()(int x1, int x2)
{
Interval p(x1, x2);
return p;
}
int length()
{
return b - a;
}
int* coordinate()
{
int v[2];
v[0] = a;
v[1] = b;
cout << "The coordinate of the Interval is:";
cout << "(" << a << "," << b << ")" << endl;
return v;
}
Interval Intersection(Interval& i1, Interval& i2)
{
int m = max(i1.a, i2.a);
int n = min(i1.b, i2.b);
if (i1.a > i1.b && i2.a > i2.b)
{
m = min(i1.a, i2.a);
n = max(i1.b, i2.b);
}
Interval p(m, n);
cout << "The Intersection of the two Interval is:";
Print(p);
return p;
}
Interval Union(Interval& i1, Interval& i2)
{
int m = min(i1.a, i2.a);
int n = max(i1.b, i2.b);
if (i1.a > i1.b && i2.a > i2.b)
{
m = max(i1.a, i2.a);
n = min(i1.b, i2.b);
}
Interval p(m, n);
cout << "The Union of the two Interval is:";
Print(p);
return p;
}
void Print(Interval& p)
{
cout << "(" << p.a << "," << p.b << ")" << endl;
}
private:
int a;
int b;
};
定义Rect类
Rect类则是调用了Interval中的函数,逻辑上难度不大。
class Rect
{
public:
Rect(int x1, int x2, int x3, int x4)
{
horizontal.a=x1;
horizontal.b = x3;
vertical.a = x2;
vertical.b = x4;
}
Rect(const Rect& R)
{
horizontal = R.horizontal;
vertical = R.vertical;
}
int width()
{
cout << "The width of the Rect is:" << (horizontal.b - horizontal.a) << endl;
return horizontal.b - horizontal.a;
}
int height()
{
cout << "The height of the Rect is:" << (vertical.a - vertical.b) << endl;
return vertical.a - vertical.b;
}
void leftcor()
{
Interval p(horizontal.a, vertical.a);
cout << "The coordinates of the upper left corner is:";
p.Print(p);
}
void rightcor()
{
Interval p(horizontal.b, vertical.b);
cout << "The coordinates of the bottom right corner is:";
p.Print(p);
}
int get_S()
{
int S = width() * height();
cout << "The area of the Rect is:"<<S<<endl;
return S;
}
void empty()
{
if (width() == 0 || height() == 0)
cout << "The rectangle is empty." << endl;
else
cout << "The rectangle is not empty." << endl;
}
int Intersection_rectangle(Rect& R1, Rect& R2)
{
Interval m = m.Intersection(R1.horizontal, R2.horizontal);
Interval n = n.Intersection(R1.vertical, R2.vertical);
int i = (m.b - m.a) * (n.a - n.b);
cout << "The area of intersecting rectangles is:" <<i<<endl;
return i;
}
int Union_rectangle(Rect& R1, Rect& R2)
{
Interval m = m.Union(R1.horizontal, R2.horizontal);
Interval n = n.Union(R1.vertical, R2.vertical);
int i = (m.b - m.a) * (n.a - n.b);
cout << "The area of Union rectangles is:" << i << endl;
return i;
}
void Iou(Rect& R1, Rect& R2)
{
double r =double( R1.Intersection_rectangle(R1, R2))/R1.Union_rectangle(R1,R2);
cout << "The Iou of these two rectangles is:" << r * 100 << "%" << endl;
cout << "---------------------------------------------------" << endl << endl;
}
private:
Interval horizontal;
Interval vertical;
};
展示信息函数
void showRect(Rect& R)
{
R.leftcor();
R.rightcor();
/*R.width();*/
/*R.height();*/
R.get_S();
cout << "---------------------------------------------------" << endl << endl ;
}
运行结果
完整代码
#include <iostream>
#include <vector>
using namespace std;
class Interval
{
friend class Rect;
public:
Interval(int x1=0, int x2=0)//构造函数
:a(x1),
b(x2)
{}
Interval operator()(int x1, int x2)
{
Interval p(x1, x2);
return p;
}
int length()
{
return b - a;
}
int* coordinate()
{
int v[2];
v[0] = a;
v[1] = b;
cout << "The coordinate of the Interval is:";
cout << "(" << a << "," << b << ")" << endl;
return v;
}
Interval Intersection(Interval& i1, Interval& i2)
{
int m = max(i1.a, i2.a);
int n = min(i1.b, i2.b);
if (i1.a > i1.b && i2.a > i2.b)
{
m = min(i1.a, i2.a);
n = max(i1.b, i2.b);
}
Interval p(m, n);
cout << "The Intersection of the two Interval is:";
Print(p);
return p;
}
Interval Union(Interval& i1, Interval& i2)
{
int m = min(i1.a, i2.a);
int n = max(i1.b, i2.b);
if (i1.a > i1.b && i2.a > i2.b)
{
m = max(i1.a, i2.a);
n = min(i1.b, i2.b);
}
Interval p(m, n);
cout << "The Union of the two Interval is:";
Print(p);
return p;
}
void Print(Interval& p)
{
cout << "(" << p.a << "," << p.b << ")" << endl;
}
private:
int a;
int b;
};
class Rect
{
public:
Rect(int x1, int x2, int x3, int x4)
{
horizontal.a=x1;
horizontal.b = x3;
vertical.a = x2;
vertical.b = x4;
}
Rect(const Rect& R)
{
horizontal = R.horizontal;
vertical = R.vertical;
}
int width()
{
cout << "The width of the Rect is:" << (horizontal.b - horizontal.a) << endl;
return horizontal.b - horizontal.a;
}
int height()
{
cout << "The height of the Rect is:" << (vertical.a - vertical.b) << endl;
return vertical.a - vertical.b;
}
void leftcor()
{
Interval p(horizontal.a, vertical.a);
cout << "The coordinates of the upper left corner is:";
p.Print(p);
}
void rightcor()
{
Interval p(horizontal.b, vertical.b);
cout << "The coordinates of the bottom right corner is:";
p.Print(p);
}
int get_S()
{
int S = width() * height();
cout << "The area of the Rect is:"<<S<<endl;
return S;
}
void empty()
{
if (width() == 0 || height() == 0)
cout << "The rectangle is empty." << endl;
else
cout << "The rectangle is not empty." << endl;
}
int Intersection_rectangle(Rect& R1, Rect& R2)
{
Interval m = m.Intersection(R1.horizontal, R2.horizontal);
Interval n = n.Intersection(R1.vertical, R2.vertical);
int i = (m.b - m.a) * (n.a - n.b);
cout << "The area of intersecting rectangles is:" <<i<<endl;
return i;
}
int Union_rectangle(Rect& R1, Rect& R2)
{
Interval m = m.Union(R1.horizontal, R2.horizontal);
Interval n = n.Union(R1.vertical, R2.vertical);
int i = (m.b - m.a) * (n.a - n.b);
cout << "The area of Union rectangles is:" << i << endl;
return i;
}
void Iou(Rect& R1, Rect& R2)
{
double r =double( R1.Intersection_rectangle(R1, R2))/R1.Union_rectangle(R1,R2);
cout << "The Iou of these two rectangles is:" << r * 100 << "%" << endl;
cout << "---------------------------------------------------" << endl << endl;
}
private:
Interval horizontal;
Interval vertical;
};
void showRect(Rect& R)
{
R.leftcor();
R.rightcor();
/*R.width();*/
/*R.height();*/
R.get_S();
cout << "---------------------------------------------------" << endl << endl ;
}
int main()
{
Rect R1(0, 1, 1, 0);
Rect R2(0, 3, 5, 0);
Rect R3(-1, 1, 2, -2);
cout <<"Sherry" << endl;
cout << "---------------------------------------------------" << endl << endl ;
cout << "The following is information about R1:" << endl;
showRect(R1);
cout << "The following is information about R2:" << endl;
showRect(R2);
cout << "The following is information about R3:" << endl;
showRect(R3);
cout << "R1 and R2:"<<endl;
R1.Iou(R1, R2);
cout << "R2 and R3:" << endl;
R1.Iou(R2, R3);
cout << "R1 and R3:" << endl;
R1.Iou(R1, R3);
return 0;
}