ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

题目集4_6的总结

2022-05-01 21:02:06  阅读:166  来源: 互联网

标签:总结 题目 scan double System else xy out


前言:

知识点:1、进一步对输入格式的判断,设置字符串进行输入,对于产生的字符串的切割使用的函数,string.split()对于从不需要的字符串进行切割,难度中等,涉及到每一道题目

    2、在上一点的基础上,对于每一种坐标的内容进行判断,判断是否为三点一线,两直线是否平行等内容,难度中等,其主要是对上一题的衍生,大部分是初高中的知识,需要判断的内容较多

    3、在上一题的基础上,对于产生的三角形进行综合判断,难度较大,所涉及的知识点较少

    4、正则表达式的判断,难度相对较小,对于上面几道题可以得到提升,缩短代码,其中很多的都是判断

    5、设计类,对于类与类之间的关系进行设计与构造设计,一个人存在多个账号和多个卡号的设计

    6、学会设计点与线的关系,通过输入点来构造线

    7、点线面之间的构造,通过继承和多态的联系来设计出类与类之间的关系,向上造型,方法重写,实现不同的对象做同一件事的不同效果的展示。

    8、构造容器类,需要掌握ArraryList容器类的用法,对于其中的内容进行删除与添加对象,在继承与多态的基础上再次构造

题目集:

(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(应该是要设计字符串的,不能单独设计数字,一开始我也想过用数字,但是失败了,如果你们有更好的欢                          迎评论区留言)

     本题没有采用正则表达式,如果用正则表达式会简便很多,推荐大家去学习一下,确实很方便,几个字符解决了很多的事情,真的是yyds。

     首先我们需要找到其中的间隔的位置,才能对格式进行判断

for(;i<getlength();i ++) {
            for(j = i;j<getlength();j++) {
                if(getData().charAt(j) =='+'||getData().charAt(j) == '-')
                    add++;
                if(getData().charAt(j) == '.')
                    dot++;
                if(getData().charAt(j) != '.'&&(getData().charAt(j) != '+'&&getData().charAt(j) != '-')&&!Character.isDigit(getData().charAt(j))) {
                    i = j;
                    num++;//表示找到了几组
                    break; //找到断点
                }
                if(getData().charAt(j) == '.'&&!Character.isDigit(getData().charAt(j+1)))
                    return 0;//格式错误
                if((getData().charAt(j) == '+'||getData().charAt(j) == '-')&&j != i)
                    return 0;//格式错误
                if(getData().charAt(j) == '0'&&getData().charAt(j+1) == '0'&&j == i)
                    return 0;
            }
            if(j == getlength())
                break;
            if(add > 1||dot >1)
                return 0;//格式错误
            add = 0; dot =0;
            if(num%2 == 0 &&getData().charAt(j) != ' ')
                return 0;//格式错误
            if(num%2 != 0 &&getData().charAt(j) !=',')
                return 0;//格式错误
        }
        if(j == getlength()&&num == 3)
            return 1;//表示格式正确,数据正确
        if((num-3) %2 == 0&&num !=3)
            return 2;//表示数据错误
        if(num %2 !=0 )
            return 1;//表示格式错误
        else 
            return 0;
}

     对于是字符串的类型,需要对首先的“+-”这两个符号进行判断,可能是存在或者是不存在,同时每一组只能出现一次,对于需要里面每次产生的内容都进行计数,这是非常重要的,其实这个代码还是                        可以的拓展的,将其中的内容进行稍微的更改一下,就可以实现代码的拓展,当然,这个除了使用正则表达式的以外,还可以使用其中string.split函数,这个会帮你自动切割,是非常方便的以后我们                      会用到的

踩坑新得:相对于这个代码中,找位置,一开始就有很多的点需要找,因此,需要设计好,一开始,我就没有计数,这样就导致了出现了bug,比如说没有考虑到00,++,1.这样的数字,都是一时难以考虑完全                    的,需要大家自己慢慢去设计。

改进建议:可以使用正则表达式去更改,这样会更好,推荐大家去尝试一下

 

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

输入格式:

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

 

