JAVA PTA第一次作业总结

(1)前言:经过了前二次作业的简单AC(不是),但到了第三次的作业时,就是究极折磨,被各种的大佬薄纱,接下来我会对这个月的作业在知识点、题量、难度上进行一个简单评价。

知识上:
前二次的作业还是没有涉及到什么知识点的,只是熟悉一下JAVA的语法,其中的一些测试点或许是有一点难测试,但是画上一些时间还是可以写出来的。
第三次作业,主要是对面向对象方法的学习,对类属性和方法的封装。
题量上:
第一次作业看起来是题目多,实际上是不如第三次作业。
前二次的作业是题量较小的,都是不到一伯行就可以解决(暴力解决)。
第三次作业不要看这小小的三道题,可是这题量可大了,第一道小小试一试水,第二题直接跳到三伯行,第三题直接写到六百行,呜呜呜。
难度上:
还是那句话,前二次的作业是没有什么难度的,是要是JAVA的语法。
第三次作业难度就上来了,各种数学知识的运用,还有可恶的精度问题(真真的难找到问题),正则表达式的运用,使用各种的类,类的封装和使用。

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

输入格式:
由0、1组成的二进制数据流。例如:11110111010111111001001101111111011111111101111

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

对各种错误情况的判断,我是对字符串进行截断,提取出需要的字符串,对其进行操作。
代码如下:

package PTA_2;

import  java.util.Scanner;

public class T7_2 {
    public static void main(String[] args) {
        String s;
        inti, j, flag = 0;
        int a = 0;
        Scanner sc = new Scanner(System.in);
        s = sc.nextLine();
        for (i = 0; i < s.length(); ++i) {
            if (s.charAt(i) == '0')
                flag = 1;

        }
        if (s.length() < 11 || flag == 0) {
            System.out.println("null data");
            return;
        }
        int sum = 0;
        boolean va = false, pa = false;

        for (i = 0; i < s.length() - 10; ++i) {


            if (s.charAt(i) == '0' ) {
                sum++;
                System.out.print(sum + ":");
                String q = s.substring(i, i + 11);
                // System.out.println(q);
                if (q.charAt(q.length() - 1) != '1')
                    va = false;
                else
                    va = true;

                a=0;
                for (j = 1; j < 9; ++j)
                    if (q.charAt(j)=='1' )
                        a++;

                if ((a & 1) == 1) {
                    if (q.charAt(q.length() - 2) != '1')
                        pa = true;
                    else
                        pa = false;
                }
                else {
                    if (q.charAt(q.length() - 2) == '1')
                        pa = true;
                    else
                        pa = false;
                }

                if (pa&&va ) {
                    for (j = 1; j < 9; j++)
                        System.out.print(q.charAt(j));
                    System.out.print("\n");
                }
                else if(!va)
                    System.out.println("validate error");
                else
                    System.out.println("parity check error");

                i = i + 10;
            }
        }
    }
}


这个还是没有写什么类的,还是保留了c面向过程的思想,直接就是写一大串再说(就是暴力),圈复杂度高,都是if else ,当然了。

第三次作业的7-1
输入连个点的坐标,计算两点之间的距离

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

输出格式:
计算所得的两点之间的距离。例如:1.4142135623730951
这就是一个(简单的)求距离的题目,套上公式就行了。

package PTA_3;

import java.util.*;

import static java.lang.Math.sqrt;

public class T7_1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double x1,x2,y1,y2;
        String s;
        s=sc.nextLine();
        if(s.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)"))
        {
            String []ss=s.split(" ");
            String [ ] ss1=ss[0].split(",");
            String [ ] ss2=ss[1].split(",");
            if((ss1[0]== "0.0" )||(ss1[1]=="0.0")||(ss2[0]=="0.0")||(ss2[1]=="0.0")){
                System.out.println("Wrong Format");
                return ;
            }
            x1=Double.parseDouble(ss1[0]);
            y1=Double.parseDouble(ss1[1]);
            x2=Double.parseDouble(ss2[0]);
            y2=Double.parseDouble(ss2[1]);
            double ans=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
            System.out.println(ans);
            //System.out.println(x1);

        }
        else if(s.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){2,}"))
            System.out.println("wrong number of points");
        else
            System.out.println("Wrong Format");

    }
}


这个还是没有写什么类的,这个就是对于正则表达式的一道题,对于后二道题是至关重要的。

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

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

package PTA_3;

