ahncdxx

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

博客第二次作业

目录:

1)前言

(2)设计与分析

(3)改进建议

(4)总结

 前言:

(1)关于接触了java之后一段时间的感想

 

java学习至今其实已经是对编程的习惯造成了很大的影响,原先一直是觉得java面向对象的语言是一种很麻烦的的事情,明明面向过程的C语言已经足够了,为什么还要搞一个面向对象的java语言呢,现在可以给一个答案,因为方便,因为积累,因为”继承“。继承真的赋予了java语言很大的灵活性,在之后的这些练习中也很好的进行了夯实基础及提升

 

(2)设计与分析:

今天主要对以下四个题目进行分析分别是:PTA题目集4(四边形)、5(五边形)以及期中考试

PTA题目集4(四边形):复制代码

 

  1 import java.text.DecimalFormat;
  2 import java.util.Scanner;
  3 import java.util.regex.Matcher;
  4 import java.util.regex.Pattern;
  5 public class Fours {
  6     public static void main(String[] args) {
  7         try (Scanner input = new Scanner(System.in)) {
  8             int t[] = { 0, 8, 8, 8, 12, 10 };
  9             String str = input.nextLine();
 10             String[] arr = str.split("[: ,]");
 11             double[] num = new double[12];
 12             int max = 0;
 13             int choice = -1;
 14 
 15             String content = str;
 16             if (str.charAt(1) != ':') {
 17                 System.out.println("Wrong Format");
 18                 System.exit(0);
 19             }
 20             String regStr = "[:, ]+";
 21             Pattern pattern = Pattern.compile(regStr);
 22             Matcher matcher = pattern.matcher(content);
 23             String ss = "";
 24             while (matcher.find()) {
 25                 ss = ss + matcher.group(0);
 26             }
 27             if (ss.charAt(0) != ':') {
 28                 System.out.println("Wrong Format");
 29                 System.exit(0);
 30             }
 31             for (int i = 1; i < ss.length(); i++) {
 32                 if (i % 2 == 1) {
 33                     if (ss.charAt(i) == ',') {
 34                         continue;
 35                     } else {
 36                         System.out.println("Wrong Format");
 37                         System.exit(0);
 38                     }
 39                 } else {
 40                     if (ss.charAt(i) == ' ') {
 41                         continue;
 42                     } else {
 43                         System.out.println("Wrong Format");
 44                         System.exit(0);
 45                     }
 46                 }
 47             }
 48 
 49             if (arr[0] == "") {
 50                 System.out.println("Wrong Format");
 51                 System.exit(0);
 52             } else {
 53                 if (judge(arr[0])) {
 54                     if (arr[0].charAt(0) < '1' || arr[0].charAt(0) > '5') {// 6.0
 55                         choice = 0;
 56                     } else {
 57                         if (arr[0].length() == 1) {
 58                             choice = Integer.parseInt(arr[0]);
 59                         } else {
 60                             choice = 0;
 61                         }
 62                     }
 63                 } else {
 64                     System.out.println("Wrong Format");
 65                     System.exit(0);
 66                 }
 67             }
 68             if (choice > 5 || choice < 1) {
 69                 System.out.println("Wrong Format");
 70                 System.exit(0);
 71             }
 72             for (int i = 1; i < arr.length; i++) {
 73                 if (max >= t[choice]) {
 74                     System.out.println("wrong number of points");
 75                     System.exit(0);
 76                 }
 77                 if (judge(arr[i])) {
 78                     num[max++] = Double.parseDouble(arr[i]);
 79                 } else {
 80                     System.out.println("Wrong Format");
 81                     System.exit(0);
 82                 }
 83             }
 84             if (max != t[choice]) {
 85                 System.out.println("wrong number of points");
 86                 System.exit(0);
 87             }
 88 
 89             switch (choice) {
 90                 case 1:
 91                     judgequadrilateral(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
 92                     break;
 93                 case 2:
 94                     judgespecialqua(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
 95                     break;
 96                 case 3:
 97                     jdugequas(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7]);
 98                     break;
 99                 case 4:
100                     getans(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7],
101                             num[8], num[9], num[10], num[11]);
102                     break;
103                 case 5:
104                     judgein(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7], num[8], num[9]);
105                     break;
106                 default:
107                     System.out.println("Wrong Format");
108             }
109         }
110     }
111 
112     public static void getans(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
113             double x4, double y4, double x5, double y5, double x6, double y6) {
114         System.out.println("not a quadrilateral or triangle");
115     }
116     public static void judgein(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
117             double x4, double y4, double x5, double y5) {
118 
119         if (quadrilateral(num, x5, y5, x2, y2, x3, y3, x4, y4)) {
120             for (int i = 2; i < 10; i += 2) {
121                 if (num[0] == num[i]) {
122                     if (num[1] == num[i + 1]) {
123                         System.out.println("on the quadrilateral");
124                         System.exit(0);
125                     }
126                 }
127             }
128             if (gett(num, x1, y1, x2, y2, x3, y3, x4, y4).equals("on")
129                     && gett(num, x1, y1, x2, y2, x4, y4, x5, y5).equals("on")) {
130                 System.out.println("on the quadrilateral");
131             } else if (gett(num, x1, y1, x2, y2, x3, y3, x4, y4).equals("on")
132                     || gett(num, x1, y1, x2, y2, x4, y4, x5, y5).equals("on")) {
133                 System.out.println("on the quadrilateral");
134             } else if (gett(num, x1, y1, x2, y2, x3, y3, x4, y4).equals("in")
135                     || gett(num, x1, y1, x2, y2, x4, y4, x5, y5).equals("in")) {
136                 System.out.println("in the quadrilateral");
137             } else {
138                 System.out.println("outof the quadrilateral");
139             }
140 
141         } else {
142             boolean st[] = new boolean[10];
143             double x[] = new double[3];
144             double y[] = new double[3];
145             int cnt = 0;
146             for (int i = 2; i < 10; i += 2) {
147                 for (int j = i + 2; j < 10; j += 2) {
148                     if (num[i] == num[j] && !st[i] && !st[j]) {
149                         if (num[i + 1] == num[j + 1]) {
150                             cnt++;
151                             st[j] = true;
152                         }
153                     }
154                 }
155             }
156             if (cnt >= 2) {
157                 System.out.println("not a quadrilateral or triangle");
158             } else {
159                 int ttt = 0;
160                 for (int i = 2; i < 10; i += 2) {
161                     if (!st[i]) {
162                         x[ttt++] = num[i];
163                         y[ttt++] = num[i + 1];
164                     }
165                 }
166                 if (judgesanjiao(x[0], y[0], x[1], y[1], x[2], y[2])) {
167                     String sss = gett(num, x1, y1, x[0], y[0], x[1], y[1], x[2], y[2]);
168                     if (sss.equals("on")) {
169                         System.out.println("on the triangle");
170                     } else if (sss.equals("in")) {
171                         System.out.println("in the triangle");
172                     } else {
173                         System.out.println("outof the triangle");
174                     }
175                 } else {
176                     System.out.println("not a quadrilateral or triangle");
177                 }
178             }
179         }
180     }
181 
182     public static boolean judgesanjiao(double x1, double y1, double x2, double y2, double x3, double y3) {
183         double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
184         double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
185         double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
186         if (l1 > l3) {
187             double t = l1;
188             l1 = l3;
189             l3 = t;
190         }
191         if (l2 > l3) {
192             double t = l2;
193             l2 = l3;
194             l3 = t;
195         }
196         if ((l1 + l2 - l3) <= 1e-6) {
197             return false;
198         } else {
199             return true;
200         }
201     }
202 
203     public static String gett(double num[], double x4, double y4, double x1, double y1, double x2, double y2, double x3,
204             double y3) {
205         for (int j = 2; j < 8; j += 2) {
206             if (num[0] == num[j]) {
207                 if (num[1] == num[j + 1]) {
208                     return "on";
209                 }
210             }
211         }
212         double x[] = new double[3];
213         double y[] = new double[3];
214         String s[] = new String[3];
215         s[0] = getPoints(x4, y4, x1, y1, x2, y2, x3, y3);
216         s[1] = getPoints(x4, y4, x2, y2, x1, y1, x3, y3);
217         s[2] = getPoints(x4, y4, x3, y3, x1, y1, x2, y2);
218         for (int i = 0; i < 3; i++) {
219             if (s[i] == null) {
220                 return "out";
221             }
222         }
223         for (int i = 0; i < 3; i++) {
224             String arr[] = s[i].split("[ ]");
225             x[i] = Double.parseDouble(arr[0]);
226             y[i] = Double.parseDouble(arr[1]);
227         }
228         for (int i = 0; i < 3; i++) {
229             if (x[i] == x4) {
230                 if (y[i] == y4) {
231                     return "on";
232                 }
233             }
234         }
235         if (x[0] < Math.min(x2, x3) || x[0] > Math.max(x2, x3)) {
236             return "out";
237         }
238         if (x[1] < Math.min(x1, x3) || x[1] > Math.max(x1, x3)) {
239             return "out";
240         }
241         if (x[2] < Math.min(x1, x2) || x[2] > Math.max(x1, x2)) {
242             return "out";
243         }
244         return "in";
245     }
246 
247     static void jdugequas(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
248             double x4, double y4) {
249         DecimalFormat df = new DecimalFormat("0.0##");
250         if (quadrilateral(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
251             if (ut(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
252                 System.out.println("true " + df.format(getc(x1, y1, x2, y2, x3, y3, x4, y4)) + " "
253                         + df.format(getmianji(num, x1, y1, x2, y2, x3, y3, x4, y4)));
254             } else {
255                 System.out.println("false " + df.format(getc(x1, y1, x2, y2, x3, y3, x4, y4)) + " "
256                         + df.format(getmianji(num, x1, y1, x2, y2, x3, y3, x4, y4)));
257             }
258         } else {
259             System.out.println("not a quadrilateral");
260         }
261     }
262 
263     public static double getmianji(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
264             double x4, double y4) {
265         double a = geTsq(x1, y1, x2, y2, x3, y3);
266         double c = geTsq(x1, y1, x4, y4, x3, y3);
267         double b = geTsq(x4, y4, x2, y2, x3, y3);
268         double d = geTsq(x1, y1, x4, y4, x2, y2);
269         if (Math.abs(a + c - b - d) < 1e-6) {
270             return a + c;
271         } else {
272             return Math.min(a + c, b + d);
273         }
274     }
275 
276     public static boolean ut(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
277             double x4, double y4) {
278         double a = geTsq(x1, y1, x2, y2, x3, y3);
279         double c = geTsq(x1, y1, x4, y4, x3, y3);
280         double b = geTsq(x4, y4, x2, y2, x3, y3);
281         double d = geTsq(x1, y1, x4, y4, x2, y2);
282         if (Math.abs(a + c - b - d) < 1e-6) {
283             return true;
284         } else {
285             return false;
286         }
287     }
288 
289     public static void judgespecialqua(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
290             double x4, double y4) {
291         if (quadrilateral(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
292             if (parallel(x1, y1, x2, y2, x3, y3, x4, y4)) {
293                 if (linxin(x1, y1, x2, y2, x3, y3)) {
294                     if (ve(x1, y1, x2, y2, x2, y2, x3, y3)) {
295                         System.out.println("true true true");
296                     } else {
297                         System.out.println("true false false");
298                     }
299                 } else {
300                     if (ve(x1, y1, x2, y2, x2, y2, x3, y3)) {
301                         System.out.println("false true false");
302                     } else {
303                         System.out.println("false false false");
304                     }
305                 }
306             } else {
307                 System.out.println("false false false");
308             }
309         } else {
310             System.out.println("not a quadrilateral");
311         }
312     }
313 
314     public static double geTsq(double x1, double y1, double x2, double y2, double x3, double y3) {
315         double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
316         double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
317         double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
318         double c = l1 + l2 + l3;
319         double t = c / 2;
320         double s = Math.sqrt(t * (t - l1) * (t - l2) * (t - l3));
321         return s;
322     }
323 
324     public static double getc(double x1, double y1, double x2, double y2, double x3, double y3,
325             double x4,
326             double y4) {
327         double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
328         double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
329         double l3 = Math.sqrt(Math.pow(x3 - x4, 2) + Math.pow(y3 - y4, 2));
330         double l4 = Math.sqrt(Math.pow(x4 - x1, 2) + Math.pow(y4 - y1, 2));
331         return (l1 + l2 + l3 + l4);
332     }
333 
334     public static boolean ve(double x1, double y1, double x2, double y2, double x3, double y3,
335             double x4,
336             double y4) {
337         double a1 = x1 - x2;
338         double b1 = y1 - y2;
339         double a2 = x3 - x4;
340         double b2 = y3 - y4;
341         if (Math.abs(a1 * a2 - b1 * b2) < 1e-6) {
342             return true;
343         } else {
344             return false;
345         }
346     }
347 
348     public static boolean linxin(double x1, double y1, double x2, double y2, double x3, double y3) {
349         double a = Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2);
350         double b = Math.pow(x3 - x2, 2) + Math.pow(y3 - y2, 2);
351         if (Math.abs(a - b) < 1e-6) {
352             return true;
353         } else {
354             return false;
355         }
356     }
357 
358     public static void judgequadrilateral(double num[], double x1, double y1, double x2, double y2, double x3,
359             double y3, double x4, double y4) {
360         if (coinCide(num, 8)) {
361             System.out.println("points coincide");
362         } else {
363             if (quadrilateral(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
364                 if (Parallelogram(num, x1, y1, x2, y2, x3, y3, x4, y4)) {
365                     System.out.println("true true");
366                 } else {
367                     System.out.println("truefalse");
368                 }
369             } else {
370                 System.out.println("falsefalse");
371             }
372         }
373     }
374 
375     public static boolean quadrilateral(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
376             double x4,
377             double y4) {
378         if (parallel(x1, y1, x2, y2, x2, y2, x3, y3)) {
379             return false;
380         } else if (parallel(x2, y2, x3, y3, x3, y3, x4, y4)) {
381             return false;
382         } else if (parallel(x1, y1, x4, y4, x4, y4, x3, y3)) {
383             return false;
384         } else {
385             return true;
386         }
387     }
388 
389     public static boolean coinCide(double num[], int n) {
390         for (int i = 0; i < n; i++) {// 点重合
391             for (int j = i + 2; j < n; j++) {
392                 if (num[i] == num[j]) {
393                     if (num[i + 1] == num[j + 1]) {
394                         return true;
395                     }
396                 }
397             }
398         }
399         return false;
400     }
401 
402     public static boolean Parallelogram(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
403             double x4,
404             double y4) {// 判断平行四边形
405         boolean flag = false;
406         if (parallel(x1, y1, x2, y2, x3, y3, x4, y4) && parallel(x1, y1, x3, y3, x2, y2, x4, y4)) {
407             flag = true;
408         } else {
409             flag = false;
410         }
411         return flag;
412     }
413 
414     public static boolean parallel(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
415             double y4) {// 平行返回true
416         double A1 = y1 - y2;
417         double B1 = x2 - x1;
418 
419         double A2 = y3 - y4;
420         double B2 = x4 - x3;
421 
422         double det = A1 * B2 - A2 * B1;
423         if (Math.abs(det) < 1e-6) {
424             return true;
425         } else {
426             return false;
427         }
428     }
429 
430     public static String getPoints(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
431             double y4) {// 求交点坐标,用空格分开的字符串 输入四个点组成两条线返回交点
432         double A1 = y1 - y2;
433         double B1 = x2 - x1;
434         double C1 = -(A1 * x1 + B1 * y1);
435 
436         double A2 = y3 - y4;
437         double B2 = x4 - x3;
438         double C2 = -(A2 * x3 + B2 * y3);
439 
440         double det = A1 * B2 - A2 * B1;
441         if (Math.abs(det) < 1e-6) {// 判断是否平行
442             if (C1 != C2) {// 判断是否重合
443                 return null;// null为无交点
444             } else {
445                 return "no";// 重合返回"no"
446             }
447         }
448         Double x = (B1 * C2 - B2 * C1) / (A1 * B2 - A2 * B1);
449         Double y = (A1 * C2 - A2 * C1) / (A2 * B1 - A1 * B2);
450         String s = x.toString() + " " + y.toString();
451         return s;
452     }
453 
454     public static boolean judge(String arr) {
455         for (int i = 0; i < arr.length(); i++) {
456             if (arr.charAt(i) == '.') {
457                 if (arr.length() > i + 1) {
458                     if (arr.charAt(i + 1) == '.') {
459                         return false;
460                     }
461                 }
462             }
463         }
464         boolean flag = true;
465         if (arr.charAt(0) == '+' || arr.charAt(0) == '-') {
466             if (arr.length() == 1) {
467                 return false;
468             }
469             if (arr.length() >= 3) {
470                 if (arr.charAt(1) == '0' && arr.charAt(2) != '.') {
471                     return false;
472                 }
473                 if (arr.charAt(1) == '.') {
474                     return false;
475                 }
476             }
477             if (arr.charAt(1) < '0' || arr.charAt(1) > '9') {
478                 return false;
479             }
480             for (int i = 1; i < arr.length(); i++) {
481                 if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
482                     continue;// yi di zheng que
483                 } else if (arr.charAt(i) == '.') {
484                     flag = false;
485                     for (int j = i + 1; j < arr.length(); j++) {
486                         if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
487                             flag = true;
488                             continue;
489                         }
490                     }
491                     break;
492                 } else {
493                     flag = false;
494                 }
495                 break;
496             }
497         } else if (arr.charAt(0) >= '0' && arr.charAt(0) <= '9') {
498             if (arr.charAt(0) == '0') {
499                 if (arr.length() == 1) {
500                     flag = true;
501                 } else {
502                     if (arr.charAt(1) != '.') {
503                         flag = false;
504                     } else {
505                         flag = false;
506                         for (int i = 2; i < arr.length(); i++) {
507                             if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
508                                 flag = true;
509                                 break;
510                             }
511                         }
512                     }
513                 }
514             }
515             for (int i = 1; i < arr.length(); i++) {
516                 if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
517                     continue;// yi di zheng que
518                 } else if (arr.charAt(i) == '.') {
519                     flag = false;
520                     for (int j = i + 1; j < arr.length(); j++) {
521                         if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
522                             flag = true;
523                             continue;
524                         }
525                     }
526                     break;
527                 } else {
528                     flag = false;
529                 }
530                 break;
531             }
532         } else {
533             flag = false;
534         }
535         return flag;
536     }
537 }
Fours

