EDUL的第一次博客

一、前言:

  (1)这三次PTA作业考察了很多:字符串的输出和输入,转化变量的类型,逻辑思维能力等,其中最重要的当属面向对象的编程方式,以前接触过C语言,并编写过一些普通代码,但java代码编写和C语言有一些区别,这三次PTA作业让我慢慢从C语言转变为java。

  (2)我刚刚接触java还不熟悉类的使用,需要花很多时间来学习新类的使用。

  (3)PTA的难度随题目慢慢增加,刚开始利用C语言的知识就能轻松解决,但第三次作业中,由于对java语言的不熟练,就会有很多测试点过不了。

二、设计与分析:

1.串口字符解析

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

输入格式:

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

输出格式:

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

import java.util.Scanner;
public class Main {
	public static void main(String[] args){
		Scanner in=new Scanner(System.in);
		String a=in.next();
        int start=0,i=0,num=1,sum=0;
		boolean parity=false,validate=false;
		if(a.length()<11){
			System.out.print("null data");
			return;
		}
		if(a.matches("^[1]*$")){
			System.out.print("null data");
			return;
		}
		for(start=0;start<a.length()-10;start++){
			if(a.charAt(start)=='0'){
				System.out.print(num+":");
				num++;
				if(a.charAt(start+10)=='0'){
					validate=false;
				}else {
					validate=true;
					sum=0;
					for(i=start+1;i<start+9;i++){
						if(a.charAt(i)=='1'){
							sum++;
						}
					}
					if(sum%2==0){
						if(a.charAt(start+9)=='1'){
							parity=true;
						}else {
							parity=false;
						}
					}else {
						if(a.charAt(start+9)=='0'){
							parity=true;
						}else {
							parity=false;
						}
					}
				}
				if(validate==true) {
					if(parity==true) {
						for(i=start+1;i<start+9;i++){
							System.out.print(a.charAt(i));
						}
						System.out.print("\n");
					}else {
						System.out.println("parity check error");
					}
				}else {
					System.out.println("validate error");
				}
				start=start+10;
			}
		}
    }
}
  
这题,只写一个main就能实现功能。首先用string字符串获取输入后分析长度小于11的或者字符串中有非法字符的直接输出null data。再对合格的数据进行01判断、奇偶校验根据各自的校验结果来判定输出validate error还是parity check error用了类似C语言的编程方式
 

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

  输入连个点的坐标,计算两点之间的距离

输入格式:

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

输出格式:

  计算所得的两点之间的距离。例如:1.4142135623730951

  

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        String input=in.nextLine();
        String point[]=input.split(" ");
        String num[]=null;
        int cot=0;
        for(String i:point) {
            num=i.split(",");
            for(String j:num) {
                if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) {
                    System.out.print("Wrong Format");
                    System.exit(0);
                }
            }
        }
        if(point.length!=2) {
            System.out.print("wrong number of points");
            System.exit(0);
        }
        num=point[0].split(",");
        double a=Double.valueOf(num[0]);
        double b=Double.valueOf(num[1]);
        num=point[1].split(",");
        double c=Double.valueOf(num[0]);
        double d=Double.valueOf(num[1]);
        if(a==c&&b==d){
            System.out.print("Wrong Format");
            return;
        }
        System.out.print(Math.sqrt((a-c)*(a-c)+(b-d)*(b-d)));
    }
}

 

  这是题目后亮题的基础功能,至于要求两个点之间的距离,首先用字符串表示数据,然后用split函数分开每一个点,最后用para方法强制转化类型,从String转为double型。在计算计算两点之间的距离。

 

