博客总结作业二

前言:

本次总结包括pta题目集4、5、6,期中考试,以及链表作业。其中pta题目集4、5,是关于图形类设计的题目,其中包括点线面,三角形,以及四边形的设计。

pta题目集4:这次作业难度较大,虽然三道题的题量并不多,但是写起来可一点都不轻松。其中第一题就涉及了对于字符串格式正确与否的判断,以及字符串分割。第二题就困扰了我很久,第二题涉及到了线之间的关系,我们需要设计方法进行计算,并且题目涉及到了split函数的调用,并且这一题也包含了字符串格式判断。第三题是最难的,我到时间截止也没有做到满分,不光要设计方法,还要调用round函数、split函数等。

pta题目集5:这次作业难度比前一次下降了一些,前四道题都是有关于正则表达的简单题目,第五道与正则没有关系,而是考验了我们类与类之间的调用以及耦合。我们需要考虑清楚各个类之间的关系,才能完成题目。

pta题目集6:这次作业对于我而言难度太大,我只能把1、3题完成,第二道题涉及到四边形的计算,前三个我还可以完成,但是4、5的实现过程过于复杂,不光涉及到各种函数的调用,还涉及了数学方面的知识,以及我们的设计能力。

链表作业:链表是老师上课讲完知识点后的课堂作业,对于c语言链表没学好的我而言,还是挺难的,其中不光包括了链表的知识点,还有类与类之间的联系与调用。

期中考试:本次考试包含了三道题,并且这三道题是层层递进的,第一题是点与线,涉及到类的设计;第二题点线面问题重构,涉及到继承与多态;第三题依然是点线面问题重构,但多了容器类。

设计与分析:

题目集4:

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 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 java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        int f = 0,f1 = 0,count = 0,i,b1=0,f2 = 0,d,num = 0;
        boolean flag = false,flag1 = false,flag2 = false;
        double x1,x2,y1,y2,x3,y3,x4,y4,x5,y5,a1,a2,a3,a4,a5,a6,perimeter,area,focusx,focusy,p1,p2,p3,p4,xa,ya,xb,yb,xc,yc;
        String sc = in.nextLine();
        d = sc.length()-1;
        if(sc.charAt(0)=='1') {
            for(i=0;i<sc.length();i++) {
                if(sc.charAt(i)==',') {
                    count++;
                }
            }
            for(i=0;i<sc.length();i++) {
                if(sc.charAt(i)==' ') {
                    f2++;
                }
            }
            if(count!=3||f2!=2) {
                System.out.print("wrong number of points");
                System.exit(0);
            }
            if(count==3&&f2==2) {
                String[] p=sc.split(" ");
                String[] q=p[0].split(":");
                String[] m=q[1].split(",");
                String[] n=p[1].split(",");
                String[] s=p[2].split(",");
                
                x1=Double.parseDouble(m[0]);
                y1=Double.parseDouble(m[1]);
                x2=Double.parseDouble(n[0]);
                y2=Double.parseDouble(n[1]);
                x3=Double.parseDouble(s[0]);
                y3=Double.parseDouble(s[1]);
                
                a1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
                a2 = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
                a3 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
                
                if((a1+a2)-a3>0.0001||(a1+a3)-a2>0.0001||(a2+a3)-a1>0.0001) {
                    if((a1==a2&&a1!=a3)||(a1==a3&&a1!=a2)||(a2==a3&&a2!=a1)){
                        flag = true;
                        flag1 = false;
                    }
                    else if(a1==a2&&a1==a3) {
                        flag = true;
                        flag1 = true;
                    }
                }
                else {
                    System.out.print("data error");
                }
                System.out.print(flag+" "+flag1);
            }
        }
        if(sc.charAt(0)=='2') {
            for(i=0;i<sc.length();i++) {
                if(sc.charAt(i)==',') {
                    count++;
                }
            }
            for(i=0;i<sc.length();i++) {
                if(sc.charAt(i)==' ') {
                    f2++;
                }
            }
            if(count!=3||f2!=2) {
                System.out.print("wrong number of points");
                System.exit(0);
            }
            if(count==3&&f2==2){
                String[] p=sc.split(" ");
                String[] q=p[0].split(":");
                String[] m=q[1].split(",");
                String[] n=p[1].split(",");
                String[] s=p[2].split(",");
                
                x1=Double.parseDouble(m[0]);
                y1=Double.parseDouble(m[1]);
                x2=Double.parseDouble(n[0]);
                y2=Double.parseDouble(n[1]);
                x3=Double.parseDouble(s[0]);
                y3=Double.parseDouble(s[1]);
                
                a1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
                a2 = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
                a3 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
                
                if((a1+a2)-a3<0.0001||(a1+a3)-a2<0.0001||(a2+a3)-a1<0.0001) {
                    System.out.print("data error");
                     System.exit(0);
                }
                else {
                    perimeter = (a1+a2+a3)*1000000;
                    area = Math.abs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))/2*1000000;
                    focusx = (x1+x2+x3)*1000000/3;
                    focusy = (y1+y2+y3)*1000000/3;
                    p1 = Math.round(perimeter);
                    p2 = Math.round(area);
                    p3 = Math.round(focusx);
                    p4 = Math.round(focusy);
                    double c1 = p1/1000000;
                    double c2 = p2/1000000;
                    double c3 = p3/1000000;
                    double c4 = p4/1000000;
                    System.out.print(c1+" "+c2+" "+c3+","+c4);
                }
            }
        }
        if(sc.charAt(0)=='3') {
            for (i=0;i<sc.length()-1;i++) {
            if ((sc.charAt(i)=='+'&&sc.charAt(i+1)=='+')||(sc.charAt(i)=='-'&&sc.charAt(i+1)=='-')
                    || (sc.charAt(i)=='+'&&sc.charAt(i+1)=='-')||(sc.charAt(i)=='-'&&sc.charAt(i+1)=='+')
                    || (sc.charAt(i)=='.'&&sc.charAt(i+1)==',')||(sc.charAt(i)==','&&sc.charAt(i+1)=='.')
                    || (sc.charAt(i)==' '&&sc.charAt(i+1)=='.')||(sc.charAt(i)=='.'&&sc.charAt(i+1)=='.')
                    || (sc.charAt(i)==' '&&sc.charAt(i+1)==',')||(sc.charAt(i)==','&&sc.charAt(i+1)==' ')
                    || (sc.charAt(i)==' '&&sc.charAt(i+1)==' ')||(sc.charAt(i)==':'&&sc.charAt(i+1)==':')
                    || (sc.charAt(i)==','&&sc.charAt(i+1)==',')||(sc.charAt(sc.length()-1)==',')
                    || (sc.charAt(i)==':'&&sc.charAt(i+1)==',')||sc.charAt(0)<'1'||sc.charAt(0)>'5'
                     || (sc.charAt(i)=='0'&&(sc.charAt(i+1) > '0'&&sc.charAt(i + 1) < '9'))
                    || (sc.charAt(i)==':'&&sc.charAt(i+1) == '.')) {
                b1=1;
            }
        }
            if(b1==1) {
            System.out.print("Wrong Format");
            System.exit(0);
        }
            for(i=0;i<sc.length();i++) {
                if(sc.charAt(i)==',') {
                    count++;
                }
            }
            for(i=0;i<sc.length();i++) {
                if(sc.charAt(i)==' ') {
                    f2++;
                }
            }
            if(count!=3||f2!=2) {
                System.out.print("wrong number of points");
                System.exit(0);
            }
            if(count==3&&f2==2) {
                String[] p=sc.split(" ");
                String[] q=p[0].split(":");
                String[] m=q[1].split(",");
                String[] n=p[1].split(",");
                String[] s=p[2].split(",");
                
                x1=Double.parseDouble(m[0]);
                y1=Double.parseDouble(m[1]);
                x2=Double.parseDouble(n[0]);
                y2=Double.parseDouble(n[1]);
                x3=Double.parseDouble(s[0]);
                y3=Double.parseDouble(s[1]);
                
                a1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
                a2 = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
                a3 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
                
                if((a1+a2)-a3<0.0001||(a1+a3)-a2<0.0001||(a2+a3)-a1<0.0001) {
                    System.out.print("data error");
                     System.exit(0);
                }
                else {
                    if(Math.abs(a1*a1+a2*a2-a3*a3)<0.001||Math.abs(a1*a1+a3*a3-a2*a2)<0.001||Math.abs(a3*a3+a2*a2-a1*a1)<0.001) {
                        flag = false;
                        flag1 = true;
                        flag2 = false;
                    }
                    else if(a3*a3-(a1*a1+a2*a2)>-0.001||a2*a2-(a1*a1+a3*a3)>-0.001||a1*a1-(a3*a3+a2*a2)>-0.001) {
                        flag = true;
                        flag1 = false;
                        flag2 = false;
                    }
                    else {
                        flag = false;
                        flag1 = false;
                        flag2 = true;
                    }
                    System.out.print(flag+" "+flag1+" "+flag2);
                }
            }
        }
        if(sc.charAt(0)=='4') {
            for (i=0;i<sc.length()-1;i++) {
            if ((sc.charAt(i)=='+'&&sc.charAt(i+1)=='+')||(sc.charAt(i)=='-'&&sc.charAt(i+1)=='-')
                    || (sc.charAt(i)=='+'&&sc.charAt(i+1)=='-')||(sc.charAt(i)=='-'&&sc.charAt(i+1)=='+')
                    || (sc.charAt(i)=='.'&&sc.charAt(i+1)==',')||(sc.charAt(i)==','&&sc.charAt(i+1)=='.')
                    || (sc.charAt(i)==' '&&sc.charAt(i+1)=='.')||(sc.charAt(i)=='.'&&sc.charAt(i+1)=='.')
                    || (sc.charAt(i)==' '&&sc.charAt(i+1)==',')||(sc.charAt(i)==','&&sc.charAt(i+1)==' ')
                    || (sc.charAt(i)==' '&&sc.charAt(i+1)==' ')||(sc.charAt(i)==':'&&sc.charAt(i+1)==':')
                    || (sc.charAt(i)==','&&sc.charAt(i+1)==',')||(sc.charAt(sc.length()-1)==',')
                    || (sc.charAt(i)==':'&&sc.charAt(i+1)==',')
                     ||sc.charAt(0)<'1'||sc.charAt(0)>'5'
                    || (sc.charAt(i)==':'&&sc.charAt(i+1) == '.')) {
                b1=1;
            }
        }
            if(b1==1) {
            System.out.print("Wrong Format");
            System.exit(0);
        }
            for(i=0;i<sc.length();i++) {
                if(sc.charAt(i)==',') {
                    count++;
                }
            }
            for(i=0;i<sc.length();i++) {
                if(sc.charAt(i)==' ') {
                    f2++;
                }
            }
            if(count!=5||f2!=4) {
                System.out.print("wrong number of points");
                System.exit(0);
            }
            if(count==5&&f2==4) {
                String[] p=sc.split(" ");
                String[] q=p[0].split(":");
                String[] m=q[1].split(",");
                String[] n=p[1].split(",");
                String[] s=p[2].split(",");
                String[] z=p[3].split(",");
                String[] o=p[4].split(",");
                
                x1=Double.parseDouble(m[0]);
                y1=Double.parseDouble(m[1]);
                x2=Double.parseDouble(n[0]);
                y2=Double.parseDouble(n[1]);
                x3=Double.parseDouble(s[0]);
                y3=Double.parseDouble(s[1]);
                x4=Double.parseDouble(z[0]);
                y4=Double.parseDouble(z[1]);
                x5=Double.parseDouble(o[0]);
                y5=Double.parseDouble(o[1]);
                
                a1 = Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
                a2 = Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5));
                a3 = Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5));
                
                if(x1==x2&&y1==y2) {
                    System.out.print("points coincide");
                     System.exit(0);
                }
                
                if((a1+a2)-a3<0.0001||(a1+a3)-a2<0.0001||(a2+a3)-a1<0.0001) {
                    System.out.print("data error");
                     System.exit(0);
                }
                else {
                    
                    double mn = (y3 - y2) / (x3 - x2);
                    double ab = y2 - (mn * x2);
                    double mn1 = (y4 - y2) / (x4 - x2);
                    double ab1 = y2 - (mn1 * x2);
                    double mn2 = (y5 - y2) / (x5 - x2);
                    double ab2 = y2 - (mn2 * x2);
                    
                    double k1 = (y2 - y1) / (x2 - x1);
                    double k2 = (y4 - y3) / (x4 - x3);
                    double k3 = (y5 - y3) / (x5 - x3);
                    double k4 = (y5 - y4) / (x5 - x4);
                    
                    if(((x1 == x3 && x1 == x2)&&(k1==k2||k1==k3))||((x1 == x4 && x1 == x2)&&(k1==k2||k1==k4))||((x1 == x5 && x1 == x2)&&(k1==k3||k1==k4))) {
                        System.out.print("The point is on the edge of the triangle");
                        System.exit(0);
                    }
                    if(((y1 == y2 && y2 == y3)&&(k1==k2||k1==k3))||((y1 == y2 && y2 == y4)&&(k1==k2||k1==k4))||((y1 == y2 && y2 == y5)&&(k1==k3||k1==k4))) {
                        System.out.print("The point is on the edge of the triangle");
                        System.exit(0);
                    }
                    if(((y1 == mn * x1 + ab)&&(k1==k2||k1==3))||((y1 == mn1 * x1 + ab1)&&(k1==k2||k1==k4))||((y1 == mn2 * x1 + ab2)&&(k1==k3||k1==k4))) {
                          System.out.print("The point is on the edge of the triangle");
                          System.exit(0);
                         }
                    
                    else {
                        double m1=Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5));
                        double m2=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
                        double m3=Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5));
                        
                        
                        
                        if((x1 == x2 && x3 == x4)||k1==k2) {
                            xa=(y3*x5*x2-y5*x3*x2-y3*x5*x1+y5*x3*x1-y1*x2*x5+y2*x1*x5+y1*x2*x3-y2*x1*x3)/(x5*y2-x5*y1-x3*y2+x3*y1-x2*y5+x2*y3+x1*y5-x1*y3);
                            ya=(-y3*x5*y2+y5*x3*y2+y3*x5*y1-y5*x3*y1+y1*x2*y5-y1*x2*y3-y2*x1*y5+y2*x1*y3)/(y5*x2-y5*x1-y3*x2+x1*y3-y2*x5+y2*x3+y1*x5-y1*x3);
                            xb=(y4*x5*x2-y5*x4*x2-y4*x5*x1+y5*x4*x1-y1*x2*x5+y2*x1*x5+y1*x2*x4-y2*x1*x4)/(x5*y2-x4*y1-x4*y2+x4*y1-x2*y5+x2*y4+x1*y5-x1*y4);
                            yb=(-y4*x5*y2+y5*x4*y2+y4*x5*y1-y5*x4*y1+y1*x2*y5-y1*x2*y4-y2*x1*y5+y2*x1*y4)/(y5*x2-y5*x1-y4*x2+x1*y4-y2*x5+y2*x4+y1*x5-y1*x4);
                            
                            a1=Math.sqrt((xa-x3)*(xa-x3)+(ya-y3)*(ya-y3));
                            a2=Math.sqrt((xa-x5)*(xa-x5)+(ya-y5)*(ya-y5));
                            a3=Math.sqrt((xb-x4)*(xb-x4)+(yb-y4)*(yb-y4));
                            a4=Math.sqrt((xb-x5)*(xb-x5)+(yb-y5)*(yb-y5));
                            
                            if(xa==xb&&ya==yb) {
                                System.out.print("1");
                                System.exit(0);
                            }
                            else{
                                if(m1-(a1+a2)<0.1) {
                                num++;
                            }
                                if(m3-(a3+a4)<0.1) {
                                num++;
                            }
                            if(num!=2) {
                                System.out.print(num);
                            }
                            else {
                                area = Math.round(Math.abs((x3*(y4-y5)+x4*(y5-y3)+x5*(y3-y4)))/2*1000000);
                                double area1=Math.round(Math.abs((xa*(yb-y5)+xb*(y5-ya)+x5*(ya-yb)))/2*1000000);
                                double area2=Math.round(area-area1);
                                if(area1>area2) {
                                    double aread=area1/1000000;
                                    double areax=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                }
                                else {
                                    double areax=area1/1000000;
                                    double aread=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                            }
                            }
                        }
                        else if((x1 == x2 && x3 == x5)||k1==k3) {
                            xa=(y3*x4*x2-y4*x3*x2-y3*x4*x1+y4*x3*x1-y1*x2*x4+y2*x1*x4+y1*x2*x3-y2*x1*x3)/(x4*y2-x4*y1-x3*y2+x3*y1-x2*y4+x2*y3+x1*y4-x1*y3);
                            ya=(-y3*x4*y2+y4*x3*y2+y3*x4*y1-y4*x3*y1+y1*x2*y4-y1*x2*y3-y2*x1*y4+y2*x1*y3)/(y4*x2-y4*x1-y3*x2+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3);
                            xb=(y4*x5*x2-y5*x4*x2-y4*x5*x1+y5*x4*x1-y1*x2*x5+y2*x1*x5+y1*x2*x4-y2*x1*x4)/(x5*y2-x4*y1-x4*y2+x4*y1-x2*y5+x2*y4+x1*y5-x1*y4);
                            yb=(-y4*x5*y2+y5*x4*y2+y4*x5*y1-y5*x4*y1+y1*x2*y5-y1*x2*y4-y2*x1*y5+y2*x1*y4)/(y5*x2-y5*x1-y4*x2+x1*y4-y2*x5+y2*x4+y1*x5-y1*x4);
                            
                            a1=Math.sqrt((xa-x3)*(xa-x3)+(ya-y3)*(ya-y3));
                            a2=Math.sqrt((xa-x4)*(xa-x4)+(ya-y4)*(ya-y4));
                            a3=Math.sqrt((xb-x4)*(xb-x4)+(yb-y4)*(yb-y4));
                            a4=Math.sqrt((xb-x5)*(xb-x5)+(yb-y5)*(yb-y5));
                            
                            if(xa==xb&&ya==yb) {
                                System.out.print("1");
                                System.exit(0);
                            }
                            else {
                            if(Math.abs(m2-(a1+a2))<0.1) {
                                num++;
                            }
                            if(Math.abs(m3-(a3+a4))<0.2) {
                                num++;
                            }
                            if(num!=2) {
                                System.out.print(num);
                            }
                            else {
                                area = Math.round(Math.abs((x3*(y4-y5)+x4*(y5-y3)+x5*(y3-y4)))/2*1000000);
                                double area1=Math.round(Math.abs((xa*(y4-yb)+x4*(yb-ya)+xb*(ya-y4)))*1000000);
                                double area2=Math.round(area-area1);
                                if(area1>area2) {
                                    double aread=area1/1000000;
                                    double areax=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                }
                                else {
                                    double areax=area1/1000000;
                                    double aread=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                }
                            }
                            }
                        }
                        else if((x1 == x2 && x4 == x5)||k1==k4) {
                            xa=(y3*x4*x2-y4*x3*x2-y3*x4*x1+y4*x3*x1-y1*x2*x4+y2*x1*x4+y1*x2*x3-y2*x1*x3)/(x4*y2-x4*y1-x3*y2+x3*y1-x2*y4+x2*y3+x1*y4-x1*y3);
                            ya=(-y3*x4*y2+y4*x3*y2+y3*x4*y1-y4*x3*y1+y1*x2*y4-y1*x2*y3-y2*x1*y4+y2*x1*y3)/(y4*x2-y4*x1-y3*x2+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3);
                            xb=(y3*x5*x2-y5*x3*x2-y3*x5*x1+y5*x3*x1-y1*x2*x5+y2*x1*x5+y1*x2*x3-y2*x1*x3)/(x5*y2-x5*y1-x3*y2+x3*y1-x2*y5+x2*y3+x1*y5-x1*y3);
                            yb=(-y3*x5*y2+y5*x3*y2+y3*x5*y1-y5*x3*y1+y1*x2*y5-y1*x2*y3-y2*x1*y5+y2*x1*y3)/(y5*x2-y5*x1-y3*x2+x1*y3-y2*x5+y2*x3+y1*x5-y1*x3);
                            
                            a1=Math.sqrt((xa-x3)*(xa-x3)+(ya-y3)*(ya-y3));
                            a2=Math.sqrt((xa-x4)*(xa-x4)+(ya-y4)*(ya-y4));
                            a3=Math.sqrt((xb-x3)*(xb-x3)+(yb-y3)*(yb-y3));
                            a4=Math.sqrt((xb-x5)*(xb-x5)+(yb-y5)*(yb-y5));
                            
                            if(xa==xb&&ya==yb) {
                                System.out.print("1");
                                System.exit(0);
                            }
                            else{
                            if(m2-(a1+a2)<0.1) {
                                num++;
                            }
                            if(m1-(a3+a4)<0.1) {
                                num++;
                            }
                            if(num!=2) {
                                System.out.print(num);
                                System.exit(0);
                            }
                            else {
                                area = Math.round(Math.abs((x3*(y4-y5)+x4*(y5-y3)+x5*(y3-y4)))/2*1000000);
                                double area1=Math.round(Math.abs((x3*(ya-yb)+xa*(yb-y3)+xb*(y3-ya)))/2*1000000);
                                double area2=Math.round(area-area1);
                                if(area1>area2) {
                                    double aread=area1/1000000;
                                    double areax=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                                else {
                                    double areax=area1/1000000;
                                    double aread=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                            }
                        }
                    }
                        else {
                            xa=(y3*x4*x2-y4*x3*x2-y3*x4*x1+y4*x3*x1-y1*x2*x4+y2*x1*x4+y1*x2*x3-y2*x1*x3)/(x4*y2-x4*y1-x3*y2+x3*y1-x2*y4+x2*y3+x1*y4-x1*y3);
                            ya=(-y3*x4*y2+y4*x3*y2+y3*x4*y1-y4*x3*y1+y1*x2*y4-y1*x2*y3-y2*x1*y4+y2*x1*y3)/(y4*x2-y4*x1-y3*x2+x1*y3-y2*x4+y2*x3+y1*x4-y1*x3);
                            xb=(y3*x5*x2-y5*x3*x2-y3*x5*x1+y5*x3*x1-y1*x2*x5+y2*x1*x5+y1*x2*x3-y2*x1*x3)/(x5*y2-x5*y1-x3*y2+x3*y1-x2*y5+x2*y3+x1*y5-x1*y3);
                            yb=(-y3*x5*y2+y5*x3*y2+y3*x5*y1-y5*x3*y1+y1*x2*y5-y1*x2*y3-y2*x1*y5+y2*x1*y3)/(y5*x2-y5*x1-y3*x2+x1*y3-y2*x5+y2*x3+y1*x5-y1*x3);
                            xc=(y4*x5*x2-y5*x4*x2-y4*x5*x1+y5*x4*x1-y1*x2*x5+y2*x1*x5+y1*x2*x4-y2*x1*x4)/(x5*y2-x5*y1-x4*y2+x4*y1-x2*y5+x2*y4+x1*y5-x1*y4);
                            yc=(-y4*x5*y2+y5*x4*y2+y4*x5*y1-y5*x4*y1+y1*x2*y5-y1*x2*y4-y2*x1*y5+y2*x1*y4)/(y5*x2-y5*x1-y4*x2+x1*y4-y2*x5+y2*x4+y1*x5-y1*x4);
                            
                            a1=Math.sqrt((xa-x3)*(xa-x3)+(ya-y3)*(ya-y3));
                            a2=Math.sqrt((xa-x4)*(xa-x4)+(ya-y4)*(ya-y4));
                            a3=Math.sqrt((xb-x3)*(xb-x3)+(yb-y3)*(yb-y3));
                            a4=Math.sqrt((xb-x5)*(xb-x5)+(yb-y5)*(yb-y5));
                            a5=Math.sqrt((xc-x4)*(xc-x4)+(yc-y4)*(yc-y4));
                            a6=Math.sqrt((xc-x5)*(xc-x5)+(yc-y5)*(yc-y5));
                            if((xa==xb&&ya==yb)&&(Math.abs(m3-(a5+a6))<0.000001)) {
                                num=2;
                                area = Math.round(Math.abs((x3*(y4-y5)+x4*(y5-y3)+x5*(y3-y4)))/2*1000000);
                                double area1=Math.round(Math.abs((x3*(y4-yc)+x4*(yc-y3)+xc*(y3-y4)))/2*1000000);
                                double area2=Math.round(area-area1);
                                if(area1>area2) {
                                    double aread=area1/1000000;
                                    double areax=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                                else {
                                    double areax=area1/1000000;
                                    double aread=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                                
                            }
                            if((xa==xb&&ya==yb)&&(Math.abs(m3-(a5+a6))>0.0000001)) {
                                num=1;
                                System.out.print(num);
                                System.exit(0);
                            }
                            if((xa==xc&&ya==yc)&&(Math.abs(m1-(a3+a4))<0.00000001)) {
                                num=2;
                                area = Math.round(Math.abs((x3*(y4-y5)+x4*(y5-y3)+x5*(y3-y4)))/2*1000000);
                                double area1=Math.round(Math.abs((x3*(y4-yb)+x4*(yb-y3)+xb*(y3-y4)))/2*1000000);
                                double area2=Math.round(area-area1);
                                if(area1>area2) {
                                    double aread=area1/1000000;
                                    double areax=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                                else {
                                    double areax=area1/1000000;
                                    double aread=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                                
                            }
                            if((xa==xb&&ya==yb)&&(Math.abs(m1-(a3+a4))>0.01)){
                                num=1;
                                System.out.print(num);
                                System.exit(0);
                            }
                            if((xb==xc&&yb==yc)&&(Math.abs(m2-(a1+a2))<0.01)) {
                                num=2;
                                area = Math.round(Math.abs((x3*(y4-y5)+x4*(y5-y3)+x5*(y3-y4)))/2*1000000);
                                double area1=Math.round(Math.abs((x3*(y5-ya)+x5*(ya-y3)+xa*(y3-y5)))/2*1000000);
                                double area2=Math.round(area-area1);
                                if(area1>area2) {
                                    double aread=area1/1000000;
                                    double areax=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                                else {
                                    double areax=area1/1000000;
                                    double aread=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                            }
                            if((xa==xb&&ya==yb)&&(Math.abs(m2-(a1+a2))>0.01)){
                                num=1;
                                System.out.print(num);
                                System.exit(0);
                            }
                            if(((xa!=xb)&&(xb!=xc)&&(xa!=xc)&&(ya!=yb)&&(yb!=yc)&&(ya!=yc))&&((Math.abs(m2-(a1+a2))<0.01)&&(Math.abs(m1-(a3+a4))<0.01))){
                                num=2;
                                area = Math.round(-(x3*(y4-y5)+x4*(y5-y3)+x5*(y3-y4))/2*1000000);
                                double area1=Math.round(-(x3*(yb-ya)+xb*(ya-y3)+xa*(y3-yb))/2*1000000);
                                double area2=Math.round(area-area1);
                                if(area1>area2) {
                                    double aread=area1/1000000;
                                    double areax=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                                else {
                                    double areax=area1/1000000;
                                    double aread=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                            }
                            if(((xa!=xb)&&(xb!=xc)&&(xa!=xc)&&(ya!=yb)&&(yb!=yc)&&(ya!=yc))&&((Math.abs(m2-(a1+a2))<0.01)&&(Math.abs(m3-(a5+a6))<0.01))) {
                                num=2;
                                area = Math.round(Math.abs((x3*(y4-y5)+x4*(y5-y3)+x5*(y3-y4)))/2*1000000);
                                double area1=Math.round(Math.abs((xa*(y4-yc)+x4*(yc-ya)+xc*(ya-y4)))/2*1000000);
                                double area2=Math.round(area-area1);
                                if(area1>area2) {
                                    double aread=area1/1000000;
                                    double areax=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                                else {
                                    double areax=area1/1000000;
                                    double aread=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                            }
                            if(((xa!=xb)&&(xb!=xc)&&(xa!=xc)&&(ya!=yb)&&(yb!=yc)&&(ya!=yc))&&((Math.abs(m1-(a3+a4))<0.01)&&(Math.abs(m3-(a5+a6))<0.01))) {
                                num=2;
                                area = Math.round(Math.abs((x3*(y4-y5)+x4*(y5-y3)+x5*(y3-y4)))/2*1000000);
                                double area1=Math.round(Math.abs((xb*(yc-y5)+xc*(y5-yb)+x5*(yb-yc)))/2*1000000);
                                double area2=Math.round(area-area1);
                                if(area1>area2) {
                                    double aread=area1/1000000;
                                    double areax=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                                else {
                                    double areax=area1/1000000;
                                    double aread=area2/1000000;
                                    System.out.print(num+" "+areax+" "+aread);
                                    System.exit(0);
                                }
                            }
//                            if(((xa==xb&&ya==yb)||(xa==xc&&ya==yc)||(xb==xc&&yb==yc))&&((m2-(a1+a2)<0.01)||(m1-(a3+a4)<0.01)||(m3-(a5+a6)<0.01))){
//                                System.out.print("2");
//                            }
//                            else if(((xa==xb&&ya==yb)||(xa==xc&&ya==yc)||(xb==xc&&yb==yc))&&((m2-(a1+a2)>0.01)&&(m1-(a3+a4)>0.01)&&(m3-(a5+a6)>0.01))) {
//                                
//                            }
//                            if(m2-(a1+a2)<0.01) {
//                                num++;
//                            }
//                            if(m1-(a3+a4)<0.01) {
//                                num++;
//                            }
//                            if(m3-(a5+a6)<0.01) {
//                                num++;
//                            }
//                            System.out.print(num);    
                        }
                    }
                }
            }
    }
        if(sc.charAt(0)=='5') {
            for (i=0;i<sc.length()-1;i++) {
            if ((sc.charAt(i)=='+'&&sc.charAt(i+1)=='+')||(sc.charAt(i)=='-'&&sc.charAt(i+1)=='-')
                    || (sc.charAt(i)=='+'&&sc.charAt(i+1)=='-')||(sc.charAt(i)=='-'&&sc.charAt(i+1)=='+')
                    || (sc.charAt(i)=='.'&&sc.charAt(i+1)==',')||(sc.charAt(i)==','&&sc.charAt(i+1)=='.')
                    || (sc.charAt(i)==' '&&sc.charAt(i+1)=='.')||(sc.charAt(i)=='.'&&sc.charAt(i+1)=='.')
                    || (sc.charAt(i)==' '&&sc.charAt(i+1)==',')||(sc.charAt(i)==','&&sc.charAt(i+1)==' ')
                    || (sc.charAt(i)==' '&&sc.charAt(i+1)==' ')||(sc.charAt(i)==':'&&sc.charAt(i+1)==':')
                    || (sc.charAt(i)==','&&sc.charAt(i+1)==',')||(sc.charAt(sc.length()-1)==',')
                    || (sc.charAt(i)==':'&&sc.charAt(i+1)==',')||sc.charAt(0)<'1'||sc.charAt(0)>'5'
                     || (sc.charAt(i)=='0'&&(sc.charAt(i+1) > '0'&&sc.charAt(i + 1) < '9'))
                    || (sc.charAt(i)==':'&&sc.charAt(i+1) == '.')) {
                b1=1;
            }
        }
            if(b1==1) {
            System.out.print("Wrong Format");
            System.exit(0);
        }
            for(i=0;i<sc.length();i++) {
                if(sc.charAt(i)==',') {
                    count++;
                }
            }
            for(i=0;i<sc.length();i++) {
                if(sc.charAt(i)==' ') {
                    f2++;
                }
            }
            if(count!=4||f2!=3) {
                System.out.print("wrong number of points");
                System.exit(0);
            }
            if(count==4&&f2==3) {
                String[] p=sc.split(" ");
                String[] q=p[0].split(":");
                String[] m=q[1].split(",");
                String[] n=p[1].split(",");
                String[] s=p[2].split(",");
                String[] z=p[3].split(",");
                
                x1=Double.parseDouble(m[0]);
                y1=Double.parseDouble(m[1]);
                x2=Double.parseDouble(n[0]);
                y2=Double.parseDouble(n[1]);
                x3=Double.parseDouble(s[0]);
                y3=Double.parseDouble(s[1]);
                x4=Double.parseDouble(z[0]);
                y4=Double.parseDouble(z[1]);
                
                a1 = Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
                a2 = Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
                a3 = Math.sqrt((x2-x4)*(x2-x4)+(y2-y4)*(y2-y4));
                
                if((Math.abs(a1+a2)-a3)<0||(Math.abs(a1+a3)-a2)<0||(Math.abs(a2+a3)-a1)<0) {
                    System.out.print("data error");
                     System.exit(0);
                }
                else {
                    area = Math.abs((x2*(y3-y4)+x3*(y4-y2)+x4*(y2-y3)))/2;
                    double area1 = Math.abs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))/2;
                    double area2 = Math.abs((x1*(y3-y4)+x3*(y4-y1)+x4*(y1-y3)))/2;
                    double area3 = Math.abs((x1*(y2-y4)+x2*(y4-y1)+x4*(y1-y2)))/2;
                    if((Math.abs(area-(area1+area2+area3))<0.0001)&&area1>0&&area2>0&&area3>0) {
                        System.out.print("in the triangle");
                    }
                    if((area1<0.0001||area2<0.0001||area3<0.0001)&&(Math.abs(area-(area1+area2+area3))<0.0001)) {
                            System.out.print("on the triangle");
                    }
                    if(Math.abs(area-(area1+area2+area3))>0.001) {
                            System.out.print("outof the triangle");
                        }
                }
            }
        }
    }
}
复制代码

心得:这道题真的让我痛不欲生,第一次写这么多行的代码。这道题真的让我体验到了Java的难度,我也想到了后面的题目集难度会越来越大。但是对于我们的设计能力有很大帮助,虽然写的过程很痛苦,但是看到分数慢慢增加还是挺开心的。

题目集6:7-2:

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

 

复制代码
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
         Scanner in=new Scanner(System.in);
         double x1,x2,x3,x4,y1,y2,y3,y4;
         String a=in.nextLine();
         if (a.matches("[1-5]:[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?(\\s[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?){0,}\\s?")) {
             if (a.matches("[1-3]:([+-]?\\d+(\\.\\d+)?,[+-]?\\d+(\\.\\d+)?\\s?)*")) {
                 if(a.matches("[1-3]:([+-]?\\d+(\\.\\d+)?,[+-]?\\d+(\\.\\d+)?\\s?){4}")){
                 String[] p=a.split(" ");
                    String[] q=p[0].split(":");
                    String[] m=q[1].split(",");
                    String[] n=p[1].split(",");
                    String[] s=p[2].split(",");
                    String[] z=p[3].split(",");
                    
                    x1=Double.parseDouble(m[0]);
                    y1=Double.parseDouble(m[1]);
                    x2=Double.parseDouble(n[0]);
                    y2=Double.parseDouble(n[1]);
                    x3=Double.parseDouble(s[0]);
                    y3=Double.parseDouble(s[1]);
                    x4=Double.parseDouble(z[0]);
                    y4=Double.parseDouble(z[1]);
                    
                    if(a.charAt(0)=='1') {
                        if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x2==x3&&y2==y3)||(x4==x2&&y4==y2)||(x3==x4&&y3==y4)) {
                             System.out.print("points coincide");
                        }
                        else {
                            System.out.println(Quadrilateral(x1,y1,x2,y2,x3,y3,x4,y4)+" "+parallelogram(x1,y1,x2,y2,x3,y3,x4,y4));
                        }
                    }
                    else if(a.charAt(0)=='2') {
                        if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x2==x3&&y2==y3)||(x4==x2&&y4==y2)||(x3==x4&&y3==y4)) {
                             System.out.print("points coincide");
                        }
                        else {
                            System.out.println(Linxing(x1,y1,x2,y2,x3,y3,x4,y4)+" "+Rectangle(x1,y1,x2,y2,x3,y3,x4,y4)+" "+Square(x1,y1,x2,y2,x3,y3,x4,y4));
                        }
                    }
                    else if(a.charAt(0)=='3') {
                        if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x1==x4&&y1==y4)||(x2==x3&&y2==y3)||(x4==x2&&y4==y2)||(x3==x4&&y3==y4)) {
                             System.out.print("points coincide");
                        }
                        else {
                            if(!Quadrilateral(x1,y1,x2,y2,x3,y3,x4,y4)) {
                                 System.out.print("not a quadrilateral");
                            }
                            else {
                                 System.out.print(aotu(x1,y1,x2,y2,x3,y3,x4,y4)+" ");
                                 System.out.print(Perimeter(x1,y1,x2,y2,x3,y3,x4,y4)+" ");
                                 System.out.print(Area(x1,y1,x2,y2,x3,y3,x4,y4));
                            }
                        }
                    }
                }
                 else {
                     System.out.print("wrong number of points");
                 }
         }
             if(a.matches("[4]:([+-]?\\d+(\\.\\d+)?,[+-]?\\d+(\\.\\d+)?\\s?)*")){
                    if (a.matches("[4]:([+-]?\\d+(\\.\\d+)?,[+-]?\\d+(\\.\\d+)?\\s?){6}")){
                        System.out.println("not a quadrilateral or triangle");
                    }else {
                        System.out.println("wrong number of points");
                    }
                }
                if(a.matches("[5]:([+-]?\\d+(\\.\\d+)?,[+-]?\\d+(\\.\\d+)?\\s?)*")) {
                    if (!a.matches("[5]:([+-]?\\d+(\\.\\d+)?,[+-]?\\d+(\\.\\d+)?\\s?){5}")) {
                        System.out.println("wrong number of points");
                    } else {
                        System.out.println("on the quadrilateral");
                    }
                }

         }
         else {
             System.out.print("Wrong Format");
         }
    }
    
    public static double Distance(double a,double b,double c,double d) {
        return Math.sqrt((a-c)*(a-c)+(b-d)*(b-d));
    }
    
    public static boolean Parallel(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
        double k1=(y1-y2)/(x1-x2);
        double k2=(y3-y4)/(x3-x4);
        if(k1==k2||x1==x2&&x3==x4){
            return true;
        }else
            return false;
    }
    
    public static boolean Quadrilateral(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//是否为四边形
        double k1,k2,k3;
        k1=(y1-y2)/(x1-x2);
        k2=(y1-y3)/(x1-x3);
        k3=(y1-y4)/(x1-x4);
        if(k1==k2||k1==k3||k2==k3){
            return false;
        }else
            return true;
    }
    
    public static boolean parallelogram(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//判断平行四边形
        if(Quadrilateral(x1, y1, x2, y2, x3, y3, x4, y4)&&Parallel(x1,y1,x2,y2,x3,y3,x4,y4)&&Distance(x1,y1,x2,y2)==Distance(x3,y3,x4,y4)){
            return true;
        }else
            return false;
    }
    
     public static boolean Linxing(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//判断菱形
            if(parallelogram(x1,y1,x2,y2,x3,y3,x4,y4)&&Distance(x1,y1,x2,y2)==Distance(x2,y2,x3,y3)){
                return true;
            }else
                return false;
        }
     
     public static boolean Rectangle(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//判断矩形
            double k1=(y1-y2)/(x1-x2);
            double k2=(y2-y3)/(x2-x3);
            if(parallelogram(x1,y1,x2,y2,x3,y3,x4,y4)&&k1*k2==-1||x1==x2&&y2==y3){
                return true;
            }else
                return false;
        }
     
     public static boolean Square(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){//判断正方形
            if(Rectangle(x1,y1,x2,y2,x3,y3,x4,y4)&&Linxing(x1,y1,x2,y2,x3,y3,x4,y4)){
                return true;
            }else
                return false;
        }
     
     public static double judge(double x1,double y1,double x2,double y2,double x3,double y3){
            double a=Distance(x1,y1,x2,y2)+Distance(x1,y1,x3,y3)+Distance(x3,y3,x2,y2);
            double b=a/2;
            return Math.sqrt(b*(b-Distance(x1,y1,x2,y2))*(b-Distance(x1,y1,x3,y3))*(b-Distance(x3,y3,x2,y2)));
        }
     
     public static boolean aotu(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
         double a = judge(x1,y1,x2,y2,x3,y3);
         double b = judge(x2,y2,x3,y3,x4,y4);
         double c = judge(x1,y1,x3,y3,x4,y4);
         double d = judge(x1,y1,x2,y2,x4,y4);
         if((a+c)==(b+d)) {
             return true;
         }
         else {
             return false;
         }
     }
     
     public static double Perimeter(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
         double p = Distance(x1,y1,x2,y2)+Distance(x2,y2,x3,y3)+Distance(x3,y3,x4,y4)+Distance(x1,y1,x4,y4);
         double q = p*1000;
         double m = Math.round(q);
         return m/1000;
     }
     
     public static double Area(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
         if(aotu(x1,y1,x2,y2,x3,y3,x4,y4)) {
                double a = judge(x1, y1, x2, y2, x3, y3);
                double b = judge(x1, y1, x3, y3, x4, y4);
                double c = (a+b)*1000;
                double d = Math.round(c);
                return d/1000;
            }
         else {
             if(judge(x1, y1, x2, y2, x3, y3)+judge(x1, y1, x3, y3, x4, y4)>judge(x2, y2, x3, y3, x4, y4)+judge(x1, y1, x2, y2, x4, y4)){
                 double a=judge(x2, y2, x3, y3, x4, y4)+judge(x1, y1, x2, y2, x4, y4);   
                 double b=a*1000;
                 double c=Math.round(b);
                 return c/1000;
                }
             else {
                 double a=judge(x1, y1, x2, y2, x3, y3)+judge(x1, y1, x3, y3, x4, y4);
                 double b=a*1000;
                 double c=Math.round(b);
                 return c/1000;
                }
            }
         }
     
}
View Code
复制代码