import java.util.*;
import static java.lang.Math.*;
public class T7_2 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s;
        s=sc.nextLine();
        int n= s.charAt(0)-'0';
        boolean istrue=true;
        String s1=s.substring(2);
        String s2=s.substring(0,2);
        //System.out.println(s2);
        // System.out.println(s1);
        if(!s2.matches("[1-5]\\:")){
            System.out.println("Wrong Format");
            return;
        }
        String []ss=s1.split(" ");
        switch(n)
        {
            case 1:
                if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){1}"))
                    case1(ss);
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){2,}"))
                    System.out.println("wrong number of points");
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){0}"))
                    System.out.println("wrong number of points");
                else  System.out.println("Wrong Format");
                break;
            case 2:
                if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){2}"))
                    case2(ss);
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){3,}"))
                    System.out.println("wrong number of points");
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){0,1}"))
                    System.out.println("wrong number of points");
                else  System.out.println("Wrong Format");
                break;
            case 3:
                if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){2}"))
                    case3(ss);
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){3,}"))
                    System.out.println("wrong number of points");
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){0,1}"))
                    System.out.println("wrong number of points");
                else  System.out.println("Wrong Format");
                break;
            case 4:
                if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){3}"))
                    case4(ss);
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){4,}"))
                    System.out.println("wrong number of points");
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){0,2}"))
                    System.out.println("wrong number of points");
                else  System.out.println("Wrong Format");
                break;
            case 5:
                if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){3}"))
                    case5(ss);
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){4,}"))
                    System.out.println("wrong number of points");
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){0,2}"))
                    System.out.println("wrong number of points");
                else  System.out.println("Wrong Format");
                break;
        }


    }
    static  class dian{
        double x,y;
        dian(double x1,double y1){
            this.x=x1;
            this.y=y1;
        }
        public void xielv (dian a){
            if(this.x==a.x&&this.y==a.y)
                System.out.println("points coincide");
            else if(this.x-a.x!=0)
                System.out.println( (this.y-a.y)/(this.x-a.x));
            else
                System.out.println("Slope does not exist");
        }
    }
    static  class jian {
        dian x,y;
        jian(dian x1,dian y1){
            this.x=x1;
            this.y=y1;
        }
        public  boolean istrue()
        {
            if(this.x.x==this.y.x&&this.x.y==this.y.y) return true;
            else return false;

        }


        public double distance (dian a){
            if(x.x==y.x){
                return abs(a.x-x.x);
            }
            double k=(x.y-y.y)/(x.x-y.x);
            double c=x.y-k*x.x;
            double d=abs(k*a.x-a.y+c)/sqrt(k*k+1);
            return d;
        }
        public  boolean tongxian(dian a){
            if(x.x==y.x&&a.x==x.x){
                return true;
            }
            double k=(x.y-y.y)/(x.x-y.x);
            double c=x.y-k*x.x;
            if(a.y==k*a.x+c) return true;
            else return false;
        }
        public boolean pingxing (jian a){
            if(x.x==y.x&&a.y.x==a.x.x){
                return true;
            }
            double k=(x.y-y.y)/(x.x-y.x);
            double k1=(a.y.y-a.x.y)/(a.y.x-a.x.x);
            if(k==k1)
                return true;
            else return false;
        }
        public dian sub(dian a,dian b){
            return new dian (a.x-b.x,a.y-b.y);
        }
        public dian add(dian a,dian b){
            return new dian (a.x+b.x,a.y+b.y);
        }
        public  double cross (dian a,dian b){
            return a.x*b.y-a.y*b.x;
        }

        public void jiaodian(jian a ){
            if(this.pingxing(a))
                System.out.println("is parallel lines,have no intersection point");
            else {
                dian v=sub(a.y,a.x);
                dian w=sub(this.y,this.x);
                dian p=a.x;
                dian q=this.x;
                dian u=sub(p,q);
                double t=cross(w,u)/cross(v,w);

                dian ans =new dian( p.x+v.x*t,p.y+v.y*t);
                System.out.print(ans.x+","+ans.y+" ");
                double y0=ans.y;
                double x0=ans.x;
                if((y0>min(this.x.y,this.y.y)||y0>min(a.x.y,a.y.y))&&(y0<max(this.x.y,this.y.y)||y0<max(a.x.y,a.y.y)))
                    System.out.println("true");
                else
                    System.out.println("false");
            }
        }

        //if((x0>min(x.x,y.x)&&x0<max(x.x,y.x))&&(x0>min(a.x.x,a.y.x)&&x0<max(a.x.x,a.y.x))&&(y0>min(a.x.y,a.y.y)&&y0<max(a.x.y,a.y.y))&&(y0>min(x.y,y.y)&&y0<max(x.y,y.y)))
        //if((abs(x0-min(x.x,y.x))>0.00001&&abs(x0-max(x.x,y.x))<0.00001)&&(abs(x0-min(a.x.x,a.y.x))>0.00001&&abs(x0-max(a.x.x,a.y.x))<0.00001)&&(abs(y0-min(a.x.y,a.y.y))>0.00001&&abs(y0-max(a.x.y,a.y.y))<0.00001)&&(abs(y0-min(x.y,y.y))>0.00001&&abs(y0-max(x.y,y.y))<0.00001))

    }

    public static void case1(String[] ss){

        if(ss.length!=2) {
            System.out.println("wrong number of points");
            return;
        }
        String [ ] ss1=ss[0].split(",");
        String [ ] ss2=ss[1].split(",");
        double x1=Double.parseDouble(ss1[0]);
        double y1=Double.parseDouble(ss1[1]);
        double x2=Double.parseDouble(ss2[0]);
        double y2=Double.parseDouble(ss2[1]);
        dian a=new dian(x1,y1);
        dian b=new dian(x2,y2);
        a.xielv(b);
    }
    public static void case2(String[] ss){
        if(ss.length!=3) {
            System.out.println("wrong number of points");
            return;
        }
        String [ ] ss1=ss[0].split(",");
        String [ ] ss2=ss[1].split(",");
        String [ ] ss3=ss[2].split(",");
        double x1=Double.parseDouble(ss1[0]);
        double y1=Double.parseDouble(ss1[1]);
        double x2=Double.parseDouble(ss2[0]);
        double y2=Double.parseDouble(ss2[1]);
        double x3=Double.parseDouble(ss3[0]);
        double y3=Double.parseDouble(ss3[1]);
        dian a=new dian(x1,y1);
        dian b=new dian(x2,y2);
        dian c=new dian(x3,y3);
        jian k=new jian(b,c);
        if(k.istrue()){
            System.out.println("points coincide");
            return ;
        }
        System.out.println(k.distance(a));
    }
    public static void case3(String[] ss){
        if(ss.length!=3) {
            System.out.println("wrong number of points");
            return;
        }
        String [ ] ss1=ss[0].split(",");
        String [ ] ss2=ss[1].split(",");
        String [ ] ss3=ss[2].split(",");
        double x1=Double.parseDouble(ss1[0]);
        double y1=Double.parseDouble(ss1[1]);
        double x2=Double.parseDouble(ss2[0]);
        double y2=Double.parseDouble(ss2[1]);
        double x3=Double.parseDouble(ss3[0]);
        double y3=Double.parseDouble(ss3[1]);
        dian a=new dian(x1,y1);
        dian b=new dian(x2,y2);
        dian c=new dian(x3,y3);
        jian k=new jian(b,c);
        if(k.istrue()){
            System.out.println("points coincide");
            return ;
        }
        if(k.tongxian(a))
            System.out.println("true");
        else System.out.println("false");
    }
    public static void case4(String[] ss){
        if(ss.length!=4) {
            System.out.println("wrong number of points");
            return;
        }
        String [ ] ss1=ss[0].split(",");
        String [ ] ss2=ss[1].split(",");
        String [ ] ss3=ss[2].split(",");
        String [ ] ss4=ss[3].split(",");
        double x1=Double.parseDouble(ss1[0]);
        double y1=Double.parseDouble(ss1[1]);
        double x2=Double.parseDouble(ss2[0]);
        double y2=Double.parseDouble(ss2[1]);
        double x3=Double.parseDouble(ss3[0]);
        double y3=Double.parseDouble(ss3[1]);
        double x4=Double.parseDouble(ss4[0]);
        double y4=Double.parseDouble(ss4[1]);
        dian a=new dian(x1,y1);
        dian b=new dian(x2,y2);
        dian c=new dian(x3,y3);
        dian d=new dian(x4,y4);
        jian q=new jian(a,b);
        jian p=new jian(c,d);
        if(q.istrue()||p.istrue()){
            System.out.println("points coincide");
            return ;
        }
        if(q.pingxing(p)) System.out.println("true");
        else System.out.println("false");
    }

    public static void case5(String[] ss){
        if(ss.length!=4) {
            System.out.println("wrong number of points");
            return;
        }
        String [ ] ss1=ss[0].split(",");
        String [ ] ss2=ss[1].split(",");
        String [ ] ss3=ss[2].split(",");
        String [ ] ss4=ss[3].split(",");
        double x1=Double.parseDouble(ss1[0]);
        double y1=Double.parseDouble(ss1[1]);
        double x2=Double.parseDouble(ss2[0]);
        double y2=Double.parseDouble(ss2[1]);
        double x3=Double.parseDouble(ss3[0]);
        double y3=Double.parseDouble(ss3[1]);
        double x4=Double.parseDouble(ss4[0]);
        double y4=Double.parseDouble(ss4[1]);
        dian a=new dian(x1,y1);
        dian b=new dian(x2,y2);
        dian c=new dian(x3,y3);
        dian d=new dian(x4,y4);
        jian q=new jian(a,b);
        jian p=new jian(c,d);
        if(q.istrue()||p.istrue()){
            System.out.println("points coincide");
            return ;
        }
        q.jiaodian(p);
    }
}



