Nchu第一次博客作业
(1)前言:
第一次作业比较简单,也是老师发给我们练练手,主要还是熟悉一下java的输入输出格式,题目很多都在以前学习c语言的时候有写过,从第二,三次作业开始就正式开始学习java了,第二次写pta的时候一开始并不知道有正则表达式这个东西,在那一周的最后一节课老师提到了,但是我还是用遍历的方法强行通过了测试点。
知识点主要是正则表达式,可以参考这个网站:java.util.regex包下的Pattern和Matcher详解(正则匹配) - 大大大圣 - 博客园 (cnblogs.com),然后还用到了分割的一个函数split
难度当然是有的,第二次写得很慢但最后还是把分数都拿满了,第三次就比较痛苦了,正则表达式不会用,最后只是拿到66分。
(2)设计与分析:
题目集2:
7-2串口字符解析
RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5~8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5~8位,具体位数由通信双方提前设置)前加上1位起始位“0”,在有效数据之后加上1位可选的奇偶校验位和1位结束位“1”。请编写程序,模拟串口接收处理程序,注:假定有效数据是8位,奇偶校验位采用奇校验。
输入格式:
由0、1组成的二进制数据流。例如:11110111010111111001001101111111011111111101111
输出格式:
过滤掉空闲、起始、结束以及奇偶校验位之后的数据,数据之前加上序号和英文冒号。
如有多个数据,每个数据单独一行显示。
若数据不足11位或者输入数据全1没有起始位,则输出"null data",
若某个数据的结束符不为1,则输出“validate error”。
若某个数据奇偶校验错误,则输出“parity check error”。
若数据结束符和奇偶校验均不合格,输出“validate error”。
如:11011或11111111111111111。
例如:
1:11101011
2:01001101
3:validate error
源码:
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner a = new Scanner(System.in);
String zifu,b;
int zmy = 0;
String [] zsh = new String[10];
zifu = a.next();
int p = 0,c = 0;
for(int i=0;i<zifu.length();i++)
{
b = zifu.substring(i,i+1);
if(zifu.length()<11)
{
System.out.println("null data");
return;
}
if(b.equalsIgnoreCase("1"))
p++;
}
if(p==zifu.length())
{
System.out.println("null data");
return;
}
for(int i=0;i<zifu.length();i++)
{
b = zifu.substring(i,i+1);
if(b.equalsIgnoreCase("0"))
{
c++;
zmy = 0;
for(int d=0;d<10;d++)
{
zsh[d] = zifu.substring(i+1+d,i+2+d);
if(zsh[d].equals("1")&&d<8){
zmy++;
//System.out.println(zmy);
}
}
if(zsh[9].equals("0"))
System.out.println(c+":validate error");
else if((zmy%2==0 && zsh[8].equals("0"))||(zmy%2!=0&&zsh[8].equals("1")))
System.out.println(c+":parity check error");
else
{
System.out.println(c+":"+zifu.substring(i+1,i+9));
}
i = i+10;
if (zifu.length()-i<11)
break;
//01111111111 0(11)111111111
}
}
}
}

题目集3:
7-1点线形系列1-计算两点之间的距离
输入连个点的坐标,计算两点之间的距离
输入格式:
4个double类型的实数,两个点的x,y坐标,依次是x1、y1、x2、y2,两个点的坐标之间以空格分隔,每个点的x,y坐标以英文“,”分隔。例如:0,0 1,1或0.1,-0.3 +3.5,15.6。
若输入格式非法,输出"Wrong Format"。
若输入格式合法但坐标点的数量超过两个,输出“wrong number of points”。
输出格式:
计算所得的两点之间的距离。例如:1.4142135623730951
源码:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner a = new Scanner(System.in);
String zifu,dian[];
zifu = a.nextLine();
String strarray[] = zifu.split(" ");
double x1,y1,x2,y2;
double num=0.0;
String b = null;
for(int i = 0;i<zifu.length()-2;i++)
{
b = zifu.substring(i,i+2);
if(b.equals("++")||b.equals("+-")||b.equals("-+")||b.equals("--"))
{
System.out.println("Wrong Format");
return;
}
}
String[] zuobiao1 = strarray[0].split(",");
x1 = Double.parseDouble(zuobiao1[0]);
y1 = Double.parseDouble(zuobiao1[1]);
String[] zuobiao2 = strarray[1].split(",");
x2 = Double.parseDouble(zuobiao2[0]);
y2 = Double.parseDouble(zuobiao2[1]);
num = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
for(String i:strarray)
{
dian = i.split(",");
for(String j:dian) {
//正则表达式
if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) {
System.out.print("Wrong Format");
System.exit(0);
}
}
}
if(strarray.length != 2)
{
System.out.println("wrong number of points");
return;
}
if(x1 ==x2&&y1 ==y2)
{
System.out.println("Wrong Format");
return;
}
System.out.println(num);
}
}

