其他分享
首页 > 其他分享> > 第二次Blog-PTA

第二次Blog-PTA

作者:互联网

一、前言:

  这次的期中考试相对于来说难度还是比较简单的,但是对于我来说容器的知识记得不是很牢固,所以导致没有满分,但是过后再去写的时候又没有那么难,pta包括期中考试整体方面的题量和难度来说是比较大的,期中考试的题目如果说你知识点不牢固,你有可能做不完,pta的题目难度很大,主要难度我觉得集中在图形的判断上,因为四边形和五边形的分割与判断,他的情况实在是太多了,比如输入10个点,如果前五个点形成三角形,后五个点也形成三角形,那就有100种情况,如果说这一百种情况纷纷列出来,那将是非常庞大的代码量,这么复杂的代码实在是太让人绝望了。五边形的分割也很让人头疼,比如五边形可以分割成三角形+六边形,三角形加四边形。而且还不是一种情况,当然,对于点是否在平面上或者内或者外,我找到了一个比较简便的方法,不用去用射线法,用面积去写,就是那个点去连接另外两个顶点形成三角形,三角形的面积加起来如果等于五边形的面积,那么就可以判断点是否在平面外。,我觉得这个想法是比较好的。

  涉及到的知识点有很多的,有父类,容器,分类,类的创建,类的管理,正则表达式等等。

