初窥门槛——菜鸟的第二次blog作业

我的第二次博客作业

 

一. 前言——以过来人的眼光看这几次作业;

 

题目集4

我只拿到了88分,面对这题量少,难度大的题我只能say一声sorry,太遗憾了,第一题如果我当时就学会了正则,也许我会拿到满分,可那是如果,当时的我,对一连串的古怪符号望而却步,我没学会,更别说应用其到题目中,于是那个错误格式的测试点我始终没有过。无奈了。后面两题测试点过多,有时候你就按着他的测试点比对,都不知道自己错在哪,我果然还不是一个合格的程序员,我的耐心还需要锻炼,而不是放弃那几个测试点。

题目集5:

这次题目集就是对正则的应用了,题量少,难度小,与上次相比宛若云泥之别。我学会了三种正则表达式的应用,如Patten的mathes方法,或是构造一个匹配器,Pattern p = new Pattern.compiles;Mather m = new

Mather(),还有对Mather的查找,遍历等等。除了最后一题比较之外,前四题就是基础运用。

题目集6:

这次题目集包含了对正则的进阶运用以及多个测试点的进一步训练,上一个题目集4已经是很难了,这次甚至还不说明测试点是什么,只能靠我们一个一个测试,老师说的对,我们以后编程的时候,需要我们debug了,难道甲方会告诉我们测试点在哪吗。我深以为然,并为之做出了奋斗。我花费一天把比较简单的第一题和第三题写出来后,花费了两个通宵,测试他们没过的部分测试点,这个第二题我是没有时间了,我还有一些其他事情,所以没有写完整第二题的所有情况,选项四和选项五我没有写,只拿到了基本分60分多一点。下次我一定会抱着精研严谨的变成思想,与他们奋战到底。

(OPP)期中考试:

这三道题是连环的迭代,一次比一次要求多一点,第一次实现Set和Get,以及ShowDate的方法,比较简单,第二次是需要编写上一次题目的类的父类,而且是抽象类,需要用到抽象方法,实现继承,多态与泛化;第三次便是用到ArrayList方法,将各个类的对象装在ArrayLiast里,因为我是现场开始学的缘故,时间不太够,测试点我无法一个一个试验了,故没有拿到满分,只拿到了84分。是我们班的平均成绩而已。

 

二——我的不太成熟的设计与分析

 

点线形系列一——两点之间的距离;

这题主要难在,输入与输出格式的正确的判断,毫无疑问,正则是最好的判断方式,但当时我的设计是用indexof,charAt,和subString等一系列String自带的方法进行的,代码长度不说,还很繁琐,可阅读性不强。

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner in = new Scanner(System.in);
 6         String str = in.nextLine();
 7         String s1 = "";
 8         String s2 = "";
 9         if(str.contains(" ")) {
10             s1 = str.substring(0,str.indexOf(" "));
11             s2 = str.substring(str.indexOf(" ")+1);
12         }
13         else {
14             System.out.println("Wrong Format");
15             System.exit(0);
16         }
17         if(s2.length() == 0) {
18             System.out.println("Wrong Format");
19             System.exit(0);
20         }
21         if(s2.contains(" ")) {
22             System.out.println("wrong number of points");
23             System.exit(0);
24         }
25         double x1 = 0;
26         double x2 = 0;
27         double y1 = 0;
28         double y2 = 0;
29         if(s1.indexOf(",",1) > 0 && s1.indexOf(",",1) < s1.length() - 1 && s2.indexOf(",",1) > 0 && s2.indexOf(",",1) < s2.length() - 1 && s1.indexOf(",") == s1.lastIndexOf(",") && s2.indexOf(",") == s2.lastIndexOf(",")) {
30             for(int i =0;i < s1.length();i++) {
31                 if(s1.charAt(i) != '+' && s1.charAt(i) != '-' && s1.charAt(i) != ',' && s1.charAt(i) != '.' && s1.charAt(i) < '0' && s1.charAt(i) > '9') {
32                     System.out.println("Wrong Format");
33                     System.exit(0);
34                 }
35             }
36             if(s1.substring(0,s1.indexOf(",")).indexOf("+") != s1.lastIndexOf("+",s1.indexOf(",")) || s2.substring(0,s2.indexOf(",")).indexOf("+") != s2.lastIndexOf("+",s2.indexOf(",")) || s1.substring(s1.indexOf(",")).indexOf("+") != s1.substring(s1.indexOf(",")).lastIndexOf("+") || s2.substring(s2.indexOf(",")).indexOf("+") != s2.substring(s2.indexOf(",")).lastIndexOf("+") ||
37                     s1.substring(0,s1.indexOf(",")).indexOf("-") != s1.lastIndexOf("-",s1.indexOf(",")) || s2.substring(0,s2.indexOf(",")).indexOf("-") != s2.lastIndexOf("-",s2.indexOf(",")) || s1.substring(s1.indexOf(",")).indexOf("-") != s1.substring(s1.indexOf(",")).lastIndexOf("-") || s2.substring(s2.indexOf(",")).indexOf("-") != s2.substring(s2.indexOf(",")).lastIndexOf("-") ||
38                     s1.substring(0,s1.indexOf(",")).indexOf(".") != s1.lastIndexOf(".",s1.indexOf(",")) || s2.substring(0,s2.indexOf(",")).indexOf(".") != s2.lastIndexOf(".",s2.indexOf(",")) || s1.substring(s1.indexOf(",")).indexOf(".") != s1.substring(s1.indexOf(",")).lastIndexOf(".") || s2.substring(s2.indexOf(",")).indexOf(".") != s2.substring(s2.indexOf(",")).lastIndexOf(".")) {
39                 System.out.println("Wrong Format1");
40                 System.exit(0);
41             }
42             if(s1.contains(".") && ((s1.charAt(s1.indexOf(".")-1) < '0'||s1.charAt(s1.indexOf(".")+1) > '9') || (s1.charAt(s1.indexOf(".")-1) < '0'||s1.charAt(s1.indexOf(".")+1) > '9')) && ((s1.charAt(s1.lastIndexOf(".")-1) < '0'||s1.charAt(s1.lastIndexOf(".")+1) > '9') || (s1.charAt(s1.lastIndexOf(".")-1) < '0'||s1.charAt(s1.lastIndexOf(".")+1) > '9'))) {
43                 System.out.println("Wrong Format");
44                 System.exit(0);
45             }
46             if(s2.contains(".") && ((s2.charAt(s2.indexOf(".")-1) < '0'||s2.charAt(s2.indexOf(".")+1) > '9') || (s2.charAt(s2.indexOf(".")-1) < '0'||s2.charAt(s2.indexOf(".")+1) > '9')) && ((s2.charAt(s2.lastIndexOf(".")-1) < '0'||s2.charAt(s2.lastIndexOf(".")+1) > '9') || (s2.charAt(s2.lastIndexOf(".")-1) < '0'||s2.charAt(s2.lastIndexOf(".")+1) > '9'))) {
47                 System.out.println("Wrong Format");
48                 System.exit(0);
49             }
50             if(s1.contains("+") && (s1.indexOf("+") != 0 && s1.indexOf("+") != s1.indexOf(",")+1) && (s1.lastIndexOf("+") != 0 && s1.lastIndexOf("+") != s1.lastIndexOf(",")+1)) {
51                 System.out.println("Wrong Format");
52                 System.exit(0);
53             }
54             if(s2.contains("+") && (s2.indexOf("+") != 0 && s2.indexOf("+") != s2.indexOf(",")+1) && (s2.lastIndexOf("+") != 0 && s2.lastIndexOf("+") != s2.lastIndexOf(",")+1)) {
55                 System.out.println("Wrong Format");
56                 System.exit(0);
57             }
58             if(s1.contains("-") && (s1.indexOf("-") != 0 && s1.indexOf("-") != s1.indexOf(",")+1) && (s1.lastIndexOf("-") != 0 && s1.lastIndexOf("-") != s1.lastIndexOf(",")+1)) {
59                 System.out.println("Wrong Format");
60                 System.exit(0);
61             }
62             if(s2.contains("-") && (s2.indexOf("-") != 0 && s2.indexOf("-") != s2.indexOf(",")+1) && (s2.lastIndexOf("-") != 0 && s2.lastIndexOf("-") != s2.lastIndexOf(",")+1)) {
63                 System.out.println("Wrong Format");
64                 System.exit(0);
65             }
66             if(((s1.indexOf("+",0) > -1 || s1.indexOf("-",0) > -1) && (s1.charAt(s1.indexOf("+")+1) >= '0' && s1.charAt(s1.indexOf("+")+1) <= '9'||s1.charAt(s1.indexOf("-")+1) >= '0' && s1.charAt(s1.indexOf("-")+1) <= '9'))||s1.indexOf("+",0) == -1 && s1.indexOf("-",0) == -1) {
67                 x1 = Double.parseDouble(s1.substring(0,s1.indexOf(",")));
68             }
69             if(((s1.indexOf("+",s1.indexOf(",")) > -1 || s1.indexOf("-",s1.indexOf(",")) > -1) && (s1.charAt(s1.lastIndexOf("+")+1) >= '0' && s1.charAt(s1.lastIndexOf("+")+1) <= '9'||s1.charAt(s1.lastIndexOf("-")+1) >= '0' && s1.charAt(s1.lastIndexOf("-")+1) <= '9'))||s1.indexOf("+",s1.indexOf(",")) == -1 && s1.indexOf("-",s1.indexOf(",")) == -1) {
70                 y1 = Double.parseDouble(s1.substring(s1.indexOf(",")+1));
71             }
72             if(((s2.indexOf("+",0) > -1 || s2.indexOf("-",0) > -1) && (s2.charAt(s2.indexOf("+")+1) >= '0' && s2.charAt(s2.indexOf("+")+1) <= '9'||s2.charAt(s2.indexOf("-")+1) >= '0' && s2.charAt(s2.indexOf("-")+1) <= '9'))||s2.indexOf("+",0) == -1 && s2.indexOf("-",0) == -1) {
73                 x2 = Double.parseDouble(s2.substring(0,s2.indexOf(",")));
74             }
75             if(((s2.indexOf("+",s2.indexOf(",")) > -1 || s2.indexOf("-",s2.indexOf(",")) > -1) && (s2.charAt(s2.lastIndexOf("+")+1) >= '0' && s2.charAt(s2.lastIndexOf("+")+1) <= '9'||s2.charAt(s2.lastIndexOf("-")+1) >= '0' && s2.charAt(s2.lastIndexOf("-")+1) <= '9'))||s2.indexOf("+",s2.indexOf(",")) == -1 && s2.indexOf("-",s2.indexOf(",")) == -1) {
76                 y2 = Double.parseDouble(s2.substring(s2.indexOf(",")+1));
77             }
78             if(x1 != 0 && y1 != 0 && x2 != 0 && y2 != 0) {
79                 System.out.println(Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
80                 System.exit(0);
81             }
82             else {
83                 System.out.println("Wrong Format");
84             }
85         }
86         else {
87             System.out.println("Wrong Format");
88         }
89     }
90 }

