uva478
题意概述:
有n(n<=10)组图形(长方形、圆形、三角形),再有有限多个点,我们需要判断点在不在各个图形中。图形和点都需要输入。图形的输入以r\c\t开始分别表示长方形、圆形、三角形。长方形的输入包括r和左上与右下两个点坐标,圆形包括c和圆心坐标与半径,三角形的输入包括t和三个点的坐标。点的输入以点(9999.9,9999.9)的输入结束。
所以这道题非常适合作为类的一个练习题。对于点可以构建一个类,包含横纵坐标,由于类的数据私有性,同时也要有相应的取值(横纵坐标)函数,由于判断点与圆的位置关系时要计算点与点的距离,故还要有一个相应的函数,最好放在点类里。对于图形,可构造一个基类,拥有一个名字(r\c\t),和一个点。再根据各类图形各自的特点加入其他数据,而对图形类保持虚继承。因为最终用来测试时,我们需要一个类(测试类),我们并不提前知道要输入什么图形。只有让测试把三者图形都继承了,输入了名字后,按照基类继承的函数接口去访问正确的函数。
样例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream>
2 #include<cmath>
3 using namespace std;
4 class point{
5 private:
6 double x;
7 double y;
8 public:
9 friend istream &operator>> (istream &input,point &t)
10 {
11 input>>t.x>>t.y;
12 return input;
13 }
14 point(double a=0.0,double b=0.0){
15 x=a;
16 y=b;
17 }
18 double getx(){
19 return x;
20 }
21 double gety(){
22 return y;
23 }
24 double distance(point &t)
25 {
26 return sqrt((x-t.x)*(x-t.x)+(y-t.y)*(y-t.y));
27 }
28 int input(){
29 cin>>x>>y;
30 if(x==9999.9&&y==9999.9)
31 return 0;
32 return 1;
33 }
34 };
35 class figure{
36 private:
37 char name;
38 point position;
39 public:
40 figure(char c='*',double x=0.0,double y=0.0)
41 :position(x,y)
42 {
43 name=c;
44 }
45 int input(){
46 cin>>name;
47 if(name=='c' || name=='r'||name=='t')
48 { cin>>position;
49 return 1; }
50 return 0;
51 }
52 char getname(){
53 return name;
54 }
55 point &getposition()
56 {
57 return position;
58 }
59 };
60 class rectangle:virtual public figure
61 {
62 private:
63 point right;
64 public:
65 void input(){
66 cin>>right;
67 }
68 int judge(point& t)
69 {
70 if(figure::getposition().getx()<t.getx() && t.getx()<right.getx()
71 &&figure::getposition().gety()>t.gety() && t.gety()>right.gety())
72 return 1;
73 return 0;
74 }
75 };
76 class circle:virtual public figure
77 {
78 private:
79 double radius;
80 public:
81 void input()
82 {
83 cin>>radius;
84 }
85 int judge(point &t)
86 {
87 if(figure::getposition().distance(t)<radius)
88 return 1;
89 return 0;
90 }
91 };
92 class triangle:virtual public figure
93 {
94 private:
95 point two;
96 point three;
97 int location;
98 public:
99 triangle(){
100 location=0;
101 }
102 void input()
103 {
104 cin>>two>>three;
105 }
106 int judge_aid(point &a,point &b,point & c,point &d)const
107 {
108 double i=(c.getx()-a.getx())*(b.gety()-a.gety())-(c.gety()-a.gety())*(b.getx()-a.getx());
109 double j=(d.getx()-a.getx())*(b.gety()-a.gety())-(d.gety()-a.gety())*(b.getx()-a.getx());
110 if( i*j>0)
111 return 1;
112 return 0;
113 }
114 int judge(point &t)
115 {
116 point temp=figure::getposition();
117 if(judge_aid(temp,two,three,t)==1
118 &&judge_aid(two,three,temp,t)==1
119 &&judge_aid(three,temp,two,t)==1)
120 return 1;
121 return 0;
122 }
123 };
124 class uva477:public rectangle,public circle,public triangle
125 {
126 private:
127 int num;
128 public:
129 uva477()
130 {
131 num=1;
132 }
133 int input(){
134 int k=figure::input();
135 if(k==0)
136 return k;
137 char c=figure::getname();
138 if(c=='c')
139 circle::input();
140 else if(c=='r')
141 { rectangle::input();
142 num=2; }
143 else
144 {
145 triangle::input();
146 num=3;
147 }
148 return 1;
149 }
150 int judge(point &t)
151 {
152 if(num==1)
153 return circle::judge(t);
154 else if(num==2)
155 return rectangle::judge(t);
156 else
157 return triangle::judge(t);
158 }
159 };
160 int main()
161 {
162 uva477 test[11];
163 point trial;
164 int i=0,count=0,sum=0,j=0;
165 for(i=0;i<11;i++)
166 if(test[i].input()==0)
167 break;
168 count=i;
169 while(trial.input()!=0)
170 {
171 sum++;
172 int flag=0;
173 for(j=0;j<count;j++)
174 if(test[j].judge(trial)==1)
175 {
176 flag=1;
177 cout<<"Point "<<sum<<" is contained in figure "<<j+1<<endl;
178 }
179 if(flag==0)
180 cout<<"Point "<<sum<<" is not contained in any figure"<<endl;
181 }
182 return 0;
183 }