设计思路:这一题是对上一题的衍生,其中很多的代码时直接可以复制粘贴过来使用的,不需要很多的设计,但是还是其中还有很多的需要判断三点共线的等等,推荐大家可以先布局,想好思路和算法,这样可                    以节省很多的时间,推荐大家上网查找一些公式,这里很多都是要判断,斜率是否存在,这是比较麻烦的

     以下是源码(确实写的很不好,但是可以实现):

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = "";
        char chance = 0;
        int mothod = 0;
        scanner scan = new scanner();
        scan.scan();//先输入
        chance = scan.getFirstchar();
        if(scan.getData().charAt(0) > '5' || scan.getData().charAt(0) < '1') {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        if(chance == '1') {
            mothod = scan.j();
            if(mothod == 0)
                System.out.println("Wrong Format");
            if(mothod == 1) {
                scan.get();
                if(scan.getX1() == scan.getX2()&&scan.getY1() != scan.getY2())
                    System.out.println("Slope does not exist");
                else if(scan.getX1() == scan.getX2()&&scan.getY1() == scan.getY2())
                    System.out.println("points coincide");
                else {
                    System.out.println((scan.getY1()-scan.getY2())/(scan.getX1()-scan.getX2()));
                }
            }
            if(mothod == 2)
                System.out.println("wrong number of points");
        }
    /*=======================================================*/
        else if(chance == '2') {
            mothod = scan.j();
            if(mothod == 0)
                System.out.println("Wrong Format");
            if(mothod == 1) {
                scan.get();
                if(scan.getX2() == scan.getX3()&&scan.getY2() == scan.getY3())
                    System.out.println("points coincide");
                else if(scan.getX2() == scan.getX3()&&scan.getY2() != scan.getY3()) {
                    if(scan.getX1() == scan.getX2())
                        System.out.println("0.0");
                    else if(scan.getX1() != scan.getX2())
                        System.out.println(Math.abs(scan.getX1()-scan.getX2()));
                }
                else {
                    double k1 = (scan.getY1()-scan.getY2())/(scan.getX1()-scan.getX2());
                    double k2 = (scan.getY1()-scan.getY3())/(scan.getX1()-scan.getX3());
                    if(k1 != k2) {
                        if(scan.getX2() != scan.getX3()) {
                            double k = (scan.getY2()-scan.getY3())/(scan.getX2()-scan.getX3());
                            double feizi = Math.abs(k*scan.getX1()-scan.getY1()-k*scan.getX2()+scan.getY2());
                            double feimu = Math.sqrt(k*k+1);
                            double d = feizi/feimu;
                            System.out.println(d);
                        }
                        else if(scan.getX2() == scan.getX3()) {
                            double d = Math.abs(scan.getX1()-scan.getX2());
                            System.out.println(d);
                        }
                    }
                    if(k1 == k2)
                        System.out.println("0.0");
                }    
            }
                if(mothod == 2)    
                    System.out.println("wrong number of points");            
        }
        /*==================================================*/
        
        else if(chance == '3') {
            mothod = scan.j();
            if(mothod == 0)
                System.out.println("Wrong Format");
            if(mothod == 1) {
                scan.get();
                if(scan.getX2() == scan.getX3()&&scan.getY2() == scan.getY3())
                    System.out.println("points coincide");
                else if(scan.getX2() == scan.getX3()&&scan.getX1() == scan.getX3())
                    System.out.println("true");
                else if(scan.getX2() == scan.getX3()&&scan.getX1() != scan.getX3())
                    System.out.println("false");
                else {
                    double k1 = (scan.getY1()-scan.getY2())/(scan.getX1()-scan.getX2());
                    double k2 = (scan.getY1()-scan.getY3())/(scan.getX1()-scan.getX3());
                    if(k1 == k2) 
                        System.out.println("true");
                    if(k1 != k2)
                        System.out.println("false");
                }
            }
            if(mothod == 2)    
                System.out.println("wrong number of points");    
        }
        /*================================================*/
        else if(chance == '4') {
            mothod = scan.j();
            if(mothod == 0)
                System.out.println("Wrong Format");
            if(mothod == 1) {
                scan.get();
                if(scan.getX1() == scan.getX2()&&scan.getY1() == scan.getY2())
                    System.out.println("points coincide");
                else if(scan.getX3() == scan.getX4()&&scan.getY3() == scan.getY4())
                    System.out.println("points coincide");
                else if(scan.getX1() == scan.getX2()&&scan.getY1() != scan.getY2()&&scan.getX3() == scan.getX4()&&scan.getY3() != scan.getY4())
                    System.out.println("true");
                else if(scan.getX1() == scan.getX2()&&scan.getY1() != scan.getY2()&&scan.getX3() != scan.getX4()) 
                    System.out.println("false");
                else if(scan.getX1() != scan.getX2()&&scan.getX3() == scan.getX4()&&scan.getY3() != scan.getY4()) 
                    System.out.println("false");
                else {
                    double k1 = (scan.getY1()-scan.getY2())/(scan.getX1()-scan.getX2());
                    double k2 = (scan.getY3()-scan.getY4())/(scan.getX3()-scan.getX4());
                    if(k1 == k2) 
                        System.out.println("true");
                    if(k1 != k2)
                        System.out.println("false");
                }
            }
            if(mothod == 2)    
                System.out.println("wrong number of points");    
        }
        /*====================================================*/
        else if(chance == '5') {
            mothod = scan.j();
            if(mothod == 0)
                System.out.println("Wrong Format");
            if(mothod == 1) {
                scan.get();
                if(scan.getX1() == scan.getX2()&&scan.getY1() == scan.getY2())
                    System.out.println("points coincide");
                else if(scan.getX3() == scan.getX4()&&scan.getY3() == scan.getY4())
                    System.out.println("points coincide");
                else if(scan.getX1() == scan.getX2()&&scan.getY1() != scan.getY2()&&scan.getX3() == scan.getX4()&&scan.getY3() != scan.getY4())
                    System.out.println("is parallel lines,have no intersection point");
/*一条在一条不存在*/else if(scan.getX1() == scan.getX2()&&scan.getY1() != scan.getY2()&&scan.getX3() != scan.getX4()) {
                    double k2 = (scan.getY3()-scan.getY4())/(scan.getX3()-scan.getX4());
                    double y = k2*scan.getX1()-k2*scan.getX3()+scan.getY3();
                    float x = (float) scan.getX1(); 
                    System.out.print(x+","+(float)y);
                    if((y - scan.getY1())*(y-scan.getY2()) <0||(scan.getX1() - scan.getX3())*(scan.getX1()-scan.getX4()) <0) 
                            System.out.println(" true");
                    else
                        System.out.println(" false");
                }
                else if(scan.getX1() != scan.getX2()&&scan.getX3() == scan.getX4()&&scan.getY3() != scan.getY4()) {
                    double k2 = (scan.getY1()-scan.getY2())/(scan.getX1()-scan.getX2());
                    double y = k2*scan.getX3()-k2*scan.getX1()+scan.getY1();
                    float x = (float) scan.getX3();
                    System.out.print(scan.getX3()+","+(float)y);
                    if((scan.getX3() - scan.getX1())*(scan.getX3()-scan.getX2()) <0||(y - scan.getY3())*(y-scan.getY4()) <0) 
                            System.out.println(" true");
                    else
                        System.out.println(" false");
                }    
                else {
                    double k1 = (scan.getY1()-scan.getY2())/(scan.getX1()-scan.getX2());
                    double k2 = (scan.getY3()-scan.getY4())/(scan.getX3()-scan.getX4());
                    if(k1 == k2) {
                        System.out.println("is parallel lines,have no intersection point");
                    }
                        
                    if(k1 != k2) {

                        double b1 = -k1*scan.getX1()+scan.getY1();
                        double b2 = -k2*scan.getX3()+scan.getY3();
                        double x = (b2-b1)/(k1-k2);
                        double y = k1*x-k1*scan.getX1()+scan.getY1();
                        System.out.print((float)x+","+(float)y);
                        if((x - scan.getX1())*(x-scan.getX2()) <0||(x - scan.getX3())*(x-scan.getX4()) <0) 
                                System.out.println(" true");
                        else
                            System.out.println(" false");
                    }
                }
            }
            if(mothod == 2)    
                System.out.println("wrong number of points");    
        }
    }//方法
}//类
class scanner {//输入一组字符串
    private int L = 0;
    private String data = "";
    private int pos [] = new int [100];
    private double x1;
    private double y1;
    private double x2;
    private double y2;
    private double x3;
    private double y3;
    private double x4;
    private double y4;
    public scanner() {
        super();
        // TODO 自动生成的构造函数存根
    }
    public void scan() {
        Scanner in = new Scanner(System.in);
        data = in.nextLine();
        L=data.length();
    }
    public int getlength() {
        return L;
    }
    public String getData() {
        return data;
    }
    public char getFirstchar() {
        return data.charAt(0);
    }
    public double getX1() {
        return x1;
    }
    public void setX1(double x1) {
        this.x1 = x1;
    }
    public double getY1() {
        return y1;
    }
    public void setY1(double y1) {
        this.y1 = y1;
    }
    public double getX2() {
        return x2;
    }
    public void setX2(double x2) {
        this.x2 = x2;
    }
    public double getY2() {
        return y2;
    }
    public void setY2(double y2) {
        this.y2 = y2;
    }
    public double getX3() {
        return x3;
    }
    public void setX3(double x3) {
        this.x3 = x3;
    }
    public double getY3() {
        return y3;
    }
    public void setY3(double y3) {
        this.y3 = y3;
    }
    public double getX4() {
        return x4;
    }
    public void setX4(double x4) {
        this.x4 = x4;
    }
    public double getY4() {
        return y4;
    }
    public void setY4(double y4) {
        this.y4 = y4;
    }
    public int j() {
        int j = 0,i = 2;
        int add = 0,dot = 0,num = 0;
        if(getData().charAt(0) =='1') {
            if(getData().charAt(1) != ':')
                return 0;//格式错误
            for(;i<getlength();i ++) {
                for(j = i;j<getlength();j++) {
                    if(getData().charAt(j) =='+'||getData().charAt(j) == '-')
                        add++;
                    if(getData().charAt(j) == '.')
                        dot++;
                    if(getData().charAt(j) != '.'&&(getData().charAt(j) != '+'&&getData().charAt(j) != '-')&&!Character.isDigit(getData().charAt(j))) {
                        i = j;
                        pos[num] = j;
                        num++;//表示找到了几组
                        break; //找到断点
                    }
                    if(getData().charAt(j) == '.'&&!Character.isDigit(getData().charAt(j+1)))
                        return 0;//格式错误
                    if((getData().charAt(j) == '+'||getData().charAt(j) == '-')&&j != i)
                        return 0;//格式错误
                    if(getData().charAt(j) == '0'&&j == i&&j+1<getlength()) {
                        if(Character.isDigit(getData().charAt(j+1)))
                        return 0;
                    }
                }
                if(j == getlength())
                    break;
                if(add > 1||dot >1)
                    return 0;//格式错误
                add = 0; dot =0;
                if(num%2 == 0 &&getData().charAt(j) != ' ')
                    return 0;//格式错误
                if(num%2 != 0 &&getData().charAt(j) !=',')
                    return 0;//格式错误
            }
                if(j == getlength()&&num == 3)
                    return 1;//表示格式正确,数据正确
                else if(i == getlength()&&!Character.isDigit(getData().charAt(getlength()-1)))
                    return 0;
                else if((num-3) %2 == 0&&num !=3)
                    return 2;//表示数据错误
                else if(num %2 !=0 )
                    return 0;//表示格式错误
                else 
                    return 0;
        }
        else if(getData().charAt(0) =='2'||getData().charAt(0)=='3') {
            if(getData().charAt(1) != ':')
                return 0;//格式错误
            for(;i<getlength();i ++) {
                for(j = i;j<getlength();j++) {
                    if(getData().charAt(j) =='+'||getData().charAt(j) == '-')
                        add++;
                    if(getData().charAt(j) == '.')
                        dot++;
                    if(getData().charAt(j) != '.'&&(getData().charAt(j) != '+'&&getData().charAt(j) != '-')&&!Character.isDigit(getData().charAt(j))) {
                        i = j;
                        pos[num] = j;
                        num++;//表示找到了几组
                        break; //找到断点
                    }
                    if(getData().charAt(j) == '.'&&!Character.isDigit(getData().charAt(j+1)))
                        return 0;//格式错误
                    if((getData().charAt(j) == '+'||getData().charAt(j) == '-')&&j != i)
                        return 0;//格式错误
                    if(getData().charAt(j) == '0'&&j == i&&j+1<getlength()) {
                        if(Character.isDigit(getData().charAt(j+1)))
                        return 0;
                    }
                }
                if(j == getlength())
                    break;
                if(add > 1||dot >1)
                    return 0;//格式错误
                add = 0; dot =0;
                if(num%2 == 0 &&getData().charAt(j) != ' ')
                    return 0;//格式错误
                if(num%2 != 0 &&getData().charAt(j) !=',')
                    return 0;//格式错误
            }
                if(j == getlength()&&num == 5)
                    return 1;//表示格式正确,数据正确
                else if(i == getlength()&&!Character.isDigit(getData().charAt(getlength()-1)))
                    return 0;
                else if((num-3) %2 == 0&&num !=5)
                    return 2;//表示数据错误
                else if(num %2 !=0 )
                    return 0;//表示格式错误
                else 
                    return 0;
            
            
        }
        else if(getData().charAt(0) =='4'||getData().charAt(0)=='5') {
            if(getData().charAt(1) != ':')
                return 0;//格式错误
            for(;i<getlength();i ++) {
                for(j = i;j<getlength();j++) {
                    if(getData().charAt(j) =='+'||getData().charAt(j) == '-')
                        add++;
                    if(getData().charAt(j) == '.')
                        dot++;
                    if(getData().charAt(j) != '.'&&(getData().charAt(j) != '+'&&getData().charAt(j) != '-')&&!Character.isDigit(getData().charAt(j))) {
                        i = j;
                        pos[num] = j;
                        num++;//表示找到了几组
                        break; //找到断点
                    }
                    if(getData().charAt(j) == '.'&&!Character.isDigit(getData().charAt(j+1)))
                        return 0;//格式错误
                    if((getData().charAt(j) == '+'||getData().charAt(j) == '-')&&j != i)
                        return 0;//格式错误
                    if(getData().charAt(j) == '0'&&j == i&&j+1<getlength()) {
                        if(Character.isDigit(getData().charAt(j+1)))
                        return 0;
                    }
                }
                if(j == getlength())
                    break;
                if(add > 1||dot >1)
                    return 0;//格式错误
                add = 0; dot =0;
                if(num%2 == 0 &&getData().charAt(j) != ' ')
                    return 0;//格式错误
                if(num%2 != 0 &&getData().charAt(j) !=',')
                    return 0;//格式错误
            }

                if(j == getlength()&&num == 7)
                    return 1;//表示格式正确,数据正确
                else if(i == getlength()&&!Character.isDigit(getData().charAt(getlength()-1)))
                    return 0;
                else if((num-3) %2 == 0&&num !=7)
                    return 2;//表示数据错误
                else if(num %2 !=0 )
                    return 0;//表示格式错误
                else 
                    return 0;
            
        }
        else return 0;
    }//方法的括号
    public void get() {
        String stringx1 ="";
        String stringy1 ="";
        String stringx2 ="";
        String stringy2 ="";
        String stringx3 ="";
        String stringy3 ="";
        String stringx4 ="";
        String stringy4 ="";
        if(getData().charAt(0)=='1') {
            stringx1 = getData().substring(2,pos[0]);
            stringy1 = getData().substring(pos[0]+1,pos[1]);
            stringx2 = getData().substring(pos[1]+1,pos[2]);
            stringy2 = getData().substring(pos[2]+1,getlength());
            x1 = Double.valueOf(stringx1.toString());//将字符串转化为数字
            y1 = Double.valueOf(stringy1.toString());//将字符串转化为数字
            x2 = Double.valueOf(stringx2.toString());//将字符串转化为数字
            y2 = Double.valueOf(stringy2.toString());//将字符串转化为数字
        }
        else if(getData().charAt(0)=='2'||getData().charAt(0)=='3') {
            stringx1 = getData().substring(2,pos[0]);
            stringy1 = getData().substring(pos[0]+1,pos[1]);
            stringx2 = getData().substring(pos[1]+1,pos[2]);
            stringy2 = getData().substring(pos[2]+1,pos[3]);
            stringx3 = getData().substring(pos[3]+1,pos[4]);
            stringy3 = getData().substring(pos[4]+1,getlength());
            x1 = Double.valueOf(stringx1.toString());//将字符串转化为数字
            y1 = Double.valueOf(stringy1.toString());//将字符串转化为数字
            x2 = Double.valueOf(stringx2.toString());//将字符串转化为数字
            y2 = Double.valueOf(stringy2.toString());//将字符串转化为数字
            x3 = Double.valueOf(stringx3.toString());//将字符串转化为数字
            y3 = Double.valueOf(stringy3.toString());//将字符串转化为数字
        }
        else if(getData().charAt(0)=='4'||getData().charAt(0)=='5') {
            stringx1 = getData().substring(2,pos[0]);
            stringy1 = getData().substring(pos[0]+1,pos[1]);
            stringx2 = getData().substring(pos[1]+1,pos[2]);
            stringy2 = getData().substring(pos[2]+1,pos[3]);
            stringx3 = getData().substring(pos[3]+1,pos[4]);
            stringy3 = getData().substring(pos[4]+1,pos[5]);
            stringx4 = getData().substring(pos[5]+1,pos[6]);
            stringy4 = getData().substring(pos[6]+1,getlength());
            x1 = Double.valueOf(stringx1.toString());//将字符串转化为数字
            y1 = Double.valueOf(stringy1.toString());//将字符串转化为数字
            x2 = Double.valueOf(stringx2.toString());//将字符串转化为数字
            y2 = Double.valueOf(stringy2.toString());//将字符串转化为数字
            x3 = Double.valueOf(stringx3.toString());//将字符串转化为数字
            y3 = Double.valueOf(stringy3.toString());//将字符串转化为数字
            x4 = Double.valueOf(stringx4.toString());//将字符串转化为数字
            y4 = Double.valueOf(stringy4.toString());//将字符串转化为数字
        }
        
    }
}//类的括号
View Code

     其中的stringy1 = getData().substring(pos[0]+1,pos[1]);,这个时一个可以将其中的单独需要的内容提取出来,是比较方便的函数,同时我们还需要将其中的数字类型的字符出转换成double类型的数                        字就需要用到一个函数Double.isvalueof(),这就可以将其中的内容,转换成Double类型的的数字

踩坑心得:其实在上面的设计思路中,我们就已经谈论到了,因为斜率不存在的问题,导致了我后面判断的时候写的很复杂,都是一直在讨论,强力推荐的大家好好的去构思一下,这个题目构思是非常重要的,                    在写之前,一个单独的一点一点的将每一部分测试一下,就是比较好的方法,我再=在这里是吃了比较大的亏。

改进建议:这里面,改进的话,推荐大家采用一些叉乘的公式,这样就不用那么复杂的去讨论斜率存在,或者是不存在的问题,还不溶于出错,代码的行数比较少,特别好用,在写了这些题目后,才发现,真的                    是,要想代码写的少,公式就要找的好,金玉良言。

设计ATM仿真系统,具体要求参见作业说明。
OO作业8-1题目说明.pdf

输入格式:

每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:

  • 存款、取款功能输入数据格式:
    卡号 密码 ATM机编号 金额(由一个或多个空格分隔),
    其中,当金额大于0时,代表取款,否则代表存款。
  • 查询余额功能输入数据格式:
    卡号

输出格式:

①输入错误处理

  • 如果输入卡号不存在,则输出Sorry,this card does not exist.
  • 如果输入ATM机编号不存在,则输出Sorry,the ATM's id is wrong.
  • 如果输入银行卡密码错误,则输出Sorry,your password is wrong.
  • 如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.
  • 如果检测为跨行存取款,则输出Sorry,cross-bank withdrawal is not supported.

②取款业务输出

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

③存款业务输出

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

④查询余额业务输出

¥[金额]

金额保留两位小数

设计思路:首先按照题目要求,构造出想要的类,对于每一个类,之间的方法,需要进行构思,对于一开始就没有设计好的好,产生了很多的重复代码,以至于代码长度会特别长,代码质量是不良的,所以我推                    荐大家仅仅攻参考,这是像我这样学过c语言的人,比较难以摆脱的内容,需要慢慢向面向对象的方向进行走一走,设计时候,可以对于每个人的账号为一个类,采用继承的方式,每个人能的卡都继                      承于同一个类,能够节省很多的重复的代码,实现代码的复用会好很多,而不是采用以每一个人来做一个类,我采用人的设计,导致后来产生了很多的问题

                  以下是源码(走了很多的弯路):

import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        operation op = new operation();
        op.operate();
    }

}
class operation {
    datayang yang = new datayang();
    dataguo guo = new dataguo();
    datazhang zhang = new datazhang();
    datawei wei = new datawei();
    show sho = new show(); 
    private String [] qq = new String [100];
    private String q = "";
    private int []size = new int [100];
    public void operate() {
        Scanner in = new Scanner(System.in);
        int i = -1,j = -1;
        int times = 0;
        int []casee = new int [100];
        while(true) {
            i++;
            qq[i] = in.nextLine();
            size[i] = qq[i].length();
            if(qq[i].equals("#"))
                break;
        }//先存储
        while(true) {
            j++;
            if(qq[j].matches("^[0-9]{19}[ ]{1,}[0-9]{8}[ ]{1,}[0-9][0-9][ ]{1,}-?([1-9]\\d*.\\d*|0.\\d[1-9]\\d*)")) {
                casee[j] = 1;times++;
            }
            else if(qq[j].matches("^[0-9]{19}")) {
                casee[j] = 2;times++;
            }
            else if(qq[j].equals("#"))
                break;
            else
                break;
        }//再正则判断
        for(j = 0;j < times;j++) {//j表示正确的数目
            if(casee[j] == 1) {
//                String x1 = qq[j].substring(0,19);//卡号
//                String x2 = qq[j].substring(20,28);//密码
//                String x3 = qq[j].substring(29,31);//机器
//                String x4 = qq[j].substring(32,size[j]);//钱
                
                String []x = qq[j].split("\\s+");
                
                double money = Double.valueOf(x[3]);
                String a = String.format("%.2f", money);
                money = Double.valueOf(a);
                if(x[0].equals(yang.getCard1())||x[0].equals(yang.getCard2())) {
                    if(x[1].equals("88888888")) {
                        if(x[2].equals("01")||x[2].equals("02")||x[2].equals("03")||x[2].equals("04")) {
                            if(money<=yang.getLastMoney1()) {
                                yang.setLastMoney1(yang.getLastMoney1()-money);
                                sho.showData1_1(yang, x[2], money);
                            }    
                            else
                                System.out.println("Sorry,your account balance is insufficient.");
                        }
                        else if(x[2].equals("05")||x[2].equals("06"))
                            System.out.println("Sorry,cross-bank withdrawal is not supported.");
                        else
                            System.out.println("Sorry,the ATM's id is wrong.");
                    }
                    else
                        System.out.println("Sorry,your password is wrong.");
                }
                /*------------------------------*/
                else if(x[0].equals(yang.getCard3())) {
                    if(x[1].equals("88888888")) {
                        if(x[2].equals("01")||x[2].equals("02")||x[2].equals("03")||x[2].equals("04")) {
                            if(money<=yang.getLastMoney2()) {
                                yang.setLastMoney2(yang.getLastMoney2()-money);
                                sho.showData1_3(yang, x[2], money);
                            }
                                
                            else
                                System.out.println("Sorry,your account balance is insufficient.");
                        }
                        else if(x[2].equals("05")||x[2].equals("06"))
                            System.out.println("Sorry,cross-bank withdrawal is not supported.");
                        else
                            System.out.println("Sorry,the ATM's id is wrong.");
                    }
                    else
                        System.out.println("Sorry,your password is wrong.");
                }
                /*-----------------------------*/
                else if(x[0].equals(guo.getCard2())) {
                    if(x[1].equals("88888888")) {
                        if(x[2].equals("01")||x[2].equals("02")||x[2].equals("03")||x[2].equals("04")) {
                            if(money<=guo.getLastMoney()) {
                                guo.setLastMoney(guo.getLastMoney()-money);
                                sho.showData2_1(guo, x[2], money);
                            }
                                
                            else
                                System.out.println("Sorry,your account balance is insufficient.");
                        }
                        else if(x[2].equals("05")||x[2].equals("06"))
                            System.out.println("Sorry,cross-bank withdrawal is not supported.");
                        else
                            System.out.println("Sorry,the ATM's id is wrong.");
                    }
                    else
                        System.out.println("Sorry,your password is wrong.");
                }
                
                /*-------------------------------*/
                else if(x[0].equals(zhang.getCard1())) {
                    if(x[1].equals("88888888")) {
                        if(x[2].equals("05")||x[2].equals("06")) {
                            if(money<=zhang.getLastMoney1()) {
                                zhang.setLastMoney1(zhang.getLastMoney1()-money);
                                sho.showData3_1(zhang, x[2], money);
                            }
                                
                            else
                                System.out.println("Sorry,your account balance is insufficient.");
                        }
                        else if(x[2].equals("01")||x[2].equals("02")||x[2].equals("03")||x[2].equals("04"))
                            System.out.println("Sorry,cross-bank withdrawal is not supported.");
                        else
                            System.out.println("Sorry,the ATM's id is wrong.");
                    }
                    else
                        System.out.println("Sorry,your password is wrong.");
                }
                /*------------------------------*/
                else if(x[0].equals(zhang.getCard2())) {
                    if(x[1].equals("88888888")) {
                        if(x[2].equals("05")||x[2].equals("06")) {
                            if(money<=zhang.getLastMoney2()) {
                                zhang.setLastMoney2(zhang.getLastMoney2()-money);
                                sho.showData3_3(zhang, x[2], money);
                            }
                                
                            else
                                System.out.println("Sorry,your account balance is insufficient.");
                        }
                        else if(x[2].equals("01")||x[2].equals("02")||x[2].equals("03")||x[2].equals("04"))
                            System.out.println("Sorry,cross-bank withdrawal is not supported.");
                        else
                            System.out.println("Sorry,the ATM's id is wrong.");
                    }
                    else
                        System.out.println("Sorry,your password is wrong.");
                }
                
                /*-----------------------*/
                else if(x[0].equals(zhang.getCard3())||x[0].equals(zhang.getCard4())) {
                    if(x[1].equals("88888888")) {
                        if(x[2].equals("05")||x[2].equals("06")) {
                            if(money<=zhang.getLastMoney3()) {
                                zhang.setLastMoney3(zhang.getLastMoney3()-money);
                                sho.showData3_5(zhang, x[2], money);
                            }
                                
                            else
                                System.out.println("Sorry,your account balance is insufficient.");
                        }
                        else if(x[2].equals("01")||x[2].equals("02")||x[2].equals("03")||x[2].equals("04"))
                            System.out.println("Sorry,cross-bank withdrawal is not supported.");
                        else
                            System.out.println("Sorry,the ATM's id is wrong.");
                    }
                    else
                        System.out.println("Sorry,your password is wrong.");
                }
                /*----------------------------*/
                else if(x[0].equals(wei.getCard1())) {
                    if(x[1].equals("88888888")) {
                        if(x[2].equals("05")||x[2].equals("06")) {
                            if(money<=wei.getLastMoney1()) {
                                wei.setLastMoney1(wei.getLastMoney1()-money);
                                sho.showData4_1(wei, x[2], money);
                            }
                                
                            else
                                System.out.println("Sorry,your account balance is insufficient.");
                        }
                        else if(x[2].equals("01")||x[2].equals("02")||x[2].equals("03")||x[2].equals("04"))
                            System.out.println("Sorry,cross-bank withdrawal is not supported.");
                        else
                            System.out.println("Sorry,the ATM's id is wrong.");
                    }
                    else
                        System.out.println("Sorry,your password is wrong.");
                }
                /*------------------------------*/
                else if(x[0].equals(wei.getCard2())) {
                    if(x[1].equals("88888888")) {
                        if(x[2].equals("05")||x[2].equals("06")) {
                            if(money<wei.getLastMoney2()) {
                                wei.setLastMoney2(wei.getLastMoney2()-money);
                                sho.showData4_3(wei, x[2], money);
                            }    
                            else
                                System.out.println("Sorry,your account balance is insufficient.");
                        }
                        else if(x[2].equals("01")||x[2].equals("02")||x[2].equals("03")||x[2].equals("04"))
                            System.out.println("Sorry,cross-bank withdrawal is not supported.");
                        else
                            System.out.println("Sorry,the ATM's id is wrong.");
                    }
                    else
                        System.out.println("Sorry,your password is wrong.");
                }
                else 
                    System.out.println("Sorry,this card does not exist.");
            }
            if(casee[j] == 2) {
                String x1 = qq[j].substring(0,19);//卡号
                if(x1.equals(yang.getCard1())||x1.equals(yang.getCard2()))
                    sho.showData1_2(yang);
                else if(x1.equals(yang.getCard3())) 
                    sho.showData1_4(yang);
                else if(x1.equals(guo.getCard2()))
                    sho.showData2_2(guo);
                else if(x1.equals(zhang.getCard1()))
                    sho.showData3_2(zhang);
                else if(x1.equals(zhang.getCard2()))
                    sho.showData3_4(zhang);
                else if(x1.equals(zhang.getCard3())||x1.equals(zhang.getCard4())) 
                    sho.showData3_6(zhang);
                else if(x1.equals(wei.getCard1()))
                    sho.showData4_2(wei);
                else if(x1.equals(wei.getCard2()))
                    sho.showData4_4(wei);
                else 
                    System.out.println("Sorry,this card does not exist.");
            }
        }
    }
}        
class show {
    public void showData1_1(datayang information,String machine,double num){
        if(num>0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上取款¥"+new DecimalFormat("0.00").format(num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney1()));
        }
        else if(num<0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上存款¥"+new DecimalFormat("0.00").format(-num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney1()));
        }
    }
    public void showData1_2(datayang information){        
        System.out.println("¥"+new DecimalFormat("0.00").format(information.getLastMoney1()));
    }
//////////////////
    public void showData1_3(datayang information,String machine,double num){
    
        if(num>0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上取款¥"+new DecimalFormat("0.00").format(num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney2()));
        }
        else if(num<0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上存款¥"+new DecimalFormat("0.00").format(-num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney2()));
        }
    }
    public void showData1_4(datayang information){    
        System.out.println("¥"+new DecimalFormat("0.00").format(information.getLastMoney2()));
    }
///////////////////////
    public void showData2_1(dataguo information,String machine,double num){
        if(num>0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上取款¥"+new DecimalFormat("0.00").format(num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney()));
        }
        else if(num<0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上存款¥"+        new DecimalFormat("0.00").format(-num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney()));
        }
    }
    public void showData2_2(dataguo information){    
        System.out.println("¥"+new DecimalFormat("0.00").format(information.getLastMoney()));
    }
///////////////////////
    public void showData3_1(datazhang information,String machine,double num){
        if(num>0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上取款¥"+new DecimalFormat("0.00").format(num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney1()));
        }
        else if(num<0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上存款¥"+new DecimalFormat("0.00").format(-num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney1()));
        }
    }
    public void showData3_2(datazhang information){        
        System.out.println("¥"+new DecimalFormat("0.00").format(information.getLastMoney1()));
    }
///////////////////////
    public void showData3_3(datazhang information,String machine,double num){
        new DecimalFormat("0.00").format(num);
        new DecimalFormat("0.00").format(information.getLastMoney2());
        if(num>0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上取款¥"+new DecimalFormat("0.00").format(num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney2()));
        }
        else if(num<0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上存款¥"+new DecimalFormat("0.00").format(-num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney2()));
        }
    }
    public void showData3_4(datazhang information){        
        System.out.println("¥"+new DecimalFormat("0.00").format(information.getLastMoney2()));
    }
    ////////////////
    public void showData3_5(datazhang information,String machine,double num){
        if(num>0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上取款¥"+new DecimalFormat("0.00").format(num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney3()));
        }
        else if(num<0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上存款¥"+new DecimalFormat("0.00").format(-num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney3()));
        }
    }
    public void showData3_6(datazhang information){        
        System.out.println("¥"+new DecimalFormat("0.00").format(information.getLastMoney3()));
    }
    //////////////////////
    public void showData4_1(datawei information,String machine,double num){
        new DecimalFormat("0.00").format(num);
        new DecimalFormat("0.00").format(information.getLastMoney1());
        if(num>0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上取款¥"+new DecimalFormat("0.00").format(num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney1()));
        }
        else if(num<0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上存款¥"+new DecimalFormat("0.00").format(-num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney1()));
        }
    }
    public void showData4_2(datawei information){    
        System.out.println("¥"+new DecimalFormat("0.00").format(information.getLastMoney1()));
    }
    //////////////////////
    public void showData4_3(datawei information,String machine,double num){
        new DecimalFormat("0.00").format(num);
        new DecimalFormat("0.00").format(information.getLastMoney2());
        if(num>0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上取款¥"+new DecimalFormat("0.00").format(num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney2()));
        }
        else if(num<0) {
            System.out.println(information.getName()+"在"+information.getBank()+"的"+machine+"号ATM机上存款¥"+new DecimalFormat("0.00").format(-num));
            System.out.println("当前余额为¥"+new DecimalFormat("0.00").format(information.getLastMoney2()));
        }
    }
    public void showData4_4(datawei information){    
        System.out.println("¥"+new DecimalFormat("0.00").format(information.getLastMoney2()));
    }
}
class datayang {
    private String name = "杨过";
    private String bank = "中国建设银行";
    
