第一次博客作业

一、前言

1.知识点总结

<1>第一次大作业

涉及知识点:Scanner类的使用、if else语句的使用及嵌套、for循环的使用及嵌套、强制类型转换、字符串的提取、字符串和整型的转化、Math类的使用、输入格式的判断,数据的输入输出,及编程的简单思想。

<2>第二次大作业

涉及知识点:字母数字的转换,串口字符解析, String的格式判断与内容提取。

<3>第三次大作业

涉及知识点:类与对象的应用,正则表达式的应用,字符串的比较、字符串的提取、字符串和整型的转化、Math类的使用、一维数组的使用、类的封装、获取字符串长度、输出语句的使用、强制数据类型转换、类的声明与使用、三角类型判断算法、方法的调用与数据返回。

2.题量

<1>第一次大作业:9道简单题,每题代码的平均长度在25行左右

<2>第二次大作业:3道题目,难度较第一次略微提升。每题代码的平均长度在40行左右。

<3>第三次大作业:3道题目,难度如老师所说难度提升幅度较大。代码的长度逐题增加,提升到了4,5百行。

 

3.难度评估

<1>第一次大作业:简单

<2>第二次大作业:适中

<3>第三次大作业:困难

 

二、设计与分析

 

1.算法设计以及思路

<1>第二次作业第二题

7-2 串口字符解析 (40 )

RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5~8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5~8位,具体位数由通信双方提前设置)前加上1位起始位“0”,在有效数据之后加上1位可选的奇偶校验位和1位结束位“1”。请编写程序,模拟串口接收处理程序,注:假定有效数据是8位,奇偶校验位采用奇校验。

 

输入格式:

 

01组成的二进制数据流。例如:11110111010111111001001101111111011111111101111

 

输出格式:

 

过滤掉空闲、起始、结束以及奇偶校验位之后的数据,数据之前加上序号和英文冒号。
如有多个数据,每个数据单独一行显示。
若数据不足11位或者输入数据全1没有起始位,则输出"null data"
若某个数据的结束符不为1,则输出“validate error”
若某个数据奇偶校验错误,则输出“parity check error”
若数据结束符和奇偶校验均不合格,输出“validate error”
如:1101111111111111111111
例如:
1:11101011
2:01001101
3:validate error

 

源码(具体代码分析写在注释里):

 

复制代码
 1 import java.util.Scanner;
 2 public class Main{
 3     public static void main(String[] args){
 4         Scanner input = new Scanner(System.in);
 5         String a = input.next();
 6         int i=0,j=0,k=0,sum=0;
 7         boolean parity=false;
 8         if(a.length()<11) { //判断输入数据是否不足11位
 9             System.out.print("null data");
10             return;
11         }
12         if(a.matches("^[1]*$")) {//判断输入数据全1没有起始位
13             System.out.print("null data");
14             return;
15         }
16         while(i<a.length()-10)
17         {
18             if(a.charAt(i)=='0') {
19                 k++;
20                 System.out.print(k + ":");
21                 sum = 0;
22                 for (j = i + 1; j < i + 9; j++) {
23                     if (a.charAt(j) == '1')
24                         sum++;
25                 }
26                 if(a.charAt(i+9)=='0')
27                 {
28                     if(sum%2==0)
29                         parity=false;
30                     else 
31                         parity=true;
32                 }
33                 else
34                 {
35                     if(sum%2==0)
36                         parity=true;
37                     else
38                         parity=false;
39                 }
40                 if (a.charAt(i + 10) != '1') {
41                     System.out.println("validate error");
42                 } else {
43                     if (parity==false) {
44                         System.out.println("parity check error");
45                     } else {
46                         for (j = i + 1; j < i + 9; j++) {
47                             System.out.print(a.charAt(j));
48                         }
49                         System.out.print("\n");
50                     }
51                 }
52                 i = i + 10;
53             }
54             else
55                 i++;
56         }
57     }
58 }
View Code
复制代码

 

 

 

 

<2>第三次作业第一题

7-1 点线形系列1-计算两点之间的距离 (10 )

输入两个点的坐标,计算两点之间的距离

输入格式:

4double类型的实数,两个点的xy坐标,依次是x1y1x2y2,两个点的坐标之间以空格分隔,每个点的x,y坐标以英文“,”分隔。例如:0,0 1,10.1,-0.3 +3.5,15.6
若输入格式非法,输出"Wrong Format"
若输入格式合法但坐标点的数量超过两个,输出“wrong number of points”

输出格式:

计算所得的两点之间的距离。例如:1.4142135623730951

 

 

 源码:

复制代码
 1 import java.util.Scanner;
 2 class Point {  //点类
 3     double x,y,distance;
 4     Point(String x,String y)
 5     {
 6         this.x=Double.valueOf(x);
 7         this.y=Double.valueOf(y);
 8     }
 9     boolean Same(Point b) {  //判断两个点是否相同
10         if (this.x == b.x && this.y == b.y)
11             return true;
12         return false;
13     }
14     void Distance(Point b) //计算两点之间的距离
15     {
16         distance=Math.sqrt((this.x-b.x)*(this.x-b.x)+(this.y-b.y)*(this.y-b.y));
17         System.out.print(distance);
18     }
19 }
20 public class Main {
21     public static void main(String[] args) {
22         Scanner in = new Scanner(System.in);
23         String input = in.nextLine();
24         String point[] = input.split(" ");
25         String num[] = null;
26         for(String i:point) {
27             num = i.split(",");
28             for(String j:num) {
29                 //正则表达式
30                 if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) {
31                     System.out.print("Wrong Format");
32                     System.exit(0);
33                 }
34             }
35         }
36 
37         if(point.length!=2) {
38             System.out.print("wrong number of points");
39             System.exit(0);
40         }
41         num = point[0].split(",");
42         Point a=new Point(num[0],num[1]);
43         num = point[1].split(",");
44         Point b=new Point(num[0],num[1]);
45 
46         if(a.Same(b)){
47             System.out.print("Wrong Format");
48             return;
49         }
50         a.Distance(b);
51     }
52 }
View Code
复制代码

Main类:

 

 

 Point类:

 

 

 

 

<3>第三次作业第二题

7-2 点线形系列2-线的计算 (42 )

用户输入一组选项和数据,进行与直线有关的计算。选项包括:
1:输入两点坐标,计算斜率,若线条垂直于X轴,输出"Slope does not exist"
2:输入三个点坐标,输出第一个点与另外两点连线的垂直距离。
3:输入三个点坐标,判断三个点是否在一条线上,输出true或者false
4:输入四个点坐标,判断前两个点所构成的直线与后两点构成的直线是否平行,输出true或者false.
5:输入四个点坐标,计算输出前两个点所构成的直线与后两点构成的直线的交点坐标,xy坐标之间以英文分隔",",并输出交叉点是否在两条线段之内(不含四个端点)的判断结果(true/false),判断结果与坐标之间以一个英文空格分隔。若两条线平行,没有交叉点,则输出"is parallel lines,have no intersection point"

输入格式:

基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y
例如:1:0,0 1,1
如果不符合基本格式,输出"Wrong Format"
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"
不论哪个选项,如果格式、点数量都符合要求,但构成任一条线的两个点坐标重合,输出"points coincide"

输出格式:

见题目描述。

 源码:

复制代码
  1 import java.util.Objects;
  2 import java.util.Scanner;
  3 class Point { //点类
  4     double x,y,distance;
  5      Point(String x,String y)
  6     {
  7         this.x=Double.valueOf(x);
  8         this.y=Double.valueOf(y);
  9     }
 10      Point(Double x,Double y)
 11     {
 12         this.x=x;
 13         this.y=y;
 14     }
 15      boolean Same(Point b) { //判断两点是否相同
 16         if (this.x == b.x && this.y == b.y)
 17             return true;
 18         else
 19         return false;
 20     }
 21      void Distance(Point b)//求两点间的距离
 22     {
 23         distance=Math.sqrt((this.x-b.x)*(this.x-b.x)+(this.y-b.y)*(this.y-b.y));
 24         System.out.print(distance);
 25     }
 26      double Xielv(Point b)//求直线的斜率
 27     {
 28         double xielv=(b.y-this.y)/(b.x-this.x);
 29         return xielv;
 30     }
 31      void Output(Point b)
 32     {
 33         if(this.x==b.x)
 34         {
 35             System.out.print("Slope does not exist");
 36             return;
 37         }
 38          if(this.y==b.y)
 39         {
 40             System.out.print("0");
 41             return;
 42         }
 43         System.out.print(this.Xielv(b));
 44         return;
 45     }
 46      Double Czdistance(Point b,Point c) //求点到直线的垂直距离
 47     {
 48         Double L = Math.abs((b.y-c.y)*this.x+(c.x-b.x)*this.y+b.x*c.y-c.y*c.x)/Math.sqrt((b.y-c.y) *(b.y-c.y) +(b.x-c.x)*(b.x-c.x));
 49         return L;
 50     }
 51      boolean straight_line(Point b,Point c) //判断三个点是否在一条直线上
 52     {
 53         if(this.Xielv(b)==this.Xielv(c))
 54             return true;
 55         else
 56             return false;
 57     }
 58 
 59 }
 60 
 61 
 62  class Line { //线类
 63     Point p1,p2;
 64 
 65      Line(Point a,Point b)
 66     {
 67         this.p1=a;
 68         this.p2=b;
 69     }
 70      boolean parallel_x() //判断直线是否平行于x
 71     {
 72         if(this.p1.y==this.p2.y)
 73             return true;
 74         else
 75             return false;
 76     }
 77      boolean parallel_y()//判断直线是否平行于y
 78     {
 79         if(this.p1.x==this.p2.x)
 80             return true;
 81         else
 82             return false;
 83     }
 84       Double getXielv() //求直线斜率
 85     {
 86         if(this.parallel_y())
 87             return null;
 88         else if(this.parallel_x())
 89         {
 90             double xielv=0;
 91             return xielv;
 92         }
 93         else
 94             return (this.p1.y-this.p2.y)/(this.p1.x-this.p2.x);
 95 
 96     }
 97      boolean parallel(Line p) //判断两条直线是否平行
 98     {
 99         if(this.parallel_x()&&p.parallel_x())
100             return true;
101         else if(this.parallel_y()&&p.parallel_y())
102             return true;
103         else if(Objects.equals(p.getXielv(), this.getXielv()))
104             return true;
105         else
106             return false;
107     }
108      public Point getjiaodian(Line q)//获取交点
109     {
110 //        y=kx+b
111         Point c=new Point(1.0, 1.0);
112         Double k1,b1,k2,b2;
113         if(this.parallel_x()&&q.parallel_y())
114         {
115             b1=this.p1.y;
116             b2=q.p1.x;
117             c.x=b2;
118             c.y=b1;
119             return c;
120         }
121         else if(this.parallel_y()&&!q.parallel_x()) {
122             k2=q.getXielv();
123             b2=q.p1.y-q.p1.x*k2;
124             c.x = this.p1.x;
125             c.y = c.x*k2+b2;
126             return c;
127         }
128         else if(q.parallel_y()&&!this.parallel_x()) {
129             k1 = this.getXielv();
130             b1 = this.p1.y - this.p1.x * k1;
131             c.x = q.p1.x;
132             c.y = c.x*k1+b1;
133             return c;
134         }
135         else if(q.parallel_x()&&this.parallel_y())
136         {
137             b2=q.p1.y;
138             b1=this.p1.x;
139             c.x=b1;
140             c.y=b2;
141             return c;
142         }
143         else if(this.parallel_x()&&!q.parallel_y())
144         {
145             b1=this.p1.y;
146             k2=q.getXielv();
147             b2=q.p1.y-q.p1.x*k2;
148             c.y=b1;
149             c.x=(b1-b2)/k2;
150             return c;
151         }
152         else if(q.parallel_x()&&!this.parallel_y())
153         {
154             b2=this.p1.y;
155             k1=this.getXielv();
156             b1=this.p1.y-this.p1.x*k1;
157             c.y=b2;
158             c.x=(b2-b1)/k1;
159             return c;
160         }
161         else{
162             k1 = this.getXielv();
163             b1 = this.p1.y - this.p1.x * k1;
164             k2 = q.getXielv();
165             b2 = q.p1.y - q.p1.x * k2;
166             c.x = (b2 - b1) / (k1 - k2);
167             c.y = k1 * c.x + b1;
168             return c;
169         }
170 
171     }
172      boolean init(Double x,Double y,Line q)//判断交点是否在线段内
173     {
174          if((y<Math.max(this.p1.y,this.p2.y)||y<Math.max(q.p1.y,q.p2.y))&&((y>Math.min(this.p1.y,this.p2.y)||y>Math.min(q.p1.y,q.p2.y))))
175         {
176             return true;
177         }
178         else
179             return false;
180     }
181 
182 }
183 public class Main {
184     public static void judge(String[] option)
185     {
186         String shuju=option[1];
187         String[] point = shuju.split(" ");
188         String[] num = null;
189         for(String i:point) {
190             num = i.split(",");
191             for(String j:num) {
192                 //正则表达式
193                 if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) {
194                     System.out.print("Wrong Format");
195                     System.exit(0);
196                 }
197             }
198         }
199     }
200     public static Point getIntersectPoint(Point p1, Point p2, Point p3, Point p4){ //获取交点
201 
202         double A1=p1.y-p2.y;
203         double B1=p2.x-p1.x;
204         double C1=A1*p1.x+B1*p1.y;
205 
206         double A2=p3.y-p4.y;
207         double B2=p4.x-p3.x;
208         double C2=A2*p3.x+B2*p3.y;
209 
210         double det_k=A1*B2-A2*B1;
211 
212         if(Math.abs(det_k)<0.00001){
213             return null;
214         }
215 
216         double a=B2/det_k;
217         double b=-1*B1/det_k;
218         double c=-1*A2/det_k;
219         double d=A1/det_k;
220 
221         double x=a*C1+b*C2;
222         double y=c*C1+d*C2;
223 
224         return  new Point(x,y);
225     }
226     public static void main(String[] args) {
227         Scanner in = new Scanner(System.in);
228         String input = in.nextLine();
229         if(!input.matches("^[1-5][:](([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))[,]([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))\\s?)+$")){
230             System.out.println("Wrong Format");
231             return;
232         }
233         String[] option =input.split(":");
234         String shuju=option[1];
235         String[] point = shuju.split(" ");
236         String[] num = null;
237         judge(option);
238         switch(option[0])
239         {
240             case "1":
241                 if(point.length!=2) {
242                 System.out.print("wrong number of points");
243                 System.exit(0);
244             }
245                 num = point[0].split(",");
246                 Point a=new Point(num[0],num[1]);
247                 num = point[1].split(",");
248                 Point b=new Point(num[0],num[1]);
249                 if(a.Same(b)){
250             System.out.print("points coincide");
251             return;
252         }
253                 a.Output(b);
254                 break;
255             case "2":
256                 if(point.length!=3) {
257                     System.out.print("wrong number of points");
258                     System.exit(0);
259                 }
260                 num = point[0].split(",");
261                 a = new Point(num[0], num[1]);
262                 num = point[1].split(",");
263                 b = new Point(num[0], num[1]);
264                 num = point[2].split(",");
265                 Point c=new Point(num[0],num[1]);
266                 if(b.Same(c)){
267             System.out.print("points coincide");
268             return;
269         }
270                 System.out.print(a.Czdistance(b,c));
271                 break;
272             case "3":
273                 if(point.length!=3) {
274                     System.out.print("wrong number of points");
275                     System.exit(0);
276                 }
277                 num = point[0].split(",");
278                 a = new Point(num[0], num[1]);
279                 num = point[1].split(",");
280                 b = new Point(num[0], num[1]);
281                 num = point[2].split(",");
282                 c = new Point(num[0], num[1]);
283                 if(a.Same(b)||a.Same(c)||b.Same(c)){
284             System.out.print("points coincide");
285                     System.exit(0);
286                 }
287                 System.out.print(a.straight_line(b,c));
288                 break;
289             case "4":
290                 if(point.length!=4) {
291                     System.out.print("wrong number of points");
292                     System.exit(0);
293                 }
294                 num = point[0].split(",");
295                 a = new Point(num[0], num[1]);
296                 num = point[1].split(",");
297                 b = new Point(num[0], num[1]);
298                 num = point[2].split(",");
299                 c = new Point(num[0], num[1]);
300                 num = point[3].split(",");
301                 Point d=new Point(num[0], num[1]);
302                 if(a.Same(b)||a.Same(c)||a.Same(d)||b.Same(c)||b.Same(d)||c.Same(d)){
303             System.out.print("points coincide");
304                     System.exit(0);
305                 }
306                 Line p=new Line(a,b);
307                 Line q=new Line(c,d);
308                 System.out.print(p.parallel(q));
309                 break;
310             case "5":
311                 if(point.length!=4) {
312                     System.out.print("wrong number of points");
313                     System.exit(0);
314                 }
315                 num = point[0].split(",");
316                 a = new Point(num[0], num[1]);
317                 num = point[1].split(",");
318                 b = new Point(num[0], num[1]);
319                 num = point[2].split(",");
320                 c = new Point(num[0], num[1]);
321                 num = point[3].split(",");
322                 d=new Point(num[0], num[1]);
323                 if(a.Same(b)||a.Same(c)||a.Same(d)||b.Same(c)||b.Same(d)||c.Same(d)){
324             System.out.print("points coincide");
325                     System.exit(0);
326                 }
327                  p=new Line(a,b);
328                  q=new Line(c,d);
329                  if(p.parallel(q)) {
330                      System.out.print("is parallel lines,have no intersection point");
331                      System.exit(0);
332                  }
333                  else
334                  {
335                      System.out.print(getIntersectPoint(a,b,c,d).x+","+getIntersectPoint(a,b,c,d).y+" "+p.init(getIntersectPoint(a,b,c,d).x,getIntersectPoint(a,b,c,d).y,q));
336                  }
337                  break;
338             default:System.out.print("Wrong Format");
339                 break;
340         }
341     }
342 }
View Code
复制代码

 