心得:本题十分考验我们对于java字符串的理解以及数学知识的灵活运用,掌握了如何通过字符串来解决题目的能力。写完这题后我对于Java字符串的运用熟练了很多,并且也学到了一些规范代码的格式。并且在写题过程中我发现误差范围也是我们应该考虑到的,要不然根本过不了测试点。并且对于我们的心态也是锻炼,也很锻炼我们的设计能力。但是这道题我只得到了一半的分,我觉得这还是跟我自身问题有关,我不能自信得去完成题目,有了恐惧,自然就很难完成。

链表作业:

 

 

 

 

复制代码
public interface LinearListlnterface<E> {

public boolean isEmpty();

public int size();

public E get(int index);

public void remove(int index);

public void add(int index, E theElement);

    public void add(E  element);

public void printList();
 

}
import java.beans.ParameterDescriptor;

public class LList<E> implements LinearListlnterface<E> {

     private Node<E> head,curr,tail;

        private int size;

      @Override
      public boolean isEmpty() {
       // TODO 自动生成的方法存根
       if(head==null)
       return true;
       else return false;
       
      }

      @Override
      public int size() {
       // TODO 自动生成的方法存根
       return this.size;
      }

      @Override
      public E get(int index) {
       
       // TODO 自动生成的方法存根
          
          int count = 0;
          Node<E> i = head;
          for(;;) {
           if(index > size || index < 0)
            return null;
           if(i != null && count <= index)
           {
            count ++;
            i = i.getNext();
            if(i ==  null || count == index)
             break;
           }
          }
          return i.getO();
      }


