Java学习总结
我们南昌航空大学的Java专业课期中刚刚结束,题目难度偏简单,相比于日常作业还是简单了很多。
以下是题目的图片。
以下是我的代码。
import java.util.ArrayList; import java.util.Scanner; abstract class Element{ public abstract void display(); } class Plane extends Element{ private String color; public Plane() { } public Plane(String color) { this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public void display() { System.out.println("The Plane's color is:" + this.color); } } class Point extends Element{ private double x; private double y; public Point() { } public Point(double x,double y) { this.x = x; this.y = y; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public void display() { System.out.println("(" + String.format("%.2f",x) + "," + String.format("%.2f",y) + ")"); } } class Line extends Element{ private Point point1; private Point point2; private String color; public Line() { } public Line(Point p1,Point p2,String color) { this.point1 = p1; this.point2 = p2; this.color = color; } public Point getPoint1() { return point1; } public void setPoint1(Point point1) { this.point1 = point1; } public Point getPoint2() { return point2; } public void setPoint2(Point point2) { this.point2 = point2; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getDistance() { return Math.sqrt(Math.pow(point1.getX() - point2.getX(),2) + Math.pow(point1.getY() - point2.getY(),2)); } public void display() { System.out.println("The line's color is:" + this.color); System.out.println("The line's begin point's Coordinate is:"); point1.display(); System.out.println("The line's end point's Coordinate is:"); point2.display(); System.out.println("The line's length is:" + String.format("%.2f", this.getDistance())); } } class GeometryObject{ private ArrayListlist = new ArrayList (); public GeometryObject(){ } public void add(Element element) { this.list.add(element); } public void remove(int index) { if(index <= this.list.size() - 1 && index >= 0) { this.list.remove(index); } } public ArrayList getList() { return list; } } public class Main { public static boolean checkInput(double a) { if(a > 200 || a <= 0) { return false; } return true; } public static boolean checkInput1(int a) { if(a > 4 || a < 0) { return false; } return true; } public static void run() { double x,y; double[] a = new double[10]; Point point1,point2; String color; GeometryObject geometryObject = new GeometryObject(); Scanner input = new Scanner(System.in); int choice = input.nextInt(); if(!checkInput1(choice)) { System.out.printf("Wrong Format"); System.exit(0); } while(choice != 0) { switch(choice) { case 1://insert Point object into list x = input.nextDouble(); y = input.nextDouble(); if(!checkInput(x)) { System.out.printf("Wrong Format"); System.exit(0); } if(!checkInput(y)) { System.out.printf("Wrong Format"); System.exit(0); } Element point = new Point(x,y); geometryObject.add(point); break; case 2://insert Line object into list for(int i = 1;i <= 4;i ++) { a[i] = input.nextDouble(); } color = input.next(); for(int i = 1;i <= 4;i ++) { if(!checkInput(a[i])) { System.out.printf("Wrong Format"); System.exit(0); } } point1 = new Point(a[1],a[2]); point2 = new Point(a[3],a[4]); Element line = new Line(point1,point2,color); geometryObject.add(line); break; case 3://insert Plane object into list color = input.next(); Element plane = new Plane(color); geometryObject.add(plane); break; case 4://delete index - 1 object from list int index = input.nextInt(); geometryObject.remove(index - 1); } choice = input.nextInt(); if(!checkInput1(choice)) { System.out.printf("Wrong Format"); System.exit(0); } } for(int i = 0;i < geometryObject.getList().size();i ++) { geometryObject.getList().get(i).display(); } } public static void main(String[] args) { run(); } }
以下是老师给的标准代码。
import java.util.ArrayList; import java.util.Scanner; abstract class Element { public abstract void display(); } class Plane extends Element { private String color; public Plane() { super(); // TODO Auto-generated constructor stub } public Plane(String color) { super(); this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public void display() { // TODO Auto-generated method stub System.out.println("The Plane's color is:" + this.color); } } class Point extends Element{ private double x,y; public Point(){ } public Point(double x,double y){ this.x = x; this.y = y; } public double getX(){ return this.x; } public void setX(double x){ this.x = x; } public double getY(){ return this.y; } public void setY(double y){ this.y = y; } @Override public void display(){ System.out.println("(" + String.format("%.2f", x) + "," + String.format("%.2f",y) + ")"); } } class Line extends Element{ private Point point1,point2; private String color; public Line(){ } public Line(Point p1,Point p2,String color){ this.point1 = p1; this.point2 = p2; this.color = color; } public Point getPoint1() { return point1; } public void setPoint1(Point point1) { this.point1 = point1; } public Point getPoint2() { return point2; } public void setPoint2(Point point2) { this.point2 = point2; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getDistance(){ return Math.sqrt(Math.pow(this.point1.getX() - this.point2.getX(), 2) + Math.pow(this.getPoint1().getY() - this.getPoint2().getY(), 2)); } @Override public void display(){ System.out.println("The line's color is:" + this.getColor()); System.out.println("The line's begin point's Coordinate is:"); this.getPoint1().display(); System.out.println("The line's end point's Coordinate is:"); this.getPoint2().display(); System.out.println("The line's length is:" + String.format("%.2f", this.getDistance())); } } class GeometryObject{ private ArrayListlist = new ArrayList<>(); public GeometryObject() { } public void add(Element element) { list.add(element); } public void remove(int index) { if(index < 1 || index > list.size()) { return; } list.remove(index - 1); } public ArrayList getList(){ return this.list; } } public class Main { public static void main(String[] args) { double x1,y1,x2,y2; String color; Scanner input = new Scanner(System.in); int choice = 0,index = 0; GeometryObject container = new GeometryObject(); choice = input.nextInt(); while(choice != 0) { switch(choice) { case 1: x1 = input.nextDouble(); y1 = input.nextDouble(); container.add(new Point(x1,y1)); break; case 2: x1 = input.nextDouble(); y1 = input.nextDouble(); x2 = input.nextDouble(); y2 = input.nextDouble(); color = input.next(); container.add(new Line(new Point(x1,y1),new Point(x2,y2),color)); break; case 3: color = input.next(); container.add(new Plane(color)); break; case 4: index = input.nextInt(); container.remove(index); break; } choice = input.nextInt(); } for(Element element:container.getList()) { element.display(); } } }
我的代码相对于标准代码来说多了一个检测输入,这是因为前面的题目是与其相关联的,并且需要检测输入。
接下来是我在作业中写了一个代码的架构,这个架构用来解决同类型的题目还是很好用的。
比如,在作业题目集4中。
以下是我的代码,可能还会有点问题。
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; class Point{ public double x; public double y; public boolean z = false; public Point(double x,double y) { this.x = x; this.y = y; } } abstract class Option{ public int trueNumber; public Listpoints; public abstract String workProblem(); public List getPoints() { return points; } public void setPoints(List points) { this.points = points; } public boolean isRightNumber() { if(this.trueNumber == this.points.size()) { return true; } return false; } public double twoPointsgetLength(Point p1,Point p2) { return Math.sqrt(Math.pow(p1.y - p2.y,2) + Math.pow(p1.x - p2.x,2)); } public Point threePointsgetFocus(Point p1,Point p2,Point p3) { Point point; return point = new Point((p1.x + p2.x + p3.x) / 3,(p1.y + p2.y + p3.y) / 3); } public boolean isCoincide() { if(points.get(0).x == points.get(1).x && points.get(0).y == points.get(1).y) { return true; } else { return false; } } public Point getPoint(Point p1,Point p2,Point p3,Point p4) { Point point; double a1 = p2.y - p1.y; double b1 = p1.x - p2.x; double c1 = p1.x*p2.y - p2.x*p1.y; double a2 = p4.y - p3.y; double b2 = p3.x - p4.x; double c2 = p3.x*p4.y - p4.x*p3.y; double det= a1*b2 - a2*b1; double x = (c1*b2 - c2*b1)/det; double y = (a1*c2 - a2*c1)/det; return point = new Point(x,y); } public double threePointsgetArea(Point p1,Point p2,Point p3) { return Math.abs(( p1.x * p2.y + p2.x * p3.y + p3.x * p1.y - p1.x * p3.y - p2.x * p1.y - p3.x * p2.y ) / 2); } public void checkisTriangle() { if(!this.isTriangle()) { System.out.printf("data error"); System.exit(0); } return ; } public void checkisRightNumber() { if(!this.isRightNumber()) { System.out.printf("wrong number of points"); System.exit(0); } return ; } public boolean isTriangle() { int i = points.size() - 1; if(threePointsgetArea(points.get(i), points.get(i - 1), points.get(i - 2)) == 0) { return false; } return true; } public String Six(String a) { String b = ""; int x = 0; int j = 0; for(int i = 0;i < a.length();i ++) { if(x == 1) { j ++; } if(j == 7) { b = String.format("%.6f", Double.parseDouble(a)); break; } b += a.charAt(i); if(a.charAt(i) == '.') { x = 1; } } return b; } } class Option1 extends Option{ public Option1() { super.trueNumber = 3; } public String workProblem() { String a = ""; double l1 = super.twoPointsgetLength(points.get(0), points.get(1)); double l2 = super.twoPointsgetLength(points.get(0), points.get(2)); double l3 = super.twoPointsgetLength(points.get(2), points.get(1)); if(l1 == l2 | l2 == l3 | l1 == l3) { a += "true "; if(l1 == l2 && l2 == l3) { a += "true"; } else { a += "false"; } } else { a += "false false"; } return a; } } class Option2 extends Option{ public Option2() { super.trueNumber = 3; } public String workProblem() { String a = ""; double l1 = super.twoPointsgetLength(points.get(0), points.get(1)); double l2 = super.twoPointsgetLength(points.get(0), points.get(2)); double l3 = super.twoPointsgetLength(points.get(2), points.get(1)); a += Six("" + (l1 + l2 + l3)) + " "; a += Six("" + (this.threePointsgetArea(points.get(0), points.get(1), points.get(2)))) + " "; Point point = this.threePointsgetFocus(points.get(0), points.get(1), points.get(2)); a += Six("" + (point.x)) + "," + Six("" + (point.y)); return a; } } class Option3 extends Option{ public Option3() { super.trueNumber = 3; } public String getAnswer(double l1,double l2,double l3) { if(Math.abs(Math.pow(l1, 2) + Math.pow(l2, 2) - Math.pow(l3, 2)) <= 0.0000000001) { return "false true false"; } else if(Math.pow(l1, 2) + Math.pow(l2, 2) > Math.pow(l3, 2)) { return "false false true"; } else { return "true false false"; } } public String workProblem() { String a = ""; double l1 = super.twoPointsgetLength(points.get(0), points.get(1)); double l2 = super.twoPointsgetLength(points.get(0), points.get(2)); double l3 = super.twoPointsgetLength(points.get(2), points.get(1)); if(l3 >= l1 && l3 >= l2) { a += this.getAnswer(l1, l2, l3); } else if(l1 >= l3 && l1 >= l2) { a += this.getAnswer(l3, l2, l1); } else { a += this.getAnswer(l3, l1, l2); } return a; } } class Option4 extends Option{ public Option4() { super.trueNumber = 5; } public double getK(Point p1,Point p2) { return (p1.y - p2.y) / (p1.x - p2.x); } public boolean isonthe(Point p1,Point p2,Point p3) { if(p1.x == p2.x && p3.x == p2.x) { return true; } else { if(getK(p1,p2) == getK(p3,p2)) { return true; } else { return false; } } } public boolean iscoincidence(Point p1,Point p2,Point p3,Point p4,Point p5) { double k1 = getK(p1,p2); double k2 = getK(p3,p4); double k3 = getK(p5,p4); double k4 = getK(p3,p5); if(k1 == k2 && isonthe(p1,p3,p4) | k1 == k3 && isonthe(p1,p5,p4) | k1 == k4 && isonthe(p1,p5,p3)) { return true; } return false; } public boolean InRec(Point p,Point p1,Point p2) { return p.x >= Math.min(p1.x, p2.x) && p.y >= Math.min(p1.y, p2.y) && p.x <= Math.max(p1.x, p2.x) && p.y <= Math.max(p1.y, p2.y); } public String workProblem() { String a = ""; if(this.isCoincide()) { return "points coincide"; } if(this.iscoincidence(points.get(0), points.get(1), points.get(3), points.get(4), points.get(2))) { return "The point is on the edge of the triangle"; } Point point1 = getPoint(points.get(0), points.get(1), points.get(3), points.get(4)); Point point2 = getPoint(points.get(0), points.get(1), points.get(2), points.get(4)); Point point3 = getPoint(points.get(0), points.get(1), points.get(3), points.get(2)); int ans = 0; int z1 = 0,z2 = 0; if(InRec(point1, points.get(3), points.get(4))) { ans ++; point1.z = true; } if(InRec(point2,points.get(2), points.get(4))) { ans ++; point2.z = true; } if(InRec(point3,points.get(2), points.get(3))) { ans ++; point3.z = true; } a += ans; double s1 = 0,s2 = 0; if(ans == 2) { double sz = this.threePointsgetArea(points.get(3), points.get(4), points.get(2)); if(point1.z == false) { if(point3.x == point2.x && point3.y == point2.y) { return "1"; } s1 = this.threePointsgetArea(point2,point3, points.get(2)); } else if(point2.z == false) { if(point3.x == point1.x && point3.y == point1.y) { return "1"; } s1 = this.threePointsgetArea(point1,point3, points.get(3)); } else { if(point1.x == point2.x && point1.y == point2.y) { return "1"; } s1 = this.threePointsgetArea(point1,point2, points.get(4)); } s2 = sz - s1; if(s1 > s2) { double s = s1; s1 = s2; s2 = s; } a += " " + Six("" + (s1)) + " " + Six("" + (s2)); } if (ans == 3) { a = "2"; double sz = this.threePointsgetArea(points.get(3), points.get(4), points.get(2)); if(point1.x == point2.x && point1.y == point2.y) { s1 = this.threePointsgetArea(point3,point2, points.get(2)); } else if(point1.x == point3.x && point1.y == point3.y) { s1 = this.threePointsgetArea(point1,point2, points.get(4)); } else { s1 = this.threePointsgetArea(point1,point2, points.get(4)); } s2 = sz - s1; if(s1 > s2) { double s = s1; s1 = s2; s2 = s; } a += " " + Six("" + (s1)) + " " + Six("" + (s2)); } return a; } } class Option5 extends Option{ public Option5() { super.trueNumber = 4; } public String workProblem() { String a = ""; double s1 = this.threePointsgetArea(points.get(1), points.get(2), points.get(3)); double s2 = this.threePointsgetArea(points.get(0), points.get(2), points.get(3)); double s3 = this.threePointsgetArea(points.get(1), points.get(0), points.get(3)); double s4 = this.threePointsgetArea(points.get(1), points.get(2), points.get(0)); if(s1 < s2 + s3 + s4) { a += "outof triangle"; } else { if(s2 == 0 | s3 == 0 | s4 == 0) { a += "on the triangle"; } else { a += "in the triangle"; } } return a; } } public class Main { public static boolean checkInput(String a) { if(a.charAt(1) != ':') { return false; } else { if(!(a.charAt(0)<='5'&&a.charAt(0)>='1')) { return false; } else { int m = 0;int n = 0; int x = 0; for(int i = 2;i < a.length();i++) { int j = i + 1; int z = 1; //System.out.println(i); if(a.charAt(i) == '+' |a.charAt(i) == '-') { if(a.charAt(j) == '0') { if(j + 1 == a.length()) { return true; } if(a.charAt(j + 1) == ' ' && x == 1) { x = 0; i = j + 1; continue; } else if(a.charAt(j + 1) == ',' && x == 0){ x = 1; i = j + 1; continue; } else if(a.charAt(j + 1) == '.' && a.charAt(j + 2) != ' ') { j = j + 2; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; continue; } else { return false; } } while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { if(a.charAt(j) == '.' && a.charAt(j + 1) != ' ') { j = j + 1; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; z = 0; break; } else { return false; } } j ++; } } else if(a.charAt(i) <= '9' && a.charAt(i) >='0') { j = i; if(a.charAt(j) == '0') { if(j + 1 == a.length()) { return true; } if(a.charAt(j + 1) == ' ' && x == 1) { x = 0; i = j + 1; continue; } else if(a.charAt(j + 1) == ',' && x == 0){ x = 1; i = j + 1; continue; } else if(a.charAt(j + 1) == '.' && a.charAt(j + 2) != ' ') { j = j + 2; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; continue; } else { return false; } } while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { if(a.charAt(j) == '.' && a.charAt(j + 1) != ' ') { j = j + 1; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; z = 0; break; } else { return false; } //System.out.println(1); } j ++; } } else { //System.out.println(1); return false; } if(z == 1) { i = j; } } if(x == 0) { //System.out.println(1); return false; } } } return true; } public static String getInput() { Scanner in = new Scanner(System.in); String a = in.nextLine(); if(!checkInput(a)) { System.out.printf("Wrong Format"); System.exit(0); } return a; } public static Option turnto(String a) { Option option; List points = new ArrayList (); String b = a.substring(2); String c [] = b.split(" "); for(int i = 0;i < c.length;i ++) { String d[] = c[i].split(","); Point point = new Point(Double.parseDouble(d[0]),Double.parseDouble(d[1])); points.add(point); } switch(a.charAt(0)) { case '1': option = new Option1(); break; case '2': option = new Option2(); break; case '3': option = new Option3(); break; case '4': option = new Option4(); break; default: option = new Option5(); break; } option.setPoints(points); if(option instanceof Option4){ if(option.isCoincide()){ System.out.printf("points coincide"); System.exit(0); } } return option; } public static String work(String a) { Option option = turnto(a); option.checkisRightNumber(); option.checkisTriangle(); return option.workProblem(); } public static String getOutput(String a) { return work(a); } public static void outPut(String a) { System.out.println(a); } public static void run() { outPut(getOutput(getInput())); } public static void main(String[] args) { run(); } }
接下来是另一个同类型的题目。
这是题目pdf链接https://images.ptausercontent.com/a0811a0d-75e7-40a0-8bc4-67df62b2d9f1.pdf
以下是我的代码。
这个代码还没完成,有一个从四边形转到三角形的是哪条边重合我还没解决。因为当时时间截止了,也太晚啦。
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; class Point{ public double x; public double y; public boolean z = false; public Point(double x,double y) { this.x = x; this.y = y; } } abstract class Option{ public int trueNumber; public Listpoints; public abstract String workProblem(); public List getPoints() { return points; } public void setPoints(List points) { this.points = points; } public boolean isRightNumber() { if(this.trueNumber == this.points.size()) { return true; } return false; } public double threePointsgetArea(Point p1,Point p2,Point p3) { return Math.abs(( p1.x * p2.y + p2.x * p3.y + p3.x * p1.y - p1.x * p3.y - p2.x * p1.y - p3.x * p2.y ) / 2); } public boolean isCoincide(Point p1,Point p2) { if(p1.x == p2.x && p1.y == p2.y) { return true; } else { return false; } } public double getD(Point p1,Point p2,Point p3,Point p4) { double l1 = this.twoPointsgetLength(p1, p2); double l2 = this.twoPointsgetLength(p2, p3); double l3 = this.twoPointsgetLength(p3, p4); double l4 = this.twoPointsgetLength(p4, p1); return l1 + l2 + l3 +l4; } public double twoPointsgetLength(Point p1,Point p2) { return Math.sqrt(Math.pow(p1.y - p2.y,2) + Math.pow(p1.x - p2.x,2)); } public boolean isDiamond(Point p1,Point p2,Point p3,Point p4) { double l1 = this.twoPointsgetLength(p1, p2); double l2 = this.twoPointsgetLength(p2, p3); double l3 = this.twoPointsgetLength(p3, p4); double l4 = this.twoPointsgetLength(p4, p1); if(l1 == l2 && l1 == l3 && l1 == l4) { return true; } return false; } public boolean isrightangle(Point p1,Point p2,Point p3) { if(Math.abs(this.twoPointsgetLength(p1, p2) * this.twoPointsgetLength(p1, p2) + this.twoPointsgetLength(p3, p2) * this.twoPointsgetLength(p3, p2) - this.twoPointsgetLength(p3, p1) * this.twoPointsgetLength(p3, p1)) < 0.000001) { return true; } return false; } public boolean isrectangle(Point p1,Point p2,Point p3,Point p4) { if(isrightangle(p1,p2,p3) && isrightangle(p2,p3,p4) && isrightangle(p3,p4,p1)) { return true; } return false; } public boolean issquare(Point p1,Point p2,Point p3,Point p4) { if(this.isrectangle(p1, p2, p3, p4) && this.isDiamond(p1, p2, p3, p4)) { return true; } return false; } public boolean Op1to3check() { for(int i = 0;i < points.size() - 1;i ++) { for(int j = i + 1;j < points.size();j ++) { if(this.isCoincide(points.get(i), points.get(j))) { return true; } } } return false; } public boolean Op4check() { if(this.isCoincide(points.get(0), points.get(1))) { return true; } else { return false; } } public double getArea(Point p1,Point p2,Point p3,Point p4) { return Math.min(this.threePointsgetArea(p1, p2, p3) + this.threePointsgetArea(p4, p1, p3),this.threePointsgetArea(p1, p2, p4) + this.threePointsgetArea(p4, p2, p3)); } public boolean istriangle(Point p1,Point p2,Point p3,Point p4) { if((this.threePointsgetArea(p1, p2, p3) == 0 && this.threePointsgetArea(p4, p2, p3) != 0) || (this.threePointsgetArea(p1, p2, p3) != 0 && this.threePointsgetArea(p4, p2, p3) == 0)) { return true; } return false; } public boolean isquadrilateral(Point p1,Point p2,Point p3,Point p4) { if(this.threePointsgetArea(p1, p2, p3) == 0 || this.threePointsgetArea(p4, p2, p3) == 0) { return false; } return true; } public boolean gimp_transform_polygon_is_convex(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){ double z1, z2, z3, z4; z1 = ((x2 - x1) * (y4 - y1) - (x4 - x1) * (y2 - y1)); z2 = ((x4 - x1) * (y3 - y1) - (x3 - x1) * (y4 - y1)); z3 = ((x4 - x2) * (y3 - y2) - (x3 - x2) * (y4 - y2)); z4 = ((x3 - x2) * (y1 - y2) - (x1 - x2) * (y3 - y2)); //System.out.println(z1 + " " + z2 + " " + z3 + " " + z4); return (z1 * z2 < 0) && (z3 * z4 < 0); } public boolean IsPXSBX(double Px1,double Py1,double Px2,double Py2,double Px3,double Py3,double Px4,double Py4) { if (((Py2 - Py1) * (Px4 - Px3) == (Py4 - Py3) * (Px2 - Px1)) && ((Py4 - Py1) * (Px2 - Px3) == (Py2 - Py3) * (Px4 - Px1))) { return true; } else if (((Py3 - Py1) * (Px4 - Px2) == (Py4 - Py2) * (Px3 - Px1)) && ((Py4 - Py1) * (Px2 - Px3) == (Py2 - Py3) * (Px4 - Px1))) { return true; } else if (((Py2 - Py1) * (Px4 - Px3) == (Py4 - Py3) * (Px2 - Px1)) && ((Py3 - Py1) * (Px2 - Px4) == (Py2 - Py4) * (Px3 - Px1))) { return true; } else { return false; } } public void checkisRightNumber() { if(!this.isRightNumber()) { System.out.printf("wrong number of points"); System.exit(0); } return ; } public String Six(int c,String a) { String b = ""; int x = 0; int j = 0; for(int i = 0;i < a.length();i ++) { if(x == 1) { j ++; } if(j == c + 1) { b = String.format("%." + c + "f", Double.parseDouble(a)); break; } b += a.charAt(i); if(a.charAt(i) == '.') { x = 1; } } return b; } } class Option1 extends Option{ public Option1() { super.trueNumber = 4; } public String workProblem() { String a = ""; if(this.Op1to3check()) { return "points coincide"; } if(this.isquadrilateral(points.get(0), points.get(1), points.get(2), points.get(3))) { a += "true"; } else { a += "false"; } a += " "; if(this.IsPXSBX(points.get(0).x,points.get(0).y,points.get(1).x,points.get(1).y,points.get(2).x,points.get(2).y,points.get(3).x,points.get(3).y)) { a += "true"; } else { a += "false"; } return a; } } class Option2 extends Option{ public Option2() { super.trueNumber = 4; } public String workProblem() { String a = ""; if(!this.isquadrilateral(points.get(0), points.get(1), points.get(2), points.get(3))) { return "not a quadrilateral"; } if(this.Op1to3check()) { return "points coincide"; } if(this.isDiamond(points.get(0),points.get(1),points.get(2),points.get(3))) { a += "true "; } else { a += "false "; } if(this.isrectangle(points.get(0),points.get(1),points.get(2),points.get(3))) { a += "true "; } else { a += "false "; } if(this.issquare(points.get(0),points.get(1),points.get(2),points.get(3))) { a += "true"; } else { a += "false"; } return a; } } class Option3 extends Option{ public Option3() { super.trueNumber = 4; } public String workProblem() { String a = ""; if(!this.isquadrilateral(points.get(0), points.get(1), points.get(2), points.get(3))) { return "not a quadrilateral"; } if(this.Op1to3check()) { return "points coincide"; } if(this.gimp_transform_polygon_is_convex(points.get(0).x,points.get(0).y,points.get(1).x,points.get(1).y,points.get(2).x,points.get(2).y,points.get(3).x,points.get(3).y)) { a += "true "; } else { a += "false "; } a += this.Six(3, String.valueOf(this.getD(points.get(0), points.get(1), points.get(2), points.get(3)))); a += " "; a += this.Six(3, String.valueOf(this.getArea(points.get(0), points.get(1), points.get(2), points.get(3)))); return a; } } class Option4 extends Option{ public Option4() { super.trueNumber = 6; } public double getK(Point p1,Point p2) { return (p1.y - p2.y) / (p1.x - p2.x); } public boolean isonthe(Point p1,Point p2,Point p3) { if(p1.x == p2.x && p3.x == p2.x) { return true; } else { if(getK(p1,p2) == getK(p3,p2)) { return true; } else { return false; } } } public boolean iscoincidence(Point p1,Point p2,Point p3,Point p4,Point p5,Point p6) { double k1 = getK(p1,p2); double k2 = getK(p3,p4); double k3 = getK(p5,p4); double k4 = getK(p6,p5); double k5 = getK(p6,p3); if(k1 == k2 && isonthe(p1,p3,p4) || k1 == k3 && isonthe(p1,p5,p4) || k1 == k4 && isonthe(p1,p5,p6) || k1 == k5 && isonthe(p1,p3,p6)) { return true; } return false; } public boolean InRec(Point p,Point p1,Point p2) { return p.x >= Math.min(p1.x, p2.x) && p.y >= Math.min(p1.y, p2.y) && p.x <= Math.max(p1.x, p2.x) && p.y <= Math.max(p1.y, p2.y); } public Point getPoint(Point p1,Point p2,Point p3,Point p4) { Point point; double a1 = p2.y - p1.y; double b1 = p1.x - p2.x; double c1 = p1.x*p2.y - p2.x*p1.y; double a2 = p4.y - p3.y; double b2 = p3.x - p4.x; double c2 = p3.x*p4.y - p4.x*p3.y; double det= a1*b2 - a2*b1; double x = (c1*b2 - c2*b1)/det; double y = (a1*c2 - a2*c1)/det; return point = new Point(x,y); } public String workProblem() { String a = ""; if(this.Op4check()) { return "points coincide"; } if(this.iscoincidence(points.get(0), points.get(1), points.get(3), points.get(4), points.get(2),points.get(5))) { return "The line is coincide with one of the lines"; } if(!this.isquadrilateral(points.get(3), points.get(4), points.get(2),points.get(5)) && !this.istriangle(points.get(3), points.get(4), points.get(2),points.get(5))) { return "not a quadrilateral or triangle"; } Point point1 = getPoint(points.get(0), points.get(1), points.get(3), points.get(4)); Point point2 = getPoint(points.get(0), points.get(1), points.get(5), points.get(4)); Point point3 = getPoint(points.get(0), points.get(1), points.get(3), points.get(2)); Point point4 = getPoint(points.get(0), points.get(1), points.get(5), points.get(2)); int ans = 0; int z1 = 0,z2 = 0; if(!this.isquadrilateral(points.get(3), points.get(4), points.get(2),points.get(5)) && this.istriangle(points.get(3), points.get(4), points.get(2),points.get(5))) { }//如果是三角形 else { if(InRec(point1, points.get(3), points.get(4))) { ans ++; point1.z = true; } if(InRec(point2,points.get(5), points.get(4))) { ans ++; point2.z = true; } if(InRec(point3,points.get(2), points.get(3))) { ans ++; point3.z = true; } if(InRec(point4,points.get(2), points.get(5))) { ans ++; point4.z = true; } a += ans; double s1 = 0,s2 = 0; if(ans == 2) { double sz = this.getArea(points.get(2),points.get(3),points.get(4),points.get(5)); if(point1.z == false && point4.z == false) { if(point3.x == point2.x && point3.y == point2.y) { return "1"; } s1 = this.getArea(points.get(2), points.get(5), point3, point2); } else if(point2.z == false && point1.z == false) { if(point3.x == point4.x && point3.y == point4.y) { return "1"; } s1 = this.threePointsgetArea(point4,point3, points.get(2)); } else if(point3.z == false && point1.z == false) { if(point2.x == point4.x && point2.y == point4.y) { return "1"; } s1 = this.threePointsgetArea(point4,point2, points.get(5)); } else if(point2.z == false && point3.z == false) { if(point1.x == point4.x && point1.y == point4.y) { return "1"; } s1 = this.getArea(points.get(2), points.get(3), point4, point1); } else if(point2.z == false && point4.z == false) { if(point1.x == point3.x && point1.y == point3.y) { return "1"; } s1 = this.threePointsgetArea(point1,point3, points.get(3)); } else { if(point1.x == point2.x && point1.y == point2.y) { return "1"; } s1 = this.threePointsgetArea(point1,point2, points.get(2)); } s2 = sz - s1; if(s1 > s2) { double s = s1; s1 = s2; s2 = s; } a += " " + Six(3,"" + (s1)) + " " + Six(3,"" + (s2)); } if (ans == 3) {//p1 3 4 p2 5 4 p3 3 2 p4 5 2 a = "2"; double sz = this.getArea(points.get(2),points.get(3),points.get(4),points.get(5)); if(point1.x == point2.x && point1.y == point2.y) { if(point3.z == true) { s1 = this.threePointsgetArea(point3,point2, points.get(3)); } else { s1 = this.threePointsgetArea(point4,point2, points.get(5)); } } else if(point1.x == point3.x && point1.y == point3.y) { if(point2.z == true) { s1 = this.threePointsgetArea(point3,point2, points.get(4)); } else { s1 = this.threePointsgetArea(point4,point3, points.get(2)); } } else if(point2.x == point4.x && point2.y == point4.y) { if(point3.z == true) { s1 = this.threePointsgetArea(point3,point2, points.get(2)); } else { s1 = this.threePointsgetArea(point1,point2, points.get(3)); } } else if(point3.x == point4.x && point3.y == point4.y){ if(point1.z == true) { s1 = this.threePointsgetArea(point3,point1, points.get(3)); } else { s1 = this.threePointsgetArea(point3,point2, points.get(5)); } } s2 = sz - s1; if(s1 > s2) { double s = s1; s1 = s2; s2 = s; } a += " " + Six(3,"" + (s1)) + " " + Six(3,"" + (s2)); } if(ans == 4) {//p1 3 4 p2 5 4 p3 3 2 p4 5 2 a = "2"; double sz = this.getArea(points.get(2),points.get(3),points.get(4),points.get(5)); if(point1.x == point2.x && point1.y == point2.y) { s1 = this.threePointsgetArea(points.get(4),points.get(5), points.get(3)); } else if(point1.x == point3.x && point1.y == point3.y) { s1 = this.threePointsgetArea(points.get(4),points.get(2), points.get(3)); } else if(point2.x == point4.x && point2.y == point4.y) { s1 = this.threePointsgetArea(points.get(4),points.get(5), points.get(2)); } else if(point3.x == point4.x && point3.y == point4.y){ s1 = this.threePointsgetArea(points.get(2),points.get(5), points.get(3)); } s2 = sz - s1; if(s1 > s2) { double s = s1; s1 = s2; s2 = s; } a += " " + Six(3,"" + (s1)) + " " + Six(3,"" + (s2)); } } return a; } } class Option5 extends Option{ public Option5() { super.trueNumber = 5; } public String workProblem() { String a = ""; double s1 = this.getArea(points.get(1), points.get(2), points.get(3),points.get(4)); double s2 = this.threePointsgetArea(points.get(0), points.get(2), points.get(3)); double s3 = this.threePointsgetArea(points.get(4), points.get(0), points.get(3)); double s4 = this.threePointsgetArea(points.get(1), points.get(2), points.get(0)); double s5 = this.threePointsgetArea(points.get(1), points.get(0), points.get(4)); if(s1 + 0.01 < s2 + s3 + s4 + s5 ) { if(!this.isquadrilateral(points.get(3), points.get(4), points.get(2),points.get(5)) && this.istriangle(points.get(3), points.get(4), points.get(2),points.get(5))) { a += "outof the triangle"; } else { a += "outof the quadrilateral"; } } else { if(s2 == 0 || s3 == 0 || s4 == 0 || s5 == 0) { if(!this.isquadrilateral(points.get(3), points.get(4), points.get(2),points.get(1)) && this.istriangle(points.get(3), points.get(4), points.get(2),points.get(1))) { a += "on the triangle"; } else { a += "on the quadrilateral"; } } else { if(!this.isquadrilateral(points.get(3), points.get(4), points.get(2),points.get(1)) && this.istriangle(points.get(3), points.get(4), points.get(2),points.get(1))) { a += "in the triangle"; } else { a += "in the quadrilateral"; } } } return a; } } public class Main { public static boolean checkInput(String a) { if(a.charAt(a.length() - 1) == ' ') { a = a.substring(0, a.length() - 1); } if(a.charAt(1) != ':') { return false; } else { if(!(a.charAt(0)<='5'&&a.charAt(0)>='1')) { return false; } else { int m = 0;int n = 0; int x = 0; for(int i = 2;i < a.length();i++) { int j = i + 1; int z = 1; //System.out.println(i); if(a.charAt(i) == '+' |a.charAt(i) == '-') { if(a.charAt(j) == '0') { if(j + 1 == a.length()) { return true; } if(a.charAt(j + 1) == ' ' && x == 1) { x = 0; i = j + 1; continue; } else if(a.charAt(j + 1) == ',' && x == 0){ x = 1; i = j + 1; continue; } else if(a.charAt(j + 1) == '.' && a.charAt(j + 2) != ' ') { j = j + 2; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; continue; } else { return false; } } while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { if(a.charAt(j) == '.' && a.charAt(j + 1) != ' ') { j = j + 1; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; z = 0; break; } else { return false; } } j ++; } } else if(a.charAt(i) <= '9' && a.charAt(i) >='0') { j = i; if(a.charAt(j) == '0') { if(j + 1 == a.length()) { return true; } if(a.charAt(j + 1) == ' ' && x == 1) { x = 0; i = j + 1; continue; } else if(a.charAt(j + 1) == ',' && x == 0){ x = 1; i = j + 1; continue; } else if(a.charAt(j + 1) == '.' && a.charAt(j + 2) != ' ') { j = j + 2; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; continue; } else { return false; } } while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { if(a.charAt(j) == '.' && a.charAt(j + 1) != ' ') { j = j + 1; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; z = 0; break; } else { return false; } //System.out.println(1); } j ++; } } else { //System.out.println(1); return false; } if(z == 1) { i = j; } } if(x == 0) { //System.out.println(1); return false; } } } return true; } public static String getInput() { Scanner in = new Scanner(System.in); String a = in.nextLine(); if(!checkInput(a)) { System.out.printf("Wrong Format"); System.exit(0); } return a; } public static Option turnto(String a) { Option option; List points = new ArrayList (); String b = a.substring(2); String c [] = b.split(" "); for(int i = 0;i < c.length;i ++) { String d[] = c[i].split(","); Point point = new Point(Double.parseDouble(d[0]),Double.parseDouble(d[1])); points.add(point); } switch(a.charAt(0)) { case '1': option = new Option1(); break; case '2': option = new Option2(); break; case '3': option = new Option3(); break; case '4': option = new Option4(); break; default: option = new Option5(); break; } option.setPoints(points); return option; } public static String work(String a) { Option option = turnto(a); option.checkisRightNumber(); return option.workProblem(); } public static String getOutput(String a) { return work(a); } public static void outPut(String a) { System.out.println(a); } public static void run() { outPut(getOutput(getInput())); } public static void main(String[] args) { run(); } }
相比来说这个题目应该是在上一个题目的基础上增加一些option抽象类中的方法和增加option子类的一些方法就可以完成这个题目,但是我不想代码太长就把跟这题没用的方法都删除了,但做到第四点时发现三角形的workproblem还是需要。
写架构比没有架构更省时间。虽然这两个题目相差了三周,但是我还是很快的就能熟悉我的代码,因为有这个架构的帮助,我也不需要修改很多代码,就能完成这个题目。
public static boolean checkInput(String a) { if(a.charAt(a.length() - 1) == ' ') { a = a.substring(0, a.length() - 1); } if(a.charAt(1) != ':') { return false; } else { if(!(a.charAt(0)<='5'&&a.charAt(0)>='1')) { return false; } else { int m = 0;int n = 0; int x = 0; for(int i = 2;i < a.length();i++) { int j = i + 1; int z = 1; //System.out.println(i); if(a.charAt(i) == '+' |a.charAt(i) == '-') { if(a.charAt(j) == '0') { if(j + 1 == a.length()) { return true; } if(a.charAt(j + 1) == ' ' && x == 1) { x = 0; i = j + 1; continue; } else if(a.charAt(j + 1) == ',' && x == 0){ x = 1; i = j + 1; continue; } else if(a.charAt(j + 1) == '.' && a.charAt(j + 2) != ' ') { j = j + 2; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; continue; } else { return false; } } while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { if(a.charAt(j) == '.' && a.charAt(j + 1) != ' ') { j = j + 1; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; z = 0; break; } else { return false; } } j ++; } } else if(a.charAt(i) <= '9' && a.charAt(i) >='0') { j = i; if(a.charAt(j) == '0') { if(j + 1 == a.length()) { return true; } if(a.charAt(j + 1) == ' ' && x == 1) { x = 0; i = j + 1; continue; } else if(a.charAt(j + 1) == ',' && x == 0){ x = 1; i = j + 1; continue; } else if(a.charAt(j + 1) == '.' && a.charAt(j + 2) != ' ') { j = j + 2; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; continue; } else { return false; } } while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { if(a.charAt(j) == '.' && a.charAt(j + 1) != ' ') { j = j + 1; while(true) { if(j == a.length()) { break; } if(x == 0 && a.charAt(j) == ',') { x = 1; break; } if(x == 1 && a.charAt(j) == ' ') { x = 0; break; } if(!(a.charAt(j) <= '9' && a.charAt(j) >='0')) { //System.out.println(1); return false; } j ++; } i = j; z = 0; break; } else { return false; } //System.out.println(1); } j ++; } } else { //System.out.println(1); return false; } if(z == 1) { i = j; } } if(x == 0) { //System.out.println(1); return false; } } } return true; }
这个checkInput的方法我写了大概两三个小时(包括测试数据的猜想),改了十几次,总算是把所有的(我觉得是所有)情况都弄进去了。
这时候有人会问,你怎么不用正则表达式呢?
当时写的时候我还没学过,但后面这次的话,有轮子为什么不用轮子。
GG