点线形系列一——线的计算;

我的主要思路是,根据数学方法 ,有没有简单易实现且变量可以提取的公式去应用,以数学思想写编程;先是用正则的split分割选项与输入的点,查看格式正确与否,再将得到的点的x值与y值代入线的公式,得出最终答案;我主要写了三个类,主类main,一个check看格式是否正确,一个dot带入方法

  1 import java.util.Scanner;
  2 public class Main {
  3     public static void main(String[] args)
  4     {
  5         Scanner in=new Scanner(System.in);
  6         String s=in.nextLine();
  7         if(Check.islegal(s)==1)
  8         {
  9             Dot.setString(s);
 10         }
 11     }
 12 }
 13 class Dot{
 14     double x;
 15     double y;
 16     public static void setString(String s) {
 17         String[] a = s.split(":");
 18         String[] b = a[1].split("\\s");
 19         Dot[] dot = new Dot[b.length];
 20         for (int i = 0; i < b.length; i++) {
 21             String[] c = b[i].split(",");
 22             dot[i] = new Dot();
 23             dot[i].x = Double.parseDouble(c[0]);
 24             dot[i].y = Double.parseDouble(c[1]);
 25             //System.out.println(dot[i].x+" "+dot[i].y);
 26         }
 27         if (a[0].charAt(0) == '1') {
 28             if (dot[0].x == dot[1].x && dot[0].y == dot[1].y)
 29             {
 30                 System.out.println("points coincide");
 31             }
 32             else if (dot[0].x == dot[1].x)
 33             {
 34                 System.out.println("Slope does not exist");
 35             }
 36             else {
 37                 double A = (dot[1].y - dot[0].y) / (dot[1].x - dot[0].x);
 38                 System.out.println(A);
 39             }
 40         }
 41         if (a[0].charAt(0) == '2') {
 42             if (dot[1].x == dot[2].x && dot[1].y == dot[2].y)
 43             {
 44                 System.out.println("points coincide");
 45             }
 46             else
 47             {
 48                 if (dot[1].x != dot[2].x) {
 49                     double A = (dot[2].y - dot[1].y) / (dot[2].x - dot[1].x);
 50                     double C = dot[1].y - A * dot[1].x;
 51                     double d = Math.abs(A * dot[0].x - dot[0].y + C) / Math.sqrt(A * A + 1);
 52                     System.out.println(d);
 53                 }
 54                 else if(dot[1].x==dot[2].x)
 55                 {
 56                     double d=Math.abs(dot[0].x-dot[1].x);
 57                     System.out.println(d);
 58                 }
 59             }
 60         }
 61         if (a[0].charAt(0) == '3') {
 62             if ((dot[0].x == dot[1].x && dot[0].y == dot[1].y) || (dot[0].x == dot[2].x && dot[0].y == dot[2].y) || (dot[1].x == dot[2].x && dot[1].y == dot[2].y)) {
 63                 System.out.println("points coincide");
 64             }
 65             else
 66             {
 67                 if(dot[2].x!=dot[1].x) {
 68                     double A = (dot[2].y - dot[1].y) / (dot[2].x - dot[1].x);
 69                     double C = dot[1].y - A * dot[1].x;
 70                     double result = A * dot[0].x + dot[0].y + C;
 71                     if (result == 0)
 72                         System.out.println("true");
 73                     else
 74                         System.out.println("false");
 75                 }
 76                 else if(dot[2].x==dot[1].x)
 77                 {
 78                     if(dot[0].x==dot[1].x)
 79                         System.out.println("true");
 80                     else
 81                         System.out.println("false");
 82                 }
 83             }
 84         }
 85         if (a[0].charAt(0) == '4') {
 86             if ((dot[0].x == dot[1].x && dot[0].y == dot[1].y) || (dot[2].x == dot[3].x && dot[2].y == dot[3].y))
 87             {
 88                 System.out.println("points coincide");
 89             }
 90             else
 91             {
 92                 double A1 = (dot[1].y - dot[0].y) / (dot[1].x - dot[0].x);
 93                 double A2 = (dot[3].y - dot[2].y) / (dot[3].x - dot[2].x);
 94                 if (A1 == A2)
 95                     System.out.println("true");
 96                 else
 97                     System.out.println("false");
 98             }
 99         }
100         if(a[0].charAt(0)=='5')
101         {
102             if((dot[0].x==dot[1].x&&dot[0].y==dot[1].y)||(dot[2].x==dot[3].x&&dot[2].y==dot[3].y))
103             {
104                 System.out.println("points coincide");
105             }
106             else
107             {
108                 double y1,y2,x1,x2,a1,a2,e;
109                 y1=dot[1].y-dot[0].y;
110                 y2=dot[3].y-dot[2].y;
111                 x1=dot[1].x-dot[0].x;
112                 x2=dot[3].x-dot[2].x;
113                 a1=x1*dot[0].y-y1*dot[0].x;
114                 a2=x2*dot[2].y-y2*dot[2].x;
115                 e=y1*x2-y2*x1;
116                 double x=(x1*a2-x2*a1)/e;
117                 double y=(y1*a2-y2*a1)/e;
118                 if(e==0)
119                 {
120                     System.out.println("is parallel lines,have no intersection point");
121                 }
122                 else
123                 {
124                     double max,min,max1,min1;
125                     if(dot[3].y>dot[2].y)
126                     {
127                         max=dot[3].y;
128                         min=dot[2].y;
129                     }
130                     else
131                     {
132                         max=dot[2].y;
133                         min=dot[3].y;
134                     }
135                     if(dot[1].y>dot[0].y)
136                     {
137                         max1=dot[1].y;
138                         min1=dot[0].y;
139                     }
140                     else
141                     {
142                         max1=dot[0].y;
143                         min1=dot[1].y;
144                     }
145                     if((y>min&&y<max)||(y>min1&&y<max1))
146                     {
147                         System.out.print(x+","+y+" ");
148                         System.out.println("true");
149                     }
150                     else
151                     {
152                         System.out.print(x+","+y+" ");
153                         System.out.println("false");
154                     }
155                 }
156             }
157         }
158     }
159 }
160 class Check{
161     public static int islegal(String s)
162     {
163         int count;
164         int count1=0;
165         int count2=0;
166         int count3=0;
167         int flag=0;
168         int flag1=0;
169         int dotnum=0;
170         String[] a=s.split(":");
171         
172          if(s.charAt(0)=='2'||s.charAt(0)=='3')
173         {
174             dotnum=3;
175         }
176         else if(s.charAt(0)=='4'||s.charAt(0)=='5')
177         {
178             dotnum=4;
179         }
180         if(s.charAt(0)>='0'&&s.charAt(0)<='5'&&s.charAt(1)!=':')
181         {
182             flag=1;
183             dotnum=5;
184         }
185         char[] n=s.toCharArray();
186         for(int i=0;i<n.length;i++)
187         {
188             if(n[i]==':')
189                 count2++;
190         }
191         if(count2>=2)
192         {
193             flag=1;
194         }
195         char[] c=a[1].toCharArray();
196         for(int i=0;i<c.length;i++)
197         {
198             if(c[i]==','&&c[i+1]==',')
199             {
200                 count1++;
201             }
202             if(c[i]==' '&&c[i+1]==' ')
203             {
204                 count3++;
205             }
206         }
207         String[] p=a[1].split("\\s");
208         if(p.length!=dotnum)
209         {
210             flag1=1;
211         }
212         if(count1>=1)
213         {
214             flag=1;
215         }
216         if(count3>=1)
217         {
218             flag=1;
219         }
220         else {
221             String b[] = a[1].split("\\s");
222             if (count2 < 2) {
223                 for (int i = 0; i < b.length; i++) {
224                     String[] m = b[i].split(",");
225                     if (m.length > dotnum) {
226                         flag = 1;
227                     }
228                     for (int j = 0; j < m.length; j++) {
229                         count = 0;
230                         for (int k = 0; k < m[j].length(); k++) {
231                             if (m[j].charAt(k) == '.') {
232                                 count++;
233                                 if (k + 1 == m[j].length())
234                                     flag = 1;
235                             }
236                         }
237                         if (count >= dotnum) {
238                             flag = 1;
239                         }
240                         if (m[j].charAt(0) == '+' || m[j].charAt(0) == '-') {
241                             if (m[j].charAt(1) < '0' || m[j].charAt(1) > '9') {
242                                 flag = 1;
243                             }
244                             if (m[j].charAt(1) == '0' && m[j].length() > 2)
245                                 if (m[j].charAt(1) == '0' && m[j].charAt(2) != '.')
246                                     flag = 1;
247                         }
248                         if (m[j].charAt(0) == '0' && m[j].length() > 1) {
249                             if (m[j].charAt(0) == '0' && m[j].charAt(1) != '.')
250                                 flag = 1;
251                         }
252                     }
253                 }
254             }
255         }
256         if(flag==1)
257         {
258             System.out.println("Wrong Format");
259         }
260         else if(flag1==1&&flag==0)
261         {
262             System.out.println("wrong number of points");
263         }
264         else
265             return 1;
266         return 0;
267     }
268 }

 