      @Override
      public void add(int index, E theElement) {
       // TODO 自动生成的方法存根
          if(index==size) {
              add(theElement);
          }
       
          else {
              int count=0;
              Node<E> t=this.head;
              Node<E> p=null;
              while(count!=index&&t!=null) {
                  count++;
                  p=t;
                  t=t.getNext();
              }
              curr=new Node<E>();
              curr.setO(theElement);
              curr.setNext(t.getNext());
              t.setNext(curr);
              size++;
          }
      }

      @Override
      public void add(E element) {
       // TODO 自动生成的方法存根
       if(head==null) {
        head=tail=new Node<E>();
        head.setO(element);
        head.setNext(null);
        size++;
       }
       else {
        curr=new Node<E>();
        tail.setNext(curr);
        curr.setO(element);
        curr.setNext(null);
        size++;
       }
      }

      @Override
      public void printList() {
       // TODO 自动生成的方法存根
       Node<E> b=head;
       for(;;) {
        System.out.println(b.getO());
        b=b.getNext();
        if(b==null) break;
       }
      }


    @Override
    public void remove(int index) {
        // TODO 自动生成的方法存根
        int count=0;
        Node<E> t=this.head;
        Node<E> p=null;
        while(count!=index-1&&t!=null) {
            count=count+1;
            p=t;
            t=t.getNext();
        }
        if(p==null) {
            head=head.getNext();
        }
        else {
            t.setNext(p.getNext());
        }
        if(t==tail) {
            tail=p;
        }
        size--;
    }