    private double lastMoney1 = 10000; 
    private double lastMoney2 = 10000;
    private String bankCard1 = "3217000010041315709" ;
    private String bankCard2 = "3217000010041315709" ;
    private String card1 = "6217000010041315709";
    private String card2 = "6217000010041315715";
    private String card3 = "6217000010041315718";
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getBank() {
        return bank;
    }
    public void setBank(String bank) {
        this.bank = bank;
    }
    public double getLastMoney1() {
        return lastMoney1;
    }
    public void setLastMoney1(double lastMoney1) {
        this.lastMoney1 = lastMoney1;
    }
    public double getLastMoney2() {
        return lastMoney2;
    }
    public void setLastMoney2(double lastMoney2) {
        this.lastMoney2 = lastMoney2;
    }
    public String getBankCard1() {
        return bankCard1;
    }
    public void setBankCard1(String bankCard1) {
        this.bankCard1 = bankCard1;
    }
    public String getBankCard2() {
        return bankCard2;
    }
    public void setBankCard2(String bankCard2) {
        this.bankCard2 = bankCard2;
    }
    public String getCard1() {
        return card1;
    }
    public void setCard1(String card1) {
        this.card1 = card1;
    }
    public String getCard2() {
        return card2;
    }
    public void setCard2(String card2) {
        this.card2 = card2;
    }
    public String getCard3() {
        return card3;
    }
    public void setCard3(String card3) {
        this.card3 = card3;
    }
}
class dataguo {
    private String name= "郭靖";
    private String bank = "中国建设银行";
    private double lastMoney = 10000; 
    private String bankCard = "3217000010051320007" ;
    private String card2 = "6217000010051320007";
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getBank() {
        return bank;
    }
    public void setBank(String bank) {
        this.bank = bank;
    }
    public double getLastMoney() {
        return lastMoney;
    }
    public void setLastMoney(double lastMoney) {
        this.lastMoney = lastMoney;
    }
    public String getBankCard() {
        return bankCard;
    }
    public void setBankCard(String bankCard) {
        this.bankCard = bankCard;
    }
    public String getCard2() {
        return card2;
    }
    public void setCard2(String card2) {
        this.card2 = card2;
    }
    
}
class datazhang {
    private String name = "张无忌";
    private String bank = "中国工商银行";
    private double lastMoney1 = 10000; 
    private double lastMoney2 = 10000; 
    private double lastMoney3 = 10000; 
    private String bankCard1 = "3222081502001312389" ;
    private String bankCard2 = "3222081502001312390" ;
    private String bankCard3 = "3222081502001312399" ;
    private String card1 = "6222081502001312389";
    private String card2 = "6222081502001312390";
    private String card3 = "6222081502001312399";
    private String card4 = "6222081502001312400";
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getBank() {
        return bank;
    }
    public void setBank(String bank) {
        this.bank = bank;
    }
    
    public double getLastMoney1() {
        return lastMoney1;
    }
    public void setLastMoney1(double lastMoney1) {
        this.lastMoney1 = lastMoney1;
    }
    
    
    public double getLastMoney2() {
        return lastMoney2;
    }
    public void setLastMoney2(double lastMoney2) {
        this.lastMoney2 = lastMoney2;
    }
    public double getLastMoney3() {
        return lastMoney3;
    }
    public void setLastMoney3(double lastMoney3) {
        this.lastMoney3 = lastMoney3;
    }
    public String getBankCard1() {
        return bankCard1;
    }
    public void setBankCard1(String bankCard1) {
        this.bankCard1 = bankCard1;
    }
    public String getBankCard2() {
        return bankCard2;
    }
    public void setBankCard2(String bankCard2) {
        this.bankCard2 = bankCard2;
    }
    public String getBankCard3() {
        return bankCard3;
    }
    public void setBankCard3(String bankCard3) {
        this.bankCard3 = bankCard3;
    }
    public String getCard1() {
        return card1;
    }
    public void setCard1(String card1) {
        this.card1 = card1;
    }
    public String getCard2() {
        return card2;
    }
    public void setCard2(String card2) {
        this.card2 = card2;
    }
    public String getCard3() {
        return card3;
    }
    public void setCard3(String card3) {
        this.card3 = card3;
    }
    public String getCard4() {
        return card4;
    }
    public void setCard4(String card4) {
        this.card4 = card4;
    }
    
    
    
}
class datawei {

    private String name = "韦小宝";
    private String bank = "中国工商银行";
    private double lastMoney1 = 10000; 
    private double lastMoney2 = 10000; 
    private String bankCard1 = "3222081502051320785" ;
    private String bankCard2 = "3222081502051320786" ;
    private String card1 = "6222081502051320785";
    private String card2 = "6222081502051320786";
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getBank() {
        return bank;
    }
    public void setBank(String bank) {
        this.bank = bank;
    }
    
    public double getLastMoney1() {
        return lastMoney1;
    }
    public void setLastMoney1(double lastMoney1) {
        this.lastMoney1 = lastMoney1;
    }
    public double getLastMoney2() {
        return lastMoney2;
    }
    public void setLastMoney2(double lastMoney2) {
        this.lastMoney2 = lastMoney2;
    }
    public String getBankCard1() {
        return bankCard1;
    }
    public void setBankCard1(String bankCard1) {
        this.bankCard1 = bankCard1;
    }
    public String getBankCard2() {
        return bankCard2;
    }
    public void setBankCard2(String bankCard2) {
        this.bankCard2 = bankCard2;
    }
    public String getCard1() {
        return card1;
    }
    public void setCard1(String card1) {
        this.card1 = card1;
    }
    public String getCard2() {
        return card2;
    }
    public void setCard2(String card2) {
        this.card2 = card2;
    }
    
    
}
View Code

踩坑新得:从上述可以看出来,这个类图十分混乱,而且,每一个里面的内容字段特别多,里面设计非常不合理,不推荐大家使用,包括里面还有很多的重复代码,十分不好,希望大家能够先罗列出一些好的                        计方案,再去施行,这样才是对滴,别直接动手,最后花费的时间还会更多,设计好了在以后,还可以把以前的代码,再次拿来使用,这是比较好的方向,对于我们这些初学者,还是先设计,因为,                    我们学习的java就是面向对象的设计,类与类之间就是要解除耦合,这样才能够在以后方便拓展和复用。

改进方案:采用继承的方式,将其中的重复的类进行继承,比如说,其中的每一个人的账户很多的内容都是相同的,我们可以了解一些继承的内容,然后去对这些账户内容复用,能够大大减少代码的行数,实现                    好的代码。

 

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

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

      ```
          The line's color is:颜色值
          The line's begin point's Coordinate is:
          (x1,y1)
          The line's end point's Coordinate is:
          (x2,y2)
          The line's length is:长度值
      ```
    
     

    其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。

 

 

 

设计思路:设计出一个Line类和Point类,其中,在Point类中进行输入,每次输入一个点,在Line类中创建两个Point的对象j,就可以实现,输入两个点,在每一个类中都存在独自的方法,比如说计算距离的                              getDistance(),和disPlay()这样的方法,该题目相对于来说较为简单,比较好实现。

     以下是源代码:

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        double x1 = in.nextDouble();
        double y1 = in.nextDouble();
        double x2 = in.nextDouble();
        double y2 = in.nextDouble();
        String 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 Line {
    private Point point1 = new Point();
    private Point point2 = new Point();
    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 distance = Math.sqrt(Math.pow(point1.getX() - point2.getX(),2) + Math.pow(point1.getY() - point2.getY(),2));
        return distance;
    }
    public void display() {
        System.out.println("The line's color is:" + color);
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.printf("The line's length is:");
        System.out.printf(String.format("%.2f", getDistance()));
    }
}
class Point {
    private double x = 0;
    private double y = 0;
    
    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 () {
        System.out.printf("(");
        System.out.printf(String.format("%.2f", x));
        System.out.printf(",");
        System.out.printf(String.format("%.2f", y));
        System.out.printf(")");
        System.out.println("");
    }
}
View Code

踩坑心得:不太清楚什么in.nextLine(),无法输入,一开始没有找到缺点,最后发现后改成了in.next(),就可以了,我也不知道是为什么,同时要对每一个点都要进行判断条件(0,200],一开始就没有太注意,导                      致犯了很严重的错误,自己也没有发现,希望大家能够,在写代码的时候,考虑完全,别丢三落四,忘掉了很多的条件,导致自己都不知道自己错在哪里了。这也是程序员比较忌讳的东西。

 

 

在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。

  • 对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
  • 再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
  • 在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
          element = p1;//起点Point
          element.display();
          
          element = p2;//终点Point
          element.display();
          
          element = line;//线段
          element.display();
          
          element = plane;//面
          element.display();

 

 

 