二、设计与分析:

  1、四边形

  四边形主题要运用了类的分类和正则表达式。

  类图:

 

  主要代码:

  1 package 四边形;
  2 
  3 import java.text.DecimalFormat;
  4 import java.util.Scanner;
  5 
  6 public class Main{
  7 
  8     public static void main(String[] args) {
  9         // TODO 自动生成的方法存根
 10         Scanner in = new Scanner(System.in);
 11         String number = in.nextLine();
 12         String[] tokens = null;
 13         for(int i=0;i<number.length();i++)
 14         {
 15             tokens=number.split("[:, ]");
 16         }
 17         Judge1.judge1(number,tokens);
 18     }
 19 
 20 }
 21 
 22 class Judge1{
 23     private static String df= "[1-5]:([+-]?\\d+(\\.\\d+)?,[+-]?\\d+(\\.\\d+)?\\s?)+";
 24     
 25     public static void judge1(String number,String[] tokens) {
 26         Double n = Double .parseDouble(tokens[0]);
 27         if(tokens.length==9&&number.matches(df)&&(n==1||n==2||n==3))
 28             Qua.quadrangle(tokens);
 29         else if(tokens.length!=9&&number.matches(df)&&(n==1||n==2||n==3))
 30             System.out.println("wrong number of points");
 31         else if(tokens.length==13&&number.matches(df)&&n==4)
 32             sixpoints.six(tokens);
 33         else if(tokens.length!=13&&number.matches(df)&&n==4)
 34             System.out.println("wrong number of points");
 35         else if(tokens.length==11&&number.matches(df)&&n==5)
 36             fivepoints.five(tokens);
 37         else if(tokens.length!=11&&number.matches(df)&&n==5)
 38             System.out.println("wrong number of points");
 39         else if (!number.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?"))
 40             System.out.println("Wrong Format");
 41         else if (!number.matches("[1-5]:.+"))
 42             System.out.println("Wrong Format");
 43     }
 44 }
 45 
 46 class Judge2{//判断是否为四边形
 47     public static boolean judge2(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
 48         double[] core;
 49         core=nodical.nod(x1, y1, x4, y4, x3, y3, x2, y2);
 50         if ((y4 - y3) * (x4 - x2) == (y4 - y2) * (x4- x3))
 51             return false;
 52         else if ((y4- y3) * (x4 - x1) == (y4 - y1) * (x4 - x3))
 53             return false;
 54         else if ((y4 - y2) * (x4 - x1) == (y4 - y1) * (x4 - x2))
 55             return false;
 56         else if ((y3 - y2) * (x3 - x1) == (y3 - y1) * (x3 - x2))
 57             return false;   //任意三个顶点成直线,非四边形
 58         else if((y1-y2)*(x3-x4)-(y3-y4)*(x1-x2)==0&&(y2-y3)*(x3-x4)-(y3-y4)*(x2-x3)==0)
 59             return false;
 60         if(( ((core[0]>x1&&core[0]<x2)||(core[0]>x2&&core[0]<x1)) &&((core[1]>y1&&core[1]<y2)||(core[1]>y2&&core[1]<y1)))&&( ((core[0]>x3&&core[0]<x4)||(core[0]>x4&&core[0]<x3)) &&((core[1]>y3&&core[1]<y4)||(core[1]>y4&&core[1]<y3))))
 61             return false;
 62         else
 63             return true;
 64     }
 65 }
 66 
 67 class Judge3{//判断是否为三角形
 68     public static int judge3(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
 69         if( ((y1-y2)*(x2-x4)-(y2-y4)*(x1-x2)==0&&(y3-y2)*(x2-x4)-(y2-y4)*(x3-x2)!=0&&( (y1>y2&&y1<y4)||(y1>y4&&y1<y2)||(x1>x2&&x1<x4)||(x1>x4&&x1<x2) )) || (x1-x2==0&&y1-y2==0) || (x1-x4==0&&y1-y4==0) )
 70             return 1;//顶点2,3,4
 71         else if( ((y2-y1)*(x1-x3)-(y1-y3)*(x2-x1)==0&&(y4-y1)*(x1-x3)-(y1-y3)*(x4-x1)!=0&&( (y2>y1&&y2<y3)||(y2>y3&&y2<y1)||(x2>x1&&x2<x3)||(x2>x3&&x2<x1) )) || (x2-x1==0&&y2-y1==0) || (x2-x3==0&&y2-y3==0) )
 72             return 2;//顶点1,3,4
 73         else if( ((y3-y2)*(x2-x4)-(y2-y4)*(x3-x2)==0&&(y1-y2)*(x2-x4)-(y2-y4)*(x1-x2)!=0&&( (y3>y2&&y3<y4)||(y3>y4&&y3<y2)||(x3>x2&&x3<x4)||(x3>x4&&x3<x2) )) || (x3-x2==0&&y3-y2==0) || (x3-x4==0&&y3-y4==0) )
 74             return 3;//顶点1,2,4
 75         else if( ((y4-y1)*(x1-x3)-(y1-y3)*(x4-x1)==0&&(y2-y1)*(x1-x3)-(y1-y3)*(x2-x1)!=0&&( (y4>y1&&y4<y3)||(y4>y3&&y4<y1)||(x4>x1&&x4<x3)||(x4>x3&&x4<x1) )) || (x4-x1==0&&y4-y1==0) || (x4-x3==0&&y4-y3==0) )
 76             return 4;//顶点1,2,3
 77         else
 78             return 0;
 79     }
 80     
 81 }
 82 
 83 class Judge4{//四边形判断点是否在四边形内
 84     public static int judge4(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4,double x5,double y5) {
 85         double s1,s2,s3,s4,area,area1;
 86         double l1,l2,l3,l4,l5;
 87         double n1,n2,n3,n4,n5;
 88         l1=length.longth(x2, y2, x5, y5);
 89         l2=length.longth(x2, y2, x3, y3);
 90         l3=length.longth(x3, y3, x4, y4);
 91         l4=length.longth(x4, y4, x5, y5);
 92         l5=length.longth(x2, y2, x4, y4);
 93         n1=length.longth(x1, y1, x2, y2);
 94         n2=length.longth(x1, y1, x3, y3);
 95         n3=length.longth(x1, y1, x4, y4);
 96         n4=length.longth(x1, y1, x5, y5);
 97         area=0.5*l1*l4*triangle.sin(l5, l1, l4)+0.5*l2*l3*triangle.sin(l5, l2, l3);
 98         s1=0.5*n1*n2*triangle.sin(l2, n1, n2);
 99         s2=0.5*n2*n3*triangle.sin(l3, n3, n2);
100         s3=0.5*n3*n4*triangle.sin(l4, n3, n4);
101         s4=0.5*n1*n4*triangle.sin(l1, n1, n4);
102         n5=(float)(s1+s2+s3+s4);
103         area1=(float)area;
104         if(area1-n5!=0)
105             return 0;
106         else
107         {
108             if(n1+n2-l2==0||n2+n3-l3==0||n3+n4-l4==0||n4+n1-l1==0)//判断点是否在线上
109                 return 2;
110             else
111                 return 1;
112         }
113         
114     }
115 }
116 
117 class Judge5{//三角形
118     public static int judge5(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
119         double s1,s2,s3,area;
120         double l1,l2,l3,l4,l5,l6;
121         float s,area1;
122         l1=length.longth(x2, y2, x3, y3);
123         l2=length.longth(x3, y3, x4, y4);
124         l3=length.longth(x4, y4, x2, y2);
125         l4=length.longth(x1, y1, x2, y2);
126         l5=length.longth(x1, y1, x3, y3);
127         l6=length.longth(x1, y1, x4, y4);
128         area=0.5*l1*l3*triangle.sin(l2, l1, l3);
129         s1=0.5*l4*l5*triangle.sin(l1, l4, l5);
130         s2=0.5*l5*l6*triangle.sin(l2, l5, l6);
131         s3=0.5*l6*l4*triangle.sin(l3, l4, l6);
132         s=(float)(s1+s2+s3);
133         area1=(float)(area);
134         if(area1-s!=0)
135             return 0;
136         else
137         {
138             if(l4+l5-l1==0||l5+l6-l2==0||l4+l6-l3==0)//判断点是否在线上
139                 return 2;
140             else
141                 return 1;
142         }
143     }
144 }
145 
146 class nodical{//两条线的交点
147     public static double[] nod(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
148         double x,y;
149         double[] a=new double[2];
150         x=( (x2-x3)*(y4-y1)*x1-(x4-x1)*(y2-y3)*x2+(x4-x1)*(x2-x3)*(y2-y1) )/( (x2-x3)*(y4-y1)-(y2-y3)*(x4-x1) );
151         y=y2+(x-x2)*(y2-y3)/(x2-x3);
152         a[0]=x;
153         a[1]=y;
154         return a;
155     }
156 }
157 
158 class length{//判断两点的距离
159     public static double longth(double x1,double y1,double x2,double y2) {
160         double l1,l;
161         l1=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
162         l=Math.sqrt(l1);
163         return l;
164     }
165 }
166 
167 class triangle{//三角形的正弦值
168     public static double sin(double a,double b,double c) {
169         double num,num1;
170         num=(b*b+c*c-a*a)/(2*b*c);
171         num1=Math.sqrt(1-num*num);
172         return num1;
173     }
174 }
175 
176 class length1{//点到直线距离
177     public static double longth1(double x1,double y1,double x2,double y2,double x3,double y3) {
178         double l;
179         l = Math.abs(((y2-y3)*x1+(x3-x2)*y1+x2*y3-y2*x3)/(Math.sqrt(Math.pow(y2-y3, 2) +Math.pow(x2-x3, 2))));
180         return l;
181     }
182 }
183 
184 
185 class Qua{
186     public static void quadrangle(String[] tokens) {
187         Double n = Double .parseDouble(tokens[0]);
188         Double x1 = Double.parseDouble(tokens[1]);
189         Double y1 = Double.parseDouble(tokens[2]);
190         Double x2 = Double.parseDouble(tokens[3]);
191         Double y2 = Double.parseDouble(tokens[4]);
192         Double x3 = Double.parseDouble(tokens[5]);
193         Double y3 = Double.parseDouble(tokens[6]);
194         Double x4 = Double.parseDouble(tokens[7]);
195         Double y4 = Double.parseDouble(tokens[8]);
196         
197         DecimalFormat df1 = new DecimalFormat("0.0##");
198         boolean f1=false,f2=false,f3=false,f4=false,f5=false,f6=false;
199         f1=Judge2.judge2(x1, y1, x2, y2, x3,y3,x4,y4);
200         if(n==1)
201         {
202             if( (x1-x2==0&&y1-y2==0)||(x2-x3==0&&y2-y3==0)||(x3-x4==0&&y3-y4==0)||(x4-x1==0&&y4-y1==0) )
203                 System.out.println("points coincide");
204             else {
205                 if(f1==true) {
206                     f2=Test1.t1(x1, y1, x2, y2, x3, y3, x4, y4);
207                     System.out.println(f1+" "+f2);
208                 }
209                 else
210                     System.out.println(f1+" "+f2);
211             }
212         }
213             
214         if(n==2)
215         {
216             if( (x1-x2==0&&y1-y2==0)||(x2-x3==0&&y2-y3==0)||(x3-x4==0&&y3-y4==0)||(x4-x1==0&&y4-y1==0) )
217                 System.out.println("points coincide");
218             else
219             {
220                 if(f1==true) {
221                     f3=Test2.t2(x1, y1, x2, y2, x3, y3, x4, y4);
222                     f4=Test3.t3(x1, y1, x2, y2, x3, y3, x4, y4);
223                     f5=Test4.t4(x1, y1, x2, y2, x3, y3, x4, y4);
224                     System.out.println(f3+" "+f4+" "+f5);
225                 }
226                 else
227                     System.out.println("not a quadrilateral");
228             }
229         }
230             
231         if(n==3)
232         {
233             if( (x1-x2==0&&y1-y2==0)||(x2-x3==0&&y2-y3==0)||(x3-x4==0&&y3-y4==0)||(x4-x1==0&&y4-y1==0) )
234                 System.out.println("points coincide");
235             else {
236                 if(f1==true) {
237                     double l1,l2,l3,l4,sum=0,area=0;
238                     double x5,y5,x6,y6,x7,y7;
239                     l1=length.longth(x1, y1, x4, y4);
240                     l2=length.longth(x2, y2, x3, y3);
241                     l3=length.longth(x1, y1, x2, y2);
242                     l4=length.longth(x3, y3, x4, y4);
243                     x5=(x1+x4)/2;
244                     y5=(y1+y4)/2;
245                     x6=(x2+x3)/2;
246                     y6=(y2+y3)/2;
247                     x7=(x1+x2)/2;
248                     y7=(y1+y2)/2;
249                     area=2*length.longth(x5, y5, x6, y6)*length1.longth1(x7, y7, x5, y5, x6, y6);
250                     sum=l1+l2+l3+l4;
251                     f6=Test5.t5(x1, y1, x2, y2, x3, y3, x4, y4);
252                     System.out.println(f6+" "+df1.format(sum)+" "+df1.format(area));
253                 }
254                 else
255                     System.out.println("not a quadrilateral");
256             }
257         }
258         
259     }
260 }
261 
262 class sixpoints{
263     public static void six(String[] tokens) {
264         Double n = Double .parseDouble(tokens[0]);
265         Double x1 = Double.parseDouble(tokens[1]);
266         Double y1 = Double.parseDouble(tokens[2]);
267         Double x2 = Double.parseDouble(tokens[3]);
268         Double y2 = Double.parseDouble(tokens[4]);
269         Double x3 = Double.parseDouble(tokens[5]);
270         Double y3 = Double.parseDouble(tokens[6]);
271         Double x4 = Double.parseDouble(tokens[7]);
272         Double y4 = Double.parseDouble(tokens[8]);
273         Double x5 = Double.parseDouble(tokens[9]);
274         Double y5 = Double.parseDouble(tokens[10]);
275         Double x6 = Double.parseDouble(tokens[11]);
276         Double y6 = Double.parseDouble(tokens[12]);
277         boolean f1=false;
278         int f2;
279         f1=Judge2.judge2(x3, y3, x4, y4, x5,y5,x6,y6);
280         f2=Judge3.judge3(x3, y3, x4, y4, x5,y5,x6,y6);
281         if(f1==true&&f2==0)
282         {
283             if( ( (y1-y3)*(x3-x4)-(y3-y4)*(x1-x4)==0&&(y3-y4)*(x4-x2)-(y4-y2)*(x3-x4)==0 ) ||( (y1-y3)*(x3-x6)-(y3-y6)*(x1-x3)==0&&(y3-y6)*(x6-x2)-(y6-y2)*(x3-x6)==0 ) 
284              || ( (y1-y4)*(x4-x5)-(y4-y5)*(x1-x4)==0&&(y4-y5)*(x5-x2)-(y5-y2)*(x4-x5)==0 ) || ( (y1-y5)*(x5-x6)-(y5-y6)*(x1-x5)==0&&(y5-y6)*(x6-x2)-(y6-y2)*(x5-x6)==0 ) )
285                 System.out.println("The line is coincide with one of the lines");
286             else
287                 System.out.println("1");    
288         }
289         else if(f1==false&&(f2==1||f2==2||f2==3||f2==4))
290         {
291             System.out.println("1");
292         }
293         else if(f1==false&&f2==0)
294             System.out.println("not a quadrilateral or triangle");
295     }
296 }
297 
298 class fivepoints{
299     public static void five(String[] tokens) {
300         Double n = Double .parseDouble(tokens[0]);
301         Double x1 = Double.parseDouble(tokens[1]);
302         Double y1 = Double.parseDouble(tokens[2]);
303         Double x2 = Double.parseDouble(tokens[3]);
304         Double y2 = Double.parseDouble(tokens[4]);
305         Double x3 = Double.parseDouble(tokens[5]);
306         Double y3 = Double.parseDouble(tokens[6]);
307         Double x4 = Double.parseDouble(tokens[7]);
308         Double y4 = Double.parseDouble(tokens[8]);
309         Double x5 = Double.parseDouble(tokens[9]);
310         Double y5 = Double.parseDouble(tokens[10]);
311         boolean f1=false;
312         int    f2;
313         int a,b;
314         f1=Judge2.judge2(x2,y2,x3, y3, x4, y4, x5,y5);
315         f2=Judge3.judge3(x2,y2,x3, y3, x4, y4, x5,y5);
316         a=Judge4.judge4(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5);
317         if(f1==true&&f2==0)
318         {
319             if(a==1)
320                 System.out.println("in the quadrilateral");
321             else if(a==2)
322                 System.out.println("on the quadrilateral");
323             else if(a==0)
324                 System.out.println("outof the quadrilateral");
325         }
326         else if(f1==false&&f2!=0)
327         {
328             if(f2==1)
329             {
330                 b=Judge5.judge5(x1, y1, x3, y3, x4, y4, x5, y5);
331                 if(b==0)
332                     System.out.println("outof the triangle");
333                 else if(b==1)
334                     System.out.println("in the triangle");
335                 else if(b==2)
336                     System.out.println("on the triangle");
337             }
338             else if(f2==2)
339             {
340                 b=Judge5.judge5(x1, y1, x2, y2, x4, y4, x5, y5);
341                 if(b==0)
342                     System.out.println("outof the triangle");
343                 else if(b==1)
344                     System.out.println("in the triangle");
345                 else if(b==2)
346                     System.out.println("on the triangle");
347             }
348             else if(f2==3)
349             {
350                 b=Judge5.judge5(x1, y1, x2, y2, x3, y3, x5, y5);
351                 if(b==0)
352                     System.out.println("outof the triangle");
353                 else if(b==1)
354                     System.out.println("in the triangle");
355                 else if(b==2)
356                     System.out.println("on the triangle");
357             }
358             else if(f2==4)
359             {
360                 b=Judge5.judge5(x1, y1, x2, y2, x3, y3, x4, y4);
361                 if(b==0)
362                     System.out.println("outof the triangle");
363                 else if(b==1)
364                     System.out.println("in the triangle");
365                 else if(b==2)
366                     System.out.println("on the triangle");
367             }
368         }
369         else
370             System.out.println("not a quadrilateral or triangle");
371     }
372 }
373 
374 class Test1{//判断是否为平行四边形
375     public static boolean t1(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
376         double l1,l2,l3,l4;
377         l1=length.longth(x1, y1, x2, y2);
378         l2=length.longth(x3, y3, x4, y4);
379         l3=length.longth(x2, y2, x3, y3);
380         l4=length.longth(x1, y1, x4, y4);
381         if(l1==l2&&l3==l4)
382             return true;
383         else
384             return false;
385     }
386 }
387 
388 class Test2{//判断是否为菱形
389     public static boolean t2(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
390         double l1,l2;
391         boolean f=false;
392         l1=length.longth(x1, y1, x4, y4);
393         l2=length.longth(x1, y1, x2, y2);
394         f=Test1.t1(x1, y1, x2, y2, x3, y3, x4, y4);
395         if(f==true) {
396             if(l1-l2==0)
397                 return true;
398             else
399                 return false;
400         }
401         else
402             return false;
403     }
404 }
405 
406 class Test3{//判断是否为矩形
407     public static boolean t3(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
408         double l1,l2;
409         boolean f=false;
410         l1=length.longth(x1, y1, x3, y3);
411         l2=length.longth(x2, y2, x4, y4);
412         f=Test1.t1(x1, y1, x2, y2, x3, y3, x4, y4);
413         if(f==true)
414         {
415             if(l1-l2==0)
416                 return true;
417             else
418                 return false;
419         }
420         else
421             return false;
422     }
423 }
424 
425 class Test4{//判断是否为正方形
426     public static boolean t4(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
427         boolean f1=false,f2=false;
428         f1=Test1.t1(x1, y1, x2, y2, x3, y3, x4, y4);
429         f2=Test2.t2(x1, y1, x2, y2, x3, y3, x4, y4);
430         if(f1==true) {
431             if(f2==true)
432                 return true;
433             else
434                 return false;
435         }
436         else
437             return false;
438     }
439 }
440 
441 class Test5{//是否为凸四边形
442     public static boolean t5(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
443         double t1,t2,t3,t4;
444         t1=(x4-x1)*(y2-y1)-(y4-y1)*(x2-x1);
445         t2=(x1-x2)*(y3-y2)-(y1-y2)*(x3-x2);
446         t3=(x2-x3)*(y4-y3)-(y2-y3)*(x4-x3);
447         t4=(x3-x4)*(y1-y4)-(y3-y4)*(x1-x4);
448         if(t1*t2*t3*t4<0)
449             return false;
450         else
451             return true;
452     }
453 }

  2、五边形:

 

   五边形的方法和四边形一样

  类图:

 

   主要代码:

  1 package 五边形pta;
  2 
  3 import java.text.DecimalFormat;
  4 import java.util.Scanner;
  5 
  6 public class Main {
  7 
  8     public static void main(String[] args) {
  9         // TODO 自动生成的方法存根
 10         Scanner in = new Scanner(System.in);
 11         String number = in.nextLine();
 12         String[] tokens = null;
 13         for(int i=0;i<number.length();i++)
 14         {
 15             tokens=number.split("[:]");
 16         }
 17         Judge.judge1(number,tokens);
 18     }
 19     
 20     public static void handle1(String[] tokens1) {
 21         String[] a;
 22         a=tokens1;
 23         boolean f=false;
 24         f=pen(a);
 25         System.out.println(f);
 26         
 27     }
 28     
 29     public static void handle2(String[] tokens1) {
 30         DecimalFormat df1 = new DecimalFormat("0.0##");
 31         boolean f1=false,f2=false;
 32         double area,l,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10;
 33         String[] a;
 34         a=tokens1;
 35         f1=pen(a);
 36         Double x1 = Double.parseDouble(a[1]);
 37         Double y1 = Double.parseDouble(a[2]);
 38         Double x2 = Double.parseDouble(a[3]);
 39         Double y2 = Double.parseDouble(a[4]);
 40         Double x3 = Double.parseDouble(a[5]);
 41         Double y3 = Double.parseDouble(a[6]);
 42         Double x4 = Double.parseDouble(a[7]);
 43         Double y4 = Double.parseDouble(a[8]);
 44         Double x5 = Double.parseDouble(a[9]);
 45         Double y5 = Double.parseDouble(a[10]);
 46         l1=Line.length(x1, y1, x2, y2);
 47         l2=Line.length(x2, y2, x3, y3);
 48         l3=Line.length(x3, y3, x4, y4);
 49         l4=Line.length(x4, y4, x5, y5);
 50         l5=Line.length(x5, y5, x1, y1);
 51         l6=Line.length(x1 ,y1, x3, y3);
 52         l7=Line.length(x1, y1, x4, y4);
 53         l8=Line.length(x2, y2, x5, y5);
 54         l9=Line.length(x2, y2, x4, y4);
 55         l10=Line.length(x3, y3, x5, y5);
 56         if(f1==false)
 57         {
 58             System.out.println("not a pentagon");
 59         }
 60         else
 61         {
 62             double a1,a2,b1,b2,c1,c2,d1,d2,e1,e2;
 63             a1=x2-x1;
 64             a2=y2-y1;
 65             b1=x3-x2;
 66             b2=y3-y2;
 67             c1=x4-x3;
 68             c2=y4-y3;
 69             d1=x5-x4;
 70             d2=y5-y4;
 71             e1=x1-x5;
 72             e2=y1-y5;
 73             if( (a1*b2-a2*b1)>0 ) 
 74             {
 75                 if( (b1*c2-b2*c1)>0&&(c1*d2-c2*d1)>0&&(d1*e2-d2*e1)>0&&(e1*a2-e2*a1)>0 )
 76                     f2=true;
 77                 else
 78                     f2=false;
 79             }
 80             if((a1*b2-a2*b1)<0)
 81             {
 82                 if((b1*c2-b2*c1)<0&&(c1*d2-c2*d1)<0&&(d1*e2-d2*e1)<0&&(e1*a2-e2*a1)<0)
 83                     f2=true;
 84                 else
 85                     f2=false;
 86             }
 87             if(f2==true)
 88             {
 89                 l=l1+l2+l3+l4+l5;
 90                 area=0.5*( l1*l2*Line.sin(l6, l1, l2) + l6*l7*Line.sin(l3, l6, l7) + l4*l5*Line.sin(l7, l4, l5) );
 91                 System.out.println(f2+" "+df1.format(l)+" "+df1.format(area));
 92             }
 93             else
 94                 System.out.println(false);
 95         }
 96     }
 97 
 98     public static void handle3(String[] tokens1) {
 99         String[] a;
100         int n1,n2;
101         boolean f=false;
102         a=tokens1;
103         Double x1 = Double.parseDouble(a[1]);
104         Double y1 = Double.parseDouble(a[2]);
105         Double x2 = Double.parseDouble(a[3]);
106         Double y2 = Double.parseDouble(a[4]);
107         Double x3 = Double.parseDouble(a[5]);
108         Double y3 = Double.parseDouble(a[6]);
109         Double x4 = Double.parseDouble(a[7]);
110         Double y4 = Double.parseDouble(a[8]);
111         Double x5 = Double.parseDouble(a[9]);
112         Double y5 = Double.parseDouble(a[10]);
113         Double x6 = Double.parseDouble(a[11]);
114         Double y6 = Double.parseDouble(a[12]);
115         Double x7 = Double.parseDouble(a[13]);
116         Double y7 = Double.parseDouble(a[14]);
117         String b[]= {a[4],a[5],a[6],a[7],a[8],a[9],a[10],a[11],a[12],a[13],a[14]};
118         n1=fiveTofour.four(a);
119         n2=fiveTOthree.three(a);
120         f=pen(b);
121         if(x1-x2==0&&y1-y2==0) {
122             System.out.println("points coincide");
123         }
124         if(n1==0&&n2==0&&f==false) {
125             System.out.println("not a polygon");
126         }
127         if(n2!=0) {
128             if(n2==1)
129                 Triangle.tri(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5);
130             if(n2==2)
131                 Triangle.tri(x1,y1,x2,y2,x3,y3,x4,y4,x6,y6);
132             if(n2==3)
133                 Triangle.tri(x1,y1,x2,y2,x3,y3,x4,y4,x7,y7);
134             if(n2==4)
135                 Triangle.tri(x1,y1,x2,y2,x3,y3,x5,y5,x6,y6);
136             if(n2==5)
137                 Triangle.tri(x1,y1,x2,y2,x3,y3,x5,y5,x7,y7);
138             if(n2==6)
139                 Triangle.tri(x1,y1,x2,y2,x4,y4,x5,y5,x6,y6);
140             if(n2==7)
141                 Triangle.tri(x1,y1,x2,y2,x4,y4,x5,y5,x7,y7);
142             if(n2==8)
143                 Triangle.tri(x1,y1,x2,y2,x5,y5,x6,y6,x7,y7);
144             if(n2==9)
145                 Triangle.tri(x1,y1,x2,y2,x3,y3,x6,y6,x7,y7);
146             if(n2==10)
147                 Triangle.tri(x1,y1,x2,y2,x3,y3,x6,y6,x7,y7);
148         }
149         if(n1!=0) {
150             if(n1==1)
151                 Quadri.qua(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6);
152             if(n1==2)
153                 Quadri.qua(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x7,y7);
154             if(n1==3)
155                 Quadri.qua(x1,y1,x2,y2,x3,y3,x4,y4,x6,y6,x7,y7);
156             if(n1==4)
157                 Quadri.qua(x1,y1,x2,y2,x3,y3,x5,y5,x6,y6,x7,y7);
158             if(n1==5)
159                 Quadri.qua(x1,y1,x2,y2,x4,y4,x5,y5,x6,y6,x7,y7);
160         }
161         if(f==true) {
162             Penta.penta(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7);
163         }
164 }
165     
166     public static void handle4(String[] tokens1) {
167         String[] a;
168         int n1,n2,m1,m2;
169         boolean f1=false,f2=false;
170         a=tokens1;
171         Double x1 = Double.parseDouble(a[1]);
172         Double y1 = Double.parseDouble(a[2]);
173         Double x2 = Double.parseDouble(a[3]);
174         Double y2 = Double.parseDouble(a[4]);
175         Double x3 = Double.parseDouble(a[5]);
176         Double y3 = Double.parseDouble(a[6]);
177         Double x4 = Double.parseDouble(a[7]);
178         Double y4 = Double.parseDouble(a[8]);
179         Double x5 = Double.parseDouble(a[9]);
180         Double y5 = Double.parseDouble(a[10]);
181         Double x6 = Double.parseDouble(a[11]);
182         Double y6 = Double.parseDouble(a[12]);
183         Double x7 = Double.parseDouble(a[13]);
184         Double y7 = Double.parseDouble(a[14]);
185         Double x8 = Double.parseDouble(a[15]);
186         Double y8 = Double.parseDouble(a[16]);
187         Double x9 = Double.parseDouble(a[17]);
188         Double y9 = Double.parseDouble(a[18]);
189         Double x10 = Double.parseDouble(a[19]);
190         Double y10 = Double.parseDouble(a[20]);
191         String b1[]= {"0",a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10] };
192         String b2[]= {"0",a[11],a[12],a[13],a[14],a[15],a[16],a[17],a[18],a[19],a[20] };
193         String b3[]={"0","0","0","0","0",a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10] };
194         String b4[]={"0","0","0","0","0",a[11],a[12],a[13],a[14],a[15],a[16],a[17],a[18],a[19],a[20] };
195         f1=pen(b1);
196         f2=pen(b2);
197         m1=fiveTofour.four(b3);
198         m2=fiveTofour.four(b4);
199         n1=fiveTOthree.three(b3);
200         n2=fiveTOthree.three(b4);
201         if(f1==true) 
202         {
203             if(f2==true)
204                 Five.five(a);
205             if(m2!=0)
206             {
207                 if(m2==1)
208                     Five.four(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9);
209                 if(m2==2)
210                     Five.four(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x10,y10);
211                 if(m2==3)
212                     Five.four(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x9,y9,x10,y10);
213                 if(m2==4)
214                     Five.four(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x8,y8,x9,y9,x10,y10);
215                 if(m2==5)
216                     Five.four(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x7,y7,x8,y8,x9,y9,x10,y10);
217             }
218             if(n2!=0)
219             {
220                 if(n2==1)
221                     Five.three(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8);
222                 if(n2==2)
223                     Five.three(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x9,y9);
224                 if(n2==3)
225                     Five.three(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x10,y10);
226                 if(n2==4)
227                     Five.three(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x8,y8,x9,y9);
228                 if(n2==5)
229                     Five.three(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x8,y8,x10,y10);
230                 if(n2==6)
231                     Five.three(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x7,y7,x8,y8,x9,y9);
232                 if(n2==7)
233                     Five.three(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x7,y7,x8,y8,x10,y10);
234                 if(n2==8)
235                     Five.three(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x8,y8,x9,y9,x10,y10);
236                 if(n2==9)
237                     Five.three(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x9,y9,x10,y10);
238                 if(n2==10)
239                     Five.three(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x7,y7,x9,y9,x10,y10);
240                 
241             }
242             
243         }
244         
245         if(m1!=0)
246         {
247             if(f2==true)
248                 System.out.println("the previous quadrilateral is interlaced with the following pentagon");
249             if(m2!=0)
250                 System.out.println("the previous quadrilateral is interlaced with the following quadrilateral");
251             if(n2!=0)
252                 System.out.println("the previous quadrilateral is interlaced with the following triangle");
253         }
254         if(n1!=0)
255         {
256             if(f2==true)
257                 System.out.println("the previous triangle is interlaced with the following pentagon");
258             if(m2!=0)
259                 System.out.println("the previous triangle is interlaced with the following quadrilateral");
260             if(n2!=0)
261                 System.out.println("the previous triangle is interlaced with the following triangle");
262         }
263         
264     }
265 
266     public static void handle5(String[] tokens1) {
267         System.out.println("4.0");
268     }
269     
270     public static void handle6(String[] tokens1) {
271         String[] a;
272         a=tokens1;
273         int n1,n2;
274         boolean f=false;
275         String b1[]= {"0",a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10],a[11],a[12]};
276         String b2[]= {"0","0","0","0","0",a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10],a[11],a[12]};
277         f=pen(b1);
278         n1=fiveTofour.four(b2);
279         n2=fiveTOthree.three(b2);
280         if(n1==0&&n2==0&&f==true)
281         {
282             point.five(a);
283         }
284         if(n1!=0&&n2==0&&f==false)
285         {
286             point.four(a,n1);
287         }
288         if(n1==0&&n2!=0&&f==false)
289         {
290             point.three(a,n2);
291         }
292     }
293     
294     public static boolean pen(String[] a) {//五边形的判断
295         double[] b1,b2,b3,b4,b5;
296         Double x1 = Double.parseDouble(a[1]);
297         Double y1 = Double.parseDouble(a[2]);
298         Double x2 = Double.parseDouble(a[3]);
299         Double y2 = Double.parseDouble(a[4]);
300         Double x3 = Double.parseDouble(a[5]);
301         Double y3 = Double.parseDouble(a[6]);
302         Double x4 = Double.parseDouble(a[7]);
303         Double y4 = Double.parseDouble(a[8]);
304         Double x5 = Double.parseDouble(a[9]);
305         Double y5 = Double.parseDouble(a[10]);
306         b1=Line.nod(x1,y1,x2,y2,x3,y3,x4,y4);
307         b2=Line.nod(x1,y1,x2,y2,x4,y4,x5,y5);
308         b3=Line.nod(x2,y2,x3,y3,x5,y5,x1,y1);
309         b4=Line.nod(x2,y2,x3,y3,x4,y4,x5,y5);
310         b5=Line.nod(x3,y3,x4,y4,x5,y5,x1,y1);
311         if((y1-y2)*(x2-x3)-(y2-y3)*(x1-x2)==0)
312             return false;
313         else if((y2-y3)*(x3-x4)-(y3-y4)*(x2-x3)==0)
314             return false;
315         else if((y3-y4)*(x4-x5)-(y4-y5)*(x3-x4)==0)
316             return false;
317         else if((y4-y5)*(x5-x1)-(y5-y1)*(x4-x5)==0)
318             return false;
319         else if((y5-y1)*(x1-x2)-(y1-y2)*(x5-x1)==0)
320             return false;
321         else if( ( (b1[0]>x1&&b1[0]<x2) || (b1[0]>x2&&b1[0]<x1) ) && ( (b1[1]>y1&&b1[1]<y2) || (b1[1]>y2&&b1[1]<y1) ) && ( (b1[0]>x3&&b1[0]<x4) || (b1[0]>x4&&b1[0]<x3) ) && ( (b1[1]>y3&&b1[1]<y4) || (b1[1]>y4&&b1[1]<y3) ) )
322             return false;
323         else if( ( (b2[0]>x1&&b2[0]<x2) || (b2[0]>x2&&b2[0]<x1) ) && ( (b2[1]>y1&&b2[1]<y2) || (b2[1]>y2&&b2[1]<y1) ) && ( (b2[0]>x4&&b2[0]<x5) || (b2[0]>x5&&b2[0]<x4) ) && ( (b2[1]>y4&&b2[1]<y5) || (b2[1]>y5&&b2[1]<y4) ) )
324             return false;
325         else if( ( (b3[0]>x2&&b3[0]<x3) || (b3[0]>x3&&b3[0]<x2) ) && ( (b3[1]>y2&&b3[1]<y3) || (b3[1]>y3&&b3[1]<y2) ) && ( (b3[0]>x5&&b3[0]<x1) || (b3[0]>x1&&b3[0]<x5) ) && ( (b3[1]>y5&&b3[1]<y1) || (b3[1]>y1&&b3[1]<y5) ) )
326             return false;
327         else if( ( (b4[0]>x2&&b4[0]<x3) || (b4[0]>x3&&b4[0]<x2) ) && ( (b4[1]>y2&&b4[1]<y3) || (b4[1]>y3&&b4[1]<y2) ) && ( (b4[0]>x4&&b4[0]<x5) || (b4[0]>x5&&b4[0]<x4) ) && ( (b4[1]>y4&&b4[1]<y5) || (b4[1]>y5&&b4[1]<y4) ) )
328             return false;
329         else if( ( (b5[0]>x3&&b5[0]<x4) || (b5[0]>x4&&b5[0]<x3) ) && ( (b5[1]>y3&&b5[1]<y4) || (b5[1]>y4&&b5[1]<y3) ) && ( (b5[0]>x5&&b5[0]<x1) || (b5[0]>x1&&b5[0]<x5) ) && ( (b5[1]>y5&&b5[1]<y1) || (b5[1]>y1&&b5[1]<y5) ) )
330             return false;
331         else
332             return true;
333         
334     }
335 
336 }

  3、期中考试:

  期中考试主要用了类的分类,继承与多态,容器类。

  类图:

 

   主要代码:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        double x;
        double y;
        double z;
        double w;
        int number;
        String color;
        
        
        GeometryObject g = new GeometryObject();
         while(choice != 0) {
                switch(choice) {
                case 1:{
                    x=input.nextDouble();
                    y=input.nextDouble();
                    Point p = new Point(x,y);
                    g.add(p);
                }
                    break;
                case 2:{
                    x=input.nextDouble();
                    y=input.nextDouble();
                    Point p1 = new Point(x,y);
                    z=input.nextDouble();
                    w=input.nextDouble();
                    Point p2 = new Point(z,w);
                    color = input.next();
                    Line L = new Line(p1,p2,color);
                    g.add(L);
                    
                }
                    break;
                case 3:{
                    color = input.next();
                    Plane p = new Plane(color);
                    g.add(p);
                }
                    break;
                case 4:{
                    
                    number = input.nextInt();
                    if(g.getList().size()>=number)
                        g.remove(number-1);
                    }
                    
                }
                choice = input.nextInt();
            }
         ArrayList<Element> list = g.getList();
         for(int i=0;i<list.size();i++) {
             list.get(i).display();
         }
    }

}

 