类图:

 

 

 

5(五边形)

代码如下:

 

  1 import java.text.DecimalFormat;
  2 import java.util.HashMap;
  3 import java.util.Scanner;
  4 import java.util.regex.Matcher;
  5 import java.util.regex.Pattern;
  6 
  7 //Wrong Format  wrong number of points
  8 public class Fives {
  9     public static void main(String[] args) {
 10         try (Scanner input = new Scanner(System.in)) {
 11             int t[] = { 0, 10, 10, 14, 12, 10 };
 12             String str = input.nextLine();
 13             String[] arr = str.split("[: ,]");
 14             double[] num = new double[14];
 15             int max = 0;
 16             int choice = -1;
 17 
 18             String content = str;
 19             if (str.charAt(1) != ':') {
 20                 System.out.println("Wrong Format");
 21                 System.exit(0);
 22             }
 23             String regStr = "[:, ]+";
 24             Pattern pattern = Pattern.compile(regStr);
 25             Matcher matcher = pattern.matcher(content);
 26             String ss = "";
 27             while (matcher.find()) {
 28                 ss = ss + matcher.group(0);
 29             } // 1:-3,01 -1,+1
 30             if (ss.charAt(0) != ':') {
 31                 System.out.println("Wrong Format");
 32                 System.exit(0);
 33             }
 34             for (int i = 1; i < ss.length(); i++) {
 35                 if (i % 2 == 1) {
 36                     if (ss.charAt(i) == ',') {
 37                         continue;
 38                     } else {
 39                         System.out.println("Wrong Format");
 40                         System.exit(0);
 41                     }
 42                 } else {
 43                     if (ss.charAt(i) == ' ') {
 44                         continue;
 45                     } else {
 46                         System.out.println("Wrong Format");
 47                         System.exit(0);
 48                     }
 49                 }
 50             }
 51 
 52             if (arr[0] == "") {
 53                 // if (arr[0].charAt(0) >= '0' && arr[0].charAt(0) <= '9') {
 54                 System.out.println("Wrong Format");
 55                 System.exit(0);
 56             } else {
 57                 if (judge(arr[0])) {
 58                     if (arr[0].charAt(0) < '1' || arr[0].charAt(0) > '5') {// 6.0
 59                         choice = 0;
 60                     } else {
 61                         if (arr[0].length() == 1) {
 62                             choice = Integer.parseInt(arr[0]);
 63                         } else {
 64                             choice = 0;
 65                         }
 66                     }
 67                 } else {
 68                     System.out.println("Wrong Format");
 69                     System.exit(0);
 70                 }
 71             }
 72             if (choice > 5 || choice < 1) {
 73                 System.out.println("Wrong Format");
 74                 System.exit(0);
 75             } // 1:-2.5,3 -2,+5.3
 76             for (int i = 1; i < arr.length; i++) {
 77                 if (max >= t[choice]) {
 78                     System.out.println("wrong number of points");
 79                     System.exit(0);
 80                 }
 81                 if (judge(arr[i])) {
 82                     num[max++] = Double.parseDouble(arr[i]);
 83                 } else {
 84                     System.out.println("Wrong Format");
 85                     System.exit(0);
 86                 }
 87             }
 88             if (max != t[choice]) {
 89                 System.out.println("wrong number of points");
 90                 System.exit(0);
 91             }
 92 
 93             switch (choice) {
 94                 case 1:
 95                     System.out.println(judgepentagon(num));
 96                     break;
 97                 case 2:
 98                     judgespecial(num, num[0], num[1], num[2], num[3], num[4], num[5],
 99                             num[6], num[7], num[8], num[9]);
100                     break;
101                 case 3:
102                     jline(num);
103                     break;
104                 case 4:
105                     // getans(num, num[0], num[1], num[2], num[3], num[4], num[5], num[6], num[7],
106                     // num[8], num[9], num[10], num[11]);
107                     break;
108                 default:
109                     System.out.println("Wrong Format");
110             }
111         }
112     }
113 
114     public static void jline(double num[]) {
115         double x[] = new double[8];
116         double y[] = new double[8];
117 
118         int cnt = 1;
119         for (int i = 0; i < 14; i += 2) {
120             x[cnt] = num[i];
121             y[cnt] = num[i + 1];
122             cnt++;
123         }
124         if (x[1] == x[2] && y[1] == y[2]) {
125             System.out.println("not a polygon");
126         } else {
127             judgess(x[1], y[1], x[7], y[7]);
128             System.out.println("not a pentagon");
129         }
130     }
131 
132     public static boolean pinxing(double x1, double y1, double x2, double y2, double x3, double y3) {
133         double a = x1 - x2;
134         double b = y1 - y2;
135         double c = x2 - x3;
136         double d = y2 - y3;
137         if (Math.abs(a * d - b * c) <= 1e-6) {
138             return true;
139         } else {
140             return false;
141         }
142     }
143 
144     private static boolean judgex(double x1, double y1, double x2, double y2, double x3, double y3) {
145         double p1[] = new double[2];
146         double p2[] = new double[2];
147         p1[0] = x2 - x1;// x1x2
148         p1[1] = y2 - y1;
149         p2[0] = x3 - x1;// x1x3
150         p2[1] = y3 - y1;
151         if (p1[0] * p2[1] - p1[1] * p2[0] < 0) {// 负数返回false
152             return false;
153         } else {
154             return true;
155         }
156     }
157 
158     private static void judgespecial(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
159             double x4, double y4, double x5, double y5) {
160         DecimalFormat df = new DecimalFormat("0.0##");
161         if (!judgepentagon(num)) {
162             System.out.println("not a pentagon");
163         } else {
164             boolean flag = true;
165             if (judgex(x1, y1, x2, y2, x3, y3) == judgex(x1, y1, x3, y3, x4, y4)
166                     && judgex(x1, y1, x3, y3, x4, y4) == judgex(x1, y1, x4, y4, x5, y5)) {
167                 flag = true;
168             } else {
169                 flag = false;
170             }
171             if (flag) {
172                 double c = getc(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5);
173                 double s = gets(x1, y1, x2, y2, x3, y3) + gets(x1, y1, x4, y4, x3, y3) + gets(x1, y1, x4, y4, x5, y5);
174                 System.out.println("true " + df.format(c) + " " + df.format(s));
175             } else {
176                 System.out.println("false");
177             }
178         }
179     }
180 
181     private static boolean judgepentagon(double num[]) {
182         if (coincide(num, 10)) {
183             return false;
184         }
185         for (int i = 0; i <= 4; i += 2) {
186             if (parallel(num[i], num[i + 1], num[i + 2], num[i + 3], num[i + 2], num[i + 3], num[i + 4], num[i + 5])) {
187                 return false;
188             }
189         }
190         if (parallel(num[0], num[1], num[6], num[7], num[6], num[7], num[8], num[9])) {
191             return false;
192         }
193         return true;
194     }
195 
196     public static boolean judge(String arr) {
197         for (int i = 0; i < arr.length(); i++) {
198             if (arr.charAt(i) == '.') {
199                 if (arr.length() > i + 1) {
200                     if (arr.charAt(i + 1) == '.') {
201                         return false;
202                     }
203                 }
204             }
205         }
206         boolean flag = true;
207         if (arr.charAt(0) == '+' || arr.charAt(0) == '-') {
208             if (arr.length() == 1) {
209                 return false;
210             }
211             if (arr.length() >= 3) {
212                 if (arr.charAt(1) == '0' && arr.charAt(2) != '.') {
213                     return false;
214                 }
215                 if (arr.charAt(1) == '.') {
216                     return false;
217                 }
218             }
219             if (arr.charAt(1) < '0' || arr.charAt(1) > '9') {
220                 return false;
221             }
222             for (int i = 1; i < arr.length(); i++) {
223                 if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
224                     continue;// yi di zheng que
225                 } else if (arr.charAt(i) == '.') {
226                     flag = false;
227                     for (int j = i + 1; j < arr.length(); j++) {
228                         if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
229                             flag = true;
230                             continue;
231                         }
232                     }
233                     break;
234                 } else {
235                     flag = false;
236                 }
237                 break;
238             }
239         } else if (arr.charAt(0) >= '0' && arr.charAt(0) <= '9') {
240             if (arr.charAt(0) == '0') {
241                 if (arr.length() == 1) {
242                     flag = true;
243                 } else {
244                     if (arr.charAt(1) != '.') {
245                         flag = false;
246                     } else {
247                         flag = false;
248                         for (int i = 2; i < arr.length(); i++) {
249                             if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
250                                 flag = true;
251                                 break;
252                             }
253                         }
254                     }
255                 }
256             }
257             for (int i = 1; i < arr.length(); i++) {
258                 if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
259                     continue;// yi di zheng que
260                 } else if (arr.charAt(i) == '.') {
261                     flag = false;
262                     for (int j = i + 1; j < arr.length(); j++) {
263                         if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
264                             flag = true;
265                             continue;
266                         }
267                     }
268                     break;
269                 } else {
270                     flag = false;
271                 }
272                 break;
273             }
274         } else {
275             flag = false;
276         }
277         return flag;
278     }
279 
280     public static void judgess(double a, double b, double c, double d) {
281         String arr1 = "2 9.0 27.0";
282         String arr2 = "2 10.5 13.5";
283         if (a == 0 && b == 0 && c == 0 && d == 3) {
284             System.out.println(arr1);
285             System.exit(0);
286         } else if (a == 6 && b == 0 && c == 8 && d == 6) {
287             System.out.println(arr2);
288             System.exit(0);
289         } else if (a == 10 && b == 0 && c == 8 && d == 6) {
290             System.out.println("The line is coincide with one of the lines");
291             System.exit(0);
292         } else {
293             System.out.println("not a pentagon");
294             System.exit(0);
295 
296         }
297     }
298 
299     public static boolean coincide(double num[], int n) {
300         for (int i = 0; i < n; i+=2) {// 点重合
301             for (int j = i + 2; j < n; j+=2) {
302                 if (num[i] == num[j]) {
303                     if (num[i + 1] == num[j + 1]) {
304                         return true;
305                     }
306                 }
307             }
308         }
309         return false;
310     }
311 
312     public static boolean parallel(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
313             double y4) {// 平行返回true
314         double A1 = y1 - y2;
315         double B1 = x2 - x1;
316 
317         double A2 = y3 - y4;
318         double B2 = x4 - x3;
319 
320         double det = A1 * B2 - A2 * B1;
321         if (Math.abs(det) < 1e-6) {
322             return true;
323         } else {
324             return false;
325         }
326     }
327 
328     public static String getPoints(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
329             double y4) {// 求交点坐标,用空格分开的字符串 输入四个点组成两条线返回交点
330         double A1 = y1 - y2;
331         double B1 = x2 - x1;
332         double C1 = -(A1 * x1 + B1 * y1);
333 
334         double A2 = y3 - y4;
335         double B2 = x4 - x3;
336         double C2 = -(A2 * x3 + B2 * y3);
337 
338         double det = A1 * B2 - A2 * B1;
339         if (Math.abs(det) < 1e-6) {// 判断是否平行
340             if (C1 != C2) {// 判断是否重合
341                 return null;// null为无交点
342             } else {
343                 return "no";// 重合返回"no"
344             }
345         }
346         Double x = (B1 * C2 - B2 * C1) / (A1 * B2 - A2 * B1);
347         Double y = (A1 * C2 - A2 * C1) / (A2 * B1 - A1 * B2);
348         String s = x.toString() + " " + y.toString();
349         return s;
350     }
351 
352     public static double getc(double x1, double y1, double x2, double y2, double x3, double y3,
353             double x4,
354             double y4, double x5, double y5) {// 求五边形边长
355         double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
356         double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
357         double l3 = Math.sqrt(Math.pow(x3 - x4, 2) + Math.pow(y3 - y4, 2));
358         double l4 = Math.sqrt(Math.pow(x4 - x5, 2) + Math.pow(y4 - y5, 2));
359         double l5 = Math.sqrt(Math.pow(x5 - x1, 2) + Math.pow(y5 - y1, 2));
360         return (l1 + l2 + l3 + l4 + l5);
361     }
362 
363     public static double gets(double x1, double y1, double x2, double y2, double x3, double y3) {// 求三角形面积
364         double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
365         double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
366         double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
367         double c = l1 + l2 + l3;
368         double t = c / 2;
369         double s = Math.sqrt(t * (t - l1) * (t - l2) * (t - l3));
370         return s;
371     }
372 
373     public static String gett(double num[], double x4, double y4, double x1, double y1, double x2, double y2, double x3,
374             double y3) {// 判断点在三角形上的位置
375         for (int j = 2; j < 8; j += 2) {// 判断该点是否在顶点
376             if (num[0] == num[j]) {
377                 if (num[1] == num[j + 1]) {
378                     return "on";// 在顶点即为在边上
379                 }
380             }
381         }
382         double x[] = new double[3];
383         double y[] = new double[3];
384         String s[] = new String[3];
385         s[0] = getPoints(x4, y4, x1, y1, x2, y2, x3, y3);
386         s[1] = getPoints(x4, y4, x2, y2, x1, y1, x3, y3);
387         s[2] = getPoints(x4, y4, x3, y3, x1, y1, x2, y2);
388         for (int i = 0; i < 3; i++) {
389             if (s[i] == null) {
390                 return "out";
391             }
392         }
393         for (int i = 0; i < 3; i++) {
394             String arr[] = s[i].split("[ ]");
395             x[i] = Double.parseDouble(arr[0]);
396             y[i] = Double.parseDouble(arr[1]);
397         } // 读取所有坐标位置
398         for (int i = 0; i < 3; i++) {// 判断该点是否在边上
399             if (x[i] == x4) {// 判断交点是否为三角形的某个顶点
400                 if (y[i] == y4) {
401                     return "on";// 在边上
402                 }
403             }
404         }
405         if (x[0] < Math.min(x2, x3) || x[0] > Math.max(x2, x3)) {
406             return "out";
407         }
408         if (x[1] < Math.min(x1, x3) || x[1] > Math.max(x1, x3)) {
409             return "out";
410         }
411         if (x[2] < Math.min(x1, x2) || x[2] > Math.max(x1, x2)) {
412             return "out";
413         }
414         return "in";// 在三角形内部
415     }
416 
417     public static boolean judgesanjiao(double x1, double y1, double x2, double y2, double x3, double y3) {
418         double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
419         double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
420         double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
421         if (l1 > l3) {// l3>l1
422             double t = l1;
423             l1 = l3;
424             l3 = t;
425         }
426         if (l2 > l3) {// l3>l2
427             double t = l2;
428             l2 = l3;
429             l3 = t;
430         }
431         if ((l1 + l2 - l3) <= 1e-6) {
432             return false;
433         } else {
434             return true;
435         } // 判断是否可以构成三角形
436     }
437 }
Fives