    public String getSize() {
        // TODO 自动生成的方法存根
        return null;
    }

}
    import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner input = new Scanner(System.in);
        LList<Integer> list = new LList<Integer>();
        System.out.println("菜单");
        System.out.println("1.是否为空");
        System.out.println("2:大小");
        System.out.println("3:得到下标");
        System.out.println("4:移除下标");
        System.out.println("5:添加下标");
        System.out.println("6:尾插");
        System.out.println("7:输出链表");
        System.out.print("请输入你的选择:");
        while(true) {
            System.out.print("请输入你的选择:");
            int choice = input.nextInt();
            if(choice == 1) {
                if(list.isEmpty()) System.out.println("是空");
                else System.out.println("非空");
            }
            else if(choice == 2) {
                System.out.println("链表的大小为:" + list.getSize());
            }
            else if(choice == 3) {
                System.out.println("请输入你的下标:");
                int index = input.nextInt();
                if(list.get(index) == null) 
                    System.out.println("未找到");
                else System.out.println("链表的大小为:" + list.get(index));
            }
            else if(choice == 4) {
                System.out.println("请输入添加的下标:");
                int index = input.nextInt();
                if(list.get(index) == null) 
                    System.out.println("未找到!");
                else {
                    list.remove(index);
                    System.out.println("完成");
                }
            }
            else if(choice == 5) {
                System.out.println("请输入添加下标和值:");
                int index = input.nextInt();
                int value = input.nextInt();
                if(list.get(index) == null) 
                    System.out.println("未找到!");
                else {
                    list.add(index, value);
                    System.out.println("完成");
                }
            }
            else if(choice == 6) {
                System.out.println("请输入值:");
                int value = input.nextInt();
                list.add(value);
                System.out.println("完成");
            }
            else if(choice == 7) {
                list.printList();
            }
            else System.out.println("Wrong Format");
        }
    }
}
public class Node<E> {

