第一次博客作业
前言
1)第一次作业的知识点主要是基本的计算啊,浮点数,精确度,if else 语句,for循环,switch语句的使用,题量比较多,有九道题目,比较简单,都是一些很基础的,主要是Java入门的题目。
2)第二次作业的知识点主要是字符串的创建,识别,判断,输出,字母大小写和ASCLL的转换,奇偶校验位的使用,字符串的提取,难度整体和第一次作业相比提升了一个层次,特别是第二题,一共三道题。
3)第三次作业的知识点主要是点和线的创建,正则表达式,类的创建,线的计算,三角形的判断,个人认为这次的题目比较难,写起来很费劲,特别是第三题,真的要难死了。
设计与分析
第一次作业
1)第一题
类图

第一题很简单,就是进行一些简单的运算,计算BMI,然后判断等级
2)第二题
类图

第二题是单位的换算,也比较简单,主要需要注意的是要把最后的结果换成float型,不然会出错
3)第三题
类图

这道题要求奇数和,只需要设置一个条件,筛选出奇数,再将它们求和就可以了,两个字 简单!
4)第四题
类图

该题也是普通的计算,没有什么难度,需要注意的是,输出的类型是float型,最后要进行转换
5)第五题
类图

本题为游戏角色和种族的选择,主要运用switch语句来完成整个代码,比较简单,没有什么特别要注意的。
6)第六题
类图

这道题是学号的识别,遇到的文体主要是当时不知道怎么分别识别字符串不同位置的内容,因为自己的粗心,输出的一个字打错了,导致有一个点答案一直错误,但是整体难度还是比较简单
7)第七题
类图

这道题特别需要注意的点是两个浮点数进行比较时,不能直接将他们的差值和0进行比较,而是和一个无限接近零的小数比较。
8) 第八题
类图

本题用一个正则表达式就可以了,比较简单,没什么特别要注意的
9)第九题
类图

本题是对三角形的判断,只要写好条件就没什么难的,逻辑什么的都挺简单的
第二次作业
第一题
类图

本题主要考查了对ASCLL码的运用,比较简单
2)第二题
题目
RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5~8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5~8位,具体位数由通信双方提前设置)前加上1位起始位“0”,在有效数据之后加上1位可选的奇偶校验位和1位结束位“1”。请编写程序,模拟串口接收处理程序,注:假定有效数据是8位,奇偶校验位采用奇校验。
输入格式:
由0、1组成的二进制数据流。例如:11110111010111111001001101111111011111111101111
输出格式:
过滤掉空闲、起始、结束以及奇偶校验位之后的数据,数据之前加上序号和英文冒号。
如有多个数据,每个数据单独一行显示。
若数据不足11位或者输入数据全1没有起始位,则输出"null data",
若某个数据的结束符不为1,则输出“validate error”。
若某个数据奇偶校验错误,则输出“parity check error”。
若数据结束符和奇偶校验均不合格,输出“validate error”。
如:11011或11111111111111111。
例如:
1:11101011
2:01001101
3:validate error
输入样例:
在这里给出一组输入。例如:
1111011101011111111111
输出样例:
在这里给出相应的输出。例如:
1:11101011
输入样例1:
在这里给出一组输入。例如:
11110111010111111001001101111111011111111101111
输出样例1:
在这里给出相应的输出。例如:
1:11101011
2:01001101
3:validate error
输入样例2:
输入数据不足11位。例如:
111101
输出样例2:
在这里给出相应的输出。例如:
null data
输入样例3:
输入数据全1没有起始位。例如:
1111111111111111
输出样例3:
在这里给出相应的输出。例如:
null data
输入样例4:
输入数据全1没有起始位。例如:
111101110101111111101111111101
输出样例4:
在这里给出相应的输出。例如:
1:11101011
2:parity check error
输入样例5:
两组数据结束符和奇偶校验均不合格。例如:
111000000000000011100000000000000
输出样例5:
在这里给出相应的输出。例如:
1:validate error
2:validate error
输入样例6:
两组数据,数据之间无空闲位。例如:
1110000000001100111000001
输出样例6:
在这里给出相应的输出。例如:
1:00000000
2:01110000
代码
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in=new Scanner(System.in); String a=in.nextLine(); String n; int x=0,m=0,i,j,k=0,q; if(a.length()<11) { System.out.print("null data"); return; } for(i=0;i<a.length();i++) { if(a.charAt(i)=='0') { k=1; if(a.length()-i<10) { if(x==0) { System.out.print("null data"); return; } } else { m=0; for(j=i+1;j<=i+8;j++) { if(a.charAt(j)=='1') m++; } x++; if(m>=2) q=m%2; else q=m; if(a.charAt(i+9)=='0'&&q==0&&a.charAt(i+10)=='0'||a.charAt(i+9)=='1'&&q==1&&a.charAt(i+10)=='0'||a.charAt(i+10)=='0') System.out.print(x+":validate error\n"); else if(a.charAt(i+9)=='1'&&q==1||a.charAt(i+9)=='0'&&q==0) System.out.print(x+":parity check error\n"); else { n=a.substring(i+1,i+9); System.out.print(x+":"+n+"\n"); } i=j+1; } } } if(k==0) System.out.print("null data"); } }
类图

