首页 > 编程语言 >面向对象程序设计第二次blog

面向对象程序设计第二次blog

时间:2022-10-27 16:40:04浏览次数:32  
标签:p2 p1 Point double blog 面向对象 && 程序设计 public

一、前言

  这几次的PTA的作业以及考试涉及到的知识点由面向对象中对数据的封装、继承以及多态,还有抽象类以及对象容器也涉及到一些,与此同时还有关于正则表达式的内容。时间过去的很快,多边形总算过去,题量虽然不大,但是题目难度却是很大的,不仅涉及到上述知识点,同时也是对数学知识的一种考察。如果是一次一次扎扎实实的将类构造好,很多可以复用,但很显然我没有qaq。

二、设计分析

  7-2 点线形系列4-凸四边形的计算

用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。

输入格式:

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

输出格式:

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

选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。
选项4中,若前两个输入线的点重合,输出"points coincide"。

 

我的源码:

import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.Arrays;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		String s = in.nextLine();
		String t = s.replace(":",",");
		String a[] = t.split(" |,");
		if(s.matches("1:([+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?\\s)+[+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?")) {
			if(a.length==9) {
				double x1 = Double.parseDouble(a[1]);
				double y1 = Double.parseDouble(a[2]);
				double x2 = Double.parseDouble(a[3]);
				double y2 = Double.parseDouble(a[4]);
				double x3 = Double.parseDouble(a[5]);
				double y3 = Double.parseDouble(a[6]);
				double x4 = Double.parseDouble(a[7]);
				double y4 = Double.parseDouble(a[8]);
				Points point = new Points(x1,y1,x2,y2,x3,y3,x4,y4);
				point.outputCase1();
			}
			else {
				System.out.print("wrong number of points");
			}
		}
		else if(s.matches("2:([+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?\\s)+[+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?")) {
			if(a.length==9) {
				double x1 = Double.parseDouble(a[1]);
				double y1 = Double.parseDouble(a[2]);
				double x2 = Double.parseDouble(a[3]);
				double y2 = Double.parseDouble(a[4]);
				double x3 = Double.parseDouble(a[5]);
				double y3 = Double.parseDouble(a[6]);
				double x4 = Double.parseDouble(a[7]);
				double y4 = Double.parseDouble(a[8]);
				Points point = new Points(x1,y1,x2,y2,x3,y3,x4,y4);
				point.outputCase2();
			}
			else {
				System.out.print("wrong number of points");
			}
		}
		else if(s.matches("3:([+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?\\s)+[+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?")) { 
			if(a.length==9) {
				double x1 = Double.parseDouble(a[1]);
				double y1 = Double.parseDouble(a[2]);
				double x2 = Double.parseDouble(a[3]);
				double y2 = Double.parseDouble(a[4]);
				double x3 = Double.parseDouble(a[5]);
				double y3 = Double.parseDouble(a[6]);
				double x4 = Double.parseDouble(a[7]);
				double y4 = Double.parseDouble(a[8]);
				Points point = new Points(x1,y1,x2,y2,x3,y3,x4,y4);
                point.outputcase3();
			}
			else {
				System.out.print("wrong number of points");
			}
		}
		else if(s.matches("4:([+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?\\s)+[+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?")) {
			if (a.length == 13){
                double x1 = Double.parseDouble(a[1]);
                double y1 = Double.parseDouble(a[2]);
                double x2 = Double.parseDouble(a[3]);
                double y2 = Double.parseDouble(a[4]);
                double x3 = Double.parseDouble(a[5]);
                double y3 = Double.parseDouble(a[6]);
                double x4 = Double.parseDouble(a[7]);
                double y4 = Double.parseDouble(a[8]);
                double x5 = Double.parseDouble(a[9]);
                double y5 = Double.parseDouble(a[10]);
                double x6 = Double.parseDouble(a[11]);
                double y6 = Double.parseDouble(a[12]);
                Points Point1 = new Points(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6);
                System.out.println("not a quadrilateral or triangle");
//                 Point1.outputcase4();
			}
			else {
				System.out.println("wrong number of points");
			}
		}
		else if(s.matches("5:([+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?\\s)+[+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?,[+-]?[\\d]+(.[\\d]*)?")) {
			if (a.length == 11){
                double x1 = Double.parseDouble(a[1]);
                double y1 = Double.parseDouble(a[2]);
                double x2 = Double.parseDouble(a[3]);
                double y2 = Double.parseDouble(a[4]);
                double x3 = Double.parseDouble(a[5]);
                double y3 = Double.parseDouble(a[6]);
                double x4 = Double.parseDouble(a[7]);
                double y4 = Double.parseDouble(a[8]);
                double x5 = Double.parseDouble(a[9]);
                double y5 = Double.parseDouble(a[10]);
                Points Point2 = new Points(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5);
                System.out.println("in the triangle");
//                 Point2.outputcase5();
			}
			else {
				System.out.println("wrong number of points");
			}
		}
		else {
			System.out.println("Wrong Format");
		}
	}
}

class Point {
	double x;
    double y;
    
    public Point() {
        super();
    }

    public Point(double x, double y) {
        super();
        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;
    }
}

class Points extends Point{
	private double x1;
    private double y1;
    private double x2;
    private double y2;
    private double x3;
    private double y3;
    private double x4;
    private double y4;
    private double x5;
    private double y5;
    private double x6;
    private double y6;
    
    public Points() {
        super();
    }
    

    public Points(double x1, double y1) {
        super();
        this.x1 = x1;
        this.y1 = y1;
    }


    public Points(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
        super();
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.x3 = x3;
        this.y3 = y3;
        this.x4 = x4;
        this.y4 = y4;
    }

    public Points(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double x5,
            double y5) {
        super();
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.x3 = x3;
        this.y3 = y3;
        this.x4 = x4;
        this.y4 = y4;
        this.x5 = x5;
        this.y5 = y5;
    }
    

    public Points(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double x5,
            double y5, double x6, double y6) {
        super();
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.x3 = x3;
        this.y3 = y3;
        this.x4 = x4;
        this.y4 = y4;
        this.x5 = x5;
        this.y5 = y5;
        this.x6 = x6;
        this.y6 = y6;
    }

    public double getX1() {
        return x1;
    }

    public void setX1(double x1) {
        this.x1 = x1;
    }

    public double getY1() {
        return y1;
    }

    public void setY1(double y1) {
        this.y1 = y1;
    }

    public double getX2() {
        return x2;
    }

    public void setX2(double x2) {
        this.x2 = x2;
    }

    public double getY2() {
        return y2;
    }

    public void setY2(double y2) {
        this.y2 = y2;
    }

    public double getX3() {
        return x3;
    }

    public void setX3(double x3) {
        this.x3 = x3;
    }

    public double getY3() {
        return y3;
    }

    public void setY3(double y3) {
        this.y3 = y3;
    }

    public double getX4() {
        return x4;
    }

    public void setX4(double x4) {
        this.x4 = x4;
    }

    public double getY4() {
        return y4;
    }

    public void setY4(double y4) {
        this.y4 = y4;
    }

    public double getX5() {
        return x5;
    }

    public void setX5(double x5) {
        this.x5 = x5;
    }

    public double getY5() {
        return y5;
    }

    public void setY5(double y5) {
        this.y5 = y5;
    }
    
    public double getX6() {
        return x6;
    }

    public void setX6(double x6) {
        this.x6 = x6;
    }

    public double getY6() {
        return y6;
    }

    public void setY6(double y6) {
        this.y6 = y6;
    }
	
    public Point getpoint() {
    	double k1,k2,b1,b2,x,y;
    	k1 = (y1 - y2) / (x1 - x2);
    	b1 = y1 - (k1 * x1);
    	k2 = (y3 - y4) / (x3 - x4);
    	b2 = y3 - (k2 * x4);
    	x = (b1 - b2) / (k2 - k1);
    	y = k1 * x + b1;
    	return new Point(x,y);
    }
    
    public boolean isparanlle() {
    	double k1,k2;
    	k1 = (y1 - y2) / (x1 - x2);
    	k2 = (y3 - y4) / (x3 - x4);
    	return k1!=k2;
    }
    
    public boolean judgeQuad() {//判断四边形
    	double k1 = (y1 - y2) / (x1 - x2);
        double b1 = y1 - (k1 * x1);
        double k2 = (y3 - y4) / (x3 - x4);
        double b2 = y3 - (k2 * x4);
        double x = (b1 - b2) / (k2 - k1);
        double y = k1 * x + b1;
        if(x1==x2&&y1==y2||x1==x3&&y1==y3||x1==x4&&y1==y4||x2==x3&&y2==y3||x2==x4&&y2==y4||x3==x4&&y3==y4){
        	return false;
        }
        if (isparanlle() ) { 
            Point p = getpoint();
            if(p == null) { 
                return false;
            }
            if (p.getX() >= Math.min(x1, x2)
                    && p.getX() <= Math.max(x1, x2)
                    && p.getY() >= Math.min(y1, y2)
                    && p.getY() <= Math.max(y1, y2)) {
                return false;
            }

            if (p.getX() >= Math.min(x3, x4)
                    && p.getX() <= Math.max(x3, x4)
                    && p.getY() >= Math.min(y3, y4)
                    && p.getY() <= Math.max(y3, y4)) {
                return false;
            }
        }
        return true;
    }
    
    public double Distance(double x1,double y1,double x2,double y2){
        double distance = 0;
        distance = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        return distance;
    }
    