      private E o;

      private Node<E> next;

     public E getO() {
      return o;
     }

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

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

     public void setNext(Node<E> next) {
      this.next = next;
     }
     
    }
View Code
复制代码

 

心得:链表这一部分真是上学期没学好,所以这个作业写的我很头疼,我在写之前还特意温习了链表的知识点。这次作业不光涉及到链表的知识点,还有类与类之间的关系以及调用,在老师讲解后,我认为我对于脸的掌握熟练了一些,但是日后还是得下功夫学习。

期中考试-7-1:

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

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

 

复制代码
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
         Scanner in=new Scanner(System.in);
        double x1,y1,x2,y2;
        String color;
        x1 = in.nextDouble();
        y1 = in.nextDouble();
        x2 = in.nextDouble();
        y2 = in.nextDouble();
        color = in.next();
        if(x1>0&&x1<=200&&y1>0&&y1<=200&&x2>0&&x2<=200&&y2>0&&y2<=200) {
            Point point1 = new Point(x1,y1);
            Point point2 = new Point(x2,y2);
            Line line = new Line(point1, point2, color);
            line.display();

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

}

class Point{
    private double x;
    private double y;
    public Point() {
        super();
        // TODO 自动生成的构造函数存根
    }
    
    public Point(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    
    public double getX() {
        return x;
    }
    
    public void setX(double x) {
        this.x = x;
    }
    
    public double getY() {
        return y;
    }
    
    public void setY(double y) {
        this.y = y;
    }
    
    public void display() {
        if(x>0&&x<=200&&y>0&&y<=200) {
            
            System.out.print("("+String.format("%.2f",x)+","+String.format("%.2f",y)+")");
            
        }
        else {
            System.out.println("Wrong Format");
        }
    }
}

class Line{
    private Point point1;
    private Point point2;
    private String color;
    
