# PTA博客作业(一)

PTA博客作业(一)

1.前言

 在三次题目集中,虽然题目的数量是依次递减,但是题目的难度是依次递增的。从第一次的熟悉理解Scanner输入输出的基础,到后面正则表达式、多边形计算的构造与调用,是一环扣着一环,后面所用到的知识都是在前面的基础上逐步扩展来的。代码的编写方式在此显得尤为重要——面向对象而非面向过程。而我在本次作业中正好犯了“面向过程”的的大忌,且代码复用尤为明显,只能给大家立个“反面教材”了。

2.设计与分析

①题集一:

本题集只是初识JAVA的基础部分,主要考验的是学生运用JAVA进行基本的输入输出,以及简单的处理字符串,因此不在此不作过多分析。

②题集二:

7-2 串口字符解析:

题目:
RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5-8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5-8位,具体位数由通信双方提前设置)前加上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

题目分析:
本题主要针对二进制输入流进行判断分析,首先是判断数据的个数,然后是起始符和结束符,接着判断其奇偶校验,是一步跟着一步走的,分别进行正确的条件判断即可,若输入均合格,则可按判断出的起始结束位置,输出合法数据。

源码展示:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        int i,j=0,num=0,l,flag=0;
        String s;
        Scanner input=new Scanner(System.in);
        s=input.nextLine();
        l=s.length();
        for(i=0;i<l;i++)
        {
            flag=0;
            if(s.charAt(i)=='0')
            {
                if(i+11<=l)
                {
                    for(j=i+1;j<=i+8;j++)
                        if(s.charAt(j)=='1')
                            flag++;
                }
                if(i+11<=l&&((s.charAt(i+9)=='1'&&flag%2==0)||(s.charAt(i+9)=='0'&&flag%2==1))&&s.charAt(i+10)=='1')
                {
                    num++;
                    System.out.print(num+":");
                    for(j=i+1;j<=i+8;j++)
                    {
                        if(j!=i+8)
                            System.out.print(s.charAt(j));
                        else
                            System.out.println(s.charAt(j));
                    }
                    i+=10;
                }
                else if(i+11<=l&&s.charAt(i+10)=='0')
                {
                    num++;
                    System.out.println(num+":validate error");
                    i+=10;
                }
                else if(i+11<=l&&((s.charAt(i+9)=='0'&&flag%2==0)||(s.charAt(i+9)=='1'&&flag%2==1)))
                {
                    num++;
                    System.out.println(num+":parity check error");
                    i+=10;
                }
            }
        }
        if(num==0)
            System.out.print("null data");
    }
}

类图:
img

SourceMonitor生成的报表内容:
img
img

本题心得:
本题主要是要了解题目意思,尤其是“奇偶校验”这个点,如果了解了题目的意思,做题会方便很多。关于代码的的可读性和复用性,从上图可看出并未达到指标,因为代码都放进主函数里去了,还是有提升空间,需要改进自身的编程方法。

③题集三:

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

题目分析:
本题的重点在于会用到正则表达式来判断输入的格式正确与否,若不符合正则表达式,则非法输出"Wrong Format",之后进行坐标点数量判断,超过指定输出“wrong number of points”,最后进行两点的直线距离计算,输出结果。

源码展示:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        String s;
        double x1,x2,y1,y2,ans;
        int flag=1;
        Scanner input=new Scanner(System.in);
        s=input.nextLine();
        String[] str=s.split(" ");
        String[] num,num1,num2;
        for(String s1:str)
        {
            num=s1.split(",");
            for(String s2:num)
            {
                if(!s2.matches("^[+-]?(0|[1-9][0-9]*(\\.[0-9]*[1-9])?|(0\\.[0-9]*[1-9])?)"))
                {
                    System.out.print("Wrong Format");
                    flag=0;
                    break;
                }
            }
        }
        if(str.length!=2&&flag==1)
        {
            System.out.print("wrong number of points");
            flag=0;
        }
        if(flag==1)
        {
            num1=str[0].split(",");
            x1=Double.valueOf(num1[0]);
            y1=Double.valueOf(num1[1]);
            num2=str[1].split(",");
            x2=Double.valueOf(num2[0]);
            y2=Double.valueOf(num2[1]);
            ans=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
            System.out.print(ans);
        }
    }
}

类图:
img

SourceMonitor生成的报表内容:
img
img