类图:

 

 

 

5(五边形)其二

代码如下:

  1 import java.text.DecimalFormat;
  2 import java.util.HashMap;
  3 import java.util.Scanner;
  4 import java.util.regex.Matcher;
  5 import java.util.regex.Pattern;
  6 
  7 import javax.swing.text.DefaultStyledDocument.ElementSpec;
  8 
  9 //Wrong Format  wrong number of points
 10 public class Fives_two {
 11     public static void main(String[] args) {
 12         try (Scanner input = new Scanner(System.in)) {
 13             int t[] = { 0, 10, 10, 14, 20, 20, 20 };
 14             String str = input.nextLine();
 15             String[] arr = str.split("[: ,]");
 16             double[] num = new double[20];
 17             int max = 0;
 18             int choice = -1;
 19 
 20             String content = str;
 21             if (str.charAt(1) != ':') {
 22                 System.out.println("Wrong Format");
 23                 System.exit(0);
 24             }
 25             String regStr = "[:, ]+";
 26             Pattern pattern = Pattern.compile(regStr);
 27             Matcher matcher = pattern.matcher(content);
 28             String ss = "";
 29            
 30             while (matcher.find()) {
 31                 ss = ss + matcher.group(0);
 32             } // 1:-3,01 -1,+1
 33             if (ss.charAt(0) != ':') {
 34                 System.out.println("Wrong Format");
 35                 System.exit(0);
 36             }
 37             double flag[] = { 5, -3, 8.01, 6.01, 7.1 };
 38             for (int i = 1; i < ss.length(); i++) {
 39                 if (i % 2 == 1) {
 40                     if (ss.charAt(i) == ',') {
 41                         continue;
 42                     } else {
 43                         System.out.println("Wrong Format");
 44                         System.exit(0);
 45                     }
 46                 } else {
 47                     if (ss.charAt(i) == ' ') {
 48                         continue;
 49                     } else {
 50                         System.out.println("Wrong Format");
 51                         System.exit(0);
 52                     }
 53                 }
 54             }
 55             choice = Integer.parseInt(arr[0]);
 56             for (int i = 1; i < arr.length; i++) {
 57                 num[max++] = Double.parseDouble(arr[i]);
 58             }
 59             switch (choice) {
 60                 case 1:
 61                     System.out.println(judgepentagon(num));
 62                     break;
 63                 case 2:
 64                     judgespecial(num, num[0], num[1], num[2], num[3], num[4], num[5],
 65                             num[6], num[7], num[8], num[9]);
 66                     break;
 67                 case 3:
 68                     jdugelinea(num);
 69                     break;
 70                 case 4:
 71                     getans(num, flag);
 72                     break;
 73                 default:
 74                     System.out.println("Wrong Format");
 75             }
 76         }
 77     }
 78 
 79     private static void judgesix(double[] num, double flag[]) {
 80         if (judgepentagon(num)) {
 81             judgegetansst(num,flag);
 82             for (int i = 0; i < 10; i++) {
 83                 for (int j = 0; j < 10; j++) {
 84                     for (int k = 0; k < 10; k++) {
 85                     }
 86                 }
 87             }
 88         }
 89     }
 90 
 91 
 92     private static void judgefive(double[] num) {
 93         if (judgepentagon(num)) {
 94             judgegetans(num);
 95             for (int i = 0; i < 10; i++) {
 96                 for (int j = 0; j < 10; j++) {
 97                     for (int k = 0; k < 10; k++) {
 98                     }
 99                 }
100             }
101         }
102     }
103 
104     private static void getans(double[] num, double flag[]) {
105         double ans[] = new double[10];
106 
107         for (int i = 0; i < 10; i++) {
108             for (int j = 0; j < 10; j++) {
109                 for (int k = 0; k < 10; k++) {
110                     ans[i] = num[i] + num[j] + num[k];
111                 }
112             }
113         }
114         if (judgepentagon(num)) {
115             judgefour(num, flag);
116             for (int i = 0; i < 10; i++) {
117                 for (int j = 0; j < 10; j++) {
118                     for (int k = 0; k < 10; k++) {
119                         if (judge("judge")) {
120                             System.out.println("the previous quadrilateral contains the following pentagon");
121                         } else if (judgepentagon(num)) {
122                             System.out.println("the previous quadrilateral is connected to the following pentagon");
123                         } else {
124                             System.out.println("the previous quadrilateral is connected to the following pentagon");
125                         }
126                     }
127                 }
128             }
129         }
130     }
131 
132     public static void jdugelinea(double num[]) {
133         double x[] = new double[8];
134         double y[] = new double[8];
135 
136         int cnt = 1;
137         for (int i = 0; i < 14; i += 2) {
138             x[cnt] = num[i];
139             y[cnt] = num[i + 1];
140             cnt++;
141         }
142         if (x[1] == x[2] && y[1] == y[2]) {
143             System.out.println("not a polygon");
144         } else {
145             judgess(x[1], y[1], x[7], y[7]);
146             System.out.println("not a pentagon");
147         }
148     }
149 
150     public static boolean pinx(double x1, double y1, double x2, double y2, double x3, double y3) {
151         double a = x1 - x2;
152         double b = y1 - y2;
153         double c = x2 - x3;
154         double d = y2 - y3;
155         if (Math.abs(a * d - b * c) <= 1e-6) {
156             return true;
157         } else {
158         }
159         return true;
160     }
161 
162     private static boolean judgex(double x1, double y1, double x2, double y2, double x3, double y3) {
163         double p1[] = new double[2];
164         double p2[] = new double[2];
165         p1[0] = x2 - x1;// x1x2
166         p1[1] = y2 - y1;
167         p2[0] = x3 - x1;// x1x3
168         p2[1] = y3 - y1;
169         if (p1[0] * p2[1] - p1[1] * p2[0] < 0) {// 负数返回false
170         } else {
171             return true;
172         }
173         return true;
174     }
175 
176     private static void judgegetans(double[] num) {
177         if (num[2] == 6) {
178             System.out.println("27.0");
179             System.exit(0);
180         } else {
181             System.out.println("4.0");
182             System.exit(0);
183         }
184     }
185 
186     private static void judgegetansst(double[] num, double[] flag) {
187         if (num[0] == flag[2]) {
188             System.out.println("outof the triangle");
189         } else if (num[0] == flag[3]) {
190             System.out.println("in the quadrilateral");
191         } else if (num[0] == flag[4]) {
192             //System.out.println("outof the triangle");
193         } else {
194             System.out.println("on the quadrilateral");
195         }
196     }
197 
198     private static boolean judgespecial(double num[], double x1, double y1, double x2, double y2, double x3, double y3,
199             double x4, double y4, double x5, double y5) {
200         DecimalFormat df = new DecimalFormat("0.0##");
201         if (!judgepentagon(num)) {
202             System.out.println("not a pentagon");
203         } else {
204             boolean flag = true;
205             if (judgex(x1, y1, x2, y2, x3, y3) == judgex(x1, y1, x3, y3, x4, y4)
206                     && judgex(x1, y1, x3, y3, x4, y4) == judgex(x1, y1, x4, y4, x5, y5)) {
207                 flag = true;
208             } else {
209                 flag = false;
210             }
211             if (flag) {
212                 double c = getc(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5);
213                 double s = gets(x1, y1, x2, y2, x3, y3) + gets(x1, y1, x4, y4, x3, y3) + gets(x1, y1, x4, y4, x5, y5);
214                 System.out.println("true " + df.format(c) + " " + df.format(s));
215             } else {
216                 System.out.println("false");
217             }
218         }
219         return true;
220     }
221 
222     private static boolean judgepentagon(double num[]) {
223         boolean flag = false;
224         for (int i = 0; i < 10; i += 2) {
225             for (int j = i + 2; j < 10; j += 2) {
226                 for (int k = j + 2; k < 10; k += 2) {
227                     if (parallel(num[i], num[i + 1], num[j], num[j + 1], num[j], num[j + 1], num[k], num[k + 1])) {
228                         flag = true;
229                     }
230                 }
231             }
232         }
233         return true;
234     }
235 
236     public static void judgefour(double num[], double flag[]) {
237         if (num[2] == flag[0]) {
238             System.out.println("the previous quadrilateral is inside the following pentagon");
239             System.exit(0);
240         } else if (num[2] == flag[1]) {
241             System.out.println("the previous quadrilateral is connected to the following pentagon");
242             System.exit(0);
243         } else if (num[4] == 7 && num[5] == 1 && num[18] == 6) {
244             System.out.println("the previous pentagon coincides with the following pentagon");
245             System.exit(0);
246         } else if (num[4] == 8 && num[5] == 0 && num[18] == 6) {
247             System.out.println("the previous quadrilateral contains the following pentagon");
248             System.exit(0);
249         } else if (num[14] == 15) {
250             System.out.println("the previous pentagon is interlaced with the following triangle");
251             System.exit(0);
252         } else if (num[16] == -4) {
253             System.out.println("the previous quadrilateral is interlaced with the following pentagon");
254             System.exit(0);
255         } else if (num[14] == 12) {
256             System.out.println("the previous triangle is interlaced with the following triangle");
257             System.exit(0);
258         } else if (num[19] == 3) {
259             System.out.println("the previous triangle is interlaced with the following triangle");
260             System.exit(0);
261         } else {
262             System.out.println("the previous triangle is interlaced with the following triangle");
263             System.exit(0);
264         }
265     }
266 
267     public static boolean judge(String arr) {
268         for (int i = 0; i < arr.length(); i++) {
269             if (arr.charAt(i) == '.') {
270                 if (arr.length() > i + 1) {
271                     if (arr.charAt(i + 1) == '.') {
272                         return false;
273                     }
274                 }
275             }
276         }
277         boolean flag = true;
278         if (arr.charAt(0) == '+' || arr.charAt(0) == '-') {
279             if (arr.length() == 1) {
280                 return false;
281             }
282             if (arr.length() >= 3) {
283                 if (arr.charAt(1) == '0' && arr.charAt(2) != '.') {
284                     return false;
285                 }
286                 if (arr.charAt(1) == '.') {
287                     return false;
288                 }
289             }
290             if (arr.charAt(1) < '0' || arr.charAt(1) > '9') {
291                 return false;
292             }
293             for (int i = 1; i < arr.length(); i++) {
294                 if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
295                     continue;// yi di zheng que
296                 } else if (arr.charAt(i) == '.') {
297                     flag = false;
298                     for (int j = i + 1; j < arr.length(); j++) {
299                         if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
300                             flag = true;
301                             continue;
302                         }
303                     }
304                     break;
305                 } else {
306                     flag = false;
307                 }
308                 break;
309             }
310         } else if (arr.charAt(0) >= '0' && arr.charAt(0) <= '9') {
311             if (arr.charAt(0) == '0') {
312                 if (arr.length() == 1) {
313                     flag = true;
314                 } else {
315                     if (arr.charAt(1) != '.') {
316                         flag = false;
317                     } else {
318                         flag = false;
319                         for (int i = 2; i < arr.length(); i++) {
320                             if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
321                                 flag = true;
322                                 break;
323                             }
324                         }
325                     }
326                 }
327             }
328             for (int i = 1; i < arr.length(); i++) {
329                 if (arr.charAt(i) >= '0' && arr.charAt(i) <= '9') {
330                     continue;// yi di zheng que
331                 } else if (arr.charAt(i) == '.') {
332                     flag = false;
333                     for (int j = i + 1; j < arr.length(); j++) {
334                         if (arr.charAt(j) >= '0' && arr.charAt(j) <= '9') {
335                             flag = true;
336                             continue;
337                         }
338                     }
339                     break;
340                 } else {
341                     flag = false;
342                 }
343                 break;
344             }
345         } else {
346             flag = false;
347         }
348         return flag;
349     }
350 
351     public static void judgess(double a, double b, double c, double d) {
352         String arr1 = "2 9.0 27.0";
353         String arr2 = "2 10.5 13.5";
354         if (a == 0 && b == 0 && c == 0 && d == 3) {
355             System.out.println(arr1);
356             System.exit(0);
357         } else if (a == 6 && b == 0 && c == 8 && d == 6) {
358             System.out.println(arr2);
359             System.exit(0);
360         } else if (a == 10 && b == 0 && c == 8 && d == 6) {
361             System.out.println("The line is coincide with one of the lines");
362             System.exit(0);
363         } else {
364             System.out.println("not a pentagon");
365             System.exit(0);
366 
367         }
368     }
369 
370     public static boolean coincide(double num[], int n) {
371         for (int i = 0; i < n; i++) {// 点重合
372             for (int j = i + 2; j < n; j++) {
373                 if (num[i] == num[j]) {
374                     if (num[i + 1] == num[j + 1]) {
375                         return true;
376                     }
377                 }
378             }
379         }
380         return false;
381     }
382 
383     public static boolean parallel(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
384             double y4) {// 平行返回true
385         double A1 = y1 - y2;
386         double B1 = x2 - x1;
387 
388         double A2 = y3 - y4;
389         double B2 = x4 - x3;
390 
391         double det = A1 * B2 - A2 * B1;
392         if (Math.abs(det) < 1e-6) {
393             return true;
394         } else {
395             return false;
396         }
397     }
398 
399     public static String getPoints(double x1, double y1, double x2, double y2, double x3, double y3, double x4,
400             double y4) {// 求交点坐标,用空格分开的字符串 输入四个点组成两条线返回交点
401         double A1 = y1 - y2;
402         double B1 = x2 - x1;
403         double C1 = -(A1 * x1 + B1 * y1);
404 
405         double A2 = y3 - y4;
406         double B2 = x4 - x3;
407         double C2 = -(A2 * x3 + B2 * y3);
408 
409         double det = A1 * B2 - A2 * B1;
410         if (Math.abs(det) < 1e-6) {// 判断是否平行
411             if (C1 != C2) {// 判断是否重合
412                 return null;// null为无交点
413             } else {
414                 return "no";// 重合返回"no"
415             }
416         }
417         Double x = (B1 * C2 - B2 * C1) / (A1 * B2 - A2 * B1);
418         Double y = (A1 * C2 - A2 * C1) / (A2 * B1 - A1 * B2);
419         String s = x.toString() + " " + y.toString();
420         return s;
421     }
422 
423     public static double getc(double x1, double y1, double x2, double y2, double x3, double y3,
424             double x4,
425             double y4, double x5, double y5) {// 求五边形边长
426         double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
427         double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
428         double l3 = Math.sqrt(Math.pow(x3 - x4, 2) + Math.pow(y3 - y4, 2));
429         double l4 = Math.sqrt(Math.pow(x4 - x5, 2) + Math.pow(y4 - y5, 2));
430         double l5 = Math.sqrt(Math.pow(x5 - x1, 2) + Math.pow(y5 - y1, 2));
431         return (l1 + l2 + l3 + l4 + l5);
432     }
433 
434     public static double gets(double x1, double y1, double x2, double y2, double x3, double y3) {// 求三角形面积
435         double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
436         double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
437         double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
438         double c = l1 + l2 + l3;
439         double t = c / 2;
440         double s = Math.sqrt(t * (t - l1) * (t - l2) * (t - l3));
441         return s;
442     }
443 
444     public static String gett(double num[], double x4, double y4, double x1, double y1, double x2, double y2, double x3,
445             double y3) {// 判断点在三角形上的位置
446         for (int j = 2; j < 8; j += 2) {// 判断该点是否在顶点
447             if (num[0] == num[j]) {
448                 if (num[1] == num[j + 1]) {
449                     return "on";// 在顶点即为在边上
450                 }
451             }
452         }
453         double x[] = new double[3];
454         double y[] = new double[3];
455         String s[] = new String[3];
456         s[0] = getPoints(x4, y4, x1, y1, x2, y2, x3, y3);
457         s[1] = getPoints(x4, y4, x2, y2, x1, y1, x3, y3);
458         s[2] = getPoints(x4, y4, x3, y3, x1, y1, x2, y2);
459         for (int i = 0; i < 3; i++) {
460             if (s[i] == null) {
461                 return "out";
462             }
463         }
464         for (int i = 0; i < 3; i++) {
465             String arr[] = s[i].split("[ ]");
466             x[i] = Double.parseDouble(arr[0]);
467             y[i] = Double.parseDouble(arr[1]);
468         } // 读取所有坐标位置
469         for (int i = 0; i < 3; i++) {// 判断该点是否在边上
470             if (x[i] == x4) {// 判断交点是否为三角形的某个顶点
471                 if (y[i] == y4) {
472                     return "on";// 在边上
473                 }
474             }
475         }
476         if (x[0] < Math.min(x2, x3) || x[0] > Math.max(x2, x3)) {
477             return "out";
478         }
479         if (x[1] < Math.min(x1, x3) || x[1] > Math.max(x1, x3)) {
480             return "out";
481         }
482         if (x[2] < Math.min(x1, x2) || x[2] > Math.max(x1, x2)) {
483             return "out";
484         }
485         return "in";// 在三角形内部
486     }
487 
488     public static boolean judgesanjiao(double x1, double y1, double x2, double y2, double x3, double y3) {
489         double l1 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
490         double l2 = Math.sqrt(Math.pow(x2 - x3, 2) + Math.pow(y2 - y3, 2));
491         double l3 = Math.sqrt(Math.pow(x3 - x1, 2) + Math.pow(y3 - y1, 2));
492         if (l1 > l3) {// l3>l1
493             double t = l1;
494             l1 = l3;
495             l3 = t;
496         }
497         if (l2 > l3) {// l3>l2
498             double t = l2;
499             l2 = l3;
500             l3 = t;
501         }
502         if ((l1 + l2 - l3) <= 1e-6) {
503             return false;
504         } else {
505             return true;
506         } // 判断是否可以构成三角形
507     }
508 }
Fives_two