设计思路:按照类图的要求,我们只需要在上一题的代码上进行迭代,就可以了。增加了一个plane的面类,将前三个都继承与Element类,三者都继承了Element的display方法,并且都进行了重写,实现了多态

                  在输出的时候,要使用向上转型,实现多态,一个对象做同一件事的不同效果,多态特性。

                  以下是源代码:写的还好吧,可以看看。

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        double x1 = in.nextDouble();
        double y1 = in.nextDouble();
        double x2 = in.nextDouble();
        double y2 = in.nextDouble();
        String 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);
            Plane plane = new Plane(color);
            Line line = new Line(point1,point2,color);
            Element element;
            element = point1;
            element.display();
            element = point2;
            element.display();
            element = line;
            element.display();
            element = plane;
            element.display();
        }
        else
            System.out.println("Wrong Format");
    }
}
class Line extends Element{
    private Point point1 = new Point();
    private Point point2 = new Point();
    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 distance = Math.sqrt(Math.pow(point1.getX() - point2.getX(),2) + Math.pow(point1.getY() - point2.getY(),2));
        return distance;
    }
    public void display() {
        System.out.println("The line's color is:" + color);
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.printf("The line's length is:");
        System.out.println(String.format("%.2f", getDistance()));
    }
}
class Point extends Element{
    private double x = 0;
    private double y = 0;
    
    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 () {
        System.out.printf("(");
        System.out.printf(String.format("%.2f", x));
        System.out.printf(",");
        System.out.printf(String.format("%.2f", y));
        System.out.printf(")");
        System.out.println("");
    }
}
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;
    }
    public void display() {
        System.out.printf("The Plane's color is:" + color);
    }
}
View Code

 

踩坑心得:说实话,这个题目,没什么坑,认真去写,多考虑以下,也就能够完成了。

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

  • 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>
  • 增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
  • 在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
    • 1:向容器中增加Point对象
    • 2:向容器中增加Line对象
    • 3:向容器中增加Plane对象
    • 4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
    • 0:输入结束
    示例代码如下:
       choice = input.nextInt();
        while(choice != 0) {
            switch(choice) {
            case 1://insert Point object into list 
              ...
                break;
            case 2://insert Line object into list
                ...
                break;
            case 3://insert Plane object into list
                ...
                break;
            case 4://delete index - 1 object from list
                int index = input.nextInt();
                ...
            }
            choice = input.nextInt();
        }
    
      输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。

 

 

 

设计思路:在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象,其中里面设计出一个add和remove的方法,其实这个容器类,是java自带的比                  较好用,可以多去了解一下,然后去使用一下,就可以实现

                以下是源代码:

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

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        GeometryObject geometryObject = new GeometryObject();
        int choice = in.nextInt();
        while(choice != 0) {
            switch(choice) {
                case 1:{//insert Point object into list 输入“点”对象的x,y值
                    double x1 = in.nextDouble();
                    double y1 = in.nextDouble();
                    if(x1 > 0&&x1 <= 200 &&y1 > 0&&y1 <= 200 ) {
                        Point point1 = new Point(x1,y1);
                        geometryObject.add(point1);
                    }
                    else 
                        System.out.println("Wrong Format");
                }
                    break;
                case 2:{//insert Line object into list输入“线”对象两个端点的x,y值
                    double x1 = in.nextDouble();
                    double y1 = in.nextDouble();
                    double x2 = in.nextDouble();
                    double y2 = in.nextDouble();
                    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);
                        String color = in.next();
                        Line line = new Line(point1,point2,color);
                        geometryObject.add(line);
            
                    }
                    else 
                        System.out.println("Wrong Format");
                }
                    break;
                case 3:{//insert Plane object into list输入“面”对象的颜色值
                    String color = in.next();
                    Plane plane = new Plane(color);
                    geometryObject.add(plane);
                }
                    break;
                case 4:{//delete index - 1 object from list输入要删除的对象位置(从1开始)  ...
                    int index = in.nextInt();
                    geometryObject.remove(index);
                }
            }
            choice = in.nextInt();
        }
        for(Element e : geometryObject.getList()) {
            e.display();
        }
    }
}
class Line extends Element{
    private Point point1 = new Point();
    private Point point2 = new Point();
    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 distance = Math.sqrt(Math.pow(point1.getX() - point2.getX(),2) + Math.pow(point1.getY() - point2.getY(),2));
        return distance;
    }
    public void display() {
        System.out.println("The line's color is:" + color);
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.printf("The line's length is:");
        System.out.println(String.format("%.2f", getDistance()));
    }
}
class Point extends Element{
    private double x = 0;
    private double y = 0;
    
    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 () {
        System.out.printf("(");
        System.out.printf(String.format("%.2f", x));
        System.out.printf(",");
        System.out.printf(String.format("%.2f", y));
        System.out.printf(")");
        System.out.println("");
    }
}
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;
    }
    public void display() {
        System.out.println("The Plane's color is:" + 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 - 1);
    }
    public ArrayList<Element> getList() {
        return list;
    }
}
View Code

踩坑心得:对于ArrayList这个类,我掌握的不好,导致在这个上面花了很多的时间,最终还是没有怎么实现好,这是很麻烦的事情,希望大家能够先去将Array List这个类了解是什么,如何使用,会使用后,就能                    够很熟练的掌握这些内容了

java链表的实现:单向链表和双向链表

很多的都是需要大家自己去理解的,难以讲解清楚:

package test47;

public interface DoubleListInterface<E> {

    public boolean isEmpty();

    public int getSize();

    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();
    
    public E getFirst();
    public E getLast();
}
package test47;

public class LList<E> implements DoubleListInterface<E> {
    private Node<E> head;// 头结点(非第一个节点),当前节点,尾节点
    private Node<E> curr;
    private Node<E> tail;

    private int size = 0;    
    
    public LList() {
        super();
        // TODO Auto-generated constructor stub
        head = new Node<E>();
        head.setNext(null);
        head.setPrevious(null);
        curr = tail = null;
        this.size = 0;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return this.size == 0;
    }

    @Override
    public int getSize() {
        // TODO Auto-generated method stub
        return this.size;
    }

    @Override
    public E get(int index) {//查找
        // TODO Auto-generated method stub
        if(index < 1 || index > this.size) {
            return null;
        }
        
        curr = head;
        for(int i = 0; i < index; i ++) {
            curr = curr.getNext();
        }
        
        return curr.getData();
    }

    @Override
    public void remove(int index) {
        // TODO Auto-generated method stub
        if(index < 1 || index > this.size) {
            return ;
        }
        
        curr = head;
        
        if(index == 1) {
            curr.getNext().setPrevious(head);
            curr = head.getNext();
            head.setNext(curr.getNext());
        }else {//找到第index - 1个节点赋给curr
            for(int i = 1;i < index; i++) {
                curr = curr.getNext();
            }
            curr.getNext().getNext().setPrevious(curr);
            curr.setNext(curr.getNext().getNext());
        }
        
        if(index == this.size) {//如果删除的是最后一个节点
            tail = curr;
        }
        
        this.size --;//整个链表的节点数量-1

    }

    @Override
    public void add(int index, E theElement) {//指定位置添加
        // TODO Auto-generated method stub
        if(index < 1 || index > this.size + 1) {
            return ;
        }
        
        Node<E> curr = new Node<>();
        curr.setData(theElement);
        curr.setNext(null);
        curr.setPrevious(null);
        if(this.size == 0) {
            head.setNext(curr);
            tail = curr;
            curr.setPrevious(head);
        }else if(index == this.size + 1) {
            this.tail.setNext(curr);
            curr.setPrevious(tail);
            tail = curr;
            
        }else {
            Node<E> tempNode = head;
            for(int i  = 1; i < index;i++) {
                tempNode = tempNode.getNext();
            }
            
            curr.setNext(tempNode.getNext());
            tempNode.getNext().setPrevious(curr);
            curr.setPrevious(tempNode);
            tempNode.setNext(curr);
//            for(int i = size ; i >= index ; i-- ) {
//                tail.getFront() = 
//            }
        }
        
        this.size ++;        

    }

    @Override
    public void add(E element) {
        // TODO Auto-generated method stub
        this.add(this.size + 1,element);

    }

    @Override
    public void printList() {
        // TODO Auto-generated method stub
        curr = head.getNext();
        for(int i = 1; i <= this.size;i ++) {
            System.out.print(curr.getData() + " ");
            curr = curr.getNext();
        }
        
        System.out.println("");

    }
    
    public E getLast() {
        if(this.size != 0) {
            return tail.getData();
        }
        return null;
    }
    public E getFirst() {
        if(this.size != 0) {
            return head.getNext().getData();
        }
        return null;
    }package test47;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        LList<Integer> list = new LList<>();
        
        list.add(2);
        list.add(4);
        list.add(6);
        list.add(8);
        list.printList();
        list.remove(3);
        
        list.printList();
        list.add(2,100);
        list.printList();
        System.out.println(list.get(4));
    }

}
package test47;

//Node
public class Node<E> {
    private Node<E> previous;
    private E data;
    private Node<E> next;
    
    public Node() {

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

    public E getData() {
        return data;
    }

    public void setData(E data) {
        this.data = data;
    }

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

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

    public Node<E> getPrevious() {
        return previous;
    }

    public void setPrevious(Node<E> previous) {
        this.previous = previous;
    }
    
}
}
双向链表
package asdfnz;

public class main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        List<String> list  = new List<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
    //    list.printList();
        list.remove(1);
        list.add(3,"asd");
        list.printList();
    }

}
interface LinearListInterface<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();    

}


//LinkedList class

class LList<E>{
    
    private Node<E> head,curr,tail;
    
    private int size;

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}



//Node

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;
    }
}
class List<E> implements LinearListInterface<E>{
    private Node<E> head = new Node<E>(); 
    private Node<E> curr = new Node<E>();
    private Node<E> tail = new Node<E>();
    int length = 0;
    @Override
    public boolean isEmpty() {
            if(curr.getO() == null)
                return true;
            else 
                return false;
        
    }
    @Override
    public int size() {
        return 0;
    }
    @Override
    public E get(int index) {
        if(curr.getO() != null )
            return curr.getO();
        else 
            return null;
    }
    @Override
    public void remove(int index) {//表示删除
        Node<E> p = new Node<E>();
        int i = 1;
        if(head != tail) {
            for(i = 1,p = head ;p != tail ; i++ , p = p.getNext()) {
                if(i == index &&index == 1&&p.getNext() != null) {
                    head = p.getNext();
                    break;
                }
                else if(i == index-1 &&index != 1&&p.getNext().getNext() != null){
                    p.setNext(p.getNext().getNext());
                    break;
                }
                else if(i == index-1 &&index != 1&&p.getNext().getNext() == null){
                    tail = p;
                    break;
                }
            }
        }    
        else {
            head = null;
        }
    }
    @Override
    public void add(int index, E theElement) {//表示指定位置增加
        //需要跳转节点
        Node p = new Node();
        Node q = new Node();
        q.setO(theElement);
        int i = 1;
        if(head != tail)
        for(i = 1,p = head ; p!=tail ; i++ , p = p.getNext()) {
            if(index == 1&&i == index) {
                head = q;
                break;
            }
            else if(index != 1&& i == index-1) {
                q.setNext(p.getNext());
                p.setNext(q);
                
                break;
            }
            else if(i == index-1 &&index != 1&&p.getNext().getNext() == null) {
                tail.setNext(q);
                q.setNext(null);
                break;
            }
        }
    }
    @Override
    public void add(E element) {//从最后面增加
        
        Node curr1= new Node();
        curr1.setO(element);
        curr = curr1;
        if(head.getO()== null) {
            head = curr;
            tail = curr;
            head.setNext(null);
        }
        else {
            tail.setNext(curr);
            tail = curr;
            tail.setNext(null);
        }
    }
    @Override
    public void printList() {
        Node<E> p = new Node<E>();
        if(head != tail) {
            for( p = head;p != tail ;p = p.getNext() ) {
                System.out.println(p.getO());
            }
            System.out.println(tail.getO());
        }
        else if(head == tail)
            System.out.println(head.getO());
    }
}
单向链表

说实话,下面这道题是最难的一道题,困扰了我很久,最后都没有完成,大家可以先浅看一下题目:

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

 

设计思路:对于一开始的那几道题,我的其中的很多的东西在这里是无法使用,因为设计的不好,代码比较长,包括判断,这个代码长度限制在了800行左右,难以实现,所以很多的代码,我都重                   写了,并且能够用方法的全部使用了。设计思路是需要先用正则表达式判断是不是合法的输入,以及是不点重合,或者其中的点的数量是不是正确,这些都需要判断,其中,使用正则                     表达式,只用了我几行代码,就完成了判断,这是一个比较好的方法:以下是判断的代码