本题心得:
如之前题目集二的7-2类似,代码的可读性和复用性差,需要合理的分类编程,同时正则表达式的应用,会使得本题的解决方式方便很多,如果单纯的只是if-else则会相对麻烦很多,本题则为后面打下了基础。

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",

输出格式:
见题目描述。

题目分析:
在7-1的基础上,多了各个选项的情况分析,除了正则表达式以外,还有对坐标点数量的判断,以及需要对点和线的位置、距离计算等数学知识进行合理运用。

源码展示:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        String str, s2;
        String[] s1, s3, num;
        double x1, x2, x3, x4, y1, y2, y3, y4, k1, k2, m, m1, l, x, y,a1,b1,c1,a2,b2,c2;
        int flag = 1;
        char a, b, c;
        Scanner input = new Scanner(System.in);
        str = input.nextLine();
        a = str.charAt(0);
        b = str.charAt(1);
        c = str.charAt(str.length() - 1);
        if (b != ':')
            System.out.print("Wrong Format");
        else if (a < '1' || a > '5')
            System.out.print("Wrong Format");
        else if (c < '0' || c > '9')
            System.out.print("Wrong Format");
        else {
            s1 = str.split(":");
            if (s1.length != 2) {
                System.out.print("Wrong Format");
                return;
            }
            s2 = s1[1];
            s3 = s2.split(" ");
            for (String i : s3) {
                num = i.split(",");
                if(num.length!=2)
                {
                    System.out.print("Wrong Format");
                    flag=0;
                    break;
                }
                for (String j : num) {
                    if (!j.matches("^[+-]?(0|[1-9][0-9]*(\\.\\d+)?|(0\\.\\d+)?)") && flag == 1) {
                        System.out.print("Wrong Format");
                        flag = 0;
                        break;
                    }
                }
            }

            if (s3.length != 2 && flag == 1 && a == '1') {
                System.out.print("wrong number of points");
                flag = 0;
            } else if (s3.length != 3 && flag == 1 && (a == '2' || a == '3')) {
                System.out.print("wrong number of points");
                flag = 0;
            } else if (s3.length != 4 && flag == 1 && (a == '4' || a == '5')) {
                System.out.print("wrong number of points");
                flag = 0;
            }
            if (flag == 1) {
                if (a == '1') {
                    num = s3[0].split(",");
                    x1 = Double.valueOf(num[0]);
                    y1 = Double.valueOf(num[1]);
                    num = s3[1].split(",");
                    x2 = Double.valueOf(num[0]);
                    y2 = Double.valueOf(num[1]);
                    if (x1 == x2 && y1 == y2) {
                        System.out.print("points coincide");
                        return;
                    }
                    if (x1 == x2)
                        System.out.print("Slope does not exist");
                    else if (x1 != x2) {
                        k1 = (y1 - y2) / (x1 - x2);
                        System.out.print(k1);
                    }
                } else if (a == '2') {
                    num = s3[0].split(",");
                    x1 = Double.valueOf(num[0]);
                    y1 = Double.valueOf(num[1]);
                    num = s3[1].split(",");
                    x2 = Double.valueOf(num[0]);
                    y2 = Double.valueOf(num[1]);
                    num = s3[2].split(",");
                    x3 = Double.valueOf(num[0]);
                    y3 = Double.valueOf(num[1]);
                    if ((x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3) || (x2 == x3 && y2 == y3)) {
                        System.out.print("points coincide");
                        return;
                    }
                    if (x2 == x3)
                        System.out.print(Math.abs(x2 - x1));
                    else {
                        k2 = (y3 - y2) / (x3 - x2);
                        m = y2 - k2 * x2;
                        l = Math.abs(k2 * x1 - y1 + m) / Math.sqrt(k2 * k2 + 1);
                        System.out.print(l);
                    }
                } else if (a == '3') {
                    num = s3[0].split(",");
                    x1 = Double.valueOf(num[0]);
                    y1 = Double.valueOf(num[1]);
                    num = s3[1].split(",");
                    x2 = Double.valueOf(num[0]);
                    y2 = Double.valueOf(num[1]);
                    num = s3[2].split(",");
                    x3 = Double.valueOf(num[0]);
                    y3 = Double.valueOf(num[1]);
                    if ((x1 == x2 && y1 == y2) || (x1 == x3 && y1 == y3) || (x2 == x3 && y2 == y3)) {
                        System.out.print("points coincide");
                        return;
                    }
                    if (x1 == x2 && x2 == x3)
                        System.out.print("true");
                    else {
                        k1 = (y1 - y2) / (x1 - x2);
                        k2 = (y3 - y2) / (x3 - x2);
                        if (k1 == k2)
                            System.out.print("true");
                        else
                            System.out.print("false");
                    }
                } else if (a == '4') {
                    num = s3[0].split(",");
                    x1 = Double.valueOf(num[0]);
                    y1 = Double.valueOf(num[1]);
                    num = s3[1].split(",");
                    x2 = Double.valueOf(num[0]);
                    y2 = Double.valueOf(num[1]);
                    num = s3[2].split(",");
                    x3 = Double.valueOf(num[0]);
                    y3 = Double.valueOf(num[1]);
                    num = s3[3].split(",");
                    x4 = Double.valueOf(num[0]);
                    y4 = Double.valueOf(num[1]);
                    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)) {
                        System.out.print("points coincide");
                        return;
                    }
                    if (x1 == x2 && x3 == x4)
                        System.out.print("true");
                    else {
                        k1 = (y1 - y2) / (x1 - x2);
                        k2 = (y3 - y4) / (x3 - x4);
                        if (k1 == k2)
                            System.out.print("true");
                        else
                            System.out.print("false");
                    }
                } else if (a == '5') {
                    num = s3[0].split(",");
                    x1 = Double.valueOf(num[0]);
                    y1 = Double.valueOf(num[1]);
                    num = s3[1].split(",");
                    x2 = Double.valueOf(num[0]);
                    y2 = Double.valueOf(num[1]);
                    num = s3[2].split(",");
                    x3 = Double.valueOf(num[0]);
                    y3 = Double.valueOf(num[1]);
                    num = s3[3].split(",");
                    x4 = Double.valueOf(num[0]);
                    y4 = Double.valueOf(num[1]);
                    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)) {
                        System.out.print("points coincide");
                        return;
                    }
                    a1=y2-y1;
                    a2=y4-y3;
                    b1=x1-x2;
                    b2=x3-x4;
                    c1=x2*y1-x1*y2;
                    c2=x4*y3-x3*y4;
                    if(a1*b2-a2*b1==0)
                        System.out.print("is parallel lines,have no intersection point");
                    else {
                        x = (b2 * c1 - b1 * c2) / (a2 * b1 - a1 * b2);
                        y = (a1 * c2 - a2 * c1) / (a2 * b1 - a1 * b2);
                        if ((x - x1) * (x - x2) < 0 || (x - x3) * (x - x4) < 0 || (y - y1) * (y - y2) < 0 || (y - y3) * (y - y4) < 0)
                            System.out.print(x + "," + y + " true");
                        else
                            System.out.print(x + "," + y + " false");
                    }
                }
            }
        }
    }
}