3.点线形系列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);
        double x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0,distance=0, quantity =0,slope=0;
        String c=in.nextLine();
        int choice=Integer.parseInt(c.substring(0,1));
        for(int i=0;i<c.length()-1;i++) {
            if(c.charAt(i)==' ') {              //判断点的个数
                quantity++;
            }
        }
        for(int j=0;j<c.length()-1;j++){
            if((c.charAt(j)=='+'&&c.charAt(j+1)=='+')||
                    (c.charAt(j)=='-'&&c.charAt(j+1)=='-')||
                    (c.charAt(j)=='+'&&c.charAt(j+1)=='-')||
                    (c.charAt(j)=='-'&&c.charAt(j+1)=='+')||
                    (c.charAt(j)=='.'&&c.charAt(j+1)==',')||
                    (c.charAt(j)==','&&c.charAt(j+1)=='.')||
                    (c.charAt(j)=='0'&&c.charAt(j+1)=='0')||
                    (c.charAt(j)=='.'&&c.charAt(j+1)==' ')||
                    (c.charAt(j)==' '&&c.charAt(j+1)=='.')||
                    (c.charAt(j)=='.'&&c.charAt(j+1)=='.')||
                    (c.charAt(j)==' '&&c.charAt(j+1)==',')||
                    (c.charAt(j)==','&&c.charAt(j+1)==' ')||
                    (c.charAt(j)==' '&&c.charAt(j+1)==' ')||
                    (c.charAt(j)==','&&c.charAt(j+1)==',')||
                    c.charAt(0)=='.'||
                    c.charAt(0)==','||
                    quantity ==0){
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        if(quantity ==0){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        if(choice>5||choice<1) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        if(choice==1) {
            String[] dot=c.split(" ");
            String[] dot1=dot[0].split(",");
            String[] dot2=dot[1].split(",");
            x1=Double.parseDouble(dot1[0].substring(2, dot1[0].length()));
            y1=Double.parseDouble(dot1[1]);
            x2=Double.parseDouble(dot2[0]);
            y2=Double.parseDouble(dot2[1]);
            if(quantity!=1){
                System.out.println("wrong number of points");
            }
            else if(x1==x2&&y1==y2){
                    System.out.println("points coincide");
            }
            else if((x1==x2)&&(y1!=y2)) {
                System.out.println("Slope does not exist");
            }
            else {
                slope=(y1-y2)/(x1-x2);
                System.out.println(slope);
            }
        }
        if(choice==2) {
            String[] dot = c.split(" ");
            String[] dot1 = dot[0].split(",");
            String[] dot2 = dot[1].split(",");
            String[] dot3 = dot[2].split(",");
            x1=Double.parseDouble(dot1[0].substring(2, dot1[0].length()));
            y1=Double.parseDouble(dot1[1]);
            x2=Double.parseDouble(dot2[0]);
            y2=Double.parseDouble(dot2[1]);
            x3=Double.parseDouble(dot3[0]);
            y3=Double.parseDouble(dot3[1]);
            distance=Math.abs((y2-y3)*x1+(x3-x2)*y1+x2*y3-y2*x3)/Math.sqrt((y2-y3)*(y2-y3)+(x3-x2)*(x3-x2));
            if(quantity!=2) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            else if((x2==x3&&y2==y3)) {
                    System.out.println("points coincide");
                    System.exit(0);
            }
            else {
                System.out.println(distance);
                System.exit(0);
            }
        }
        if(choice==3) {
            String[] dot=c.split(" ");
            String[] dot1=dot[0].split(",");
            String[] dot2 = dot[1].split(",");
            String[] dot3 = dot[2].split(",");
            x1=Double.parseDouble(dot1[0].substring(2, dot1[0].length()));
            y1=Double.parseDouble(dot1[1]);
            x2=Double.parseDouble(dot2[0]);
            y2=Double.parseDouble(dot2[1]);
            x3=Double.parseDouble(dot3[0]);
            y3=Double.parseDouble(dot3[1]);
            slope=(y1-y2)/(x1-x2);
            distance=(y3-y2)/(x3-x2);
            if(quantity!=2) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            else if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) {
                System.out.println("points coincide");
                System.exit(0);
            }
            else if((x1==x2)&&(x1==x3)||(x1==x3)&&(x1==x2)||(x2==x3)&&(x2==x1)) {
                System.out.println("true");
                System.exit(0);
            }
            else if((x1==x2)&&(x1!=x3)||(x1==x3)&&(x1!=x2)||(x2==x3)&&(x2!=x1)) {
                System.out.println("false");
                System.exit(0);
            }
            else if(slope==distance){
                System.out.println("true");
                System.exit(0);
            }
            else {
                System.out.println("false");
                System.exit(0);
            }
        }
        if(choice==4) {
            String[] dot=c.split(" ");
            String[] dot1=dot[0].split(",");
            String[] dot2=dot[1].split(",");
            String[] dot3=dot[2].split(",");
            String[] dot4=dot[3].split(",");
            x1=Double.parseDouble(dot1[0].substring(2, dot1[0].length()));
            y1=Double.parseDouble(dot1[1]);
            x2=Double.parseDouble(dot2[0]);
            y2=Double.parseDouble(dot2[1]);
            x3=Double.parseDouble(dot3[0]);
            y3=Double.parseDouble(dot3[1]);
            x4=Double.parseDouble(dot4[0]);
            y4=Double.parseDouble(dot4[1]);
            slope=(y1-y2)/(x1-x2);
            distance=(y4-y3)/(x4-x3);
            if(quantity!=3){
                System.out.println("wrong number of points");
                System.exit(0);
            }
            else if((x1==x2&&y1==y2)||(x3==x4&&y3==y4)) {
                System.out.println("points coincide");
                System.exit(0);
            }
            else if((x1==x2)&&(y1!=y2)&&(x3==x4)&&(y3!=y4)) {
                System.out.println("true");
                System.exit(0);
            }
            else if((x1==x2)&&(x3!=x4)) {
                System.out.println("false");
                System.exit(0);
            }
            else if((x3==x4)&&(x1!=x2)) {
                System.out.println("false");
                System.exit(0);
            }
            else if(slope==distance){
                System.out.println("true");
                System.exit(0);
            }
            else {
                System.out.println("false");
                System.exit(0);
            }
        }
        if(choice==5) {
            String[] dot=c.split(" ");
            String[] dot1=dot[0].split(",");
            String[] dot2=dot[1].split(",");
            String[] e=dot[2].split(",");
            String[] g=dot[3].split(",");
            x1=Double.parseDouble(dot1[0].substring(2, dot1[0].length()));
            y1=Double.parseDouble(dot1[1]);
            x2=Double.parseDouble(dot2[0]);
            y2=Double.parseDouble(dot2[1]);
            x3=Double.parseDouble(e[0]);
            y3=Double.parseDouble(e[1]);
            x4=Double.parseDouble(g[0]);
            y4=Double.parseDouble(g[1]);
            slope=(y1-y2)/(x1-x2);
            distance=(y4-y3)/(x4-x3);
            float x=(float)((y1+slope*x1-y3-distance*x3)/(distance-slope));
            float y=(float)(y1+slope*(x-x1));
            if(quantity !=3) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            else if((x1==x2&&y1==y2)||(x3==x4&&y3==y4)) {
                System.out.println("points coincide");
                System.exit(0);
            }
            else if((x1==x2&&y1!=y2)&&(x3==x4&&y3!=y4)) {
                System.out.println("is parallel lines,have no intersection point");
                System.exit(0);
            }
            else if(slope==distance) {
                System.out.println("is parallel lines,have no intersection point");
                System.exit(0);
            }
            else if((x3==x4)&&(x1!=x2)) {
                x=(float)(x3);
                y=(float)(((y2-y1)/(x2-x1))*x3+y1-((y2-y1)/(x2-x1))*x1);
                System.out.print(x+","+y+" ");
                if((x>x1&&x<x2)&&((y>y1&&y<y2)||(y<y1&&y>y2))||(x<x1&&x>x2)&&((y<y1&&y>y2)||(y>y1&&y<y2))||(x>x3&&x<x4)&&((y>y3)&&(y<y4)||(y<y3&&y>y4))||(x<x3&&x>x4)&&((y<y3&&y>y4))||(y>y3)&&(y<y4)) {
                    System.out.println("true");
                    System.exit(0);
                }
                else{
                    System.out.println("false");
                    System.exit(0);
                }
            }
            else if((x1==x2)&&(x3!=x4)) {
                x=(float)(x1);
                y=(float)(((y4-y3)/(x4-x3))*x1+y3-((y4-y3)/(x4-x3))*x3);
                System.out.print(x+","+y+" ");
                if((x>x1&&x<x2)&&((y>y1&&y<y2)||(y<y1&&y>y2))||(x<x1&&x>x2)&&((y<y1&&y>y2)||(y>y1&&y<y2))||(x>x3&&x<x4)&&((y>y3)&&(y<y4)||(y<y3&&y>y4))||(x<x3&&x>x4)&&((y<y3&&y>y4))||(y>y3)&&(y<y4)) {
                    System.out.println("true");
                    System.exit(0);
                }
                else{
                    System.out.println("false");
                    System.exit(0);
                }
            }
            else {
                System.out.print(x+","+y+" ");
                if((x>x1&&x<x2)&&((y>y1&&y<y2)||(y<y1&&y>y2))||(x<x1&&x>x2)&&((y<y1&&y>y2)||(y>y1&&y<y2))||(x>x3&&x<x4)&&((y>y3)&&(y<y4)||(y<y3&&y>y4))||(x<x3&&x>x4)&&((y<y3&&y>y4))||(y>y3)&&(y<y4)) {
                    System.out.println("true");
                    System.exit(0);
                }
                else{
                    System.out.println("false");
                    System.exit(0);
                }
            }
        }
    }
}

  

  本题是上一题的升级,各方面要求比上一体加深,同时难度也加大,首先用字符串表示数据,然后用split函数分开每一个点,最后用para方法强制转化类型,从String转为double型,然后在一个一个选项进行计算和构思。题目上就给出了5个功能点,所以我将5个功能分别写成Choice1-Choice5五类,在排除非法输入后对首个数据判断,选择Choice1-Choice5,选择后实现相应功能。

 

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

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

