第一次博客

目录

1. 前言

前期题目十分基础,基本上属于JAVA基本输出输入语法的学习,没有涉及到算法,简单的字符串判断已经处理。后面题目的难度相比较于第一次作业难度跨度较大,尤其是第三次作业,比第二次的作业难度相差不是一般的大,要考虑的方面比较多,一不小心就漏了或者思路错,细心在本次作业中的重要性体现地极为重要。

二. 设计与分析

题目集2 — 7-2 - 串口字符解析

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

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

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

题目分析

题目其实比较简单,重点是要读清楚题目。如果题目不理解做该题就比较难搞。从简单入手,简单输入,但是要考虑周到使用NextLine接收字符串。null data的情况无非就是字符串长度以及输入的数据中全为1,正确数据再依次判断做出其他相应的输出操作。

代码块:

点击查看代码
	import java.util.Scanner;
	public class Main{
    public static void main(String[] args) {
        Scanner set = new Scanner(System.in);
        String a;
        int c = 1;
        a = set.nextLine();
        int flag1 = 1;
        int flag2 = 0;
        if(a.length()<11){
            flag1 = 0;
        }
        for (int i = 0; i < a.length(); i++) {
            if(a.charAt(i)=='1') {
                flag2 = 0;
            }
            if(a.charAt(i)=='0'){
                flag2 = 1;
                break;
            }
        }
        if(flag1==0||flag2==0){
            System.out.print("null data\n");
        }
        else{
            for(int i=0;i<a.length();i++){
                if(a.charAt(i)=='0'&&i+10<a.length()){
                    int k=0;
                    String str =a.substring(i+1,i+9);
                    for(int j=i+1;j<=i+9;j++){
                        if(a.charAt(j)=='1'){
                            k++;
                        }
                    }
                    if(c!=1){
                        System.out.println();
                    }
                    if(k%2!=0&&a.charAt(i+10)=='1'){
                        System.out.printf(c+":"+str);
                        c++;
                    }
                    else if(k%2==0&&a.charAt(i+10)=='1'){
                        System.out.printf(c+":parity check error");
                        c++;
                    }
                    else{
                        System.out.printf(c+":validate error");
                        c++;
                    }
                    i=i+10;
                }
            }
        }
    }
}

SourceMonitor的代码分析图如下:

image

总结与分析

这道题目比较简单,就是题目的分析要到位,重点就是需要注意输出的格式,我的思路是一步一步慢慢来,可能做的比较慢,思路正确后在考虑格式问题

题目集3 — 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.ArrayList;
	import java.util.Scanner;
	public class Main {
    public static void main(String[] args) {
        Scanner set = new Scanner(System.in);
        ArrayList<String> list = new ArrayList<>();
        ArrayList<Double> list1 = new ArrayList<>();
        int flag = 1;
        int index = 0;
        String record;
        String t = set.nextLine();
        String x [] = null;
        String k [] = t.split(" ");
        for (String d : k){
            x = d.split(",");
            for (String j:x){
                if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) {
                    flag = 0;
                }
            }
        }
		//        for (int i = 0; i < t.length(); i++) {
		//            String a [] = t.split(",");
		//            for (int j = 0; j < ; j++) {
		//
		//            }
		//            if(!.matches("^([+-]?\\d+)(\\.\\d+)?")){
        //                flag = 0;
        //                break;
        //            }
        //            else {
        //                flag = 1;
        //            }
        //        }
        //        System.out.printf(String.valueOf(flag));

        System.out.printf(String.valueOf(flag));
        if(flag==1)
        {
            for (int i = 0; i < t.length(); i++) {
                if(t.charAt(i)==' '||t.charAt(i)==',')
                {
                    if(index==0){
                        record = t.substring(index,i);
                    }
                    else {
                        record = t.substring(index + 1, i);
                    }
                    list.add(record);
                    index = i;
                }
            }
            record = t.substring(index+1,t.length());
            list.add(record);
        }

        if(list.size()>4){
            flag = 2;
        }
        if(list.size()<2)
        {
            flag = 0;
        }
        if(flag==1){
            for (int i = 0; i < list.size(); i++) {
                double g = Double.parseDouble(list.get(i));
                list1.add(g);
            }
            double x1 = list1.get(0);
            double y1 = list1.get(1);
            double x2 = list1.get(2);
            double y2 = list1.get(3);
            double s = (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
            double result = Math.sqrt(s);
            System.out.println(result);
        }

        if(flag==0){
            System.out.printf("Wrong Format");
        }
        if(flag ==2)
        {
            System.out.printf("wrong number of points");
        }
    	}
	}

SourceMonitor的代码分析图如下:

image

总结与分析

在本次作业中的三道练习中都用到正则表达式判断,判断输入格式是否合法,第一道题目没有考虑到精确度的问题,所以比较简单,没啥太大问题。因为是将字符串分割再存储到字符串集合中再进行字符串转数字集合的转换,复杂度较于其他同学可能高好多。脑子转的比较慢,想的方法就比较复杂。

题目集3 —— 7-2 - 点线形系列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",

输出格式:
见题目描述。

题目分析

题目二比题目一多了一个选项+“:”,多考虑将输入的字符串直接分割成两个部分,一个就是判断选项和冒号,另一个就是判断输入的坐标。判断正确后在进行分割存储后的操作。对存储的数据进行对应选项的方法。并且注意特殊输出,如重合、垂直或者平行等情况。

代码块:

点击查看代码
	import java.util.ArrayList;
	import java.util.Scanner;

	public class Main {
    public static void main(String[] args) {
        Scanner set = new Scanner(System.in);
        double x1, y1, x2, y2, x3, y3, x4, y4;
        String str = set.nextLine();

        int flag = 1;
        String str1[] = str.split(":");
        String str2 = str1[1];
        String str3[] = str2.split(" ");

        for (String d : str3){
            String x [] = d.split(",");
            if(x.length!=2){
                flag=0;
            }
            for (String j:x){
                if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) {
                    flag = 0;
                }
            }
        }


        if(str.charAt(1)==':'&&flag==1){
        ArrayList<String> list = new ArrayList<>();
        ArrayList<Double> list1 = new ArrayList<>();

        int index = 0;
        String record;

        int j=0;
        for (j= 0; j < str2.length(); j++) {
            if (str2.charAt(j) == ' ' || str2.charAt(j) == ',') {
                if (index == 0) {
                    record = str2.substring(index, j);
                } else {
                    record = str2.substring(index + 1, j);
                }
                list.add(record);
                index = j;
            }
        }

	//         if((str2.charAt(str2.length()-1)!=' ')){
            record = str2.substring(index + 1, str2.length());
            list.add(record);
	//         }

        for (int i = 0; i < list.size(); i++) {
            double g = Double.parseDouble(list.get(i));
            list1.add(g);
        }

	//        System.out.printf(String.valueOf(list));
	//        System.out.printf(String.valueOf(list1));
            switch (str.charAt(0)){
                case '1' :
                    if(list1.size()!=4){
                        System.out.println("wrong number of points");
                    }
                    else {
                        x1 = list1.get(0);
                        y1 = list1.get(1);
                        x2 = list1.get(2);
                        y2 = list1.get(3);
                        double result = method1(x1,y1,x2,y2);
                        if(result!=0){
                            System.out.printf(String.valueOf(result));
                        }
                    }
                    break;
                case '2' :
                    if(list1.size()!=6){
                        System.out.println("wrong number of points");
                    }
                    else {
                        x1 = list1.get(0);
                        y1 = list1.get(1);
                        x2 = list1.get(2);
                        y2 = list1.get(3);
                        x3 = list1.get(4);
                        y3 = list1.get(5);
                        method2(x1,y1,x2,y2,x3,y3);
                    }
                    break;
                case '3' :if(list1.size()!=6){
                    System.out.printf("wrong number of points");
                }
                else{
                    x1 = list1.get(0);
                    y1 = list1.get(1);
                    x2 = list1.get(2);
                    y2 = list1.get(3);
                    x3 = list1.get(4);
                    y3 = list1.get(5);
                    method3(x1,y1,x2,y2,x3,y3);
                }
                    break;
                case '4' :if(list1.size()==8){
                    x1 = list1.get(0);
                    y1 = list1.get(1);
                    x2 = list1.get(2);
                    y2 = list1.get(3);
                    x3 = list1.get(4);
                    y3 = list1.get(5);
                    x4 = list1.get(6);
                    y4 = list1.get(7);
                    method4(x1,y1,x2,y2,x3,y3,x4,y4);
                }
                else{
                    System.out.printf("wrong number of points");
                }
                    break;
                case '5' :if(list1.size()!=8){
                    System.out.printf("wrong number of points");
                }
                else{
                    x1 = list1.get(0);
                    y1 = list1.get(1);
                    x2 = list1.get(2);
                    y2 = list1.get(3);
                    x3 = list1.get(4);
                    y3 = list1.get(5);
                    x4 = list1.get(6);
                    y4 = list1.get(7);
                    method5(x1,y1,x2,y2,x3,y3,x4,y4);
                }
                    break;
                default:System.out.println("Wrong Format");
                    break;
            }
        }
        else
        {
            System.out.printf("Wrong Format");
        }
    }
    public static boolean superposition(double x1,double y1,double x2, double y2){
        if(x1==x2&&y1==y2){
            return true;
        }
        else {
            return false;
        }
    }
    public static double method1(double x1, double y1, double x2, double y2){
        if(!superposition(x1,y1,x2,y2)){
            if(x1 != x2){
                double result = (y2-y1)/(x2-x1);
                return result;
            }
            else {
                System.out.printf("Slope does not exist");
            }
        }
        else {
            System.out.println("points coincide");
        }
        return 0;
    }

    public static void method2(double x1, double y1, double x2, 
                               double y2, double x3, double y3){
        double d;
        d=Math.abs((y3-y2)*x1+(x2-x3)*y1+x3*y2-y3*x2) /
                Math.sqrt(Math.pow(y3-y2, 2)+Math.pow(x3-x2, 2));
        if(superposition(x2, y2, x3, y3)){
            System.out.println("points coincide");
            return;
        }
        System.out.println(d);
    }

    public static void method3(double x1, double y1, double x2,
                               double y2, double x3, double y3) {
        boolean flag = false;
        if(!superposition(x2, y2, x3, y3)&&!superposition(x1, y1, x3, y3)){
            if(x1 == x2 && x2 == x3){
                flag = true;
                System.out.println(flag);
            }
            else{
                if((y2 - y1) / (x2 - x1) - (y3 - y2) / (x3 - x2) == 0) {
                    flag = true;
                    System.out.println(flag);
                } else {
                    System.out.println(flag);
                }
            }

        }
    }

    public static void method4(double x1, double y1, double x2, double y2,
                               double x3, double y3, double x4, double y4){
        double k1=0,k2=0;
        if((!superposition(x1, y1, x2, y2))&&(!superposition(x3,y3,x4,y4))){
            if(x1==x2 && x3 == x4){
                System.out.println("true");
            }
            else {
                k1 = (y2-y1)/(x2-x1);
                k2 = (y4-y3)/(x4-x3);
                if(k1 == k2){
                    System.out.println("true");
                }
                else {
                    System.out.println("false");
                }
            }
        }
        else {
            System.out.println("points coincide");
        }
    }

    public static boolean focus(double x1, double y1, double x2, double y2,
                                double x3, double y3, double x4, double y4,double x5,double y5){
        if ((y2 - y1) / (x5 - x1) == (y5 - y1) / (x1 - x1)
                || (y3 - y2) / (x5 - x2) == (y5 - y2) / (x3 - x2))
            return true;
        else
            return false;
    }


    public static void method5(double x1, double y1, double x2, double y2,
                               double x3, double y3, double x4, double y4){
        double k1 = 0.0,k2 = 0.0;
        double x5,y5;
        if(superposition(x1, y1, x2, y2)||superposition(x3,y3,x4,y4)){
            System.out.print("points coincide");
        }
        if(!superposition(x1, y1, x2, y2)&&!superposition(x3,y3,x4,y4)){
            k1 = (x2-x1)/(y2-y1);
            k2 = (x4-x3)/(y4-y3);
            if(k1 == k2){
                System.out.print("is parallel lines,have no intersection point");
            }
            else {
                y5 = ((y1 - y2) * (y4 - y3) * x1 + (y4 - y3) * (x2 - x1) * y1//xz,yz为交点坐标
                        + (y2 - y1) * (y4 - y3) * x3 + (x3 - x4) * (y2 - y1) * y3)
                        / ((x2 - x1) * (y4 - y3) + (y1 - y2) * (x4 - x3));
                x5 = x3 + (x4 - x3) * (y5 - y3) / (y4 - y3);;

                System.out.print(x5+","+y5+" ");
                if ((y2 - y1) / (x5 - x1) == (y5 - y1) / (x2 - x1)
                        || (y4 - y3) / (x5 - x3) == (y5 - y3) / (x4 - x3))
                    System.out.print("true");
                else
                    System.out.print("false");
            }
        }

    }

    public static boolean isRight(String str) {
        String[] a = str.split(",");
        boolean flag=true;
        for (int i = 0; i < a.length; i++) {
            if(!a[i].matches("^([+-]?\\d+)(\\.\\d+)?")){
                return false;
            }
        }
        return true;
    }
	}