类图:
img

SourceMonitor生成的报表内容:
img
img

本题心得:
没有很好的分类解题,导致代码的可读性和复用性差,代码的复制粘贴较多,另外对点的坐标数量的判断,利用了分割判断,实现了较好的处理。其他的判断处理过于单一重复,有待改进。

7-3 点线形系列3-三角形的计算

题目:
用户输入一组选项和数据,进行与三角形有关的计算。选项包括:
1:输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果输出true/false,两个结果之间以一个英文空格符分隔。
2:输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。
3:输入三个点坐标,输出是钝角、直角还是锐角三角形,依次输出三个判断结果(true/false),以一个英文空格分隔,
4:输入五个点坐标,输出前两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积。若直线与三角形一条线重合,输出"The point is on the edge of the triangle"
5:输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof the triangle)。
必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外。若点在三角形的某条边上,输出"on the triangle"

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

输出格式:
基本输出格式见每种选项的描述。

题目分析:
本题在7-2的基础上。由点和线之间的计算延展到三角形的计算,输入的判断可以沿用之前的分割处理,而本题的重点则是三角形的点线相关公式的正确运用,根据题目要求,选择合适的式子进行计算。

源码展示:

import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
    public static void main(String[] args) {
        String str, s2;
        String[] s1, s3, num;
        double x1,x2,x3,x4,x5,y1,y2,y3,y4,y5,x,y,l1,l2,l3,l,s,xx1,xx2,yy1,yy2,a1,b1,c1,a2,b2,c2,a3,b3,c3,a4,b4,c4,ar1,ar2,ar3,ar4;
        int t=1,k=0,r;
        double[] point=new double[6];
        for(r=0;r<4;r++)
            point[r]=-1;
        char a, b, c;
        Scanner input = new Scanner(System.in);
        str = input.nextLine();
        a = str.charAt(0);
        b = str.charAt(1);
        c = str.charAt(str.length() - 1);
        if (b != ':')
            System.out.print("Wrong Format");
        else if (a < '1' || a > '5')
            System.out.print("Wrong Format");
        else if (c < '0' || c > '9')
            System.out.print("Wrong Format");
        else {
            s1 = str.split(":");
            if (s1.length != 2) {
                System.out.print("Wrong Format");
                return;
            }
            s2 = s1[1];
            s3 = s2.split(" ");
            for (String i : s3) {
                num = i.split(",");
                if(num.length!=2)
                {
                    System.out.print("Wrong Format");
                    return;
                }
                for (String j : num) {
                    if (!j.matches("[+-]?(0|[1-9][0-9]*(\\.\\d+)?|(0\\.\\d+)?)")) {
                        System.out.print("Wrong Format");
                        return;
                    }
                }
            }
            if (s3.length != 3 && ( a == '1'||a == '2' || a == '3')) {
                System.out.print("wrong number of points");
                return;
            } else if (s3.length != 4 && a == '5') {
                System.out.print("wrong number of points");
                return;
            } else if (s3.length != 5 && a == '4') {
                System.out.print("wrong number of points");
                return;
            }
            num = s3[0].split(",");
            x1 = Double.valueOf(num[0]);
            y1 = Double.valueOf(num[1]);
            num = s3[1].split(",");
            x2 = Double.valueOf(num[0]);
            y2 = Double.valueOf(num[1]);
            num = s3[2].split(",");
            x3 = Double.valueOf(num[0]);
            y3 = Double.valueOf(num[1]);
            l1=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
            l2=Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2));
            l3=Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2));
            if (a == '1')
            {

                if(l1==l2||l1==l3||l2==l3)
                {
                    if(l1==l2&&l1==l3)
                        System.out.print("true true");
                    else
                        System.out.print("true false");
                }
                else
                    System.out.print("false false");
            }
            else if (a == '2')
            {
                l=l1+l2+l3;
                String format1 = new DecimalFormat("#.######").format(l);
                String str1=String.valueOf(l);
                if(str1.length()>8)
                    System.out.print(format1);
                else
                    System.out.print(l);
                s=0.5*l1*l2*Math.sqrt(1-Math.pow((l1*l1+l2*l2-l3*l3)/(2*l1*l2),2));
                String format2 = new DecimalFormat("#.######").format(s);
                String str2=String.valueOf(s);
                if(str2.length()>8)
                    System.out.print(" "+format2);
                else
                    System.out.print(" "+s);
                x=(x1+x2+x3)/3;
                String format3 = new DecimalFormat("#.######").format(x);
                String str3=String.valueOf(x);
                if(str3.length()>8)
                    System.out.print(" "+format3);
                else
                    System.out.print(" "+x);
                y=(y1+y2+y3)/3;
                String format4 = new DecimalFormat("#.######").format(y);
                String str4=String.valueOf(y);
                if(str4.length()>8)
                    System.out.print(","+format4);
                else
                    System.out.print(","+y);
            }
            else if (a == '3')
            {
                xx1=x2-x1;xx2=x3-x1;yy1=y2-y1;yy2=y3-y1;
                if(xx1*xx2+yy1*yy2<0&&t==1)
                    t=3;
                else if(xx1*xx2+yy1*yy2==0&&t==1)
                    t=2;
                xx1=x1-x2;xx2=x3-x2;yy1=y1-y2;yy2=y3-y2;
                if(xx1*xx2+yy1*yy2<0&&t==1)
                    t=3;
                else if(xx1*xx2+yy1*yy2==0&&t==1)
                    t=2;
                xx1=x1-x3;xx2=x2-x3;yy1=y1-y3;yy2=y2-y3;
                if(xx1*xx2+yy1*yy2<0&&t==1)
                    t=3;
                else if(xx1*xx2+yy1*yy2==0&&t==1)
                    t=2;
                if(t==1)
                    System.out.print("false false true");
                else if(t==2)
                    System.out.print("false true false");
                else if(t==3)
                    System.out.print("true false false");
            }
            else if (a == '4')
            {
                if(x1==x2&&y1==y2)
                {
                    System.out.print("points coincide");
                    return;
                }
                num = s3[3].split(",");
                x4 = Double.valueOf(num[0]);
                y4 = Double.valueOf(num[1]);
                num = s3[4].split(",");
                x5 = Double.valueOf(num[0]);
                y5 = Double.valueOf(num[1]);
                xx1=x4-x3;xx2=x5-x3;yy1=y4-y3;yy2=y5-y3;
                if(xx1*yy2==xx2*yy1)
                {
                    System.out.print("data error");
                    return;
                }
                a1=y2-y1;b1=x1-x2;c1=x2*y1-x1*y2;
                a2=y4-y3;b2=x3-x4;c2=x4*y3-x3*y4;
                a3=y5-y3;b3=x3-x5;c3=x5*y3-x3*y5;
                a4=y5-y4;b4=x4-x5;c4=x5*y4-x4*y5;
                if(a1*x3+b1*y3+c1==0&&a1*x4+b1*y4+c1==0)
                {
                    System.out.print("The point is on the edge of the triangle");
                    return;
                }
                else if(a1*x3+b1*y3+c1==0&&a1*x5+b1*y5+c1==0)
                {
                    System.out.print("The point is on the edge of the triangle");
                    return;
                }
                else if(a1*x5+b1*y5+c1==0&&a1*x4+b1*y4+c1==0)
                {
                    System.out.print("The point is on the edge of the triangle");
                    return;
                }
                if(a1*b2-a2*b1!=0)
                {
                    x=(b2*c1-b1*c2)/(a2*b1-a1*b2);
                    y=(a1*c2-a2*c1)/(a2*b1-a1*b2);
                    if((x-x3)*(x-x4)<=0&&(y-y3)*(y-y4)<=0) {
                        if(k==0)
                        {
                            point[0]=x;point[1]=y;point[2]=3;k++;
                        }
                        else if((point[0]!=x||point[1]!=y)&&k==1)
                        {
                            point[3]=x;point[4]=y;point[5]=3;k++;
                        }
                    }
                }
                if(a1*b3-a3*b1!=0)
                {
                    x=(b3*c1-b1*c3)/(a3*b1-a1*b3);
                    y=(a1*c3-a3*c1)/(a3*b1-a1*b3);
                    if((x-x3)*(x-x5)<=0&&(y-y3)*(y-y5)<=0) {
                        if(k==0)
                        {
                            point[0]=x;point[1]=y;point[2]=4;k++;
                        }
                        else if((point[0]!=x||point[1]!=y)&&k==1)
                        {
                            point[3]=x;point[4]=y;point[5]=4;k++;
                        }
                    }
                }
                if(a1*b4-a4*b1!=0)
                {
                    x=(b4*c1-b1*c4)/(a4*b1-a1*b4);
                    y=(a1*c4-a4*c1)/(a4*b1-a1*b4);
                    if((x-x4)*(x-x5)<=0&&(y-y4)*(y-y5)<=0) {
                        if(k==0)
                        {
                            point[0]=x;point[1]=y;point[2]=5;k++;
                        }
                        else if((point[0]!=x||point[1]!=y)&&k==1)
                        {
                            point[3]=x;point[4]=y;point[5]=5;k++;
                        }
                    }
                }
                System.out.print(k);
                if(k==2)
                {
                    if(a1*x3+b1*y3+c1==0)
                    {
                        ar1=getarea(point[0],point[1],point[3],point[4],x4,y4);
                        String f1 = new DecimalFormat("#.######").format(ar1);
                        String str1=String.valueOf(ar1);
                        ar2=getarea(x5,y5,x3,y3,x4,y4)-getarea(point[0],point[1],point[3],point[4],x4,y4);
                        String f2 = new DecimalFormat("#.######").format(ar2);
                        String str2=String.valueOf(ar2);
                        if(ar1<=ar2)
                        {
                            if(str1.length()>8)
                                System.out.print(" "+f1);
                            else
                                System.out.print(" "+str1);
                            if(str2.length()>8)
                                System.out.print(" "+f2);
                            else
                                System.out.print(" "+str2);
                        }
                        else
                        {
                            if(str2.length()>8)
                                System.out.print(" "+f2);
                            else
                                System.out.print(" "+str2);
                            if(str1.length()>8)
                                System.out.print(" "+f1);
                            else
                                System.out.print(" "+str1);
                        }
                    }
                    else if(a1*x4+b1*y4+c1==0)
                    {
                        ar1=getarea(point[0],point[1],point[3],point[4],x5,y5);
                        String f1 = new DecimalFormat("#.######").format(ar1);
                        String str1=String.valueOf(ar1);
                        ar2=getarea(x5,y5,x3,y3,x4,y4)-getarea(point[0],point[1],point[3],point[4],x5,y5);
                        String f2 = new DecimalFormat("#.######").format(ar2);
                        String str2=String.valueOf(ar2);
                        if(ar1<=ar2)
                        {
                            if(str1.length()>8)
                                System.out.print(" "+f1);
                            else
                                System.out.print(" "+str1);
                            if(str2.length()>8)
                                System.out.print(" "+f2);
                            else
                                System.out.print(" "+str2);
                        }
                        else
                        {
                            if(str2.length()>8)
                                System.out.print(" "+f2);
                            else
                                System.out.print(" "+str2);
                            if(str1.length()>8)
                                System.out.print(" "+f1);
                            else
                                System.out.print(" "+str1);
                        }
                    }
                    else if(a1*x5+b1*y5+c1==0)
                    {
                        ar1=getarea(point[0],point[1],point[3],point[4],x3,y3);
                        String f1 = new DecimalFormat("#.######").format(ar1);
                        String str1=String.valueOf(ar1);
                        ar2=getarea(x5,y5,x3,y3,x4,y4)-getarea(point[0],point[1],point[3],point[4],x3,y3);
                        String f2 = new DecimalFormat("#.######").format(ar2);
                        String str2=String.valueOf(ar2);
                        if(ar1<=ar2)
                        {
                            if(str1.length()>8)
                                System.out.print(" "+f1);
                            else
                                System.out.print(" "+str1);
                            if(str2.length()>8)
                                System.out.print(" "+f2);
                            else
                                System.out.print(" "+str2);
                        }
                        else
                        {
                            if(str2.length()>8)
                                System.out.print(" "+f2);
                            else
                                System.out.print(" "+str2);
                            if(str1.length()>8)
                                System.out.print(" "+f1);
                            else
                                System.out.print(" "+str1);
                        }
                    }
                    else
                    {
                        if((point[2]==3&&point[5]==4)||(point[2]==4&&point[5]==3))
                        {
                            ar1=getarea(point[0],point[1],point[3],point[4],x3,y3);
                            String f1 = new DecimalFormat("#.######").format(ar1);
                            String str1=String.valueOf(ar1);
                            ar2=getarea(x5,y5,x3,y3,x4,y4)-getarea(point[0],point[1],point[3],point[4],x3,y3);
                            String f2 = new DecimalFormat("#.######").format(ar2);
                            String str2=String.valueOf(ar2);
                            if(ar1<=ar2)
                            {
                                if(str1.length()>8)
                                    System.out.print(" "+f1);
                                else
                                    System.out.print(" "+str1);
                                if(str2.length()>8)
                                    System.out.print(" "+f2);
                                else
                                    System.out.print(" "+str2);
                            }
                            else
                            {
                                if(str2.length()>8)
                                    System.out.print(" "+f2);
                                else
                                    System.out.print(" "+str2);
                                if(str1.length()>8)
                                    System.out.print(" "+f1);
                                else
                                    System.out.print(" "+str1);
                            }
                        }
                        else if((point[2]==3&&point[5]==5)||(point[2]==5&&point[5]==3))
                        {
                            ar1=getarea(point[0],point[1],point[3],point[4],x4,y4);
                            String f1 = new DecimalFormat("#.######").format(ar1);
                            String str1=String.valueOf(ar1);
                            ar2=getarea(x5,y5,x3,y3,x4,y4)-getarea(point[0],point[1],point[3],point[4],x4,y4);
                            String f2 = new DecimalFormat("#.######").format(ar2);
                            String str2=String.valueOf(ar2);
                            if(ar1<=ar2)
                            {
                                if(str1.length()>8)
                                    System.out.print(" "+f1);
                                else
                                    System.out.print(" "+str1);
                                if(str2.length()>8)
                                    System.out.print(" "+f2);
                                else
                                    System.out.print(" "+str2);
                            }
                            else
                            {
                                if(str2.length()>8)
                                    System.out.print(" "+f2);
                                else
                                    System.out.print(" "+str2);
                                if(str1.length()>8)
                                    System.out.print(" "+f1);
                                else
                                    System.out.print(" "+str1);
                            }
                        }
                        else if((point[2]==4&&point[5]==5)||(point[2]==5&&point[5]==4))
                        {
                            ar1=getarea(point[0],point[1],point[3],point[4],x5,y5);
                            String f1 = new DecimalFormat("#.######").format(ar1);
                            String str1=String.valueOf(ar1);
                            ar2=getarea(x5,y5,x3,y3,x4,y4)-getarea(point[0],point[1],point[3],point[4],x5,y5);
                            String f2 = new DecimalFormat("#.######").format(ar2);
                            String str2=String.valueOf(ar2);
                            if(ar1<=ar2)
                            {
                                if(str1.length()>8)
                                    System.out.print(" "+f1);
                                else
                                    System.out.print(" "+str1);
                                if(str2.length()>8)
                                    System.out.print(" "+f2);
                                else
                                    System.out.print(" "+str2);
                            }
                            else
                            {
                                if(str2.length()>8)
                                    System.out.print(" "+f2);
                                else
                                    System.out.print(" "+str2);
                                if(str1.length()>8)
                                    System.out.print(" "+f1);
                                else
                                    System.out.print(" "+str1);
                            }
                        }
                    }
                }
            }
            else if (a == '5')
            {
                num = s3[0].split(",");
                x1 = Double.valueOf(num[0]);
                y1 = Double.valueOf(num[1]);
                num = s3[1].split(",");
                x2 = Double.valueOf(num[0]);
                y2 = Double.valueOf(num[1]);
                num = s3[2].split(",");
                x3 = Double.valueOf(num[0]);
                y3 = Double.valueOf(num[1]);
                num = s3[3].split(",");
                x4 = Double.valueOf(num[0]);
                y4 = Double.valueOf(num[1]);
                a2 = y3-y2; b2 = x2-x3; c2 = x3*y2-x2*y3;
                a3 = y4-y2; b3 = x2-x4; c3 = x4*y2-x2*y4;
                a4 = y4-y3; b4 = x3-x4; c4 = x4*y3-x3*y4;
                if((a2*x4+b2*y4+c2==0)||(a3*x3+b3*y3+c3==0)||(a4*x2+b4*y2+c4==0))
                {
                    System.out.print("data error");
                    return;
                }
                if(a2*x1+b2*y1+c2==0)
                {
                    if((x1-x2)*(x1-x3)<=0&&(y1-y2)*(y1-y3)<=0)
                    {
                        System.out.print("on the triangle");
                        return;
                    }
                }
                else if(a3*x1+b3*y1+c3==0)
                {
                    if((x1-x2)*(x1-x4)<=0&&(y1-y2)*(y1-y4)<=0)
                    {
                        System.out.print("on the triangle");
                        return;
                    }
                }
                else if(a4*x1+b4*y1+c4==0)
                {
                    if((x1-x3)*(x1-x4)<=0&&(y1-y3)*(y1-y4)<=0)
                    {
                        System.out.print("on the triangle");
                        return;
                    }
                }
                ar1=getarea(x1,y1,x2,y2,x3,y3);
                ar2=getarea(x1,y1,x2,y2,x4,y4);
                ar3=getarea(x1,y1,x4,y4,x3,y3);
                ar4=getarea(x4,y4,x2,y2,x3,y3);
                if(Math.abs(ar4-ar3-ar2-ar1)<0.0001)
                    System.out.print("in the triangle");
                else
                    System.out.print("outof the triangle");
            }
        }
    }
    public static double getarea(double x1,double y1,double x2,double y2,double x3,double y3)
    {
        double l1,l2,l3,s;
        l1=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
        l2=Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2));
        l3=Math.sqrt(Math.pow(x3-x2,2)+Math.pow(y3-y2,2));
        s=0.5*l1*l2*Math.sqrt(1-Math.pow((l1*l1+l2*l2-l3*l3)/(2*l1*l2),2));
        String format1 = new DecimalFormat("#.######").format(s);
        double ans=Double.valueOf(format1);
        return ans;
    }
}