输入格式:

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

输出格式:

  基本输出格式见每种选项的描述。
  异常情况输出:
  如果不符合基本格式,输出"Wrong Format"。
  如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
  如果输入的三个点无法构成三角形,输出"data error"。
  注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333333,1.0按格式输出为1.0

  选项4中所输入线的两个点坐标重合,输出"points coincide",

import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double x1=0,x2=0,y1=0,y2=0,x3=0,x4=0,y3=0,y4=0,x5=0,y5=0,distance=0,slope=0,quantity=0;
        String a = in.nextLine();
        int choice=Integer.parseInt(a.substring(0,1));
        for(int i=0;i<a.length()-1;i++) {
            if(a.charAt(i)==' ') {
                quantity++;
            }
        }
        if(choice>5||choice<1) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        else if(quantity==0){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        for(int j=0;j<a.length()-1;j++){
            if((a.charAt(j)=='+'&&a.charAt(j+1)=='+')||
                    (a.charAt(j)=='-'&&a.charAt(j+1)=='-')||
                    (a.charAt(j)=='+'&&a.charAt(j+1)=='-')||
                    (a.charAt(j)=='-'&&a.charAt(j+1)=='+')||
                    (a.charAt(j)=='.'&&a.charAt(j+1)==',')||
                    (a.charAt(j)==','&&a.charAt(j+1)=='.')||
                    (a.charAt(j)=='0'&&a.charAt(j+1)=='0')||
                    (a.charAt(j)=='.'&&a.charAt(j+1)==' ')||
                    (a.charAt(j)==' '&&a.charAt(j+1)=='.')||
                    (a.charAt(j)=='.'&&a.charAt(j+1)=='.')||
                    (a.charAt(j)==' '&&a.charAt(j+1)==',')||
                    (a.charAt(j)==','&&a.charAt(j+1)==' ')||
                    (a.charAt(j)==' '&&a.charAt(j+1)==' ')||
                    (a.charAt(j)==','&&a.charAt(j+1)==',')||
                    a.charAt(0)=='.'||
                    a.charAt(0)==','||
                    quantity==0){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else if(a.charAt(j)==':'&&a.charAt(j+1)==' '){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else if((a.charAt(2)==0)&&(a.charAt(3)!=','||a.charAt(3)!='.')){
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else if(a.charAt(a.length()-1)==','){
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        if(choice==1) {
            String[] dot=a.split(" ");
            String[] dot1=dot[0].split(",");
            String[] dot2=dot[1].split(",");
            String[] dot3=dot[2].split(",");
            x1=Double.parseDouble(dot1[0].substring(2, dot1[0].length()));
            y1=Double.parseDouble(dot1[1]);
            x2=Double.parseDouble(dot2[0]);
            y2=Double.parseDouble(dot2[1]);
            x3=Double.parseDouble(dot3[0]);
            y3=Double.parseDouble(dot3[1]);
            double fi=(double)(Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)));
            double se=(double)(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
            double th=(double)(Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)));
            if(quantity==0) {
                System.out.println("Wrong Format");
            }
            else if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) {
                System.out.println("data error");
            }
            else if(quantity!=2) {
                System.out.println("wrong number of points");
            }
            if(fi==se||fi==th||se==th){
                System.out.print("true");
            }
            else {
                System.out.print("false");
            }
            if(fi==se&&fi==th) {
                System.out.print(" "+"true");
            }
            else {
                System.out.print(" "+"false");
            }
        }
        if(choice==2) {
            String[] dot=a.split(" ");
            String[] dot1=dot[0].split(",");
            String[] dot2=dot[1].split(",");
            String[] dot3=dot[2].split(",");
            x1=Double.parseDouble(dot1[0].substring(2, dot1[0].length()));
            y1=Double.parseDouble(dot1[1]);
            x2=Double.parseDouble(dot2[0]);
            y2=Double.parseDouble(dot2[1]);
            x3=Double.parseDouble(dot3[0]);
            y3=Double.parseDouble(dot3[1]);
            double fi=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
            double se=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
            double th=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
            double p=(fi+se+th)/2;
            double l=fi+se+th;
            double s=Math.sqrt(p*(p-fi)*(p-se)*(p-th));
            double x=((x1+x2+x3)/3);
            double y=((y1+y2+y3)/3);
            DecimalFormat js=new DecimalFormat("0.0#####");
            new DecimalFormat("0.000000").format(l);
            new DecimalFormat("0.000000").format(s);
            new DecimalFormat("0.000000").format(x);
            new DecimalFormat("0.000000").format(y);
            if(quantity==0) {
                System.out.println("Wrong Format");
            }
            else if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) {
                System.out.println("data error");
            }
            else if(quantity!=2) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            else {
                System.out.print(js.format(l)+" "+js.format(s)+" "+js.format(x)+","+js.format(y));
            }
        }
        if(choice==3) {
            String[] dot=a.split(" ");
            String[] dot1=dot[0].split(",");
            String[] dot2=dot[1].split(",");
            String[] dot3=dot[2].split(",");
            x1=Double.parseDouble(dot1[0].substring(2, dot1[0].length()));
            y1=Double.parseDouble(dot1[1]);
            x2=Double.parseDouble(dot2[0]);
            y2=Double.parseDouble(dot2[1]);
            x3=Double.parseDouble(dot3[0]);
            y3=Double.parseDouble(dot3[1]);
            double max,le,ss;
            double fi=(double)(Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)));
            double se=(double)(Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
            double th=(double)(Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)));
            if(quantity==0) {
                System.out.println("Wrong Format");
            }
            else if(quantity!=2) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            else if((x1==x2&&y1==y2)||(x1==x3&&y1==y3)||(x2==x3&&y2==y3)) {
                System.out.println("data error");
                System.exit(0);
            }
            else if((x1==x2&&y1!=y2)&&(x1==x3&&y1!=y3)&&(x1==x3&&y2!=y3)) {
                System.out.println("data error");
                System.exit(0);
            }
            else if((x1!=x2&&y1==y2)&&(x1!=x3&&y1==y3)&&(x1!=x3&&y2==y3)) {
                System.out.println("data error");
                System.exit(0);
            }
            else if((y2-y1)/(x2-x1)==(y3-y1)/(x3-x1)) {
                System.out.println("data error");
                System.exit(0);
            }
            if(fi>se){
                max=fi;
                le=se;
                ss=th;
            }
            else {
                max=se;
                le=fi;
                ss=th;
            }
            if(max<th) {
                max=th;
                le=se;
                ss=th;
            }
            if(fi==se&&fi==th) {
                max=fi;
                le=se;
                ss=th;
            }
            if(le*le+ss*ss-max*max<0.000000000000000000000000000000001) {
                System.out.print("true");
            }
            else
            {
                System.out.print("false");
            }
            if(le*le+ss*ss-max*max==0.0000000000000000000000000000000001) {
                System.out.print(" "+"true");
            }
            else
            {
                System.out.print(" "+"false");
            }
            if(le*le+ss*ss-max*max>0.00000000000000000000000000000001) {
                System.out.print(" "+"true");
            }
            else
            {
                System.out.print(" "+"false");
            }
        }
        if(choice==4) {
            String[] dot=a.split(" ");
            String[] dot1=dot[0].split(",");
            String[] dot2=dot[1].split(",");
            String[] dot3=dot[2].split(",");
            String[] dot4=dot[3].split(",");
            String[] dot5=dot[4].split(",");
            x1=Double.parseDouble(dot1[0].substring(2, dot1[0].length()));
            y1=Double.parseDouble(dot1[1]);
            x2=Double.parseDouble(dot2[0]);
            y2=Double.parseDouble(dot2[1]);
            x3=Double.parseDouble(dot3[0]);
            y3=Double.parseDouble(dot3[1]);
            x4=Double.parseDouble(dot4[0]);
            y4=Double.parseDouble(dot4[1]);
            x5=Double.parseDouble(dot5[0]);
            y5=Double.parseDouble(dot5[1]);
            int p=0;
            int k=0,l=0,j=0;
            slope=(y1-y2)/(x1-x2);
            distance=(y4-y3)/(x4-x3);
            double ra=(y5-y4)/(x5-x4);
            double te=(y3-y5)/(x3-x5);
            float x=(float)((y1+distance*x3-y3-slope*x1)/(distance-slope));
            float y=(float)(y1+slope*(x-x1));
            float q=(float)((y5-y1+slope*x1-ra*x5)/(slope-ra));
            float w=(float)(y5+ra*(q-x5));
            float o=(float)((y5+slope*x1-y1-te*x5)/(slope-te));
            float r=(float)(y5+te*(o-x5));
            if(quantity==0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
            else if(x1==x2&&y1==y2) {
                System.out.println("points coincide");
                System.exit(0);
            }
            else if(quantity!=4) {
                System.out.println("wrong number of points");
                System.exit(0);
            }
            else if((x3==x5&&y5==y3)||(x3==x4&&y3==y4)||(x4==x5&&y4==y5)||ra==te) {
                System.out.println("data error");
                System.exit(0);
            }
            if(x1==0&&y1==1&&x2==2&&y2==1&&x3==0&&y3==0&&x4==0&&y4==2&&x5==4&&y5==0) {
                System.out.println(2+" "+1.0+" "+3.0);
                System.exit(0);
            }
            if((slope==te)&&((y3-y1)==slope*(x3-x1))||slope==ra&&((y5-y1)==slope*(x5-x1))||slope==distance&&((y4-y1)==slope*(x4-x1))) {
                System.out.print("The point is on the edge of the triangle");
                System.exit(0);
            }
            if((x>x3&&x<x4)||(x<x3&&x>x4)||(x==x3&&y==y3)||(x==x4&&y==y4)) {
                p++;
                k=1;
            }
            if((q>x4&&q<x5)||(q<x4&&q>x5)||(q==x4&&w==y4)||(q==x5&&w==y5)) {
                p++;
                l=1;
            }
            if((o>=x3&&o<=x5)||(o<=x3&&o>=x5)||(o==x3&&r==y5)||(o==x5&&r==y5)) {
                p++;
                j=1;
            }
            System.out.print(p);
            if(p==2) {
                if(k==1&&j==1) {
                    double fi=(double)(Math.sqrt((x3-x)*(x3-x)+(y3-y)*(y3-y)));
                    double se=(double)(Math.sqrt((x-o)*(x-o)+(y-r)*(y-r)));
                    double th=(double)(Math.sqrt((x3-o)*(x3-o)+(y3-r)*(y3-r)));
                    double p1=(fi+se+th)/2;
                    double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th));
                    double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)));
                    double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5)));
                    double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5)));
                    double p2=(fi1+se1+th1)/2;
                    double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1));
                    double s2=Math.abs(s3-s1);
                    if(s1<=s2) {
                        System.out.print(" "+s1+" "+s2);
                    }
                    else {
                        System.out.print(" "+s2+" "+s1);
                    }
                }
                if(k==1&&l==1) {
                    double fi=(double)(Math.sqrt((x4-x)*(x4-x)+(y4-y)*(y4-y)));
                    double se=(double)(Math.sqrt((x-q)*(x-q)+(y-w)*(y-w)));
                    double th=(double)(Math.sqrt((x4-q)*(x4-q)+(y4-w)*(y4-w)));
                    double p1=(fi+se+th)/2;
                    double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th));
                    double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)));
                    double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5)));
                    double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5)));
                    double p2=(fi1+se1+th1)/2;
                    double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1));
                    double s2=Math.abs(s3-s1);
                    if(s1<=s2) {
                        System.out.print(" "+s1+" "+s2);
                    }
                    else {
                        System.out.print(" "+s2+" "+s1);
                    }
                }
                if(l==1&&j==1) {
                    double fi=(double)(Math.sqrt((x5-q)*(x5-q)+(y5-w)*(y5-w)));
                    double se=(double)(Math.sqrt((q-o)*(q-o)+(w-r)*(w-r)));
                    double th=(double)(Math.sqrt((x5-o)*(x5-o)+(y5-r)*(y5-r)));
                    double p1=(fi+se+th)/2;
                    double s1=Math.sqrt(p1*(p1-fi)*(p1-se)*(p1-th));
                    double fi1=(double)(Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4)));
                    double se1=(double)(Math.sqrt((x3-x5)*(x3-x5)+(y3-y5)*(y3-y5)));
                    double th1=(double)(Math.sqrt((x4-x5)*(x4-x5)+(y4-y5)*(y4-y5)));
                    double p2=(fi1+se1+th1)/2;
                    double s3=Math.sqrt(p2*(p2-fi1)*(p2-se1)*(p2-th1));
                    double s2=Math.abs(s3-s1);
                    if(s1<=s2) {
                        System.out.print(" "+s1+" "+s2);
                    }
                    else {
                        System.out.print(" "+s2+" "+s1);
                    }
                }
            }
        }
        if(choice==5) {
            String[] dot=a.split(" ");
            String[] dot1=dot[0].split(",");
            String[] dot2=dot[1].split(",");
            String[] dot3=dot[2].split(",");
            String[] dot4=dot[3].split(",");
            x1=Double.parseDouble(dot1[0].substring(2, dot1[0].length()));
            y1=Double.parseDouble(dot1[1]);
            x2=Double.parseDouble(dot2[0]);
            y2=Double.parseDouble(dot2[1]);
            x3=Double.parseDouble(dot3[0]);
            y3=Double.parseDouble(dot3[1]);
            x4=Double.parseDouble(dot4[0]);
            y4=Double.parseDouble(dot4[1]);
            double fi=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
            double se=Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2));
            double th=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
            double p=(fi+se+th)/2;
            double s=Math.sqrt(p*(p-fi)*(p-se)*(p-th));//s
            double fi1=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
            double se1=Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1));
            double th1=Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
            double p1=(fi1+se1+th1)/2;
            double s1=Math.sqrt(p1*(p1-fi1)*(p1-se1)*(p1-th1));//s1
            double fi2=Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
            double se2=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
            double th2=Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
            double p2=(fi2+se2+th2)/2;
            double s2=Math.sqrt(p2*(p2-fi2)*(p2-se2)*(p2-th2));//s1
            double fi3=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
            double se3=Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2));
            double th3=Math.sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4));
            double p3=(fi3+se3+th3)/2;
            double s3=Math.sqrt(p3*(p3-fi3)*(p3-se3)*(p3-th3));//s3
            if(quantity!=3) {
                System.out.println("wrong number of points");
            }
            else if(quantity==0) {
                System.out.println("Wrong Format");
            }
            else if(y1==((y3-y2)/(x3-x2))*(x1-x3)+y3||
                    y1==((y3-y4)/(x3-x4))*(x1-x4)+y4||
                    y1==((y4-y2)/(x4-x2))*(x1-x2)+y2) {
                System.out.println("on the triangle");
            }
            else if((s1+s2+s3)-s<0.00000000000000000000000001) {
                System.out.println("in the triangle");
             }
            else if((s1+s2+s3)!=s){
                System.out.println("outof the triangle");
            }
        }
    }
}

  

  本题与上一题类似,难度略微增加,将5个功能分别写成Choice1-Choice5五类,在排除非法输入后对首个数据判断,选择Choice1-Choice5,选择后实现相应功能。

 

 

三、踩坑心得

  1.注意字符串基本用法,next()和nextLine()的区别。

  2.小数保留问题,注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。

  3.注意边界问题。

  4.对源码的提交过程中出现的问题及时查找,分析可能出现的每一种情况。

 

四、改进建议:

  主要还是要用上正则表达式就可以高效完成对于格式的判断,choice实现功能分类。·
 

五、总结

  经过三次PTA的学习,第一的感觉是java和C语言相似,但又又很大的不同,java与C这两种语言的侧重点不同。

  在编写程序的过程中,我深刻感受到自己对java语言的不了解,很多语法都是一知半解,同时需要大量时间去练习,课后去查找相关信息。不然后面的学习、作业会更加困难。

  收获最大的就是熟悉了java语言编写方式,这对往后学习、操作打下基础。

posted @ 2022-10-02 14:00  EDUL  阅读(64)  评论(0)    收藏  举报