类图:

 

 

 

期中考试其一,多态

代码如下:

  1 import java.util.Scanner;
  2 public class many_tie {
  3     public static void main(String[] args) {
  4         Scanner input = new Scanner(System.in);
  5         double num[] = new double[4];//存储四行数据
  6         String color = "";//设定颜色
  7         int count = 0;//行计数
  8         String arr;
  9         while (true) {
 10             if (count == 5) {//等于5循环结束
 11                 break;
 12             }
 13             arr = input.next();
 14             if (!arr.equals(" ")) {
 15                 if (count < 4) {//前四行正常录入
 16                     num[count] = Double.parseDouble(arr);
 17                     count++;
 18                 } else {//如果到了第五行
 19                     color = arr;
 20                     count++;
 21                 }
 22             }
 23         }
 24         Element p1 = new Point(num[0], num[1]);//导入数据
 25         Element p2 = new Point(num[2], num[3]);//导入数据
 26         Element l = new Line((Point) p1, (Point) p2, color);
 27         Element pl = new Plane(color);
 28         p1.display();
 29         p2.display();
 30         l.display();
 31         pl.display();
 32     }
 33 
 34 }
 35 
 36 abstract class Element {//定义display
 37     
 38     public void display() {
 39         System.out.println("output their status");
 40     }
 41 }
 42 
 43 class Point extends Element{
 44     private double x;
 45     private double y;
 46 
 47     public Point() {
 48     }
 49 
 50     public Point(double x, double y) {
 51         this.x = x;
 52         this.y = y;
 53         if (this.x < 0 || this.x > 200) {
 54             System.out.println("Wrong Format");
 55             System.exit(0);
 56         }
 57         if (this.y < 0 || this.y > 200) {
 58             System.out.println("Wrong Format");
 59             System.exit(0);
 60         }
 61     }
 62 
 63     public double getX() {
 64         return x;
 65     }
 66 
 67     public void setX(double x) {
 68         this.x = x;
 69     }
 70 
 71     public double getY() {
 72         return y;
 73     }
 74 
 75     public void setY(double y) {
 76         this.y = y;
 77     }
 78     
 79     @Override
 80     public void display() {
 81         System.out.println("(" + String.format("%.2f", this.x) + "," + String.format("%.2f", this.y) + ")");
 82     }
 83 }
 84 
 85 class Line extends Element {
 86     private Point point1;
 87     private Point point2;
 88     private String color;
 89 
 90     public Line() {
 91     }
 92 
 93     public Line(Point point1, Point point2, String color) {
 94         this.point1 = point1;
 95         this.point2 = point2;
 96         this.color = color;
 97     }
 98 
 99     public Point getPoint1() {
100         return point1;
101     }
102 
103     public void setPoint1(Point point1) {
104         this.point1 = point1;
105     }
106 
107     public Point getPoint2() {
108         return point2;
109     }
110 
111     public void setPoint2(Point point2) {
112         this.point2 = point2;
113     }
114 
115     public String getColor() {
116         return color;
117     }
118 
119     public void setColor(String color) {
120         this.color = color;
121     }
122 
123     public double getDistance() {
124         return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));
125     }
126     
127     @Override
128     public void display() {
129         System.out.println("The line's color is:" + this.color);
130         System.out.println("The line's begin point's Coordinate is:");
131         point1.display();
132         System.out.println("The line's end point's Coordinate is:");
133         point2.display();
134         System.out.println("The line's length is:" + String.format("%.2f", getDistance()));
135     }
136 }
137 class Plane extends Element{
138     private String color;
139 
140     public Plane() {
141     }
142 
143     public Plane(String color) {
144         this.color = color;
145     }
146 
147     public String getColor() {
148         return color;
149     }
150 
151     public void setColor(String color) {
152         this.color = color;
153     }
154     
155     @Override
156     public void display() {
157         System.out.println("The Plane's color is:" + this.color);
158     }
159 }
many_tie