String [] a = x.split(" ");
        int i = 0;
        if(a[0].matches("^[1-6][:]((-|[+])?(([1-9]+[0-9]{0,})|[0]))(\\.\\d+)?[,]((-|[+])?(([1-9]+[0-9]{0,})|[0]))(\\.\\d+)?") == true) {
            for(i = 1 ; i < a.length ; i++) {
                if(a[i].matches("^((-|[+])?(([1-9]+[0-9]{0,})|[0]))(\\.\\d+)?[,]((-|[+])?(([1-9]+[0-9]{0,})|[0]))(\\.\\d+)?") == false) {
                    System.out.print("Wrong Format");
                    System.exit(0);
                }
            }
            if(i == a.length&& i != 1)
                return i;//返回几个点]
            else if(i == 1) {
                System.out.print("wrong number of points");
                System.exit(0);
            }
        }
        else
            System.out.print("Wrong Format");//存在一个超出范围的没有设置
        return 0;
        }
View Code

     短短10行实现了多种类型的判断,同时也要使用循环判断的方式这样可以节省很多时间,还有提取数字的方法,也进行了修改,也从原来的单一方法变成了循环提取,这样又可以节省很多,将判断四                    边形平行,四边形菱形,矩形正方形等,都单独设置一个方法,这样方便,以后调用,四边形的判断比较复杂,推荐在网上找好公式,不要自己轻易的去判断,很容易遗漏一些特殊的值,判断完是否                    是四边形之后可以再次,在基础上判断是否是平行四边形,之后,在平行四边形的基础上,再次叠加判断菱形,矩形,正方形等,这是我们,编写代码之前必须做好的工作,判断是凸四边形还是凹四                    边形的时候其实比较好判断,采用的方法对了,也就容易了,我们可以采用面积法,进行切割,这样可以很快的实现。嗯,进行到输入六个点坐标的时候就比较复杂了,新药判断是否是三角形或者构                    成的点,是否是四边形,其中有很多是按顺序输入的,导致他可能会扭转在一起,这是一个比较难以判断的点,希望大家能够注意到,然后进行切割,判断面积,并计算出面积,输出面积,最后一点                    是判断一个点是否在三角形或者四边形内,这还是可以采用面积法,这是比较容易的一次判断里面我们还需要一系列方法,比如说求两条直线的交点坐标,判断三点是否共线,这些都是需要我们单独                    设立方法比较好的

                  以下是不完全代码:可以参考一下,大部分的内容还是实现了的。

     

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        String x = in.nextLine();
        Judge judg = new Judge(x);
        int chance = judg.judge();
        double[] xy = new double[12];
        xy = judg.getNum(chance);
        if(x.charAt(0) == '1'){//第一种情况
            if(chance == 4) {
                if(judg.quadrabgle(xy) ==1) System.out.printf("true ");    
                else System.out.printf("false ");    
                if(judg.parallelogram(xy))System.out.println("true");
                else System.out.println("false");
            }
            else
                System.out.println("wrong number of points");
        }
        if(x.charAt(0) == '2') {
            if(chance == 4) {
                if(judg.quadrabgle(xy) == 0) {
                    System.out.print("not a quadrilateral");
                    System.exit(0);
                }        
                if(judg.quadrabgle(xy) == 2) {
                    System.out.println("points coincide");
                    System.exit(0);
                }
                if(judg.rhombus(xy)) System.out.print("true ");
                    else System.out.print("false ");
                if(judg.rectangle(xy)) System.out.print("true ");
                    else System.out.print("false ");
                if(judg.square(xy)) System.out.println("true");
                    else System.out.println("false");
            }
            else
                System.out.println("wrong number of points");
        }
        if(x.charAt(0) == '3') {
            if(chance == 4) {
                if(judg.quadrabgle(xy) == 0) {
                    System.out.print("not a quadrilateral");
                    System.exit(0);
                }
                    double s1 = 0,s2 = 0,s3 = 0,s4 = 0;
                    s1 = Math.abs(xy[2]*xy[5]+xy[4]*xy[1]+xy[0]*xy[3]-xy[2]*xy[1]-xy[0]*xy[5]-xy[4]*xy[3]);//123
                    s2 = Math.abs(xy[2]*xy[7]+xy[6]*xy[1]+xy[0]*xy[3]-xy[2]*xy[1]-xy[0]*xy[7]-xy[6]*xy[3]);//124
                    s3 = Math.abs(xy[2]*xy[5]+xy[4]*xy[7]+xy[6]*xy[3]-xy[2]*xy[7]-xy[6]*xy[5]-xy[4]*xy[3]);//234
                    s4 = Math.abs(xy[4]*xy[7]+xy[6]*xy[1]+xy[0]*xy[5]-xy[4]*xy[1]-xy[0]*xy[7]-xy[6]*xy[5]);//134
                    double c = Math.sqrt(Math.pow(xy[0] - xy[2],2) + Math.pow(xy[1] - xy[3],2)) + Math.sqrt(Math.pow(xy[2] - xy[4],2) + Math.pow(xy[3] - xy[5],2)) 
                    + Math.sqrt(Math.pow(xy[4] - xy[6],2) + Math.pow(xy[5] - xy[7],2)) + Math.sqrt(Math.pow(xy[0] - xy[6],2) + Math.pow(xy[1] - xy[7],2));
                    if(s1+s4 == s2+s3) {
                        System.out.print("true ");
                        System.out.print(Math.round(c*1000)/1000.0); 
                        System.out.print(" ");
                        System.out.print(Math.round((s1 + s4)/2*1000)/1000.0); 
                    }
                    else{
                        System.out.print("false ");
                        if(s1 + s4 < s2 + s3) {
    
                            System.out.print(Math.round(c*1000)/1000.0); 
                            System.out.print(" ");
                            System.out.print(Math.round((s1 + s4)/2*1000)/1000.0 );     
                        }
                        else {
                            System.out.print(Math.round(c*1000)/1000.0); 
                            System.out.print(" ");
                            System.out.print(Math.round((s2 + s3)/2*1000)/1000.0); 
                        }
                    }                                    
            }
            else
                System.out.println("Wrong Format");
        }
        
        if(x.charAt(0) == '5') {
            if(chance == 5) {
                judg.case5(xy);
            }
            else
                System.out.println("Wrong Format");
        }
        if(x.charAt(0) == '4') {
            if(chance == 6) {
                
                judg.difficult(xy);
            }
            else
                System.out.println("Wrong Format");
        }
    }
}
class Judge {
    private String x;
    private double [] num = new double[10];
    public Judge(String x) {
        super();
        this.x = x;
    }

    public int judge() {
        String [] a = x.split(" ");
        int i = 0;
        if(a[0].matches("^[1-6][:]((-|[+])?(([1-9]+[0-9]{0,})|[0]))(\\.\\d+)?[,]((-|[+])?(([1-9]+[0-9]{0,})|[0]))(\\.\\d+)?") == true) {
            for(i = 1 ; i < a.length ; i++) {
                if(a[i].matches("^((-|[+])?(([1-9]+[0-9]{0,})|[0]))(\\.\\d+)?[,]((-|[+])?(([1-9]+[0-9]{0,})|[0]))(\\.\\d+)?") == false) {
                    System.out.print("Wrong Format");
                    System.exit(0);
                }
            }
            if(i == a.length&& i != 1)
                return i;//返回几个点]
            else if(i == 1) {
                System.out.print("wrong number of points");
                System.exit(0);
            }
        }
        else
            System.out.print("Wrong Format");//存在一个超出范围的没有设置
        return 0;
        }
    