本题有点难,首先要判断输入的字符串的长度必须大于11,而且要对其中的奇偶检验位进行判断,看其是否正确,而且还要对位进行判断,起始位和结束位,其中我自己踩过最大的坑就是有几个点一直答案错误,用软件分布调试也没有发现问题,最后竟然是输出的单词有错误,所以最好是直接复制题目给出的输出,以免造成不必要的错误,还很难检查出看来
第三次作业
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
输入样例:
整数输入。例如:
0,0 1,1
输出样例:
在这里给出相应的输出。例如:
1.4142135623730951
输入样例1:
带符号double类型实数输入。例如:
+2,-2.3 0.9,-3.2
输出样例1:
在这里给出相应的输出。例如:
1.42126704035519
输入样例2:
格式非法。例如:
++2,-2.3 0.9,-3.2
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
输入样例3:
点的数量超过两个。例如:
+2,-2.3 0.9,-3.2 +2,-2.3
输出样例3:
在这里给出相应的输出。例如:
wrong number of points
代码
import java.util.Scanner; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Stringhan a = new Stringhan(); a.Stringhand(); } } class strConv { double dou1; double dou2; int istrue1num = 0; String str1 = new String(); String str2 = new String(); boolean strCo(String s){ if(istrue1(s)) { if(istrue2(s)) { dou1 = Double.parseDouble(str1); dou2 = Double.parseDouble(str2); return true; } } return false; }//判断逗号个数 记录逗号下标 boolean istrue1(String str) { int num = 0; char[] chars=str.toCharArray(); for(int i = 0; i < chars.length; i++) { if(chars[i] == ',') { istrue1num = i; num++; if(num > 1) return false; } } return true; }// 切割字符串 判断是否为浮点数 boolean istrue2(String str) { str1 = str.substring(0 , istrue1num ); str2 = str.substring(istrue1num + 1); if((isDouble(str1)||isInt(str1))&&(isDouble(str2)||isInt(str2))) return true; else return false; } //判断是否符合要求 private boolean isDouble(String str) { //判断浮点数点数 if (null == str || "".equals(str)) { return false; } Pattern pattern = Pattern.compile("^[-\\+]?\\d*[.]\\d+$"); return pattern.matcher(str).matches(); } private boolean isInt(String str) { //判断整数 if (null == str || "".equals(str)) { return false; } Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches(); } } class Stringhan { String stco1; String stco2; double res; //运行函数 void Stringhand(){ Scanner input = new Scanner(System.in); String hand = input.nextLine(); //输入坐标非法 if(!legit(hand)) { System.out.print("Wrong Format"); return; } if(!Numtrue(hand)) { //输入坐标个数错误 System.out.print("wrong number of points"); return; } strConv s1 = new strConv(); strConv s2 = new strConv(); cut(hand); //切割字符串 if(s1.strCo(stco1)&&s2.strCo(stco2)) { res = distan(s1.dou1,s1.dou2,s2.dou1,s2.dou2); System.out.print(res); } else System.out.print("Wrong Format"); } //检验输入坐标是否合法 Boolean legit(String s) { if(s.matches("(([+|-]?[1-9]+([\\.][0-9]+|[0-9]*)|([+|-]?[0]([\\.][0-9])?))[,]([+|-]?[1-9]+([\\.][0-9]+|[0-9]*)|([+|-]?[0]([\\.][0-9])?))" + "[\\s]{1})+(([+|-]?[1-9]+([\\.][0-9]+|[0-9]*)|([+|-]?[0]([\\.][0-9])?))[,]([+|-]?[1-9]+([\\.][0-9]+|[0-9]*)|([+|-]?[0]([\\.][0-9])?)))+")) return true; else return false; } //检验输入坐标个数是否正确 Boolean Numtrue(String s) { if(s.matches("([+|-]?[0-9]+([\\.][0-9]+|[0-9]*)[,][+|-]?[0-9]+([\\.][0-9]+|[0-9]*)" + "[\\s]{1})([+|-]?[0-9]+([\\.][0-9]+|[0-9]*)[,][+|-]?[0-9]+([\\.][0-9]+|[0-9]*))")) return true; else return false; } //切割字符串 void cut(String a) { int num = 0; for(int i = 0;i < a.length();i++) { if(Character.isWhitespace(a.charAt(i))) { //判断是否是空格 num = i; } } stco1 = a.substring(0 , num ); stco2 = a.substring(num + 1); } //计算两点间距离 double distan(double x1,double y1,double x2,double y2){ double x = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); return x; } }
类图

这道题主要是创建一个点的类,然后用正则表达式来验证输入的字符串是否正确,再切割,需要好几个类,有点难,写了好久,算点的距离倒是蛮简单的,套公式就好了。
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",
输出格式:
见题目描述。
输入样例1:
选项1,两点重合。例如:
1:-2,+5 -2,+5
输出样例:
在这里给出相应的输出。例如:
points coincide
输入样例2:
选项1,斜率无穷大的线。例如:
1:-2,3 -2,+5
输出样例:
在这里给出相应的输出。例如:
Slope does not exist
输入样例3:
选项1,斜率无穷大。例如:
1:-2,3 -2,+5
输出样例:
在这里给出相应的输出。例如:
Slope does not exist
输入样例4:
选项1,符合格式输入,带符号/不带符号数混合。例如:
1:-2.5,3 -2,+5.3
输出样例:
在这里给出相应的输出。例如:
4.6
输入样例5:
选项2,计算第一个点到另外两点连线的垂直距离。例如:
2:0,1 1,0 2,0
输出样例:
在这里给出相应的输出。例如:
1.0
输入样例6:
选项3,判断三个点是否在一条线上。例如:
3:0,1 2,2 5,3
输出样例:
在这里给出相应的输出。例如:
false
输入样例7:
选项4,判断两条线是否平行。例如:
4:0,1 0,2 2,1 3,0
输出样例:
在这里给出相应的输出。例如:
false
输入样例8:
选项5,判断两条线的交点。例如:
5:0,0 -1,-1 0,2 3,-1
输出样例:
在这里给出相应的输出,交点坐标之间以英文","分隔,判断结果与坐标之间以一个英文空格分隔。例如:
1.0,1.0 true
输入样例9:
选项5,判断两条线的交点。但两条线平行例如:
5:0,0 -1,-1 2,3 3,4
输出样例:
在这里给出相应的输出,交点坐标之间以英文","分隔,判断结果与坐标之间以一个英文空格分隔。例如:
is parallel lines,have no intersection point
代码
import java.util.Scanner; import java.util.regex.Pattern; public class Main{ public static void main(String[] args){ Stringhan a = new Stringhan(); Scanner in = new Scanner(System.in); String p = in.nextLine(); if(!p.matches("[1-5][:](([+|-]?[1-9]+([\\.][0-9]+|[0-9]*)|([+|-]?[0]([\\.][0-9])?))[,]([+|-]?[1-9]+([\\.][0-9]+|[0-9]*)|([+|-]?[0]([\\.][0-9])?))" + "[\\s]{1})+(([+|-]?[1-9]+([\\.][0-9]+|[0-9]*)|([+|-]?[0]([\\.][0-9])?))[,]([+|-]?[1-9]+([\\.][0-9]+|[0-9]*)|([+|-]?[0]([\\.][0-9])?)))+")) { System.out.print("Wrong Format"); return; } a.cho = (int)p.charAt(0) - 48; String q = p.substring(2); a.chotr = a.Num(q); //获取坐标个数 if(!a.StNum(a.cho,a.chotr)) { System.out.print("wrong number of points"); return; } a.cut(q); switch(a.cho) { case 1: a.do1(q); break; case 2: a.do2(q); break; case 3: a.do3(q); break; case 4: a.do4(q); break; case 5: a.do5(q); break; } } } class strConv { double x; double y; int istrue1num = 0; String str1 = new String(); String str2 = new String(); public void strCo(String s){ if(istrue1(s)) { if(istrue2(s)) { x = Double.parseDouble(str1); y = Double.parseDouble(str2); } else System.out.print("数据有误"); } else System.out.print("数据有误"); } //判断逗号个数 记录逗号下标 private boolean istrue1(String str) { int num = 0; char[] chars=str.toCharArray(); for(int i = 0; i < chars.length; i++) { if(chars[i] == ',') { istrue1num = i; num++; if(num > 1) return false; } } return true; } // 切割字符串 判断是否为浮点数 private boolean istrue2(String str) { str1 = str.substring(0 , istrue1num ); str2 = str.substring(istrue1num + 1); if((isDouble(str1)||isInt(str1))&&(isDouble(str2)||isInt(str2))) return true; else return false; } //判断数据是否符合要求 private boolean isDouble(String str) { if (null == str || "".equals(str)) { return false; } Pattern pattern = Pattern.compile("^[-\\+]?\\d*[.]\\d+$"); return pattern.matcher(str).matches(); } private boolean isInt(String str) { if (null == str || "".equals(str)) { return false; } Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches(); } } class Stringhan { int cho = 0; int chotr = 1; String stco1; String stco2; String stco3; String stco4; String stco5; strConv s1 = new strConv(); strConv s2 = new strConv(); strConv s3 = new strConv(); strConv s4 = new strConv(); strConv s5 = new strConv(); double res; //运行函数 void cut(String a) { int []num = new int[4]; int fax = 0; for(int i = 0;i < a.length();i++) { if(Character.isWhitespace(a.charAt(i))) { //判断是否是空格 num[fax] = i; fax++; } } if(fax==1) { stco1 = a.substring(0 , num[0] ); stco2 = a.substring(num[0] + 1); } if(fax==2) { stco1 = a.substring(0 , num[0] ); stco2 = a.substring(num[0]+1 , num[1] ); stco3 = a.substring(num[1] + 1); } if(fax==3) { stco1 = a.substring(0 , num[0] ); stco2 = a.substring(num[0]+1 , num[1] ); stco3 = a.substring(num[1] + 1,num[2]); stco4 = a.substring(num[2] + 1); } } //获取坐标个数 int Num(String s) { int x = 1; for(int i = 0; i < s.length(); i++) { if(s.charAt(i) == ' ') x++; } return x; } //检验坐标个数是否符合 boolean StNum(int cho , int chotr) { if((cho == 1&&chotr == 2)||(cho == 2&&chotr == 3)||(cho == 3&&chotr == 3)||(cho == 4&&chotr == 4)||(cho == 5&&chotr == 4)) return true; else return false; } //检验坐标是否相同 Boolean isiden(strConv st1,strConv st2) { if(st1.x==st2.x&&st1.y==st2.y) return true; else return false; } //计算两点斜率 double slope(strConv st1,strConv st2){ double x = (st1.y - st2.y)/(st1.x - st2.x); return x; } //判断点是否在线段内 Boolean line(strConv st1,strConv st2,double x ,double y){ double xmax = Math.max(st1.x, st2.x); double xmin = Math.min(st1.x, st2.y); double ymax = Math.max(st1.y, st2.y); double ymin = Math.min(st1.y, st2.y); if((x < xmax&&x > xmin)||(y < ymax&&y > ymin)) return true; else return false; } //选项一 void do1(String s) { s1.strCo(stco1); s2.strCo(stco2); if(isiden(s1,s2)) { //检查共点 System.out.print("points coincide"); return; } if(s1.x==s2.x) //斜率不存在 System.out.print("Slope does not exist"); else { System.out.print(slope(s1,s2)); } } //选项二 void do2(String s) { s1.strCo(stco1); s2.strCo(stco2); s3.strCo(stco3); if(isiden(s2,s3)) { //检查共点 System.out.print("points coincide"); return; } if(s2.x==s3.x) { //检查斜率 System.out.print(Math.abs(s1.x - s2.x)); return; } double k = slope(s2,s3); res = (Math.abs(s1.y - k * (s1.x - s2.x) - s2.y)/Math.sqrt(1+k*k)); System.out.print(res); } //选项三 void do3(String s) { s1.strCo(stco1); s2.strCo(stco2); s3.strCo(stco3); if(isiden(s1,s2)||isiden(s2,s3)||isiden(s1,s3)) { //检查共点 System.out.print("points coincide"); return; } if(s1.x==s2.x||s2.x==s3.x) { //检查斜率 if(s1.x==s2.x&&s2.x==s3.x) System.out.print("true"); else System.out.print("false"); return; } if(slope(s1,s2)==slope(s2,s3)) //三点共线 System.out.print("true"); else System.out.print("false"); } //选项四 void do4(String s) { s1.strCo(stco1); s2.strCo(stco2); s3.strCo(stco3); s4.strCo(stco4); if(isiden(s1,s2)||isiden(s3,s4)) { //检查共点 System.out.print("points coincide"); return; } if(s1.x==s2.x||s3.x==s4.x) { //检查斜率 if(s1.x==s2.x&&s3.x==s4.x) System.out.print("true"); else System.out.print("false"); return; } if(slope(s1,s2)==slope(s3,s4)) //斜率相同 System.out.print("true"); else System.out.print("false"); } //选项五 void do5(String s) { s1.strCo(stco1); s2.strCo(stco2); s3.strCo(stco3); s4.strCo(stco4); double x = 0; double y = 0; double k1; double k2; if(isiden(s1,s2)||isiden(s3,s4)) { //检查共点 System.out.print("points coincide"); return; } if(s1.x==s2.x&&s3.x==s4.x) { //检查斜率 System.out.print("is parallel lines,have no intersection point"); return; } k1 = slope(s1,s2); k2 = slope(s3,s4); if(k1 == k2) { System.out.print("is parallel lines,have no intersection point"); return; } x = (s3.x*s4.x*s2.x-s4.y*s3.x*s2.x-s3.y*s4.x*s1.x+s4.y*s3.x*s1.x-s1.y*s2.x*s4.x+s2.y*s1.x*s4.x+s1.y*s2.x*s3.x-s2.y*s1.x*s3.x)/(s4.x*s2.y-s4.x*s1.y-s3.x*s2.y+s3.x*s1.y-s2.x*s4.y+s2.x*s3.y+s1.x*s4.y-s1.x*s3.y); y = ((-s3.y*s4.x*s2.y+s4.y*s3.x*s2.y+s3.y*s4.x*s1.y-s4.y*s3.x*s1.y+s1.y*s2.x*s4.y-s1.y*s2.x*s3.y-s2.y*s1.x*s4.y+s2.y*s1.x*s3.y)/(s4.y*s2.x-s4.y*s1.x-s3.y*s2.x+s1.x*s3.y-s2.y*s4.x+s2.y*s3.x+s1.y*s4.x-s1.y*s3.x)); System.out.print(x + "," + y + " "); if(line(s1,s2,x,y)||line(s3,s4,x,y)) System.out.print("true"); else System.out.print("false"); } }
类图

这道题比第一题难多了,不过可以用第一题的类。线的斜率可以用两个点的坐标计算,判断两直线是否平行也可以直接比较斜率,题目比较难,要分好几种情况,用了一个switch语句,主要的问题是计算交点坐标不太会,一直答案错误,咱也不知道是哪错了,哎!
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",
输入样例1:
选项4,定义线的两点重合。例如:
4:1,0 1,0 0,0 2,0 4,0
输出样例:
在这里给出相应的输出。例如:
points coincide
输入样例2:
选项4,构成三角形的三个点在一条线上,无法构成三角形。例如:
4:1,0 0,2 0,0 0,0 4,0
输出样例:
在这里给出相应的输出。例如:
data error
输入样例3:
选项1,判断等腰、等边三角形。例如:
1:-2,0 2,0 0,4
输出样例:
两个判断结果。例如:
true false
输入样例4:
选项2,输出边长、面积、重心坐标。例如:
2:0,0 3,0 0,1
输出样例:
在这里给出相应的输出。例如:
7.162278 1.5 1.0,0.333333
输入样例5:
选项3,钝角、直角、锐角的判断。例如:
3:0,1 1,0 2,0
输出样例:
在这里给出相应的输出。例如:
true false false
输入样例6:
选项4,直线与三角形交点的数量等于2,输出数量值以及三角形被分割的两部分面积。例如:
4:1,0 0,2 0,0 0,2 4,0
输出样例:
在这里给出相应的输出。例如:
2 1.0 3.0
输入样例7:
选项4,直线与三角形交点的数量少于两个,只输出数量值。例如:
4:-1,0 1,2 0,1 0,-1 2,0
输出样例:
在这里给出相应的输出。例如:
1
输入样例8:
选项5,用射线法判断点是否在三角形内部。例如:
5:0.5,0.5 0,0 0,2 4,0
输出样例:
在这里给出相应的输出,交点坐标之间以英文","分隔,判断结果与坐标之间以一个英文空格分隔。例如:
in the triangle
输入样例9:
选项5,用射线法判断点是否在三角形内部。例如:
5:0,0 -1,-1 2,3 3,4
输出样例:
在这里给出相应的输出。例如:
outof the triangle
代码
import java.util.Scanner; import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Stringhan a = new Stringhan(); a.Strhand(); } } // 坐标类 class strConv { double x = 0; double y = 0; int istrue1num = 0; String str1 = new String(); String str2 = new String(); public void strCo(String s){ if(istrue1(s)) { if(istrue2(s)) { x = Double.parseDouble(str1); y = Double.parseDouble(str2); } else System.out.print("数据有误"); } else System.out.print("数据有误"); } //判断逗号个数 记录逗号下标 private boolean istrue1(String str) { int num = 0; char[] chars=str.toCharArray(); for(int i = 0; i < chars.length; i++) { if(chars[i] == ',') { istrue1num = i; num++; if(num > 1) return false; } } return true; } // 切割字符串 判断是否为浮点数 private boolean istrue2(String str) { str1 = str.substring(0 , istrue1num ); str2 = str.substring(istrue1num + 1); if((isDouble(str1)||isInt(str1))&&(isDouble(str2)||isInt(str2))) return true; else return false; } //判断数据是否符合要求 private boolean isDouble(String str) { if (null == str || "".equals(str)) { return false; } Pattern pattern = Pattern.compile("^[-\\+]?\\d*[.]\\d+$"); return pattern.matcher(str).matches(); } private boolean isInt(String str) { if (null == str || "".equals(str)) { return false; } Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); return pattern.matcher(str).matches(); } } //字符串处理 及 点线坐标运算 类 class Stringhan { int cho = 0; int chotr = 1; String stco1; String stco2; String stco3; String stco4; String stco5; strConv s1 = new strConv(); strConv s2 = new strConv(); strConv s3 = new strConv(); strConv s4 = new strConv(); strConv s5 = new strConv(); strConv inter1 = new strConv(); strConv inter2 = new strConv(); strConv inter3 = new strConv(); double res; //运行函数 void Strhand(){ Scanner input = new Scanner(System.in); String hand = input.nextLine(); //输入坐标 if(!true0(hand)) { //判断是否合法 System.out.print("Wrong Format"); return; } cho = (int)hand.charAt(0) - 48; String handX = hand.substring(2); chotr = faNum(handX); //获取坐标个数 if(!StNum1(cho,chotr)) { //检验坐标个数是否正确 System.out.print("wrong number of points"); return; } cut(handX); //裁剪字符串 switch(cho) { //选项执行 case 1: dost1(handX); break; case 2: dost2(handX); break; case 3: dost3(handX); break; case 4: dost4(handX); break; case 5: dost5(handX); break; } } //选项一 void dost1(String handX) { s1.strCo(stco1); s2.strCo(stco2); s3.strCo(stco3); int num = 0; if(!istrian(s1,s2,s3)) { System.out.print("data error"); return; } if(Math.abs(distan(s1,s2)-distan(s1,s3))<=0.00001) { num++; } if(Math.abs(distan(s1,s2)-distan(s2,s3))<=0.00001) { num++; } if(Math.abs(distan(s1,s3)-distan(s2,s3))<=0.00001) { num++; } if(num >= 1) System.out.print("true" + " "); else System.out.print("false" + " "); if(num >= 2) System.out.print("true"); else System.out.print("false"); } //选项二 void dost2(String handX) { s1.strCo(stco1); s2.strCo(stco2); s3.strCo(stco3); if(!istrian(s1,s2,s3)) { System.out.print("data error"); return; } output(distan(s1, s2) + distan(s2, s3) + distan(s3, s1)); //输出周长 System.out.print(" "); output(meas(s1, s2, s3)); //输出面积 System.out.print(" "); output((s1.x+s2.x+s3.x)/3); //输出重心 System.out.print(","); output((s1.y+s2.y+s3.y)/3); } //选项三 void dost3(String handX) { } //选项四 void dost4(String handX){ s1.strCo(stco1); s2.strCo(stco2); s3.strCo(stco3); s4.strCo(stco4); s5.strCo(stco5); double S1 = 0; double S2 = 0; if(isiden(s1,s2)) { System.out.print("points coincide"); //非直线 return; } if(!istrian(s3,s4,s5)) { System.out.print("data error"); //非三角形 return; } //存在交点 if(!istrian(s1, s2, s3)) { if(!istrian(s1, s2, s4)||!istrian(s1, s2, s5)) { System.out.print("The point is on the edge of the triangle"); //四点共线 return; } if(outinter(s1, s2, s4, s5, inter2)) { //三点共线 另外有一交点 inter1.x = s3.x; inter1.y = s3.y; S1 = Math.min(meas(inter1, inter2, s4),meas(inter1, inter2, s5)); S2 = Math.max(meas(inter1, inter2, s4),meas(inter1, inter2, s5)); System.out.print("2 "); output(S1); System.out.print(" "); output(S2); return; } else { //三点共线 另外无交点 System.out.print("1"); return; } } if(!istrian(s1, s2, s4)) { if(!istrian(s1, s2, s3)||!istrian(s1, s2, s5)) { System.out.print("The point is on the edge of the triangle"); return; } if(outinter(s1, s2, s3, s5, inter2)) { //三点共线 另外有一交点 inter1.x = s4.x; inter1.y = s4.y; S1 = Math.min(meas(inter1, inter2, s3),meas(inter1, inter2, s5)); S2 = Math.max(meas(inter1, inter2, s3),meas(inter1, inter2, s5)); System.out.print("2 "); output(S1); System.out.print(" "); output(S2); return; } else { //三点共线 另外无交点 System.out.print("1"); return; } } if(!istrian(s1, s2, s5)) { if(!istrian(s1, s2, s3)||!istrian(s1, s2, s4)) { System.out.print("The point is on the edge of the triangle"); return; } if(outinter(s1, s2, s3, s4, inter2)) { //三点共线 另外有一交点 inter1.x = s5.x; inter1.y = s5.y; S1 = Math.min(meas(inter1, inter2, s3),meas(inter1, inter2, s4)); S2 = Math.max(meas(inter1, inter2, s3),meas(inter1, inter2, s4)); System.out.print("2 "); output(S1); System.out.print(" "); output(S2); return; } else { //三点共线 另外无交点 System.out.print("1"); return; } } //无任何交点 if((!outinter(s1, s2, s3, s4, inter1))&&(!outinter(s1, s2, s4, s5, inter1))&&(!outinter(s1, s2, s3, s5, inter1))) { System.out.print("0"); return; } //正常情况 if(outinter(s1, s2, s3, s4, inter1)){ if(outinter(s1, s2, s3, s5, inter2)){ S1 = Math.min(meas(inter1, inter2, s3),meas(s3, s4, s5) - meas(inter1, inter2, s3)); S2 = Math.max(meas(inter1, inter2, s3),meas(s3, s4, s5) - meas(inter1, inter2, s3)); System.out.print("2 "); output(S1); System.out.print(" "); output(S2); return; } else if(outinter(s1, s2, s4, s5, inter2)){ S1 = Math.min(meas(inter1, inter2, s4),meas(s3, s4, s5) - meas(inter1, inter2, s4)); S2 = Math.max(meas(inter1, inter2, s4),meas(s3, s4, s5) - meas(inter1, inter2, s4)); System.out.print("2 "); output(S1); System.out.print(" "); output(S2); return; } } else if(outinter(s1, s2, s3, s5, inter1)&&outinter(s1, s2, s4, s5, inter2)) { S1 = Math.min(meas(inter1, inter2, s5),meas(s3, s4, s5) - meas(inter1, inter2, s5)); S2 = Math.max(meas(inter1, inter2, s5),meas(s3, s4, s5) - meas(inter1, inter2, s5)); System.out.print("2 "); output(S1); System.out.print(" "); output(S2); return; } } //选项五 void dost5(String handX) { return; } //格式化输出 void output(double x) { int num = -100; String s = Double.toString(x); for(int i = 0;i<s.length();i++) { if(s.charAt(i)=='.') { num = i; } } int su = s.length() - num - 1; if(su<6&&su>0) { if(su==1) { DecimalFormat df = new DecimalFormat("0.0#####"); // 保留一位小数 System.out.print(df.format(x)); } if(su==2) { DecimalFormat df = new DecimalFormat("0.00####"); // 保留两位小数 System.out.print(df.format(x)); } if(su==3) { DecimalFormat df = new DecimalFormat("0.000###"); // 保留三位小数 System.out.print(df.format(x)); } if(su==4) { DecimalFormat df = new DecimalFormat("0.0000##"); // 保留四位小数 System.out.print(df.format(x)); } if(su==5) { DecimalFormat df = new DecimalFormat("0.00000#"); // 保留五位小数 System.out.print(df.format(x)); } } else { DecimalFormat df = new DecimalFormat("0.######"); System.out.print(df.format(x)); } } //判断是否是三角形 Boolean istrian(strConv s1,strConv s2,strConv s3) { double x1 = s1.x; double y1 = s1.y; double x2 = s2.x; double y2 = s2.y; double x3 = s3.x; double y3 = s3.y; double a, b, c; a = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); b = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); c = Math.sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2)); if (a + b > c && a + c > b && b + c > a ) return true; else return false; } //计算面积 double meas(strConv s1,strConv s2,strConv s3) { double a = distan(s1, s2); double b = distan(s2, s3); double c = distan(s3, s1); double p = (a + b + c) / 2; double S = Math.sqrt(p * (p - a) * (p - b) * (p - c)); return S; } //检验输入是否合法 Boolean true0(String s) { if(s.matches("[1-5][:](([+|-]?[0-9]+([\\.][0-9]+|[0-9]*)[,][+|-]?[0-9]+([\\.][0-9]+|[0-9]*)[\\s]{1})+([+|-]?[0-9]+([\\.][0-9]+|[0-9]*)[,][+|-]?[0-9]+([\\.][0-9]+|[0-9]*))+)")) return true; else return false; } boolean StNum1(int cho , int chotr) { if((cho == 1&&chotr == 3)||(cho == 2&&chotr == 3)||(cho == 3&&chotr == 3)||(cho == 4&&chotr == 5)||(cho == 5&&chotr == 4)) return true; else return false; } //获取坐标个数 int faNum(String s) { int x = 1; for(int i = 0; i < s.length(); i++) { if(s.charAt(i) == ' ') { x++; } } return x; } //检验坐标个数是否符合 boolean StNum(int cho , int chotr) { if((cho == 1&&chotr == 2)||(cho == 2&&chotr == 3)||(cho == 3&&chotr == 3)||(cho == 4&&chotr == 4)||(cho == 5&&chotr == 4)) return true; else return false; } //切割字符串 void cut(String a) { int []num = new int[5]; int fax = 0; for(int i = 0;i < a.length();i++) { if(Character.isWhitespace(a.charAt(i))) { //判断是否是空格 num[fax] = i; fax++; } } if(fax==1) { stco1 = a.substring(0 , num[0] ); stco2 = a.substring(num[0] + 1); } if(fax==2) { stco1 = a.substring(0 , num[0] ); stco2 = a.substring(num[0]+1 , num[1] ); stco3 = a.substring(num[1] + 1); } if(fax==3) { stco1 = a.substring(0 , num[0] ); stco2 = a.substring(num[0]+1 , num[1] ); stco3 = a.substring(num[1] + 1,num[2]); stco4 = a.substring(num[2] + 1); } if(fax==4) { stco1 = a.substring(0 , num[0] ); stco2 = a.substring(num[0]+1 , num[1] ); stco3 = a.substring(num[1] + 1,num[2] ); stco4 = a.substring(num[2] + 1,num[3] ); stco5 = a.substring(num[3] + 1); } } //检验坐标是否相同 Boolean isiden(strConv st1,strConv st2) { if(st1.x==st2.x&&st1.y==st2.y) return true; else return false; } //计算两点间距离 double distan(strConv s1,strConv s2){ double x = Math.sqrt((s1.x-s2.x)*(s1.x-s2.x)+(s1.y-s2.y)*(s1.y-s2.y)); return x; } //计算两点斜率 double slope(strConv st1,strConv st2){ double x = (st1.y - st2.y)/(st1.x - st2.x); return x; } //判断点是否在线段内 Boolean line(strConv st1,strConv st2,double x ,double y){ double xmax = Math.max(st1.x, st2.x); double xmin = Math.min(st1.x, st2.x); double ymax = Math.max(st1.y, st2.y); double ymin = Math.min(st1.y, st2.y); if((x < xmax&&x > xmin)||(y < ymax&&y > ymin)) return true; else return false; } //求两直线交点 Boolean outinter(strConv s1 , strConv s2 , strConv s3 , strConv s4 ,strConv s) { if(s1.x==s2.x&&s3.x==s4.x) { return false; } double k1 = slope(s1,s2); double k2 = slope(s3,s4); if(k1 == k2) { return false; } double x = (s3.y*s4.x*s2.x-s4.y*s3.x*s2.x-s3.y*s4.x*s1.x+s4.y*s3.x*s1.x-s1.y*s2.x*s4.x+s2.y*s1.x*s4.x+s1.y*s2.x*s3.x-s2.y*s1.x*s3.x)/(s4.x*s2.y-s4.x*s1.y-s3.x*s2.y+s3.x*s1.y-s2.x*s4.y+s2.x*s3.y+s1.x*s4.y-s1.x*s3.y); double y = ((-s3.y*s4.x*s2.y+s4.y*s3.x*s2.y+s3.y*s4.x*s1.y-s4.y*s3.x*s1.y+s1.y*s2.x*s4.y-s1.y*s2.x*s3.y-s2.y*s1.x*s4.y+s2.y*s1.x*s3.y)/(s4.y*s2.x-s4.y*s1.x-s3.y*s2.x+s1.x*s3.y-s2.y*s4.x+s2.y*s3.x+s1.y*s4.x-s1.y*s3.x)); if(line(s3,s4,x,y)) { s.x = x; s.y = y; return true; } else return false; } }
类图

这道题更别提了好吧!是给人写的吗,实在太难了, 很多不知道该怎么判断,就像判断是什么三角形,叫我判断是否是直角三角形还好,锐角钝角是什么鬼啊!实在不会,好吧,我承认我是个大傻子,判断点是在三角形的里面还是外面也不会
踩坑心得
做题目一定要认真,特别是要输出什么英文的时候,要是没有十足的打算,就直接复制题目上的就好了,要不然真的很容易出错,而且还不容易发现,还有就是,浮点数之间比较大小,不要将他们的差值和0比,而是和一个无限接近于0的小数相比,不然会出错,一定要注意啊!!!正则表达式很重要,要学会
总结
java是一门全新的课程,在学习的过程中,还是有些许困难,很多知识点不太懂,像正则运算啊,奇偶校验位啊,类的运用啊,这些对我来说都是新的知识点,掌握的一般,还会继续学习,我相信刷题是个不错的选择,做题的过程中一定要细心,不要乱来。
浙公网安备 33010602011771号