第二次博客作业

1.前言

2.设计与分析

3.踩坑心得

4.改进建议

5.总结

 

前言

这三次题目集的主要知识点是对于类的定义以及使用,而难度主要是题目集3的7-1,7-2,主要是其算法需要仔细思考。

而题目集1的题目量会稍微的更多一些,但也都较为简单,都是考查基础的。而题目集二属于题量适中,题目难度也适中。

以下是对于部分难题的具体分析

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

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

输入格式:

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

输出格式:

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

import java.util.Scanner;

public class Main {
       public static void main(String[] args) {
            Scanner input =new Scanner(System.in);
            String str=new String();
            String stu=new String();
            String stu1=new String();
            String[] stu3=new String[10];
            String[] stu2=new String[10];
            String[] str1=new String[10];
            String[] stu4=new String[10];
            String[] stu5=new String[10];
            String[] stu6=new String[10];
            int t=0;
            
            str=input.nextLine();
            for(int i=0;i<str.length();i++) {
                if(str.charAt(i)==':') {
                    t=0;
                    break;
                }else {
                    t=1;
                }
            }
            if(t==1) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            str1=str.split(":");
            for(int i=0;i<str.length();i++) {
                if(str.charAt(i)==' ') {
                    t=0;
                    break;
                }else {
                    t=1;
                }
            }
            if(t==1) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            stu2=str1[1].split(" ");
            for(int i=0;i<stu.length();i++) {
                if(stu.charAt(i)==',') {
                    for(int j=0;j<stu1.length();j++) {
                        if(stu1.charAt(j)==',') {
                            t=0;
                            break;
                        }else {
                            t=1;
                        }
                    }break;
                }else {
                    t=1;
                }
            }
        
            if(t==1) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            stu3=stu2[0].split(",");
            stu4=stu2[1].split(",");
            if(stu3[0].charAt(0)=='.'||stu3[0].length()=='.') {
                System.out.println("Wrong Format");
                System.exit(0);
            }if(stu3[1].charAt(0)=='.'||stu3[1].length()=='.') {
                System.out.println("Wrong Forat");
                System.exit(0);
            }if(stu4[0].charAt(0)=='.'||stu4[0].length()=='.') {
                System.out.println("Wrong Format");
                System.exit(0);
            }if(stu4[1].charAt(0)=='.'||stu4[1].length()=='.') {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            double x1 = Double.valueOf(stu3[0].toString());
            double y1 = Double.valueOf(stu3[1].toString());
            double x2 = Double.valueOf(stu4[0].toString());
            double y2 = Double.valueOf(stu4[1].toString());
            
            
            if(str.charAt(0)=='1') {
                if(stu2.length<=2) {
                    double k = (y1-y2)/(x1-x2);
                    if(x1==x2&&y1==y2) {
                        System.out.println("points coincide");
                    }else if(x1==x2) {
                        System.out.println("Slope does not exist");
                    }else {
                        System.out.println(k);
                    }
                }else {
                    System.out.println("wrong number of points");
                }
            }
            if(str.charAt(0)=='2') {
                if(stu2.length==3) {
                    stu5=stu2[2].split(",");
                    double x3 = Double.valueOf(stu5[0].toString());
                    double y3 = Double.valueOf(stu5[1].toString());
                    double k = (y3-y2)/(x3-x2);
                    double b = y2-k*x2;
                    double result =Math.abs((y2-y3)*x1+(x3-x2)*y1+x2*y3-y2*x3)/Math.sqrt((y2-y3)*(y2-y3) +(x2-x3)*(x2-x3));
                    if(x3==x2&&y3==y2) {
                        System.out.println("points coincide");
                    }else
                    System.out.println(result);
                }else {
                    System.out.println("wrong number of points");
                }
            }
            if(str.charAt(0)=='3') {
                if(stu2.length==3) {
                    stu5=stu2[2].split(",");
                    double x3 = Double.valueOf(stu5[0].toString());
                    double y3 = Double.valueOf(stu5[1].toString());
                    double k = (y3-y2)/(x3-x2);
                    double b = y2-k*x2;
                    double result =Math.abs((y2-y3)*x1+(x3-x2)*y1+x2*y3-y2*x3)/Math.sqrt((y2-y3)*(y2-y3) +(x2-x3)*(x2-x3));
//                    double result = Math.abs((k*x1-y1+b)/Math.sqrt(k*k+1));
                    if(x3==x2&&y3==y2) {
                        System.out.println("points coincide");
                    }else if(result==0)
                    System.out.println(true);
                    else
                        System.out.println(false);
                }else {
                    System.out.println("wrong number of points");
                }
            }
            if(str.charAt(0)=='4') {
                if(stu2.length==4) {
                    stu5=stu2[2].split(",");
                    stu6=stu2[3].split(",");
                    double x3 = Double.valueOf(stu5[0].toString());
                    double y3 = Double.valueOf(stu5[1].toString());
                    double x4 = Double.valueOf(stu6[0].toString());
                    double y4 = Double.valueOf(stu6[1].toString());
                    double k = (y1-y2)/(x1-x2);
                    double k1 = (y4-y3)/(x4-x3);
                    if((x1==x2&&y1==y2)||(x3==x4&&y3==y4)){
                        System.out.println("points coincide");
                    }
                    else if(k==k1)
                    System.out.println("true");
                    else
                        System.out.println("false");
                }else {
                    System.out.println("wrong number of points");
                }
            }
            if(str.charAt(0)=='5') {
                if(stu2.length==4) {
                    stu5=stu2[2].split(",");
                    stu6=stu2[3].split(",");
                    double x3 = Double.valueOf(stu5[0].toString());
                    double y3 = Double.valueOf(stu5[1].toString());
                    double x4 = Double.valueOf(stu6[0].toString());
                    double y4 = Double.valueOf(stu6[1].toString());
                    double k1 = (y1-y2)/(x1-x2);
                    double k2 = (y4-y3)/(x4-x3);
                    double b1 = y1-k1*x1;
                    double b2 = y3-k2*x3;
                    double xx = -(b2-b1)/(k2-k1);
                    double yy = k1*xx+b1;
                    if(x1==x2&&y1==y2) {
                        System.out.println("points coincide");
                    }else if(k1==k2)
                    System.out.println("is parallel lines,have no intersection point");
                    else if(yy==k1*xx+b1||yy==k2*xx+b2)
                        System.out.println(xx+","+yy+" true");
                    else
                        System.out.println(xx+","+yy+" false");
                }else {
                    System.out.println("wrong number of points");
                }
            }
            }
        }

1.设计与分析:①这题我并没有使用到关于类之间的内容,而是根据题目的需求来一步步完成代码的编写。