7-2 点线形系列2-线的计算
用户输入一组选项和数据,进行与直线有关的计算。选项包括:
1:输入两点坐标,计算斜率,若线条垂直于X轴,输出"Slope does not exist"。
2:输入三个点坐标,输出第一个点与另外两点连线的垂直距离。
3:输入三个点坐标,判断三个点是否在一条线上,输出true或者false。
4:输入四个点坐标,判断前两个点所构成的直线与后两点构成的直线是否平行,输出true或者false.
5:输入四个点坐标,计算输出前两个点所构成的直线与后两点构成的直线的交点坐标,x、y坐标之间以英文分隔",",并输出交叉点是否在两条线段之内(不含四个端点)的判断结果(true/false),判断结果与坐标之间以一个英文空格分隔。若两条线平行,没有交叉点,则输出"is parallel lines,have no intersection point"。
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。
例如:1:0,0 1,1
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
不论哪个选项,如果格式、点数量都符合要求,但构成任一条线的两个点坐标重合,输出"points coincide",
输出格式:
见题目描述。
源码:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner a = new Scanner(System.in);
String zifu,b,c,dian[];
int xh;
zifu = a.nextLine();
b = zifu.substring(2,zifu.length());
c = zifu.substring(1,2);
String strarray[] = b.split(" ");
double x1,y1,x2,y2,x3,y3,x4,y4;
String[] zuobiao1 = strarray[0].split(",");
x1 = Double.parseDouble(zuobiao1[0]);
y1 = Double.parseDouble(zuobiao1[1]);
String[] zuobiao2 = strarray[1].split(",");
x2 = Double.parseDouble(zuobiao2[0]);
y2 = Double.parseDouble(zuobiao2[1]);
xh = Integer.parseInt(zifu.substring(0,1));
if(c.equals(":"))
;
else
{
System.out.println("Wrong Format");
return;
}
if(xh == 1)
{
for(String i:strarray)
{
dian = i.split(",");
for(String j:dian) {
//正则表达式
if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$"))
{
System.out.print("Wrong Format");
System.exit(0);
}
}
}
}
else if(xh == 2||xh == 3)
{
for(String i:strarray)
{
dian = i.split(",");
for(String j:dian) {
//正则表达式
if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$"))
{
System.out.print("Wrong Format");
System.exit(0);
}
}
}
}
else if(xh == 4||xh == 5)
{
for(String i:strarray)
{
dian = i.split(",");
for(String j:dian) {
//正则表达式
if(!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$"))
{
System.out.print("Wrong Format");
System.exit(0);
}
}
}
}
else
{
System.out.println("Wrong Format");
return;
}
if(x1 == x2&&y1 == y2)
{
System.out.println("points coincide");
return;
}
if(xh == 1)
{
double k;
if(x1 == x2)
{
System.out.println("Slope does not exist");
return;
}
k = (y1-y2)/(x1-x2);
System.out.println(k);
}
else if(xh == 2)
{
String[] zuobiao3 = strarray[2].split(",");
x3 = Double.parseDouble(zuobiao3[0]);
y3 = Double.parseDouble(zuobiao3[1]);
if(x1 == x3&&y1 == y3)
{
System.out.println("points coincide");
return;
}
if(x3 == x2&&y3 == y2)
{
System.out.println("points coincide");
return;
}
double d = (Math.abs((y3 - y2) * x1 + (x2 - x3) * y1 + ((x3 * y2) - (x2 * y3)))) / (Math.sqrt(Math.pow(y3 - y2, 2) + Math.pow(x2 - x3, 2)));
System.out.println(d);
}
else if(xh == 3)
{
String[] zuobiao3 = strarray[2].split(",");
x3 = Double.parseDouble(zuobiao3[0]);
y3 = Double.parseDouble(zuobiao3[1]);
if(x1 == x3&&y1 == y3)
{
System.out.println("points coincide");
return;
}
if(x3 == x2&&y3 == y2)
{
System.out.println("points coincide");
return;
}
double d = (Math.abs((y3 - y2) * x1 + (x2 - x3) * y1 + ((x3 * y2) - (x2 * y3)))) / (Math.sqrt(Math.pow(y3 - y2, 2) + Math.pow(x2 - x3, 2)));
if(d == 0.0)
System.out.println("true");
else
{
System.out.println("false");
}
}
else if(xh == 4)
{
String[] zuobiao3 = strarray[2].split(",");
x3 = Double.parseDouble(zuobiao3[0]);
y3 = Double.parseDouble(zuobiao3[1]);
String[] zuobiao4 = strarray[3].split(",");
x4 = Double.parseDouble(zuobiao4[0]);
y4 = Double.parseDouble(zuobiao4[1]);
double k1,k2;
if(x1 == x3&&y1 == y3)
{
System.out.println("points coincide");
return;
}
if(x3 == x2&&y3 == y2)
{
System.out.println("points coincide");
return;
}
if(x1 == x4&&y1 == y4)
{
System.out.println("points coincide");
return;
}
if(x4 == x2&&y4 == y2)
{
System.out.println("points coincide");
return;
}
if(x4 == x3&&y4 == y3)
{
System.out.println("points coincide");
return;
}
if(x1 == x2 && x3 ==x4)
{
System.out.println("true");
return;
}
k1 = (y1-y2)/(x1-x2);
k2 = (y3-y4)/(x3-x4);
double d = (Math.abs((y2 - y1) * x3 + (x1 - x2) * y3 + ((x2 * y1) - (x1 * y2)))) / (Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x1 - x2, 2)));
if(d == 0 && k1 ==k2)
System.out.println("true");
else if(k1 == k2)
System.out.println("true");
else
System.out.println("false");
}
else if(xh == 5)
{
String[] zuobiao3 = strarray[2].split(",");
x3 = Double.parseDouble(zuobiao3[0]);
y3 = Double.parseDouble(zuobiao3[1]);
String[] zuobiao4 = strarray[3].split(",");
x4 = Double.parseDouble(zuobiao4[0]);
y4 = Double.parseDouble(zuobiao4[1]);
double k1,k2;
k1 = (y1-y2)/(x1-x2);
k2 = (y3-y4)/(x3-x4);
if(k1 == k2)
{
System.out.println("is parallel lines,have no intersection point");
return;
}
double A1,B1,C1;
A1 = y2 - y1;
B1 = x1 - x2;
C1 = x2*y1-x1*y2;
double A2,B2,C2;
A2 = y4 - y3;
B2 = x3 - x4;
C2 = x4*y3-x3*y4;
double x,y;
y = (C1 * A2 - C2 * A1) / (A1 * B2 - A2 * B1);
x = (C2 * B1 - C1 * B2) / (A1 * B2 - A2 * B1);
double dax,xiaox,day,xiaoy;
if(x1>x2)
{
dax = x1;
xiaox = x2;
}
else
{
dax = x2;
xiaox = x1;
}
if(y1>y2)
{
day = y1;
xiaoy = y2;
}
else
{
day = y2;
xiaoy = y1;
}
if(xiaox<x&&x<dax&&xiaoy<y&&y<day)
{
System.out.println(x+","+y+" "+"true");
}
if(x3>x4)
{
dax = x3;
xiaox = x4;
}
else
{
dax = x4;
xiaox = x3;
}
if(y3>y4)
{
day = y3;
xiaoy = y4;
}
else
{
day = y4;
xiaoy = y3;
}
if(xiaox<x&&x<dax&&xiaoy<y&&y<day)
{
System.out.println(x+","+y+" "+"true");
}
else
System.out.println(x+","+y+" "+"false");
}
else
{
System.out.println("Wrong Format");
}
}
}