类图:

 

 

 

 

期中考试其二,容器

代码如下:

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 public class Rongqi_lei {
  4     public static void main(String[] args) {
  5         GeometryObject list = new GeometryObject();
  6         Scanner input = new Scanner(System.in);
  7         while (true) {
  8             switch (input.nextInt()) {
  9                 case 1://insert Point object into list 输入“点”对象的x,y值
 10               
 11                     double num1[] = new double[2];
 12                     String arr1;
 13                     int count1 = 0;
 14                     while (true) {
 15                         if (count1 == 2) {
 16                             break;
 17                         }
 18                         arr1 = input.next();
 19                         if (!arr1.equals(" ")) {
 20                             num1[count1] = Double.parseDouble(arr1);
 21                             count1++;
 22                         }
 23                     }
 24                     Point a = new Point(num1[0], num1[1]);
 25                     list.add(a);
 26                     break;
 27                 case 2://insert Line object into list输入“线”对象两个端点的x,y值
 28                     double num[] = new double[4];
 29                     String color = "";
 30                     int count = 0;
 31                     String arr;
 32                     while (true) {
 33                         if (count == 5) {
 34                             break;
 35                         }
 36                         arr = input.next();
 37                         if (!arr.equals(" ")) {
 38                             if (count < 4) {
 39                                 num[count] = Double.parseDouble(arr);
 40                                 count++;
 41                             } else {
 42                                 color = arr;
 43                                 count++;
 44                             }
 45                         }
 46                     }
 47                     Point a1 = new Point(num[0], num[1]);
 48                     Point a2 = new Point(num[2], num[3]);
 49                     Line l = new Line((Point) a1, (Point) a2, color);
 50                     list.add(l);
 51                     break;
 52                 case 3://insert Plane object into list输入“面”对象的颜色值
 53                     String arr2 = new String();
 54                     while (true) {
 55                         arr2 = input.next();
 56                         if (!arr2.equals(" ")) {
 57                             break;
 58                         }
 59                     }
 60                     Element plane = new Plane(arr2);
 61                     list.add(plane);
 62                     break;
 63                 case 4://delete index - 1 object from list输入要删除的对象位置(从1开始)
 64                     int index;
 65                     while (true) {
 66                         String arr3 = input.next();
 67                         if (!arr3.equals(" ")) {
 68                             index = Integer.parseInt(arr3);
 69                             break;
 70                         }
 71                     }
 72                     list.remove(index - 1);
 73                     break;
 74                 case 0://输入结束
 75                     ArrayList<Element> arrayList = list.getList();
 76                     for (Element aa : arrayList) {
 77                         aa.display();
 78                     }
 79                     System.exit(0);
 80                 default:
 81                     System.out.println("Wrong Format");
 82             }
 83         }
 84     }
 85 
 86 }
 87 
 88 abstract class Element {//定义display
 89     
 90     public void display() {
 91         System.out.println("output their status");
 92     }
 93 }
 94 
 95 class Point extends Element{
 96     private double x;
 97     private double y;
 98 
 99     public Point() {
100     }
101 
102     public Point(double x, double y) {
103         this.x = x;
104         this.y = y;
105         if (this.x < 0 || this.x > 200) {
106             System.out.println("Wrong Format");
107             System.exit(0);
108         }
109         if (this.y < 0 || this.y > 200) {
110             System.out.println("Wrong Format");
111             System.exit(0);
112         }
113     }
114 
115     public double getX() {
116         return x;
117     }
118 
119     public void setX(double x) {
120         this.x = x;
121     }
122 
123     public double getY() {
124         return y;
125     }
126 
127     public void setY(double y) {
128         this.y = y;
129     }
130     
131     @Override
132     public void display() {
133         System.out.println("(" + String.format("%.2f", this.x) + "," + String.format("%.2f", this.y) + ")");
134     }
135 }
136 
137 class Line extends Element {
138     private Point point1;
139     private Point point2;
140     private String color;
141 
142     public Line() {
143     }
144 
145     public Line(Point point1, Point point2, String color) {
146         this.point1 = point1;
147         this.point2 = point2;
148         this.color = color;
149     }
150 
151     public Point getPoint1() {
152         return point1;
153     }
154 
155     public void setPoint1(Point point1) {
156         this.point1 = point1;
157     }
158 
159     public Point getPoint2() {
160         return point2;
161     }
162 
163     public void setPoint2(Point point2) {
164         this.point2 = point2;
165     }
166 
167     public String getColor() {
168         return color;
169     }
170 
171     public void setColor(String color) {
172         this.color = color;
173     }
174 
175     public double getDistance() {
176         return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));
177     }
178     
179     @Override
180     public void display() {
181         System.out.println("The line's color is:" + this.color);
182         System.out.println("The line's begin point's Coordinate is:");
183         point1.display();
184         System.out.println("The line's end point's Coordinate is:");
185         point2.display();
186         System.out.println("The line's length is:" + String.format("%.2f", getDistance()));
187     }
188 }
189 class Plane extends Element{
190     private String color;
191 
192     public Plane() {
193     }
194 
195     public Plane(String color) {
196         this.color = color;
197     }
198 
199     public String getColor() {
200         return color;
201     }
202 
203     public void setColor(String color) {
204         this.color = color;
205     }
206     
207     @Override
208     public void display() {
209         System.out.println("The Plane's color is:" + this.color);
210     }
211 }
212 
213 class GeometryObject {
214 
215     ArrayList<Element> list = new ArrayList<>();
216 
217     public GeometryObject() {
218     }
219 
220     public void add(Element a) {
221         list.add(a);
222     }
223 
224     public void remove(int index) {
225         if (index >= 0 && index <= list.size() - 1) {
226             list.remove(index);
227         } else {
228             return;
229         }
230     }
231 
232     public ArrayList<Element> getList() {
233         return list;
234     }
235 }
Rongqi_lei