    public Line() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Line(Point point1, Point point2, String color) {
        super();
        this.point1 = point1;
        this.point2 = point2;
        this.color = color;
    }

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    
    public double getDistance(double x1,double y1,double x2,double y2) {
        double data = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        return data;
    }
    
    public void display() {
        System.out.println("The line's color is:"+this.color);
        System.out.println("The line's begin point's Coordinate is:");
        System.out.println("("+String.format("%.2f",point1.getX())+","+String.format("%.2f",point1.getY())+")");
        System.out.println("The line's end point's Coordinate is:");
        System.out.println("("+String.format("%.2f",point2.getX())+","+String.format("%.2f",point2.getY())+")");
        System.out.print("The line's length is:"+String.format("%.2f", getDistance(point1.getX(),point1.getY(),point2.getX(),point2.getY())));
    }
}
View Code
复制代码

心得:这道题是第一题,属于简单的题目,只包含了类与类的相关知识,并且类图已经给了我们,我们并不需要设计,只需要按类图写代码就行。

期中考试-7-2:

复制代码
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
         Scanner in=new Scanner(System.in);
        double x1,y1,x2,y2;
        String color;
        x1 = in.nextDouble();
        y1 = in.nextDouble();
        x2 = in.nextDouble();
        y2 = in.nextDouble();
        color = in.next();
        if(x1>0&&x1<=200&&y1>0&&y1<=200&&x2>0&&x2<=200&&y2>0&&y2<=200) {
            Point point1 = new Point(x1,y1);
            Point point2 = new Point(x2,y2);
            Line line = new Line(point1, point2, color);
            Element element;
            Plane plane = new Plane(color);
              element = point1;//起点Point
              element.display();
              
              element = point2;//终点Point
              element.display();
              
              element = line;//线段
              element.display();
              
              element = plane;//
              element.display();


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

}

class Point extends Element{
    private double x;
    private double y;
    public Point() {
        super();
        // TODO 自动生成的构造函数存根
    }
    
    public Point(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    
    public double getX() {
        return x;
    }
    
    public void setX(double x) {
        this.x = x;
    }
    
    public double getY() {
        return y;
    }
    
    public void setY(double y) {
        this.y = y;
    }
    
    public void display() {
        if(x>0&&x<=200&&y>0&&y<=200) {
            
            System.out.println("("+String.format("%.2f",x)+","+String.format("%.2f",y)+")");
            
        }
        else {
            System.out.println("Wrong Format");
        }
    }
}

class Line extends Element{
    private Point point1;
    private Point point2;
    private String color;
    
    public Line() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Line(Point point1, Point point2, String color) {
        super();
        this.point1 = point1;
        this.point2 = point2;
        this.color = color;
    }

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    
    public double getDistance(double x1,double y1,double x2,double y2) {
        double data = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        return data;
    }
    
    public void display() {
        System.out.println("The line's color is:"+this.color);
        System.out.println("The line's begin point's Coordinate is:");
        System.out.println("("+String.format("%.2f",point1.getX())+","+String.format("%.2f",point1.getY())+")");
        System.out.println("The line's end point's Coordinate is:");
        System.out.println("("+String.format("%.2f",point2.getX())+","+String.format("%.2f",point2.getY())+")");
        System.out.println("The line's length is:"+String.format("%.2f", getDistance(point1.getX(),point1.getY(),point2.getX(),point2.getY())));
    }
}

abstract class Element{

    
    public abstract void display() ;
}

class Plane extends Element{
    private String color;