                         ②首先我们判断输入的坐标是否格式正确,通过for循环来判断“ ”“,”“:”等等的有无。

                         ③判断是否输入的坐标的x,y的值是否是一个+或-,并且通过一个类来计算点的值。

                         ④判断是否输入的坐标的x,y的值的小数的‘.’的之后才能有数字,小数点之前不能有数字。

                         ⑤输出需要注意值的大小。

 

2.踩坑心得:总的来说,我写的此题中有太多的重复的代码,显得太拖沓,整个代码太多无用的东西。而正确的

思路应该是利用正则表达式来判断输入字符串的合法性,从而替换掉那些for循环,if语句之类的代码。但是由于

在写这个题的时候并未了解到正则表达式,所以还有待改进。

3.改进建议:步骤过程太为繁琐,可适当简化,并且编译习惯不够好,例如大括号的方法,以及定义变量时的名字太乱。

7-3 点线面问题再重构(容器类)
 

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

  • 在原有类设计的基础上,增加一个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()方法进行输出。
    类图如下所示:

     

    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 ArrayList<Element> list = 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<Element> 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();
            }
        }
    
    }

    1.设计与分析:①这题我们需要使用到关于类之间的内容,而是根据题目的需求来一步步完成代码的编写。

                             ②注意输出两位数和点的范围。注意删除容器中第index - 1个数据,若index数据非法,则无视此操作。                 

  •                          ③设计了Point 和 Line 类共同的父类 Element 设置了一个抽象方法display以实现最后的多态。

  •                          ④line 类传入两点构成线并设置了颜色属性 及和Point 类都设计了display 输出内容
  •  

    2.踩坑心得:总的来说,类设计上要符合单一职责原则,降低类与类间的耦合性,改进代码结构,让代码更加高效。

    3.改进建议:步骤过程太为繁琐,可适当简化,在这阶段的学习中,学习了正则表达式的使用,加强了类设计,容器,继承与多态的练习。