呜呜呜,debug了好久,一开始就卡在这里,这是个精度问题,不能用正常的方法来计算他们的交点,要用向量来计算,公式如下:

dian v=sub(a.y,a.x);
                dian w=sub(this.y,this.x);//向量的减法
                dian p=a.x;
                dian q=this.x;
                dian u=sub(p,q);
                double t=cross(w,u)/cross(v,w);//内积

                dian ans =new dian( p.x+v.x*t,p.y+v.y*t);


类图是这样的,还是把一些关联性不强的方法放在类里面,比如向量计算。

第三次作业的7-3
用户输入一组选项和数据,进行与三角形有关的计算。选项包括:
1:输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果输出true/false,两个结果之间以一个英文空格符分隔。
2:输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。
3:输入三个点坐标,输出是钝角、直角还是锐角三角形,依次输出三个判断结果(true/false),以一个英文空格分隔,
4:输入五个点坐标,输出前两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积。若直线与三角形一条线重合,输出"The point is on the edge of the triangle"
5:输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外。若点在三角形的某条边上,输出"on the triangle"
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
如果输入的三个点无法构成三角形,输出"data error"。
注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333333,1.0按格式输出为1.0
选项4中所输入线的两个点坐标重合,输出"points coincide",
前几个case没什么问题,case4就开始折磨了,多种情况,分成二个三角行,分成三角形和四边形,重合,没有交点。