SourceMonitor的代码分析图如下:

image
image

总结与分析

题目难度暴增!!!该题目看着内容好像比较简单,但是考虑的东西非常多。尤其是在判断输入格式正确与否这里!要清楚如何正确使用正则表达式,并且正确分割字符串并对分割的字符串进行判断,这里处理输入格式真的需要思考的。

题目集2 — 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",

题目分析

三个题目相当于题目的进阶再进阶,题目思路还是一样。分割字符串后再对输入格式进行判断。若输入格式无误再进行其他操作。

代码块:

点击查看代码
	import com.sun.jdi.DoubleValue;
	import java.math.BigDecimal;
	import java.text.DecimalFormat;
	import java.text.NumberFormat;
	import java.util.ArrayList;
	import java.util.Scanner;

	public class Main {
    public static void main(String[] args) {
        Scanner set = new Scanner(System.in);
        double x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
        String str = set.nextLine();

        //case6!!!是没有“:“需要输出format
        boolean first = false;
        for (int i = 0; i < str.length();i++) {
            if(str.charAt(i)==':')
                first = true;
        }
        if(first){
            int flag = 1;
            String str1[] = str.split(":");
            String str2 = str1[1];
	//        System.out.printf(str2);
            String str3[] = str2.split(" ");

            for (String d : str3) {
                String x[] = d.split(",");
                if (x.length != 2) {
                    flag = 0;
                }
                for (String j : x) {
                    if (!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) {
                        flag = 0;
                    }
                }
            }

            if(str.charAt(1)==':'&&flag==1){
                ArrayList<String> list = new ArrayList<>();
                ArrayList<Double> list1 = new ArrayList<>();

                int index = 0;
                String record;

                int j=0;
                for (j= 0; j < str2.length(); j++) {
                    if (str2.charAt(j) == ' ' || str2.charAt(j) == ',') {
                        if (index == 0) {
                            record = str2.substring(index, j);
                        } else {
                            record = str2.substring(index + 1, j);
                        }
                        list.add(record);
                        index = j;
                    }
                }

	//        if((str2.charAt(str2.length()-1)!=' ')){
                record = str2.substring(index + 1, str2.length());
                list.add(record);
	//        }

                for (int i = 0; i < list.size(); i++) {
                    double g = Double.parseDouble(list.get(i));
                    list1.add(g);
                }

	//        System.out.printf(String.valueOf(list));
	//        System.out.printf(String.valueOf(list1));
                switch (str.charAt(0)){
                    case '1':
                        if(list1.size()!=6){
                            System.out.printf("wrong number of points");
                        }
                        else {
                            x1 = list1.get(0);
                            y1 = list1.get(1);
                            x2 = list1.get(2);
                            y2 = list1.get(3);
                            x3 = list1.get(4);
                            y3 = list1.get(5);
                            method1sos(x1,y1,x2,y2,x3,y3);
                            method1sss(x1,y1,x2,y2,x3,y3);
                        }
                        break;
                    case '2':
                        if(list1.size()!=6){
                            System.out.printf("wrong number of points");
                        }
                        else {
                            x1 = list1.get(0);
                            y1 = list1.get(1);
                            x2 = list1.get(2);
                            y2 = list1.get(3);
                            x3 = list1.get(4);
                            y3 = list1.get(5);
                            method2C(x1,y1,x2,y2,x3,y3);
                            System.out.printf(method2S(x1,y1,x2,y2,x3,y3)+" ");
                            method2O(x1,y1,x2,y2,x3,y3);
                        }
                        break;
                    case '3':
                        if(list1.size()!=6){
                            System.out.printf("wrong number of points");
                        }
                        else {
                            x1 = list1.get(0);
                            y1 = list1.get(1);
                            x2 = list1.get(2);
                            y2 = list1.get(3);
                            x3 = list1.get(4);
                            y3 = list1.get(5);
                            method3(x1,y1,x2,y2,x3,y3);
                        }
                        break;
                    case '4':
                        if(list1.size()!=10){
                            System.out.printf("wrong number of points");
                        }
                        else {
                            x1 = list1.get(0);
                            y1 = list1.get(1);
                            x2 = list1.get(2);
                            y2 = list1.get(3);
                            x3 = list1.get(4);
                            y3 = list1.get(5);
                            x4 = list1.get(6);
                            y4 = list1.get(7);
                            x5 = list1.get(8);
                            y5 = list1.get(9);
                            method4(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5);
                        }
                        break;
                    case '5':
                        if(list1.size()!=8){
                            System.out.printf("wrong number of points");
                        }
                        else {
                            x1 = list1.get(0);
                            y1 = list1.get(1);
                            x2 = list1.get(2);
                            y2 = list1.get(3);
                            x3 = list1.get(4);
                            y3 = list1.get(5);
                            x4 = list1.get(6);
                            y4 = list1.get(7);
                            method5(x1,y1,x2,y2,x3,y3,x4,y4);
                        }
                        break;
                    default:
                        System.out.printf("Wrong Format");
                        break;
                }
            }
            else {
                System.out.printf("Wrong Format");
            }

        }
        else{
            System.out.printf("Wrong Format");
        }
        }



    public static boolean superposition(double x1,double y1,double x2, double y2){
        if(x1==x2&&y1==y2){
            return true;
        }
        else {
            return false;
        }
    }
    public static double getlinesxv(double x1,double y1,double x2,double y2){
        double xv = (y2-y1)/(x2-x1);
        return xv;
    }
    public static double Distance(double x1,double y1,double x2,double y2) {
        double d = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
        return d;
    }
    public static void method1sos(double x1, double y1, double x2,
                               double y2, double x3, double y3){
        double d1 = Distance(x1, y1, x2, y2);
        double d2 = Distance(x1, y1, x3, y3);
        double d3 = Distance(x2, y2, x3, y3);
        if(superposition(x1, y1, x2, y2)||
                superposition(x1, y1, x3, y3)|| superposition(x2, y2,x3, y3)
                ||!(d1+d2>d3 || d1+d3>d2 || d2+d3>d1)){
            System.out.printf("data error");
        }
        else{
            if(d1==d2||d1==d3||d2==d3){
                System.out.printf("true ");
            }
            else {
                System.out.printf("false ");
            }
        }
    }
    public static void method1sss(double x1, double y1, double x2,
                                  double y2, double x3, double y3){
        double d1 = Distance(x1, y1, x2, y2);
        double d2 = Distance(x1, y1, x3, y3);
        double d3 = Distance(x2, y2, x3, y3);
        if(!superposition(x1, y1, x2, y2)&&
                !superposition(x1, y1, x3, y3)&& !superposition(x2, y2, x3, y3)
                && (d1+d2>d3||d1+d3>d2||d2+d3>d1)){
            if(d1==d2&&d1==d3&&d2==d3){
                System.out.printf("true");
            }
            else {
                System.out.printf("false");
            }
        }
    }
    public static void method2C(double x1, double y1, double x2,
                                   double y2, double x3, double y3){
        double C = 0;
        double d1 = Distance(x1, y1, x2, y2);
        double d2 = Distance(x1, y1, x3, y3);
        double d3 = Distance(x2, y2, x3, y3);
        if(superposition(x1, y1, x2, y2)||
                superposition(x1, y1, x3, y3)|| superposition(x2, y2,x3, y3)
                || !(d1+d2>d3 || d1+d3>d2 || d2+d3>d1)){
            System.out.printf("data error");
        }
        else {
            C = d1 + d2 +d3;
            String format2 = new DecimalFormat("#.######").format(C);
            String str = String.valueOf(C);
            if(str.length()>8)
                System.out.printf(Double.parseDouble(format2)+" ");
            else
                System.out.printf(String.valueOf(C));
        }
    }
    public static double method2S(double x1, double y1, double x2,
                                double y2, double x3, double y3){
        double d1 = Distance(x1, y1, x2, y2);
        double d2 = Distance(x1, y1, x3, y3);
        double d3 = Distance(x2, y2, x3, y3);
        double C = d1 + d2 +d3;
        double S = Math.abs(Math.pow((C/2)*(C/2-d1)*(C/2-d2)*(C/2-d3),0.5));
        if(!superposition(x1, y1, x2, y2)&&
                !superposition(x1, y1, x3, y3)&& !superposition(x2, y2, x3, y3)
                && (d1+d2>d3||d1+d3>d2||d2+d3>d1)){
            String format2 = new DecimalFormat("#.######").format(S);
            String str = String.valueOf(S);
            if(str.length()>8)
                return Double.parseDouble(format2);
            else
                return S;
        }
        return 0;
    }
    public static void method2O(double x1, double y1, double x2,
                                double y2, double x3, double y3){
        double d1 = Distance(x1, y1, x2, y2);
        double d2 = Distance(x1, y1, x3, y3);
        double d3 = Distance(x2, y2, x3, y3);
        double xo = (x1+x2+x3)/3;
        double yo = (y1+y2+y3)/3;
        if(!superposition(x1, y1, x2, y2)&&
                !superposition(x1, y1, x3, y3)&& !superposition(x2, y2, x3, y3)
                && (d1+d2>d3||d1+d3>d2||d2+d3>d1)) {
            String format2 = new DecimalFormat("#.######").format(xo);
            String str = String.valueOf(xo);
            if(str.length()>8)
                System.out.printf(Double.parseDouble(format2)+",");
            else
                System.out.printf(xo+",");

            String format3 = new DecimalFormat("#.######").format(yo);
            String str2 = String.valueOf(yo);
            if(str2.length()>8)
                System.out.printf(String.valueOf(Double.parseDouble(format3)));
            else
                System.out.printf(String.valueOf(yo));
        }
    }
    public static void method3(double x1, double y1, double x2,
                               double y2, double x3, double y3){
        double d1 = (x2-x1)*(x3-x1)+(y2-y1)*(y3-y1);
        double d2 = (x3-x2)*(x1-x2)+(y3-y2)*(y1-y2);
        double d3 = (x1-x3)*(x2-y3)+(y1-y3)*(y2-y3);
        if(superposition(x1, y1, x2, y2)||
                superposition(x1, y1, x3, y3)|| superposition(x2, y2,x3, y3)
                ||!(d1+d2>d3 || d1+d3>d2 || d2+d3>d1)){
            System.out.printf("data error");
        }
        if(!superposition(x1, y1, x2, y2)&&!superposition(x1, y1, x3, y3)&&
                !superposition(x2, y2,x3, y3)&&(d1+d2>d3 || d1+d3>d2 || d2+d3>d1)){
            if(d1<0||d2<0||d3<0){
                System.out.print("true false false");
            }
            else if(d1==0||d2==0||d3==0){
                System.out.print("false true false");
            }
            else{
                System.out.print("false false true");
            }
        }
    }

    public static double getjdy(double x1, double y1, double x2, double y2,
                                double x3, double y3, double x4, double y4){
        double m = (y2-y1)*(x3-x4)-(y4-y3)*(x1-x2);
        double y = ((x2 * y1 - x1 * y2)*(y4 - y3) - (x4 * y3 - x3 * y4)*(y2 - y1)) / m;
        return y;
    }
    public static double getjdx(double x1, double y1, double x2, double y2,
                                double x3, double y3, double x4, double y4){
        double m = (y2-y1)*(x3-x4)-(y4-y3)*(x1-x2);
        double x = ((x4 * y3 - x3 * y4)*(x1 - x2) - (x2 * y1 - x1 * y2)*(x3 - x4)) / m;
        return x;
    }
    public static double max(double x1,double x2){
        if(x1>x2){
            return x1;
        }else {
            return x2;
        }
    }
    public static double min(double x1,double x2){
        if(x1<x2){
            return x1;
        }else {
            return x2;
        }
    }
    //判断一个点在一条线段上 (a,b)是这个点
    public static boolean isOnLine(double a,double b,double x1,double y1,double x2,double y2){
        boolean bl = false;
        double d1 = Distance(a,b,x1,y1);
        double d2 = Distance(a,b,x2,y2);
        double D  = Distance(x1,y1,x2,y2);
        if(d1+d2==D){
            return true;
        }
        else{
            double p = (y2-y1)/(x2-x1);
            if(p<=0){
                if(x1>x2){
                    if(y1>y2){
                        if(a>x2 && a<x1 && b>=y2 && b<=y1){
                            bl = true;
                        }
                    }else{
                        if(a>x2 && a<x1 && b>=y1 && b<=y2){
                            bl = true;
                        }
                    }
                }else{
                    if(y1>y2){
                        if(a>x1 && a<x2 && b>=y2 && b<=y1){
                            bl = true;
                        }
                    }else{
                        if(a>x1 && a<x2 && b>=y1 && b<=y2){
                            bl = true;
                        }
                    }
                }
            }else{
                if(x1>x2){
                    if(y1>y2){
                        if(a>x2 && a<x1 && b>y2 && b<y1){
                            bl = true;
                        }
                    }else{
                        if(a>x2 && a<x1 && b>y1 && b<y2){
                            bl = true;
                        }
                    }
                }else{
                    if(y1>y2){
                        if(a>x1 && a<x2 && b>y2 && b<y1){
                            bl = true;
                        }
                    }else{
                        if(a>x1 && a<x2 && b>y1 && b<y2){
                            bl = true;
                        }
                    }
                }
            }
        }
        return bl;
    }

    public static double juli(double x1, double y1, double x2,
                               double y2, double x3, double y3){
        double d;
        d=Math.abs((y3-y2)*x1+(x2-x3)*y1+x3*y2-y3*x2) /
                Math.sqrt(Math.pow(y3-y2, 2)+Math.pow(x3-x2, 2));
            return d;
        }
    public static boolean chline(double x1, double y1, double x2, double y2,
                                 double x3, double y3, double x4, double y4){
        double d1 = juli(x1, y1, x4, y4, x3, y3);
        double d2 = juli(x2, y2, x4, y4, x3, y3);
        if(d1==d2&&d1==0)
            return true;
        else
            return false;
    }
    public static ArrayList<Double> jd(double x1, double y1, double x2, double y2,
                                       double x3, double y3, double x4, double y4,double x5,double y5){
        ArrayList<Double> jiaodian = new ArrayList<>();
        double line1 = getlinesxv(x3,y3,x4,y4);
        double line2 = getlinesxv(x3,y3,x5,y5);
        double line3 = getlinesxv(x4,y4,x5,y5);
        double line4 = getlinesxv(x1,y1,x2,y2);
        double line13 = getlinesxv(x1,y1,x3,y3);
        double line14 = getlinesxv(x1,y1,x4,y4);
        double line15 = getlinesxv(x2,y2,x5,y5);
        double line23 = getlinesxv(x2,y2,x3,y3);
        double line24 = getlinesxv(x2,y2,x4,y4);
        double line25 = getlinesxv(x2,y2,x5,y5);
        double line45 = getlinesxv(x4,y4,x5,y5);
        double line35 = getlinesxv(x3,y3,x5,y5);
        double line34 = getlinesxv(x3,y3,x4,y4);
        if(chline(x1, y1, x2, y2, x3, y3, x4, y4)||chline(x1, y1, x2, y2, x3, y3, x5, y5)||
        chline(x1, y1, x2, y2, x5, y5, x4, y4)){
            return jiaodian;
        }
        else{
            //x1x2与线段1:
            if(line4!=line1){
                double getline1jdx = getjdx(x1,y1,x2,y2,x3,y3,x4,y4);
                double getline1jdy = getjdy(x1,y1,x2,y2,x3,y3,x4,y4);
		//                System.out.println(getline1jdx+" "+getline1jdy+" 线1");
                boolean flagjd1 = isOnLine(getline1jdx,getline1jdy,x3,y3,x4,y4);
		//                System.out.println(flagjd1);
                if(flagjd1){
                        jiaodian.add(getline1jdx);
                        jiaodian.add(getline1jdy);
                }
            }
            //x1,x2与线段2:
            if(line4!=line2){
                double getline2jdx = getjdx(x1,y1,x2,y2,x3,y3,x5,y5);
                double getline2jdy = getjdy(x1,y1,x2,y2,x3,y3,x5,y5);
		//                System.out.println(getline2jdx+" "+getline2jdy+" 线2");

                boolean flagjd2 = isOnLine(getline2jdx,getline2jdy,x3,y3,x5,y5);
		//                System.out.println(flagjd2);
                if(flagjd2){
                    if(jiaodian.size()==0){
                        jiaodian.add(getline2jdx);
                        jiaodian.add(getline2jdy);
                    }
                    else {
                        int flag = 1;
                        for (int i = 0; i < jiaodian.size(); i=i+2) {
                            if(jiaodian.get(i)==getline2jdx&&jiaodian.get(i+1)==getline2jdy)
                            {
                                flag=0;
		//                                System.out.printf("2重复啦");
                                break;
                            }
                        }
                        if(flag == 1){
                            jiaodian.add(getline2jdx);
                            jiaodian.add(getline2jdy);
                        }
                    }
                }
            }
		//4:2,8 2,5 1,0 1,2 4,0
            //x1,x2与线段3:
            if(line4!=line3){
                double getline3jdx = getjdx(x1,y1,x2,y2,x4,y4,x5,y5);
                double getline3jdy = getjdy(x1,y1,x2,y2,x4,y4,x5,y5);
	//                System.out.println(getline3jdx+" "+getline3jdy+" 线3");
	//                System.out.printf(Distance(getline3jdx,getline3jdy,x4,y4)+" ");
	//                System.out.printf(Distance(getline3jdx,getline3jdy,x5,y5)+" ");
	//                System.out.printf(Distance(x4,y4,x5,y5)+" ");
	//                if(Distance(getline3jdx,getline3jdy,x4,y4)+Distance(getline3jdx,getline3jdy,x5,y5)-Distance(x4,y4,x5,y5)<0.000001){
	//                    System.out.printf("yes");
	//                }
	//                else {
	//                    System.out.printf("no");
	//                }
                boolean flagjd3 = isOnLine(getline3jdx,getline3jdy,x4,y4,x5,y5);
	//                System.out.printf("dada");
	//                System.out.println(flagjd3);
                if(flagjd3){
                    if(jiaodian.size()==0){
                    jiaodian.add(getline3jdx);
                    jiaodian.add(getline3jdy);
                }
                    else {
                        int flag = 1;
                        for (int i = 0; i < jiaodian.size(); i=i+2) {
                            if(jiaodian.get(i)==getline3jdx&&jiaodian.get(i+1)==getline3jdy)
                            {
                                flag=0;
	//                                System.out.printf("3重复啦");
                                break;
                            }
                        }
                        if(flag == 1){
                            jiaodian.add(getline3jdx);
                            jiaodian.add(getline3jdy);
                        }
                    }
                }
            }
        }
        return jiaodian;
    }
    public static void method4(double x1, double y1, double x2, double y2,
                               double x3, double y3, double x4, double y4,double x5,double y5) {
        double line1 = getlinesxv(x3, y3, x4, y4);
        double line2 = getlinesxv(x3, y3, x5, y5);
        double line3 = getlinesxv(x4, y4, x5, y5);
        double line4 = getlinesxv(x1, y1, x2, y2);

        if (superposition(x1, y1, x2, y2)) {
            System.out.printf("points coincide");
        } else if (superposition(x3, y3, x4, y4) ||
                superposition(x4, y4, x5, y5) || superposition(x3, y3, x5, y5)
                || line1 == line2 || line1 == line3 || line3 == line2) {
            System.out.printf("data error");
        } else {
            ArrayList<Double> JD;
            JD = jd(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5);
            if(JD.size()==0){
                if(chline(x1, y1, x2, y2, x3, y3, x4, y4)||chline(x1, y1, x2, y2, x3, y3, x5, y5)||
                   chline(x1, y1, x2, y2, x5, y5, x4, y4)){
                    System.out.printf("The point is on the edge of the triangle");
                    System.exit(0);
                }
                if(!chline(x1, y1, x2, y2, x3, y3, x4, y4)&&!chline(x1, y1, x2, y2, x3, y3, x5, y5)&&
                        !chline(x1, y1, x2, y2, x5, y5, x4, y4)){
                    System.out.printf("0");
                    System.exit(0);
                }
            }
            if(JD.size()!=4){
                System.out.printf(String.valueOf(JD.size()/2));
                System.exit(0);
            }
            if(JD.size()==4){
                System.out.printf(JD.size() / 2 + " ");
                double S = method2S(x3, y3, x4, y4, x5, y5);
	//                for (int i = 0; i < JD.size(); i++) {
	//                    System.out.println(JD.get(i) + " " + i);
	//                }
                double x11 = JD.get(0);
                double y11 = JD.get(1);
                double x22 = JD.get(2);
                double y22 = JD.get(3);
                double s123 = method2S(x11, y11, x22, y22, x3, y3);
                double s124 = method2S(x11, y11, x22, y22, x4, y4);
                double s245 = method2S(x22, y22, x4, y4, x5, y5);
                double s145 = method2S(x11, y11, x4, y4, x5, y5);
                double s125 = method2S(x11, y11, x22, y22, x5, y5);
                double s134 = method2S(x11, y11, x3, y3, x4, y4);
                double s234 = method2S(x22, y22, x3, y3, x4, y4);
                double s235 = method2S(x22, y22, x3, y3, x5, y5);
                double s135 = method2S(x11, y11, x3, y3, x5, y5);
                double sh = 0;
                if (x11 == x3 && y11 == y3) {
                    if (S == s124 + s125) {
                        System.out.printf(min(s124, s125) + " " + max(s124, s125));
                        System.exit(0);
                    }
                } else if (x22 == x3 && y22 == y3) {
                    if (S == s124 + s125) {
                        System.out.printf(min(s124, s125) + " " + max(s124, s125));
                        System.exit(0);
                    }
                } else if (x11 == x4 && y11 == y4) {
                    if (S == s123 + s125) {
                        System.out.printf(min(s123, s125) + " " + max(s123, s125));
                        System.exit(0);
                    }
                } else if (x22 == x4 && y22 == y4) {
                    if (S == s123 + s125) {
                        System.out.printf(min(s123, s125) + " " + max(s123, s125));
                        System.exit(0);
                    }
                } else if (x11 == x5 && y11 == y5) {
                    if (S == s123 + s124) {
                        System.out.printf(min(s123, s124) + " " + max(s123, s124));
                        System.exit(0);
                    }
                }
                else if(x22 == x5 && y22 == y5) {
                    if (S == s123 + s124) {
                        System.out.printf(min(s123, s124) + " " + max(s123, s124));
                        System.exit(0);
                    }
                }
                if ((isOnLine(x11, y11, x3, y3, x4, y4) && isOnLine(x22, y22, x3, y3, x5, y5)) ||
                        (isOnLine(x22, y22, x3, y3, x4, y4) && isOnLine(x11, y11, x3, y3, x5, y5))) {
                        sh = S-s123;
                        String format2 = new DecimalFormat("#.######").format(sh);
                        String str = String.valueOf(sh);
                        if(str.length()>8){
                            System.out.printf(min(s123,Double.parseDouble(format2))+" "+max(s123,Double.parseDouble(format2)));
                            System.exit(0);
                        }
                        else{
                        System.out.printf(min(s123, sh) + " " + max(s123, sh));
                        System.exit(0);
                    }
                } else if ((isOnLine(x11, y11, x3, y3, x4, y4) && isOnLine(x22, y22, x4, y4, x5, y5)) ||
                           (isOnLine(x22, y22, x3, y3, x4, y4) && isOnLine(x11, y11, x4, y4, x5, y5))) {
                        sh = S-s124;
                        String format2 = new DecimalFormat("#.######").format(sh);
                        String str = String.valueOf(sh);
                        if(str.length()>8){
                            System.out.printf(min(s124,Double.parseDouble(format2))+" "+max(s124,Double.parseDouble(format2)));
                            System.exit(0);
                        }
                        else{
                            System.out.printf(min(s124, sh) + " " + max(s124, sh));
                            System.exit(0);
                        }
                } else if ((isOnLine(x11, y11, x3, y3, x5, y5) && isOnLine(x22, y22, x4, y4, x5, y5)) ||
                           (isOnLine(x22, y22, x3, y3, x5, y5) && isOnLine(x11, y11, x4, y4, x5, y5))) {
                        sh = S-s125;
                        String format2 = new DecimalFormat("#.######").format(sh);
                        String str = String.valueOf(sh);
                        if(str.length()>8){
                            System.out.printf(min(s125,Double.parseDouble(format2))+" "+max(s125,Double.parseDouble(format2)));
                            System.exit(0);
                        }
                        else{
                            System.out.printf(min(s125, sh) + " " + max(s125, sh));
                            System.exit(0);
                        }
                }
            }

        }
    }


    public static void method5(double x1, double y1, double x2, double y2,
                               double x3, double y3, double x4, double y4){
        double s1 = method2S(x1,y1,x2,y2,x3,y3);
        double s2 = method2S(x1,y1,x2,y2,x4,y4);
        double s3 = method2S(x1,y1,x3,y3,x4,y4);
        double S = method2S(x2,y2,x3,y3,x4,y4);
        double line1 = getlinesxv(x2,y2,x3,y3);
        double line2 = getlinesxv(x2,y2,x4,y4);
        double line3 = getlinesxv(x3,y3,x4,y4);

        if(superposition(x4, y4, x2, y2) ||
                superposition(x4, y4, x3, y3) || superposition(x2, y2,x3, y3)
                || line1==line2 || line1==line3 || line3==line2){
            System.out.printf("data error");
        }
        if((!superposition(x4, y4, x2, y2)&&!superposition(x4, y4, x3, y3)&&
                !superposition(x2, y2,x3, y3))
                && line1!=line2 && line1!=line3 && line3!=line2){
            if(Distance(x1, y1, x2, y2)+Distance(x1, y1, x3, y3)==Distance(x2,y2,x3,y3)||
                    Distance(x1, y1, x2, y2)+Distance(x1, y1, x4, y4)==Distance(x2,y2,x4,y4)||
                    Distance(x1, y1, x3, y3)+Distance(x1, y1, x4, y4)==Distance(x4,y4,x3,y3)){
                System.out.printf("on the triangle");
                }
            else{
                if(s1+s2+s3==S){
                    System.out.printf("in the triangle");
                }
                else {
                    System.out.printf("outof the triangle");
                }
            }
        }
    }
	}

