南昌航空大学 软件学院 pta Java 第三次作业 蔡珂 opp
第三次作业好像是不太一样了hhhh
写正则表达式写到怀疑人生
但机智如我
难点和思路会放在阶段总结性的博客中
这里就贴贴代码
先看题
题目列表
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; // 需要导入 util 包
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// String ch = input.nextLine();
PointTaking a = new PointTaking();
a.ch = input.nextLine();//读入
double ans =a.Solve();//读入答案
if(ans==-1)
System.out.printf("Wrong Format\n");
else if(ans==-2)
System.out.printf("wrong number of points\n");
else
System.out.printf(ans+"\n");
}
}
class Point {//点的类
double x = 0 , y = 0 ;
double DistanceCalculation(Point b) {
return Math.sqrt((this.x-b.x)*(this.x-b.x)+(this.y-b.y)*(this.y-b.y));
}//返回两点距离
}
class PointTaking {
String ch;
double Solve() {
Point a = new Point();
int pos = 0;
Point b = new Point();
// String[]
String[] c = this.ch.split(" ");//将字符串以空格分割
for(String i:c) {//对分割的字符串遍历
String[] d = i.split(",");
for(String j:d) {
// System.out.printf(j+"\n");
if(!j.matches("^[+-]?(([0-9][1-9]*\\.\\d+)|(0\\.\\d+)|([0-9][1-9]*))$"))//正则表达式判断是否合法
return -1;//返回输入错误
if(pos==0)//按位读入坐标x1,y1,x2,y2
a.x =Double.parseDouble(j);
else if(pos == 1)
a.y = Double.parseDouble(j);
else if(pos == 2)
b.x=Double.parseDouble(j);
else
b.y =Double.parseDouble(j);
pos++;
}
}
if(c.length!=2)
return -2;//返回错误点数量
return a.DistanceCalculation(b);
}
}
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; // 需要导入 util 包
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// String ch = input.nextLine();
PointTaking a = new PointTaking();
a.ch = input.nextLine();//读入
double ans =a.Solve();//读入答案
if(ans==-1)
System.out.printf("Wrong Format\n");
else if(ans==-2)
System.out.printf("wrong number of points\n");
else if(ans==1)
System.out.printf("true\n");
else if(ans==2)
System.out.printf("false\n");
}
}
class Line {
Point a ,b;
double slope(){//斜率
if(this.a.x==this.b.x)
return 0;
return (this.a.y-this.b.y)/(this.a.x-this.b.x);
}
double intercept() {//截距
return this.a.x-(this.slope())*this.a.y;
}
double pointVerticalDistanc(Point c){//点到线距离
if(this.a.x==this.b.x)
return Math.abs(c.x-this.a.x);
if(this.a.y==this.b.y)
return Math.abs(c.y-this.a.y);
return Math.abs((this.a.y-this.b.y)*c.x+(this.b.x-this.a.x)*c.y+this.a.x*this.b.y-this.a.y*this.b.x)/
Math.sqrt((this.a.y-this.b.y)*(this.a.y-this.b.y)+(this.a.x-this.b.x)*(this.a.x-this.b.x));
}
Point intersection(Line line){
double t1,t2;
Point point1 = new Point();
point1.x=((line.a.x-line.b.x)*(this.a.y*this.b.x-this.a.x*this.b.y)-(this.a.x-this.b.x)*(line.a.y*line.b.x-line.a.x*line.b.y))/
((this.b.y-this.a.y)*(line.b.x-line.a.x)-(this.b.x-this.a.x)*(line.b.y-line.a.y));
point1.y=((line.a.y-line.b.y)*(this.a.y*this.b.x-this.a.x*this.b.y)-(this.a.y-this.b.y)*(line.a.y*line.b.x-line.a.x*line.b.y))/
((this.b.y-this.a.y)*(line.b.x-line.a.x)-(this.b.x-this.a.x)*(line.b.y-line.a.y));
return point1;
}
}
class Point {//点的类
double x = 0 , y = 0 ;
double DistanceCalculation(Point b) {
return Math.sqrt((this.x-b.x)*(this.x-b.x)+(this.y-b.y)*(this.y-b.y));
}//返回两点距离
}
class PointTaking {
String ch;
double Solve() {
Point[] pointSet = new Point[15];
int pos = 0;
// String[]
String[] splitSet2;
String[] splitSet1 = this.ch.split(":");//将字符串以冒号分割,剥离选项与坐标
if(splitSet1.length!=2)
return -1;
if(!splitSet1[0].matches("[1-5]"))//判断选项是否合规
return -1;
int x = Integer.parseInt(splitSet1[0]);//剥离出第选项
int[] digitalDeposit= {0,2,3,3,4,4};//定义选项要的点数
for(int i=1;i<=10;i++)
pointSet[i] = new Point();//new多个点
splitSet2 = splitSet1[1].split(" ");
for(String i:splitSet2) {//对分割的字符串遍历
String[] splitSet3 = i.split(",");
int count=2;
for(String j:splitSet3) {
if(!j.matches("[+-]?(([1-9]\\d*\\.\\d+)|(0\\.\\d+)|([1-9]\\d*)|0)"))//正则表达式判断是否合法
return -1;//返回输入错误
int pos2 = pos/2 +1;
// System.out.printf(pos2+"");
if((pos&1)==0)//按位读入坐标
pointSet[pos2].x =Double.parseDouble(j);
else if((pos&1)==1)
pointSet[pos2].y = Double.parseDouble(j);
pos++;
count--;
}
if(count!=0)
return -1;
}
if(splitSet2.length!=digitalDeposit[x])
return -2;//返回错误点数量
for(int i=1;i<=digitalDeposit[x];i++){//如果一样则返回
// System.out.printf("%f %f\n",pointSet[i].x,pointSet[i].y);
for(int j = i+1;j<=digitalDeposit[x];j++)
if(pointSet[i].x==pointSet[j].x&&pointSet[i].y==pointSet[j].y) {
System.out.printf("points coincide\n");
return 4;
}
}
Line line = new Line();
if(x==1) {//1
if(pointSet[1].x==pointSet[2].x) {
System.out.printf("Slope does not exist");
return 4;
}
line.a=pointSet[1];
line.b=pointSet[2];
System.out.printf(line.slope()+"");
}
else if(x==2) {
line.a = pointSet[2];
line.b = pointSet[3];
System.out.printf(line.pointVerticalDistanc(pointSet[1])+"");
return 4;
}
else if(x==3){
Line line2= new Line();
line2.a = line.a = pointSet[1];
line.b=pointSet[2];
line2.b = pointSet[3];
if(line2.slope()==line.slope())
return 1;
return 2;
}
else if(x==4){
Line line2= new Line();
line.a = pointSet[1];
line.b = pointSet[2];
line2.a = pointSet[3];
line2.b = pointSet[4];
if((line.a.x-line.b.x)*(line2.a.y-line2.b.y)!=(line2.a.x-line2.b.x)*(line.a.y-line.b.y))
return 2;
return 1;
}
else if(x==5){
Line line2= new Line();
line.a = pointSet[1];
line.b = pointSet[2];
line2.a = pointSet[3];
line2.b = pointSet[4];
if(line2.slope()==line.slope()){
System.out.print("is parallel lines,have no intersection point");
return 4;
}
Point point= line.intersection(line2);
// System.out.printf("111");
System.out.printf(point.x+","+point.y+" ");
if((point.x<Math.max(line.a.x,line.b.x)&&point.x>Math.min(line.a.x,line.b.x))||(point.x<Math.max(line2.a.x,line2.b.x)&&point.x>Math.min(line2.a.x,line2.b.x)))
System.out.printf("true");
else
System.out.printf("false");
}
return 5;
}
}
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; // 需要导入 util 包
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// String ch = input.nextLine();
PointTaking a = new PointTaking();
a.ch = input.nextLine();//读入
double ans =a.Solve();//读入答案
if(ans==-1)
System.out.printf("Wrong Format\n");
else if(ans==-2)
System.out.printf("wrong number of points\n");
else if (ans==-3)
System.out.printf("data error\n");
else if(ans==1)
System.out.printf("true\n");
else if(ans==2)
System.out.printf("false\n");
}
}
class Line {
Point a ,b;
double slope(){//斜率
if(this.a.x==this.b.x)
return 0;
return (this.a.y-this.b.y)/(this.a.x-this.b.x);
}
double intercept() {//截距
return a.x-(this.slope())*a.y;
}
double pointVerticalDistanc(Point c){//点到线距离
if(this.a.x==this.b.x)
return Math.abs(c.x-this.a.x);
return (Math.abs((this.a.y-this.b.y)*c.x+(this.b.x-this.a.x)*c.y+this.a.x*this.b.y-this.a.y*this.b.x)/
(Math.sqrt((this.a.y-this.b.y)*(this.a.y-this.b.y)+(this.a.x-this.b.x)*(this.a.x-this.b.x))));
}
int judgeIntersection(Line line){
if(slope()==line.slope()&&intercept()==line.slope())
return 0;
if(this.slope()==line.slope())
return -1;
Point point = this.intersection(line);
if(point.x<=Math.max(line.a.x,line.b.x)&&point.x>=Math.min(line.a.x,line.b.x)&&point.y<=Math.max(line.a.y,line.b.y)&&point.y>=Math.min(line.a.y,line.b.y))
return 1;
return -1;
}
boolean sameIntersection(Line line1,Line line2){//判断相交点是否相同
Point point1=this.intersection(line1),point2=this.intersection(line2);
if(point2.x==point1.x&&point1.y==point2.y)
return true;
return false;
}
double dis(){
return a.DistanceCalculation(b);
}
boolean judgeParallelism(Line a) {
if(a.slope()==this.slope()) return true;
return false;
}
Point intersection(Line line){//求相交点
double t1,t2;
Point point1 = new Point();
point1.x=((line.a.x-line.b.x)*(this.a.y*this.b.x-this.a.x*this.b.y)-(this.a.x-this.b.x)*(line.a.y*line.b.x-line.a.x*line.b.y))/
((this.b.y-this.a.y)*(line.b.x-line.a.x)-(this.b.x-this.a.x)*(line.b.y-line.a.y));
point1.y=((line.a.y-line.b.y)*(this.a.y*this.b.x-this.a.x*this.b.y)-(this.a.y-this.b.y)*(line.a.y*line.b.x-line.a.x*line.b.y))/
((this.b.y-this.a.y)*(line.b.x-line.a.x)-(this.b.x-this.a.x)*(line.b.y-line.a.y));
return point1;
}
}
class Point {//点的类
double x = 0 , y = 0 ;
double DistanceCalculation(Point b) {
return Math.sqrt((this.x-b.x)*(this.x-b.x)+(this.y-b.y)*(this.y-b.y));
}//返回两点距离
}
class Triangle {
Line a,b,c;
double area() {
double p= (Math.sqrt((a.a.x-a.b.x)*(a.a.x-a.b.x)+(a.a.y-a.b.y)*(a.a.y-a.b.y))+Math.sqrt((b.a.x-b.b.x)*(b.a.x-b.b.x)+(b.a.y-b.b.y)*(b.a.y-b.b.y))+ Math.sqrt((c.a.x-c.b.x)*(c.a.x-c.b.x)+(c.a.y-c.b.y)*(c.a.y-c.b.y)) )/2;
return Math.sqrt(p*(p- Math.sqrt((a.a.x-a.b.x)*(a.a.x-a.b.x)+(a.a.y-a.b.y)*(a.a.y-a.b.y)) )*(p - Math.sqrt((b.a.x-b.b.x)*(b.a.x-b.b.x)+(b.a.y-b.b.y)*(b.a.y-b.b.y)) )*(p- Math.sqrt((c.a.x-c.b.x)*(c.a.x-c.b.x)+(c.a.y-c.b.y)*(c.a.y-c.b.y)) ));
}
}
class PointTaking {
String ch;
public static double printDouble(double num) {
String str = String.format("%.6f",num);
num = Double.parseDouble(str);
return num;
}
public static double vectorComputation(Point a,Point b,Point c){//计算点线是否相交
double x1 = a.x-c.x, y1=a.y-c.y,x2=b.x-c.x,y2=b.y-c.y;
return x1*y2-x2*y1;
}
public static double areaCalculation(Point a,Point b,Point c,Line d,double s) {//计算面积case4
Line aa = new Line();
Line bb = new Line();
aa.a=a;aa.b=b;
bb.a=a;bb.b=c;
Point x1 = aa.intersection(d);
Point x2 = bb.intersection(d);
double q = (a.DistanceCalculation(b)*a.DistanceCalculation(c));
double p = (a.DistanceCalculation(x1)*a.DistanceCalculation(x2));
// System.out.printf(p+" "+q+" ");
// Systemystem.out.printf(s+" ");
return (s*p)/q;
}
double Solve() {
Point[] pointSet = new Point[15];//储存点
int pos = 0;
// String[]
String[] splitSet1 = this.ch.split(":");//将字符串以冒号分割,剥离选项与坐标
if (splitSet1.length != 2)
return -1;
if (!splitSet1[0].matches("[1-5]"))//判断选项是否合规
return -1;
int x = Integer.parseInt(splitSet1[0]);//剥离出第选项
int[] digitalDeposit = {0, 3, 3, 3, 5, 4};//定义选项要的点数
for (int i = 1; i <= 10; i++)
pointSet[i] = new Point();//new多个点
String[] splitSet2 = splitSet1[1].split(" ");
for (String i : splitSet2) {//对分割的字符串遍历
int count=2;
String[] splitSet3 = i.split(",");
for (String j : splitSet3) {
if (!j.matches("^[+-]?(([1-9]\\d*\\.\\d+)|(0\\.\\d+)|([1-9]\\d*)|0)$"))//正则表达式判断是否合法
return -1;//返回输入错误
int pos2 = pos / 2 + 1;
// System.out.printf(pos2+"");
if ((pos & 1) == 0)//按位读入坐标
pointSet[pos2].x = Double.parseDouble(j);
else if ((pos & 1) == 1)
pointSet[pos2].y = Double.parseDouble(j);
pos++;
count--;
}
if(count!=0)
return -1;
}
if (splitSet2.length != digitalDeposit[x])
return -2;//返回错误点数量
Line line = new Line();
if (x == 1) {//1
int num = 0;
double[] distance = new double [5];
distance[1]=pointSet[1].DistanceCalculation(pointSet[2]);
distance[2]=pointSet[1].DistanceCalculation(pointSet[3]);
distance[3]=pointSet[2].DistanceCalculation(pointSet[3]);
Arrays.sort(distance,0,4);
// System.out.printf(distance[1]+" "+distance[2]+" "+distance[3]+"\n");
if(distance[1]+distance[2]<=distance[3])
return -3;
if(distance[1]==distance[2]||distance[2]==distance[3]||distance[3]==distance[1])
System.out.printf("true ");
else
System.out.printf("false ");
if(distance[1]==distance[2]&&distance[1]==distance[3]&&distance[3]==distance[2])
System.out.printf("true");
else
System.out.printf("false");
}
else if (x == 2) {
double[] distance = new double [5];
distance[1]=pointSet[1].DistanceCalculation(pointSet[2]);
distance[2]=pointSet[1].DistanceCalculation(pointSet[3]);
distance[3]=pointSet[2].DistanceCalculation(pointSet[3]);
Arrays.sort(distance,0,4);
if(distance[1]+distance[2]<=distance[3])
return -3;
System.out.printf(printDouble(distance[1]+distance[2]+distance[3])+" "+printDouble(Math.sqrt((distance[1]+distance[2]+distance[3])*(distance[1]+distance[2]-distance[3])*(distance[1]+distance[3]-distance[2])*(distance[2]+distance[3]-distance[1]))/4)+" "+printDouble((pointSet[1].x+pointSet[2].x+pointSet[3].x)/3)+","+printDouble((pointSet[1].y+pointSet[2].y+pointSet[3].y)/3));
}
else if (x == 3) {//判断是啥三角形
double[] distance = new double [5];
distance[1] = pointSet[1].DistanceCalculation(pointSet[2]);
distance[2] = pointSet[1].DistanceCalculation(pointSet[3]);
distance[3] = pointSet[2].DistanceCalculation(pointSet[3]);
Arrays.sort(distance,0,4);
if(distance[1]+distance[2]<=distance[3])
return -3;
if((int)(distance[1]*distance[1])+(int)(distance[2]*distance[2])==(int)(distance[3]*distance[3]))
System.out.printf("false true false");
else if(distance[1]*distance[1]+distance[2]*distance[2]>distance[3]*distance[3])
System.out.printf("false false true");
else
System.out.printf("true false false");
}
else if (x == 4) {
if(pointSet[1].x==pointSet[2].x&&pointSet[1].y==pointSet[2].y) {
System.out.printf("points coincide");
return 5;
}
double[] distance = new double [5];
if((pointSet[5].x==pointSet[3].x&&pointSet[3].x==pointSet[4].x)||(pointSet[5].y==pointSet[3].y&&pointSet[3].y==pointSet[4].y))
return -3;
line.a=pointSet[1];line.b = pointSet[2];
// for(int i=1;i<=5;i++)
// System.out.printf(pointSet[i].x+" "+pointSet[i].y+"\n");
Triangle tri = new Triangle();
tri.a = new Line();
tri.b = new Line();
tri.c = new Line();
tri.a.a = pointSet[3];tri.a.b = pointSet[4];
tri.b.a = pointSet[3];tri.b.b = pointSet[5];
tri.c.a = pointSet[4];tri.c.b = pointSet[5];
int num = 0;
// System.out.printf(line.judgeIntersection(tri.a)+" "+line.judgeIntersection(tri.b)+" "+line.judgeIntersection(tri.c)+"\n");
if(line.judgeIntersection(tri.a)==1)
num++;
if(line.judgeIntersection(tri.b)==1)
num++;
if(line.judgeIntersection(tri.c)==1)
num++;
if(num==0) {
System.out.printf("0");
return 5;
}
if(num==1||(num==2&&(line.sameIntersection(tri.c, tri.a)||line.sameIntersection(tri.b, tri.a)||line.sameIntersection(tri.c, tri.b)))){
System.out.printf("1");
return 5;
}
System.out.printf("2 ");
double s = tri.area(),s2=0;
if(line.judgeIntersection(tri.c)==-1|| line.sameIntersection(tri.c, tri.a)|| line.sameIntersection(tri.b, tri.c)){
s2 = areaCalculation(pointSet[3],pointSet[4],pointSet[5],line,s);
// System.out.printf(1+"\n");
}
else if(line.judgeIntersection(tri.b)==-1||line.sameIntersection(tri.a, tri.b)){
s2= areaCalculation(pointSet[4],pointSet[3],pointSet[5],line,s);
// System.out.printf(2+"\n");
}
else{
s2 = areaCalculation(pointSet[5],pointSet[4],pointSet[3],line,s);
// System.out.printf(3+"\n");
}
// System.out.printf(s2+" ");
System.out.printf(printDouble(Math.min(s2,s-s2))+" "+printDouble(Math.max(s2,s-s2)));
}
else if (x == 5) {
double[] distance = new double [5];
if((pointSet[2].x==pointSet[3].x&&pointSet[3].x==pointSet[4].x)||(pointSet[2].y==pointSet[3].y&&pointSet[3].y==pointSet[4].y))
return -3;
Triangle a= new Triangle(),b= new Triangle(),c= new Triangle(),d= new Triangle();
a.b= new Line();a.a= new Line();a.c = new Line();
b.b= new Line();b.a= new Line();b.c = new Line();
c.b= new Line();c.a= new Line();c.c = new Line();
d.b= new Line();d.a= new Line();d.c = new Line();
a.a.a=pointSet[2];a.a.b = pointSet[3];
a.b.a=pointSet[2];a.b.b = pointSet[4];
a.c.a=pointSet[4];a.c.b = pointSet[3];
b.a.a=pointSet[1];b.a.b = pointSet[3];
b.b.a=pointSet[1];b.b.b = pointSet[4];
b.c.a=pointSet[4];b.c.b = pointSet[3];
c.a.a=pointSet[2];c.a.b = pointSet[1];
c.b.a=pointSet[2];c.b.b = pointSet[4];
c.c.a=pointSet[4];c.c.b = pointSet[1];
d.a.a=pointSet[2];d.a.b = pointSet[3];
d.b.a=pointSet[2];d.b.b = pointSet[1];
d.c.a=pointSet[1];d.c.b = pointSet[3];
if(d.area()==0||b.area()==0||c.area()==0)
System.out.println("on the triangle");
else if(Math.abs(a.area()-b.area()-c.area()-d.area())<0.0005)
System.out.printf("in the triangle");
else
System.out.println("outof the triangle");
}
return 5;
}
}
case4卡我巨久hhhhh
其他到时还好,如果不用类封装真的会写到死