package PTA_3;

import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.sort;

public class T7_3 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String s;
        s=sc.nextLine();
        int n= s.charAt(0)-'0';
        String s1=s.substring(2);
        String s2=s.substring(0,2);
        if(!s2.matches("[1-5]\\:")){
            System.out.println("Wrong Format");
            return;
        }
        String []ss=s1.split(" ");
        switch(n)
        {
            case 1:
                if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){2}"))
                    case1(ss);
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){3,}"))
                    System.out.println("wrong number of points");
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){0,1}"))
                    System.out.println("wrong number of points");
                else  System.out.println("Wrong Format");
                break;
            case 2:
                if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){2}"))
                    case2(ss);
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){3,}"))
                    System.out.println("wrong number of points");
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){0,1}"))
                    System.out.println("wrong number of points");
                else  System.out.println("Wrong Format");
                break;
            case 3:
                if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){2}"))
                    case3(ss);
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){3,}"))
                    System.out.println("wrong number of points");
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){0,1}"))
                    System.out.println("wrong number of points");
                else  System.out.println("Wrong Format");
                break;
            case 4:
                if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){4}"))
                    case4(ss);
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){5,}"))
                    System.out.println("wrong number of points");
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){0,3}"))
                    System.out.println("wrong number of points");
                else  System.out.println("Wrong Format");
                break;
            case 5:
                if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){3}"))
                    case5(ss);
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){4,}"))
                    System.out.println("wrong number of points");
                else  if(s1.matches("[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)(\\s[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)\\,[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)){0,2}"))
                    System.out.println("wrong number of points");
                else  System.out.println("Wrong Format");
                break;
        }


    }
    public static boolean doubleequal(double a,double b){
        return abs(a-b)<1e-6;
    }

    static  class dian{
        double x,y;
        public dian(double x1, double y1){
            this.x=x1;
            this.y=y1;
        }
        public boolean isequlity(dian a){
            if(doubleequal(this.x,a.x)&&doubleequal(this.y,a.y)) return  true;
            else
                return false;
        }
        public  double distance(dian a) {
            double x1=this.x;
            double x2=a.x;
            double y1=this.y;
            double y2=a.y;
            return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        }
        public void xielv (dian a){
            if(this.x==a.x&&this.y==a.y)
                System.out.println("points coincide");
            else if(this.x-a.x!=0)
                System.out.println( (this.y-a.y)/(this.x-a.x));
            else
                System.out.println("Slope does not exist");

        }
    }
    static  class jian{
        dian x,y;
        jian(dian x1, dian y1){
            this.x=x1;
            this.y=y1;
        }
        public  boolean istrue()
        {
            if(doubleequal(this.x.x ,this.y.x)&&doubleequal(this.x.y,this.y.y)) return true;
            else return false;

        }
        public boolean isequality(jian a){
            if(doubleequal(a.x.x,a.y.x)&&doubleequal(this.x.x,this.y.x)) {
                if(doubleequal(a.x.x,this.x.x)) return true;
                else
                    return false ;
            }
            else  if((doubleequal(this.x.x,this.y.x)&&!doubleequal(a.x.x,a.y.x))||(!doubleequal(this.x.x,this.y.x)&&doubleequal(a.x.x,a.y.x)))
                return false;

            double k1=(x.y-y.y)/(x.x-y.x);
            double k2=(a.y.y-a.x.y)/(a.y.x-a.x.x);
            double c1=x.y-k1*x.x;
            double c2=a.x.y-k2*a.x.x;

            if(doubleequal(k1,k2)&&doubleequal(c1,c2))
                return true ;

            return false;
        }
        public double distance (dian a){
            if(x.x==y.x){
                return abs(a.x-x.x);
            }
            double k=(x.y-y.y)/(x.x-y.x);
            double c=x.y-k*x.x;
            double d=abs(k*a.x-a.y+c)/sqrt(k*k+1);
            return d;
        }
        public  boolean tongxian(dian a){
            if(x.x==y.x&&a.x==x.x){
                return true;
            }
            double k=(x.y-y.y)/(x.x-y.x);
            double c=x.y-k*x.x;
            if(doubleequal(a.y,k*a.x+c)) return true;
            else return false;
        }
        public boolean pingxing (jian a){
            if(x.x==y.x&&a.y.x==a.x.x){
                return true;
            }
            if(x.x!=y.x&&a.y.x==a.x.x){
                return false;
            }
            if(x.x==y.x&&a.y.x!=a.x.x){
                return false;
            }
            double k=(x.y-y.y)/(x.x-y.x);
            double k1=(a.y.y-a.x.y)/(a.y.x-a.x.x);
            if(k==k1)
                return true;
            else return false;
        }
        public dian sub(dian a, dian b){
            return new dian(a.x-b.x,a.y-b.y);
        }
        public dian add(dian a, dian b){
            return new dian(a.x+b.x,a.y+b.y);
        }
        public  double cross (dian a, dian b){
            return a.x*b.y-a.y*b.x;
        }
        public int jiaodiannum(jian a ){
            if(this.pingxing(a)) return 0;

            dian v=sub(a.y,a.x);
            dian w=sub(this.y,this.x);
            dian p=a.x;
            dian q=this.x;
            dian u=sub(p,q);
            double t=cross(w,u)/cross(v,w);
            dian ans =new dian( p.x+v.x*t,p.y+v.y*t);
            // System.out.print(ans.x+","+ans.y+" ");
            double y0=ans.y;
            double x0=ans.x;
            //if(cross(sub(ans,this.x),sub(this.x,this.y))==0)min(x.x,y.x)
            double x1=min(x.x,y.x);
            double x2=max(x.x,y.x);

            if(((x0)>=min(x.x,y.x)&&(x0)<=max(x.x,y.x))&&((y0)>=min(x.y,y.y)&&(y0)<=max(x.y,y.y)))
                return 1;
            return 0;
        }
        public dian jiaodian(jian a ){
            dian ans = null;
            double k1;
            double k2;
            double c1;
            double c2;
            double x0;
            double y0;
            //System.out.print(x0+","+y0+" ");//x.x  y.x
            if(x.x==y.x&&a.y.x!=a.x.x){
                k2=(a.y.y-a.x.y)/(a.y.x-a.x.x);
                c2=a.x.y-k2*a.x.x;
                x0=x.x;
                y0=k2*x0+c2;
                //System.out.print(x0+","+y0+" ");
                if(((x0)>=min(x.x,y.x)&&(x0)<=max(x.x,y.x))&&((y0)>=min(x.y,y.y)&&(y0)<=max(x.y,y.y)))
                    ans=new dian(x0,y0);
            }
            else  if(x.x!=y.x&&a.y.x==a.x.x){
                k1=(x.y-y.y)/(x.x-y.x);
                c1=x.y-k1*x.x;
                x0=a.x.x;
                y0=k1*x0+c1;
                //System.out.print(x0+","+y0+" ");
                if(((x0)>=min(x.x,y.x)&&(x0)<=max(x.x,y.x))&&((y0)>=min(x.y,y.y)&&(y0)<=max(x.y,y.y)))
                    ans=new dian(x0,y0);
            }
            else {
                k1=(x.y-y.y)/(x.x-y.x);
                k2=(a.y.y-a.x.y)/(a.y.x-a.x.x);
                c1=x.y-k1*x.x;
                c2=a.x.y-k2*a.x.x;
                x0=(c2-c1)/(k1-k2);
                y0=k1*x0+c1;
                if((x0>=min(x.x,y.x)&&x0<=max(x.x,y.x))&&(y0>=min(x.y,y.y)&&y0<=max(x.y,y.y)))
                    ans=new dian(x0,y0);
            }
            return ans;
        }
    }
    static class quad {
        dian p1, p2, p3, p4;
        quad(dian x1, dian x2, dian x3, dian x4){
            this.p1=x1;
            this.p2=x2;
            this.p3=x3;
            this.p4=x4;

        }
        public double getArea(){
            double []xarr={p1.x,p2.x,p3.x,p4.x};
            double []yarr={p1.y,p2.y,p3.y,p4.y};
            double mianji = 0;
            for(int i=0;i<4;i++){
                mianji+=(xarr[i-1]*yarr[i]-xarr[i]*yarr[i-1]);
            }
            mianji+=xarr[3]*yarr[0]-xarr[0]*yarr[3];
            mianji=Math.abs(0.5*mianji);
            return mianji;
        }

    }
    static  class tri{
        dian x,y,z;
        tri(dian x1,dian y1, dian z1){
            this.x=x1;
            this.y=y1;
            this.z=z1;
        }
        public boolean istri(){
            double a=x.distance(y);
            double b=x.distance(z);
            double c=y.distance(z);
            double a1,b1,c1;
            double temp,maxn,sum;
            sum=a+b+c;
            temp=max(a,b);
            maxn=max(temp,c);
            c1=maxn;
            b1=temp;
            a1=sum-b1-c1;
            if((a+b<=c)||(a+c<=b)||(c+b<=a))
                return false;
            return true;
        }
        public boolean isEquil(){
            double a=x.distance(y);
            double b=x.distance(z);
            double c=y.distance(z);
            if(a==b&&b==c)
                return true;
            return false;
        }
        public boolean isIsosceles(){//&&!this.isEquil()
            double a=x.distance(y);
            double b=x.distance(z);
            double c=y.distance(z);
            if((a==b||a==c||b==c)) return true;
            return false;
        }
        public double mianji(){
            double a=x.distance(y);
            double b=x.distance(z);
            double c=y.distance(z);
            double sum=a+b+c;
            double p=(a+b+c)/2;
            double S=sqrt(p*(p-a)*(p-b)*(p-c));
            return S;
        }
        public void output(){
            double a=x.distance(y);
            double b=x.distance(z);
            double c=y.distance(z);
            double sum=0;
            sum=a+b+c;
            double S=(sqrt(((a+b+c))*(b+c-a)*(a+c-b)*(a+b-c)))/4;
            double x0=(x.x+y.x+z.x)/3;
            double y0=(x.y+y.y+z.y)/3;
            System.out.println(  data(sum)+" "+data(S)+" "+data(x0)+","+data(y0));
        }
        public void judge(){
            double a=x.distance(y);
            double b=x.distance(z);
            double c=y.distance(z);
            double a1,b1,c1;
            double temp,maxn,sum;
            boolean zhi=false,dun=false,rui=false;
//            sum=a+b+c;
//            temp=max(a,b);
//            maxn=max(temp,c);
            double[ ] arr={a,b,c};
            sort(arr);
            //Systerm.out.println(arr[2]+" "+arr[1]+" "+arr[0]);
            if(abs(arr[2]*arr[2]-(arr[0]*arr[0]+arr[1]*arr[1]))<0.001) zhi=true;
            else if(((arr[0]*arr[0]+arr[1]*arr[1]-arr[2]*arr[2]))<0) dun=true;
            else
                rui=true;

            System.out.println(dun+" "+zhi+" "+rui);
        }
        public  void jiaodiannum_tri(jian a) {
            jian x1=new jian(this.x,this.y);
            jian x2=new jian(this.x,this.z);
            jian x3 =new jian (this.y,this.z);
            dian a1 = null,a2 = null,a3 = null;
            dian d1= null,d2= null;
            if(a.isequality(x1)||a.isequality(x2)||a.isequality(x3)){
                System.out.println("The point is on the edge of the triangle");
                return ;
            }
            //System.out.println(x1.jiaodiannum(a)+" "+x2.jiaodiannum(a)+" "+x3.jiaodiannum(a));
            int sum=x1.jiaodiannum(a)+x2.jiaodiannum(a)+x3.jiaodiannum(a);
            if(sum==0){
                System.out.println(sum);
                return;
            }
            boolean flag1=false,flag2=false,flag3=false;
            if(x1.jiaodiannum(a)==1){
                a1=x1.jiaodian(a);
                flag1=true;
            }

            if(x2.jiaodiannum(a)==1){
                a2=x2.jiaodian(a);
                flag2=true;
            }

            if(x3.jiaodiannum(a)==1){
                a3=x3.jiaodian(a);
                flag3=true;
            }

            //System.out.println(flag1+" "+flag2+" "+flag3);
            if(!flag1&&flag2&&flag3){
                if((a2.isequlity(a3))&&sum==2)
                {
                    System.out.println("1");
                    return;
                }
                d1=a2;
                d2=a3;
            }
            else   if(flag1&&!flag2&&flag3){
                if((a1.isequlity(a3))&&sum==2)
                {
                    System.out.println("1");
                    return;
                }
                d1=a1;
                d2=a3;
            }
            else  if(flag1&&flag2&&!flag3){
                if((a2.isequlity(a1))&&sum==2)
                {
                    System.out.println("1");
                    return;
                }
                d1=a1;
                d2=a2;
            }
            if(flag1&&flag2&&flag3){
                if(a1.isequlity(a2)){
                    d1=a1;
                    d2=a3;
                }
                if(a1.isequlity(a3)){
                    d1=a1;
                    d2=a2;
                }
                if(a2.isequlity(a3)){
                    d1=a1;
                    d2=a3;
                }
            }
            System.out.print("2 ");
            if(sum==3){
                if(a.tongxian(this.x)){
                    tri s1=new tri(d1,d2,this.y);
                    tri s2=new tri(d1,d2,this.z);
                    System.out.println( data(min(s1.mianji(),s2.mianji()))+" "+data(max(s1.mianji(),s2.mianji())));
                    return;
                }
                else if(a.tongxian(this.y)){
                    tri s1=new tri(d1,d2,this.x);
                    tri s2=new tri(d1,d2,this.z);
                    System.out.println(data(min(s1.mianji(),s2.mianji()))+" "+data(max(s1.mianji(),s2.mianji())));
                    return;
                }
                else {
                    tri s1=new tri(d1,d2,this.y);
                    tri s2=new tri(d1,d2,this.x);
                    System.out.println(data(min(s1.mianji(),s2.mianji()))+" "+data(max(s1.mianji(),s2.mianji())));
                    return;
                }
            }
            if(sum==2){
                if((x1.tongxian(d1)&&x2.tongxian(d2))||(x1.tongxian(d2)&&x2.tongxian(d1))){
                    tri san= new tri(d1,d2,x);
                    System.out.println(data(min(this.mianji()-san.mianji(),san.mianji()))+" "+data(max(this.mianji()-san.mianji(),san.mianji())));
                    return;
                }
                else if((x1.tongxian(d1)&&x3.tongxian(d2))||(x1.tongxian(d2)&&x3.tongxian(d1))){
                    tri san= new tri(d1,d2,y);
                    System.out.println(data(min(this.mianji()-san.mianji(),san.mianji()))+" "+data(max(this.mianji()-san.mianji(),san.mianji())));
                    return;
                }
                else if((x3.tongxian(d1)&&x2.tongxian(d2))||(x3.tongxian(d2)&&x2.tongxian(d1))){
                    tri san= new tri(d1,d2,z);
                    System.out.println(data(min(this.mianji()-san.mianji(),san.mianji()))+" "+data(max(this.mianji()-san.mianji(),san.mianji())));
                    return;
                }
            }


        }
        public void inpoint(dian a){
            dian [] w={this.x,this.y,this.z};
            jian x1=new jian(this.x,this.y);
            jian x2=new jian(this.x,this.z);
            jian x3 =new jian (this.y,this.z);
            if( x1.tongxian(a)){
                if((a.x>=min(x1.x.x,x1.y.x)&&a.x<=max(x1.x.x,x1.y.x))&&(a.y<=max(x1.x.y,x1.y.y)&&a.y>=min(x1.x.y,x1.y.y))){
                    System.out.println("on the triangle");return ;
                }
            }
            if( x2.tongxian(a)){
                if((a.x>=min(x2.x.x,x2.y.x)&&a.x<=max(x2.x.x,x2.y.x))&&(a.y<=max(x2.x.y,x2.y.y)&&a.y>=min(x2.x.y,x2.y.y))){
                    System.out.println("on the triangle");return ;
                }
            }
            if( x3.tongxian(a)){
                if((a.x>=min(x3.x.x,x3.y.x)&&a.x<=max(x3.x.x,x3.y.x))&&(a.y<=max(x3.x.y,x3.y.y)&&a.y>=min(x3.x.y,x3.y.y))){
                    System.out.println("on the triangle");return ;
                }
            }
            int i;
            int count=0;


            for(i=0;i<3;++i){
                dian p1=w[i];
                dian p2=w[(i+1)%3];
                if(p1.y==p2.y) continue;
                if(a.y<=min(p1.y,p2.y))continue;
                if(a.y>=max(p1.y,p2.y))continue;
                double tep=(a.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
                if(tep>a.x) count++;
            }


            if((count&1)==1) System.out.println("in the triangle");
            else System.out.println("outof the triangle");
        }
    }

    public static double data(double a){
        int tep = (int )(a*1e6+0.5);
        double ans=tep/1e6;
        return ans;
    }
    public static void case1(String []ss){
        if(ss.length!=3) {
            System.out.println("wrong number of points");
            return;
        }
        String [ ] ss1=ss[0].split(",");
        String [ ] ss2=ss[1].split(",");
        String [ ] ss3=ss[2].split(",");
        double x1=Double.parseDouble(ss1[0]);
        double y1=Double.parseDouble(ss1[1]);
        double x2=Double.parseDouble(ss2[0]);
        double y2=Double.parseDouble(ss2[1]);
        double x3=Double.parseDouble(ss3[0]);
        double y3=Double.parseDouble(ss3[1]);
        dian a=new dian(x1,y1);
        dian b=new dian(x2,y2);
        dian c=new dian(x3,y3);
        tri s=new tri(a,b,c);
        if(!s.istri()) {
            System.out.println("data error");
            return ;
        }
        System.out.println(s.isIsosceles()+" "+s.isEquil());
    }
    public static void case2(String []ss){
        if(ss.length!=3) {
            System.out.println("wrong number of points");
            return;
        }
        String [ ] ss1=ss[0].split(",");
        String [ ] ss2=ss[1].split(",");
        String [ ] ss3=ss[2].split(",");
        double x1=Double.parseDouble(ss1[0]);
        double y1=Double.parseDouble(ss1[1]);
        double x2=Double.parseDouble(ss2[0]);
        double y2=Double.parseDouble(ss2[1]);
        double x3=Double.parseDouble(ss3[0]);
        double y3=Double.parseDouble(ss3[1]);
        dian a=new dian(x1,y1);
        dian b=new dian(x2,y2);
        dian c=new dian(x3,y3);
        tri s=new tri(a,b,c);
        if(!s.istri()) {
            System.out.println("data error");
            return ;
        }
        s.output();

    }
    public static void case3(String []ss){
        if(ss.length!=3) {
            System.out.println("wrong number of points");
            return;
        }
        String [ ] ss1=ss[0].split(",");
        String [ ] ss2=ss[1].split(",");
        String [ ] ss3=ss[2].split(",");
        double x1=Double.parseDouble(ss1[0]);
        double y1=Double.parseDouble(ss1[1]);
        double x2=Double.parseDouble(ss2[0]);
        double y2=Double.parseDouble(ss2[1]);
        double x3=Double.parseDouble(ss3[0]);
        double y3=Double.parseDouble(ss3[1]);
        dian a=new dian(x1,y1);
        dian b=new dian(x2,y2);
        dian c=new dian(x3,y3);
        tri s=new tri(a,b,c);
        if(!s.istri()) {
            System.out.println("data error");
            return ;
        }
        s.judge();

    }
    public static void case4(String []ss){
        if(ss.length!=5) {
            System.out.println("wrong number of points");
            return;
        }
//        System.out.println(ss[0]);
//        System.out.println(ss[1]);
        String [ ] ss1=ss[0].split(",");
        String [ ] ss2=ss[1].split(",");
        String [ ] ss3=ss[2].split(",");
        String [ ] ss4=ss[3].split(",");
        String [ ] ss5=ss[4].split(",");
        double x1=Double.parseDouble(ss1[0]);
        double y1=Double.parseDouble(ss1[1]);
        double x2=Double.parseDouble(ss2[0]);
        double y2=Double.parseDouble(ss2[1]);
        double x3=Double.parseDouble(ss3[0]);
        double y3=Double.parseDouble(ss3[1]);
        double x4=Double.parseDouble(ss4[0]);
        double y4=Double.parseDouble(ss4[1]);
        double x5=Double.parseDouble(ss5[0]);
        double y5=Double.parseDouble(ss5[1]);
        dian a=new dian(x1,y1);
        dian b=new dian(x2,y2);
        dian c=new dian(x3,y3);
        dian d=new dian(x4,y4);
        dian e=new dian(x5,y5);
        jian j=new jian(a,b);
        tri s=new tri(c,d,e);
//        System.out.println(a.x+" "+a.y);
//        System.out.println(b.x+" "+b.y);
        if(j.istrue()) {
            System.out.println("points coincide");
            return ;
        }
        if(!s.istri()) {
            System.out.println("data error");
            return ;
        }
        s.jiaodiannum_tri(j);

    }
    public static void case5(String []ss){
        if(ss.length!=4) {
            System.out.println("wrong number of points");
            return;
        }
//        System.out.println(ss[0]);
//        System.out.println(ss[1]);
        String [ ] ss1=ss[0].split(",");
        String [ ] ss2=ss[1].split(",");
        String [ ] ss3=ss[2].split(",");
        String [ ] ss4=ss[3].split(",");
        double x1=Double.parseDouble(ss1[0]);
        double y1=Double.parseDouble(ss1[1]);
        double x2=Double.parseDouble(ss2[0]);
        double y2=Double.parseDouble(ss2[1]);
        double x3=Double.parseDouble(ss3[0]);
        double y3=Double.parseDouble(ss3[1]);
        double x4=Double.parseDouble(ss4[0]);
        double y4=Double.parseDouble(ss4[1]);
        dian a=new dian(x1,y1);
        dian b=new dian(x2,y2);
        dian c=new dian(x3,y3);
        dian d=new dian(x4,y4);
        tri s=new tri(b,c,d);
        if(!s.istri()) {
            System.out.println("data error");
            return ;
        }
        s.inpoint(a);
    }
}