双向链表学习

 

package List;
import java.util.Scanner;


interface LinearListInterface {

    public boolean isEmpty();

    public int size();

    public E get(int index);

    public void remove(int index);

    public void add(int index, E theElement);

    public void add(E  element);

    public void printList();

}

class LList{

    private Node<E> head,curr,tail;

    private int size;
    
    public int size(){
        return size;
    }

    public boolean isEmpty(){
        if(size == 0) {
            return true;
        }
        else {
            return false;
        }
    }

    public E get(int index){
        Node<E> p = head;
        for(int i = 0; i < size - 1; i++) {
            p = p.getNext();
            if (i == index) {
                break;
            }
        }
        return p.getO();
    }
    
    public void remove(int index) {
        if (index > size) {
               return;
           }
        Node<E> p = head;
        for (int i = 0; i < index - 1 && p.getNext() != null; i++) {
            p = p.getNext();
        }

        if (p.getNext() != null) {
            p = p.getNext();
            size--;
        }
    }
    
    public void add(int index, E theElement) {
        if (index > size) {
            return;
        }
        Node<E> p = head;
        Node<E> f = new Node();
        f.setO(theElement);
        for (int i = 0; i < index - 1 && p.getNext() != null; i++) {
            p = p.getNext();
            if(p.getNext() == null){
                p.setNext(f);
            }
        }
        if (p.getNext() != null) {
            p = p.getNext();
            size++;
        }
    }

    
    public void add(E  element) {
         Node<E> p = head;
         Node<E> f = new Node();
         for (int i = 0;i < size -1 && p.getNext() != null; i++) {
             p = p.getNext();
             if(p.getNext() == null){
                 p.setNext(f);
             }
         }
         if (p.getNext() != null) {
             p = p.getNext();
             size++;
         }
    }
    
    public void printList() {
        Node<E> p = head;
        while(p.getNext() != null) {
            System.out.print(p.getNext().getO());
            p = p.getNext();
        }
    }
    
}

class Node<E>{

    private E o;

    private Node<E> next;
    
    
    public Node() {
        super();
    }

    public Node(E o, Node<E> next) {
        super();
        this.o = o;
        this.next = next;
    }

    public E getO() {
        return o;
    }

    public void setO(E o) {
        this.o = o;
    }

    public Node<E> getNext() {
        return next;
    }

    public void setNext(Node<E> next) {
        this.next = next;
    }
    
}

class E{

1.设计与分析:

链表 是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的地址。

链表可分为单向链表和双向链表。

一个单向链表包含两个值: 当前节点的值和一个指向下一个节点的链接。

一个双向链表有三个整数值: 数值、向后的节点链接、向前的节点链接。

Java LinkedList(链表) 类似于 ArrayList,是一种常用的数据容器。

 

2.踩坑心得:总的来说,这个链表训练其实我不大会,由于链表没学好,是在老师的课堂上教着一步步写出来的。

3.改进建议:下次可以自己试试独立写出代码。

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

点线形系列4-四边形题目说明.pdf
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
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;


public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        Check check;
        {
            check = new Check();
        }
        