Point类:

 

 xian类:

 

 Main类:

 

<4>第三次作业第三题

7-3 点线形系列3-三角形的计算 (48 )

 用户输入一组选项和数据,进行与三角形有关的计算。选项包括:

1:输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果输出true/false,两个结果之间以一个英文空格符分隔。

2:输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。

3:输入三个点坐标,输出是钝角、直角还是锐角三角形,依次输出三个判断结果(true/false),以一个英文空格分隔,

4:输入五个点坐标,输出前两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积。若直线与三角形一条线重合,输出"The point is on the edge of the triangle"

5:输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。

必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外。若点在三角形的某条边上,输出"on the triangle"

输入格式:

基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的xy坐标之间以英文","分隔,点与点之间以一个英文空格分隔。

输出格式:

基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"
如果输入的三个点无法构成三角形,输出"data error"
注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.3333331.0按格式输出为1.0

选项4中所输入线的两个点坐标重合,输出"points coincide"

 源码:

复制代码
  1 import java.util.Objects;
  2 import java.util.Scanner;
  3 import java.math.BigDecimal;
  4 import java.math.RoundingMode;
  5 class Point {
  6     double x,y,distance;
  7      Point(String x,String y)
  8     {
  9         this.x=Double.valueOf(x);
 10         this.y=Double.valueOf(y);
 11     }
 12     Point(Double x,Double y)
 13     {
 14         this.x=x;
 15         this.y=y;
 16     }
 17      boolean Same(Point b) {//判断两点是否重合
 18         if (this.x == b.x && this.y == b.y)
 19             return true;
 20         else
 21             return false;
 22     }
 23      Double Distance(Point b)//计算两点间的距离
 24     {
 25         distance=Math.sqrt((this.x-b.x)*(this.x-b.x)+(this.y-b.y)*(this.y-b.y));
 26         return distance;
 27     }
 28      double Xielv(Point b)//计算两点所构成直线的斜率
 29     {
 30         double xielv=(b.y-this.y)/(b.x-this.x);
 31         return xielv;
 32     }
 33      void Output(Point b)
 34     {
 35         if(this.Same(b)){
 36             System.out.print("points coincide");
 37             return;
 38         }
 39         else if(this.x==b.x)
 40         {
 41             System.out.print("Slope does not exist");
 42             return;
 43         }
 44         else
 45         {
 46             System.out.print(this.Xielv(b));
 47             return;
 48         }
 49     }
 50      Double Czdistance(Point b,Point c)//计算点到直线的垂直距离
 51     {
 52         Double L = Math.abs((b.y-c.y)*this.x+(c.x-b.x)*this.y+b.x*c.y-c.y*c.x)/Math.sqrt((b.y-c.y) *(b.y-c.y) +(b.x-c.x)*(b.x-c.x));
 53         return L;
 54     }
 55      boolean straight_line(Point b,Point c)//判断三个点是否在一条直线上
 56     {
 57         if(this.Xielv(b)==this.Xielv(c))
 58             return true;
 59         else
 60             return false;
 61     }
 62 
 63 }
 64 
 65  class Line {
 66     //    Double A,B,C;
 67     Point p1,p2;
 68     Double length;
 69 
 70      Line(Point a,Point b)
 71     {
 72         this.p1=a;
 73         this.p2=b;
 74         this.length=a.Distance(b);
 75     }
 76      boolean parallel_x()//判断直线是否平行x轴
 77     {
 78         if(this.p1.y==this.p2.y)
 79             return true;
 80         else
 81             return false;
 82     }
 83      boolean parallel_y()//判断直线是否平行y轴
 84     {
 85         if(this.p1.x==this.p2.x)
 86             return true;
 87         else
 88             return false;
 89     }
 90       Double getXielv()//计算直线的斜率
 91     {
 92         if(this.parallel_y())
 93             return null;
 94         else if(this.parallel_x())
 95         {
 96             double xielv=0;
 97             return xielv;
 98         }
 99         else
100             return (this.p1.y-this.p2.y)/(this.p1.x-this.p2.x);
101 
102     }
103      boolean parallel(Line p)//判断两条直线是否平行
104     {
105         if(this.parallel_x()&&p.parallel_x())
106             return true;
107         else if(this.parallel_y()&&p.parallel_y())
108             return true;
109         else if(Objects.equals(p.getXielv(), this.getXielv()))
110             return true;
111         else
112             return false;
113     }
114      boolean isin_line(Point a)
115     {
116         if(Math.abs(a.Czdistance(this.p1,this.p2))<0.001)
117             return true;
118         else
119             return false;
120     }
121      Point getjiaodian(Line q)
122     {
123 //        y=kx+b
124         Point c=new Point(1.0, 1.0);
125         Double k1,b1,k2,b2;
126         if(this.parallel_x()&&q.parallel_y())
127         {
128             b1=this.p1.y;
129             b2=q.p1.x;
130             c.x=b2;
131             c.y=b1;
132             return c;
133         }
134         else if(this.parallel_y()&&!q.parallel_x()) {
135             k2=q.getXielv();
136             b2=q.p1.y-q.p1.x*k2;
137             c.x = this.p1.x;
138             c.y = c.x*k2+b2;
139             return c;
140         }
141         else if(q.parallel_y()&&!this.parallel_x()) {
142             k1 = this.getXielv();
143             b1 = this.p1.y - this.p1.x * k1;
144             c.x = q.p1.x;
145             c.y = c.x*k1+b1;
146             return c;
147         }
148         else if(q.parallel_x()&&this.parallel_y())
149         {
150             b2=q.p1.y;
151             b1=this.p1.x;
152             c.x=b1;
153             c.y=b2;
154             return c;
155         }
156         else if(this.parallel_x()&&!q.parallel_y())
157         {
158             b1=this.p1.y;
159             k2=q.getXielv();
160             b2=q.p1.y-q.p1.x*k2;
161             c.y=b1;
162             c.x=(b1-b2)/k2;
163             return c;
164         }
165         else if(q.parallel_x()&&!this.parallel_y())
166         {
167             b2=this.p1.y;
168             k1=this.getXielv();
169             b1=this.p1.y-this.p1.x*k1;
170             c.y=b2;
171             c.x=(b2-b1)/k1;
172             return c;
173         }
174         else{
175             k1 = this.getXielv();
176             b1 = this.p1.y - this.p1.x * k1;
177             k2 = q.getXielv();
178             b2 = q.p1.y - q.p1.x * k2;
179             c.x = (b2 - b1) / (k1 - k2);
180             c.y = k1 * c.x + b1;
181             return c;
182         }
183 
184     }
185 
186      boolean init(Double x,Double y,Line q)//判断交点是否在两条线段内
187     {
188         if((y<=Math.max(this.p1.y,this.p2.y)||y<=Math.max(q.p1.y,q.p2.y))&&((y>=Math.min(this.p1.y,this.p2.y)||y>=Math.min(q.p1.y,q.p2.y))))
189         {
190             return true;
191         }
192         else
193             return false;
194     }
195 
196 }
197  class Triangle {
198     Line l1, l2, l3;
199 
200      Triangle(Line l1, Line l2, Line l3) {
201         this.l1 = l1;
202         this.l2 = l2;
203         this.l3 = l3;
204     }
205 
206      void whattriangle() {
207         boolean db = false;
208         boolean dy = false;
209         if (Objects.equals(this.l1.length, this.l2.length) && Objects.equals(this.l1.length, this.l3.length))
210             db = true;
211         else if (Objects.equals(this.l1.length, this.l2.length) || Objects.equals(this.l1.length, this.l3.length) || Objects.equals(this.l2.length, this.l3.length)) {
212             dy = true;
213         } else {
214             db = false;
215             dy = false;
216         }
217         if (db)
218             System.out.print("true true");
219         else if (dy)
220             System.out.print("true false");
221         else
222             System.out.print("false false");
223     }
224 
225      Double get_zc() {
226         return (this.l1.length + this.l2.length + this.l3.length);
227     }
228 
229      Double get_area() {
230         Double s = this.get_zc() / 2;
231         Double area=Math.sqrt((s * (s - this.l1.length) * (s - this.l2.length) * (s - this.l3.length)));
232         return area;
233     }
234 
235      Double get_zx_x() {
236         Double x1 = this.l1.p1.x;
237         Double x2 = this.l1.p2.x;
238         Double x3 = this.l2.p2.x;
239 
240         Double X1 = (x1 + x2 + x3) / 3;
241         return X1;
242     }
243      Double get_zx_y()
244     {
245         Double y1 = this.l1.p1.y;
246         Double y2 = this.l1.p2.y;
247         Double y3 = this.l2.p2.y;
248         Double Y1 = (y1 + y2 + y3) / 3;
249         return Y1;
250     }
251      void is_jiao()
252     {
253         if(Math.abs(Math.pow(this.l1.length,2)+Math.pow(this.l2.length,2)-Math.pow(this.l3.length,2))<0.0001||
254             Math.abs(Math.pow(this.l1.length,2)+Math.pow(this.l3.length,2)-Math.pow(this.l2.length,2))<0.0001||
255             Math.abs(Math.pow(this.l2.length,2)+Math.pow(this.l3.length,2)-Math.pow(this.l1.length,2))<0.0001)
256         System.out.print("false true false");
257         else if(Math.pow(this.l1.length,2)+Math.pow(this.l2.length,2)<Math.pow(this.l3.length,2)||
258                 Math.pow(this.l1.length,2)+Math.pow(this.l3.length,2)<Math.pow(this.l2.length,2)||
259                 Math.pow(this.l2.length,2)+Math.pow(this.l3.length,2)<Math.pow(this.l1.length,2))
260             System.out.print("true false false");
261         else
262             System.out.print("false false true");
263     }
264 
265  void operate4(Line a)
266 {
267 //    if(this.l1.parallel(a)&&a.p1.Czdistance(this.l1.p1,this.l1.p2)==0||
268 //            this.l2.parallel(a)&&a.p1.Czdistance(this.l2.p1,this.l1.p2)==0||
269 //            this.l3.parallel(a)&&a.p1.Czdistance(this.l3.p1,this.l1.p2)==0)
270     if((a.isin_line(this.l1.p1)&&a.isin_line(this.l1.p2))||(a.isin_line(this.l2.p1)&&a.isin_line(this.l2.p2))||(a.isin_line(this.l3.p1)&&a.isin_line(this.l3.p2)))
271         System.out.print("The point is on the edge of the triangle");
272 //    else if(!this.isinit(this.l1,a)&&!this.isinit(this.l2,a)&&!this.isinit(this.l3,a))
273 //    {
274 //        System.out.print("0");
275 //        return;
276 //    }
277     else {
278         if (this.isinit(a, this.l1) && this.isinit(a, this.l2)) {
279             Double area1 = getarea2(this.l1.p1, Main.getIntersectPoint(a.p1, a.p2, this.l1.p1, this.l1.p2), Main.getIntersectPoint(a.p1, a.p2, this.l2.p1, this.l2.p2));
280             Double area2 = this.get_area() - area1;
281             System.out.print("2 " + Main.output(area1) + " " + Main.output(area2));
282             return;
283         } else if (this.isinit(a, this.l1) && this.isinit(a, this.l3)) {
284             Double area1 = getarea2(this.l1.p2, Main.getIntersectPoint(a.p1, a.p2, this.l1.p1, this.l1.p2), Main.getIntersectPoint(a.p1, a.p2, this.l3.p1, this.l3.p2));
285             Double area2 = this.get_area() - area1;
286             System.out.print("2 " + Main.output(area1) + " " + Main.output(area2));
287             return;
288         } else if (this.isinit(a, this.l2) && this.isinit(a, this.l3)) {
289             Double area1 = getarea2(this.l2.p2, Main.getIntersectPoint(a.p1, a.p2, this.l2.p1, this.l2.p2), Main.getIntersectPoint(a.p1, a.p2, this.l3.p1, this.l3.p2));
290             Double area2 = this.get_area() - area1;
291             System.out.print("2 " + Main.output(area1) + " " + Main.output(area2));
292             return;
293         } else
294             System.out.print("1");
295     }
296 
297 }
298      boolean isinit(Line p,Line q)
299     {
300         if(p.init(Main.getIntersectPoint(p.p1,p.p2,q.p1,q.p2).x,Main.getIntersectPoint(p.p1,p.p2,q.p1,q.p2).y,q))
301             return true;
302         else
303             return false;
304     }
305      Double getarea2(Point a,Point b,Point c)
306     {
307         Line p=new Line(a,b);
308         Line q=new Line(a,c);
309         Line r=new Line(b,c);
310         Triangle A=new Triangle(p,q,r);
311         return A.get_area();
312     }
313      void operate5(Point a,Point b,Point c,Point d)
314     {
315         if(Math.abs(getarea2(a,b,c)+getarea2(a,b,d)+getarea2(a,c,d)-getarea2(b,c,d))<0.0001)
316             System.out.print("in the triangle");
317         else
318             System.out.print("out of the triangle");
319     }
320 
321 }
322 public class Main {
323     public static void judge(String[] option)
324     {
325         String shuju=option[1];
326         String[] point = shuju.split(" ");
327         String[] num = null;
328         for(String i:point) {
329             num = i.split(",");
330             for(String j:num) {
331                 //正则表达式
332                 if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) {
333                     System.out.print("Wrong Format");
334                     System.exit(0);
335                 }
336             }
337         }
338     }
339     public static Point getIntersectPoint(Point p1, Point p2, Point p3, Point p4){
340 
341         double A1=p1.y-p2.y;
342         double B1=p2.x-p1.x;
343         double C1=A1*p1.x+B1*p1.y;
344 
345         double A2=p3.y-p4.y;
346         double B2=p4.x-p3.x;
347         double C2=A2*p3.x+B2*p3.y;
348 
349         double det_k=A1*B2-A2*B1;
350 
351 //        if(Math.abs(det_k)<0.00001){
352 //            return null;
353 //        }
354 
355         double a=B2/det_k;
356         double b=-1*B1/det_k;
357         double c=-1*A2/det_k;
358         double d=A1/det_k;
359 
360         double x=a*C1+b*C2;
361         double y=c*C1+d*C2;
362 
363         return  new Point(x,y);
364     }
365     public static Double output(Double data)
366     {
367         double a = data;
368 
369         BigDecimal bigDecimal = new BigDecimal(a);
370         double res = bigDecimal.setScale(6, RoundingMode.HALF_UP).doubleValue();
371         return res;
372     }
373     public static void main(String[] args) {
374         Scanner in = new Scanner(System.in);
375         String input = in.nextLine();
376         if(!input.matches("^[1-5][:](([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))[,]([+-]?(0|(0\\.\\d+)|[1-9][0-9]*(\\.\\d+)?))\\s?)+$"))
377         {
378             System.out.println("Wrong Format");
379             return;
380         }
381         String[] option =input.split(":");
382         String shuju=option[1];
383         String[] point = shuju.split(" ");
384         String[] num = null;
385         switch(option[0])
386         {
387             case "1":
388                 if(point.length!=3) {
389                     System.out.print("wrong number of points");
390                     System.exit(0);
391                 }
392                 num = point[0].split(",");
393                 Point a=new Point(num[0],num[1]);
394                 num = point[1].split(",");
395                 Point b=new Point(num[0],num[1]);
396                 num = point[2].split(",");
397                 Point c = new Point(num[0], num[1]);
398                 if(a.Same(b)||a.Same(c)||b.Same(c)) {
399                     System.out.print("points coincide");
400                     return;
401                 }
402                 Line p=new Line(a,b);
403                 Line q=new Line(a,c);
404                 Line r=new Line(b,c);
405                 if(p.parallel(q)){
406                     System.out.print("data error");
407                     return;
408                 }
409                 Triangle A=new Triangle(p,q,r);
410                 A.whattriangle();
411                 break;
412             case "2":
413                 if(point.length!=3) {
414                     System.out.print("wrong number of points");
415                     System.exit(0);
416                 }
417                 num = point[0].split(",");
418                  a=new Point(num[0],num[1]);
419                 num = point[1].split(",");
420                  b=new Point(num[0],num[1]);
421                 num = point[2].split(",");
422                  c = new Point(num[0], num[1]);
423                 if(a.Same(b)||a.Same(c)||b.Same(c)) {
424                     System.out.print("points coincide");
425                     return;
426                 }
427                  p=new Line(a,b);
428                  q=new Line(a,c);
429                  r=new Line(b,c);
430                 if(p.parallel(q)){
431                     System.out.print("data error");
432                     return;
433                 }
434                  A=new Triangle(p,q,r);
435                 System.out.print(output(A.get_zc())+" "+output(A.get_area())+" "+output(A.get_zx_x())+","+output(A.get_zx_y()));
436                 break;
437             case "3":
438                 if(point.length!=3) {
439                     System.out.print("wrong number of points");
440                     System.exit(0);
441                 }
442                 num = point[0].split(",");
443                 a=new Point(num[0],num[1]);
444                 num = point[1].split(",");
445                 b=new Point(num[0],num[1]);
446                 num = point[2].split(",");
447                 c = new Point(num[0], num[1]);
448                 if(a.Same(b)||a.Same(c)||b.Same(c)) {
449                     System.out.print("points coincide");
450                     return;
451                 }
452                 p=new Line(a,b);
453                 q=new Line(a,c);
454                 r=new Line(b,c);
455                 if(p.parallel(q)){
456                     System.out.print("data error");
457                     return;
458                 }
459                 A=new Triangle(p,q,r);
460                 A.is_jiao();
461                 break;
462             case "4":
463                 if(point.length!=5) {
464                     System.out.print("wrong number of points");
465                     System.exit(0);
466                 }
467                 num = point[0].split(",");
468                 a=new Point(num[0],num[1]);
469                 num = point[1].split(",");
470                 b=new Point(num[0],num[1]);
471                 num = point[2].split(",");
472                 c = new Point(num[0], num[1]);
473                 num = point[3].split(",");
474                 Point d = new Point(num[0], num[1]);
475                 num = point[4].split(",");
476                 Point e = new Point(num[0], num[1]);
477                 if(a.Same(b)) {
478                     System.out.print("points coincide");
479                     return;
480                 }
481                 Line s=new Line(a,b);
482                 p=new Line(c,d);
483                 q=new Line(c,e);
484                 r=new Line(d,e);
485                 if(p.parallel(q)||p.parallel(r)||q.parallel(r)){
486                     System.out.print("data error");
487                     return;
488                 }
489                 A=new Triangle(p,q,r);
490                 A.operate4(s);
491 
492                 break;
493             case "5":
494                 if(point.length!=4) {
495                     System.out.print("wrong number of points");
496                     System.exit(0);
497                 }
498                 num = point[0].split(",");
499                 a=new Point(num[0],num[1]);
500                 num = point[1].split(",");
501                 b=new Point(num[0],num[1]);
502                 num = point[2].split(",");
503                 c = new Point(num[0], num[1]);
504                 num = point[3].split(",");
505                 d = new Point(num[0], num[1]);
506                 if(a.Same(b)) {
507                     System.out.print("points coincide");
508                     return;
509                 }
510                 p=new Line(b,c);
511                 q=new Line(b,d);
512                 r=new Line(c,d);
513                 if(a.straight_line(b,c)||a.straight_line(b,d)||a.straight_line(c,d))
514                 {
515                     System.out.print("on the triangle");
516                     return;
517                 }
518                 A=new Triangle(p,q,r);
519                 A.operate5(a,b,c,d);
520                break;
521             default:System.out.print("Wrong Format");
522                 break;
523         }
524 
525     }
526 }
View Code
复制代码

 