额 额,这次的代码是有一点长(好吧,是很长),感觉是可以优化的,但是没时间了(绝对不是懒)。

整体复杂度较高,代码量较多,结构也不够清晰,其实我觉得这样和写C语言的函数差不多,还是不够面向对象啊。

(3)采坑心得:
第三题7-2的精度问题

要用向量来计算,公式在上面。

在第三题7-2题目一直用float强转,导致错误,以为是和第一次作业一样的用加上float,但偏偏不是。

在第三题7-3题目上case3 ,判断直角三角形时,有精度问题,要判断一下。

 if(abs(arr[2]*arr[2]-(arr[0]*arr[0]+arr[1]*arr[1]))<0.001) zhi=true;
            else if(((arr[0]*arr[0]+arr[1]*arr[1]-arr[2]*arr[2]))<0) dun=true;
            else
                rui=true;

在第三题7-3题目上的输出和样例的输出是不一样的,导致我疯狂wrong,最后还是同学的提醒才过的。
题目上的输出:


样例的输出:



(4)改进建议:写的代码的注释不多,类的复用性很差,遇到了同一种情况,还需改许多。对于精度问题还是不是很在行,不知道从哪里下手。

(5)总结:通过这几次的作业,我加强了对JAVA的理解,学会了运用类与类之间的使用,还有正则表达式的使用(是真的好用),在学习方面应该要多写一些题目,mooc是可以多看看的,总之就是要看自己咯。

posted @ 2022-10-02 15:14  栎悠  阅读(74)  评论(0)    收藏  举报