点线形系列一——三角形的计算;

刚开始与第二题类似,都用split方法来分割选项与点的坐标;接着我构思了一个check类查看选项;一个dot来实现选项中的操作,因为想省事,dot类里把所有选项的方法几乎都包括了,其中判断三角形的方法,大部分都是和斜率有关系,于是第二题的代码也可以copy一些;

  1 import java.util.Scanner;
  2 public class Main {
  3     public static void main(String[] args)
  4     {
  5         Scanner in=new Scanner(System.in);
  6         String s=in.nextLine();
  7         if(Check.islegal(s)==1)
  8         {
  9             Dot.setString(s);
 10         }
 11     }
 12 }
 13 class Dot{
 14     double x;
 15     double y;
 16     Dot(double x,double y)
 17     {
 18         this.x=x;
 19         this.y=y;
 20     }
 21     public static void setString(String s)
 22     {
 23         String[] a = s.split(":");
 24         String[] b = a[1].split("\\s");
 25         Dot[] dot = new Dot[b.length];
 26         for (int i = 0; i < b.length; i++) {
 27             String[] c = b[i].split(",");
 28             dot[i] = new Dot(Double.parseDouble(c[0]),Double.parseDouble(c[1]));
 29             //System.out.println(dot[i].x+" "+dot[i].y);
 30         }
 31         if(a[0].charAt(0)=='1')
 32         {
 33             double l1,l2,l3;
 34             l1=Math.sqrt((dot[1].x-dot[0].x)*(dot[1].x-dot[0].x)+(dot[1].y-dot[0].y)*(dot[1].y-dot[0].y));
 35             l2=Math.sqrt((dot[2].x-dot[0].x)*(dot[2].x-dot[0].x)+(dot[2].y-dot[0].y)*(dot[2].y-dot[0].y));
 36             l3=Math.sqrt((dot[2].x-dot[1].x)*(dot[2].x-dot[1].x)+(dot[2].y-dot[1].y)*(dot[2].y-dot[1].y));
 37             if(l1+l2<=l3||l1+l3<=l2||l2+l3<=l1||(dot[0].x==dot[1].x&&dot[1].x==dot[2].x)||(dot[0].y==dot[1].y&&dot[1].y==dot[2].y)) {
 38                 System.out.println("data error");
 39             }
 40             else {
 41                 if (l1 == l2 || l1 == l3 || l2 == l3) {
 42                     System.out.print("true ");
 43                 } else {
 44                     System.out.print("false ");
 45                 }
 46                 if (l1 == l2 && l2 ==l3) {
 47                     System.out.print("true");
 48                 } else {
 49                     System.out.print("false");
 50                 }
 51             }
 52         }
 53         if(a[0].charAt(0)=='2')
 54         {
 55             double l1=Math.sqrt((dot[1].x-dot[0].x)*(dot[1].x-dot[0].x)+(dot[1].y-dot[0].y)*(dot[1].y-dot[0].y));
 56             double l2=Math.sqrt((dot[2].x-dot[0].x)*(dot[2].x-dot[0].x)+(dot[2].y-dot[0].y)*(dot[2].y-dot[0].y));
 57             double l3=Math.sqrt((dot[2].x-dot[1].x)*(dot[2].x-dot[1].x)+(dot[2].y-dot[1].y)*(dot[2].y-dot[1].y));
 58             if(l1+l2<=l3||l1+l3<=l2||l2+l3<=l1||(dot[0].x==dot[1].x&&dot[1].x==dot[2].x)||(dot[0].y==dot[1].y&&dot[1].y==dot[2].y))
 59             {
 60                 System.out.println("data error");
 61             }
 62             else {
 63                 double L = l1 + l2 + l3;
 64                 double S = (dot[0].x * dot[1].y + dot[1].x * dot[2].y + dot[2].x * dot[0].y - dot[0].x * dot[2].y - dot[1].x * dot[0].y - dot[2].x * dot[1].y) / 2;
 65                 double x = (dot[0].x + dot[1].x + dot[2].x) / 3;
 66                 double y = (dot[0].y + dot[1].y + dot[2].y) / 3;
 67                 double num[] = {L, S, x, y};
 68                 String[] m = new String[4];
 69                 int count = 0;
 70                 for (int i = 0; i < 4; i++) {
 71                     String str = num[i] + "";
 72                     int n = str.length() - (str.indexOf(".") + 1);
 73                     if (n > 6) {
 74                         m[count] = String.format("%.6f", num[i]);
 75                         count++;
 76                     }
 77                     else
 78                     {
 79                         m[count] = num[i] + "";
 80                         count++;
 81                     }
 82                 }
 83                 System.out.print(m[0] + " " + m[1] + " " + m[2] + ',' + m[3]);
 84             }
 85         }
 86         if(a[0].charAt(0)=='3')
 87         {
 88             double l1=Math.sqrt((dot[1].x-dot[0].x)*(dot[1].x-dot[0].x)+(dot[1].y-dot[0].y)*(dot[1].y-dot[0].y));
 89             double l2=Math.sqrt((dot[2].x-dot[0].x)*(dot[2].x-dot[0].x)+(dot[2].y-dot[0].y)*(dot[2].y-dot[0].y));
 90             double l3=Math.sqrt((dot[2].x-dot[1].x)*(dot[2].x-dot[1].x)+(dot[2].y-dot[1].y)*(dot[2].y-dot[1].y));
 91             if(l1+l2<=l3||l1+l3<=l2||l2+l3<=l1||(dot[0].x==dot[1].x&&dot[1].x==dot[2].x)||(dot[0].y==dot[1].y&&dot[1].y==dot[2].y)) {
 92                 System.out.println("data error");
 93             }
 94             else {
 95                 if (l1*l1+l2*l2<l3*l3-0.001||l1*l1+l3*l3<l2*l2-0.001||l2*l2+l3*l3<l1*l1-0.001)
 96                     System.out.print("true ");
 97                 else
 98                     System.out.print("false ");
 99                 if (Math.abs(l1*l1+l2*l2-l3*l3)<0.001||Math.abs(l1*l1+l3*l3-l2*l2)<0.001||Math.abs(l2*l2+l3*l3-l1*l1)<0.001)
100                     System.out.print("true ");
101                 else
102                     System.out.print("false ");
103                 if (l1*l1+l2*l2>l3*l3&&l1*l1+l3*l3>l2*l2&&l2*l2+l3*l3>l1*l1)
104                     System.out.print("true");
105                 else
106                     System.out.print("false");
107             }
108         }
109         if(a[0].charAt(0)=='4')
110         {
111             double l1=Math.sqrt((dot[3].x-dot[2].x)*(dot[3].x-dot[2].x)+(dot[3].y-dot[2].y)*(dot[3].y-dot[2].y));
112             double l2=Math.sqrt((dot[4].x-dot[2].x)*(dot[4].x-dot[2].x)+(dot[4].y-dot[2].y)*(dot[4].y-dot[2].y));
113             double l3=Math.sqrt((dot[4].x-dot[3].x)*(dot[4].x-dot[3].x)+(dot[4].y-dot[3].y)*(dot[4].y-dot[3].y));
114             if (dot[0].x == dot[1].x && dot[0].y == dot[1].y) {
115                 System.out.println("points coincide");
116             }
117             else {
118                 if (l1 + l2 <= l3 || l1 + l3 <= l2 || l2 + l3 <= l1 || (dot[2].x == dot[3].x && dot[3].x == dot[4].x) || (dot[2].y == dot[3].y && dot[3].y == dot[4].y)) {
119                     System.out.println("data error");
120                 }
121                 else
122                 {
123                     double A = (dot[1].y - dot[0].y) / (dot[1].x - dot[0].x);
124                     double C = dot[0].y - A * dot[0].x;
125                     double A1 = (dot[3].y - dot[2].y) / (dot[3].x - dot[2].x);
126                     double C1 = dot[2].y - A1 * dot[2].x;
127                     double A2 = (dot[4].y - dot[2].y) / (dot[4].x - dot[2].x);
128                     double C2 = dot[2].y - A2 * dot[2].x;
129                     double A3 = (dot[4].y - dot[3].y) / (dot[4].x - dot[3].x);
130                     double C3 = dot[3].y - A3 * dot[3].x;
131                     if((A==A1&&C==C1)||(A==A2&&C==C2)||(A==A3&&C==C3))
132                     {
133                         System.out.println("The point is on the edge of the triangle");
134                     }
135                     else
136                     {
137                         int dotnum=0;
138                         int num1=0;
139                         int num2=0;
140                         int num3=0;
141                         Dot d1=calcudot(dot[0],dot[1],dot[2],dot[3]);
142                         Dot d2=calcudot(dot[0],dot[1],dot[2],dot[4]);
143                         Dot d3=calcudot(dot[0],dot[1],dot[3],dot[4]);
144                         if(isin(dot[2],dot[3],d1)) {
145                             num1=1;
146                             dotnum++;
147                         }
148                         if(isin(dot[2],dot[4],d2)) {
149                             dotnum++;
150                             num2=1;
151                         }
152                         if(isin(dot[3],dot[4],d3)) {
153                             dotnum++;
154                             num3=1;
155                         }
156                         if(d1.x==d2.x&&d1.y==d2.y) {
157                             dotnum--;
158                             num1=0;
159                             num2=0;
160                         }
161                         if(d1.x==d3.x&&d1.y==d3.y) {
162                             dotnum--;
163                             num1=0;
164                             num3=0;
165                         }
166                         if(d2.x==d3.x&&d2.y==d3.y) {
167                             dotnum--;
168                             num2=0;
169                             num3=0;
170                         }
171                         if(dotnum==1||dotnum==0)
172                             System.out.println(dotnum);
173                         else
174                         {
175                             double S = Math.abs(dot[2].x * dot[3].y + dot[3].x * dot[4].y + dot[4].x * dot[2].y - dot[2].x * dot[4].y - dot[3].x * dot[2].y - dot[4].x * dot[3].y) / 2;
176                             double S1=0;
177                             double S2;
178                             if(num1==1&&num2==1)
179                             {
180                                 S1 = Math.abs(d1.x * d2.y + d2.x * dot[2].y + dot[2].x * d1.y - d1.x * dot[2].y - d2.x * d1.y - dot[2].x * d2.y) / 2;
181                             }
182                             else if(num1==1&&num3==1)
183                             {
184                                 S1 = Math.abs(d1.x * d3.y + d3.x * dot[3].y + dot[3].x * d1.y - d1.x * dot[3].y - d3.x * d1.y - dot[3].x * d3.y) / 2;
185                             }
186                             else if(num2==1&&num3==1)
187                             {
188                                 S1 = Math.abs(d2.x * d3.y + d3.x * dot[4].y + dot[4].x * d2.y - d2.x * dot[4].y - d3.x * d2.y - dot[4].x * d3.y) / 2;
189                             }
190                             else if(num1==1&&num2==0&&num3==0)
191                             {
192                                 S1 = Math.abs(d1.x * dot[3].y + dot[3].x * dot[4].y + dot[4].x * d1.y - d1.x * dot[4].y - dot[3].x * d1.y - dot[4].x * dot[3].y) / 2;
193                             }
194                             else if(num2==1&&num1==0&&num3==0)
195                             {
196                                 S1 = Math.abs(d2.x * dot[3].y + dot[3].x * dot[2].y + dot[2].x * d2.y - d2.x * dot[2].y - dot[3].x * d2.y - dot[2].x * dot[3].y) / 2;
197                             }
198                             else if(num3==1&&num1==0&&num2==0)
199                             {
200                                 S1 = Math.abs(d3.x * dot[3].y + dot[3].x * dot[2].y + dot[2].x * d3.y - d3.x * dot[2].y - dot[3].x * d3.y - dot[2].x * dot[3].y) / 2;
201                             }
202                             if(S-S1<S1)
203                             {
204                                 S2=S1;
205                                 S1=S-S1;
206                             }
207                             else
208                             {
209                                 S2=S-S1;
210                             }
211                             double num[] = {S1,S2};
212                             String[] m = new String[2];
213                             int count = 0;
214                             for (int i = 0; i < 2; i++) {
215                                 String str = num[i] + "";
216                                 int n = str.length() - (str.indexOf(".") + 1);
217                                 if (n > 6) {
218                                     m[count] = String.format("%.6f", num[i]);
219                                     count++;
220                                 }
221                                 else
222                                 {
223                                     m[count] = num[i] + "";
224                                     count++;
225                                 }
226                             }
227                             System.out.println("2 "+m[0]+" "+m[1]);
228                         }
229                     }
230                 }
231             }
232         }
233         if(a[0].charAt(0)=='5')
234         {
235             double l1=Math.sqrt((dot[2].x-dot[1].x)*(dot[2].x-dot[1].x)+(dot[2].y-dot[1].y)*(dot[2].y-dot[1].y));
236             double l2=Math.sqrt((dot[3].x-dot[1].x)*(dot[3].x-dot[1].x)+(dot[3].y-dot[1].y)*(dot[3].y-dot[1].y));
237             double l3=Math.sqrt((dot[3].x-dot[2].x)*(dot[3].x-dot[2].x)+(dot[3].y-dot[2].y)*(dot[3].y-dot[2].y));
238             if(l1+l2<=l3||l1+l3<=l2||l2+l3<=l1||(dot[1].x==dot[2].x&&dot[2].x==dot[3].x)||(dot[1].y==dot[2].y&&dot[2].y==dot[3].y)){
239                 System.out.println("data error");
240             }
241             else
242             {
243                 int i,j;
244                 int flag=0;
245                 boolean redo,inside;
246                 redo=true;
247                 for(i=1;i<4;i++)
248                 {
249                     if(dot[0].x==dot[i].x&&dot[0].y==dot[i].y)
250                     {
251                         redo=false;
252                         inside=true;
253                         System.out.println("on the triangle");
254                         break;
255                     }
256                 }
257                 while (redo)
258                 {
259                     redo = false;
260                     inside = false;
261                     for (i = 1,j =3;i <4;j=i++)
262                     {
263                         if ( (dot[i].y < dot[0].y && dot[0].y < dot[j].y) || (dot[j].y < dot[0].y && dot[0].y < dot[i].y) )
264                         {
265                             if(dot[0].x <= dot[i].x || dot[0].x <= dot[j].x)
266                             {
267                                 double x = (dot[0].y-dot[i].y)*(dot[j].x-dot[i].x)/(dot[j].y-dot[i].y)+dot[i].x;
268                                 if (dot[0].x <x)
269                                 {
270                                     System.out.println("in the triangle");
271                                     inside = !inside;
272                                 }
273                                 else if (dot[0].x==x)
274                                 {
275                                     System.out.println("on the triangle");
276                                     inside = true;
277                                     break;
278                                 }
279                                 else
280                                 {
281                                     System.out.println("outof the triangle");
282                                     inside=false;
283                                 }
284                             }
285                         }
286                         else if ( dot[0].y ==dot[i].y)
287                         {
288                             if (dot[0].x < dot[i].x)
289                             {
290                                 System.out.println("outof the triangle");
291                                 redo = true;
292                                 flag=1;
293                                 break;
294                             }
295                         }
296                         else if (dot[i].y==dot[j].y &&dot[0].y == dot[i].y && ( (dot[i].x < dot[0].x && dot[0].x < dot[j].x) || (dot[j].x < dot[0].x && dot[0].x < dot[i].x) ) )
297                         {
298                             System.out.println("in the triangle");
299                             inside = true;
300                             break;
301                         }
302                     }
303                 }
304             }
305         }
306     }
307     public static Dot calcudot(Dot d1,Dot d2,Dot d3,Dot d4)
308     {
309         double y1,y2,x1,x2,a1,a2,e;
310         y1=d2.y-d1.y;
311         y2=d4.y-d3.y;
312         x1=d2.x-d1.x;
313         x2=d4.x-d3.x;
314         a1=x1*d1.y-y1*d1.x;
315         a2=x2*d3.y-y2*d3.x;
316         e=y1*x2-y2*x1;
317         double x=(x1*a2-x2*a1)/e;
318         double y=(y1*a2-y2*a1)/e;
319         Dot p1=new Dot(x,y);
320         return p1;
321     }
322     public static Boolean isin(Dot d1,Dot d2,Dot d3)
323     {
324         if(d3.y<=Math.max(d1.y,d2.y)&&d3.y>=Math.min(d1.y,d2.y))
325         {
326             return true;
327         }
328         return false;
329     }
330 }
331 class Check{
332     public static int islegal(String s)
333     {
334         int count;
335         int count1=0;
336         int count2=0;
337         int count3=0;
338         int count4=0;
339         int flag=0;
340         int flag1=0;
341         int dotnum=0;
342         String[] a=s.split(":");
343         if(s.charAt(0)=='1'||s.charAt(0)=='2'||s.charAt(0)=='3')
344         {
345             dotnum=3;
346         }
347         else if(s.charAt(0)=='4')
348         {
349             dotnum=5;
350         }
351         else if (s.charAt(0) =='5') {
352             dotnum=4;
353         }
354         if(s.charAt(0)>='0'&&s.charAt(0)<='5'&&s.charAt(1)!=':')
355         {
356             flag=1;
357             dotnum=10;
358         }
359         if(s.charAt(1)==':')
360         {
361             count4=1;
362         }
363         char[] n=s.toCharArray();
364         for(int i=0;i<n.length;i++)
365         {
366             if(n[i]==':')
367                 count2++;
368         }
369         if(count2>=2)
370         {
371             flag=1;
372         }
373         if(count2==1&&count4==1) {
374             char[] c = a[1].toCharArray();
375             for (int i = 0; i < c.length; i++) {
376                 if (c[i] == ',' && c[i + 1] == ',') {
377                     count1++;
378                 }
379                 if (c[i] == ' ' && i + 1 != c.length) {
380                     if (c[i + 1] == ' ')
381                         count3++;
382                 }
383             }
384             if (c[c.length - 1] == ' ') {
385                 flag = 1;
386             }
387             String[] p = a[1].split("\\s");
388             if (p.length != dotnum) {
389                 flag1 = 1;
390             }
391             if (count1 >= 1) {
392                 flag = 1;
393             }
394             if (count3 >= 1) {
395                 flag = 1;
396             }
397         }
398         else {
399             if (count4 == 1) {
400                 String b[] = a[1].split("\\s");
401                 if (count2 < 2 && count1 < 1) {
402                     for (int i = 0; i < b.length; i++) {
403                         String[] m = b[i].split(",");
404                         if (m.length > dotnum) {
405                             flag = 1;
406                         }
407                         for (int j = 0; j < m.length; j++) {
408                             count = 0;
409                             for (int k = 0; k < m[j].length(); k++) {
410                                 if (m[j].charAt(k) == '.') {
411                                     count++;
412                                     if (k + 1 == m[j].length())
413                                         flag = 1;
414                                 }
415                             }
416                             if (count >= dotnum) {
417                                 flag = 1;
418                             }
419                             if (m[j].charAt(0) == '+' || m[j].charAt(0) == '-') {
420                                 if (m[j].charAt(1) < '0' || m[j].charAt(1) > '9') {
421                                     flag = 1;
422                                 }
423                                 if (m[j].charAt(1) == '0' && m[j].length() > 2)
424                                     if (m[j].charAt(1) == '0' && m[j].charAt(2) != '.')
425                                         flag = 1;
426                             }
427                             if (m[j].charAt(0) == '0' && m[j].length() > 1) {
428                                 if (m[j].charAt(0) == '0' && m[j].charAt(1) != '.')
429                                     flag = 1;
430                             }
431                         }
432                     }
433                 }
434             }
435         }
436         if(flag==1)
437         {
438             System.out.println("Wrong Format");
439         }
440         else if(flag1==1&&flag==0)
441         {
442             System.out.println("wrong number of points");
443         }
444         else
445             return 1;
446         return 0;
447     }
448 }