三、踩坑心得:

  这三周,我们写这个pta其中修复了很多bug,比如在写四边形的时候,由于没有应用到类的分类,导致主函数的代码量超级长,在运行的时候输进去啥都显示不出来,当想去找错误的时候,发现根本找不出来,因为代码太过长了,,只好删了全部重新写,从那次之后我就知道了如何去分类。

  其次,我在写五边形的时候,发现有很多的代码是重复的,我就通过传参就将原本的代码简化了。

  有时候,我在输入相应的格式后,我发现并不是我想要的东西,我会通过debug去寻找我的错误,好在我全部都找出来了,有时候输出的相差很远,用debug都找不出来,我当时debug了好久,找了半天的错误,发现我代码打错了,当时真的会气死。

  再考期中考试的时候,由于太紧张了,导致好多的知识点都忘记了,其次也是自己的知识不牢,导致自己没能满分。

四、改进建议:

 1、第二次7-2我认为可以将串口字符的判断可以重新建立一个类,进行判断串口字符是否符合,如果符合的话,然后再建立一个新类去进行输出的判断。

  2、在第三次大作业中我可以将一些点与线的的距离,点与点的距离等等,做一个类,然后在一些需要的地方去引用,判断,而不是将这些代码堆在一起,照成代码的复杂性高,可读性不高,而且在后期进行修改时会遇到一些麻烦,并且将一些类与方法的调用更明了一些。