7-3点线形系列3-三角形的计算
用户输入一组选项和数据,进行与三角形有关的计算。选项包括:
1:输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果输出true/false,两个结果之间以一个英文空格符分隔。
2:输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。
3:输入三个点坐标,输出是钝角、直角还是锐角三角形,依次输出三个判断结果(true/false),以一个英文空格分隔,
4:输入五个点坐标,输出前两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积。若直线与三角形一条线重合,输出"The point is on the edge of the triangle"
5:输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外。若点在三角形的某条边上,输出"on the triangle"
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
如果输入的三个点无法构成三角形,输出"data error"。
注意:输出的数据若小数点后超过6位,只保留小数点后6位,多余部分采用四舍五入规则进到最低位。小数点后若不足6位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333333,1.0按格式输出为1.0
选项4中所输入线的两个点坐标重合,输出"points coincide",
源码:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner a = new Scanner(System.in);
String zifu,b,c,dian[];
int xh,geshu = 1;
zifu = a.nextLine();
b = zifu.substring(2,zifu.length());
c = zifu.substring(1,2);
String strarray[] = b.split(" ");
double x1,y1,x2,y2,x3,y3,x4,y4,x5,y5;
String[] zuobiao1 = strarray[0].split(",");
x1 = Double.parseDouble(zuobiao1[0]);
y1 = Double.parseDouble(zuobiao1[1]);
String[] zuobiao2 = strarray[1].split(",");
x2 = Double.parseDouble(zuobiao2[0]);
y2 = Double.parseDouble(zuobiao2[1]);
xh = Integer.parseInt(zifu.substring(0,1));
if(c.equals(":"))
;
else
{
System.out.println("Wrong Format");
return;
}
if(x1 == x2&&y1 == y2)
{
System.out.println("points coincide");
return;
}
if(xh == 1)
{
String[] zuobiao3 = strarray[2].split(",");
x3 = Double.parseDouble(zuobiao3[0]);
y3 = Double.parseDouble(zuobiao3[1]);
double bian1,bian2,bian3;
bian1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
bian2 = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
bian3 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
double d = (Math.abs((y3 - y2) * x1 + (x2 - x3) * y1 + ((x3 * y2) - (x2 * y3)))) / (Math.sqrt(Math.pow(y3 - y2, 2) + Math.pow(x2 - x3, 2)));
if(d == 0)
{
System.out.println("data error");
return;
}
if(bian1 == bian2||bian1 == bian3||bian3 == bian2)
System.out.print("true"+" ");
else
System.out.print("false"+" ");
if(bian1 == bian2 && bian1 == bian3 && bian3 == bian2)
System.out.println("true");
else
System.out.println("false");
}
else if(xh == 2)
{
double x,y;
String[] zuobiao3 = strarray[2].split(",");
x3 = Double.parseDouble(zuobiao3[0]);
y3 = Double.parseDouble(zuobiao3[1]);
double bian1,bian2,bian3,zc,mj;
bian1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
bian2 = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
bian3 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
zc = bian1+bian2+bian3;
double d = (Math.abs((y3 - y2) * x1 + (x2 - x3) * y1 + ((x3 * y2) - (x2 * y3)))) / (Math.sqrt(Math.pow(y3 - y2, 2) + Math.pow(x2 - x3, 2)));
mj = 0.5*d*bian3;
x = (x1 + x2 + x3)/3;
y = (y1 + y2 + y3)/3;
System.out.printf("%.6f",zc);
System.out.print(" ");
System.out.print(mj);
System.out.print(" ");
System.out.print(x);
System.out.print(",");
System.out.printf("%.6f",y);
}
else if(xh == 3)
{
String[] zuobiao3 = strarray[2].split(",");
x3 = Double.parseDouble(zuobiao3[0]);
y3 = Double.parseDouble(zuobiao3[1]);
double bian1,bian2,bian3,zc,mj;
bian1 = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
bian2 = Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
bian3 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
if(bian1*bian1>bian2*bian2+bian3*bian3||bian2*bian2>bian1*bian1+bian3*bian3||bian3*bian3>bian2*bian2+bian1*bian1)
System.out.print("true ");
else
System.out.print("false ");
if(bian1*bian1==bian2*bian2+bian3*bian3||bian2*bian2==bian1*bian1+bian3*bian3||bian3*bian3==bian2*bian2+bian1*bian1)
System.out.print("true ");
else
System.out.print("false ");
if(bian1*bian1<bian2*bian2+bian3*bian3&&bian2*bian2<bian1*bian1+bian3*bian3&&bian3*bian3<bian2*bian2+bian1*bian1)
System.out.print("true");
else
System.out.print("false");
}
else if(xh == 4)
{
String[] zuobiao3 = strarray[2].split(",");
x3 = Double.parseDouble(zuobiao3[0]);
y3 = Double.parseDouble(zuobiao3[1]);
String[] zuobiao4 = strarray[3].split(",");
x4 = Double.parseDouble(zuobiao4[0]);
y4 = Double.parseDouble(zuobiao4[1]);
String[] zuobiao5 = strarray[5].split(",");
x5 = Double.parseDouble(zuobiao4[0]);
y5 = Double.parseDouble(zuobiao4[1]);
double d = (Math.abs((y5 - y4) * x3 + (x4 - x5) * y3 + ((x5 * y4) - (x4 * y5)))) / (Math.sqrt(Math.pow(y5 - y4, 2) + Math.pow(x4 - x5, 2)));
if(d == 0)
{
System.out.println("data error");
return;
}
}
else if(xh == 5)
{
String[] zuobiao3 = strarray[2].split(",");
x3 = Double.parseDouble(zuobiao3[0]);
y3 = Double.parseDouble(zuobiao3[1]);
String[] zuobiao4 = strarray[3].split(",");
x4 = Double.parseDouble(zuobiao4[0]);
y4 = Double.parseDouble(zuobiao4[1]);
double jl1,jl2,jl3,jl;
jl = (Math.abs((y3 - y4) * x2 + (x4 - x3) * y2 + ((x3 * y4) - (x4 * y3)))) / (Math.sqrt(Math.pow(y3 - y4, 2) + Math.pow(x4 - x3, 2)));
jl1 = (Math.abs((y3 - y2) * x1 + (x2 - x3) * y1 + ((x3 * y2) - (x2 * y3)))) / (Math.sqrt(Math.pow(y3 - y2, 2) + Math.pow(x2 - x3, 2)));
jl2 = (Math.abs((y4 - y2) * x1 + (x2 - x4) * y1 + ((x4 * y2) - (x2 * y4)))) / (Math.sqrt(Math.pow(y4 - y2, 2) + Math.pow(x2 - x4, 2)));
jl3 = (Math.abs((y3 - y4) * x1 + (x4 - x3) * y1 + ((x3 * y4) - (x4 * y3)))) / (Math.sqrt(Math.pow(y3 - y4, 2) + Math.pow(x4 - x3, 2)));
if(jl1 == 0||jl2 == 0||jl3 == 0)
{
System.out.println("on the triangle");
}
double bian1,bian2,bian3,sum,mj;
bian1 = Math.sqrt((x4-x2)*(x4-x2)+(y4-y2)*(y4-y2));
bian2 = Math.sqrt((x4-x3)*(x4-x3)+(y4-y3)*(y4-y3));
bian3 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
mj = jl*bian2;
sum = jl1*bian3+jl2*bian1+jl3*bian2;
if(mj == sum)
{
System.out.println("in the triangle");
}
else
System.out.println("outof the triangle");
}
}
}

(3)采坑心得:对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空
出现程序错误时要有返回值。
(4)改进建议:
主要还是要用上正则表达式就可以高效完成对于格式的判断,使用类来简化主类的长度。·
(5)总结:对本阶段三次题目集的综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。
学到了类对于java编程的重要性,此次作业没有分类,导致主类过于冗长,正则表达式需要好好掌握,确实比较难。

浙公网安备 33010602011771号