第一次博客作业
前言:
第一次作业:刚刚接触JAVA,知识点主要是基础语法,难度简单。
第二次作业:因做了第一次,开始学习JAVA的面向对象,比起面向过程更加有条理性。
第三次作业:有了前两次的锻炼,对面向对象有了一定的了解,在第二次的题量的基础上难道增加。
设计与分析:
题目集2(7-2)
RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5~8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5~8位,具体位数由通信双方提前设置)前加上1位起始位“0”,在有效数据之后加上1位可选的奇偶校验位和1位结束位“1”。请编写程序,模拟串口接收处理程序,注:假定有效数据是8位,奇偶校验位采用奇校验。
输入格式:
由0、1组成的二进制数据流。例如:11110111010111111001001101111111011111111101111
输出格式:
过滤掉空闲、起始、结束以及奇偶校验位之后的数据,数据之前加上序号和英文冒号。
如有多个数据,每个数据单独一行显示。
若数据不足11位或者输入数据全1没有起始位,则输出"null data",
若某个数据的结束符不为1,则输出“validate error”。
若某个数据奇偶校验错误,则输出“parity check error”。
若数据结束符和奇偶校验均不合格,输出“validate error”。
如:11011或11111111111111111。
例如:
1:11101011
2:01001101
3:validate error
输入样例:
在这里给出一组输入。例如:
1111011101011111111111
输出样例:
在这里给出相应的输出。例如:
1:11101011
输入样例1:
在这里给出一组输入。例如:
11110111010111111001001101111111011111111101111
输出样例1:
在这里给出相应的输出。例如:
1:11101011
2:01001101
3:validate error
输入样例2:
输入数据不足11位。例如:
111101
输出样例2:
在这里给出相应的输出。例如:
null data
输入样例3:
输入数据全1没有起始位。例如:
1111111111111111
输出样例3:
在这里给出相应的输出。例如:
null data
输入样例4:
输入数据全1没有起始位。例如:
111101110101111111101111111101
输出样例4:
在这里给出相应的输出。例如:
1:11101011
2:parity check error
输入样例5:
两组数据结束符和奇偶校验均不合格。例如:
111000000000000011100000000000000
输出样例5:
在这里给出相应的输出。例如:
1:validate error
2:validate error
输入样例6:
两组数据,数据之间无空闲位。例如:
1110000000001100111000001
输出样例6:
在这里给出相应的输出。例如:
1:00000000
2:01110000
SourceMonito报表
类图
这次没有用到面向对象,因此类图只有一个类。
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList<String> arr = new ArrayList<String>(); ArrayList<String> arr1 = new ArrayList<String>(); boolean flag = true ; String s1 = "" ; String s = sc.nextLine(); if (s.length() < 11 ) { System.out.println( "null data" ); return ; } for ( int i = 0 ; i < s.length() - 10 ; i++) { if (s.charAt(i) == '0' ) { s1 = s.substring(i, i + 11 ); arr.add(s1); i = i + 10 ; flag = false ; } } if (flag) { System.out.println( "null data" ); return ; } for ( int i = 0 ; i < arr.size(); i++) { s1 = arr.get(i); if (s1.length() < 11 ) { System.out.print( "null data" ); return ; } else if (s1.charAt( 10 ) != '1' ) { arr1.add( "validate error" ); } else if (!ParityChecking(s1)) { arr1.add( "parity check error" ); } else if (s1.charAt( 10 ) != '1' && !ParityChecking(s1)) { arr1.add( "validate error" ); } else { s1 = s1.substring( 1 , 9 ); arr1.add(s1); } } for ( int i = 0 ; i < arr1.size(); i++) { System.out.println((i + 1 ) + ":" + arr1.get(i)); } } //判断奇校验是否正确 public static boolean ParityChecking(String s) { String s1 = "" ; int num = 0 ; if (s.charAt( 9 ) == '1' ) { s1 = s.substring( 1 , 9 ); for ( int i = 0 ; i < s1.length(); i++) { if (s1.charAt(i) == '1' ) { num++; } } if (num % 2 == 0 ) return true ; else return false ; } else if (s.charAt( 9 ) == '0' ) { s1 = s.substring( 1 , 9 ); for ( int i = 0 ; i < s1.length(); i++) { if (s1.charAt(i) == '0' ) { num++; } } if (num % 2 == 0 ) return false ; else return true ; } return false ; } } |
本次的难度不是很大,但在判断奇校验是否正确上判断了多次。
题目集3(7-1)
输入连个点的坐标,计算两点之间的距离
输入格式:
4个double类型的实数,两个点的x,y坐标,依次是x1、y1、x2、y2,两个点的坐标之间以空格分隔,每个点的x,y坐标以英文“,”分隔。例如:0,0 1,1或0.1,-0.3 +3.5,15.6。
若输入格式非法,输出"Wrong Format"。
若输入格式合法但坐标点的数量超过两个,输出“wrong number of points”。
输出格式:
计算所得的两点之间的距离。例如:1.4142135623730951
输入样例:
整数输入。例如:
0,0 1,1
输出样例:
在这里给出相应的输出。例如:
1.4142135623730951
输入样例1:
带符号double类型实数输入。例如:
+2,-2.3 0.9,-3.2
输出样例1:
在这里给出相应的输出。例如:
1.42126704035519
输入样例2:
格式非法。例如:
++2,-2.3 0.9,-3.2
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
输入样例3:
点的数量超过两个。例如:
+2,-2.3 0.9,-3.2 +2,-2.3
输出样例3:
在这里给出相应的输出。例如:
wrong number of points
SourceMonito报表
类图
这次也只是用了一个类
完整代码
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String s2 = ""; ArrayList<String> arr = new ArrayList<String>(); String s1[] = s.split(" "); String s3[] = null; for (int i = 0; i < s1.length; i++) { s3 = s1[i].split(","); for (int j = 0; j < s3.length; j++) { if (!s3[j].matches("^[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$")) { System.out.println("Wrong Format"); return; } arr.add(s3[j]); } } if (s1.length != 2) { System.out.println("wrong number of points"); return; } double[] d = new double[4]; for (int i = 0; i < arr.size(); i++) { d[i] = Double.valueOf(arr.get(i)); } if (d[0] == d[2] && d[1] == d[3]) { System.out.println("Wrong Format"); return; } System.out.println(Math.sqrt(Math.pow(d[0] - d[2], 2) + Math.pow(d[1] - d[3], 2))); } }
在字符匹配上使用了三个正则表达式才通过,
正则表达式注意转义符是两个\\。
题目集3 (7-2)
用户输入一组选项和数据,进行与直线有关的计算。选项包括:
1:输入两点坐标,计算斜率,若线条垂直于X轴,输出"Slope does not exist"。
2:输入三个点坐标,输出第一个点与另外两点连线的垂直距离。
3:输入三个点坐标,判断三个点是否在一条线上,输出true或者false。
4:输入四个点坐标,判断前两个点所构成的直线与后两点构成的直线是否平行,输出true或者false.
5:输入四个点坐标,计算输出前两个点所构成的直线与后两点构成的直线的交点坐标,x、y坐标之间以英文分隔",",并输出交叉点是否在两条线段之内(不含四个端点)的判断结果(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:
选项1,两点重合。例如:
1:-2,+5 -2,+5
输出样例:
在这里给出相应的输出。例如:
points coincide
输入样例2:
选项1,斜率无穷大的线。例如:
1:-2,3 -2,+5
输出样例:
在这里给出相应的输出。例如:
Slope does not exist
输入样例3:
选项1,斜率无穷大。例如:
1:-2,3 -2,+5
输出样例:
在这里给出相应的输出。例如:
Slope does not exist
输入样例4:
选项1,符合格式输入,带符号/不带符号数混合。例如:
1:-2.5,3 -2,+5.3
输出样例:
在这里给出相应的输出。例如:
4.6
输入样例5:
选项2,计算第一个点到另外两点连线的垂直距离。例如:
2:0,1 1,0 2,0
输出样例:
在这里给出相应的输出。例如:
1.0
输入样例6:
选项3,判断三个点是否在一条线上。例如:
3:0,1 2,2 5,3
输出样例:
在这里给出相应的输出。例如:
false
输入样例7:
选项4,判断两条线是否平行。例如:
4:0,1 0,2 2,1 3,0
输出样例:
在这里给出相应的输出。例如:
false
输入样例8:
选项5,判断两条线的交点。例如:
5:0,0 -1,-1 0,2 3,-1
输出样例:
在这里给出相应的输出,交点坐标之间以英文","分隔,判断结果与坐标之间以一个英文空格分隔。例如:
1.0,1.0 true
输入样例9:
选项5,判断两条线的交点。但两条线平行例如:
5:0,0 -1,-1 2,3 3,4
输出样例:
在这里给出相应的输出,交点坐标之间以英文","分隔,判断结果与坐标之间以一个英文空格分隔。例如:
is parallel lines,have no intersection point
SourceMonito报表

类图

import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = 0; ArrayList<String> arr = new ArrayList<>(); String s = sc.nextLine(); String s2[] = null; String s1[] = null; String s3[] = null; String s4 = ""; double[] d = new double[20]; if (s.matches("[1-5]:[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$") || s.matches("[1-5]:[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$") || s.matches("[1-5]:[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$") || s.matches("[1-5]:[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$") || s.matches("[1-5]:[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$")) { s2 = s.split(":"); s4 = s2[1]; s1 = s4.split(" "); for (int i = 0; i < s1.length; i++) { s3 = s1[i].split(","); for (int j = 0; j < s3.length; j++) { if (!s3[j].matches("^[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$")) { System.out.println("Wrong Format"); return; } arr.add(s3[j]); } } for (int i = 0; i < arr.size(); i++) { d[i] = Double.valueOf(arr.get(i)); } switch (s.charAt(0)) { case '1': if (arr.size() == 4) { Point p1 = new Point(d[0], d[1]); Point p2 = new Point(d[2], d[3]); Line l = new Line(p1, p2); if (p1.equal(p2)) { System.out.println("points coincide"); } else if (l.b == 0) { System.out.println("Slope does not exist"); } else { System.out.println(l.getSlope()); } } else { System.out.println("wrong number of points"); } break; case '2': if (arr.size() == 6) { Point p1 = new Point(d[0], d[1]); Point p2 = new Point(d[2], d[3]); Point p3 = new Point(d[4], d[5]); Line l = new Line(p2, p3); if (p2.equal(p3)) { System.out.println("points coincide"); } else { System.out.println(l.getDistance(p1)); } } else { System.out.println("wrong number of points"); } break; case '3': if (arr.size() == 6) { Point p1 = new Point(d[0], d[1]); Point p2 = new Point(d[2], d[3]); Point p3 = new Point(d[4], d[5]); Line l = new Line(p2, p3); if (p2.equal(p3)) { System.out.println("points coincide"); } else { if (l.getDistance(p1) == 0) System.out.println("true"); else System.out.println("false"); } } else { System.out.println("wrong number of points"); } break; case '4': if (arr.size() == 8) { Point p1 = new Point(d[0], d[1]); Point p2 = new Point(d[2], d[3]); Point p3 = new Point(d[4], d[5]); Point p4 = new Point(d[6], d[7]); Line l1 = new Line(p1, p2); Line l2 = new Line(p3, p4); if (p1.equal(p2) || p3.equal(p4)) { System.out.println("points coincide"); } else { if (l1.getSlope() == l2.getSlope()) System.out.println("true"); else System.out.println("false"); } } else { System.out.println("wrong number of points"); } break; case '5': if (arr.size() == 8) { Point p1 = new Point(d[0], d[1]); Point p2 = new Point(d[2], d[3]); Point p3 = new Point(d[4], d[5]); Point p4 = new Point(d[6], d[7]); Line l1 = new Line(p1, p2); Line l2 = new Line(p3, p4); if (p1.equal(p2) || p3.equal(p4)) { System.out.println("points coincide"); } else { if (l1.getSlope() == l2.getSlope()) { System.out.println("is parallel lines,have no intersection point"); } else { Point p = l1.InterSection(l2); // if (p.x == 0) { // p.x = 0.0; // } // if(p.y == 0){ // p.y =0.0; // } System.out.print(p.x + "," + p.y + " "); if ((p1.x > p.x && p2.x < p.x) || (p1.x < p.x && p2.x > p.x) || (p3.x > p.x && p4.x < p.x) || (p3.x < p.x && p4.x > p.x)||(p1.y > p.y && p2.y < p.y) || (p1.y < p.y && p2.y > p.y) || (p3.y > p.y && p4.y < p.y) || (p3.y < p.y && p4.y > p.y)) { if (p.equal(p1) || p.equal(p2) || p.equal(p3) || p.equal(p4)) System.out.println("false"); else System.out.println("true"); } else System.out.println("false"); } } } else { System.out.println("wrong number of points"); } break; default: System.out.println("Wrong Format"); return; } } else { System.out.println("Wrong Format"); } } } class Point { double x, y; Point() { } Point(double x, double y) { this.x = x; this.y = y; } double distance(Point p) { return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2)); } boolean equal(Point p) { if (this.x == p.x && this.y == p.y) { return true; } else { return false; } } } class Line { Point p1, p2; double a, b, c; double length; Line() { } Line(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; this.a = p2.y - p1.y; this.b = p1.x - p2.x; this.c = p2.x * p1.y - p1.x * p2.y; this.length = p1.distance(p2); } double getSlope() { if (b == 0) { return 99999; } else { return -a / b; } } double getDistance(Point p) { return Math.abs(this.a * p.x + this.b * p.y + c) / Math.sqrt(Math.pow(this.a, 2) + Math.pow(this.b, 2)); } boolean isParallel(Line l) { if (this.a == l.a && this.b == l.b) return true; else return false; } boolean IsMid(Point p) { if ((this.p2.x >= p.x && this.p1.x <= p.x) || (this.p2.x <= p.x && this.p1.x >= p.x)) { return true; } else { return false; } } Point InterSection(Line l) { double temp = this.a * l.b - l.a * this.b; double x = (this.b * l.c - l.b * this.c) / temp; double y = (l.a * this.c - this.a * l.c) / temp; Point p = new Point(x,y); return p; } }
作为第一题的扩展类型,这次使用了面向对象的编程方法。
在代码的简洁方面还有不足。
题目集3 (7-3)
用户输入一组选项和数据,进行与三角形有关的计算。选项包括:
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。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
如果输入的三个点无法构成三角形,输出"data error"。
注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333333,1.0按格式输出为1.0
选项4中所输入线的两个点坐标重合,输出"points coincide",
输入样例1:
选项4,定义线的两点重合。例如:
4:1,0 1,0 0,0 2,0 4,0
输出样例:
在这里给出相应的输出。例如:
points coincide
输入样例2:
选项4,构成三角形的三个点在一条线上,无法构成三角形。例如:
4:1,0 0,2 0,0 0,0 4,0
输出样例:
在这里给出相应的输出。例如:
data error
输入样例3:
选项1,判断等腰、等边三角形。例如:
1:-2,0 2,0 0,4
输出样例:
两个判断结果。例如:
true false
输入样例4:
选项2,输出边长、面积、重心坐标。例如:
2:0,0 3,0 0,1
输出样例:
在这里给出相应的输出。例如:
7.162278 1.5 1.0,0.333333
输入样例5:
选项3,钝角、直角、锐角的判断。例如:
3:0,1 1,0 2,0
输出样例:
在这里给出相应的输出。例如:
true false false
输入样例6:
选项4,直线与三角形交点的数量等于2,输出数量值以及三角形被分割的两部分面积。例如:
4:1,0 0,2 0,0 0,2 4,0
输出样例:
在这里给出相应的输出。例如:
2 1.0 3.0
输入样例7:
选项4,直线与三角形交点的数量少于两个,只输出数量值。例如:
4:-1,0 1,2 0,1 0,-1 2,0
输出样例:
在这里给出相应的输出。例如:
1
输入样例8:
选项5,用射线法判断点是否在三角形内部。例如:
5:0.5,0.5 0,0 0,2 4,0
输出样例:
在这里给出相应的输出,交点坐标之间以英文","分隔,判断结果与坐标之间以一个英文空格分隔。例如:
in the triangle
输入样例9:
选项5,用射线法判断点是否在三角形内部。例如:
5:0,0 -1,-1 2,3 3,4
输出样例:
在这里给出相应的输出。例如:
outof the triangle
SourceMonito报表

类图

import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); ArrayList<String> arr = new ArrayList<>(); int num = 0; String s2[] = null; String s1[] = null; String s3[] = null; String s4 = ""; double[] d = new double[10]; if (s.matches("[1-5]:[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$") || s.matches("[1-5]:[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$") || s.matches("[1-5]:[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?) [-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?),[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$")) { s2 = s.split(":"); s4 = s2[1]; s1 = s4.split(" "); for (int i = 0; i < s1.length; i++) { s3 = s1[i].split(","); for (int j = 0; j < s3.length; j++) { if (!s3[j].matches("^[-+]?(0|(0\\.\\d+)?|[1-9]\\d*(\\.\\d+)?)$")) { System.out.println("Wrong Format"); return; } arr.add(s3[j]); } } for (int i = 0; i < arr.size(); i++) { d[i] = Double.valueOf(arr.get(i)); } switch (s.charAt(0)) { case '1': if (arr.size() == 6) { Point p1 = new Point(d[0], d[1]); Point p2 = new Point(d[2], d[3]); Point p3 = new Point(d[4], d[5]); Line l1 = new Line(p1, p2); Line l2 = new Line(p2, p3); Line l3 = new Line(p3, p1); if (l1.getDistance(p3) == 0 || l2.getDistance(p1) == 0 || l3.getDistance(p2) == 0) { System.out.println("data error"); return; } if ((p1.distance(p2) == p2.distance(p3)) || (p1.distance(p2) == p1.distance(p3)) || (p2.distance(p3) == p1.distance(p3))) { System.out.print("true "); } else { System.out.print("false "); } if ((p1.distance(p2) == p2.distance(p3)) && (p1.distance(p2) == p1.distance(p3))) { System.out.println("true"); } else { System.out.println("false"); } } else { System.out.println("wrong number of points"); } break; case '2': if (arr.size() == 6) { Point p1 = new Point(d[0], d[1]); Point p2 = new Point(d[2], d[3]); Point p3 = new Point(d[4], d[5]); Line l1 = new Line(p1, p2); Line l2 = new Line(p2, p3); Line l3 = new Line(p3, p1); if (l1.getDistance(p3) == 0 || l2.getDistance(p1) == 0 || l3.getDistance(p2) == 0) { System.out.println("data error"); return; } double girth, area; Point core = new Point(); girth = p1.distance(p2) + p2.distance(p3) + p3.distance(p1); area = p1.distance(p2) * l1.getDistance(p3) * 0.5; core.x = (p1.x + p2.x + p3.x) / 3; core.y = (p1.y + p2.y + p3.y) / 3; //System.out.print(girth + " " + area + " "); //System.out.printf("%.6f %.6f %.6f,%.6f",girth,area,core.x,core.y); System.out.print(new DecimalFormat("0.0#####").format(girth) + " " + new DecimalFormat("0.0#####").format(area) + " " + new DecimalFormat("0.0#####").format(core.x) + "," + new DecimalFormat("0.0#####").format(core.y)); //System.out.print(new DecimalFormat("#.######").format(girth) + " " + area + " " + core.x + "," + new DecimalFormat("#.######").format(core.y)); } else { System.out.println("wrong number of points"); } break; case '3': if (arr.size() == 6) { Point p1 = new Point(d[0], d[1]); Point p2 = new Point(d[2], d[3]); Point p3 = new Point(d[4], d[5]); Line l1 = new Line(p1, p2); Line l2 = new Line(p2, p3); Line l3 = new Line(p3, p1); if (l1.getDistance(p3) == 0 || l2.getDistance(p1) == 0 || l3.getDistance(p2) == 0) { System.out.println("data error"); return; } if (p1.equal(p2) || p1.equal(p3) || p2.equal(p3)) { System.out.println("points coincide"); return; } double a, b, c; a = l1.length; b = l2.length; c = l3.length; if (Math.abs(a * a - b * b + c * c) <= 0.001 || Math.abs(b * b - c * c + a * a) <= 0.001 || Math.abs(c * c - a * a + b * b) <= 0.001) { System.out.println("false true false"); } else if (a * a - 0.001 > b * b + c * c || b * b - 0.001 > c * c + a * a || c * c - 0.001 > a * a + b * b) { System.out.println("true false false"); } else if (a * a + 0.001 < b * b + c * c || b * b + 0.001 < c * c + a * a || c * c + 0.001 < a * a + b * b) { System.out.println("false false true"); } // double A, B, C; // A = Math.acos((Math.pow(p1.distance(p2), 2) + Math.pow(p1.distance(p3), 2) - Math.pow(p2.distance(p3), 2)) / (2 * p1.distance(p2) * p1.distance(p3))); // B = Math.acos((Math.pow(p2.distance(p3), 2) + Math.pow(p2.distance(p1), 2) - Math.pow(p1.distance(p3), 2)) / (2 * p1.distance(p2) * p2.distance(p3))); // C = Math.acos((Math.pow(p2.distance(p3), 2) + Math.pow(p1.distance(p3), 2) - Math.pow(p1.distance(p2), 2)) / (2 * p2.distance(p3) * p1.distance(p3))); // if (A < Math.PI / 2 && B < Math.PI / 2 && C < Math.PI / 2) { // System.out.println("false false true"); // } else if (A == Math.PI / 2 || B == Math.PI / 2 || C == Math.PI / 2) { // System.out.println("false true false"); // } else if (A > Math.PI / 2 || B > Math.PI / 2 || C > Math.PI / 2) { // System.out.println("true false false"); // } } else { System.out.println("wrong number of points"); } break; case '4': if (arr.size() == 10) { Point p1 = new Point(d[0], d[1]); Point p2 = new Point(d[2], d[3]); Point p3 = new Point(d[4], d[5]); Point p4 = new Point(d[6], d[7]); Point p5 = new Point(d[8], d[9]); Line l = new Line(p1, p2); Line l1 = new Line(p3, p4); Line l2 = new Line(p4, p5); Line l3 = new Line(p3, p5); Delta D = new Delta(p3, p4, p5); if (p1.equal(p2)) { System.out.println("points coincide"); return; } if(p3.equal(p4)&& p3.equal(p5) && p4.equal(p5)) { System.out.println("data error"); return; } if (l1.getDistance(p5) < 0.00001 || l2.getDistance(p3) < 0.00001 || l3.getDistance(p4) < 0.00001) { System.out.println("data error"); return; } D.intersection_point(l); } else { System.out.println("wrong number of points"); } break; case '5': if (arr.size() == 8) { Point p1 = new Point(d[0], d[1]); Point p2 = new Point(d[2], d[3]); Point p3 = new Point(d[4], d[5]); Point p4 = new Point(d[6], d[7]); Line l1 = new Line(p2, p3); Line l2 = new Line(p3, p4); Line l3 = new Line(p2, p4); Delta D = new Delta(p2, p3, p4); if (l1.getDistance(p4) == 0 || l2.getDistance(p2) == 0 || l3.getDistance(p3) == 0) { System.out.println("data error"); return; } D.IsInTheTriangle(p1); } else { System.out.println("wrong number of points"); } break; default: System.out.println("Wrong Format"); } } else { System.out.println("Wrong Format"); } } } class Point { double x, y; Point() { } Point(double x, double y) { this.x = x; this.y = y; } double distance(Point p) { return Math.sqrt(Math.pow(this.x - p.x, 2) + Math.pow(this.y - p.y, 2)); } boolean equal(Point p) { if (this.x == p.x && this.y == p.y) { return true; } else { return false; } } } class Line { Point p1, p2; double a, b, c; double length; Line() { } Line(Point p1, Point p2) { this.p1 = p1; this.p2 = p2; this.a = p2.y - p1.y; this.b = p1.x - p2.x; this.c = p2.x * p1.y - p1.x * p2.y; this.length = p1.distance(p2); } double getSlope() { if (b == 0) { return 9999; } else { return -a / b; } } double getDistance(Point p) { return Math.abs(this.a * p.x + this.b * p.y + c) / Math.sqrt(Math.pow(this.a, 2) + Math.pow(this.b, 2)); } boolean isParallel(Line l) { if (this.a == l.a && this.b == l.b) return true; else return false; } boolean IsSame(Line l) { if (this.a == l.a && this.b == l.b && this.c == l.c) return true; else return false; } boolean IsMid(Point p) { if (this.b != 0){ if((this.p2.x >= p.x && this.p1.x <= p.x) || (this.p2.x <= p.x && this.p1.x >= p.x)){ return true; }else{ return false; } }else{ if((this.p2.y >= p.y && this.p1.y <= p.y) || (this.p2.y <= p.y && this.p1.y >= p.y)){ return true; }else{ return false; } } } Point InterSection(Line l) { double temp = this.a * l.b - l.a * this.b; double x = (this.b * l.c - l.b * this.c) / temp; double y = (l.a * this.c - this.a * l.c) / temp; Point p = new Point(x, y); return p; } } class Delta { Point p3, p4, p5; Line l1; Line l2; Line l3; Delta() { } Delta(Point p1, Point p2, Point p3) { this.p3 = p1; this.p4 = p2; this.p5 = p3; this.l1 = new Line(this.p3, this.p4); this.l2 = new Line(this.p4, this.p5); this.l3 = new Line(this.p3, this.p5); } void intersection_point(Line l) { Point temp1 = new Point(); Point temp2 = new Point(); Point temp3 = new Point(); temp1 = l.InterSection(this.l1); temp2 = l.InterSection(this.l2); temp3 = l.InterSection(this.l3); int num1 = 0; int num2 = 0; int num3 = 0; if (l.IsSame(this.l3) || l.IsSame(this.l2) || l.IsSame(this.l1)) { System.out.println("The point is on the edge of the triangle"); return; } if (this.l1.IsMid(temp1)) { num1++; } if (this.l2.IsMid(temp2)) { num2++; } if (this.l3.IsMid(temp3)) { num3++; } if (num1 + num2 + num3 == 2) { if (num1 == 1 && num2 == 1) { if (temp1.equal(temp2)) { System.out.println("1"); } else { Line tempLine = new Line(temp1, temp2); double areaAll = 0.5 * l1.length * l1.getDistance(this.p5); double area = 0.5 * temp1.distance(temp2) * tempLine.getDistance(this.p4); Show_area(area, areaAll - area); } } else if (num1 == 1 && num3 == 1) { if (temp1.equal(temp3)) { System.out.println("1"); } else { Line tempLine = new Line(temp1, temp3); double areaAll = 0.5 * l1.length * l1.getDistance(this.p5); double area = 0.5 * temp1.distance(temp3) * tempLine.getDistance(this.p3); Show_area(area, areaAll - area); } } else if (num2 == 1 && num3 == 1) { if (temp2.equal(temp3)) { System.out.println("1"); } else { Line tempLine = new Line(temp2, temp3); double areaAll = 0.5 * l1.length * l1.getDistance(this.p5); double area = 0.5 * temp3.distance(temp2) * tempLine.getDistance(this.p5); Show_area(area, areaAll - area); } } } else if (num1 + num2 + num3 == 3) { if (temp1.equal(temp2)) { Line tempLine = new Line(temp1, temp3); double areaAll = 0.5 * l1.length * l1.getDistance(this.p5); double area = 0.5 * temp1.distance(temp3) * tempLine.getDistance(this.p3); Show_area(area, areaAll - area); } else if (temp1.equal(temp3)) { Line tempLine = new Line(temp2, temp3); double areaAll = 0.5 * l1.length * l1.getDistance(this.p5); double area = 0.5 * temp3.distance(temp2) * tempLine.getDistance(this.p5); Show_area(area, areaAll - area); } else if (temp2.equal(temp3)) { Line tempLine = new Line(temp1, temp2); double areaAll = 0.5 * l1.length * l1.getDistance(this.p5); double area = 0.5 * temp1.distance(temp2) * tempLine.getDistance(this.p4); Show_area(area, areaAll - area); } } else { System.out.println(num1 + num2 + num3); } } void Show_area(double area1, double area2) { if (area1 > area2) { System.out.println("2 " + new DecimalFormat("0.0#####").format(area2) + " " + new DecimalFormat("0.0#####").format(area1)); } else { System.out.println("2 " + new DecimalFormat("0.0#####").format(area1) + " " + new DecimalFormat("0.0#####").format(area2)); } } void IsInTheTriangle(Point p) { Point temp1 = new Point(); Point temp2 = new Point(); Line l = new Line(); l.p1 = p; l.a = this.l1.a; l.b = this.l1.b; l.c = -l.a * l.p1.x - l.b * l.p1.y; temp1 = l.InterSection(this.l2); temp2 = l.InterSection(this.l3); l.p1 = temp1; l.p2 = temp2; if ((this.l1.getDistance(p) == 0 && l1.IsMid(p)) || (this.l2.getDistance(p) == 0 && l2.IsMid(p)) || (l3.IsMid(p) && this.l3.getDistance(p) == 0)) { System.out.println("on the triangle"); return; } if ((this.l1.getDistance(p) == 0 && !l1.IsMid(p)) || (this.l2.getDistance(p) == 0 && !l2.IsMid(p)) || (!l3.IsMid(p) && this.l3.getDistance(p) == 0)) { System.out.println("outof the triangle"); return; } if (temp1.equal(temp2)) { System.out.println("outof the triangle"); return; } if (!this.l2.IsMid(temp1) || !this.l3.IsMid(temp2)) { System.out.println("outof the triangle"); return; } if (this.l2.IsMid(temp1) && this.l3.IsMid(temp2)) { if (l.IsMid(p)) { System.out.println("in the triangle"); return; } else { System.out.println("outof the triangle"); return; } } } }
第三题同样作为第二题的扩展类型,在算法上有难度,同时在计算分割三角形的面积时比较困难,我的思路是先做一条过第一个点且与三角形随便一条边平行的直线,判断直线与哪两条线段相交,再看两线段的交点,先算小三角形的面积,在用大三角形的面积减去小三角形的面积。
踩坑心得
题目集1 (7-7)
这道题要注意double类型的精度问题。
题目集2(7-2)
在提取11为数据的时候,未考虑循环自身会自增加1,导致多加了1位。
题目集3(7-3)
使用射线法求交点时,没有考虑斜率不存在的情况(直线使用点斜式),导致要判断多种斜率不存在的情况,要写多个判断条件。
总结
这三次作业让我熟悉了JAVA的面向对象编程方法,并且利用代码分析工具优化自己的代码,但对类之间的关系并不清晰,对于复杂的问题不能得出全部的框架。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义