    public double[] getNum(int dot) {
        String [] stringPlace = new String [12];
        double [] place = new double [12];
        int pos1 = 0;
        int pos2 = 0;
        String [] a = x.split(" ");//1:123,25;
            for(int i = 1; ;i ++) {
                if(x.charAt(i) == ',')
                    pos1 = i;
                if(x.charAt(i) == ' ') {
                    pos2 = i;break;
                }    
            }
            stringPlace[0] = a[0].substring(2,pos1);
            stringPlace[1] = a[0].substring(pos1+1,pos2);
            
            for(int i = 1,j = 2;i < dot ;i ++,j = j + 2) {
                String [] temp = a[i].split(",");
                stringPlace[j] = temp[0];
                stringPlace[j+1] = temp[1];
            }
            
            for(int i = 0 ;i < dot*2; i++) {
                place[i] = Double.valueOf(stringPlace[i]);
            }
            return place;
    }
    public double pointX(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
        double x = -((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4));
        return x;
    }
    public double pointY(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {//求焦点坐标
        double y = -((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4));
        return y;
    }
    public double getArea(double x1,double y1,double x2,double y2,double x3,double y3) {//已知三点求面积
        double area = Math.abs(x2*y3 + x3*y1 + x1*y2 - x2*y1 - x1*y3 - x3*y2);
        return area/2;
    }
    public double sumArea(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {//求四边形面积
        double s1 = getArea(x1,y1,x2,y2,x3,y3);
        double s2 = getArea(x1,y1,x3,y3,x4,y4);
        double s3 = getArea(x1,y1,x2,y2,x4,y4);
        double s4 = getArea(x2,y2,x3,y3,x4,y4);
        if(s1 + s2 <= s3 +s4)
            return s1 + s2;
        else {
            return s3 + s4;
        }
    }
    public boolean judgejudge(double X,double Y,double x1,double y1,double x2,double y2) {
        if((X-x1)*(X-x2) < 0&&(Y-y1)*(Y-y2) <= 0 ||(X-x1)*(X-x2) <= 0&&(Y-y1)*(Y-y2) < 0)
            return true;
        else
            return false;
    }
    public int triangle(double xy[]) {
        if(quadrabgle(xy) == 0||quadrabgle(xy) == 2) {//首先不是四边形
            double d1 = getArea(xy[0],xy[1],xy[2],xy[3],xy[4],xy[5]);//123
            double d2 = getArea(xy[0],xy[1],xy[2],xy[3],xy[6],xy[7]);//124
            double d3 = getArea(xy[2],xy[3],xy[4],xy[5],xy[6],xy[7]);//234
            double d4 = getArea(xy[0],xy[1],xy[4],xy[5],xy[6],xy[7]);//134
            if(d1 == 0&&d2 !=0 && (judgejudge(xy[2],xy[3],xy[0],xy[1],xy[3],xy[4])||(xy[0] == xy[2]&&xy[1] == xy[3]||xy[4] == xy[2]&&xy[5] == xy[3]))){
                return 1;
            }
            else if(d3 == 0&&d2 !=0 && (judgejudge(xy[4],xy[5],xy[2],xy[3],xy[6],xy[7])||(xy[2] == xy[4]&&xy[3] == xy[5]||xy[6] == xy[4]&&xy[7] == xy[5]))){
                return 2;
            }
            else if(d4 == 0&&d1 !=0 && (judgejudge(xy[6],xy[7],xy[0],xy[1],xy[4],xy[5])||(xy[0] == xy[6]&&xy[1] == xy[7]||xy[4] == xy[6]&&xy[5] == xy[7]))){
                return 3;
            }
            else if(d2 == 0&&d4 !=0 && (judgejudge(xy[0],xy[1],xy[2],xy[3],xy[6],xy[7])||(xy[2] == xy[0]&&xy[3] == xy[1]||xy[6] == xy[0]&&xy[7] == xy[1]))){
                return 4;
            }
            else
                return 0;
        }
        else
            return 0;
        
    }
    public int quadrabgle(double xy[]) {//判断四边形
        
        double x1 = pointX(xy[0],xy[1],xy[2],xy[3],xy[4],xy[5],xy[6],xy[7]);
        double y1 = pointY(xy[0],xy[1],xy[2],xy[3],xy[4],xy[5],xy[6],xy[7]);
        double x2 = pointX(xy[0],xy[1],xy[6],xy[7],xy[2],xy[3],xy[4],xy[5]);
        double y2 = pointY(xy[0],xy[1],xy[6],xy[7],xy[2],xy[3],xy[4],xy[5]);
        if((xy[0] == xy[2]&&xy[1] == xy[3])||(xy[2] == xy[4]&&xy[3] == xy[5])||(xy[4] == xy[6]&&xy[5] == xy[7])||(xy[0] == xy[6]&&xy[1] == xy[7])) {
            return 2; 
        }
        else if(getArea(xy[0],xy[1],xy[2],xy[3],xy[4],xy[5]) == 0 ||getArea(xy[0],xy[1],xy[2],xy[3],xy[6],xy[7]) == 0 ||
                getArea(xy[6],xy[7],xy[2],xy[3],xy[4],xy[5]) == 0 ||getArea(xy[0],xy[1],xy[3],xy[4],xy[6],xy[7]) == 0 )
            return 0;
        else if(getArea(xy[4],xy[5],xy[0],xy[1],xy[2],xy[3]) == getArea(xy[6],xy[7],xy[0],xy[1],xy[2],xy[3])&&getArea(xy[2],xy[3],xy[0],xy[1],xy[6],xy[7]) != getArea(xy[4],xy[5],xy[0],xy[1],xy[6],xy[7])&&
                (judgejudge(x2,y2,xy[2],xy[3],xy[4],xy[5])==true||judgejudge(x1,y1,xy[0],xy[1],xy[6],xy[7])==true)) //12和34平行
            return 0;
        else if(getArea(xy[4],xy[5],xy[0],xy[1],xy[2],xy[3]) != getArea(xy[6],xy[7],xy[0],xy[1],xy[2],xy[3])&&getArea(xy[2],xy[3],xy[0],xy[1],xy[6],xy[7]) == getArea(xy[4],xy[5],xy[0],xy[1],xy[6],xy[7])&&
                (judgejudge(x2,y2,xy[2],xy[3],xy[4],xy[5])==true&&judgejudge(x1,y1,xy[0],xy[1],xy[6],xy[7])==true)) //14和23平行
            return 0;
        else if(getArea(xy[4],xy[5],xy[0],xy[1],xy[2],xy[3]) == getArea(xy[6],xy[7],xy[0],xy[1],xy[2],xy[3])&&getArea(xy[2],xy[3],xy[0],xy[1],xy[6],xy[7]) == getArea(xy[4],xy[5],xy[0],xy[1],xy[6],xy[7])) {
            double x = pointX(xy[0],xy[1],xy[6],xy[7],xy[2],xy[3],xy[4],xy[5]);
            double y = pointY(xy[0],xy[1],xy[6],xy[7],xy[2],xy[3],xy[4],xy[5]);
            if(judgejudge(x,y,xy[0],xy[1],xy[6],xy[7])==true)
                return 0;
            else 
                return 1;
        }
        else if((judgejudge(x1,y1,xy[0],xy[1],xy[2],xy[3])==false&&judgejudge(x1,y1,xy[4],xy[5],xy[6],xy[7])==false)||(judgejudge(x2,y2,xy[2],xy[3],xy[4],xy[5])==false&&judgejudge(x1,y1,xy[0],xy[1],xy[6],xy[7])==false)) {
            double d1 = getArea(xy[0],xy[1],xy[2],xy[3],xy[4],xy[5]);//123
            double d2 = getArea(xy[0],xy[1],xy[2],xy[3],xy[6],xy[7]);//124
            double d3 = getArea(xy[2],xy[3],xy[4],xy[5],xy[6],xy[7]);//234
            double d4 = getArea(xy[0],xy[1],xy[4],xy[5],xy[6],xy[7]);//134
            if(d1 != 0&&d2 != 0&&d3 != 0&&d4 != 0)
                return 1;
            else 
                return 0;
        }
        else
            return 0;
    }
    public boolean parallelogram(double xy[]) {//判断平行四边形
        
        if(quadrabgle(xy) == 1 ) {
            
            double a = Math.pow((xy[0]-xy[2]),2)+Math.pow((xy[1]-xy[3]),2);
            double b = Math.pow((xy[2]-xy[4]),2)+Math.pow((xy[3]-xy[5]),2);
            double c = Math.pow((xy[4]-xy[6]),2)+Math.pow((xy[5]-xy[7]),2);
            double d = Math.pow((xy[6]-xy[0]),2)+Math.pow((xy[7]-xy[1]),2);
            
            if(a == c&&b == d)
                return true;
            else
                return false;
        }
        
        else
            return false;
    }
    
    public boolean rhombus(double xy[]) {//判断是菱形
        if(parallelogram(xy) == true) {
            double a = Math.pow((xy[0]-xy[2]),2)+Math.pow((xy[1]-xy[3]),2);
            double b = Math.pow((xy[2]-xy[4]),2)+Math.pow((xy[3]-xy[5]),2);
            double c = Math.pow((xy[4]-xy[6]),2)+Math.pow((xy[5]-xy[7]),2);
            double d = Math.pow((xy[6]-xy[0]),2)+Math.pow((xy[7]-xy[1]),2);
            
            if(a == b&&a == d)
                return true;
            else
                return false;
        }
        else
            return false;
    }
    public boolean rectangle(double xy[]) {//判断是矩形
        if(parallelogram(xy) == true) {
            double oppo1 = Math.pow((xy[0]-xy[4]),2)+Math.pow((xy[1]-xy[5]),2);
            double oppo2 = Math.pow((xy[2]-xy[6]),2)+Math.pow((xy[3]-xy[7]),2);            
            
            if(oppo1 == oppo2)
                return true;
            else
                return false;
        }
        else
            return false;
    }
    public boolean square(double xy[]) {//判断是正方形
        if(rectangle(xy) == true) {
            double a = Math.pow((xy[0]-xy[2]),2)+Math.pow((xy[1]-xy[3]),2);
            double b = Math.pow((xy[2]-xy[4]),2)+Math.pow((xy[3]-xy[5]),2);    
            
            if(a == b)
                return true;
            else
                return false;
        }
        else
            return false;
    }
    public boolean highAndShort(double xy[]) {//判断凸凹四边形:true是凸的,false是凹的
        if(quadrabgle(xy) == 1 ) {
            double s1 = 0,s2 = 0,s3 = 0,s4 = 0;
            s1 = Math.abs(xy[2]*xy[5]+xy[4]*xy[1]+xy[0]*xy[3]-xy[2]*xy[1]-xy[0]*xy[5]-xy[4]*xy[3]);//123
            s2 = Math.abs(xy[2]*xy[7]+xy[6]*xy[1]+xy[0]*xy[3]-xy[2]*xy[1]-xy[0]*xy[7]-xy[6]*xy[3]);//124
            s3 = Math.abs(xy[2]*xy[5]+xy[4]*xy[7]+xy[6]*xy[3]-xy[2]*xy[7]-xy[6]*xy[5]-xy[4]*xy[3]);//234
            s4 = Math.abs(xy[4]*xy[7]+xy[6]*xy[1]+xy[0]*xy[5]-xy[4]*xy[1]-xy[0]*xy[7]-xy[6]*xy[5]);//134
            
            if(s1+s4 == s2+s3) 
                return true;
            else
                return false;
            }
        else
            return false;
    }
    public void case5(double XY[]) {
        double xy[] = {XY[2],XY[3],XY[4],XY[5],XY[6],XY[7],XY[8],XY[9]};
        if(quadrabgle(xy) == 1) {
            double d1 = getArea(XY[0],XY[1],xy[0],xy[1],xy[2],xy[3]);
            double d2 = getArea(XY[0],XY[1],xy[2],xy[3],xy[4],xy[5]);
            double d3 = getArea(XY[0],XY[1],xy[4],xy[5],xy[6],xy[7]);
            double d4 = getArea(XY[0],XY[1],xy[6],xy[7],xy[0],xy[1]);
            double s = sumArea(xy[0],xy[1],xy[2],xy[3],xy[4],xy[5],xy[6],xy[7]);
            if((d1 == 0 ||d2 == 0 ||d3 == 0 ||d4 == 0 )&&(s == d1 + d2 + d3 + d4))
                System.out.println("on the quadrilateral");
            else if(s == d1 + d2 + d3 + d4) 
                System.out.println("in the quadrilateral");
            else
                System.out.println("outof the quadrilateral");
        }
        else if(triangle(xy) == 1) {
            double d1 = getArea(XY[0],XY[1],xy[0],xy[1],xy[4],xy[5]);
            double d2 = getArea(XY[0],XY[1],xy[0],xy[1],xy[6],xy[7]);
            double d3 = getArea(XY[0],XY[1],xy[4],xy[5],xy[6],xy[7]);
            double s = getArea(xy[0],xy[1],xy[4],xy[5],xy[6],xy[7]);
            if((d1 == 0 ||d2 == 0 ||d3 == 0 )&&s == d1 + d2 + d3 )
                System.out.println("on the triangle");
            else if(s == d1 + d2 + d3 ) 
                System.out.println("in the triangle");
            else
                System.out.println("outof the triangle");
        }
        else if(triangle(xy) == 2) {
            double d1 = getArea(XY[0],XY[1],xy[0],xy[1],xy[2],xy[3]);
            double d2 = getArea(XY[0],XY[1],xy[2],xy[3],xy[6],xy[7]);
            double d3 = getArea(XY[0],XY[1],xy[0],xy[1],xy[6],xy[7]);
            double s = getArea(xy[0],xy[1],xy[2],xy[3],xy[6],xy[7]);
            if((d1 == 0 ||d2 == 0 ||d3 == 0 )&&s == d1 + d2 + d3 )
                System.out.println("on the triangle");
            else if(s == d1 + d2 + d3 ) 
                System.out.println("in the triangle");
            else
                System.out.println("outof the triangle");
        }
        else if(triangle(xy) == 3) {
            double d1 = getArea(XY[0],XY[1],xy[0],xy[1],xy[2],xy[3]);
            double d2 = getArea(XY[0],XY[1],xy[0],xy[1],xy[4],xy[5]);
            double d3 = getArea(XY[0],XY[1],xy[2],xy[3],xy[4],xy[5]);
            double s = getArea(xy[0],xy[1],xy[2],xy[3],xy[4],xy[5]);
            if((d1 == 0 ||d2 == 0 ||d3 == 0 )&&s == d1 + d2 + d3 )
                System.out.println("on the triangle");
            else if(s == d1 + d2 + d3 ) 
                System.out.println("in the triangle");
            else
                System.out.println("outof the triangle");
        }
        else if(triangle(xy) == 4) {
            double d1 = getArea(XY[0],XY[1],xy[2],xy[3],xy[6],xy[7]);
            double d2 = getArea(XY[0],XY[1],xy[2],xy[3],xy[4],xy[5]);
            double d3 = getArea(XY[0],XY[1],xy[4],xy[5],xy[6],xy[7]);
            double s = getArea(xy[2],xy[3],xy[4],xy[5],xy[6],xy[7]);
            if((d1 == 0 ||d2 == 0 ||d3 == 0 )&&s == d1 + d2 + d3 )
                System.out.println("on the triangle");
            else if(s == d1 + d2 + d3 ) 
                System.out.println("in the triangle");
            else
                System.out.println("outof the triangle");
        }
        else
            System.out.println("not a quadrilateral or triangle");
    }
    public void difficult(double XY[]) {//这里面有6个点,需要将前两个排除
        double [] xy ={XY[4],XY[5],XY[6],XY[7],XY[8],XY[9],XY[10],XY[11]};//可能存在问题
        if(XY[0] == XY[3]&&XY[1] == XY[4]) {//前面两个点重合
            System.out.println("points coincide");
            System.exit(0);
        }
        if(quadrabgle(xy) == 1) {//如果是四边形
            //点到直线的距离,存在四条
            double s = sumArea(xy[0],xy[1],xy[2],xy[3],xy[4],xy[5],xy[6],xy[7]);//求四边形面积
            double d1 = getArea(XY[0],XY[1],XY[2],XY[3],xy[0],xy[1]);//1
            double d2 = getArea(XY[0],XY[1],XY[2],XY[3],xy[2],xy[3]);//2
            double d3 = getArea(XY[0],XY[1],XY[2],XY[3],xy[4],xy[5]);//3
            double d4 = getArea(XY[0],XY[1],XY[2],XY[3],xy[6],xy[7]);//4
            int num12 = 0,num23 = 0,num34 = 0,num41 = 0,sum = 0;
            //求焦点坐标
            double X1 = pointX(XY[0],XY[1],XY[2],XY[3],xy[0],xy[1],xy[2],xy[3]);
            double Y1 = pointY(XY[0],XY[1],XY[2],XY[3],xy[0],xy[1],xy[2],xy[3]);//12
            double X2 = pointX(XY[0],XY[1],XY[2],XY[3],xy[2],xy[3],xy[4],xy[5]);
            double Y2 = pointY(XY[0],XY[1],XY[2],XY[3],xy[2],xy[3],xy[4],xy[5]);//23
            double X3 = pointX(XY[0],XY[1],XY[2],XY[3],xy[4],xy[5],xy[6],xy[7]);
            double Y3 = pointY(XY[0],XY[1],XY[2],XY[3],xy[4],xy[5],xy[6],xy[7]);//34
            double X4 = pointX(XY[0],XY[1],XY[2],XY[3],xy[6],xy[7],xy[0],xy[1]);
            double Y4 = pointY(XY[0],XY[1],XY[2],XY[3],xy[6],xy[7],xy[0],xy[1]);//41
            //判断是否在线段内
            if(judgejudge(X1,Y1,xy[0],xy[1],xy[2],xy[3]))//判断有几个焦点
                num12++;//与12
            if(judgejudge(X2,Y2,xy[2],xy[3],xy[4],xy[5]))
                num23++;//23
            if(judgejudge(X3,Y3,xy[4],xy[5],xy[6],xy[7]))
                num34++;//34
            if(judgejudge(X4,Y4,xy[6],xy[7],xy[0],xy[1]))
                num41++;//41
            sum = num12 + num23 + num34 + num41;
            if((d1 == 0&&d4 == 0)||(d1 == 0&&d2 == 0)||(d2 == 0&&d3 == 0)||(d3 == 0&&d4 == 0)) {//表示两条线重合了
                System.out.println("The line is coincide with one of the lines");
                System.exit(0);
            }
            
            else if(d1 != 0&&d2 != 0&&d3 != 0&&d4 != 0) {//表示与顶点一个都不相交
                
                System.out.println(sum);
                if(num12 == 1&&num23 == 1&&num41 == 0&&num41 == 0) {
                    double s1 = getArea(X1,Y1,X2,Y2,xy[2],xy[3]);
                    System.out.print(s1 + " ");
                    System.out.println(s - s1);
                    System.exit(0);
                }
                else if(num23 == 1&&num34 == 1&&num41 == 0&& num12 == 0) {
                    double s1 = getArea(X2,Y2,X3,Y3,xy[4],xy[5]);
                    System.out.print(s1 + " ");
                    System.out.println(s - s1);
                    System.exit(0);
                }
                else if(num34 == 1&&num41 == 1&& num12 == 0&& num23 == 0) {
                    double s1 = getArea(X3,Y3,X4,Y4,xy[6],xy[7]);
                    System.out.print(s1 + " ");
                    System.out.println(s - s1);
                    System.exit(0);
                }
                else if(num41 == 1&&num12 == 1&& num23 == 0&& num34 == 0) {
                    double s1 = getArea(X4,Y4,X1,Y1,xy[0],xy[1]);
                    System.out.print(s1 + " ");
                    System.out.println(s - s1);
                    System.exit(0);
                }
                else if(num12 == 1&&num34 == 1&&num23 == 0&&num41 == 0) {//形成两个四边形
                    double s1 = sumArea(X1,Y1,X3,Y3,xy[0],xy[1],xy[6],xy[7]);
                    double s2 = sumArea(X1,Y1,X3,Y3,xy[2],xy[3],xy[4],xy[5]);
                    if(s1 <= s2) {
                        System.out.print(s1 + " ");
                        System.out.println(s - s1);
                    }
                    else {
                        System.out.print(s2 + " ");
                        System.out.println(s - s2);
                    }    
                }
                else if(num23 == 1&&num41 == 1&&num12 == 0&&num34 == 0) {
                    double s1 = sumArea(X2,Y2,X4,Y4,xy[0],xy[1],xy[2],xy[3]);
                    double s2 = sumArea(X2,Y2,X4,Y4,xy[4],xy[5],xy[6],xy[7]);
                    if(s1 <= s2) {
                        System.out.print(s1 + " ");
                        System.out.println(s - s1);
                    }
                    else {
                        System.out.print(s2 + " ");
                        System.out.println(s - s2);
                    }    
                }
            }
            else if(d1 == 0&&d3 == 0||d2 == 0&&d4 == 0) {//存在连个端点相交,就是对角线相交
                double s1 = 0,s2 = 0,s3 = 0,s4 = 0;
                s1 = Math.abs(xy[2]*xy[5]+xy[4]*xy[1]+xy[0]*xy[3]-xy[2]*xy[1]-xy[0]*xy[5]-xy[4]*xy[3]);//123
                s2 = Math.abs(xy[2]*xy[7]+xy[6]*xy[1]+xy[0]*xy[3]-xy[2]*xy[1]-xy[0]*xy[7]-xy[6]*xy[3]);//124
                s3 = Math.abs(xy[2]*xy[5]+xy[4]*xy[7]+xy[6]*xy[3]-xy[2]*xy[7]-xy[6]*xy[5]-xy[4]*xy[3]);//234
                s4 = Math.abs(xy[4]*xy[7]+xy[6]*xy[1]+xy[0]*xy[5]-xy[4]*xy[1]-xy[0]*xy[7]-xy[6]*xy[5]);//134
                System.out.println("2");
                if(highAndShort(xy)) {            
                    System.out.print(s/2+" ");
                    System.out.println(s/2);
                }
                else if(!highAndShort(xy)&&d1 == 0&&d3 == 0) {
                    if(s1+s4 > s2+s3) {
                        System.out.print(s/2+" ");
                        System.out.println(s/2);
                    }    
                }
                else if(!highAndShort(xy)&&d2 == 0&&d4 == 0) {
                    if(s1+s4 < s2+s3) {
                        System.out.print(s/2+" ");
                        System.out.println(s/2);
                    }    
                }
            }
            //只存在一个端点
            else if(d1 == 0&&d2 != 0&&d3 != 0&&d4 != 0) {//并且要与四边形有两个交点
                System.out.println(sum);
                if(num23 == 1 && num12 == 0&& num34 ==0 &&num41 == 0) {
                    double s1 = getArea(xy[0],xy[1],xy[2],xy[3],X2,Y2);
                    double s2 = s - s1;
                    if(s1 <= s2) {
                        System.out.print(s1 + " ");
                        System.out.println(s - s1);
                    }
                    else {
                        System.out.print(s2 + " ");
                        System.out.println(s - s2);
                    }
                }
                if(num34 == 1&&num12 == 0&& num23 ==0 &&num41 == 0) {
                    double s1 = getArea(xy[0],xy[1],xy[6],xy[7],X3,Y3);
                    double s2 = s - s1;
                    if(s1 <= s2) {
                        System.out.print(s1 + " ");
                        System.out.println(s - s1);
                    }
                    else {
                        System.out.print(s2 + " ");
                        System.out.println(s - s2);
                    }
                }
            }
            else if(d1 != 0&&d2 == 0&&d3 != 0&&d4 != 0) {//并且要与四边形有两个交点
                
                System.out.println(sum);
                if(num34 == 1&&num12 == 0&& num23 ==0 &&num41 == 0) {
                    double s1 = getArea(xy[2],xy[3],xy[4],xy[5],X3,Y3);
                    double s2 = s - s1;
                    if(s1 <= s2) {
                        System.out.print(s1 + " ");
                        System.out.println(s - s1);
                    }
                    else {
                        System.out.print(s2 + " ");
                        System.out.println(s - s2);
                    }
                }
                if(num41 == 1&&num12 == 0&& num23 ==0 &&num34 == 0) {
                    double s1 = getArea(xy[0],xy[1],xy[2],xy[3],X4,Y4);
                    double s2 = s - s1;
                    if(s1 <= s2) {
                        System.out.print(s1 + " ");
                        System.out.println(s - s1);
                    }
                    else {
                        System.out.print(s2 + " ");
                        System.out.println(s - s2);
                    }
                }
            }
            else if(d1 != 0&&d2 != 0&&d3 == 0&&d4 != 0) {//并且要与四边形有两个交点
                
                System.out.println(sum);
                if(num12 == 1&&num23 == 0&& num34 ==0 &&num41 == 0) {
                    double s1 = getArea(xy[2],xy[3],xy[4],xy[5],X1,Y1);
                    double s2 = s - s1;
                    if(s1 <= s2) {
                        System.out.print(s1 + " ");
                        System.out.println(s - s1);
                    }
                    else {
                        System.out.print(s2 + " ");
                        System.out.println(s - s2);
                    }
                }
                if(num41 == 1&&num12 == 0&& num23 ==0 &&num34 == 0) {
                    double s1 = getArea(xy[4],xy[5],xy[6],xy[7],X4,Y4);
                    double s2 = s - s1;
                    if(s1 <= s2) {
                        System.out.print(s1 + " ");
                        System.out.println(s - s1);
                    }
                    else {
                        System.out.print(s2 + " ");
                        System.out.println(s - s2);
                    }
                }
            }
            else if(d1 != 0&&d2 != 0&&d3 != 0&&d4 == 0) {//并且要与四边形有两个交点
                
                System.out.println(sum);
                if(num12 == 1&&num23 == 0&& num34 ==0 &&num41 == 0) {
                    double s1 = getArea(xy[6],xy[7],xy[0],xy[1],X1,Y1);
                    double s2 = s - s1;
                    if(s1 <= s2) {
                        System.out.print(s1 + " ");
                        System.out.println(s - s1);
                    }
                    else {
                        System.out.print(s2 + " ");
                        System.out.println(s - s2);
                    }
                }
                if(num23 == 1&&num12 == 0&& num34 ==0 &&num41 == 0) {
                    double s1 = getArea(xy[6],xy[7],xy[4],xy[5],X2,Y2);
                    double s2 = s - s1;
                    if(s1 <= s2) {
                        System.out.print(s1 + " ");
                        System.out.println(s - s1);
                    }
                    else {
                        System.out.print(s2 + " ");
                        System.out.println(s - s2);
                    }
                }
            }
        }
    }
}
View Code

踩坑心得:刚开始的四边形的公式就用错了,并且还自己判断特别复杂,推荐不要自己判断,并且,一定要先设计好第四个点的思路,这个会很复杂,对于细节,一定要实现好,我就是第四个没有实现完全,推                    荐大家多多去考虑。很多的公式要使用那种不需要考虑斜率的,这个是非常有必要的。 

总结:在这段时间的学习中,更多的是需要改变C语言的写法,从C语言中走向面向对象设计,实现很多方法的使用,也多多使用继承和多态,这样可以实现代码的复用,能够大大减少代码的数量,同时也要改               变当初的写程序的方法,比如说以前都是直接拿起来写,我们需要,刚开始就设计好,可以大大减少时间,而不是盲目的去写,思路上也变清晰了在这里,我们学习了很多,比如说继承多态,向上转型,             向下转型,以及很多字符串的用法,还有正则表达式,这是一个比较好的方法,值得去推荐,相比于之前来说,现在对于类与类之间的设计,变得更加清晰明确,对于面向对象设计也越来越近,希望自己             能够以后更加努力加油,也希望大家能够一起加油!

 

 

标签:总结,题目,scan,double,System,else,xy,out
来源: https://www.cnblogs.com/en-em-em/p/16213070.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有