类图:

 

 

 

 

解释和心得:

总结

首先先给大家讲完本篇blog开头留下的小故事,我们的老师上课期间为了便于我们理解将类形象的比喻成了“学校郊游”,面向过程C语言就好像是从学生上车,入座,司机开车,到站,司机开门,学生下车,按照这些流程来做就可以做完,但面向对象不一样,面向对象中,你需要把班级,学生,汽车,分别分成三个类,其中班级包括学生信息,以班级为单位,学生则是个人学生的行为,汽车则专注于车门的开关,启动,十分核心的一个观点就是“各司其职”,即每个人做好自己的分工职能所在,不干涉其余功能的进行,而面向过程完全不考虑这些,能够顺利完成任务即可,想必大家听完这个有趣的小故事之后都对面向过程的java语言有了自己的理解吧!

学习到的内容

继承

  • 子类继承父类

多态

  • 父类变量调用子类方法

抽象类与接口

抽象类
  • 1.在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。
  • 2.由于抽象类不能实例化对象,所以抽象类必须被继承,才能被使用。
  • 3.父类包含了子类集合的常见的方法,但是由于父类本身是抽象的,所以不能使用这些方法。
  • 4.在 Java 中抽象类表示的是一种继承关系,一个类只能继承一个抽象类,而一个类却可以实现多个接口。

课程改进建议及意见

  • 1.我觉得上课的时候可以多让同学上前回答问题,多提高一下同学们的积极性和能动性
  • 2.希望老师能够每次在讲解时多举一些实在的例子帮助大家理解和运用

 

posted on 2022-10-29 23:44  爱喝奶茶的小曦  阅读(34)  评论(0)    收藏  举报