点线形系列一——凸四边形的计算;

没错跨了一个正则,这个点线形系列又回来了,我的这个系列分一次比一次少;首先我觉得以后可能还会出现迭代,而且学会了正则,于是我规范化了我的代码,首先main类里直接判断选项,输入格式匹配用mathes方法,分割以及替换空格的split方法,switch方法进入选项,Quadrilateral类写入判断方法,类型boolean,我只编写了前三种选项的方法,所以分数不高。我接下来的修改将是再次编写另外两个类,受同学启发,一个关于线的判断逻辑Line类,一个判断格式正不正确的点类Point。

代码如下:

  1 import java.text.DecimalFormat;
  2 import java.util.Scanner;
  3 
  4 public class Main {
  5     public static void main(String[] args) {
  6         Scanner in = new Scanner(System.in);
  7         String string = in.nextLine();
  8         Quadrilateral q = new Quadrilateral();
  9         int count = 0;
 10         int choice = Integer.parseInt(string.substring(0,1));
 11         String str = string.substring(2);
 12         String ReGex = " ";
 13         //String Regex = "^(\\-|\\+)?\\d+(\\.\\d+)?,(\\-|\\+)?\\d+(\\.\\d+)?$";
 14         String Reg1 = "((\\-|\\+)?\\d+(\\.\\d+)?,(\\-|\\+)?\\d+(\\.\\d+)?\\s?){4}";
 15         String Reg2 = "((\\-|\\+)?\\d+(\\.\\d+)?,(\\-|\\+)?\\d+(\\.\\d+)?\\s?){5}";
 16         String Reg3 = "((\\-|\\+)?\\d+(\\.\\d+)?,(\\-|\\+)?\\d+(\\.\\d+)?\\s?){6}";
 17         String[] arr1 = str.split(ReGex);
 18         for(String s :arr1)
 19             count++;
 20         double[] arr2 = new double[12];
 21         if((choice==1||choice==2||choice==3)&&count==4 || choice==4&&count==6 || choice==5&&count==5) {
 22             if((choice==1||choice==2||choice==3)&&str.matches(Reg1) || choice==4&&str.matches(Reg3) || choice==5&&str.matches(Reg2)) {
 23 
 24                 arr2[0] = Double.parseDouble(arr1[0].substring(0,arr1[0].indexOf(",")));
 25                 arr2[1] = Double.parseDouble(arr1[0].substring(arr1[0].indexOf(",")+1));
 26                 arr2[2] = Double.parseDouble(arr1[1].substring(0,arr1[1].indexOf(",")));
 27                 arr2[3] = Double.parseDouble(arr1[1].substring(arr1[1].indexOf(",")+1));
 28                 arr2[4] = Double.parseDouble(arr1[2].substring(0,arr1[2].indexOf(",")));
 29                 arr2[5] = Double.parseDouble(arr1[2].substring(arr1[2].indexOf(",")+1));
 30                 arr2[6] = Double.parseDouble(arr1[3].substring(0,arr1[3].indexOf(",")));
 31                 arr2[7] = Double.parseDouble(arr1[3].substring(arr1[3].indexOf(",")+1));
 32                 switch (choice) {
 33                     case 1:
 34                         for(int i=0 ;i < 3;i++) {
 35                             for(int j=i+1;j<4;j++) {
 36                                 if(arr1[i].equals(arr1[j])) {
 37                                     System.out.println("points coincide");
 38                                     System.exit(0);
 39                                 }
 40                             }
 41                         }
 42                         System.out.print(q.isQuadrilateral(arr2[0],arr2[1],arr2[2],arr2[3],arr2[4],arr2[5],arr2[6],arr2[7]));
 43                         System.out.println(" "+q.isParallelogram(arr2[0],arr2[1],arr2[2],arr2[3],arr2[4],arr2[5],arr2[6],arr2[7]));
 44                         break;
 45                     case 2:
 46                         
 47                         if(q.isQuadrilateral(arr2[0],arr2[1],arr2[2],arr2[3],arr2[4],arr2[5],arr2[6],arr2[7])) {
 48                             System.out.print(q.isRhombus(arr2[0],arr2[1],arr2[2],arr2[3],arr2[4],arr2[5],arr2[6],arr2[7]));
 49                             System.out.print(" "+q.isRectangle(arr2[0],arr2[1],arr2[2],arr2[3],arr2[4],arr2[5],arr2[6],arr2[7]));
 50                             System.out.println(" "+q.isSquare(arr2[0],arr2[1],arr2[2],arr2[3],arr2[4],arr2[5],arr2[6],arr2[7]));
 51                         }
 52                         else {
 53                             System.out.println("not a quadrilateral");
 54                         }
 55                         break;
 56                     case 3:
 57                         for(int i=0 ;i < 3;i++) {
 58                             for(int j=i+1;j<4;j++) {
 59                                 if(arr1[i].equals(arr1[j])) {
 60                                     System.out.println("points coincide");
 61                                     System.exit(0);
 62                                 }
 63                             }
 64                         }
 65                         if(q.isQuadrilateral(arr2[0],arr2[1],arr2[2],arr2[3],arr2[4],arr2[5],arr2[6],arr2[7])) {
 66                             System.out.print(q.isConvexQuadrilateral(arr2[0],arr2[1],arr2[2],arr2[3],arr2[4],arr2[5],arr2[6],arr2[7]));
 67                             System.out.print(" "+q.perimeter(arr2[0],arr2[1],arr2[2],arr2[3],arr2[4],arr2[5],arr2[6],arr2[7]));
 68                             System.out.println(" "+q.area(arr2[0],arr2[1],arr2[2],arr2[3],arr2[4],arr2[5],arr2[6],arr2[7]));
 69                         }
 70                         else {
 71                             System.out.println("not a quadrilateral");
 72                         }
 73                         break;
 74                     case 4:
 75                         System.out.println("not a quadrilateral or triangle");
 76                         break;
 77                     default:
 78                         System.out.println("not a quadrilateral or triangle");
 79                 }
 80             }
 81             else {
 82                 System.out.println("Wrong Format");
 83             }
 84         }
 85         else if((choice==1||choice==2||choice==3)&&count<4 || choice==4&&count<6 || choice==5&&count<5) {
 86             System.out.println("wrong number of points");
 87         }
 88         else {
 89             System.out.println("Wrong Format");
 90         }
 91     }
 92 }
 93 class Quadrilateral{
 94     public boolean isQuadrilateral(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {//四边形
 95         if(y1==y2 || y2==y3 || y3==y4 || y1==y4) {
 96             if(y1==y2&&y1!=y4&&y2!=y3 || y3==y2&&y1!=y2&&y4!=y3 || y3==y4&&y1!=y4&&y2!=y3 || y1==y4&&y1!=y2&&y4!=y3) {
 97                 return true;
 98             }
 99             else {
100                 return false;
101             }
102         }
103         else {
104             double k1 = (x1-x2)/(y1-y2);
105             double k2 = (x2-x3)/(y2-y3);
106             double k3 = (x3-x4)/(y3-y4);
107             double k4 = (x1-x4)/(y1-y4);
108             if(k1 != k2 && k2 != k3 && k3 != k4 && k1 != k4) {
109                 return true;
110             }
111             else {
112                 return false;
113             }
114         }
115     }
116     public boolean isParallelogram(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {//平行四边形
117         if(isQuadrilateral(x1, y1, x2, y2, x3, y3, x4, y4)) {
118             if(y1==y2 || y2==y3 || y3==y4 || y1==y4) {
119                 if((x1-x2)==(x4-x3)&&y2==y1&&y3==y4 || (x1-x4)==(x2-x3)&&y2==y3&&y1==y4) {
120                     return true;
121                 } else {
122                     return false;
123                 }
124             } else {
125                 double k1 = (x1-x2)/(y1-y2);
126                 double k2 = (x2-x3)/(y2-y3);
127                 double k3 = (x3-x4)/(y3-y4);
128                 double k4 = (x1-x4)/(y1-y4);
129                 if(k1 == k3 && k2 == k4) {
130                     return true;
131                 } else {
132                     return false;
133                 }
134             }
135         } else {
136             return false;
137         }
138     }
139     public boolean isRhombus(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {//菱形
140         double l1 = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
141         double l2 = (x3-x2)*(x3-x2)+(y3-y2)*(y3-y2);
142         double l3 = (x3-x4)*(x3-x4)+(y3-y4)*(y3-y4);
143         double l4 = (x1-x4)*(x1-x4)+(y1-y4)*(y1-y4);
144         return (l1==l2&&l1==l3&&l1==l4);
145     }
146     public boolean isRectangle(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {//矩形
147         if(isParallelogram(x1, y1, x2, y2, x3, y3, x4, y4)) {
148             double l1 = (x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
149             double l2 = (x4-x2)*(x4-x2)+(y4-y2)*(y4-y2);
150             return (l1==l2);
151         }
152         else {
153             return false;
154         }
155     }
156     public boolean isSquare(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {//正方形
157         return (isRectangle(x1, y1, x2, y2, x3, y3, x4, y4) && isRhombus(x1, y1, x2, y2, x3, y3, x4, y4));
158     }
159     public boolean isConvexQuadrilateral(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {//凸四边形
160         double l1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
161         double l2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
162         double l3 = Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
163         double l4 = Math.sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4));
164         double l5 = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
165         double l6 = Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2));
166         double p1 = (l1+l2+l5)/2;
167         double p2 = (l3+l4+l5)/2;
168         double p3 = (l1+l4+l6)/2;
169         double p4 = (l3+l2+l6)/2;
170         double area1 = Math.sqrt(p1*(p1-l1)*(p1-l2)*(p1-l5)) + Math.sqrt(p2*(p2-l3)*(p2-l4)*(p2-l5));
171         double area2 = Math.sqrt(p3*(p3-l1)*(p3-l4)*(p3-l6)) + Math.sqrt(p4*(p4-l2)*(p4-l3)*(p4-l6));
172         return (area1 == area2);
173     }
174     public double perimeter(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {//周长
175         double l1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
176         double l2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
177         double l3 = Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
178         double l4 = Math.sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4));
179         return Double.parseDouble(new DecimalFormat("#.###").format(l1+l2+l3+l4));
180     }
181     public double area(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {//面积
182         double l1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
183         double l2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
184         double l3 = Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
185         double l4 = Math.sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4));
186         double l5 = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
187         double l6 = Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2));
188         double p1 = (l1+l2+l5)/2;
189         double p2 = (l3+l4+l5)/2;
190         double p3 = (l1+l4+l6)/2;
191         double p4 = (l3+l2+l6)/2;
192         double area1 = Math.sqrt(p1*(p1-l1)*(p1-l2)*(p1-l5)) + Math.sqrt(p2*(p2-l3)*(p2-l4)*(p2-l5));
193         double area2 = Math.sqrt(p3*(p3-l1)*(p3-l4)*(p3-l6)) + Math.sqrt(p4*(p4-l2)*(p4-l3)*(p4-l6));
194         if(area1 <= area2) {
195             return Double.parseDouble(new DecimalFormat("#.###").format(area1));
196         }
197         else {
198             return Double.parseDouble(new DecimalFormat("#.###").format(area2));
199         }
200     }
201 }