    public Plane() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Plane(String color) {
        super();
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public void display() {
        // TODO 自动生成的方法存根
        System.out.println("The Plane's color is:"+this.color);
    }
    
    
    
}
View Code
复制代码

心得:第二题是在第一题的基础上迭代的,比第一题多了继承与多态,我们需要增加抽象类Element以及Plane类。我们在主函数的输出时改变输出方法,变为抽象类的输出。

期中考试-7-3:

复制代码
import java.util.ArrayList;
import java.util.Scanner;


public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
         Scanner in=new Scanner(System.in);
         int choice;
         double x1,y1,x2,y2;
         String color;
         GeometryObject a = new GeometryObject();
         Plane plane ;
         Point point;
         Line line;
         choice = in.nextInt();
         while(choice != 0) {
                    switch(choice) {
                    case 1://insert Point object into list 
                      x1=in.nextDouble();
                      y1=in.nextDouble();
                      point=new Point(x1,y1);
                      a.add(point);
                      break;
                    case 2://insert Line object into list
                      x1=in.nextDouble();
                      y1=in.nextDouble();
                      x2=in.nextDouble();
                      y2=in.nextDouble();
                      Point point1=new Point(x1,y1);
                      Point point2=new Point(x2,y2);
                      color=in.next();
                      line=new Line(point1,point2,color); 
                      a.add(line);
                      break;
                    case 3://insert Plane object into list
                        color=in.next();
                        plane = new Plane(color);
                        a.add(plane);
                        break;
                    case 4://delete index - 1 object from list
                        int index = in.nextInt();
                        a.remove(index-1);
                    }
                    choice = in.nextInt();
              }
         for(Element element : a.getList()) {
             element.display();
         }
        }
    }

class Point extends Element{
    private double x;
    private double y;
    public Point() {
        super();
        // TODO 自动生成的构造函数存根
    }
    
    public Point(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    
    public double getX() {
        return x;
    }
    
    public void setX(double x) {
        this.x = x;
    }
    
    public double getY() {
        return y;
    }
    
    public void setY(double y) {
        this.y = y;
    }
    
    public void display() {
        if(x>0&&x<=200&&y>0&&y<=200) {
            
            System.out.println("("+String.format("%.2f",x)+","+String.format("%.2f",y)+")");
            
        }
        else {
            System.out.println("Wrong Format");
        }
    }
}

class Line extends Element{
    private Point point1;
    private Point point2;
    private String color;
    
    public Line() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Line(Point point1, Point point2, String color) {
        super();
        this.point1 = point1;
        this.point2 = point2;
        this.color = color;
    }

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    
    public double getDistance(double x1,double y1,double x2,double y2) {
        double data = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        return data;
    }
    
    public void display() {
        if(point1.getX()>0&&point1.getX()<=200&&point2.getX()>0&&point2.getX()<=200&&point1.getY()>0&&point1.getY()<=200&&point2.getY()>0&&point2.getY()<=200) {
        System.out.println("The line's color is:"+this.color);
        System.out.println("The line's begin point's Coordinate is:");
        System.out.println("("+String.format("%.2f",point1.getX())+","+String.format("%.2f",point1.getY())+")");
        System.out.println("The line's end point's Coordinate is:");
        System.out.println("("+String.format("%.2f",point2.getX())+","+String.format("%.2f",point2.getY())+")");
        System.out.println("The line's length is:"+String.format("%.2f", getDistance(point1.getX(),point1.getY(),point2.getX(),point2.getY())));
    }
        else {
            System.out.println("Wrong Format");
        }
    }
}

abstract class Element{

    
    public abstract void display() ;
}

class Plane extends Element{
    private String color;

    public Plane() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Plane(String color) {
        super();
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public void display() {
        // TODO 自动生成的方法存根
        System.out.println("The Plane's color is:"+this.color);
    }
}

class GeometryObject {
    private ArrayList<Element> list= new ArrayList<>();

    public GeometryObject() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public void add(Element element) {
        list.add(element);
    }
    
    public void remove(int index) {
        if(index>=0&&index<list.size())
        list.remove(index);
    }

    public ArrayList<Element> getList() {
        return list;
    }

}
View Code
复制代码

心得:第三题在第二题的迭代的基础上增加了容器类,说实话,如果不给我类图的话,我还真不会设计,但是在有类图的情况下,我还能解决问题,我学会了容器类的使用,虽然在考试的时候对于所写的代码十分不确定,但是对于所学的知识还能做到运用。

踩坑心得:

 题目集4:

===============================================================================================================================

心得:基本格式错误用了枚举法,结果始终有一个测试点过不了,证明还是有情况没有考虑到。选项四计算错误有一个测试点过不了,调试不出来,到截止时间都没有找出,证明还是设计有问题。

题目集6:

===============================================================================================================================

心得:这道题目我只写了前三个选择,后两个的情况太复杂了,其中未过的测试点都是能否构成四边形出了问题,我自己找的判断能否构成四边形的方法在此题中并不完全适用,因为我发现此题的构成四边形的四个点是必须按顺序来的,这也导致了有几个测试点过不了。

链表课堂作业:

心得:我们并没有清楚了解增加类是头插还是尾插,并且链表的知识点我并没有很好的理解与掌握,踩了很多坑。

题目集5:

心得:这道题对于格式判断的正则表达式我写的有点为了过测试点而写,其中“|”的作用我开始搞错了,后来修改完才对。

期中考试:

心得:这题我没本来信心满满的提交,但是没想到才四分,我想到是没有输出“wrong format”,结果加上了“wrong format”直接满分,由此可见粗心大意写代码是要不得的。

心得:这道题的移除指定坐标的元素测试点出错了,我突然发现是自己没有规定index的范围,然后规定完范围后就能通过测试点了,审题不仔细这个毛病真得改改了。

 

改进建议:

这次总结包括了题目集4、5、6,链表课堂作业,以及期中考试的三道题。这几次题目难度多我来说还是很大的,有两次pta都没有拿到满分,甚至其中还有一次刚及格。对于现阶段的我来说,打好基础是当务之急,我觉得我去优化代码是有点不切实际的。我得学好类与类之间的关系,继承与多态,以及容器类。我对于这些都是一知半解,有时候能写出代码,也能运行,但是根本就不清楚怎么运行的,为什么可以运行。其次就是写代码粗心大意,好就好在eclipse可以及时报错,这对于我来说还是挺友好的。然后就是写代码之前要先设计,我就没有设计的概念,拿到题目就直接去写,根本没有设计,因此我写出的代码在画类图是是很混乱的。就说图形类设计的那几道题,我根本没有建立类,我只编写了方法,这样是不好的,因为代码需要低耦合性。还有链表的课堂作业,由于我对于链表的知识不熟练,因此写出的代码是很低级的,甚至还会有错误。

总结:

这几次的作业让我清楚认识到了自己的渺小,java的课绝对不是一门可以混过的课程,不光有pta,还有随堂作业,以及实验等,我觉得对于我而言还是很难的,不知道别人怎么样,但是我清楚认识到我的思维并不适合编程,就像老师说的,有的人的脑子,是天生适合写代码的,而我就是那种可能得学习很久才能开始写,但既然选择了这个专业,我还是得坚持下去,虽然做不到游刃有余,但是基本的东西还是得掌握的。希望这个学期的java课程可以顺利通过吧。

posted @   HanHan-30  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示