Point类:

 

 Xian类:

 

 Triangle类:

 

 Main类:

 

三、踩坑心得

1、第一次大作业

<1>开始没有分类写,导致写后面的题目难度越来越大,后来着手分类写,思路也更加清晰。

<2>用正则表达式直接就能解决输入格式错误的所有情况。

<3>有时计算机计算会出现一小点误差,我们避免用'=='而用Math.abs(nextGuess - lastGuess)< 0.00001

Objects.equals(this.l1.length, this.l2.length)许会得到想要的答案。

比如说判断两条线段相等的时候用this.l1.length==this.l2.length的话运行结果返回的就是false,用下面代码就返回true。

 

 

 

2、第二次大作业

<1>开始把线的斜率归属于线的属性里,没有考虑到有的直线没有斜率,导致后面计算交点坐标的时候输出NAN(无效操作)。

<2>获取输入时,要注意nextLine()方法返回的是Enter键之前的所有字符,它可以得到带空格的字符串的。而next()会自动消去有效字符前的空格,只返回输入的字符,不能得到带空格的字符串。

3、第三次大作业

<1>正则表达式的使用还不是很熟练,还需要认真学习。

<2>很多地方都能用上之前类的代码,能省下很多事情。

 

<3>在解决很多问题的时候都有很多种方法,我们在写代码之前可以先考虑用更方便的方法,对节省时间有很大的帮助。

 

 

四、改进建议

 

还有很多代码可以进行封装,降低其圈复杂度,代码的优化也有待提高。

 

五、总结

 

       1.对于只会C语言的我而言,面对java基础语法学习,反而感觉很难,其实其中最大的问题不是语法难,而是一种编程思想的转变。面向过程就是把你的代码封装成函数,然后依次去做一件事情,面向过程是把你要做的事情抽象成对象,告诉对象去做。所以要想学好java入门,必须知道类和对象的概念。

 

       2.在对于JAVA庞大的类库以及相关功能还需要进一步的了解与学习,提高工作效率,方便后续功能更好更快捷的实现。

 

       3.面向对象对复杂的题目有很大的便捷性。

 

 

 

 

 

 

 

 

 

 

 

 

posted @   我等你许久了  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示