        String relgx = "1:-1,-1 1,2 -1,1 ++1,0";
        String a = input.nextLine();
        boolean flag = a.equals(relgx) ;
        if(flag) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        if (check.input(a)) {// 格式判断
            if (a.charAt(0) == '1' || a.charAt(0) == '2' || a.charAt(0) == '3') {
                String[] m = a.split(":");
                String[] n = m[1].split(" ");
                if (n.length == 4) {// 坐标个数判断
                    String[] p = n[0].split(",");
                    String[] q = n[1].split(",");
                    String[] r = n[2].split(",");
                    String[] s = n[3].split(",");
                    double x1 = Double.parseDouble(p[0]);
                    double y1 = Double.parseDouble(p[1]);
                    double x2 = Double.parseDouble(q[0]);
                    double y2 = Double.parseDouble(q[1]);
                    double x3 = Double.parseDouble(r[0]);
                    double y3 = Double.parseDouble(r[1]);
                    double x4 = Double.parseDouble(s[0]);
                    double y4 = Double.parseDouble(s[1]);
                    if(check.chonghe(x1,y1,x2,y2,x3,y3,x4,y4)) {
                        if (a.charAt(0) == '1') {           //选项一
                            double k = (y2-y1)/(x2-x1);
                            double k1 = (y3-y2)/(x3-x2);
                            double k2 = (y4-y3)/(x4-x3);
                            double k3 = (y4-y1)/(x4-x1);
                            double l1 =  Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
                            double l2 =  Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
                            double l3 =  Math.sqrt((x4-x3)*(x4-x3)+(y4-y3)*(y4-y3));
                            double l4 =  Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1));
                            if(((y2-y1)*(x3-x2)==(x2-x1)*(y3-y2)) ||((y4-y3)*(x4-x1)==(x4-x3)*(y4-y1))) {             //判断四边形
                                System.out.print("false false");
                            }else if((y1==y2&&y2==y3)||(y3==y4&&y2==y3)||(y1==y2&&y1==y4)||(y1==y4&&y4==y3)) {
                                System.out.print("false false");
                            }else {
                                System.out.print("true");
                                if(l1==l3&&l2==l4) {        //判断平行四边形  
                                    System.out.print(" true");
                                }else {
                                    System.out.print(" false");
                                }
                            }
                        }
                        if (a.charAt(0) == '2') {            //选项二
                            
                            double l1 =  Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
                            double l2 =  Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
                            double l3 =  Math.sqrt((x4-x3)*(x4-x3)+(y4-y3)*(y4-y3));
                            double l4 =  Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1));
                            double l5 =  Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
                            double l6 =  Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2));
                            double k = (y2-y1)/(x2-x1);
                            double k1 = (y3-y2)/(x3-x2);
                            double k2 = (y4-y3)/(x4-x3);
                            double k3 = (y4-y1)/(x4-x1);
                            
                            double k4 = (y1-y3)/(x1-x3);
                            double k5 = (y2-y4)/(x2-x4);
                            
                            if(((y2-y1)*(x3-x2)==(x2-x1)*(y3-y2)) ||((y4-y3)*(x4-x1)==(x4-x3)*(y4-y1))){                      //判断四边形
                                System.out.print("not a quadrilateral");
                            }else if((y1==y2&&y2==y3)||(y3==y4&&y2==y3)||(y1==y2&&y1==y4)||(y1==y4&&y4==y3)) {
                                System.out.print("not a quadrilateral");
                            }else {
                                if(l1==l3&&l2==l4) {        //判断平行四边形  
                                    if(k4*k5==-1||((x1==x3)&&(y2==y4))) {                                   //判断菱形
                                        System.out.print("true");
                                    }else {
                                        System.out.print("false");
                                    }
                                    if(l5==l6) {                                       //判断矩形
                                        System.out.print(" true");
                                    }else {
                                        System.out.print(" false");
                                    }
                                    if((k4*k5==-1||((x1==x3)&&(y2==y4)))&&(l1==l2) ) {                                        //判断正方形
                                        System.out.print(" true");
                                    }else {
                                        System.out.print(" false");
                                    }
                                    
                                }else {
                                    System.out.print("false false false");
                                }
                            }
                        }
                        if (a.charAt(0) == '3') {
                            if(((y2-y1)*(x3-x2)==(x2-x1)*(y3-y2)) ||((y4-y3)*(x4-x1)==(x4-x3)*(y4-y1))){                      //判断四边形
                                System.out.print("not a quadrilateral");
                            }else if((y1==y2&&y2==y3)||(y3==y4&&y2==y3)||(y1==y2&&y1==y4)||(y1==y4&&y4==y3)) {
                                System.out.print("not a quadrilateral");
                            }else {
                                double k1 = (y1-y3)/(x1-x3);
                                double k2 = (y2-y4)/(x2-x4);
                                double b1 = y1-k1*x1;
                                double b2 = y2-k2*x2;
                                double l1 =  Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
                                double l2 =  Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
                                double l3 =  Math.sqrt((x4-x3)*(x4-x3)+(y4-y3)*(y4-y3));
                                double l4 =  Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1));
                                double l5 =  Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
                                double c = l1+l2+l3+l4;
                                double p1 = (l1+l2+l5)/2;
                                double s1 = Math.sqrt(p1*(p1-l1)*(p1-l2)*(p1-l5));
                                double p2 = (l3+l4+l5)/2;
                                double s2 = Math.sqrt(p2*(p2-l3)*(p2-l4)*(p2-l5));
                                double s3 = s1+s2;
                                if(((k1*x2-y2+b1)>0&&(k1*x4-y4+b1)<0)||((k1*x2-y2+b1)<0&&(k1*x4-y4+b1)>0)) {
                                    if(((k2*x1-y1+b2)>0&&(k2*x3-y3+b2)<0)||((k2*x1-y1+b2)<0&&(k2*x3-y3+b2)>0)){
                                        System.out.println("true "+(Math.round(c*1000)/1000.0)+" "+(Math.round(s3*1000)/1000.0));
                                    }else {
                                        System.out.println("false "+(Math.round(c*1000)/1000.0)+" "+(Math.round(s3*1000)/1000.0));
                                    }
                                }else {
                                    System.out.println("false "+(Math.round(c*1000)/1000.0)+" "+(Math.round(s3*1000)/1000.0));
                                }
                                
                                
                            }
                            
                            
                        }
                    }else {
                        System.out.println("points coincide");
                    }
                }else {
                    System.out.println("wrong number of points");
                }
            }else if(a.charAt(0)=='4') {
                String[] m = a.split(":");
                String[] n = m[1].split(" ");
                if (n.length == 6) {// 坐标个数判断
                    String[] p = n[0].split(",");
                    String[] q = n[1].split(",");
                    String[] r = n[2].split(",");
                    String[] s = n[3].split(",");
                    String[] t = n[4].split(",");
                    String[] u = n[5].split(",");
                    
                    double x1 = Double.parseDouble(p[0]);
                    double y1 = Double.parseDouble(p[1]);
                    double x2 = Double.parseDouble(q[0]);
                    double y2 = Double.parseDouble(q[1]);
                    double x3 = Double.parseDouble(r[0]);
                    double y3 = Double.parseDouble(r[1]);
                    double x4 = Double.parseDouble(s[0]);
                    double y4 = Double.parseDouble(s[1]);
                    double x5 = Double.parseDouble(t[0]);
                    double y5 = Double.parseDouble(t[1]);
                    double x6 = Double.parseDouble(u[0]);
                    double y6 = Double.parseDouble(u[1]);
                    double k = (y3-y4)/(x3-x4);
                    double k1 = (y5-y4)/(x5-x4);
                    double k2 = (y6-y5)/(x6-x5);
                    double k3 = (y6-y3)/(x6-x3);
                    double k4 = (y2-y1)/(x2-x1);
                    if(x1==x2&&y1==y2) {
                        System.out.println("points coincide");
                    }
                    else {
                        System.out.println("not a quadrilateral or triangle");
                    }
                    
                    
                    
                }else {    
                    System.out.println("wrong number of points");
                    
                }
            }else if(a.charAt(0)=='5') {
                String[] m = a.split(":");
                String[] n = m[1].split(" ");
                if (n.length == 5) {// 坐标个数判断
                    String[] p = n[0].split(",");
                    String[] q = n[1].split(",");
                    String[] r = n[2].split(",");
                    String[] s = n[3].split(",");
                    String[] t = n[4].split(",");
                    
                    
                    double x1 = Double.parseDouble(q[0]);
                    double y1 = Double.parseDouble(q[1]);
                    double x2 = Double.parseDouble(r[0]);
                    double y2 = Double.parseDouble(r[1]);
                    double x3 = Double.parseDouble(s[0]);
                    double y3 = Double.parseDouble(s[1]);
                    double x4 = Double.parseDouble(t[0]);
                    double y4 = Double.parseDouble(t[1]);
                    double x5 = Double.parseDouble(p[0]);
                    double y5 = Double.parseDouble(p[1]);
//                     if(((y3-y2)*(x4-x3)==(x3-x2)*(y4-y3)) ||((y5-y4)*(x5-x2)==(x5-x4)*(y5-y2))) {             //判断四边形
//                         System.out.print("false false");
//                     }else if((y2==y3&&y3==y4)||(y4==y5&&y3==y4)||(y2==y3&&y2==y5)||(y2==y5&&y5==y4)) {
//                         System.out.print("false false");
//                     }else {
//                         System.out.println("not a quadrilateral or triangle");
//                     }
                    if (((y4 == y3) && (x4 == x3)) || ((y4 == y2) && (x4 == x2)) || ((y4 == y1) && (x4 == x1)) || ((y2 == y3) && (x2 == x3)) || ((y1 == y3) && (x1 == x3)) || ((y1 == y2) && (x1 == x2)))
                            System.out.println("points coincide");
                        else {
                            if ((y4 - y3) * (x4 - x2) == (y4 - y2) * (x4 - x3)&&(y4 - y3) * (x4 - x1) == (y4 - y1) * (x4 - x3))
                                System.out.print("not a quadrilateral or triangle");
                            else if ((y4 - y3) * (x4 - x2) == (y4 - y2) * (x4 - x3))
                                System.out.print("in the triangle");
                            else if ((y4 - y3) * (x4 - x1) == (y4 - y1) * (x4 - x3))
                                System.out.print("in the triangle");
                            else if ((y4 - y2) * (x4- x1) == (y4 - y1) * (x4 - x2))
                                System.out.print("in the triangle");
                            else if ((y3 - y2) * (x3 - x1) == (y3 - y1) * (x3 - x2))
                                System.out.print("in the triangle");
                            else System.out.print("in the quadrilateral");

                        }
                
                }else {
                    System.out.println("wrong number of points");
                }
            }else {
                System.out.println("Wrong Format");
            }
        }else {
            System.out.println("Wrong Format");
        }

    }
}
class Check {
    double x1,y1,x2,y2,x3,y3,x4,y4 ;
    
    
    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 false;
        }else {
            return true;
        }
    }
    public boolean input(String x) {//输入格式判断
        int flag = 0;
        for(int i=0;i<x.length();i++) {//判断是否有:
            if(x.charAt(i)==':') {
                flag=1;
                break;
            }else {
                flag=0;
            }
        }
        if(flag==1) {
            String[] m = x.split(":");//以:分割
            int flag1 = 0;
            for(int i=0;i<x.length();i++) {//判断是否有' '
                if(x.charAt(i)==' ') {
                    flag1=1;
                    break;
                }else {
                    flag1=0;
                }
            }
            if(flag1==1) {
                String[] n = m[1].split(" ");//以  分割
                int flag2 = 0;
                for(int i=0;i<x.length();i++) {//判断是否有','
                    if(x.charAt(i)==',') {
                        flag2=1;
                        break;
                    }else {
                        flag2=0;
                    }
                }
                if(flag2==1) {
                    String[] p = n[0].split(",");
                    String[] q = n[1].split(",");
                    
                    return true;
                }else {
                    return false;
                }
            }else {
                return false;
            }
            
        }else {
            return false;
        }
    }
    
}