SourceMonitor的代码分析图如下:

image
image

总结与分析

题目难度还是一样成指数型增长,一个练习多学一门知识,多了解一种方法。该题目的选项二的输出格式需要思考如何输出题目要求的格式。并且精度问题十分重要,一不小心判断就会出现问题。并且输入格式的判断还是一样的难,需要认真考虑。设计计算的方法需要考虑到精度的问题。

三.踩坑心得

题目集 2 - 7-2

该题目无太大问题,要注意当前判断字符串的下标,以及需要判断的长度

题目集 3 - 7-1

1.由于起初的集合的对象是字符串集合,后需要将字符串集合转成数字集合在进行计算
image
image
image

题目集 3 - 7-2

这里有个非常重要的地方!!就是题目给出的其中一个测试点是多了一个空格的!!因此我对空格进行了判断,本来其他测试点不需要的。
image
image
如果没有这个判断这个有空格的测试样例会出现长度问题的报错。
image
image

题目集 3 - 7-3

坑点1:输入格式问题,测试点与第二题不太一样,但是还是一样的思路,测试点没过,我就对之前的判断输入格式正确进行了修改。格式问题的测试点才过。
坑点2:改题目选项2的计算输出的格式是需要考虑的,这里的输出我是通过查阅资料并询问同学才将输出格式的测试点完成的。
image
坑点3:精度问题十分重要!!!不管是哪里,一定要考虑精度计算时导致误差后的判断!本来有精度相关问题,导致判断错误的,后面我换了个方法判断,避免精度问题导致错误判断。例如点在线段上、以及直线重合。
坑点4:对数据进行操作时,一定要注意操作前的判断。比如两个点重合、两条线重合等等,在操作前将操作的所有情况都要考虑清楚,要不然就会出现问题,如选项4中的直线在三角形边上还能输出三角形面积。

四.代码改进

第一题,应该没有太大问题。
第三次作业中,坐标问题,其实可以利用设计类和设计对象方法,应该更加简洁明了,对于查找自己的bug有很大帮助,我的题目并没有设计其他类的操作,只有一个测试类。所以第三次的作业,可以多设计一些关于类的方法,比如在坐标中,可以设计点、线类,就可以对点、以及线的方法再进行设计和运用,这样能够大大提高代码的清晰度,降低复杂度。

五.总结

题目难度属实是指数型增长,而且测试点比较多。需要考虑的非常周到完整,本次题目还是缺少了对象概念,知识学了很多,除了java基本的语法,还能够了解到更多的函数方法,本次作业重点在于对字符串的相应问题进行处理,以及计算、判断等等多方面的考虑。对于程序员的思路提升还是有的。并且能够自己独自完成并且能够通过感到十分高兴,不断学习一些其他方法还是很舒服的。也希望自己能够通过思考想到一些更完整的思路和写代码的方法.能够写出一套非常实用的代码!

建议:希望老师能够多讲解题目,并且说明题目的某些测试点设置的原因

posted @   昌航小迷弟四号  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
点击右上角即可分享
微信分享提示