类图:
img

SourceMonitor生成的报表内容:
img
img

本题心得:
由上图可知,本题的代码编程过于“面向过程”,都放进了主函数运行而缺少分类规划,大量的代码粘贴重复,以及过多的if-else判断,导致代码的可读性和复用性很差,有待改进。另外关于计算方面,由于计算精度的问题导致以斜率为主的计算方式并不适合本题,而AX+BY+C=0这一般式方程计算则更加方便。

3.采坑心得:

① 串口字符解析

在本次题目中,由于并未完全理解“奇偶校验”的意思,以为奇偶校验只是用字符‘1’确定,导致很多测试点过不去。经过学习查证后,才理解意思是“根据被传输的一组二进制代码的数位中‘1’的个数是奇数或偶数来进行校验”。对个数进行判断断后,问题便迎刃而解了。

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

本题的重点其实是正则表达式的利用,需要考虑的输入错误格式的各个方面
例如

(1) ++2,-2.3 0.9,-3.2
(2) +2,,-2.3 0.9,-3.2
(3) +2 -2.3,0.9 -3.2
(4) +2 -2.3 0.9 -3.2
(5) 2.0000,-2.3.00 0.9000,-3.200
...

其中第五项错误格式点让我想了许久,等我想到后,终于也是成功解决了过不去的测试点。