超星学习通的链表练习;

双向链表与单链表相比,多了一种操作——从尾节点开始查找;其他的几乎相同,我的思路是:

在Node类里有两个数据,一个是下一个节点的地址,需要创建New出一个,一个便是Date数据;

存入——在为节点里构造一个新的Node用于指向数据的存放节点,然后数据代入Date;

删除——让上一个节点的为节点跳过Index节点,直接指向下下个地址;

类图如下:

 

 部分代码如下

main

 1 public class Main {
 2 
 3     public static void main(String[] args) {
 4         DoubleLinkedList<String> list1 = new DoubleLinkedList<String>();
 5         System.out.println("\"链表是空\" 是 " + list1.isEmpty());
 6         list1.add("张无忌");
 7         list1.add("韦小宝");
 8         list1.add("杨过");
 9         list1.add("郭靖");
10         list1.printList();
11             //越界数据测试(边界值)
12         System.out.println("\"链表是空\" 是 " + list1.isEmpty());
13         System.out.println("请做出你对双向链表的选择 :"+
14                 "\n1.获取当前链表的长度"+
15                 "\n2.增加元素在链表的末尾"+
16                 "\n3.输入索引,得到索引处的数据"+
17                 "\n4.在指定索引处添加元素"+
18                 "\n5.在指定索引处移除元素");
19         while(true) {
20             Scanner in = new Scanner(System.in);
21             int choice = in.nextInt();
22             switch (choice) {
23                 case 1 :
24                     System.out.println("列表的长度是" + list1.getSize());
25                     break;
26                 case 2 :
27                     System.out.printf("请输入数据 :");
28                     String str2 = in.next();
29                     list1.add(str2);
30                     break;
31                 case 3 :
32                     System.out.printf("请输入索引 :");
33                     int index3 = in.nextInt();
34                     System.out.println("链表第"+index3+"个是:"+list1.getData(index3));
35                     break;
36                 case 4 :
37                     System.out.printf("请输入索引 :");
38                     int index4 = in.nextInt();
39                     System.out.printf("请输入数据 :");
40                     String str4 = in.next();
41                     list1.add(index4,str4);
42                     break;
43                 default :
44                     System.out.printf("请输入索引 :");
45                     int index5 = in.nextInt();
46                     list1.remove(index5);
47             }
48             list1.printList();
49             //越界数据测试(边界值)
50             System.out.println("\"链表是空\" 是 " + list1.isEmpty());
51         }
52     }
53 
54 }