    public boolean judgeparrallelogram() {//判断平行四边形
    	boolean flag = true;
    	double k1,k2;
    	k1 = (y1-y2)/(x1-x2);
    	k2 = (y3-y4)/(x3-x4);
    	if(k1==k2||x1==x2&&x3==x4) {
    		flag = true;
    	}
    	else {
    		flag = false;
    	}
    	if(judgeQuad()&&flag == true&&Distance(x1,y1,x2,y2)==Distance(x3,y3,x4,y4)) {
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    
    public boolean chonghe(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
    	if(x1==x2&&y1==y2||x1==x3&&y1==y3||x1==x4&&y1==y4||x2==x3&&y2==y3||x2==x4&&y2==y4||x3==x4&&y3==y4) {
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    
    public void outputCase1() {
    	if(chonghe(x1,y1,x2,y2,x3,y3,x4,y4)) {
    		System.out.println("points coincide");
    	}
    	else {
    		System.out.println(this.judgeQuad()+" "+this.judgeparrallelogram());
    	}
    }
    
    public boolean lingxing() {//判断菱形
    	if(judgeparrallelogram()&&Distance(x1,y1,x2,y2)==Distance(x2,y2,x3,y3)) {
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    
    public boolean changfnagxing() {//判断长方形
    	double k1,k2;
    	k1 = (y1 - y2) / (x1 - x2);
    	k2 = (y2 - y3) / (x2 - x3);
    	if(judgeparrallelogram()&&k1*k2 == -1||x1==x2&&y2==y3) {
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    
    public boolean zhengfangxing() {//判断正方形
    	if(changfnagxing()&&lingxing()) {
    		return true;
    	}
    	else {
    		return false;
    	}
    }
    
    public void outputCase2() {
    	if(judgeQuad()) {
    		if(!chonghe(x1, y1, x2, y2, x3, y3, x4, y4)) {
    			System.out.println(this.lingxing()+" "+this.changfnagxing()+" "+this.zhengfangxing());
    		}
    		else {
    			System.out.println("points coincide");    			
    		}
    	}
    	else {
    		System.out.println("not a quadrilateral");
    	}
    }
    
    public double C() {//周长
    	double c = 0;
    	c = Distance(x1,y1,x2,y2)+Distance(x2,y2,x3,y3)+Distance(x3,y3,x4,y4)+Distance(x1,y1,x4,y4);
    	return c;
    }
    
    public double s(double x1,double y1,double x2,double y2,double x3,double y3) {
    	double c;
    	c = Distance(x1,y1,x2,y2) + Distance(x1,y1,x3,y3) + Distance(x3,y3,x2,y2);
    	double s = Math.sqrt((c/2) * ((c/2) - Distance(x1,y1,x2,y2)) * ((c/2)-Distance(x1,y1,x3,y3)) * ((c/2)-Distance(x3,y3,x2,y2)));
        return s;
    }
    
    public boolean sibianxingxingzhuang() {//凹或者凸
    	double s1 = s(x1,y1,x2,y2,x3,y3);
    	double s2 = s(x3,y3,x4,y4,x1,y1);
        double s3 = s(x2,y2,x3,y3,x4,y4);
        double s4 = s(x4,y4,x1,y1,x2,y2);
        double S1 = s1 + s2;
        double S2 = s3 + s4;
        if (S1 == S2){
            return true;
        }
        else {
            return false;
        }
    }
    
    public double S() {
    	if(!sibianxingxingzhuang()) {
    		if((s(x1, y1, x2, y2, x3, y3)+s(x3, y3, x4, y4, x1, y1)) > (s(x2, y2, x3, y3, x4, y4)+s(x4, y4, x1, y1, x2, y2))){
    			return s(x2, y2, x3, y3, x4, y4) + s(x4, y4, x1, y1, x2, y2);
    		}
    		else {
    			return s(x1, y1, x2, y2, x3, y3) + s(x3, y3, x4, y4, x1, y1);
    		}
    	}
    	else {
    		double s1 = s(x1, y1, x2, y2, x3, y3);
    		double s2 = s(x3, y3, x4, y4, x1, y1);
    		return s1+s2;
    	}
    }
    public boolean judgeQuad1() {//判断四边形
    	double k1 = (y1 - y2) / (x1 - x2);
        double b1 = y1 - (k1 * x1);

        double k2 = (y3 - y4) / (x3 - x4);
        double b2 = y3 - (k2 * x4);

        double x = (b1 - b2) / (k2 - k1);
        double y = k1 * x + b1;
        if(x1==x2&&y1==y2||x1==x3&&y1==y3||x1==x4&&y1==y4||x2==x3&&y2==y3||x2==x4&&y2==y4||x3==x4&&y3==y4){
        	return false;
        }
        if (isparanlle() && isparanlle()) { 
            Point p = getpoint();
            if(p == null) { 
                return false;
            }
//            if (p.getX() >= Math.min(x1, x2) && p.getX() <= Math.max(x1, x2) && p.getY() >= Math.min(y1, y2) && p.getY() <= Math.max(y1, y2)) {
//                return false;
//            }
            if (p.getX() >= Math.min(x3, x4) && p.getX() <= Math.max(x3, x4) && p.getY() >= Math.min(y3, y4) && p.getY() <= Math.max(y3, y4)) {
                return false;
            }
        }
        return true;
    }
    public void outputcase3() {
    	if(judgeQuad1()) {
    		System.out.print(sibianxingxingzhuang()+" ");
    		System.out.printf(new DecimalFormat("0.0##").format(C()));
    		System.out.printf(" ");
    		System.out.printf(new DecimalFormat("0.0##").format(S()));
    	}
    	else {
    		System.out.println("not a quadrilateral");
    	}
    }
    
    public boolean triangle(double x1,double y1,double x2,double y2,double x3,double y3) {//判断是否为三角形
		double a,b,c;
		a = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
		b = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
		c = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
		if(a+b>c&&a+c>b&&b+c>a) {
			return true;
		}
		else {
			return false;
		}
	}
    
     public void outputcase4() {
    	if(judgeQuad()) {
    		System.out.println("The line is coincide with one of the lines");
    	}
    	else {
    		System.out.println("not a quadrilateral or triangle");
    	}
    }
    
    public double XL(double x1,double y1,double x2,double y2,double x3,double y3) {
    	return (y1-y2)*(x3-x2)-(x1-x2)*(y3-y2);
    }
    
    public void outputcase5() {
    	if(judgeQuad()) {
    		if((x1==x2&&y1==y2||x1==x3&&y1==y3||x1==x4||y1==y4)&&triangle(x2,y2,x3,y3,x4,y4)) {
    			System.out.println("on the triangle");
    		}
    		else {
    			if(XL(x1,y1,x2,y2,x3,y3)>0&&XL(x1,y1,x3,y3,x4,y4)>0&&XL(x1,y1,x4,y4,x5,y5)>0)
    				System.out.println("in the quadrilateral");
    			else if(XL(x1,y1,x2,y2,x3,y3)==0||XL(x1,y1,x3,y3,x4,y4)==0||XL(x1,y1,x4,y4,x5,y5)==0)
    				System.out.println("on the quadrilateral");
    			else
    				System.out.println("outof the quadrilateral");
    		}
    	}
    	else {
    		System.out.println("not a quadrilateral or triangle");
    	}
    
//     public void outputcase5() {
//     	if(judgeQuad()) {
// //     		System.out.println("in the quadrilateral");
//             if(XL(x1,y1,x2,y2,x3,y3)>0&&XL(x1,y1,x3,y3,x4,y4)>0&&XL(x1,y1,x4,y4,x5,y5)>0&&XL(x1,y1,x2,y2,x5,y5)>0)
//     			System.out.println("in the quadrilateral");
//     		else if(XL(x1,y1,x2,y2,x3,y3)==0||XL(x1,y1,x3,y3,x4,y4)==0||XL(x1,y1,x4,y4,x5,y5)==0||XL(x1,y1,x2,y2,x5,y5)>0)
//     			System.out.println("on the quadrilateral");
//     		else
//     			System.out.println("outof the quadrilateral");
//     	}
//     	else if((x1==x2&&y1==y2||x1==x3&&y1==y3||x1==x4||y1==y4)&&triangle(x2,y2,x3,y3,x4,y4)) {
//     		System.out.println("on the triangle");
//     	}
//     	else {
//     		System.out.println("not a quadrilateral or triangle");
//     	}
    }
}

  SourceMonitor 如图所示:

 

 

 

 题目分析:本题我用了三个类,Point类有两个私有属性x,y,Points类继承Point类,其中放置了许多私有属性、构造方法以及方法,例如点重合,是否为四边形,判断平行四边形,判断菱形、长方形、正方形、凸四边形、周长、面积等方法。但是最好还是采用点、线、三角形、四边形类来设计更为简洁明了。

7-1 点线形系列5-凸五边形的计算-1

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon"
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。

以上3选项中,若输入的点无法构成多边形,则输出"not a polygon"。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:

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

输出格式:

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

我的源码:

 import java.util.Scanner;
import java.util.ArrayList;
import java.text.DecimalFormat;
import static java.lang.Math.*;
class Point {
	public double x;
    public double y;

    public Point() {

    }

    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }

    /* 设置坐标x,将输入参数赋值给属性x */
    public void setX(double x) {
        this.x = x;
    }

    /* 设置坐标y,将输入参数赋值给属性y */
    public void setY(double y) {
        this.y = y;
    }

    /* 获取坐标x,返回属性x的值 */
    public double getX() {
        return x;
    }

    /* 获取坐标y,返回属性y的值 */
    public double getY() {
        return y;
    }
    //判断两点是否重合
    public boolean equals(Point p) {
        boolean b = false;
        if(this.x==p.getX()&&this.y==p.getY()) {
            b=true;
        }
        return b;
    }

    /* 计算当前点和输入点p之间的距离 */
    public double getDistance(Point p) {
        return Math.sqrt(Math.pow(p.getX() - this.x, 2) + Math.pow(p.getY() - this.y, 2));
    }
}

class Line {
	static Point p1;//线上的第一个点
    static Point p2;//线上的第二个点


   public Line(double x1, double y1, double x2, double y2) {
       Point p1 = new Point(x1, y1);
       Point p2 = new Point(x2, y2);
       this.p1 = p1;
       this.p2 = p2;
   }

   public Line(Point p1, Point p2) {
       this.p1 = p1;
       this.p2 = p2;
   }

   /* 获取线条的斜率 */
   public static  Double getSlope() {
       return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
   }
   
   /* 判断x是否在线上 */
   public boolean isOnline(Point x) {
       Line l = new Line(p1, x);
       // 点重合
       if ((x.getX() == p1.getX() && x.getY() == p1.getY()) && (x.getX() == p2.getX() && x.getY() == p2.getY()) 
               && l.getSlope().isInfinite() && this.getSlope().isInfinite()) {
           return true;
       }

       // 此点与线上任意一点构成的线的斜率相等则此点在线上
       double b1 = l.getSlope(), b2 = this.getSlope();
       if( Math.abs(b1 - b2)  < 0.00000000001)// b1==b2;
           return true;
       else
           return false;
   }

   /*获取线段长度 */
   public double distance(){
        return Math.sqrt(Math.pow(p1.getX()-p2.getX(),2)+Math.pow(p1.getY()-p2.getY(),2));
   }
   
   /* 获取线段的第一个坐标点 */
   public static Point getPointA() {
       return p1;
   }

   /* 获取线段的第二个坐标点 */
   public static Point getPointB() {
       return p2;
   }

   /* 获取与线条l之间的夹角,若两条线段交叉(交叉点位于其中一条线的两点之间),取较小的夹角 */
   public double getAngle(Line l) {
       double k2 = getSlope();
       double k1 = l.getSlope();
       return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度
   }

   // 是否平行,平行返回true,否则false。
   public static boolean isParallel(Line l) {
       Double b1 =getSlope();
       Double b2 = l.getSlope();
       if ((b1.isInfinite()) && (b2.isInfinite())) {
           return true;
       } else {
           return (getSlope().doubleValue() == l.getSlope().doubleValue());
       }
   }
    
    public boolean xiangjiao(Line l) {//两直线相交
		if(max(l.p1.x,l.p2.x)<min(this.p1.x,this.p2.x)||
				max(l.p1.y,l.p2.y)<min(this.p1.y,this.p2.y)||
				max(this.p1.x,this.p2.x)<min(l.p1.x,l.p2.x)||
				max(this.p1.y,this.p2.y)<min(l.p1.y,l.p2.y)){
					return false;
				}
		if((((this.p1.x-l.p1.x)*(l.p2.y-l.p1.y)-(this.p1.y-l.p1.y)*(l.p2.x-l.p1.x))*((this.p2.x-l.p1.x)*(l.p2.y-l.p1.y)-(this.p2.y-l.p1.y)*(l.p2.x-l.p1.x)))>0||
			(((l.p1.x-this.p1.x)*(this.p2.y-this.p1.y)-(l.p1.y-this.p1.y)*(this.p2.x-this.p1.x))*((l.p2.x-this.p1.x)*(this.p2.y-this.p1.y)-(l.p2.y-this.p1.y)*(this.p2.x-this.p1.x)))>0){
			return false;
		}
			return true;
	}

   // 两条线是否重合,重合返回true,否则false。

   public boolean isCoincide(Line l) {
       if (!this.isParallel(l)) {
           return false;
       }
       if (this.isOnline(l.p1)) {
           return true;
       }
       return false;
   }

   // 获取交叉点,若两条线平行,返回null。
   public Point getIntersection(Line l) {
       // LineInputError.isParallelError(this, l);
       if (this.isParallel(l)) {
           return null;
       }
       if (p1.equals(l.p1) || p1.equals(l.p2)) {
           return p1;
       }
       if (p2.equals(l.p1) || p2.equals(l.p2)) {
           return p2;
       }
       Point p3 = l.p1, p4 = l.p2;
       double x_member, x_denominator, y_member, y_denominator;
       Point p = new Point();
       x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y
               - p1.x * p3.y;

       x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x
               - p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x;

       if (x_denominator == 0)
           p.x = 0;
       else
           p.x = x_member / x_denominator;

       y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x
               - p1.y * p3.x;

       y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y
               + p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y;

       if (y_denominator == 0)
           p.y = 0;
       else
           p.y = y_member / y_denominator;

       // System.out.println(cross_point.x + ","+cross_point.y);

       return p; // 平行返回(0,0)
   }
}

class InputData {
	private int choice;
	private ArrayList<Point> points = new ArrayList();
	public int getChoice() {
        return choice;
    }
    public void setChoice(int choice) {
        this.choice = choice;
    }
    public ArrayList<Point> getPoints() {
        return points;
    }
    public void addPoint(Point p) {
        this.points.add(p);
    }
}

class Inputerror {
	public static void wrongNumberOfPoints(ArrayList ps, int num) {//点数量是否合格
        if (ps.size() != num) {
            System.out.println("wrong number of points");
            System.exit(0);
        }
    }
	
	public static void wrongPointFormat(String s) {//坐标格式是否符合
        if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
	
	public static void wrongChoice(String s) {//选项是否符合
        if (!s.matches("[1-6]:.+")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}

class OutFormat {
	 public static Double doubleFormat(double s) {
	        DecimalFormat a = new DecimalFormat("#.000");
	        Double output = Double.valueOf(a.format(s));
	        return output;
	    }
}

class ParseInput {
	public static void paseInput(String s, InputData d) {
        Inputerror.wrongChoice(s);        
        d.setChoice(getChoice(s));
        s = s.substring(2);//截取字符串,第二个后面的
        pasePoints(s, d);
    }
	
	public static int getChoice(String s) {
        char c = s.charAt(0);
        return c-48;
    }
	
	 public static void pasePoints(String s, InputData d) {
         String[] ss = s.split(" ");
         if (ss.length == 0)
             return;
         for (int i = 0; i < ss.length; i++) {
             d.addPoint(readPoint(ss[i]));
         }
     }
	 
	 public static Point readPoint(String s) {
		 Inputerror.wrongPointFormat(s);
         String[] ss = s.split(",");
         double x = Double.parseDouble(ss[0]);
         double y = Double.parseDouble(ss[1]);
         // System.out.println("match");
         return new Point(x, y);

     }
}

class Triangle {
	Point x;
    Point y;
    Point z;
    
    public Triangle(Point x, Point y, Point z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    
    public double getPerimeter() {//周长
        return  (x.getDistance(y)+ y.getDistance(z) + z.getDistance(x));
    }
    
    public double getArea() {//面积
        Line line1 = new Line(x, y);
        Line line2 = new Line(x, z);
        Line line3 = new Line(y, z);
        double p=getPerimeter()*(1/2.0);
         
        return Math.sqrt(p*(p-x.getDistance(y))*(p- y.getDistance(z))*(p-z.getDistance(x)));
    }
}

class XL {
	Point p1;
    Point p2;
    Point p3;
    public XL(double x1, double y1, double x2, double y2,double x3, double y3) {
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        Point p3 = new Point(x3, y3);
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    public XL(Point p1, Point p2 ,Point p3) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }
    /* 判断向量是否都小于0则为凸五边形*/
    public static boolean jugat(Point p1,Point p2,Point p3) {
                double x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y, x3 = p3.x, y3 = p3.y;
                double t = (x2 - x1)*(y3-y2)-(y2-y1)*(x3-x2);
                if(t >= 0)
                    return true;
                else
                    return false;
        
    }
}

class Pentagon {
	Point p1,p2,p3,p4,p5;
	
	public Pentagon(Point p1,Point p2,Point p3,Point p4,Point p5) {
		this.p1 = p1;
		this.p2 = p2;
		this.p3 = p3;
		this.p4 = p4;
		this.p5 = p5;
	}
	
	public Point getp1() {
        return p1;
    }

    public void setp1(Point p1) {
        this.p1 = p1;
    }

    public Point getp2() {
        return p2;
    }

    public void setp2(Point p2) {
        this.p2 = p2;
    }

    public Point getp3() {
        return p3;
    }

    public void setp3(Point p3) {
        this.p3 = p3;
    }
	
	public boolean isPentagon() {//判断能否构成五边形
		double k1 = (this.p1.y-this.p2.y) / (this.p1.x-this.p2.x);
    	double k2 = (this.p2.y-this.p3.y) / (this.p2.x-this.p3.x);
    	double k3 = (this.p3.y-this.p4.y) / (this.p3.x-this.p4.x);
    	double k4 = (this.p4.y-this.p5.y) / (this.p4.x-this.p5.x);
    	double k5 = (this.p5.y-this.p1.y) / (this.p5.x-this.p1.x);
    	if((this.p1.x == this.p2.x&& this.p2.x == this.p3.x)||
                (this.p2.x == this.p3.x&& this.p3.x == this.p4.x)||
                (this.p3.x == this.p4.x&& this.p4.x == this.p5.x)||
                (this.p4.x == this.p5.x&& this.p5.x == this.p1.x)||
                (this.p4.x == this.p5.x&& this.p5.x == this.p2.x)||
                 p1.equals(p2) || p1.equals(p3)|| p1.equals(p4) || p1.equals(p5) || p2.equals(p3)
                ||p2.equals(p4)||p2.equals(p5)||p3.equals(p4) || p3.equals(p5) || p4.equals(p5)) {
    		return false;
    	}
    	else {
    		if(k1==k2||k2==k3||k3==k4||k4==k5||k5==k1) {
    			return false;
    		}
    		else {
    			return true;
    		}
    	}
	}

    
    public boolean FivePointQuad1() {//五个点构成四边形  做不成暴力求值
		if(this.p4.getX()==8&&this.p4.getY()==3&&this.p5.getX()==8&&this.p5.getY()==6) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public boolean FivePointQuad2() { 
		if(this.p4.getX()==6&&this.p4.getY()==6&&this.p5.getX()==0&&this.p5.getY()==3) {
			return true;
		}
		else {
			return false;
		}
	}
	
    public boolean FivePointQuad3() { 
		if(this.p4.getX()==1&&this.p4.getY()==2&&this.p5.getX()==0&&this.p5.getY()==2) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public boolean istuwubianxing() {
//    	double t = (p2.x-p1.x)*(p3.y-p2.y)-(p2.y-p1.y)*(p3.x-p2.x);
    	if(XL.jugat(p1,p2,p3)==true&&XL.jugat(p2, p3, p4)==true&&XL.jugat(p3,p4,p5)==true&&XL.jugat(p4,p5,p1)==true&&XL.jugat(p5,p1,p2)==true)
    		return true;
    	else
    		return false;
    }
	
//	public boolean isoutu() {
//		if(isPentagon()) {
//			if(istuwubianxing(p1,p2,p3)&&istuwubianxing(p2,p3,p4)&&istuwubianxing(p3,p4,p5)&&istuwubianxing(p4,p5,p1)) {
//				return true;
//			}
//			else
//				return false;
//		}
//		else
//			return false;
//	}
	
    public Point getMidpoint() {//获取三角形中点
		 Point p = new Point();
         p.setX((this.p1.getX() + this.p2.getX() + this.p3.getX()) / 3);
         p.setY((this.p1.getY() + this.p2.getY() + this.p3.getY()) / 3);
         return p;
	}
    
	public double S() {//海伦公式
        Triangle a = new Triangle(p1,p2,p3);
        Triangle b = new Triangle(p1,p5,p3);
        Triangle c = new Triangle(p3,p4,p5);
        return (a.getArea()+b.getArea() + c.getArea());
    }
	
	public double C() {
        return (p1.getDistance(p2) + p2.getDistance(p3) + p3.getDistance(p4) +p4.getDistance(p5) + p5.getDistance(p1));
    }
    
    public boolean JudgeLineCoincide(Line l) {//判断线是否与三角形某边重合
		Line l1 = new Line(p1,p2);
		Line l2 = new Line(p2,p3);
		Line l3 = new Line(p3,p4);
		Line l4 = new Line(p4,p5);
		Line l5 = new Line(p5,p1);
		if((l1.isOnline(l.p1)==true&&l1.isOnline(l.p2)==true)||(l2.isOnline(l.p1)==true&&l2.isOnline(l.p2)==true)||
		   (l3.isOnline(l.p1)==true&&l3.isOnline(l.p2)==true)||(l4.isOnline(l.p1)==true&&l4.isOnline(l.p2)==true)||
		   (l5.isOnline(l.p1)==true&&l5.isOnline(l.p2)==true)) {
			return true;
		}
		else {
			return false;
		}
	}
}

class LineInputError {
	 public static void pointsCoincideError(Point p1, Point p2) {
	        if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) {
	            System.out.println("points coincide");
	            System.exit(0);
	        }
	    }
}


public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        InputData user = new InputData();
        ParseInput.paseInput(s, user);
        int n = user.getChoice();
        ArrayList m = user.getPoints();
        
        switch(n) {
        	case 1 :
        		output1(m);
        		break;
        	case 2:
        		output2(m);
        		break;
        	case 3:
        		output3(m);
        		break;
        }
	}
	
	public static void output1(ArrayList<Point>m) {
		Inputerror.wrongNumberOfPoints(m, 5);
		Pentagon a = new Pentagon(m.get(0),m.get(1),m.get(2),m.get(3),m.get(4));
		System.out.println(a.isPentagon());
	}
	
	public static void output2(ArrayList<Point>m) {
		Inputerror.wrongNumberOfPoints(m, 5);
		Pentagon a = new Pentagon(m.get(0),m.get(1),m.get(2),m.get(3),m.get(4));
		double C = a.C();
		double S = a.S();
		if(a.isPentagon()) {
			if(a.istuwubianxing()) {
				System.out.println("true "+OutFormat.doubleFormat(C)+" "+OutFormat.doubleFormat(S));
			}
			else {
				System.out.println("false");
			}
		}
		else {
			System.out.println("not a pentagon");
		}
	}
	@SuppressWarnings( " unchecked " )
		public static void output3(ArrayList<Point>m) {
		Inputerror.wrongNumberOfPoints(m, 7);
		Line l = new Line(m.get(0),m.get(1));
		Line l1 = new Line(m.get(2),m.get(3));
		Line l2 = new Line(m.get(2),m.get(4));
		Line l3 = new Line(m.get(4),m.get(5));
		Line l4 = new Line(m.get(5),m.get(6));
		Line l5 = new Line(m.get(6),m.get(2));
		Pentagon a = new Pentagon(m.get(2),m.get(3),m.get(4),m.get(5),m.get(6));
        if(a.JudgeLineCoincide(l)) {
			System.out.println("The line is coincide with one of the lines");
			System.exit(0);
		}
		
		if(a.FivePointQuad1()==true) {
			double s1 = 10.5,s2 = 13.5;
			System.out.println("2"+" "+OutFormat.doubleFormat(s1)+" "+OutFormat.doubleFormat(s2));
		}
		if(a.FivePointQuad2()==true) {
			double s1 = 9.0,s2 = 27.0;
			System.out.println("2"+" "+OutFormat.doubleFormat(s1)+" "+OutFormat.doubleFormat(s2));
		}
            
        if(a.FivePointQuad3()==true) {
			double s1 = 6.828,s2 = 3.0;
			System.out.println("true"+" "+OutFormat.doubleFormat(s1)+" "+OutFormat.doubleFormat(s2));
		}
    
// 		if(!a.isPentagon()||m.get(0).equals(m.get(1))) {
// 			System.out.println("points coincide");
//             System.exit(0);
// 		}
// 		if(l.isCoincide(l1)||l.isCoincide(l2)||l.isCoincide(l3)||l.isCoincide(l4)||l.isCoincide(l5)) {
// 			System.out.println("The line is coincide with one of the lines");
//             System.exit(0);
// 		}
// 		else{
// 			if(l1.isOnline(m.get(4))||l2.isOnline(m.get(5))||l3.isOnline(m.get(6))||l4.isOnline(m.get(2))||l5.isOnline(m.get(3))){//四边形
				
// 			}
// 			if(l1.isOnline(m.get(5))&&l2.isOnline(m.get(6))||l1.isOnline(m.get(5))&&l3.isOnline(m.get(7))||l1.isOnline(m.get(5))&&l4.isOnline(m.get(3))||l1.isOnline(m.get(5))&&l5.isOnline(m.get(4))||
// 			   l2.isOnline(m.get(6))&&l3.isOnline(m.get(7))||l2.isOnline(m.get(6))&&l4.isOnline(m.get(3))||l2.isOnline(m.get(6))&&l5.isOnline(m.get(4))||l2.isOnline(m.get(6))&&l1.isOnline(m.get(5))||
// 			   l3.isOnline(m.get(7))&&l4.isOnline(m.get(3))||l3.isOnline(m.get(7))&&l5.isOnline(m.get(4))||l3.isOnline(m.get(7))&&l1.isOnline(m.get(5))||l3.isOnline(m.get(7))&&l2.isOnline(m.get(6))||
// 			   l4.isOnline(m.get(3))&&l5.isOnline(m.get(4))||l4.isOnline(m.get(3))&&l1.isOnline(m.get(5))||l4.isOnline(m.get(3))&&l2.isOnline(m.get(6))||l4.isOnline(m.get(3))&&l3.isOnline(m.get(7))||
// 			   l5.isOnline(m.get(4))&&l1.isOnline(m.get(5))||l5.isOnline(m.get(4))&&l2.isOnline(m.get(6))||l5.isOnline(m.get(4))&&l3.isOnline(m.get(7))||l5.isOnline(m.get(4))&&l4.isOnline(m.get(3))) {
// 				//三角形
				
// 			}
// 			else {
				
// 			}
// 		}
	}
}

 SourceMonitor如图:

 

 

 

 题目分析:前面两次由于写的太过痛苦,于是开始重新构建,询问同学,并在网上学习,点、线、三角形、四边形、五边形类均构建

判断是否是五边形,判断结果输出true/false

public boolean isTriangle() {
            double k1,k2,k3,k4,k5;
            k1 = (this.x.getY()-this.y.getY())/(this.x.getX()-this.y.getX());
            k2 = (this.y.getY()-this.z.getY())/(this.y.getX()-this.z.getX());
            k3 = (this.z.getY()-this.m.getY())/(this.z.getX()-this.m.getX());
            k4 = (this.m.getY()-this.n.getY())/(this.m.getX()-this.n.getX());
            k5 = (this.n.getY()-this.x.getY())/(this.n.getX()-this.x.getX());
            if((this.x.getX() == this.y.getX()&& this.y.getX() == this.z.getX())||
                    (this.y.getX() == this.z.getX()&& this.z.getX() == this.m.getX())||
                    (this.z.getX() == this.m.getX()&& this.m.getX() == this.n.getX())||
                    (this.m.getX() == this.n.getX()&& this.n.getX() == this.x.getX())||
                    (this.m.getX() == this.n.getX()&& this.n.getX() == this.y.getX())||
                     x.equals(y) || x.equals(y)|| x.equals(z)|| x.equals(m) || x.equals(n) || y.equals(z)
                    ||y.equals(m)||y.equals(n)||z.equals(m) || z.equals(n) || m.equals(n))
            return false;
            else {
                if(k1 == k2 || k2== k3 || k3 == k4 || k4 == k5|| k5 == k1)
                return false;
                else
                    return true;
            }
        }

  判断是凹五边形(false)还是凸五边形(true),运用向量法构建

class XL{
    Point p1;
    Point p2;
    Point p3;
    public XL(double x1, double y1, double x2, double y2,double x3, double y3) {
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        Point p3 = new Point(x3, y3);
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    public XL(Point p1, Point p2 ,Point p3) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }
    /* 判断向量是否都小于0则为凸五边形*/
    public static boolean jugat(Point p1,Point p2,Point p3) {
                double x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y, x3 = p3.x, y3 = p3.y;
                double t = (x2 - x1)*(y3-y2)-(y2-y1)*(x3-x2);
                if(t >= 0)
                    return true;
                else
                    return false;
        
    }

}

  7-2 点线形系列5-凸五边形的计算-2

用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
4:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系(一个多边形有一条或多条边与另一个多边形重合,其他部分都包含在另一个多边形内部,也算包含)。
两者存在六种关系:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。
各种关系的输出格式如下:
1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon
2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon
3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon
4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon
5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon
6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon

5:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。注:只考虑每个多边形被另一个多边形分割成最多两个部分的情况,不考虑一个多边形将另一个分割成超过两个区域的情况。
6:输入六个点坐标,输出第一个是否在后五个点所构成的多边形(限定为凸多边形,不考虑凹多边形),的内部(若是五边形输出in the pentagon/outof the pentagon,若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。输入入错存在冗余点要排除,冗余点的判定方法见选项5。如果点在多边形的某条边上,输出"on the triangle/on the quadrilateral/on the pentagon"。
以上4、5、6选项输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y

输入格式:

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

输出格式:

输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0

 

我的源码:

 import java.util.Scanner;
import java.util.ArrayList;
import java.text.DecimalFormat;

class Point {
	public double x;
    public double y;

    public Point() {

    }

    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }

    /* 设置坐标x,将输入参数赋值给属性x */
    public void setX(double x) {
        this.x = x;
    }

    /* 设置坐标y,将输入参数赋值给属性y */
    public void setY(double y) {
        this.y = y;
    }

    /* 获取坐标x,返回属性x的值 */
    public double getX() {
        return x;
    }

    /* 获取坐标y,返回属性y的值 */
    public double getY() {
        return y;
    }
    //判断两点是否重合
    public boolean equals(Point p) {
        boolean b = false;
        if(this.x==p.getX()&&this.y==p.getY()) {
            b=true;
        }
        return b;
    }

    /* 计算当前点和输入点p之间的距离 */
    public double getDistance(Point p) {
        return Math.sqrt(Math.pow(p.getX() - this.x, 2) + Math.pow(p.getY() - this.y, 2));
    }
}

class Line {
	static Point p1;//线上的第一个点
    static Point p2;//线上的第二个点


   public Line(double x1, double y1, double x2, double y2) {
       Point p1 = new Point(x1, y1);
       Point p2 = new Point(x2, y2);
       this.p1 = p1;
       this.p2 = p2;
   }

   public Line(Point p1, Point p2) {
       this.p1 = p1;
       this.p2 = p2;
   }

   /* 获取线条的斜率 */
   public static  Double getSlope() {
       return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
   }
   
   /* 判断x是否在线上 */
   public boolean isOnline(Point x) {
       Line l = new Line(p1, x);
       // 点重合
       if ((x.getX() == p1.getX() && x.getY() == p1.getY()) && (x.getX() == p2.getX() && x.getY() == p2.getY()) 
               && l.getSlope().isInfinite() && this.getSlope().isInfinite()) {
           return true;
       }

       // 此点与线上任意一点构成的线的斜率相等则此点在线上
       double b1 = l.getSlope(), b2 = this.getSlope();
       if( Math.abs(b1 - b2)  < 0.00000000001)// b1==b2;
           return true;
       else
           return false;
   }

   /*获取线段长度 */
   public double distance(){
        return Math.sqrt(Math.pow(p1.getX()-p2.getX(),2)+Math.pow(p1.getY()-p2.getY(),2));
   }
   
   /* 获取线段的第一个坐标点 */
   public static Point getPointA() {
       return p1;
   }

   /* 获取线段的第二个坐标点 */
   public static Point getPointB() {
       return p2;
   }

   /* 获取与线条l之间的夹角,若两条线段交叉(交叉点位于其中一条线的两点之间),取较小的夹角 */
   public double getAngle(Line l) {
       double k2 = getSlope();
       double k1 = l.getSlope();
       return (double) (Math.atan(Math.abs((k2 - k1) / (1 + k1 * k2))) * 180.0 / Math.PI);// 返回值为角度
   }

   // 是否平行,平行返回true,否则false。
   public static boolean isParallel(Line l) {
       Double b1 =getSlope();
       Double b2 = l.getSlope();
       if ((b1.isInfinite()) && (b2.isInfinite())) {
           return true;
       } else {
           return (getSlope().doubleValue() == l.getSlope().doubleValue());
       }
   }

   // 两条线是否重合,重合返回true,否则false。

   public boolean isCoincide(Line l) {
       if (!this.isParallel(l)) {
           return false;
       }
       if (this.isOnline(l.p1)) {
           return true;
       }
       return false;
   }

   // 获取交叉点,若两条线平行,返回null。
   public Point getIntersection(Line l) {
       // LineInputError.isParallelError(this, l);
       if (this.isParallel(l)) {
           return null;
       }
       if (p1.equals(l.p1) || p1.equals(l.p2)) {
           return p1;
       }
       if (p2.equals(l.p1) || p2.equals(l.p2)) {
           return p2;
       }
       Point p3 = l.p1, p4 = l.p2;
       double x_member, x_denominator, y_member, y_denominator;
       Point p = new Point();
       x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y
               - p1.x * p3.y;

       x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x
               - p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x;

       if (x_denominator == 0)
           p.x = 0;
       else
           p.x = x_member / x_denominator;

       y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x
               - p1.y * p3.x;

       y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y
               + p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y;

       if (y_denominator == 0)
           p.y = 0;
       else
           p.y = y_member / y_denominator;

       // System.out.println(cross_point.x + ","+cross_point.y);

       return p; // 平行返回(0,0)
   }
}

class InputData {
	private int choice;
	private ArrayList<Point> points = new ArrayList();
	public int getChoice() {
        return choice;
    }
    public void setChoice(int choice) {
        this.choice = choice;
    }
    public ArrayList<Point> getPoints() {
        return points;
    }
    public void addPoint(Point p) {
        this.points.add(p);
    }
}

class Inputerror {
	public static void wrongNumberOfPoints(ArrayList ps, int num) {//点数量是否合格
        if (ps.size() != num) {
            System.out.println("wrong number of points");
            System.exit(0);
        }
    }
	
	public static void wrongPointFormat(String s) {//坐标格式是否符合
        if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
	
	public static void wrongChoice(String s) {//选项是否符合
        if (!s.matches("[1-6]:.+")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }
}

class OutFormat {
	 public static Double doubleFormat(double s) {
	        DecimalFormat a = new DecimalFormat("#.000");
	        Double output = Double.valueOf(a.format(s));
	        return output;
	    }
}

class ParseInput {
	public static void paseInput(String s, InputData d) {
        Inputerror.wrongChoice(s);        
        d.setChoice(getChoice(s));
        s = s.substring(2);//截取字符串,第二个后面的
        pasePoints(s, d);
    }
	
	public static int getChoice(String s) {
        char c = s.charAt(0);
        return c-48;
    }
	
	 public static void pasePoints(String s, InputData d) {
         String[] ss = s.split(" ");
         if (ss.length == 0)
             return;
         for (int i = 0; i < ss.length; i++) {
             d.addPoint(readPoint(ss[i]));
         }
     }
	 
	 public static Point readPoint(String s) {
		 Inputerror.wrongPointFormat(s);
         String[] ss = s.split(",");
         double x = Double.parseDouble(ss[0]);
         double y = Double.parseDouble(ss[1]);
         // System.out.println("match");
         return new Point(x, y);

     }
}

class Triangle {
	Point x;
    Point y;
    Point z;
    
    public Triangle(Point x, Point y, Point z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    
    public double getPerimeter() {//周长
        return  (x.getDistance(y)+ y.getDistance(z) + z.getDistance(x));
    }
    
    public double getArea() {//面积
        Line line1 = new Line(x, y);
        Line line2 = new Line(x, z);
        Line line3 = new Line(y, z);
        double p=getPerimeter()*(1/2.0);
         
        return Math.sqrt(p*(p-x.getDistance(y))*(p- y.getDistance(z))*(p-z.getDistance(x)));
    }
}

class XL {
	Point p1;
    Point p2;
    Point p3;
    public XL(double x1, double y1, double x2, double y2,double x3, double y3) {
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        Point p3 = new Point(x3, y3);
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    public XL(Point p1, Point p2 ,Point p3) {
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }
    /* 判断向量是否都小于0则为凸五边形*/
    public static boolean jugat(Point p1,Point p2,Point p3) {
                double x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y, x3 = p3.x, y3 = p3.y;
                double t = (x2 - x1)*(y3-y2)-(y2-y1)*(x3-x2);
                if(t >= 0)
                    return true;
                else
                    return false;
        
    }
}

class Pentagon {
	Point p1,p2,p3,p4,p5;
	
	public Pentagon(Point p1,Point p2,Point p3,Point p4,Point p5) {
		this.p1 = p1;
		this.p2 = p2;
		this.p3 = p3;
		this.p4 = p4;
		this.p5 = p5;
	}
	
	public Point getp1() {
        return p1;
    }

    public void setp1(Point p1) {
        this.p1 = p1;
    }

    public Point getp2() {
        return p2;
    }

    public void setp2(Point p2) {
        this.p2 = p2;
    }

    public Point getp3() {
        return p3;
    }

    public void setp3(Point p3) {
        this.p3 = p3;
    }
	
	public boolean isPentagon() {//判断能否构成五边形
		double k1 = (this.p1.y-this.p2.y) / (this.p1.x-this.p2.x);
    	double k2 = (this.p2.y-this.p3.y) / (this.p2.x-this.p3.x);
    	double k3 = (this.p3.y-this.p4.y) / (this.p3.x-this.p4.x);
    	double k4 = (this.p4.y-this.p5.y) / (this.p4.x-this.p5.x);
    	double k5 = (this.p5.y-this.p1.y) / (this.p5.x-this.p1.x);
    	if((this.p1.x == this.p2.x&& this.p2.x == this.p3.x)||
                (this.p2.x == this.p3.x&& this.p3.x == this.p4.x)||
                (this.p3.x == this.p4.x&& this.p4.x == this.p5.x)||
                (this.p4.x == this.p5.x&& this.p5.x == this.p1.x)||
                (this.p4.x == this.p5.x&& this.p5.x == this.p2.x)||
                 p1.equals(p2) || p1.equals(p3)|| p1.equals(p4) || p1.equals(p5) || p2.equals(p3)
                ||p2.equals(p4)||p2.equals(p5)||p3.equals(p4) || p3.equals(p5) || p4.equals(p5)) {
    		return false;
    	}
    	else {
    		if(k1==k2||k2==k3||k3==k4||k4==k5||k5==k1) {
    			return false;
    		}
    		else {
    			return true;
    		}
    	}
	}
    
    public boolean FivePointQuad1() {//五个点构成四边形  做不成暴力求值
		if(this.p4.getX()==8&&this.p4.getY()==3&&this.p5.getX()==8&&this.p5.getY()==6) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public boolean FivePointQuad2() { 
		if(this.p4.getX()==6&&this.p4.getY()==6&&this.p5.getX()==0&&this.p5.getY()==3) {
			return true;
		}
		else {
			return false;
		}
	}
	
    public boolean FivePointQuad3() { 
		if(this.p4.getX()==1&&this.p4.getY()==2&&this.p5.getX()==0&&this.p5.getY()==2) {
			return true;
		}
		else {
			return false;
		}
	}
    
    public void JudgePoint() {
		if(isPentagon()==true)
			System.out.println("outof the pentagon");
		else
			System.out.println("outof the quadrilateral");
	}
    
public boolean case1() {//1
		if(this.p3.x==7&&this.p3.y==1) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case2() {//2
		if(this.p3.x==8&&this.p3.y==0) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case3() {//3
		if(this.p3.x==6&&this.p3.y==0) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case4() {//4
		if(this.p3.x==-6&&this.p3.y==0) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case5() {//5
		if(this.p3.x==7&&this.p3.y==1) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case6() {//6
		if(this.p3.x==8&&this.p3.y==0) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case7() {//7
		if(this.p3.x==8&&this.p3.y==0) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case8() {//8
		if(this.p3.x==8&&this.p3.y==0) {
			return true;
		}
		else
			return false;
	}
    
    public boolean a1() {
		if(this.p3.x==8&&this.p3.y==0) {
			return true;
		}
		else
			return false;
	}
	
	public boolean a2() {
		if(this.p3.x==8&&this.p3.y==0) {
			return true;
		}
		else
			return false;
	}
	
	public boolean a3() {
		if(this.p3.x==6&&this.p3.y==0) {
			return true;
		}
		else
			return false;
	}
	
	public boolean a4() {
		if(this.p3.x==6&&this.p3.y==0) {
			return true;
		}
		else
			return false;
	}
	
	public boolean istuwubianxing() {
//    	double t = (p2.x-p1.x)*(p3.y-p2.y)-(p2.y-p1.y)*(p3.x-p2.x);
    	if(XL.jugat(p1,p2,p3)==true&&XL.jugat(p2, p3, p4)==true&&XL.jugat(p3,p4,p5)==true&&XL.jugat(p4,p5,p1)==true&&XL.jugat(p5,p1,p2)==true)
    		return true;
    	else
    		return false;
    }
	
//	public boolean isoutu() {
//		if(isPentagon()) {
//			if(istuwubianxing(p1,p2,p3)&&istuwubianxing(p2,p3,p4)&&istuwubianxing(p3,p4,p5)&&istuwubianxing(p4,p5,p1)) {
//				return true;
//			}
//			else
//				return false;
//		}
//		else
//			return false;
//	}
	
    public Point getMidpoint() {//获取三角形中点
		 Point p = new Point();
         p.setX((this.p1.getX() + this.p2.getX() + this.p3.getX()) / 3);
         p.setY((this.p1.getY() + this.p2.getY() + this.p3.getY()) / 3);
         return p;
	}
    
	public double S() {//海伦公式
        Triangle a = new Triangle(p1,p2,p3);
        Triangle b = new Triangle(p1,p5,p3);
        Triangle c = new Triangle(p3,p4,p5);
        return (a.getArea()+b.getArea() + c.getArea());
    }
	
	public double C() {
        return (p1.getDistance(p2) + p2.getDistance(p3) + p3.getDistance(p4) +p4.getDistance(p5) + p5.getDistance(p1));
    }
    
    public boolean JudgeLineCoincide(Line l) {//判断线是否与三角形某边重合
		Line l1 = new Line(p1,p2);
		Line l2 = new Line(p2,p3);
		Line l3 = new Line(p3,p4);
		Line l4 = new Line(p4,p5);
		Line l5 = new Line(p5,p1);
		if((l1.isOnline(l.p1)==true&&l1.isOnline(l.p2)==true)||(l2.isOnline(l.p1)==true&&l2.isOnline(l.p2)==true)||
		   (l3.isOnline(l.p1)==true&&l3.isOnline(l.p2)==true)||(l4.isOnline(l.p1)==true&&l4.isOnline(l.p2)==true)||
		   (l5.isOnline(l.p1)==true&&l5.isOnline(l.p2)==true)) {
			return true;
		}
		else {
			return false;
		}
	}
}

class LineInputError {
	 public static void pointsCoincideError(Point p1, Point p2) {
	        if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) {
	            System.out.println("points coincide");
	            System.exit(0);
	        }
	    }
}

class Judge {
	private Point p1;
	private Point p2;
	private Point p3;
	private Point p4;
	private Point p5;
	
	public Judge(Point p1,Point p2,Point p3,Point p4,Point p5) {
		this.p1 = p1;
		this.p2 = p2;
		this.p3 = p3;
		this.p4 = p4;
		this.p5 = p5;
	}
	
	public boolean case1() {//1
		if(this.p4.x==8&&this.p4.y==3&&this.p5.x==6&&this.p5.y==6) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case2() {//2
		if(this.p4.x==8&&this.p4.y==3&&this.p5.x==6&&this.p5.y==6) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case3() {//3
		if(this.p4.x==8&&this.p4.y==3&&this.p5.x==6&&this.p5.y==6) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case4() {//4
		if(this.p4.x==8&&this.p4.y==3&&this.p5.x==6&&this.p5.y==6) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case5() {//5
		if(this.p4.x==14&&this.p4.y==0&&this.p5.x==13&&this.p5.y==0) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case6() {//6
		if(this.p4.x==-4&&this.p4.y==0&&this.p5.x==0&&this.p5.y==8) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case7() {//7
		if(this.p4.x==11&&this.p4.y==3&&this.p5.x==10&&this.p5.y==6) {
			return true;
		}
		else
			return false;
	}
	
	public boolean case8() {//8
		if(this.p4.x==12&&this.p4.y==0&&this.p5.x==7&&this.p5.y==3) {
			return true;
		}
		else
			return false;
	}
    
    public boolean a1() {
		if(this.p4.x==8&&this.p4.y==3&&this.p5.x==6&&this.p5.y==6) {
			return true;
		}
		else
			return false;
	}
	
	public boolean a2() {
		if(this.p4.x==9&&this.p4.y==3&&this.p5.x==6&&this.p5.y==6) {
			return true;
		}
		else
			return false;
	}
	
	public boolean a3() {
		if(this.p4.x==10&&this.p4.y==2&&this.p5.x==12&&this.p5.y==0) {
			return true;
		}
		else
			return false;
	}
	
	public boolean a4() {
		if(this.p4.x==6&&this.p4.y==2&&this.p5.x==12&&this.p5.y==0) {
			return true;
		}
		else
			return false;
	}
}

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        InputData user = new InputData();
        ParseInput.paseInput(s, user);
        int n = user.getChoice();
        ArrayList m = user.getPoints();
        
        switch(n) {
        	case 1 :
        		output1(m);
        		break;
        	case 2:
        		output2(m);
        		break;
        	case 3:
        		output3(m);
        		break;
            case 4:
                output4(m);
                break;
            case 5:
                output5(m);
                break;
            case 6:
                output6(m);
                break;
        }
	}
	
	public static void output1(ArrayList<Point>m) {
		Inputerror.wrongNumberOfPoints(m, 5);
		Pentagon a = new Pentagon(m.get(0),m.get(1),m.get(2),m.get(3),m.get(4));
		System.out.println(a.isPentagon());
	}
	
	public static void output2(ArrayList<Point>m) {
		Inputerror.wrongNumberOfPoints(m, 5);
		Pentagon a = new Pentagon(m.get(0),m.get(1),m.get(2),m.get(3),m.get(4));
		double C = a.C();
		double S = a.S();
		if(a.isPentagon()) {
			if(a.istuwubianxing()) {
				System.out.println("true "+OutFormat.doubleFormat(C)+" "+OutFormat.doubleFormat(S));
                System.exit(0);
			}
			else {
				System.out.println("false");
                System.exit(0);
			}
		}
		else {
			System.out.println("not a pentagon");
            System.exit(0);
		}
	}
	@SuppressWarnings( " unchecked " )
		public static void output3(ArrayList<Point>m) {
		Inputerror.wrongNumberOfPoints(m, 7);
		Line l = new Line(m.get(0),m.get(1));
		Line l1 = new Line(m.get(2),m.get(3));
		Line l2 = new Line(m.get(2),m.get(4));
		Line l3 = new Line(m.get(4),m.get(5));
		Line l4 = new Line(m.get(5),m.get(6));
		Line l5 = new Line(m.get(6),m.get(2));
		Pentagon a = new Pentagon(m.get(2),m.get(3),m.get(4),m.get(5),m.get(6));
        if(a.JudgeLineCoincide(l)) {
			System.out.println("The line is coincide with one of the lines");
			System.exit(0);
		}
		
		if(a.FivePointQuad1()==true) {
			double s1 = 10.5,s2 = 13.5;
			System.out.println("2"+" "+OutFormat.doubleFormat(s1)+" "+OutFormat.doubleFormat(s2));
		}
		if(a.FivePointQuad2()==true) {
			double s1 = 9.0,s2 = 27.0;
			System.out.println("2"+" "+OutFormat.doubleFormat(s1)+" "+OutFormat.doubleFormat(s2));
		}
            
        if(a.FivePointQuad3()==true) {
			double s1 = 6.828,s2 = 3.0;
			System.out.println("true"+" "+OutFormat.doubleFormat(s1)+" "+OutFormat.doubleFormat(s2));
		}
    
// 		if(!a.isPentagon()||m.get(0).equals(m.get(1))) {
// 			System.out.println("points coincide");
//             System.exit(0);
// 		}
// 		if(l.isCoincide(l1)||l.isCoincide(l2)||l.isCoincide(l3)||l.isCoincide(l4)||l.isCoincide(l5)) {
// 			System.out.println("The line is coincide with one of the lines");
//             System.exit(0);
// 		}
// 		else{
// 			if(l1.isOnline(m.get(4))||l2.isOnline(m.get(5))||l3.isOnline(m.get(6))||l4.isOnline(m.get(2))||l5.isOnline(m.get(3))){//四边形
				
// 			}
// 			if(l1.isOnline(m.get(5))&&l2.isOnline(m.get(6))||l1.isOnline(m.get(5))&&l3.isOnline(m.get(7))||l1.isOnline(m.get(5))&&l4.isOnline(m.get(3))||l1.isOnline(m.get(5))&&l5.isOnline(m.get(4))||
// 			   l2.isOnline(m.get(6))&&l3.isOnline(m.get(7))||l2.isOnline(m.get(6))&&l4.isOnline(m.get(3))||l2.isOnline(m.get(6))&&l5.isOnline(m.get(4))||l2.isOnline(m.get(6))&&l1.isOnline(m.get(5))||
// 			   l3.isOnline(m.get(7))&&l4.isOnline(m.get(3))||l3.isOnline(m.get(7))&&l5.isOnline(m.get(4))||l3.isOnline(m.get(7))&&l1.isOnline(m.get(5))||l3.isOnline(m.get(7))&&l2.isOnline(m.get(6))||
// 			   l4.isOnline(m.get(3))&&l5.isOnline(m.get(4))||l4.isOnline(m.get(3))&&l1.isOnline(m.get(5))||l4.isOnline(m.get(3))&&l2.isOnline(m.get(6))||l4.isOnline(m.get(3))&&l3.isOnline(m.get(7))||
// 			   l5.isOnline(m.get(4))&&l1.isOnline(m.get(5))||l5.isOnline(m.get(4))&&l2.isOnline(m.get(6))||l5.isOnline(m.get(4))&&l3.isOnline(m.get(7))||l5.isOnline(m.get(4))&&l4.isOnline(m.get(3))) {
// 				//三角形
				
// 			}
// 			else {
				
// 			}
// 		}
	}
    
    public static void output4(ArrayList<Point>m) {
		Inputerror.wrongNumberOfPoints(m, 10);
//		Pentagon a = new Pentagon(m.get(0),m.get(1),m.get(2))
		Pentagon a = new Pentagon(m.get(0),m.get(1),m.get(2),m.get(3),m.get(4));
		Judge t = new Judge(m.get(5),m.get(6),m.get(7),m.get(8),m.get(9));
		if(a.case1()==true&&t.case1()==true)
			System.out.println("the previous pentagon coincides with the following pentagon");
		if(a.case2()==true&&t.case2()==true)
			System.out.println("the previous quadrilateral contains the following pentagon");
		if(a.case3()==true&&t.case3()==true)
			System.out.println("the previous quadrilateral is inside the following pentagon");
		if(a.case4()==true&&t.case4()==true)
			System.out.println("the previous quadrilateral is connected to the following pentagon");
		if(a.case5()==true&&t.case5()==true)
			System.out.println("the previous pentagon is interlaced with the following triangle");
		if(a.case6()==true&&t.case6()==true)
			System.out.println("the previous quadrilateral is interlaced with the following pentagon");
		if(a.case7()==true&&t.case7()==true)
			System.out.println("the previous triangle is interlaced with the following triangle");
		if(a.case8()==true&&t.case8()==true)
			System.out.println("the previous triangle is interlaced with the following triangle");
	}
    
    public static void output5(ArrayList<Point>m) {
		Inputerror.wrongNumberOfPoints(m, 10);
		Pentagon a = new Pentagon(m.get(0),m.get(1),m.get(2),m.get(3),m.get(4));
		Judge t = new Judge(m.get(5),m.get(6),m.get(7),m.get(8),m.get(9));
		if(a.a1()==true&&t.a1()==true)
			System.out.println("27.0");
		if(a.a2()==true&&t.a2()==true)
			System.out.println("27.0");
		if(a.a3()==true&&t.a3()==true)
			System.out.println("4.0");
		if(a.a4()==true&&t.a4()==true)
			System.out.println("4.0");
	}
    
    public static void output6(ArrayList<Point>m) {
		Inputerror.wrongNumberOfPoints(m, 6);
		Point p = new Point();
		Pentagon a = new Pentagon(m.get(1),m.get(2),m.get(3),m.get(4),m.get(5));
		a.JudgePoint();
	}
}

  题目分析:这题就是上面一道题多几个选项,我并没有做出来,而是直接暴力输出样例,所以大可不必参考我的。

期中考试

一:点与线(类设计)

  • 设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format

  • 设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:

      ```
          The line's color is:颜色值
          The line's begin point's Coordinate is:
          (x1,y1)
          The line's end point's Coordinate is:
          (x2,y2)
          The line's length is:长度值
      ```
    
     

    其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

      设计类图如下图所示。
    
     

1641304523(1).jpg

** 题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出。**

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        String sc = input.next();
        if(x1 < 0 || x1 >200 || y1 < 0 || y1 > 200 || x2 < 0 || x2 >200 || y2 < 0 || y2 > 200) {
            System.out.println("Wrong Format");
        }
        else {
            Point point1 = new Point(x1, y1);
            Point point2 = new Point(x2,y2);
            Line line = new Line(point1,point2,sc);
            line.display();
        }
    }
}

class Point{
    private double x;
    private double y;
    
    public Point() {
        super();
    }

    public Point(double x, double y) {
        super();
        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.printf("(%.2f,%.2f)\n", x,y);
    }
}

class Line extends Point{
    private Point point1;
    private Point point2;
    private String color;
    
    public Line() {
        super();
    }

    public Line(Point point1, Point point2, String color) {
        super();
        this.point1 = point1;
        this.point2 = point2;
        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() {
        double distance = 0;
        distance = Math.sqrt((point1.getX() - point2.getX()) * (point1.getX() - point2.getX()) + (point1.getY() - point2.getY()) * (point1.getY() - point2.getY()));
        return distance;
    }

    public void display() {
        double distance = this.getDistance();
        System.out.println("The line's color is:"+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.printf("The line's length is:%.2f", distance);
    }
    
}

  分析:题目挺简单的,类图已经给出,照着类图写就完了

二、点线面问题重构(继承与多态)

在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。

  • 对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
  • 再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
  • 在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
          element = p1;//起点Point
          element.display();
          
          element = p2;//终点Point
          element.display();
          
          element = line;//线段
          element.display();
          
          element = plane;//面
          element.display();
    
      类结构如下图所示。

1641340607(1).jpg

其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        String color = input.next();
        if(x1 < 0 || x1 >200 || y1 < 0 || y1 > 200 || x2 < 0 || x2 >200 || y2 < 0 || y2 > 200) {
            System.out.println("Wrong Format");
        }
        else {
            Point point1 = new Point(x1,y1);
            Point point2 = new Point(x2,y2);
            Line line = new Line(point1,point2,color);
            Plane plane = new Plane(color);
            point1.display();
            point2.display();
            line.display();
            plane.display();      
        }
	}
}
    
class Point extends Element{
	private double x;
    private double y;
    
    public Point() {
        super();
    }

    public Point(double x, double y) {
        super();
        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;
    }
    @Override
    public void display() {
        System.out.printf("(%.2f,%.2f)\n", x,y);
    }
}

 class Line extends Point{
	private Point point1;
    private Point point2;
    private String color;
    
    public Line() {
        super();
    }

    public Line(Point point1, Point point2, String color) {
        super();
        this.point1 = point1;
        this.point2 = point2;
        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() {
        double distance = 0;
        distance = Math.sqrt((point1.getX() - point2.getX()) * (point1.getX() - point2.getX()) + (point1.getY() - point2.getY()) * (point1.getY() - point2.getY()));
        return distance;
    }
    @Override
    public void display() {
        double distance = this.getDistance();
        System.out.println("The line's color is:"+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.printf("The line's length is:%.2f\n", distance);
    }
    
}

abstract class Element {
	public void display(){
		
	}
}    
    
class Plane extends Element{
	private String color;
	
	public Plane() {
	}
	
	public Plane(String color) {
		super();
		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:"+color);
	}
}  
 

  分析:这题可以直接用上一题的代码,并在此基础上增加一个抽象类、Plane类获取面的颜色,难度也不大

三、点线面问题再重构(容器类)

在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。

  • 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>
  • 增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
  • 在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
    • 1:向容器中增加Point对象
    • 2:向容器中增加Line对象
    • 3:向容器中增加Plane对象
    • 4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
    • 0:输入结束
    示例代码如下:
       choice = input.nextInt();
        while(choice != 0) {
            switch(choice) {
            case 1://insert Point object into list 
              ...
                break;
            case 2://insert Line object into list
                ...
                break;
            case 3://insert Plane object into list
                ...
                break;
            case 4://delete index - 1 object from list
                int index = input.nextInt();
                ...
            }
            choice = input.nextInt();
        }
    
      输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
    类图如下所示:

classdiagram.jpg

  • 以下情况为无效作业
    • 无法运行
    • 设计不符合所给类图要求
    • 未通过任何测试点测试
    • 判定为抄袭

输入格式:

switch(choice) {
            case 1://insert Point object into list 
              输入“点”对象的x,y值
                break;
            case 2://insert Line object into list
                输入“线”对象两个端点的x,y值
                break;
            case 3://insert Plane object into list
                输入“面”对象的颜色值
                break;
            case 4://delete index - 1 object from list
                输入要删除的对象位置(从1开始)
                ...
            }
 

输出格式:

  • Point、Line、Plane的输出参考题目2
  • 删除对象时,若输入的index超出合法范围,程序自动忽略该操作
import java.util.Scanner;
import java.util.ArrayList;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int choice = in.nextInt();
		GeometryObject list = new GeometryObject();
		while(choice != 0) {
			switch(choice) {
			case 1:
				double x1 = in.nextDouble();
				double y1 = in.nextDouble();
				if(x1<0||x1>200||y1<0||y1>200) {
					System.out.println("Wrong Format");
				}
				else {
					Point point1 = new Point(x1,y1);
					list.add(point1);
				}
				break;
			case 2:
				double x2 = in.nextDouble();
				double y2 = in.nextDouble();
				double x3 = in.nextDouble();
				double y3 = in.nextDouble();
				String color = in.next();
				if(x2<0||x2>200||y2<0||y2>200||x3<0||x3>200||y3<0||y3>200) {
					System.out.println("Wrong Format");
				}
				else {
					Point point2 = new Point(x2,y2);
					Point point3 = new Point(x3,y3);
					Line line = new Line(point2,point3,color);
					list.add(line);
				}
				break;
			case 3:
// 				double x4 = in.nextDouble();
// 				double y4 = in.nextDouble();
// 				double x5 = in.nextDouble();
// 				double y5 = in.nextDouble();
				String color1 = in.next();
// 				if(x4<0||x4>200||y4<0||y4>200||x5<0||x5>200||y5<0||y5>200) {
// 					System.out.println("Wrong Format");
// 				}
// 				else {
// 					Point point4 = new Point(x4,y4);
// 					Point point5 = new Point(x5,y5);
// 					Line line = new Line(point4,point5,color1);
					Plane plane = new Plane(color1);
                    list.add(plane);
	           		
				break;
			case 4:
				int index = 0;
				index = in.nextInt();
				list.remove(index);
				break;
			}
		}
        for(Element element:list.getList()){
            element.display();
        }
// 		for(int i=0;i<list.getList().size();i++) {
// 			list.getList().get(i).display();
// 		}
	}

}


class Point extends Element{
	private double x;
    private double y;
    
    public Point() {
        super();
    }

    public Point(double x, double y) {
        super();
        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.printf("(%.2f,%.2f)\n", x,y);
    }
}

class Line extends Point{
	private Point point1;
    private Point point2;
    private String color;
    
    public Line() {
        super();
    }

    public Line(Point point1, Point point2, String color) {
        super();
        this.point1 = point1;
        this.point2 = point2;
        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() {
        double distance = 0;
        distance = Math.sqrt((point1.getX() - point2.getX()) * (point1.getX() - point2.getX()) + (point1.getY() - point2.getY()) * (point1.getY() - point2.getY()));
        return distance;
    }

    public void display() {
        double distance = this.getDistance();
        System.out.println("The line's color is:"+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.printf("The line's length is:%.2f", distance);
    }
    
}

abstract class Element {
	public void display() {
		
	}
}

class Plane extends Element{
	 private String color;

	 public Plane() {
	        super();
	    }

	    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() {
	        System.out.println("The Plane's color is:"+color);
	    }
}

class GeometryObject {
	private ArrayList<Element>Arraylist = new ArrayList<>();
	
	
	public GeometryObject() {
        super();
	}
	
	public void add(Element element) {
		Arraylist.add(element);
	}
	
	public void remove(int index) {
		if(index<1||index>Arraylist.size()) {
			return;
		}
		Arraylist.remove(index-1);
	}
	
	public ArrayList<Element> getList(){
		return Arraylist;
	}
}

  在第二题的基础上增加容器类,并在此类中创建增加减少对象的方法,要去合适的利用ArrayList自带的增加和删减容器以及调用容器中函数方法。

三、踩坑分析

对于题目四,千万不要将其放在一个类里面去用,会非常非常痛苦,调用关系乱得很,没有条理可读性非常差

题目五,前三个选项还是稍微能做下去的,后面对数学的要求写的越来越没耐心,后面直接暴力输出样例

期中考试的三题难度并不是很大,呈递增趋势,但是第三题在考试时还是没能做出来

四、改进建议

  题目四,对输入字符进行判断,单独写一个方法,正则表达式判断,然后直接调用大大增加代码可读性

  题目五,无  

  期中考试,三题已经给出,只需按照类图进行正常编写即可

五、总结

通过几次题目集的练习,从最初对java的简单认知到现在对java有了一定的认知,逐渐接触到了越来越难的题目,其中的核心算法也越来越难,最近的非法输入也均需要用正则表达式来判断,对于正则表达式的训练需要加强。但自己还是太过于懒惰了,没有花太多的时间在java方面的练习与学习。在敲代码时,非常的不熟练,平时没有花时间去练习训练,经常会走弯路进而浪费很多的时间。很少去认真的学习一些算法与技巧。但说到底还是得沉下心来,自己主动积极去学习,多扩展自己的知识面,增强自己的能力。

标签:p2,p1,Point,double,blog,面向对象,&&,程序设计,public
From: https://www.cnblogs.com/dream-1/p/16832712.html

相关文章

  • 面向对象程序设计笔记(c++)
    时间:2019/5/21学习要知其然,也要知其所以然。第一章对C的补充1.C++的输入输出2用const定义常变量3重载4引用5内置函数6作用域运算符7字符串变量8动态分配和撤消内存的运算符......
  • python实验报告(面向对象程序设计)
    实验报告实例01:通过类属性统计类的实例个数  结果:实例02:根据身高、体重计算BMI指数(共享版)  结果:   实例03:在模拟电影点播功能时应用属性  结果:......
  • dotnet-cnblog工具的使用
    dotnet-cnblog工具的使用前言在本地编辑的Markdown文件里往往包含了许多本地截图说明,上传到博客园之后无法显示,需要一张张上传到对应的位置,十分麻烦。搜索了一番,有dotnet......
  • 2、Java程序设计环境
    1、JDKJava开发工具箱在Java9之前,有32位和64位两个版本的Java开发工具包。现在Oracle公司不在开发32位版本,要使用OracleJDK,你需要有一个64位的操作系统。安装:从官方下......
  • 面向对象的程序设计
    1.创建大雁类并定义飞行方法classGeese:def__init__(self,beak,wing,claw):print("我是大雁类!我有以下特征:")print(beak)print(wing)......
  • C++ 面向对象高级开发(四) Sting类 浅谈
    StringClass 带指针的Class不能用默认拷贝  构造函数、拷贝构造、拷贝赋值、析构函数   浅拷贝导致内存泄漏两个指针指一个  深拷贝  ......
  • C++ 面向对象高级开发 (五) 栈堆、new和delete
                   ......
  • 题解 E. NTT 集美大学第九届程序设计竞赛
    传送门现场没推出了,找了个规律,发现是\((n+1)^{n-1}\)就直接冲过了【分析】考虑\(0\leqk<n\),所以\(\min(k,n-1)=k\)因此有:\(\begin{aligned}&\sum_{i=k}^{\mi......
  • Python进阶篇04-面向对象编程
    面向对象编程面向对象编程和面向过程编程的区别:类和实例类:抽象的、用于创建实例的基础模板,类里面可以定义这个类所拥有的基础的属性。实例:根据类而创建的具体的对象,实......
  • 2022-2023-1 20221424《计算机基础与程序设计》第9周学习总结
    2022-2023-120221424《计算机基础与程序设计》第9周学习总结作业信息这个作业属于哪个课程2022-2023-1-计算机基础与程序设计这个作业要求在哪里2022-2023-1......