③ 点线形系列2-线的计算

本题的输入格式判断在上题的基础上,又延伸出了坐标点的个数判断,以及多了一些错误格式,例如整数部分前不可出现多余的0,例如0010、002.33等。另外,最值得注意的一点,便是此题的计算精度问题,如果单纯的通过斜率计算,是过不去测试点的,必须通过AX+BY+C=0这一般式方程计算,才能够成功避免出现这一问题。
以下是一般式方程关于线的相关公式,方便同学们计算点与线的相关问题:

a1=y2-y1;
a2=y4-y3;
b1=x1-x2;
b2=x3-x4;
c1=x2*y1-x1*y2;
c2=x4*y3-x3*y4;a1*b2-a2*b1=0,则两线平行不相交,否则相交;
若以(x,y)表示交点坐标,则
x=(b2*c1-b1*c2)/(a2*b1-a1*b2);
y=(a1*c2-a2*c1)/(a2*b1-a1*b2);
④ 点线形系列3-三角形的计算

本题是题目集中最复杂也是最难过测试点的一道题,输入的判断沿用7-2的方式即可,两者的错误格式判定并无差异,但是相比之间,此题从单纯的点和线延伸到了三角形的计算,以及多出了输入格式的判断——保留六位小数,且要求四舍五入,若未超过6位小数,则保留原始位数。
本题对于交点的数量判断以及精度判断要求非常严格。
关于交点的判断,我使用了point数组变量来帮助自己判断交点在哪条边或在哪个顶点上,并用变量k限制了交点的个数,具体如下:

if((x-x3)*(x-x4)<=0&&(y-y3)*(y-y4)<=0)
{
    if(k==0)
    {
        point[0]=x;point[1]=y;point[2]=3;k++;
    }
    else if((point[0]!=x||point[1]!=y)&&k==1)
    {
        point[3]=x;point[4]=y;point[5]=3;k++;
    }
}

关于四舍五入的输出,我利用了因此我用到了DecimalFormat类来帮助自己四舍五入,先将计算结果四舍五入转为字符串,然后再强转为double类型防止出现多余的0,具体如下:

s=0.5*l1*l2*Math.sqrt(1-Math.pow((l1*l1+l2*l2-l3*l3)/(2*l1*l2),2));
String format1 = new DecimalFormat("#.######").format(s);
double ans=Double.valueOf(format1);

另外有一点是输出在三角形外的时候,是“outof the triangle”,但在原题上,确是“outof triangle”,属实是坑了我一下,让我硬是过不去一个测试点,好在后面检查发现了问题。

4.改进建议:

因为自己将代码全都归类到main这一主函数中去,导致代码的可读性和复用性较差,所以需要很好地深入地去学习如何“面向对象”,分类编程,避免这种“面向过程”的发生。
同时对一些判断性代码可能有更好的函数来帮助自己,但是自己太过一板一眼,浪费了太多的时间和空间去写或者复制无用的代码,这点也需要很好的改进。
最后便是自己的编程习惯,没有很好的注释或者是给变量取名,导致自己的代码别人很难看懂以及理解,以后需要注意这一点。

5.总结:

总体来说,这次的作业质量是比较差劲的,虽然很好的通过了全部的测试点,拿到了满分,但是自己并不是很好熟练了如何掌握运用java来面向对象,反而还是保留了以前的惯性思路,这点以后需要不断改善。还有一些数学知识的运用也十分重要,可能某一个数学式子的正确运用,就能帮助你省略很多步骤,避免很多错误点。不断地调试,也是本次作业中重要的一环,没有十次上百次的调试,你就无法发现自身代码的漏洞,因此要学会不断地DEBUG。
那么关于课程和作业,我也希望了老师能够有实操性的去讲解题目,还有一些函数、算法之类能够在课上学到,因为几次的作业解题方式都是老师课上没有教过的,都是书上后面的内容,这点对于我们来说不是很适应。
总之,下次希望自己能够逐渐改善自己的不足,争取做到满意吧!

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