Node

 1 public class Node<E> {
 2 
 3     private E data;//数据域,类型为泛型E
 4 
 5     private Node<E> next = null;//后继引用(指针)
 6 
 7     private Node<E> previous = null;//前驱引用(指针)
 8 
 9     public Node() {
10 
11     }
12 
13     public Node(E data) {
14         super();
15         this.data = data;
16     }
17 
18     public E getData() {
19         return data;
20     }
21 
22     public void setData(E data) {
23         this.data = data;
24     }
25 
26     public Node<E> getNext() {
27         return next;
28     }
29 
30     public void setNext(Node<E> next) {
31         this.next = next;
32     }
33 
34     public Node<E> getPrevious() {
35         return previous;
36     }
37 
38     public void setPrevious(Node<E> previous) {
39         this.previous = previous;
40     }
41 }

DoubleLinkedList

  1 package DoubleLinkedList;
  2 
  3 public class DoubleLinkedList<E> implements DoubleLinkedListImpl<E> {
  4 
  5     private Node<E> head;//头结点,非第一个节点
  6 
  7     private Node<E> curr;//当前节点
  8 
  9     private Node<E> tail;//最后一个节点
 10 
 11     private int size;
 12 
 13     public DoubleLinkedList() {
 14         super();
 15         head = new Node<E>();
 16         curr = head;
 17         tail = head;
 18     }
 19     public boolean isEmpty() {
 20         return (size == 0);
 21     }
 22     public int getSize() {
 23         return size;
 24     }
 25 
 26     public E getData(int index) {
 27         if (index > size || index < 0) {
 28             System.out.println("链表里没有索引\"" + index + "\"!");
 29             return null;
 30         } else {
 31             curr = head;
 32             for (int i = 0; i < index; i ++ )
 33             {
 34                 curr = curr.getNext();
 35             }
 36             return curr.getData();
 37         }
 38     }
 39 
 40     public void remove() {
 41         curr = tail.getPrevious();
 42         tail = curr;
 43         tail.setNext(null);
 44         size --;
 45     }
 46 
 47     public void remove(int index) {
 48         if (index > size || index <= 0) {
 49             System.out.println("链表里没有索引\"" + index + "\"!");
 50         }
 51         else {
 52             if (index == size) {
 53                 tail = tail.getPrevious();
 54                 tail.setNext(null);
 55                 size --;
 56             }
 57             else {
 58                 curr = head;
 59                 for (int i = 0; i < index - 1; i ++) {
 60                     curr = curr.getNext();
 61                 }
 62                 curr.setNext(curr.getNext().getNext());
 63                 curr.getNext().setPrevious(curr);
 64                 size --;
 65             }
 66         }
 67     }
 68     public void add(int index, E theElement) {
 69         if (index > size || index <= 0) {
 70             System.out.println("链表里没有索引\"" + index + "\"!");
 71         }
 72         else {
 73             curr = head;
 74             Node<E> temp = new Node<>(theElement);
 75             for (int i = 0; i < index - 1; i ++) {
 76                 curr = curr.getNext();
 77             }
 78             curr.getNext().setPrevious(temp);
 79             temp.setPrevious(curr);
 80             temp.setNext(curr.getNext());
 81             curr.setNext(temp);
 82             size ++;
 83         }
 84     }
 85     public void add(E element) {
 86         Node<E> temp = new Node<>(element);
 87         tail.setNext(temp);
 88         temp.setPrevious(tail);
 89         tail = temp;
 90         size ++;
 91     }
 92     public void printList() {
 93         curr = head;
 94         if (curr.getNext() == null) {
 95             System.out.println("The list is empty!");
 96         } else {
 97             curr = head.getNext();
 98             for (int i = 0; i < size; i++, curr = curr.getNext()) {
 99                 if (i > 0) {
100                     System.out.print(", ");
101                 }
102                 System.out.print("Node" + (i + 1) + "{" + curr.getData() + "}");
103             }
104             System.out.println();
105         }
106     }
107     public E getFirst() {
108         if (head.getNext() == null) {
109             System.out.println("The first data is null!");
110             return null;
111         }
112         else {
113             return head.getNext().getData();
114         }
115     }
116     public E getLast() {
117         if (tail.getData() == null) {
118             System.out.println("The last data is null!");
119             return null;
120         }
121         else {
122             return tail.getData();
123         }
124     }//当前链表节点数
125 
126 }