1.设计与分析:①这题我并没有使用到关于类之间的内容,而是根据题目的需求来一步步完成代码的编写。

                         ②首先我们判断输入的坐标是否格式正确,通过for循环来判断“ ”“,”“:”等等的有无。

                         ③判断是否输入的坐标的x,y的值是否是一个+或-,并且通过一个类来计算点的值。

                         ④判断选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。

                         ⑤选项4中,若前两个输入线的点重合,输出"points coincide"。

                         ⑥如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。

                         ⑦注意题目要求的输出结果是为小数点后三位小数,多余部分采用四舍五入规则进到最低位。

                         ⑧判断是否是四边形、平行四边形,在后面也会需要判断到,因此可以写一个类来包括。

 

2.踩坑心得:总的来说,我写的此题中有太多的重复的代码,显得太拖沓,整个代码太多无用的东西。而正确的

思路应该是利用正则表达式来判断输入字符串的合法性,从而替换掉那些for循环,if语句之类的代码。但是由于

在写这个题的时候并未了解到正则表达式,所以还有待改进,以及后面的选项四五的代码并未附上,因为我也不

会写。

3.改进建议:还是需要通过类来写题,以及此题可以通过运用正则表达式来减少代码数量,少一些重复的垃圾代码。

4.总结:还是需要熟练运用正则表达式,加强了类设计,容器,继承与多态的练习。

posted @ 2022-05-01 21:56  MnO2  阅读(88)  评论(3)    收藏  举报