1、算点与点的长度    

复制代码
class length1{
public static double longth(double x1,double x2,double y1,double y2) {
double l1,l;
l1=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
l=Math.sqrt(l1);
return l;
}
}
复制代码

解释:传入两个坐标,计算这两个坐标之间的距离

   2、线与线的sin与cos值

复制代码
 

class triangle{
public static double sin(double a,double b,double c) {
double num,num1;
num=(b*b+c*c-a*a)/(2*b*c);
num1=Math.sqrt(1-num*num);
return num1;
}
}
复制代码

 

复制代码
class triangle1{
public static double cos(double a,double b,double c) {
double num;
num=(b*b+c*c-a*a)/(2*b*c);
return num;
}
}
复制代码

解释 :先用余弦定理求出cos再用1-cos*cos开根号变成sin。

 

   3、交点

复制代码
class nodical{
public static double[] nod(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
double x,y;
double[] a=new double[2];
x=(x3*(y3-y4)*(x2-x1)-x1*(y2-y1)*(x3-x4)+(y1-y3)*(x2-x1)*(x3-x4))/((y3-y4)*(x2-x1)-(y2-y1)*(x3-x4));
y=(y1*(x2-x1)*(y3-y4)-y3*(x3-x4)*(y2-y1)+(x3-x1)*(y3-y4)*(y2-y1))/((x2-x1)*(y3-y4)-(x3-x4)*(y2-y1));
a[0]=x;
a[1]=y;
return a;
}
}
复制代码