OPP期中考试:

除了基本的题意理解,写了三个子类--点point,线Line,面Plane,后来第三题加上了关于ArrayList的GeometryObject容器类,父类Element类,主类main。每个子类基本的Set与Get,还有子类泛型showDate;按照题意便能做出来,其中需要注意的是ArrayList的索引Index需要位序减1。

类图如下:

 

 第三题代码如下:

  1 import java.util.Scanner;
  2 import java.util.ArrayList;
  3 
  4 public class Main {
  5     public static void main(String[] args) {
  6         Scanner in = new Scanner(System.in);
  7         GeometryObject<Element> list = new GeometryObject<>();
  8         int choice = in.nextInt();
  9         while(choice != 0) {
 10             switch(choice) {
 11                 case 1://insert Point object into list
 12                     double x = in.nextDouble();
 13                     double y = in.nextDouble();
 14                     Point p = new Point(x,y);
 15                     list.add(p);
 16                     break;
 17                 case 2://insert Line object into list
 18                     double x1 = in.nextDouble();
 19                     double y1 = in.nextDouble();
 20                     double x2 = in.nextDouble();
 21                     double y2 = in.nextDouble();
 22                     String color = in.next();
 23                     Point p1 = new Point(x1,y1);
 24                     Point p2 = new Point(x2,y2);
 25                     Line line = new Line(p1,p2,color);
 26                     list.add(line);
 27                     break;
 28                 case 3://insert Plane object into list
 29                     String color1 = in.next();
 30                     Plane plane = new Plane(color1);
 31                     list.add(plane);
 32                     break;
 33                 case 4://delete index - 1 object from list
 34                     int index = in.nextInt();
 35                     list.remove(index);
 36             }
 37             choice = in.nextInt();
 38         }
 39         for(int i=0;i<list.getList().size();i++) {
 40             list.getList().get(i).display();
 41         }
 42     }
 43 }
 44 class Point extends Element{
 45     private double x;
 46     private double y;
 47     public Point() {
 48 
 49     }
 50     public Point(double x,double y) {
 51         this.x = x;
 52         this.y = y;
 53     }
 54     public double getX() {
 55         return x;
 56     }
 57 
 58     public void setX(double x) {
 59         this.x = x;
 60     }
 61 
 62     public double getY() {
 63         return y;
 64     }
 65 
 66     public void setY(double y) {
 67         this.y = y;
 68     }
 69 
 70     public void display() {
 71         System.out.println("("+String.format( "%.2f", x)+","+String.format( "%.2f", y)+")");
 72     }
 73 
 74 }
 75 
 76 class Line extends Element{
 77     private Point point1;
 78     private Point point2;
 79     private String color;
 80     public Line() {
 81 
 82     }
 83     public Line(Point point1,Point point2,String string) {
 84         this.point1 = point1;
 85         this.point2 = point2;
 86         this.color = string;
 87     }
 88 
 89     public Point getPoint1() {
 90         return point1;
 91     }
 92 
 93     public void setPoint1(Point point1) {
 94         this.point1 = point1;
 95     }
 96 
 97     public Point getPoint2() {
 98         return point2;
 99     }
100 
101     public void setPoint2(Point point2) {
102         this.point2 = point2;
103     }
104 
105     public String getColor() {
106         return color;
107     }
108 
109     public void setColor(String color) {
110         this.color = color;
111     }
112 
113     public double getDistance() {
114         return Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY()));
115     }
116 
117     public void display() {
118         System.out.println("The line's color is:"+color);
119         System.out.println("The line's begin point's Coordinate is:");
120         point1.display();
121         System.out.println("The line's end point's Coordinate is:");
122         point2.display();
123         System.out.println("The line's length is:"+String.format("%.2f",getDistance()));
124     }
125 
126 }
127 class Plane extends Element{
128     private String color;
129     public Plane() {
130 
131     }
132     public Plane(String color) {
133         this.color = color;
134     }
135 
136     public String getColor() {
137         return color;
138     }
139 
140     public void setColor(String color) {
141         this.color = color;
142     }
143 
144     public void display() {
145         System.out.println("The Plane's color is:"+color);
146     }
147 
148 }
149 abstract class Element {
150     public abstract void display();
151 
152 }
153 class GeometryObject<E> {
154     ArrayList<E> list =new ArrayList<>();
155     public void add(Element element) {
156         list.add((E) element);
157     }
158     public void remove(int index) {
159         list.remove(index);
160     }
161     public ArrayList<E> getList() {
162         return list;
163     }
164 
165 }

 

三.我踩坑与填坑心得。

 

1. 如果想要使用ArrayList和LinkedList,一定要记得索引值要减1;在OPP期中考试的最后一题中,那个测试点我没通过的原因,便是索引值问题。

 

 2. 泛化的应用有时候会更加简便。比如LinkedList需要编写一个节点Node里有两个数据Node及数据类型String,这时候用标准型<E>,会更方便add数据。又比如OPP期中考试的第三题,如果用那几个Point,Line,Plane,我们需要在GeometryObject容器类编写三个ArrayList,把三个类都i给对应起来,不太方便。

 public void add(E element) {
        Node<E> temp = new Node<>(element);
        tail.setNext(temp);
        temp.setPrevious(tail);
        tail = temp;
        size ++;
    }

3. 正则表达式的测试工具很难识别%d,%s等,最好用[0-9][a-zA-Z]来代替测试。

4. 正则和string自带方法可以灵活使用,不一定非得用某一种。

 

四.我的一些不成熟的建议。

 

1. pta题目集5第五题,常识是一个卡号拥有一个独立的账户,题目却是一个人不管有多少账户或卡号,都只拥有一个卡号,这导致了我每次都错。希望老师可以在题意的说明上,更严谨一点;

2. 测试点过多的时候,我真不希望老师连个提示都没有,我是根本过不去,还不知道怎么测试,这样我并不能学到一些对自己有用的知识;

3. 链表老师刚开始要求写的时候,我相信绝大部分人都是网上copy的,因为我们根本想不出这样的方法,还有接口,老师都是提了一下就带过,我们根本理解不了如何写。都是借助网上的视频或者源码进行修改,修改完之后甚至还不知所以然。并不存在老师说明的那种借鉴网上的优秀经验。

 

五.那就总结一下吧。

 

题目开始越来越难,内容开始有深度起来,我听课是越来越不懂,只能勉强跟上,但是当做题之后,突然就发现自己学会了好多东西,借助csdn,借助百度,我们总能获得我们意想不到的方法。老师最近讲lambda,还有map这种我们不需要理解,只会用就可以的方法,我是很喜欢现在的讲课方式的,我自己学的这种往往比老师讲的更牢固。老师最近讲的我毕竟不能马上投入使用,我感觉老师可以进行布置一些课堂测试题,让我们去练习,让我们巩固这些有深度的方法,我觉得还是很有必要的。

posted @ 2022-04-30 17:32  一只不会程序的猿  阅读(43)  评论(0)    收藏  举报