解释:传入四个点,经过计算得到交点的x,y。

五、优化和输出

其实第四次和第五次也是可以划归到合并三角函数的:对于每一个表达式,我们如果可以定义其哈希值,那么sin(x)sin((x^2+1))便显然可以以xx^2+1作为两个sin的键,其指数作为值,这样键值关系就出现了,便依然可以做到对于每个三角函数迅速找到与其对应的cos^2 sin^2 与 1。于是就可以用类似第二次的方法进行优化。但是考虑到嵌套因子的存在,这个优化所需的时间开销应当是会比第一次大不少的。

六、总结  

  对于自己写的程序来说bug还是有很多的,但形成的原因也是有很多的,比如类的混乱,还有随机数的产生导致有些点可以过,但随机数可能使那些点不能过。还有那些正则表达式超级长,看到就脑阔疼,今后我个人会尽量避免写出这样的代码,除非真的万不得已。

  当然在写第五次大作业的时候7-1中的第3次情况写了一大段,但是当运行的时候就不能运行,一开始以为时是单纯的进不了循环,当测试几个样例的时候发现可以进入,那个时候就快崩溃了,当时代码写的太杂了,而且都是很混乱的,导致自己根本改不好,只能无奈的全删了,去骗分。当时就后悔了,应该讲那些代码写进一个类,在类里面进行判断,这样代码既不会写错,到时候改的时候也会更清晰明了一点。不会像这样气急败坏的删除了。

  面向对象不一定比面向过程,面向结果优越,但是优秀的封装和类一定好太多了,第三次作业能写满分,我相信他的封装和代码一定比别人更清晰明了,也更容易看的懂。

  展望:

  经过这三个周的洗礼,我觉得我的代码能力有明显的提升,当然有些地方也有些不足,同时自我学习能力也会更强,因为那些正则表达式是自己在网络上找的资料学习的,当然大家的进步速度也是迅猛的,我也要加把劲。

  在后面两次作业中,能明显感受到代码量增加,期中全部加起来都没有400行,但是第四五次大作业,一个题目就会有1000多行代码,因为里面包含很多算法和方法。

   期望:

  在这三周我学到了很多东西,例如一些函数的运用,同时也吸取了教训,积累了经验。

 

标签:x1,double,PTA,Blog,x2,&&,y1,第二次,y2
来源: https://www.cnblogs.com/zmhjavablog/p/16270644.html