第二次博客作业
前言
本月主要主要学习了继承与多态,正则表达式的运用,容器,流等内容,理解起来可能问题不大,但在实际操作的过程中总会出现一些大大小小的问题,因此这个月PTA作业花费的时间明显比上个月多了许多,很多时候一天都做不出来一道题,对我们逻辑的严谨性要求更严了,也就是说我们不能像以前那样拿到题目就做,而是因该先仔细读题,在内心里构建起最初的框架,用纸笔写出来,最后再用程序去实现,我认为花部分时间去布局,可以有效的提升编程的效率和代码的质量。
本月所学
1、继承于多态
我们在写程序的时候经常会遇到,多个类的模式大体相同,这样会显得代码非常冗余。
例如,PTA题目集06的第二题,最开始我的想法是分别创建4个类L1_2,L2_3,L3_4,L4_1来储存四边形每条线段的长度和对应点的坐标,之后提交的时候应为代码长度超限,所以创建了一个父类Sample,将类L1_2,L2_3,L3_4,L4_1中重复的数据储存到父类中,但很可惜的是,在之后升级代码的时候,我取消了这个设计,因此没有对应代码来展示父类对于代码的精简的重要作用。
其实多态的概念到现在我还不是特别清楚,套用定义来说就是 是指同一行为,具有多个不同表现形式,比方说在父类中定义一个方法print,在A类中重写该方法,在B类中不做任何操作,然后在Main中分别创建A,B的对象a,b。调用方法a.print和b.print,会发现a用的父类定义的方法,b则用的是B类重写后的方法。一个方法干了多件不同的事情就是多态。
2、正则表达式
正则表达式的学习可以在最大程度上优化我们的代码长度。
例如,PTA题目集04的三道题,我当时做这套题目集的时候可以用坐牢来形容,测试点五花八门的格式错误,由于缺乏写代码前的设计环节,所以总是漏掉不同种类的格式错误,在判断格式的环节,无数个if排放在一起,使得在后期检查的时候的工作量非常之大,当时有很多格式错误的要求可谓是困扰我许久。
比如说:00,0.00 (这种错误其实挺有争议的,严格意义上来说00可以看作是0,因此在后面的题目里,取消了这种情况)
小数点数量位置错误以及小数点的存在性 (需要很多if来判断,非常难受)
...
在做题目集04的7-1时,我最开始代码写了快400行,逻辑相当混乱,于是我又推到重写,将代码压缩到了150行,起初我和室友对比了一下,发现自己代码挺简练的,有点沾沾自喜,觉得自己写了一回高质量代码。结果发现隔壁有个人,用50行解决了习题7-1。我当时大为震惊,通过询问才得知了正则表达式的存在,由于没太看懂,加上后面两题错误格式测试点和第一题差不多一样,就没有用正则了。
后面学习了正则之后,才真正意义上了解到了正则表达式的强大之处,这里举几个例子来说明一下
1.题目集06习题7-2
1 num=arr.split("\\s"); //切割字符串 2 for(int i=0;i<num.length;i++) { 3 if(!checkNum(num[i])) { 4 System.out.println("Wrong Format"); 5 passport++; 6 break; 7 } 8 } 9 10 public boolean checkNum(String num) { //检查格式是否错误 11 String reagex="((-?([1-9]\\d*[.]\\d*|0\\[.]\\d*[1-9]\\d*))|-?[1-9]\\d*|0),((-?([1-9]\\d*[.]\\d*|0\\[.]\\d*[1-9]\\d*))|-?[1-9]\\d*|0)"; 12 boolean bool =num.matches(reagex); 13 if(bool) { 14 return true; 15 } 16 else { 17 return false; 18 } 19 }
这是我迭代升级后的代码,num是一个字符串数组,先通过空格去切割坐标并储存带num中,再判断格式是否错误。

1 public String cutArr() { //切割坐标 2 int blankCount=0; 3 for(int i=0;i<arr.length();i++) { 4 if(arr.charAt(i)==' ') { 5 cutArr=arr.substring(0,i); 6 arr=arr.substring(i+1,arr.length()); 7 blankCount++; 8 break; 9 } 10 } 11 if(blankCount==0) { 12 cutArr=arr.substring(0,arr.length()); 13 } 14 return cutArr; 15 } 16 17 public boolean checkNum(String num) { 18 int countOne=0; 19 int count=0; 20 int j=0; 21 //坐标为空 22 if(num=="") { 23 return false; 24 } 25 //冒号错误 26 if(mark.charAt(0)!=':') { 27 return false; 28 } 29 //判断逗号数量是否错误 30 for(int i=0;i<num.length();i++) { 31 if(num.charAt(i)==',') { 32 countOne++; 33 } 34 } 35 if(countOne!=1) { 36 return false; 37 } 38 //判断小数点数量是否错误 39 while(num.charAt(j)!=',') { 40 if(num.charAt(j)=='.') { 41 count++; 42 } 43 j++; 44 } 45 if(count>1) { 46 return false; 47 } 48 count=0; 49 while(j<num.length()) { 50 if(num.charAt(j)=='.') { 51 count++; 52 } 53 j++; 54 } 55 if(count>1) { 56 return false; 57 } 58 for(int i=0;i<num.length();i++) { 59 60 //非法字符 61 if(num.charAt(i)!=',' && num.charAt(i)!='+' && num.charAt(i)!='-' && num.charAt(i)!='.' && (num.charAt(i)<48 || num.charAt(i)>57)) { 62 return false; 63 } 64 //x:00,01 65 if((i==0) && (num.charAt(i)=='0' && (num.charAt(i+1)!='.' && num.charAt(i+1)!=','))) { 66 return false; 67 } 68 //y:00,01 69 if((num.charAt(i)==',' && i!=num.length()-1 && num.charAt(i+1)=='0' && i+2<num.length()) && (num.charAt(i+2)!='.')) { 70 return false; 71 } 72 //+,-后面必须接数字 73 if((i==0) && ((num.charAt(i)!='+' && num.charAt(i)!='-') && (num.charAt(i)<48 || num.charAt(i)>57))) { 74 return false; 75 } 76 //不得有多个+,- 77 if((num.charAt(i)=='+' || num.charAt(i)=='-') && (i!=0 && num.charAt(i-1)!=',')) { 78 return false; 79 } 80 //小数点后必须接数字 81 if((num.charAt(i)=='.') && (i==num.length()-1 || (num.charAt(i+1)<48 || num.charAt(i+1)>57))) { 82 return false; 83 } 84 //逗号前后输入是否有误 85 if((num.charAt(i)==',') && ((i==num.length()-1 || (num.charAt(i-1)<48 || num.charAt(i-1)>57)) || ((num.charAt(i+1)!='+' && num.charAt(i+1)!= '-') && (num.charAt(i+1)<48 || num.charAt(i+1)>57)))) { 86 return false; 87 } 88 //最后一个字符不能是逗号 89 if(num.charAt(num.length()-1)==',') { 90 return false; 91 } 92 } 93 return true; 94 }
这是我改进前的代码,由于长度问题我就将代码折叠了,这两部分代码的功能完全是一摸一样的,但代码的质量可谓是天差地别。
其实正则表达式能做到的不仅仅只是优化一些判断的过程,题目集06的习题7-2第一版完工时代码长度为950(不包括选项4),已将逼近了内存限制。加入正则判断后代码长度为490(写完了所有的选项),当然还是可以继续精简的,比如我的室友在写1,2,3,5四个选项的时候代码只有200行。我相信以上足以说明正则表达式的优越性。
目前我在使用正则的时候大多是套用模板,然后加以修改,并没有完全弄明白正则的语法,尚未达到随机应变的能力层次,所以之后仍要加强对正则的运用和学习。
3、容器
1.LinkedList
2.ArrayList
3.TreeSet
4.HashSet
5.HasLinkedSet
容器可以分为两个大类,List和Set。均用于储存一个种类的数据,可以看作是另一个形式的链表,可以做到,储存,删除,修改查找数据,并且拥有大量的方法去完成这些操作
下面简单介绍一些方法:
public void add(Object element) //增添元素
public void add(int index,Object element) //在指定位置增添元素
public boolean remove(Object o) //删除指定对象
public Object remove(int index) //删除指定位置的元素
public Object set(int index,Object element) //修改指定位置元素的值
public Object get(int index) //获取指定位置元素
public int indexOf(Object o) //获取指定元素的位置
public boolean contains(Object o) //判断指定元素是否存在
public int size() //获取容器中元素个数
public Iterator<E> iterator() //获取迭代器
public void clear() //清空元素
ArrayList适合快速查找元素,LinkedList适合频繁地对列表进行增加或删除元素操作,因此LinkedList类可用于实现堆栈和队列,下面是一些LinkedList独特的方法:
public void add(Object element) //增添元素
public boolean remove(object element) //删除元素
public boolean contains(Object o) //判断元素是否存在
public int size() //获取容器中元素个数
public boolean isEmpty() //判断集合是否为空
public Iterator<E> iterator() //获取迭代器
public void clear() //清空元素
在软工期中考试7-3D点线面问题重构(容器)中,需要创建一个容器类GeometryObject,用于储存Element类型的数据,我对容器的概念相当的模糊,所以在做题的时候我感觉其实就是在用ArrayList来解决问题。
题目分析
PTA题目集4 7-1 点线形系列1-计算两点之间的距离
从现在的角度来看,这道题的难度不大,有了正则表达式可以轻松解决问题,即便不用正则,只要逻辑清晰,也能高效完成本题。
类图:
代码:

1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 String numOne; 8 String numTwo; 9 String numThree; 10 11 double x1; 12 double y1; 13 double x2; 14 double y2; 15 double distance; 16 17 int k=0; 18 Scanner in = new Scanner(System.in); 19 String numArr = in.nextLine(); 20 checkNum num = new checkNum(numArr); 21 k=num.ifTwo(); 22 if(k==0) { 23 System.out.println("Wrong Format"); 24 } 25 else if(k==1) { 26 numOne=num.cutArr(); 27 numTwo=num.cutArr(); 28 checkNum arrOne = new checkNum(numOne); 29 checkNum arrTwo = new checkNum(numTwo); 30 if(!arrOne.checkArr()) { 31 System.out.println("Wrong Format"); 32 } 33 else if(!arrTwo.checkArr()) { 34 System.out.println("Wrong Format"); 35 } 36 else { 37 x1=arrOne.cutX(numOne); 38 y1=arrOne.cutY(numOne); 39 x2=arrTwo.cutX(numTwo); 40 y2=arrTwo.cutY(numTwo); 41 distance=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); 42 System.out.println(distance); 43 } 44 } 45 else if(k>1) { 46 numOne=num.cutArr(); 47 numTwo=num.cutArr(); 48 numThree=num.cutArr(); 49 checkNum arrOne = new checkNum(numOne); 50 checkNum arrTwo = new checkNum(numTwo); 51 checkNum arrThree =new checkNum(numThree); 52 if(!arrOne.checkArr()) { 53 System.out.println("Wrong Format"); 54 } 55 else if(!arrTwo.checkArr()) { 56 System.out.println("Wrong Format"); 57 } 58 else if(!arrThree.checkArr()) { 59 System.out.println("Wrong format"); 60 } 61 else { 62 System.out.println("wrong number of points"); 63 } 64 } 65 } 66 } 67 68 class checkNum { 69 70 String num; 71 String cutNum; 72 String loctOne; 73 String loctTwo; 74 String loctThree; 75 int allBlank=0; 76 77 public checkNum(String numArr) { 78 num=numArr.substring(0,numArr.length()); 79 } 80 81 //判断是否有两个坐标 82 public int ifTwo() { 83 for(int i=0;i<num.length();i++) { 84 if(num.charAt(i)==' ') { 85 allBlank++; 86 } 87 } 88 return allBlank; 89 } 90 91 //切割字符串 92 public String cutArr() { 93 int blankCount=0; 94 for(int i=0;i<num.length();i++) { 95 if(num.charAt(i)==' ') { 96 cutNum=num.substring(0,i); 97 num=num.substring(i+1,num.length()); 98 blankCount++; 99 break; 100 } 101 } 102 if(blankCount==0) { 103 cutNum=num.substring(0,num.length()); 104 } 105 return cutNum; 106 } 107 108 //检查坐标格式是否正确 109 public boolean checkArr() { 110 for(int i=0;i<num.length();i++) { 111 if((i==0) && (num.charAt(i+1)=='0' && num.charAt(i+2)!='.')) { 112 return false; 113 } 114 if((i==0) && ((num.charAt(i)!='+' && num.charAt(i)!='-') && (num.charAt(i)<48 || num.charAt(i)>57))) { 115 return false; 116 } 117 if((num.charAt(i)=='.') && (i==num.length()-1 || (num.charAt(i+1)<48 || num.charAt(i+1)>57))) { 118 return false; 119 } 120 if((num.charAt(i)==',') && ((i==num.length()-1 || (num.charAt(i-1)<48 || num.charAt(i-1)>57)) || ((num.charAt(i+1)!='+' && num.charAt(i+1)!= '-') && (num.charAt(i+1)<48 || num.charAt(i+1)>57)))) { 121 return false; 122 } 123 if((num.charAt(i)=='+' || num.charAt(i)=='-') && ((num.charAt(i+1)<48 || num.charAt(i+1)>57) || (i==num.length()-1))) { 124 return false; 125 } 126 if(num.charAt(i)==' ') { 127 return false; 128 } 129 } 130 return true; 131 } 132 133 //切割得x的值 134 public double cutX(String arr) { 135 String X; 136 double x=0; 137 for(int i=0;i<arr.length();i++) { 138 if(arr.charAt(i)==',') { 139 X=arr.substring(0,i); 140 x=Double.parseDouble(X); 141 break; 142 } 143 } 144 return x; 145 } 146 147 //切割得y的值 148 public double cutY(String arr) { 149 String Y; 150 double y=0; 151 for(int i=0;i<arr.length();i++) { 152 if(arr.charAt(i)==',') { 153 Y=arr.substring(i+1,arr.length()); 154 y=Double.parseDouble(Y); 155 break; 156 } 157 } 158 return y; 159 } 160 }
PTA题目集4 7-2点线形系列2-线的计算
本体是上一题的迭代版本,难度不大,重点在于,等于关系,需要用精确值。
例:x1=x2,判断条件为:Math.abs(x1-x2)<0.001。
类图:
代码:

1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 cauTangle what = new cauTangle(); 8 what.option(); 9 } 10 } 11 12 class cauTangle { 13 14 Scanner in = new Scanner(System.in); 15 16 String arr; //输入的字符串 17 String cutArr; //坐标 18 String head; //字符串前缀 19 String mark; //英文冒号 20 String choice; //选项 21 22 int wrong=0; 23 int Choice=0; //选项 24 int allBlank=0; //空格的数量 25 26 double l1=0; 27 double l2=0; 28 double l3=0; 29 30 double L1=0; 31 double L2=0; 32 double L3=0; 33 34 double s1=0; 35 double s2=0; 36 double s3=0; 37 double S=0; 38 39 double x1=0; 40 double y1=0; 41 double x2=0; 42 double y2=0; 43 double x3=0; 44 double y3=0; 45 double x4=0; 46 double y4=0; 47 double x5=0; 48 double y5=0; 49 50 String numOne; 51 String numTwo; 52 String numThree; 53 String numFour; 54 String numFive; 55 String numSix; 56 57 public void option() { 58 inPut(); 59 if(arr.length()==1) { 60 System.out.println("Wrong Format"); 61 wrong++; 62 } 63 else { 64 cutHead(); 65 cutOption(); 66 if(arr.charAt(0)==' ') { 67 System.out.println("Wrong Format"); 68 wrong++; 69 } 70 } 71 if((wrong==0) && (choice.charAt(0)=='1' || choice.charAt(0)=='2' || choice.charAt(0)=='3' || choice.charAt(0)=='4' || choice.charAt(0)=='5')) { 72 Choice=Integer.parseInt(choice); 73 } 74 else { 75 System.out.println("Wrong Format"); 76 wrong++; 77 } 78 if(wrong==0) { 79 switch(Choice) { 80 case 1: 81 isqWeather(); 82 break; 83 case 2: 84 algorTra(); 85 break; 86 case 3: 87 whatAngle(); 88 break; 89 case 4: 90 centreNum(); 91 break; 92 case 5: 93 outWeather(); 94 } 95 } 96 } 97 98 //输入字符串 99 public void inPut() { 100 arr=in.nextLine(); 101 } 102 103 //切去头部 104 public void cutHead() { 105 head=arr.substring(0,2); 106 arr=arr.substring(2,arr.length()); 107 } 108 109 //提取选项与符号 110 public void cutOption() { 111 choice=head.substring(0,1); 112 mark=head.substring(1,2); 113 } 114 115 //切割坐标 116 public String cutArr() { 117 int blankCount=0; 118 for(int i=0;i<arr.length();i++) { 119 if(arr.charAt(i)==' ') { 120 cutArr=arr.substring(0,i); 121 arr=arr.substring(i+1,arr.length()); 122 blankCount++; 123 break; 124 } 125 } 126 if(blankCount==0) { 127 cutArr=arr.substring(0,arr.length()); 128 } 129 return cutArr; 130 } 131 132 //计算空格的个数 133 public int numBlank() { 134 for(int i=0;i<arr.length();i++) { 135 if(arr.charAt(i)==' ' && i!=arr.length()-1) { 136 allBlank++; 137 } 138 } 139 return allBlank; 140 } 141 142 //判断输入数据是多于所需数据 143 public boolean numData(int n) { 144 switch(Choice) { 145 case 1: 146 if(allBlank!=2) { 147 return false; 148 } 149 else { 150 return true; 151 } 152 case 2: 153 if(allBlank!=2) { 154 return false; 155 } 156 else { 157 return true; 158 } 159 case 3: 160 if(allBlank!=2) { 161 return false; 162 } 163 else { 164 return true; 165 } 166 case 4: 167 if(allBlank!=4) { 168 return false; 169 } 170 else { 171 return true; 172 } 173 case 5: 174 if(allBlank!=3) { 175 return false; 176 } 177 else { 178 return true; 179 } 180 } 181 return false; 182 } 183 184 //切割得x的值 185 public double cutX(String arr) { 186 String X; 187 double x=0; 188 for(int i=0;i<arr.length();i++) { 189 if(arr.charAt(i)==',') { 190 X=arr.substring(0,i); 191 x=Double.parseDouble(X); 192 break; 193 } 194 } 195 return x; 196 } 197 198 //切割得y的值 199 public double cutY(String arr) { 200 String Y; 201 double y=0; 202 for(int i=0;i<arr.length();i++) { 203 if(arr.charAt(i)==',') { 204 Y=arr.substring(i+1,arr.length()); 205 y=Double.parseDouble(Y); 206 break; 207 } 208 } 209 return y; 210 } 211 212 //检查格式是否错误 213 public boolean checkNum(String num) { 214 int countOne=0; 215 int count=0; 216 int j=0; 217 //坐标为空 218 if(num=="") { 219 return false; 220 } 221 //冒号错误 222 if(mark.charAt(0)!=':') { 223 return false; 224 } 225 //判断逗号数量是否错误 226 for(int i=0;i<num.length();i++) { 227 if(num.charAt(i)==',') { 228 countOne++; 229 } 230 } 231 if(countOne!=1) { 232 return false; 233 } 234 //判断小数点数量是否错误 235 while(num.charAt(j)!=',') { 236 if(num.charAt(j)=='.') { 237 count++; 238 } 239 j++; 240 } 241 if(count>1) { 242 return false; 243 } 244 count=0; 245 while(j<num.length()) { 246 if(num.charAt(j)=='.') { 247 count++; 248 } 249 j++; 250 } 251 if(count>1) { 252 return false; 253 } 254 for(int i=0;i<num.length();i++) { 255 256 //非法字符 257 if(num.charAt(i)!=',' && num.charAt(i)!='+' && num.charAt(i)!='-' && num.charAt(i)!='.' && (num.charAt(i)<48 || num.charAt(i)>57)) { 258 return false; 259 } 260 //x:00,01 261 if((i==0) && (num.charAt(i)=='0' && (num.charAt(i+1)!='.' && num.charAt(i+1)!=','))) { 262 return false; 263 } 264 //y:00,01 265 if((num.charAt(i)==',' && i!=num.length()-1 && num.charAt(i+1)=='0' && i+2<num.length()) && (num.charAt(i+2)!='.')) { 266 return false; 267 } 268 //+,-后面必须接数字 269 if((i==0) && ((num.charAt(i)!='+' && num.charAt(i)!='-') && (num.charAt(i)<48 || num.charAt(i)>57))) { 270 return false; 271 } 272 //不得有多个+,- 273 if((num.charAt(i)=='+' || num.charAt(i)=='-') && (i!=0 && num.charAt(i-1)!=',')) { 274 return false; 275 } 276 //小数点后必须接数字 277 if((num.charAt(i)=='.') && (i==num.length()-1 || (num.charAt(i+1)<48 || num.charAt(i+1)>57))) { 278 return false; 279 } 280 //逗号前后输入是否有误 281 if((num.charAt(i)==',') && ((i==num.length()-1 || (num.charAt(i-1)<48 || num.charAt(i-1)>57)) || ((num.charAt(i+1)!='+' && num.charAt(i+1)!= '-') && (num.charAt(i+1)<48 || num.charAt(i+1)>57)))) { 282 return false; 283 } 284 //最后一个字符不能是逗号 285 if(num.charAt(num.length()-1)==',') { 286 return false; 287 } 288 } 289 return true; 290 } 291 292 public boolean traWeather() { 293 if(l1+l2>l3 && l2+l3>l1 && l1+l3>l2) { 294 return true; 295 } 296 else { 297 return false; 298 } 299 } 300 301 public boolean parWeather(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { 302 double k1=0; 303 double k2=0; 304 // System.out.println("1"); 305 if(x3!=x4 && x3!=x4) { 306 k1=(y3-y4)/(x3-x4); 307 k2=(y1-y2)/(x1-x2); 308 if(k1==k2) { 309 return true; 310 } 311 } 312 if(x3!=x4 && x3==x4) { 313 return false; 314 } 315 if(x3==x4 && x3!=x4) { 316 return false; 317 } 318 if(x3==x4 && x1==x2) { 319 return true; 320 } 321 return false; 322 } 323 324 public boolean sameWeather(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { 325 double k1=0; 326 double k2=0; 327 double k3=0; 328 if(x3==x4) 329 if(x1==x3) { 330 return true; 331 } 332 else { 333 return false; 334 } 335 else { 336 k1=(y3-y4)/(x3-x4); 337 k2=(y2-y3)/(x2-x3); 338 k3=(y2-y4)/(x2-x4); 339 if(k1==k2 || k3==k1) { 340 return true; 341 } 342 else { 343 return false; 344 } 345 } 346 } 347 348 //case 1 349 public void isqWeather() { 350 if(numData(numBlank())) { 351 numOne=cutArr(); 352 numTwo=cutArr(); 353 numThree=cutArr(); 354 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree)) { 355 x1=cutX(numOne); 356 y1=cutY(numOne); 357 x2=cutX(numTwo); 358 y2=cutY(numTwo); 359 x3=cutX(numThree); 360 y3=cutY(numThree); 361 l1=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); 362 l2=Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)); 363 l3=Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2)); 364 if(traWeather()) { 365 if((Math.abs(l1-l2)<0.000001) || (Math.abs(l1-l3)<0.000001) || (Math.abs(l2-l3)<0.000001)) { 366 System.out.print("true"+" "); 367 } 368 else { 369 System.out.print("false"+" "); 370 } 371 if(l1==l2 && l2==l3) { 372 System.out.print("true"); 373 } 374 else { 375 System.out.print("false"); 376 } 377 } 378 else { 379 System.out.println("data error"); 380 } 381 } 382 else { 383 System.out.println("Wrong Format"); 384 } 385 } 386 else { 387 if(allBlank<2) { 388 numOne=cutArr(); 389 numTwo=cutArr(); 390 if(checkNum(numOne) && checkNum(numTwo)) { 391 System.out.println("wrong number of points"); 392 } 393 else { 394 System.out.println("Wrong Format"); 395 } 396 } 397 else if(allBlank>2) { 398 numOne=cutArr(); 399 numTwo=cutArr(); 400 numThree=cutArr(); 401 numFour=cutArr(); 402 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour)) { 403 System.out.println("wrong number of points"); 404 } 405 else { 406 System.out.println("Wrong Format"); 407 } 408 } 409 } 410 } 411 412 //case 2 413 public void algorTra() { 414 double x=0; 415 double y=0; 416 double area=0; 417 double c=0; 418 419 if(numData(numBlank())) { 420 numOne=cutArr(); 421 numTwo=cutArr(); 422 numThree=cutArr(); 423 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree)) { 424 x1=cutX(numOne); 425 y1=cutY(numOne); 426 x2=cutX(numTwo); 427 y2=cutY(numTwo); 428 x3=cutX(numThree); 429 y3=cutY(numThree); 430 l1=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); 431 l2=Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)); 432 l3=Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2)); 433 if(traWeather()) { 434 x=(x1+x2+x3)/3; 435 y=(y1+y2+y3)/3; 436 area=0.5*(x1*y2-x1*y3+x2*y3-x2*y1+x3*y1-x2*y2); 437 c=l1+l2+l3; 438 System.out.println((double)Math.round(c*1000000)/1000000+" "+(double)Math.round(area*1000000)/1000000+" "+(double)Math.round(x*1000000)/1000000+","+(double)Math.round(y*1000000)/1000000); 439 } 440 else { 441 System.out.println("data error"); 442 } 443 } 444 else { 445 System.out.println("Wrong Format"); 446 } 447 } 448 else { 449 if(allBlank<2) { 450 numOne=cutArr(); 451 numTwo=cutArr(); 452 if(checkNum(numOne) && checkNum(numTwo)) { 453 System.out.println("wrong number of points"); 454 } 455 else { 456 System.out.println("Wrong Format"); 457 } 458 } 459 else if(allBlank>2) { 460 numOne=cutArr(); 461 numTwo=cutArr(); 462 numThree=cutArr(); 463 numFour=cutArr(); 464 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour)) { 465 System.out.println("wrong number of points"); 466 } 467 else { 468 System.out.println("Wrong Format"); 469 } 470 } 471 } 472 } 473 474 //case 3 475 public void whatAngle() { 476 477 if(numData(numBlank())) { 478 numOne=cutArr(); 479 numTwo=cutArr(); 480 numThree=cutArr(); 481 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree)) { 482 x1=cutX(numOne); 483 y1=cutY(numOne); 484 x2=cutX(numTwo); 485 y2=cutY(numTwo); 486 x3=cutX(numThree); 487 y3=cutY(numThree); 488 l1=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); 489 l2=Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)); 490 l3=Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2)); 491 L1=l1*l1; 492 L2=l2*l2; 493 L3=l3*l3; 494 if(traWeather()) { 495 //钝角 496 if((L1-(L2+L3))>0.000001 || (L2-L1-L3)>0.000001 || (L3-L1-L2)>0.000001) { 497 System.out.print("true"+" "); 498 } 499 else { 500 System.out.print("false"+" "); 501 } 502 //直角 503 if(Math.abs(L1-(L2+L3))<0.00001 || Math.abs(L2-(L1+L3))<0.00001 || Math.abs(L3-(L1+L2))<0.00001) { 504 System.out.print("true"+" "); 505 } 506 else { 507 System.out.print("false"+" "); 508 } 509 //锐角 510 if(L1-L2-L3<-0.000001 && L2-L1-L3<-0.000001 && L3-L1-L2<-0.000001) { 511 System.out.print("true"); 512 } 513 else { 514 System.out.print("false"); 515 } 516 } 517 else { 518 System.out.println("data error"); 519 } 520 } 521 } 522 else { 523 if(allBlank<2) { 524 numOne=cutArr(); 525 numTwo=cutArr(); 526 if(checkNum(numOne) && checkNum(numTwo)) { 527 System.out.println("wrong number of points"); 528 } 529 else { 530 System.out.println("Wrong Format"); 531 } 532 } 533 else if(allBlank>2) { 534 numOne=cutArr(); 535 numTwo=cutArr(); 536 numThree=cutArr(); 537 numFour=cutArr(); 538 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour)) { 539 System.out.println("wrong number of points"); 540 } 541 else { 542 System.out.println("Wrong Format"); 543 } 544 } 545 } 546 } 547 548 //case 4 549 public void centreNum() { 550 //第一条直线与每条边的交点 551 double pX1=0; 552 double pY1=0; 553 double pX2=0; 554 double pY2=0; 555 double pX3=0; 556 double pY3=0; 557 558 double S=0; 559 double s1=0; 560 double s2=0; 561 double temp=0; 562 563 int countOne=0; 564 int countTwo=0; 565 int countThree=0; 566 int wrongPiont1=0; 567 int wrongPiont2=0; 568 int wrongPiont3=0; 569 int allCount=0; 570 int wrongPiont=0; 571 int num=0; 572 573 574 if(numData(numBlank())) { 575 numOne=cutArr(); 576 numTwo=cutArr(); 577 numThree=cutArr(); 578 numFour=cutArr(); 579 numFive=cutArr(); 580 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour) && checkNum(numFive)) { 581 x1=cutX(numOne); 582 y1=cutY(numOne); 583 x2=cutX(numTwo); 584 y2=cutY(numTwo); 585 x3=cutX(numThree); 586 y3=cutY(numThree); 587 x4=cutX(numFour); 588 y4=cutY(numFour); 589 x5=cutX(numFive); 590 y5=cutY(numFive); 591 if(x1==x2 && y1==y2) { 592 System.out.println("points coincide"); 593 } 594 else { 595 l1=Math.sqrt(Math.pow(x3-x4,2)+Math.pow(y3-y4,2)); 596 l2=Math.sqrt(Math.pow(x3-x5,2)+Math.pow(y3-y5,2)); 597 l3=Math.sqrt(Math.pow(x4-x5,2)+Math.pow(y4-y5,2)); 598 L1=l1*l1; 599 L2=l2*l2; 600 L3=l3*l3; 601 if(traWeather()) { 602 if(parWeather(x1,y1,x2,y2,x3,y3,x4,y4)) { 603 if(sameWeather(x1,y1,x2,y2,x3,y3,x4,y4)) { 604 System.out.print("The point is on the edge of the triangle"); 605 } 606 else { 607 pX2=(-((x3-x5)*(x2*y1-x1*y2)-(x1-x2)*(x5*y3-x3*y5))/((y3-y5)*(x1-x2)-(y1-y2)*(x3-x5))); //x1y1x2y2 和 x3y3x5y5 608 pY2=(-((y3-y5)*(y2*x1-y1*x2)-(y1-y2)*(y5*x3-y3*x5))/((x3-x5)*(y1-y2)-(x1-x2)*(y3-y5))); 609 pX3=(-((x5-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y5-x5*y4))/((y5-y4)*(x1-x2)-(y1-y2)*(x5-x4))); //x1y1x2y2 和 x4y4x5y5 610 pY3=(-((y5-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x5-y5*x4))/((x5-x4)*(y1-y2)-(x1-x2)*(y5-y4))); 611 if((x3==x5 && ((pY2>=y3 && pY1<=y5) || (pY2>=y5 && pY2<=y3)))) { //x3y3与x5y5构成的直线垂直 612 if(pY2!=y3 && pY2!=y5) { 613 countTwo++; 614 } 615 if(pY2==y3 && wrongPiont1==0) { 616 wrongPiont1++; 617 } 618 if(pY2==y5 && wrongPiont3==0) { 619 wrongPiont3++; 620 } 621 } 622 if(x3!=x5 && ((pX2>=x3 && pX2<=x5) || (pX2>=x5 && pX2<=x3))) { //不垂直 623 if(pX2!=x3 && pX2!=x5) { 624 countTwo++; 625 } 626 if(pX2==x3 && wrongPiont1==0) { 627 wrongPiont1++; 628 } 629 if(pX2==x5 && wrongPiont3==0) { 630 wrongPiont3++; 631 } 632 } 633 if((x4==x5 && ((pY3>=y4 && pY3<=y5) || (pY3>=y5 && pY3<=y4)))) { //x4y4与x5y5构成的直线垂直 634 if(pY3!=y3 && pY3!=y4) { 635 countThree++; 636 } 637 if(pY3==y4 && wrongPiont1==0) { 638 wrongPiont2++; 639 } 640 if(pY3==y5 && wrongPiont2==0) { 641 wrongPiont3++; 642 } 643 } 644 if(x4!=x5 && ((pX3>=x4 && pX3<=x5) || (pX3>=x5 && pX3<=x4))) { //不垂直 645 if(pX3!=x4 && pX3!=x5) { 646 countThree++; 647 } 648 if(pX3==x4 && wrongPiont2==0) { 649 wrongPiont2++; 650 } 651 if(pX3==x5 && wrongPiont3==0) { 652 wrongPiont3++; 653 } 654 } 655 wrongPiont=wrongPiont3+wrongPiont2+wrongPiont1; 656 allCount=countOne+countTwo+countThree; 657 if(wrongPiont+allCount==0) { 658 System.out.print("0"); 659 } 660 if(wrongPiont==1) { 661 System.out.print("1"); 662 } 663 else { 664 System.out.print(allCount+" "); 665 S=0.5*Math.abs((x4*y5-x5*y4)+(x5*y3-x3*y5)+(x3*y4-x4*y3)); 666 s1=0.5*Math.abs((pX3*pY2-pX2*pY3)+(pX2*y5-x5*pY2)+(x5*pY3-pX3*y5)); 667 s2=S-s1; 668 if(Math.abs(s2)>Math.abs(s1)) { 669 temp=s2; 670 s2=s1; 671 s1=temp; 672 } 673 s1=Math.abs(s1); 674 s2=Math.abs(s2); 675 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 676 } 677 } 678 } 679 680 else if(parWeather(x1,y1,x2,y2,x3,y3,x5,y5)) { 681 if(sameWeather(x1,y1,x2,y2,x3,y3,x5,y5)) { 682 System.out.print("The point is on the edge of the triangle"); 683 } 684 else { 685 pX1=(-((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4))); //x1y1x2y2 和 x3y3x4y4 686 pY1=(-((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4))); 687 pX3=(-((x5-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y5-x5*y4))/((y5-y4)*(x1-x2)-(y1-y2)*(x5-x4))); //x1y1x2y2 和 x4y4x5y5 688 pY3=(-((y5-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x5-y5*x4))/((x5-x4)*(y1-y2)-(x1-x2)*(y5-y4))); 689 if((x3==x4 && ((pY1>=y3 && pY1<=y4) || (pY1>=y4 && pY1<=y3)))) { //x3y3与x4y4构成的直线垂直 690 if(pY1!=y3 && pY1!=y4) { 691 countOne++; 692 } 693 if(pY1==y3) { 694 wrongPiont1++; 695 } 696 if(pY1==y4) { 697 wrongPiont2++; 698 } 699 } 700 if(x3!=x4 && ((pX1>=x3 && pX1<=x4) || (pX1>=x4 && pX1<=x3))) { //不垂直 701 if(pX1!=x3 && pX1!=x4) { 702 countOne++; 703 } 704 if(pX1==x3) { 705 wrongPiont1++; 706 } 707 if(pX1==x4) { 708 wrongPiont2++; 709 } 710 } 711 if((x4==x5 && ((pY3>=y4 && pY3<=y5) || (pY3>=y5 && pY3<=y4)))) { //x4y4与x5y5构成的直线垂直 712 if(pY3!=y3 && pY3!=y4) { 713 countThree++; 714 } 715 if(pY3==y4 && wrongPiont2==0) { 716 wrongPiont2++; 717 } 718 if(pY3==y5 && wrongPiont3==0) { 719 wrongPiont3++; 720 } 721 } 722 if(x4!=x5 && ((pX3>=x4 && pX3<=x5) || (pX3>=x5 && pX3<=x4))) { //不垂直 723 if(pX3!=x4 && pX3!=x5) { 724 countThree++; 725 } 726 if(pX3==x4 && wrongPiont2==0) { 727 wrongPiont2++; 728 } 729 if(pX3==x5 && wrongPiont3==0) { 730 wrongPiont3++; 731 } 732 } 733 wrongPiont=wrongPiont3+wrongPiont2+wrongPiont1; 734 allCount=countOne+countTwo+countThree; 735 if(wrongPiont+allCount==0) { 736 System.out.print("0"); 737 } 738 else if(wrongPiont==1) { 739 System.out.println("1"); 740 } 741 else { 742 System.out.print(allCount+" "); 743 S=0.5*Math.abs((x4*y5-x5*y4)+(x5*y3-x3*y5)+(x3*y4-x4*y3)); 744 s1=0.5*Math.abs((pX1*pY3-pX3*pY1)+(pX3*y4-x4*pY3)+(x4*pY1-pX1*y4)); 745 s2=S-s1; 746 if(Math.abs(s2)>Math.abs(s1)) { 747 temp=s2; 748 s2=s1; 749 s1=temp; 750 } 751 s1=Math.abs(s1); 752 s2=Math.abs(s2); 753 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 754 } 755 } 756 } 757 758 else if(parWeather(x1,y1,x2,y2,x4,y4,x5,y5)) { 759 if(sameWeather(x1,y1,x2,y2,x4,y4,x5,y5)) { 760 System.out.print("The point is on the edge of the triangle"); 761 } 762 else { 763 pX1=(-((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4))); //x1y1x2y2 和 x3y3x4y4 764 pY1=(-((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4))); 765 pX2=(-((x3-x5)*(x2*y1-x1*y2)-(x1-x2)*(x5*y3-x3*y5))/((y3-y5)*(x1-x2)-(y1-y2)*(x3-x5))); //x1y1x2y2 和 x3y3x5y5 766 pY2=(-((y3-y5)*(y2*x1-y1*x2)-(y1-y2)*(y5*x3-y3*x5))/((x3-x5)*(y1-y2)-(x1-x2)*(y3-y5))); 767 if((x3==x4 && ((pY1>=y3 && pY1<=y4) || (pY1>=y4 && pY1<=y3)))) { //x3y3与x4y4构成的直线垂直 768 if(pY1!=y3 && pY1!=y4) { 769 countOne++; 770 } 771 if(pY1==y3) { 772 wrongPiont1++; 773 } 774 if(pY1==y4) { 775 wrongPiont2++; 776 } 777 } 778 if(x3!=x4 && ((pX1>=x3 && pX1<=x4) || (pX1>=x4 && pX1<=x3))) { //不垂直 779 if(pX1!=x3 && pX1!=x4) { 780 countOne++; 781 } 782 if(pX1==x3) { 783 wrongPiont1++; 784 } 785 if(pX1==x4) { 786 wrongPiont2++; 787 } 788 } 789 if((x3==x5 && ((pY2>=y3 && pY1<=y5) || (pY2>=y5 && pY2<=y3)))) { //x3y3与x5y5构成的直线垂直 790 if(pY2!=y3 && pY2!=y5) { 791 countTwo++; 792 } 793 if(pY2==y3 && wrongPiont1==0) { 794 wrongPiont1++; 795 } 796 if(pY2==y5 && wrongPiont3==0) { 797 wrongPiont3++; 798 } 799 } 800 if(x3!=x5 && ((pX2>=x3 && pX2<=x5) || (pX2>=x5 && pX2<=x3))) { //不垂直 801 if(pX2!=x3 && pX2!=x5) { 802 countTwo++; 803 } 804 if(pX2==x3 && wrongPiont1==0) { 805 wrongPiont1++; 806 } 807 if(pX2==x5 && wrongPiont3==0) { 808 wrongPiont3++; 809 } 810 } 811 wrongPiont=wrongPiont3+wrongPiont2+wrongPiont1; 812 allCount=countOne+countTwo+countThree; 813 if(wrongPiont+allCount==0) { 814 System.out.print("0"); 815 } 816 else if(wrongPiont==1) { 817 System.out.println("1"); 818 } 819 else { 820 System.out.print(allCount+" "); 821 S=0.5*Math.abs((x4*y5-x5*y4)+(x5*y3-x3*y5)+(x3*y4-x4*y3)); 822 s1=0.5*Math.abs((pX1*pY2-pX2*pY1)+(pX2*y3-x3*pY2)+(x3*pY1-pX1*y3)); 823 s2=S-s1; 824 if(Math.abs(s2)>Math.abs(s1)) { 825 temp=s2; 826 s2=s1; 827 s1=temp; 828 } 829 s1=Math.abs(s1); 830 s2=Math.abs(s2); 831 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 832 } 833 } 834 } 835 836 else { 837 pX1=(-((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4))); //x1y1x2y2 和 x3y3x4y4 838 pY1=(-((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4))); 839 pX2=(-((x3-x5)*(x2*y1-x1*y2)-(x1-x2)*(x5*y3-x3*y5))/((y3-y5)*(x1-x2)-(y1-y2)*(x3-x5))); //x1y1x2y2 和 x3y3x5y5 840 pY2=(-((y3-y5)*(y2*x1-y1*x2)-(y1-y2)*(y5*x3-y3*x5))/((x3-x5)*(y1-y2)-(x1-x2)*(y3-y5))); 841 pX3=(-((x5-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y5-x5*y4))/((y5-y4)*(x1-x2)-(y1-y2)*(x5-x4))); //x1y1x2y2 和 x4y4x5y5 842 pY3=(-((y5-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x5-y5*x4))/((x5-x4)*(y1-y2)-(x1-x2)*(y5-y4))); 843 // System.out.println(pX1+" "+pY1+" "+pX2+" "+pY2+" "+pX3+" "+pY3); 844 //三角形的三个顶点 845 //判断交点是否在线段内部 846 if((x3==x4 && ((pY1>=y3 && pY1<=y4) || (pY1>=y4 && pY1<=y3)))) { //x3y3与x4y4构成的直线垂直 847 if(pY1!=y3 && pY1!=y4) { 848 countOne++; 849 } 850 if(pY1==y3 && wrongPiont1==0) { 851 wrongPiont1++; 852 } 853 if(pY1==y4 && wrongPiont2==0) { 854 wrongPiont2++; 855 } 856 } 857 if(x3!=x4 && ((pX1>=x3 && pX1<=x4) || (pX1>=x4 && pX1<=x3))) { //不垂直 858 if(pX1!=x3 && pX1!=x4) { 859 countOne++; 860 } 861 if(pX1==x3 && wrongPiont1==0) { 862 wrongPiont1++; 863 } 864 if(pX1==x4 && wrongPiont2==0) { 865 wrongPiont2++; 866 } 867 } 868 if((x3==x5 && ((pY2>=y3 && pY1<=y5) || (pY2>=y5 && pY2<=y3)))) { //x3y3与x5y5构成的直线垂直 869 if(pY2!=y3 && pY2!=y5) { 870 countTwo++; 871 } 872 if(pY2==y3 && wrongPiont1==0) { 873 wrongPiont1++; 874 } 875 if(pY2==y5 && wrongPiont3==0) { 876 wrongPiont3++; 877 } 878 } 879 if(x3!=x5 && ((pX2>=x3 && pX2<=x5) || (pX2>=x5 && pX2<=x3))) { //不垂直 880 if(pX2!=x3 && pX2!=x5) { 881 countTwo++; 882 } 883 if(pX2==x3 && wrongPiont1==0) { 884 wrongPiont1++; 885 } 886 if(pX2==x5 && wrongPiont3==0) { 887 wrongPiont3++; 888 } 889 } 890 if((x4==x5 && ((pY3>=y4 && pY3<=y5) || (pY3>=y5 && pY3<=y4)))) { //x4y4与x5y5构成的直线垂直 891 if(pY3!=y3 && pY3!=y4) { 892 countThree++; 893 } 894 if(pY3==y4 && wrongPiont2==0) { 895 wrongPiont2++; 896 } 897 if(pY3==y5 && wrongPiont3==0) { 898 wrongPiont3++; 899 } 900 } 901 if(x4!=x5 && ((pX3>=x4 && pX3<=x5) || (pX3>=x5 && pX3<=x4))) { //不垂直 902 if(pX3!=x4 && pX3!=x5) { 903 countThree++; 904 } 905 if(pX3==x4 && wrongPiont2==0) { 906 wrongPiont2++; 907 } 908 if(pX3==x5 && wrongPiont3==0) { 909 wrongPiont3++; 910 } 911 } 912 wrongPiont=wrongPiont1+wrongPiont2+wrongPiont3; 913 allCount=countOne+countTwo+countThree; 914 if(allCount+wrongPiont==0) { 915 System.out.print("0"); 916 } 917 else if(wrongPiont==1 && allCount==0) { 918 System.out.print("1"); 919 } 920 else if(wrongPiont!=2){ 921 System.out.print((allCount+wrongPiont)+" "); 922 } 923 if(wrongPiont<=1) { 924 if(allCount+wrongPiont==2) { 925 if(wrongPiont==0) { 926 if(countOne==1 && countTwo==1) { 927 //x3 pX1 pX2 928 S=0.5*Math.abs((x4*y5-x5*y4)+(x5*y3-x3*y5)+(x3*y4-x4*y3)); 929 s1=0.5*Math.abs((pX1*pY2-pX2*pY1)+(pX2*y3-x3*pY2)+(x3*pY1-pX1*y3)); 930 s2=S-s1; 931 if(Math.abs(s2)>Math.abs(s1)) { 932 temp=s2; 933 s2=s1; 934 s1=temp; 935 } 936 s1=Math.abs(s1); 937 s2=Math.abs(s2); 938 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 939 } 940 if(countOne==1 && countThree==1) { 941 //x4 pX1 pX3 942 S=0.5*Math.abs(x4*y5+x5*y3+x3*y4-x4*y3-x5*y4-x3*y5); 943 s1=0.5*Math.abs((pX1*pY3-pX3*pY1)+(pX3*y4-x4*pY3)+(x4*pY1-pX1*y4)); 944 // S=0.5*Math.abs(x4*y2+x2*y3+x3*y4-x4*y3-x2*y4-x3*y2); 945 s2=S-s1; 946 if(Math.abs(s2)>Math.abs(s1)) { 947 temp=s2; 948 s2=s1; 949 s1=temp; 950 } 951 System.out.print(Math.abs(s1)+" "+Math.abs(s2)); 952 } 953 if(countTwo==1 && countThree==1) { 954 //x5 pX2 pX3 955 S=0.5*Math.abs((x4*y5-x5*y4)+(x5*y3-x3*y5)+(x3*y4-x4*y3)); 956 s1=0.5*Math.abs((pX3*pY2-pX2*pY3)+(pX2*y5-x5*pY2)+(x5*pY3-pX3*y5)); 957 s2=S-s1; 958 if(Math.abs(s2)>Math.abs(s1)) { 959 temp=s2; 960 s2=s1; 961 s1=temp; 962 } 963 s1=Math.abs(s1); 964 s2=Math.abs(s2); 965 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 966 } 967 } 968 else if(wrongPiont==1) { 969 // System.out.println("1"); 970 if(wrongPiont1==1) { 971 //px3 x3 x4 972 // System.out.println(pX3+" "+pY3); 973 S=0.5*Math.abs(x4*y5+x5*y3+x3*y4-x4*y3-x5*y4-x3*y5); 974 s1=0.5*Math.abs(x4*pY3+pX3*y3+x3*y4-x4*y3-pX3*y4-x3*pY3); 975 s2=S-s1; 976 if(Math.abs(s2)>Math.abs(s1)) { 977 temp=s2; 978 s2=s1; 979 s1=temp; 980 } 981 s1=Math.abs(s1); 982 s2=Math.abs(s2); 983 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 984 } 985 if(wrongPiont2==1) { 986 // System.out.println(pX2+" "+pY2); 987 S=0.5*Math.abs(x4*y5+x5*y3+x3*y4-x4*y3-x5*y4-x3*y5); 988 s1=0.5*Math.abs(x4*pY2+pX2*y3+x3*y4-x4*y3-pX2*y4-x3*pY2); //px2 x4 x3 989 s2=S-s1; 990 if(Math.abs(s2)>Math.abs(s1)) { 991 temp=s2; 992 s2=s1; 993 s1=temp; 994 } 995 s1=Math.abs(s1); 996 s2=Math.abs(s2); 997 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 998 } 999 if(wrongPiont3==1) { 1000 // System.out.println(pX1+" "+pY1); 1001 S=0.5*Math.abs(x4*y5+x5*y3+x3*y4-x4*y3-x5*y4-x3*y5); 1002 s1=0.5*Math.abs((pX1*y5-x5*pY1)+(x5*y4-x4*y5)+(x4*pY1-pX1*y4)); 1003 s2=S-s1; 1004 if(Math.abs(s2)>Math.abs(s1)) { 1005 temp=s2; 1006 s2=s1; 1007 s1=temp; 1008 } 1009 s1=Math.abs(s1); 1010 s2=Math.abs(s2); 1011 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 1012 } 1013 } 1014 } 1015 } 1016 } 1017 if(wrongPiont==2) { 1018 System.out.print("The point is on the edge of the triangle"); //* 1019 } 1020 } 1021 else { 1022 System.out.println("data error"); 1023 } 1024 } 1025 } 1026 else { 1027 System.out.println("Wrong Format"); 1028 } 1029 } 1030 else { 1031 if(allBlank<4) { 1032 numOne=cutArr(); 1033 numTwo=cutArr(); 1034 numThree=cutArr(); 1035 numFour=cutArr(); 1036 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour)) { 1037 System.out.println("wrong number of points"); 1038 } 1039 else { 1040 System.out.println("Wrong Format"); 1041 } 1042 } 1043 else if(allBlank>4) { 1044 numOne=cutArr(); 1045 numTwo=cutArr(); 1046 numThree=cutArr(); 1047 numFour=cutArr(); 1048 numFive=cutArr(); 1049 numSix=cutArr(); 1050 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour) && checkNum(numFive) && checkNum(numSix)) { 1051 System.out.println("wrong number of points"); 1052 } 1053 else { 1054 System.out.println("Wrong Format"); 1055 } 1056 } 1057 } 1058 } 1059 1060 //case 5 1061 public void outWeather() { 1062 if(numData(numBlank())) { 1063 numOne=cutArr(); 1064 numTwo=cutArr(); 1065 numThree=cutArr(); 1066 numFour=cutArr(); 1067 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour)) { 1068 x1=cutX(numOne); 1069 y1=cutY(numOne); 1070 x2=cutX(numTwo); 1071 y2=cutY(numTwo); 1072 x3=cutX(numThree); 1073 y3=cutY(numThree); 1074 x4=cutX(numFour); 1075 y4=cutY(numFour); 1076 1077 1078 l1=Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)); 1079 l2=Math.sqrt(Math.pow(x3-x4,2)+Math.pow(y3-y4,2)); 1080 l3=Math.sqrt(Math.pow(x2-x4,2)+Math.pow(y2-y4,2)); 1081 L1=l1*l1; 1082 L2=l2*l2; 1083 L3=l3*l3; 1084 if(traWeather()) { 1085 S=0.5*Math.abs(x4*y2+x2*y3+x3*y4-x4*y3-x2*y4-x3*y2); 1086 s1=0.5*Math.abs(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2); 1087 s2=0.5*Math.abs(x1*y4+x4*y3+x3*y1-x1*y3-x4*y1-x3*y4); 1088 s3=0.5*Math.abs(x1*y2+x2*y4+x4*y1-x1*y4-x2*y1-x4*y2); 1089 if(s1==0 || s2==0 || s3==0) { 1090 System.out.println("on the triangle"); 1091 } 1092 else if(S==s1+s2+s3) { 1093 System.out.println("in the triangle"); 1094 } 1095 else { 1096 System.out.println("outof the triangle"); 1097 } 1098 } 1099 else { 1100 System.out.println("data error"); 1101 } 1102 1103 } 1104 else { 1105 System.out.println("Wrong Format"); 1106 } 1107 } 1108 else { 1109 if(allBlank<4) { 1110 numOne=cutArr(); 1111 numTwo=cutArr(); 1112 numThree=cutArr(); 1113 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree)) { 1114 System.out.println("wrong number of points"); 1115 } 1116 else { 1117 System.out.println("Wrong Format"); 1118 } 1119 } 1120 else if(allBlank>4) { 1121 numOne=cutArr(); 1122 numTwo=cutArr(); 1123 numThree=cutArr(); 1124 numFour=cutArr(); 1125 numFive=cutArr(); 1126 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour) && checkNum(numFive)) { 1127 System.out.println("wrong number of points"); 1128 } 1129 else { 1130 System.out.println("Wrong Format"); 1131 } 1132 } 1133 } 1134 } 1135 }
PTA题目集4 7-3点线形系列3-三角形的计算


1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 cauTangle what = new cauTangle(); 8 what.option(); 9 } 10 } 11 12 class cauTangle { 13 14 Scanner in = new Scanner(System.in); 15 16 String arr; //输入的字符串 17 String cutArr; //坐标 18 String head; //字符串前缀 19 String mark; //英文冒号 20 String choice; //选项 21 22 int wrong=0; 23 int Choice=0; //选项 24 int allBlank=0; //空格的数量 25 26 double l1=0; 27 double l2=0; 28 double l3=0; 29 30 double L1=0; 31 double L2=0; 32 double L3=0; 33 34 double s1=0; 35 double s2=0; 36 double s3=0; 37 double S=0; 38 39 double x1=0; 40 double y1=0; 41 double x2=0; 42 double y2=0; 43 double x3=0; 44 double y3=0; 45 double x4=0; 46 double y4=0; 47 double x5=0; 48 double y5=0; 49 50 String numOne; 51 String numTwo; 52 String numThree; 53 String numFour; 54 String numFive; 55 String numSix; 56 57 public void option() { 58 inPut(); 59 if(arr.length()==1) { 60 System.out.println("Wrong Format"); 61 wrong++; 62 } 63 else { 64 cutHead(); 65 cutOption(); 66 if(arr.charAt(0)==' ') { 67 System.out.println("Wrong Format"); 68 wrong++; 69 } 70 } 71 if((wrong==0) && (choice.charAt(0)=='1' || choice.charAt(0)=='2' || choice.charAt(0)=='3' || choice.charAt(0)=='4' || choice.charAt(0)=='5')) { 72 Choice=Integer.parseInt(choice); 73 } 74 else { 75 System.out.println("Wrong Format"); 76 wrong++; 77 } 78 if(wrong==0) { 79 switch(Choice) { 80 case 1: 81 isqWeather(); 82 break; 83 case 2: 84 algorTra(); 85 break; 86 case 3: 87 whatAngle(); 88 break; 89 case 4: 90 centreNum(); 91 break; 92 case 5: 93 outWeather(); 94 } 95 } 96 } 97 98 //输入字符串 99 public void inPut() { 100 arr=in.nextLine(); 101 } 102 103 //切去头部 104 public void cutHead() { 105 head=arr.substring(0,2); 106 arr=arr.substring(2,arr.length()); 107 } 108 109 //提取选项与符号 110 public void cutOption() { 111 choice=head.substring(0,1); 112 mark=head.substring(1,2); 113 } 114 115 //切割坐标 116 public String cutArr() { 117 int blankCount=0; 118 for(int i=0;i<arr.length();i++) { 119 if(arr.charAt(i)==' ') { 120 cutArr=arr.substring(0,i); 121 arr=arr.substring(i+1,arr.length()); 122 blankCount++; 123 break; 124 } 125 } 126 if(blankCount==0) { 127 cutArr=arr.substring(0,arr.length()); 128 } 129 return cutArr; 130 } 131 132 //计算空格的个数 133 public int numBlank() { 134 for(int i=0;i<arr.length();i++) { 135 if(arr.charAt(i)==' ' && i!=arr.length()-1) { 136 allBlank++; 137 } 138 } 139 return allBlank; 140 } 141 142 //判断输入数据是多于所需数据 143 public boolean numData(int n) { 144 switch(Choice) { 145 case 1: 146 if(allBlank!=2) { 147 return false; 148 } 149 else { 150 return true; 151 } 152 case 2: 153 if(allBlank!=2) { 154 return false; 155 } 156 else { 157 return true; 158 } 159 case 3: 160 if(allBlank!=2) { 161 return false; 162 } 163 else { 164 return true; 165 } 166 case 4: 167 if(allBlank!=4) { 168 return false; 169 } 170 else { 171 return true; 172 } 173 case 5: 174 if(allBlank!=3) { 175 return false; 176 } 177 else { 178 return true; 179 } 180 } 181 return false; 182 } 183 184 //切割得x的值 185 public double cutX(String arr) { 186 String X; 187 double x=0; 188 for(int i=0;i<arr.length();i++) { 189 if(arr.charAt(i)==',') { 190 X=arr.substring(0,i); 191 x=Double.parseDouble(X); 192 break; 193 } 194 } 195 return x; 196 } 197 198 //切割得y的值 199 public double cutY(String arr) { 200 String Y; 201 double y=0; 202 for(int i=0;i<arr.length();i++) { 203 if(arr.charAt(i)==',') { 204 Y=arr.substring(i+1,arr.length()); 205 y=Double.parseDouble(Y); 206 break; 207 } 208 } 209 return y; 210 } 211 212 //检查格式是否错误 213 public boolean checkNum(String num) { 214 int countOne=0; 215 int count=0; 216 int j=0; 217 //坐标为空 218 if(num=="") { 219 return false; 220 } 221 //冒号错误 222 if(mark.charAt(0)!=':') { 223 return false; 224 } 225 //判断逗号数量是否错误 226 for(int i=0;i<num.length();i++) { 227 if(num.charAt(i)==',') { 228 countOne++; 229 } 230 } 231 if(countOne!=1) { 232 return false; 233 } 234 //判断小数点数量是否错误 235 while(num.charAt(j)!=',') { 236 if(num.charAt(j)=='.') { 237 count++; 238 } 239 j++; 240 } 241 if(count>1) { 242 return false; 243 } 244 count=0; 245 while(j<num.length()) { 246 if(num.charAt(j)=='.') { 247 count++; 248 } 249 j++; 250 } 251 if(count>1) { 252 return false; 253 } 254 for(int i=0;i<num.length();i++) { 255 256 //非法字符 257 if(num.charAt(i)!=',' && num.charAt(i)!='+' && num.charAt(i)!='-' && num.charAt(i)!='.' && (num.charAt(i)<48 || num.charAt(i)>57)) { 258 return false; 259 } 260 //x:00,01 261 if((i==0) && (num.charAt(i)=='0' && (num.charAt(i+1)!='.' && num.charAt(i+1)!=','))) { 262 return false; 263 } 264 //y:00,01 265 if((num.charAt(i)==',' && i!=num.length()-1 && num.charAt(i+1)=='0' && i+2<num.length()) && (num.charAt(i+2)!='.')) { 266 return false; 267 } 268 //+,-后面必须接数字 269 if((i==0) && ((num.charAt(i)!='+' && num.charAt(i)!='-') && (num.charAt(i)<48 || num.charAt(i)>57))) { 270 return false; 271 } 272 //不得有多个+,- 273 if((num.charAt(i)=='+' || num.charAt(i)=='-') && (i!=0 && num.charAt(i-1)!=',')) { 274 return false; 275 } 276 //小数点后必须接数字 277 if((num.charAt(i)=='.') && (i==num.length()-1 || (num.charAt(i+1)<48 || num.charAt(i+1)>57))) { 278 return false; 279 } 280 if((num.charAt(i)==',') && ((i==num.length()-1 || (num.charAt(i-1)<48 || num.charAt(i-1)>57)) || ((num.charAt(i+1)!='+' && num.charAt(i+1)!= '-') && (num.charAt(i+1)<48 || num.charAt(i+1)>57)))) { 281 return false; 282 } 283 if(num.charAt(num.length()-1)==',') { 284 return false; 285 } 286 } 287 return true; 288 } 289 public boolean traWeather() { 290 if(l1+l2>l3 && l2+l3>l1 && l1+l3>l2) { 291 return true; 292 } 293 else { 294 return false; 295 } 296 } 297 public boolean parWeather(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { 298 double k1=0; 299 double k2=0; 300 if(x3!=x4 && x3!=x4) { 301 k1=(y3-y4)/(x3-x4); 302 k2=(y1-y2)/(x1-x2); 303 if(k1==k2) { 304 return true; 305 } 306 } 307 if(x3!=x4 && x3==x4) { 308 return false; 309 } 310 if(x3==x4 && x3!=x4) { 311 return false; 312 } 313 if(x3==x4 && x1==x2) { 314 return true; 315 } 316 return false; 317 } 318 public boolean sameWeather(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) { 319 double k1=0; 320 double k2=0; 321 double k3=0; 322 if(x3==x4) 323 if(x1==x3) { 324 return true; 325 } 326 else { 327 return false; 328 } 329 else { 330 k1=(y3-y4)/(x3-x4); 331 k2=(y2-y3)/(x2-x3); 332 k3=(y2-y4)/(x2-x4); 333 if(k1==k2 || k3==k1) { 334 return true; 335 } 336 else { 337 return false; 338 } 339 } 340 } 341 342 //case 1 343 public void isqWeather() { 344 if(numData(numBlank())) { 345 numOne=cutArr(); 346 numTwo=cutArr(); 347 numThree=cutArr(); 348 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree)) { 349 x1=cutX(numOne); 350 y1=cutY(numOne); 351 x2=cutX(numTwo); 352 y2=cutY(numTwo); 353 x3=cutX(numThree); 354 y3=cutY(numThree); 355 l1=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); 356 l2=Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)); 357 l3=Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3, 2)); 358 if(traWeather()) { 359 if((Math.abs(l1-l2)<0.000001) || (Math.abs(l1-l3)<0.000001) || (Math.abs(l2-l3)<0.000001)) { 360 System.out.print("true"+" "); 361 } 362 else { 363 System.out.print("false"+" "); 364 } 365 if(l1==l2 && l2==l3) { 366 System.out.print("true"); 367 } 368 else { 369 System.out.print("false"); 370 } 371 } 372 else { 373 System.out.println("data error"); 374 } 375 } 376 else { 377 System.out.println("Wrong Format"); 378 } 379 } 380 else { 381 if(allBlank<2) { 382 numOne=cutArr(); 383 numTwo=cutArr(); 384 if(checkNum(numOne) && checkNum(numTwo)) { 385 System.out.println("wrong number of points"); 386 } 387 else { 388 System.out.println("Wrong Format"); 389 } 390 } 391 else if(allBlank>2) { 392 numOne=cutArr(); 393 numTwo=cutArr(); 394 numThree=cutArr(); 395 numFour=cutArr(); 396 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour)) { 397 System.out.println("wrong number of points"); 398 } 399 else { 400 System.out.println("Wrong Format"); 401 } 402 } 403 } 404 } 405 406 //case 2 407 public void algorTra() { 408 double x=0; 409 double y=0; 410 double area=0; 411 double c=0; 412 413 if(numData(numBlank())) { 414 numOne=cutArr(); 415 numTwo=cutArr(); 416 numThree=cutArr(); 417 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree)) { 418 x1=cutX(numOne); 419 y1=cutY(numOne); 420 x2=cutX(numTwo); 421 y2=cutY(numTwo); 422 x3=cutX(numThree); 423 y3=cutY(numThree); 424 l1=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); 425 l2=Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)); 426 l3=Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2)); 427 if(traWeather()) { 428 x=(x1+x2+x3)/3; 429 y=(y1+y2+y3)/3; 430 area=0.5*(x1*y2-x1*y3+x2*y3-x2*y1+x3*y1-x2*y2); 431 c=l1+l2+l3; 432 System.out.println((double)Math.round(c*1000000)/1000000+" "+(double)Math.round(area*1000000)/1000000+" "+(double)Math.round(x*1000000)/1000000+","+(double)Math.round(y*1000000)/1000000); 433 } 434 else { 435 System.out.println("data error"); 436 } 437 } 438 else { 439 System.out.println("Wrong Format"); 440 } 441 } 442 else { 443 if(allBlank<2) { 444 numOne=cutArr(); 445 numTwo=cutArr(); 446 if(checkNum(numOne) && checkNum(numTwo)) { 447 System.out.println("wrong number of points"); 448 } 449 else { 450 System.out.println("Wrong Format"); 451 } 452 } 453 else if(allBlank>2) { 454 numOne=cutArr(); 455 numTwo=cutArr(); 456 numThree=cutArr(); 457 numFour=cutArr(); 458 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour)) { 459 System.out.println("wrong number of points"); 460 } 461 else { 462 System.out.println("Wrong Format"); 463 } 464 } 465 } 466 } 467 468 //case 3 469 public void whatAngle() { 470 471 if(numData(numBlank())) { 472 numOne=cutArr(); 473 numTwo=cutArr(); 474 numThree=cutArr(); 475 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree)) { 476 x1=cutX(numOne); 477 y1=cutY(numOne); 478 x2=cutX(numTwo); 479 y2=cutY(numTwo); 480 x3=cutX(numThree); 481 y3=cutY(numThree); 482 l1=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); 483 l2=Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)); 484 l3=Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3,2)); 485 L1=l1*l1; 486 L2=l2*l2; 487 L3=l3*l3; 488 if(traWeather()) { 489 //钝角 490 if((L1-(L2+L3))>0.000001 || (L2-L1-L3)>0.000001 || (L3-L1-L2)>0.000001) { 491 System.out.print("true"+" "); 492 } 493 else { 494 System.out.print("false"+" "); 495 } 496 //直角 497 if(Math.abs(L1-(L2+L3))<0.00001 || Math.abs(L2-(L1+L3))<0.00001 || Math.abs(L3-(L1+L2))<0.00001) { 498 System.out.print("true"+" "); 499 } 500 else { 501 System.out.print("false"+" "); 502 } 503 //锐角 504 if(L1-L2-L3<-0.000001 && L2-L1-L3<-0.000001 && L3-L1-L2<-0.000001) { 505 System.out.print("true"); 506 } 507 else { 508 System.out.print("false"); 509 } 510 } 511 else { 512 System.out.println("data error"); 513 } 514 } 515 } 516 else { 517 if(allBlank<2) { 518 numOne=cutArr(); 519 numTwo=cutArr(); 520 if(checkNum(numOne) && checkNum(numTwo)) { 521 System.out.println("wrong number of points"); 522 } 523 else { 524 System.out.println("Wrong Format"); 525 } 526 } 527 else if(allBlank>2) { 528 numOne=cutArr(); 529 numTwo=cutArr(); 530 numThree=cutArr(); 531 numFour=cutArr(); 532 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour)) { 533 System.out.println("wrong number of points"); 534 } 535 else { 536 System.out.println("Wrong Format"); 537 } 538 } 539 } 540 } 541 542 //case 4 543 public void centreNum() { 544 //第一条直线与每条边的交点 545 double pX1=0; 546 double pY1=0; 547 double pX2=0; 548 double pY2=0; 549 double pX3=0; 550 double pY3=0; 551 552 double S=0; 553 double s1=0; 554 double s2=0; 555 double temp=0; 556 557 int countOne=0; 558 int countTwo=0; 559 int countThree=0; 560 int wrongPiont1=0; 561 int wrongPiont2=0; 562 int wrongPiont3=0; 563 int allCount=0; 564 int wrongPiont=0; 565 int num=0; 566 567 568 if(numData(numBlank())) { 569 numOne=cutArr(); 570 numTwo=cutArr(); 571 numThree=cutArr(); 572 numFour=cutArr(); 573 numFive=cutArr(); 574 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour) && checkNum(numFive)) { 575 x1=cutX(numOne); 576 y1=cutY(numOne); 577 x2=cutX(numTwo); 578 y2=cutY(numTwo); 579 x3=cutX(numThree); 580 y3=cutY(numThree); 581 x4=cutX(numFour); 582 y4=cutY(numFour); 583 x5=cutX(numFive); 584 y5=cutY(numFive); 585 if(x1==x2 && y1==y2) { 586 System.out.println("points coincide"); 587 } 588 else { 589 l1=Math.sqrt(Math.pow(x3-x4,2)+Math.pow(y3-y4,2)); 590 l2=Math.sqrt(Math.pow(x3-x5,2)+Math.pow(y3-y5,2)); 591 l3=Math.sqrt(Math.pow(x4-x5,2)+Math.pow(y4-y5,2)); 592 L1=l1*l1; 593 L2=l2*l2; 594 L3=l3*l3; 595 if(traWeather()) { 596 if(parWeather(x1,y1,x2,y2,x3,y3,x4,y4)) { 597 if(sameWeather(x1,y1,x2,y2,x3,y3,x4,y4)) { 598 System.out.print("The point is on the edge of the triangle"); 599 } 600 else { 601 pX2=(-((x3-x5)*(x2*y1-x1*y2)-(x1-x2)*(x5*y3-x3*y5))/((y3-y5)*(x1-x2)-(y1-y2)*(x3-x5))); //x1y1x2y2 和 x3y3x5y5 602 pY2=(-((y3-y5)*(y2*x1-y1*x2)-(y1-y2)*(y5*x3-y3*x5))/((x3-x5)*(y1-y2)-(x1-x2)*(y3-y5))); 603 pX3=(-((x5-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y5-x5*y4))/((y5-y4)*(x1-x2)-(y1-y2)*(x5-x4))); //x1y1x2y2 和 x4y4x5y5 604 pY3=(-((y5-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x5-y5*x4))/((x5-x4)*(y1-y2)-(x1-x2)*(y5-y4))); 605 if((x3==x5 && ((pY2>=y3 && pY1<=y5) || (pY2>=y5 && pY2<=y3)))) { //x3y3与x5y5构成的直线垂直 606 if(pY2!=y3 && pY2!=y5) { 607 countTwo++; 608 } 609 if(pY2==y3 && wrongPiont1==0) { 610 wrongPiont1++; 611 } 612 if(pY2==y5 && wrongPiont3==0) { 613 wrongPiont3++; 614 } 615 } 616 if(x3!=x5 && ((pX2>=x3 && pX2<=x5) || (pX2>=x5 && pX2<=x3))) { //不垂直 617 if(pX2!=x3 && pX2!=x5) { 618 countTwo++; 619 } 620 if(pX2==x3 && wrongPiont1==0) { 621 wrongPiont1++; 622 } 623 if(pX2==x5 && wrongPiont3==0) { 624 wrongPiont3++; 625 } 626 } 627 if((x4==x5 && ((pY3>=y4 && pY3<=y5) || (pY3>=y5 && pY3<=y4)))) { //x4y4与x5y5构成的直线垂直 628 if(pY3!=y5 && pY3!=y4) { 629 countThree++; 630 } 631 if(pY3==y4 && wrongPiont1==0) { 632 wrongPiont2++; 633 } 634 if(pY3==y5 && wrongPiont2==0) { 635 wrongPiont3++; 636 } 637 } 638 if(x4!=x5 && ((pX3>=x4 && pX3<=x5) || (pX3>=x5 && pX3<=x4))) { //不垂直 639 if(pX3!=x4 && pX3!=x5) { 640 countThree++; 641 } 642 if(pX3==x4 && wrongPiont2==0) { 643 wrongPiont2++; 644 } 645 if(pX3==x5 && wrongPiont3==0) { 646 wrongPiont3++; 647 } 648 } 649 wrongPiont=wrongPiont3+wrongPiont2+wrongPiont1; 650 allCount=countOne+countTwo+countThree; 651 if(wrongPiont+allCount==0) { 652 System.out.print("0"); 653 } 654 if(wrongPiont==1) { 655 System.out.print("1"); 656 } 657 else { 658 System.out.print(allCount+" "); 659 S=0.5*Math.abs((x4*y5-x5*y4)+(x5*y3-x3*y5)+(x3*y4-x4*y3)); 660 s1=0.5*Math.abs((pX3*pY2-pX2*pY3)+(pX2*y5-x5*pY2)+(x5*pY3-pX3*y5)); 661 s2=S-s1; 662 if(Math.abs(s2)>Math.abs(s1)) { 663 temp=s2; 664 s2=s1; 665 s1=temp; 666 } 667 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 668 } 669 } 670 } 671 672 else if(parWeather(x1,y1,x2,y2,x3,y3,x5,y5)) { 673 if(sameWeather(x1,y1,x2,y2,x3,y3,x5,y5)) { 674 System.out.print("The point is on the edge of the triangle"); 675 } 676 else { 677 pX1=(-((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4))); //x1y1x2y2 和 x3y3x4y4 678 pY1=(-((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4))); 679 pX3=(-((x5-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y5-x5*y4))/((y5-y4)*(x1-x2)-(y1-y2)*(x5-x4))); //x1y1x2y2 和 x4y4x5y5 680 pY3=(-((y5-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x5-y5*x4))/((x5-x4)*(y1-y2)-(x1-x2)*(y5-y4))); 681 if((x3==x4 && ((pY1>=y3 && pY1<=y4) || (pY1>=y4 && pY1<=y3)))) { //x3y3与x4y4构成的直线垂直 682 if(pY1!=y3 && pY1!=y4) { 683 countOne++; 684 } 685 if(pY1==y3) { 686 wrongPiont1++; 687 } 688 if(pY1==y4) { 689 wrongPiont2++; 690 } 691 } 692 if(x3!=x4 && ((pX1>=x3 && pX1<=x4) || (pX1>=x4 && pX1<=x3))) { //不垂直 693 if(pX1!=x3 && pX1!=x4) { 694 countOne++; 695 } 696 if(pX1==x3) { 697 wrongPiont1++; 698 } 699 if(pX1==x4) { 700 wrongPiont2++; 701 } 702 } 703 if((x4==x5 && ((pY3>=y4 && pY3<=y5) || (pY3>=y5 && pY3<=y4)))) { //x4y4与x5y5构成的直线垂直 704 if(pY3!=y5 && pY3!=y4) { 705 countThree++; 706 } 707 if(pY3==y4 && wrongPiont2==0) { 708 wrongPiont2++; 709 } 710 if(pY3==y5 && wrongPiont3==0) { 711 wrongPiont3++; 712 } 713 } 714 if(x4!=x5 && ((pX3>=x4 && pX3<=x5) || (pX3>=x5 && pX3<=x4))) { //不垂直 715 if(pX3!=x4 && pX3!=x5) { 716 countThree++; 717 } 718 if(pX3==x4 && wrongPiont2==0) { 719 wrongPiont2++; 720 } 721 if(pX3==x5 && wrongPiont3==0) { 722 wrongPiont3++; 723 } 724 } 725 wrongPiont=wrongPiont3+wrongPiont2+wrongPiont1; 726 allCount=countOne+countTwo+countThree; 727 if(wrongPiont+allCount==0) { 728 System.out.print("0"); 729 } 730 else if(wrongPiont==1) { 731 System.out.println("1"); 732 } 733 else { 734 System.out.print(allCount+" "); 735 S=0.5*Math.abs((x4*y5-x5*y4)+(x5*y3-x3*y5)+(x3*y4-x4*y3)); 736 s1=0.5*Math.abs((pX1*pY3-pX3*pY1)+(pX3*y4-x4*pY3)+(x4*pY1-pX1*y4)); 737 s2=S-s1; 738 if(Math.abs(s2)>Math.abs(s1)) { 739 temp=s2; 740 s2=s1; 741 s1=temp; 742 } 743 s1=Math.abs(s1); 744 s2=Math.abs(s2); 745 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 746 } 747 } 748 } 749 750 else if(parWeather(x1,y1,x2,y2,x4,y4,x5,y5)) { 751 if(sameWeather(x1,y1,x2,y2,x4,y4,x5,y5)) { 752 System.out.print("The point is on the edge of the triangle"); 753 } 754 else { 755 pX1=(-((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4))); //x1y1x2y2 和 x3y3x4y4 756 pY1=(-((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4))); 757 pX2=(-((x3-x5)*(x2*y1-x1*y2)-(x1-x2)*(x5*y3-x3*y5))/((y3-y5)*(x1-x2)-(y1-y2)*(x3-x5))); //x1y1x2y2 和 x3y3x5y5 758 pY2=(-((y3-y5)*(y2*x1-y1*x2)-(y1-y2)*(y5*x3-y3*x5))/((x3-x5)*(y1-y2)-(x1-x2)*(y3-y5))); 759 if((x3==x4 && ((pY1>=y3 && pY1<=y4) || (pY1>=y4 && pY1<=y3)))) { //x3y3与x4y4构成的直线垂直 760 if(pY1!=y3 && pY1!=y4) { 761 countOne++; 762 } 763 if(pY1==y3) { 764 wrongPiont1++; 765 } 766 if(pY1==y4) { 767 wrongPiont2++; 768 } 769 } 770 else if(x3!=x4 && ((pX1>=x3 && pX1<=x4) || (pX1>=x4 && pX1<=x3))) { //不垂直 771 if(pX1!=x3 && pX1!=x4) { 772 countOne++; 773 } 774 if(pX1==x3) { 775 wrongPiont1++; 776 } 777 if(pX1==x4) { 778 wrongPiont2++; 779 } 780 } 781 if((x3==x5 && ((pY2>=y3 && pY1<=y5) || (pY2>=y5 && pY2<=y3)))) { //x3y3与x5y5构成的直线垂直 782 if(pY2!=y3 && pY2!=y5) { 783 countTwo++; 784 } 785 if(pY2==y3 && wrongPiont1==0) { 786 wrongPiont1++; 787 } 788 if(pY2==y5 && wrongPiont3==0) { 789 wrongPiont3++; 790 } 791 } 792 else if(x3!=x5 && ((pX2>=x3 && pX2<=x5) || (pX2>=x5 && pX2<=x3))) { //不垂直 793 if(pX2!=x3 && pX2!=x5) { 794 countTwo++; 795 } 796 if(pX2==x3 && wrongPiont1==0) { 797 wrongPiont1++; 798 } 799 if(pX2==x5 && wrongPiont3==0) { 800 wrongPiont3++; 801 } 802 } 803 wrongPiont=wrongPiont3+wrongPiont2+wrongPiont1; 804 allCount=countOne+countTwo+countThree; 805 if(wrongPiont+allCount==0) { 806 System.out.print("0"); 807 } 808 else if(wrongPiont==1) { 809 System.out.println("1"); 810 } 811 else { 812 System.out.print(allCount+" "); 813 S=0.5*Math.abs((x4*y5-x5*y4)+(x5*y3-x3*y5)+(x3*y4-x4*y3)); 814 s1=0.5*Math.abs((pX1*pY2-pX2*pY1)+(pX2*y3-x3*pY2)+(x3*pY1-pX1*y3)); 815 s2=S-s1; 816 if(Math.abs(s2)>Math.abs(s1)) { 817 temp=s2; 818 s2=s1; 819 s1=temp; 820 } 821 s1=Math.abs(s1); 822 s2=Math.abs(s2); 823 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 824 } 825 } 826 } 827 828 else { 829 pX1=(-((x3-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y3-x3*y4))/((y3-y4)*(x1-x2)-(y1-y2)*(x3-x4))); //x1y1x2y2 和 x3y3x4y4 830 pY1=(-((y3-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x3-y3*x4))/((x3-x4)*(y1-y2)-(x1-x2)*(y3-y4))); 831 pX2=(-((x3-x5)*(x2*y1-x1*y2)-(x1-x2)*(x5*y3-x3*y5))/((y3-y5)*(x1-x2)-(y1-y2)*(x3-x5))); //x1y1x2y2 和 x3y3x5y5 832 pY2=(-((y3-y5)*(y2*x1-y1*x2)-(y1-y2)*(y5*x3-y3*x5))/((x3-x5)*(y1-y2)-(x1-x2)*(y3-y5))); 833 pX3=(-((x5-x4)*(x2*y1-x1*y2)-(x1-x2)*(x4*y5-x5*y4))/((y5-y4)*(x1-x2)-(y1-y2)*(x5-x4))); //x1y1x2y2 和 x4y4x5y5 834 pY3=(-((y5-y4)*(y2*x1-y1*x2)-(y1-y2)*(y4*x5-y5*x4))/((x5-x4)*(y1-y2)-(x1-x2)*(y5-y4))); 835 // System.out.println(pX1+" "+pY1+" "+pX2+" "+pY2+" "+pX3+" "+pY3); 836 //三角形的三个顶点 837 //判断交点是否在线段内部 838 if((x3==x4 && ((pY1>=y3 && pY1<=y4) || (pY1>=y4 && pY1<=y3)))) { //x3y3与x4y4构成的直线垂直 839 if(pY1!=y3 && pY1!=y4) { 840 countOne++; 841 } 842 if(pY1==y3 && wrongPiont1==0) { 843 wrongPiont1++; 844 } 845 if(pY1==y4 && wrongPiont2==0) { 846 wrongPiont2++; 847 } 848 } 849 if(x3!=x4 && ((pX1>=x3 && pX1<=x4) || (pX1>=x4 && pX1<=x3))) { //不垂直 850 if(pX1!=x3 && pX1!=x4) { 851 countOne++; 852 } 853 if(pX1==x3 && wrongPiont1==0) { 854 wrongPiont1++; 855 } 856 if(pX1==x4 && wrongPiont2==0) { 857 wrongPiont2++; 858 } 859 } 860 if((x3==x5 && ((pY2>=y3 && pY1<=y5) || (pY2>=y5 && pY2<=y3)))) { //x3y3与x5y5构成的直线垂直 861 if(pY2!=y3 && pY2!=y5) { 862 countTwo++; 863 } 864 if(pY2==y3 && wrongPiont1==0) { 865 wrongPiont1++; 866 } 867 if(pY2==y5 && wrongPiont3==0) { 868 wrongPiont3++; 869 } 870 } 871 if(x3!=x5 && ((pX2>=x3 && pX2<=x5) || (pX2>=x5 && pX2<=x3))) { //不垂直 872 if(pX2!=x3 && pX2!=x5) { 873 countTwo++; 874 } 875 if(pX2==x3 && wrongPiont1==0) { 876 wrongPiont1++; 877 } 878 if(pX2==x5 && wrongPiont3==0) { 879 wrongPiont3++; 880 } 881 } 882 if((x4==x5 && ((pY3>=y4 && pY3<=y5) || (pY3>=y5 && pY3<=y4)))) { //x4y4与x5y5构成的直线垂直 883 if(pY3!=y5 && pY3!=y4) { 884 countThree++; 885 } 886 if(pY3==y4 && wrongPiont2==0) { 887 wrongPiont2++; 888 } 889 if(pY3==y5 && wrongPiont3==0) { 890 wrongPiont3++; 891 } 892 } 893 if(x4!=x5 && ((pX3>=x4 && pX3<=x5) || (pX3>=x5 && pX3<=x4))) { //不垂直 894 if(pX3!=x4 && pX3!=x5) { 895 countThree++; 896 } 897 if(pX3==x4 && wrongPiont2==0) { 898 wrongPiont2++; 899 } 900 if(pX3==x5 && wrongPiont3==0) { 901 wrongPiont3++; 902 } 903 } 904 wrongPiont=wrongPiont1+wrongPiont2+wrongPiont3; 905 allCount=countOne+countTwo+countThree; 906 if(allCount+wrongPiont==0) { 907 System.out.print("0"); 908 } 909 else if(wrongPiont==1 && allCount==0) { 910 System.out.print("1"); 911 } 912 else if(wrongPiont!=2){ 913 System.out.print((allCount+wrongPiont)+" "); 914 } 915 if(wrongPiont<=1) { 916 if(allCount+wrongPiont==2) { 917 if(wrongPiont==0) { 918 if(countOne==1 && countTwo==1) { 919 //x3 pX1 pX2 920 S=0.5*Math.abs((x4*y5-x5*y4)+(x5*y3-x3*y5)+(x3*y4-x4*y3)); 921 s1=0.5*Math.abs((pX1*pY2-pX2*pY1)+(pX2*y3-x3*pY2)+(x3*pY1-pX1*y3)); 922 s2=S-s1; 923 if(Math.abs(s2)>Math.abs(s1)) { 924 temp=s2; 925 s2=s1; 926 s1=temp; 927 } 928 s1=Math.abs(s1); 929 s2=Math.abs(s2); 930 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 931 } 932 else if(countOne==1 && countThree==1) { 933 //x4 pX1 pX3 934 S=0.5*Math.abs(x4*y5+x5*y3+x3*y4-x4*y3-x5*y4-x3*y5); 935 s1=0.5*Math.abs((pX1*pY3-pX3*pY1)+(pX3*y4-x4*pY3)+(x4*pY1-pX1*y4)); 936 // S=0.5*Math.abs(x4*y2+x2*y3+x3*y4-x4*y3-x2*y4-x3*y2); 937 s2=S-s1; 938 if(Math.abs(s2)>Math.abs(s1)) { 939 temp=s2; 940 s2=s1; 941 s1=temp; 942 } 943 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 944 } 945 else if(countTwo==1 && countThree==1) { 946 //x5 pX2 pX3 947 S=0.5*Math.abs((x4*y5-x5*y4)+(x5*y3-x3*y5)+(x3*y4-x4*y3)); 948 s1=0.5*Math.abs((pX3*pY2-pX2*pY3)+(pX2*y5-x5*pY2)+(x5*pY3-pX3*y5)); 949 s2=S-s1; 950 if(Math.abs(s2)>Math.abs(s1)) { 951 temp=s2; 952 s2=s1; 953 s1=temp; 954 } 955 s1=Math.abs(s1); 956 s2=Math.abs(s2); 957 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 958 } 959 } 960 else if(wrongPiont==1) { 961 // System.out.println("1"); 962 if(wrongPiont1==1) { 963 //px3 x3 x4 964 // System.out.println(pX3+" "+pY3); 965 S=0.5*Math.abs(x4*y5+x5*y3+x3*y4-x4*y3-x5*y4-x3*y5); 966 s1=0.5*Math.abs(x4*pY3+pX3*y3+x3*y4-x4*y3-pX3*y4-x3*pY3); 967 s2=S-s1; 968 if(Math.abs(s2)>Math.abs(s1)) { 969 temp=s2; 970 s2=s1; 971 s1=temp; 972 } 973 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 974 } 975 if(wrongPiont2==1) { 976 // System.out.println(pX2+" "+pY2); 977 S=0.5*Math.abs(x4*y5+x5*y3+x3*y4-x4*y3-x5*y4-x3*y5); 978 s1=0.5*Math.abs(x4*pY2+pX2*y3+x3*y4-x4*y3-pX2*y4-x3*pY2); //px2 x4 x3 979 s2=S-s1; 980 if(Math.abs(s2)>Math.abs(s1)) { 981 temp=s2; 982 s2=s1; 983 s1=temp; 984 } 985 s1=Math.abs(s1); 986 s2=Math.abs(s2); 987 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 988 } 989 if(wrongPiont3==1) { 990 // System.out.println(pX1+" "+pY1); 991 S=0.5*Math.abs(x4*y5+x5*y3+x3*y4-x4*y3-x5*y4-x3*y5); 992 s1=0.5*Math.abs((pX1*y5-x5*pY1)+(x5*y4-x4*y5)+(x4*pY1-pX1*y4)); 993 s2=S-s1; 994 if(Math.abs(s2)>Math.abs(s1)) { 995 temp=s2; 996 s2=s1; 997 s1=temp; 998 } 999 s1=Math.abs(s1); 1000 s2=Math.abs(s2); 1001 System.out.print((double)Math.round(s2*1000000)/1000000+" "+(double)Math.round(s1*1000000)/1000000); 1002 } 1003 } 1004 } 1005 } 1006 } 1007 if(wrongPiont==2) { 1008 System.out.print("The point is on the edge of the triangle"); //* 1009 } 1010 } 1011 else { 1012 System.out.println("data error"); 1013 } 1014 } 1015 } 1016 else { 1017 System.out.println("Wrong Format"); 1018 } 1019 } 1020 else { 1021 if(allBlank<4) { 1022 numOne=cutArr(); 1023 numTwo=cutArr(); 1024 numThree=cutArr(); 1025 numFour=cutArr(); 1026 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour)) { 1027 System.out.println("wrong number of points"); 1028 } 1029 else { 1030 System.out.println("Wrong Format"); 1031 } 1032 } 1033 else if(allBlank>4) { 1034 numOne=cutArr(); 1035 numTwo=cutArr(); 1036 numThree=cutArr(); 1037 numFour=cutArr(); 1038 numFive=cutArr(); 1039 numSix=cutArr(); 1040 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour) && checkNum(numFive) && checkNum(numSix)) { 1041 System.out.println("wrong number of points"); 1042 } 1043 else { 1044 System.out.println("Wrong Format"); 1045 } 1046 } 1047 } 1048 } 1049 1050 //case 5 1051 public void outWeather() { 1052 if(numData(numBlank())) { 1053 numOne=cutArr(); 1054 numTwo=cutArr(); 1055 numThree=cutArr(); 1056 numFour=cutArr(); 1057 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour)) { 1058 x1=cutX(numOne); 1059 y1=cutY(numOne); 1060 x2=cutX(numTwo); 1061 y2=cutY(numTwo); 1062 x3=cutX(numThree); 1063 y3=cutY(numThree); 1064 x4=cutX(numFour); 1065 y4=cutY(numFour); 1066 1067 1068 l1=Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)); 1069 l2=Math.sqrt(Math.pow(x3-x4,2)+Math.pow(y3-y4,2)); 1070 l3=Math.sqrt(Math.pow(x2-x4,2)+Math.pow(y2-y4,2)); 1071 L1=l1*l1; 1072 L2=l2*l2; 1073 L3=l3*l3; 1074 if(traWeather()) { 1075 S=0.5*Math.abs(x4*y2+x2*y3+x3*y4-x4*y3-x2*y4-x3*y2); 1076 s1=0.5*Math.abs(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2); 1077 s2=0.5*Math.abs(x1*y4+x4*y3+x3*y1-x1*y3-x4*y1-x3*y4); 1078 s3=0.5*Math.abs(x1*y2+x2*y4+x4*y1-x1*y4-x2*y1-x4*y2); 1079 if(s1==0 || s2==0 || s3==0) { 1080 System.out.println("on the triangle"); 1081 } 1082 else if(S==s1+s2+s3) { 1083 System.out.println("in the triangle"); 1084 } 1085 else { 1086 System.out.println("outof the triangle"); 1087 } 1088 } 1089 else { 1090 System.out.println("data error"); 1091 } 1092 1093 } 1094 else { 1095 System.out.println("Wrong Format"); 1096 } 1097 } 1098 else { 1099 if(allBlank<4) { 1100 numOne=cutArr(); 1101 numTwo=cutArr(); 1102 numThree=cutArr(); 1103 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree)) { 1104 System.out.println("wrong number of points"); 1105 } 1106 else { 1107 System.out.println("Wrong Format"); 1108 } 1109 } 1110 else if(allBlank>4) { 1111 numOne=cutArr(); 1112 numTwo=cutArr(); 1113 numThree=cutArr(); 1114 numFour=cutArr(); 1115 numFive=cutArr(); 1116 if(checkNum(numOne) && checkNum(numTwo) && checkNum(numThree) && checkNum(numFour) && checkNum(numFive)) { 1117 System.out.println("wrong number of points"); 1118 } 1119 else { 1120 System.out.println("Wrong Format"); 1121 } 1122 } 1123 } 1124 } 1125 }
PTA题目集5 7-5ATM机类结构设计(一)
这道题我的构想是创建4个类分别储存4个人的银行账户信息,但后期我发现这样做并不合理。因为一家银行会有很多客户,也就意味着如果建类的话,需要建很多各类,这显然不太现实,所以我有了一种新的想法
类图:
代码:

1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 Scanner in = new Scanner(System.in); 8 String cardNum; 9 String passWord; 10 String whatAtm; 11 String money; 12 13 int k=0; 14 int i=0; 15 16 check A = new check(); 17 18 String numArray=in.nextLine(); 19 while(numArray.compareTo("#")!=0) { 20 String regexFive = "\\d{19}\\s{1,}\\d{8}\\s{1,}\\d{2}\\s{1,}-?([1-9]\\d*[.]\\d*|0\\[.]\\d*[1-9]\\d*)"; 21 String regexSix = "\\d{19}"; 22 boolean boolFive = numArray.matches(regexFive); 23 boolean boolSix = numArray.matches(regexSix); 24 if(boolSix && !boolFive) { 25 A.checkMoney(numArray); 26 } 27 else if(boolFive) { 28 cardNum=numArray.substring(0,19); 29 k=A.cutNum(numArray,i+19); 30 passWord=numArray.substring(k+1,k+9); 31 k=A.cutNum(numArray,k+1+8); 32 whatAtm=numArray.substring(k+1,k+3); 33 k=A.cutNum(numArray,k+1+2); 34 money=numArray.substring(k+1,numArray.length()); 35 A.checkCard(cardNum,passWord,whatAtm,money); 36 } 37 else { 38 break; 39 } 40 numArray=in.nextLine(); 41 } 42 43 A.outPut(); 44 } 45 } 46 47 class check { 48 49 int p=0; 50 String[] num = new String[40]; 51 52 Yang yang = new Yang(); 53 Guo guo = new Guo(); 54 Zhang zhang = new Zhang(); 55 Wei wei = new Wei(); 56 57 double moneyTake = 0; 58 double amount = 0; 59 60 public void checkCard(String numOne, String numTwo, String numThree, String numFour) { 61 if (numOne.compareTo(yang.cardOne1) == 0 || numOne.compareTo(yang.cardOne2) == 0 || numOne.compareTo(yang.cardTwo1) == 0) { 62 if (numTwo.compareTo("88888888") == 0) { 63 if (numThree.compareTo("01") == 0 || numThree.compareTo("02") == 0 || numThree.compareTo("03") == 0 || numThree.compareTo("04") == 0) { 64 moneyTake = Double.parseDouble(numFour); 65 66 if(numOne.compareTo(yang.cardOne1) == 0 || numOne.compareTo(yang.cardOne2) == 0) { 67 amount = yang.balanceCone1; 68 if (moneyTake > 0 && Math.abs(moneyTake) > amount) { 69 num[p]="Sorry,your account balance is insufficient."; 70 p++; 71 } else { 72 yang.balanceCone1 = amount - moneyTake; 73 if(moneyTake>0) { 74 num[p]="杨过在中国建设银行的"+numThree+"号ATM机上取款¥"+numFour; 75 p++; 76 num[p]="当前余额为¥"+String.format("%.2f",yang.balanceCone1); 77 p++; 78 } 79 if(moneyTake<0) { 80 numFour=numFour.substring(1,numFour.length()); 81 num[p]="杨过在中国建设银行的"+numThree+"号ATM机上存款¥"+numFour; 82 p++; 83 num[p]="当前余额为¥"+String.format("%.2f",yang.balanceCone1); 84 p++; 85 } 86 } 87 } 88 else if(numOne.compareTo(yang.cardTwo1) == 0) { 89 amount = yang.balanceCone2; 90 if (moneyTake > 0 && Math.abs(moneyTake) > amount) { 91 num[p]="Sorry,your account balance is insufficient."; 92 p++; 93 } else { 94 yang.balanceCone2 = amount - moneyTake; 95 if(moneyTake>0) { 96 num[p]="杨过在中国建设银行的"+numThree+"号ATM机上取款¥"+numFour; 97 p++; 98 num[p]="当前余额为¥"+String.format("%.2f",yang.balanceCone2); 99 p++; 100 } 101 if(moneyTake<0) { 102 numFour=numFour.substring(1,numFour.length()); 103 num[p]="杨过在中国建设银行的"+numThree+"号ATM机上存款¥"+numFour; 104 p++; 105 num[p]="当前余额为¥"+String.format("%.2f",yang.balanceCone2); 106 p++; 107 } 108 } 109 } 110 } else if (numThree.compareTo("05") == 0 || numThree.compareTo("06") == 0) { 111 num[p]="Sorry,cross-bank withdrawal is not supported."; 112 p++; 113 } else { 114 num[p]="Sorry,the ATM's id is wrong."; 115 p++; 116 } 117 } else { 118 num[p]="Sorry,your password is wrong."; 119 p++; 120 } 121 } 122 else if (numOne.compareTo(guo.cardOne1) == 0) { 123 if (numTwo.compareTo("88888888") == 0) { 124 if (numThree.compareTo("01") == 0 || numThree.compareTo("02") == 0 || numThree.compareTo("03") == 0 || numThree.compareTo("04") == 0) { 125 moneyTake = Double.parseDouble(numFour); 126 amount = guo.balanceCone1; 127 if (moneyTake > 0 && Math.abs(moneyTake) > amount) { 128 num[p]="Sorry,your account balance is insufficient."; 129 p++; 130 } else { 131 guo.balanceCone1 = amount - moneyTake; 132 if(moneyTake>0) { 133 num[p]="郭靖在中国建设银行的"+numThree+"号ATM机上取款¥"+numFour; 134 p++; 135 num[p]="当前余额为¥"+String.format("%.2f",guo.balanceCone1); 136 p++; 137 } 138 if(moneyTake<0) { 139 numFour=numFour.substring(1,numFour.length()); 140 num[p]="郭靖在中国建设银行的"+numThree+"号ATM机上存款¥"+numFour; 141 p++; 142 num[p]="当前余额为¥"+String.format("%.2f",guo.balanceCone1); 143 p++; 144 } 145 } 146 } else if (numThree.compareTo("05") == 0 || numThree.compareTo("06") == 0) { 147 num[p]="Sorry,cross-bank withdrawal is not supported."; 148 p++; 149 } else { 150 num[p]="Sorry,the ATM's id is wrong."; 151 p++; 152 } 153 } else { 154 num[p]="Sorry,your password is wrong."; 155 p++; 156 } 157 } 158 159 else if (numOne.compareTo(zhang.cardOne1) == 0 || numOne.compareTo(zhang.cardTwo1) == 0 || numOne.compareTo(zhang.cardThree1) == 0 || numOne.compareTo(zhang.cardThree2) == 0) { 160 if (numTwo.compareTo("88888888") == 0) { 161 if (numThree.compareTo("05") == 0 || numThree.compareTo("06") == 0) { 162 moneyTake = Double.parseDouble(numFour); 163 164 if(numOne.compareTo(zhang.cardOne1)==0) { 165 amount = zhang.balance1; 166 if (moneyTake > 0 && Math.abs(moneyTake) > amount) { 167 num[p]="Sorry,your account balance is insufficient."; 168 p++; 169 } else { 170 zhang.balance1 = amount - moneyTake; 171 if(moneyTake>0) { 172 num[p]="张无忌在中国工商银行的"+numThree+"号ATM机上取款¥"+numFour; 173 p++; 174 num[p]="当前余额为¥"+String.format("%.2f",zhang.balance1); 175 p++; 176 } 177 if(moneyTake<0) { 178 numFour=numFour.substring(1,numFour.length()); 179 num[p]="张无忌在中国工商银行的"+numThree+"号ATM机上存款¥"+numFour; 180 p++; 181 num[p]="当前余额为¥"+String.format("%.2f",zhang.balance1); 182 p++; 183 } 184 } 185 } 186 else if(numOne.compareTo(zhang.cardTwo1)==0) { 187 amount = zhang.balance2; 188 if (moneyTake > 0 && Math.abs(moneyTake) > amount) { 189 num[p]="Sorry,your account balance is insufficient."; 190 p++; 191 } else { 192 zhang.balance2 = amount - moneyTake; 193 if(moneyTake>0) { 194 num[p]="张无忌在中国工商银行的"+numThree+"号ATM机上取款¥"+numFour; 195 p++; 196 num[p]="当前余额为¥"+String.format("%.2f",zhang.balance2); 197 p++; 198 } 199 if(moneyTake<0) { 200 numFour=numFour.substring(1,numFour.length()); 201 num[p]="张无忌在中国工商银行的"+numThree+"号ATM机上存款¥"+numFour; 202 p++; 203 num[p]="当前余额为¥"+String.format("%.2f",zhang.balance2); 204 p++; 205 } 206 } 207 } 208 else if(numOne.compareTo(zhang.cardThree1)==0 || numOne.compareTo(zhang.cardThree2)==0) { 209 amount = zhang.balance3; 210 if (moneyTake > 0 && Math.abs(moneyTake) > amount) { 211 num[p]="Sorry,your account balance is insufficient."; 212 p++; 213 } else { 214 zhang.balance3 = amount - moneyTake; 215 if(moneyTake>0) { 216 num[p]="张无忌在中国工商银行的"+numThree+"号ATM机上取款¥"+numFour; 217 p++; 218 num[p]="当前余额为¥"+String.format("%.2f",zhang.balance3); 219 p++; 220 } 221 if(moneyTake<0) { 222 numFour=numFour.substring(1,numFour.length()); 223 num[p]="张无忌在中国工商银行的"+numThree+"号ATM机上存款¥"+numFour; 224 p++; 225 num[p]="当前余额为¥"+String.format("%.2f",zhang.balance3); 226 p++; 227 } 228 } 229 } 230 } else if (numThree.compareTo("01") == 0 || numThree.compareTo("02") == 0 || numThree.compareTo("03") == 0 || numThree.compareTo("04") == 0) { 231 num[p]="Sorry,cross-bank withdrawal is not supported."; 232 p++; 233 } else { 234 num[p]="Sorry,the ATM's id is wrong."; 235 p++; 236 } 237 } else { 238 num[p]="Sorry,your password is wrong."; 239 p++; 240 } 241 } 242 else if (numOne.compareTo(wei.cardOne1) == 0 || numOne.compareTo(wei.cardTwo1) == 0) { 243 if (numTwo.compareTo("88888888") == 0) { 244 if (numThree.compareTo("05") == 0 || numThree.compareTo("06") == 0) { 245 246 moneyTake = Double.parseDouble(numFour); 247 //case 1 248 if(numOne.compareTo(wei.cardOne1) == 0) { 249 amount = wei.balanceCone1; 250 if (moneyTake > 0 && Math.abs(moneyTake) > amount) { 251 num[p]="Sorry,your account balance is insufficient."; 252 p++; 253 } else { 254 wei.balanceCone1 = amount - moneyTake; 255 if(moneyTake>0) { 256 num[p]="韦小宝在中国工商银行的"+numThree+"号ATM机上取款¥"+numFour; 257 p++; 258 num[p]="当前余额为¥"+String.format("%.2f",wei.balanceCone1); 259 p++; 260 } 261 if(moneyTake<0) { 262 numFour=numFour.substring(1,numFour.length()); 263 num[p]="韦小宝在中国工商银行的"+numThree+"号ATM机上存款¥"+numFour; 264 p++; 265 num[p]="当前余额为¥"+String.format("%.2f",wei.balanceCone1); 266 p++; 267 } 268 } 269 } 270 //case 2 271 else if(numOne.compareTo(wei.cardTwo1) == 0) { 272 amount = wei.balanceCone2; 273 if (moneyTake > 0 && Math.abs(moneyTake) > amount) { 274 num[p]="Sorry,your account balance is insufficient."; 275 p++; 276 } else { 277 wei.balanceCone2 = amount - moneyTake; 278 if(moneyTake>0) { 279 num[p]="韦小宝在中国工商银行的"+numThree+"号ATM机上取款¥"+numFour; 280 p++; 281 num[p]="当前余额为¥"+String.format("%.2f",wei.balanceCone2); 282 p++; 283 } 284 if(moneyTake<0) { 285 numFour=numFour.substring(1,numFour.length()); 286 num[p]="韦小宝在中国工商银行的"+numThree+"号ATM机上存款¥"+numFour; 287 p++; 288 num[p]="当前余额为¥"+String.format("%.2f",wei.balanceCone2); 289 p++; 290 } 291 } 292 } 293 } else if (numThree.compareTo("01") == 0 || numThree.compareTo("02") == 0 || numThree.compareTo("03") == 0 || numThree.compareTo("04") == 0) { 294 num[p]="Sorry,cross-bank withdrawal is not supported."; 295 p++; 296 } else { 297 num[p]="Sorry,the ATM's id is wrong."; 298 p++; 299 } 300 } else { 301 num[p]="Sorry,your password is wrong."; 302 p++; 303 } 304 } 305 else { 306 num[p]="Sorry,this card does not exist."; 307 p++; 308 } 309 } 310 311 public void checkMoney(String num1) { 312 if(num1.compareTo(yang.cardOne1)==0 || num1.compareTo(yang.cardOne2)==0) { 313 num[p]="¥"+String.format("%.2f",yang.balanceCone1); 314 p++; 315 } 316 else if(num1.compareTo(yang.cardTwo1)==0) { 317 num[p]="¥"+String.format("%.2f",yang.balanceCone2); 318 p++; 319 } 320 else if(num1.compareTo(zhang.cardOne1)==0) { 321 num[p]="¥"+String.format("%.2f",zhang.balance1); 322 p++; 323 } 324 else if(num1.compareTo(zhang.cardTwo1)==0) { 325 num[p]="¥"+String.format("%.2f",zhang.balance2); 326 p++; 327 } 328 else if(num1.compareTo(zhang.cardThree1)==0 || num1.compareTo(zhang.cardThree2)==0) { 329 num[p]="¥"+String.format("%.2f",zhang.balance3); 330 p++; 331 } 332 else if(num1.compareTo(guo.cardOne1)==0) { 333 num[p]="¥"+String.format("%.2f",guo.balanceCone1); 334 p++; 335 } 336 else if(num1.compareTo(wei.cardOne1)==0 || num1.compareTo(wei.cardTwo1)==0) { 337 num[p]="¥"+String.format("%.2f",wei.balanceCone1); 338 p++; 339 } 340 } 341 342 public int cutNum(String num,int i) { 343 int k=0; 344 for(;i<num.length();i++) { 345 if(num.charAt(i)==' ' && ((num.charAt(i+1)>=48 && num.charAt(i+1)<=57) || (num.charAt(i+1)=='-' || num.charAt(i+1)=='+'))) { 346 k=i; 347 return k; 348 } 349 } 350 return 0; 351 } 352 353 public void outPut() {; 354 for(int j=0;j<p;j++) { 355 System.out.println(num[j]); 356 } 357 } 358 } 359 360 class Yang { 361 String accountOne="3217000010041315709"; 362 String accountTwo="3217000010041315715"; 363 String cardOne1="6217000010041315709"; 364 String cardOne2="6217000010041315715"; 365 String cardTwo1="6217000010041315718"; 366 double balanceCone1=10000; 367 double balanceCone2=10000; 368 } 369 370 class Guo { 371 String accountOne="3217000010051320007"; 372 String cardOne1="6217000010051320007"; 373 double balanceCone1=10000; 374 } 375 376 class Zhang { 377 String accountOne="3217000010041315709"; 378 String accountTwo="3217000010041315715"; 379 String cardOne1="6222081502001312389"; 380 String cardTwo1="6222081502001312390"; 381 String cardThree1="6222081502001312399"; 382 String cardThree2="6222081502001312400"; 383 double balance1=10000; 384 double balance2=10000; 385 double balance3=10000; 386 } 387 388 class Wei { 389 String accountOne="3222081502051320785"; 390 String accountTwo="3222081502051320786"; 391 String cardOne1="6222081502051320785"; 392 String cardTwo1="6222081502051320786"; 393 double balanceCone1=10000; 394 double balanceCone2=10000; 395 }
PTA题目集6 7-2 点线形系列4-凸四边形的计算
上文提到过这道题我最开始的想法是创建4个类去储存线段信息,但大量的getter和setter充斥在程序中,所以我有将getter和setter删除了。
4个线段类也显得有些冗余,所以我也将其删除了,只留下5个选项类,用于判断题目给的5个选项。下面我来讲述一些我遇到的难点。
1.凸凹四边形的判断 最开始我试图用cos去计算,但过长的代码,以及不完善的逻辑让我被迫放弃,最后选择了百度的公式去判断凸凹性。
2.判断是否为4变形 判断四边形是否成立比判断三角形是否成立的难度大写一些,首相判断是否有重合的点,三点共线的情况;接着两两相连计算每条线段的长度;最关键的一部在于判断有否有交叉的情况存在。计算第二条线段和第四条线段,第一条线段和第三条线段的交点是否在两线段内,若在则交叉,不在则不交叉。
3.4个点可以构成三角形或四边形 这个选项允许有一个点重合,或三点一线存在。有一个点重合,判断是否能构成三角线,三点一线成立,且四点一线不成立可以构成三角形。无点重合判断是否构成三角形。
4.判断点是否在三角形/四边形内部 这个选项有两种方法可以判断,用面积法或射线法,两种方法上午均有介绍,就不多做赘述了。
类图:
代码:

1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 // TODO 自动生成的方法存根 7 choose Choose = new choose(); 8 Choose.option(); 9 } 10 } 11 12 class choose { 13 Scanner in = new Scanner(System.in); 14 String arr; //输入的字符串 15 String cutArr; //坐标 16 String head; //字符串前缀 17 String mark; //英文冒号 18 String choice; //选项 19 String[] num; 20 21 int wrong=0; 22 int Choice=0; 23 int passport=0; 24 double S=0,C=0; 25 double l1_2=0, l2_3=0, l3_4=0, l4_1=0; 26 double s1=0, s2=0, s3=0, s4=0; 27 double px1=0, py1=0, px2=0, py2=0, px=0, py=0; 28 double x1=0, y1=0, x2=0, y2=0, x3=0, y3=0, x4=0, y4=0; 29 30 public void option() { 31 inPut(); 32 if(arr.length()==1) { 33 System.out.println("Wrong Format"); 34 wrong++; 35 } 36 else { 37 cutHead(); 38 cutOption(); 39 if(arr.charAt(0)==' ') { 40 System.out.println("Wrong Format"); 41 wrong++; 42 } 43 } 44 if((wrong==0) && (choice.charAt(0)=='1' || choice.charAt(0)=='2' || choice.charAt(0)=='3' || choice.charAt(0)=='4' || choice.charAt(0)=='5')) { 45 Choice=Integer.parseInt(choice); 46 } 47 else { 48 System.out.println("Wrong Format"); 49 wrong++; 50 } 51 if(wrong==0) { 52 switch(Choice) { 53 case 1: 54 boolean resultOne=false; 55 boolean resultTwo=false; 56 squareCase(); 57 if(passport==0) { 58 if(square()) { 59 resultOne=true; 60 if(parWeather()) { 61 resultTwo=false; 62 } 63 } 64 System.out.println(resultOne+" "+resultTwo); 65 } 66 break; 67 case 2: 68 boolean result1=false; 69 boolean result2=false; 70 boolean result3=false; 71 squareCase(); 72 if(passport==0) { 73 if(!sameLine(x1,y1,x2,y2,x3,y3) || !sameLine(x2,y2,x1,y1,x4,y4) || !sameLine(x2,y2,x3,y3,x4,y4) || !sameLine(x1,y1,x3,y3,x4,y4)) { 74 if(equalLine()) { 75 result1=true; 76 if(chooseOne()) { 77 result3=true; 78 result2=true; 79 80 } 81 } 82 System.out.println(result1+" "+result2+" "+result3); 83 84 // if(chooseTwo()) { 85 // result2=true; 86 // } 87 } 88 else { 89 System.out.println("not a quadrilateral"); 90 } 91 } 92 break; 93 case 3: 94 squareCase(); 95 conOrnot(); 96 boolean r=false; 97 if(!sameLine(x1,y1,x2,y2,x3,y3) || !sameLine(x2,y2,x1,y1,x4,y4) || !sameLine(x2,y2,x3,y3,x4,y4) || !sameLine(x1,y1,x3,y3,x4,y4)) { 98 passport++; 99 System.out.println("not a quadrilateral"); 100 } 101 if(passport==0) { 102 if(conOrnot()) { 103 r=true; 104 C=l1_2+l2_3+l3_4+l4_1; 105 S=cauSone(); 106 } 107 else { 108 C=l1_2+l2_3+l3_4+l4_1; 109 S=cauStwo(); 110 } 111 System.out.print(r+" "+(double)Math.round(C*1000)/1000+" "+(double)Math.round(S*1000)/1000); 112 } 113 break; 114 case 4: 115 System.out.println("not a quadrilateral or triangle"); 116 break; 117 case 5: 118 squareCase(); 119 if(inPiont()) { 120 System.out.println("in the quadrilateral"); 121 } 122 else { 123 System.out.println("outof the quadrilateral"); 124 } 125 squareCase(); 126 } 127 } 128 } 129 //输入字符串 130 public void inPut() { 131 arr=in.nextLine(); 132 } 133 //切去头部 134 public void cutHead() { 135 head=arr.substring(0,2); 136 arr=arr.substring(2,arr.length()); 137 } 138 //提取选项与符号 139 public void cutOption() { 140 choice=head.substring(0,1); 141 mark=head.substring(1,2); 142 } 143 //计算空格的个数 144 public int numBlank() { 145 int allBlank=0; 146 for(int i=0;i<arr.length();i++) { 147 if(arr.charAt(i)==' ') { 148 allBlank++; 149 } 150 } 151 return allBlank; 152 } 153 //判断输入数据是多于所需数据 154 public boolean numData(int n) { 155 switch(Choice) { 156 case 1: 157 if(n!=3) { 158 return false; 159 } 160 else { 161 return true; 162 } 163 case 2: 164 if(n!=3) { 165 return false; 166 } 167 else { 168 return true; 169 } 170 case 3: 171 if(n!=3) { 172 return false; 173 } 174 else { 175 return true; 176 } 177 case 4: 178 if(n!=5) { 179 return false; 180 } 181 else { 182 return true; 183 } 184 case 5: 185 if(n!=4) { 186 return false; 187 } 188 else { 189 return true; 190 } 191 } 192 return false; 193 } 194 //切割得x的值 195 public double cutX(String arr) { 196 String X; 197 double x=0; 198 for(int i=0;i<arr.length();i++) { 199 if(arr.charAt(i)==',') { 200 X=arr.substring(0,i); 201 x=Double.parseDouble(X); 202 break; 203 } 204 } 205 return x; 206 } 207 public double cutY(String arr) { //切割得y的值 208 String Y; 209 double y=0; 210 for(int i=0;i<arr.length();i++) { 211 if(arr.charAt(i)==',') { 212 Y=arr.substring(i+1,arr.length()); 213 y=Double.parseDouble(Y); 214 break; 215 } 216 } 217 return y; 218 } 219 public boolean checkNum(String num) { //检查格式是否错误 220 String reagex="((-?([1-9]\\d*[.]\\d*|0\\[.]\\d*[1-9]\\d*))|-?[1-9]\\d*|0),((-?([1-9]\\d*[.]\\d*|0\\[.]\\d*[1-9]\\d*))|-?[1-9]\\d*|0)"; 221 boolean bool =num.matches(reagex); 222 if(bool) { 223 return true; 224 } 225 else { 226 return false; 227 } 228 } 229 //是否有三点一线 230 public boolean sameLine(double X1,double Y1,double X2,double Y2,double X3,double Y3) { 231 double k2=0; 232 double K2=0; 233 double b2=0; 234 if(X1==X2 && X2==X3) { 235 return false; 236 } 237 else if(X1!=X2 && X2==X3){ 238 return true; 239 } 240 else if(X1==X2 && X2!=X3) { 241 return true; 242 } 243 else { 244 k2=(Y3-Y2)/(X3-X2); 245 K2=(Y2-Y1)/(X2-X1); 246 if(k2==K2) { 247 return false; 248 } 249 else { 250 return true; 251 } 252 } 253 } 254 //有无点重合 255 public boolean samePiont(double X1,double Y1,double X2,double Y2,double X3,double Y3,double X4,double Y4) { 256 if(X1==X2 && Y1==Y2) { 257 return true; 258 } 259 else if(X1==X3 && Y1==Y3) { 260 return true; 261 } 262 else if(X1==X4 && Y1==Y4) { 263 return true; 264 } 265 else if(X2==X3 && Y2==Y3) { 266 return true; 267 } 268 else if(X2==X4 && Y2==Y4) { 269 return true; 270 } 271 else if(X3==X4 && Y3==Y4) { 272 return true; 273 } 274 else { 275 return false; 276 } 277 } 278 public void squareCase() { 279 if(numData(numBlank())) { 280 num=arr.split("\\s"); 281 for(int i=0;i<num.length;i++) { 282 if(!checkNum(num[i])) { 283 System.out.println("Wrong Format"); 284 passport++; 285 break; 286 } 287 } 288 } 289 else { 290 num=arr.split("\\s"); 291 int wrong=0; 292 for(int i=0;i<num.length;i++) { 293 if(!checkNum(num[i])) { 294 wrong++; 295 } 296 } 297 if(wrong==0) { 298 System.out.println("wrong number of points"); 299 } 300 else { 301 System.out.println("Wrong Format"); 302 } 303 passport++; 304 } 305 if(passport==0 && (Choice==1 || Choice==2 || Choice==3)) { 306 x1=cutX(num[0]); y1=cutY(num[0]); 307 x2=cutX(num[1]); y2=cutY(num[1]); 308 x3=cutX(num[2]); y3=cutY(num[2]); 309 x4=cutX(num[3]); y4=cutY(num[3]); 310 l1_2=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2)); 311 l2_3=Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2)); 312 l3_4=Math.sqrt(Math.pow(x3-x4,2)+Math.pow(y3-y4,2)); 313 l4_1=Math.sqrt(Math.pow(x4-x1,2)+Math.pow(y4-y1,2)); 314 } 315 if(passport==0 && Choice==4) { 316 px1=cutX(num[0]); py1=cutY(num[0]); 317 px2=cutX(num[1]); py2=cutY(num[1]); 318 x1=cutX(num[2]); y1=cutY(num[2]); 319 x2=cutX(num[3]); y2=cutY(num[3]); 320 x3=cutX(num[4]); y3=cutY(num[4]); 321 x4=cutX(num[5]); y4=cutY(num[5]); 322 if(px1==px2 && py1==py2) { 323 System.out.println("points coincide"); 324 passport++; 325 } 326 } 327 if(passport==0 && Choice==5) { 328 px=cutX(num[0]); py=cutY(num[0]); 329 x1=cutX(num[1]); y1=cutY(num[1]); 330 x2=cutX(num[2]); y2=cutY(num[2]); 331 x3=cutX(num[3]); y3=cutY(num[3]); 332 x4=cutX(num[4]); y4=cutY(num[4]); 333 334 } 335 } 336 public boolean square() { 337 int wrong=0; 338 if(!sameLine(x1,y1,x2,y2,x3,y3) || !sameLine(x1,y1,x2,y2,x4,y4) || !sameLine(x1,y1,x3,y3,x4,y4) || !sameLine(x2,y2,x3,y3,x4,y4)) { 339 wrong++; 340 } 341 if(wrong==0) { 342 return true; 343 } 344 else { 345 return false; 346 } 347 } 348 public boolean parWeather() { 349 if(l1_2==l3_4 && l2_3==l4_1) { 350 return true; 351 } 352 else { 353 return false; 354 } 355 } 356 public boolean equalLine() { 357 if(l1_2==l2_3 && l3_4==l2_3 && l3_4==l4_1) { //判断菱形或正方形 358 return true; 359 } 360 else { //是否为矩形 361 return false; 362 } 363 } 364 public boolean chooseOne() { //判断任意两条临边是否垂直 365 if( (x2-x1)*(x3-x2)+(y2-y1)*(y3-y2)==0 && (x3-x2)*(x4-x3)+(y3-y2)*(y4-y3)==0 && (x4-x3)*(x4-x1)+(y4-y3)*(y4-y1)==0 && (x4-x1)*(x2-x1)+(y4-y1)*(y2-y1)==0) { 366 return true; 367 } 368 else { 369 System.out.println((x2-x1)*(x3-x2)+(y2-y1)*(y3-y2)); 370 System.out.println((x3-x2)*(x4-x3)+(y3-y2)*(y4-y3)); 371 System.out.println((x4-x3)*(x4-x1)+(y4-y3)*(y4-y1)); 372 System.out.println((x4-x1)*(x2-x1)+(y4-y1)*(y2-y1)); 373 return false; 374 } 375 } 376 // public boolean chooseTwo() { 377 // if(l1_2==l3_4 && l2_3==l4_1) { 378 // if((x2-x2)*(line2.getX3()-line2.getX2()) + (line1.getY2()-line1.getY1())*(line2.getY3()-line2.getY2()) == 0) { 379 // return true; 380 // } 381 // else { 382 // return false; 383 // } 384 // } 385 // else { 386 // return false; 387 // } 388 // } 389 public boolean conOrnot() { //Case5 390 double z1 = (x4-x1)*(y2-y1)-(y4-y1)*(x2-x1); 391 double z2 = (x1-x2)*(y3-y2)-(y1-y2)*(x3-x2); 392 double z3 = (x2-x3)*(y4-y3)-(y2-y3)*(x4-x3); 393 double z4 = (x3-x4)*(y1-y4)-(y3-y4)*(x1-x4); 394 if(z1*z2*z3*z4 > 0 && z1*z2>0 && z3*z4>0) { 395 return true; 396 } 397 else { 398 return false; 399 } 400 } 401 public double cauSone() { 402 s1=0.5*Math.abs((x3*y2-x2*y3)+(x2*y1-x1*y2)+(x1*y3-x3*y1)); 403 s2=0.5*Math.abs((x3*y4-x4*y3)+(x4*y1-x1*y4)+(x1*y3-x3*y1)); 404 S=s1+s2; 405 return S; 406 } 407 public double cauStwo() { 408 s1=0.5*Math.abs((x3*y2-x2*y3)+(x2*y1-x1*y2)+(x1*y3-x3*y1)); 409 s2=0.5*Math.abs((x3*y4-x4*y3)+(x4*y1-x1*y4)+(x1*y3-x3*y1)); 410 s3=0.5*Math.abs((x3*y2-x2*y3)+(x2*y4-x4*y2)+(x4*y3-x3*y4)); 411 s4=0.5*Math.abs((x4*y2-x2*y4)+(x2*y1-x1*y2)+(x1*y4-x4*y1)); 412 if(s1+s2>s3+s4) { 413 S=s3+s4; 414 return S; 415 } 416 else { 417 S=s1+s2; 418 return S; 419 } 420 } 421 public boolean inPiont() { 422 s1=0.5*Math.abs((x3*y2-x2*y3)+(x2*py-px*y2)+(px*y3-x3*py)); 423 s2=0.5*Math.abs((x3*py-px*y3)+(px*y4-x4*py)+(x4*y3-x3*y4)); 424 s3=0.5*Math.abs((px*y2-x2*py)+(x2*y1-x1*y2)+(x1*py-px*y1)); 425 s4=0.5*Math.abs((px*y4-x4*py)+(x4*y1-x1*y4)+(x1*py-px*y1)); 426 S=0.5*Math.abs((x3*y2-x2*y3)+(x2*y1-x1*y2)+(x1*y3-x3*y1))+0.5*Math.abs((x3*y4-x4*y3)+(x4*y1-x1*y4)+(x1*y3-x3*y1)); 427 if(s1+s2+s3+s4==S) { 428 return true; 429 } 430 else { 431 return false; 432 } 433 } 434 }
心得体会
我认为这个月的学习相比上个与上个月的更注重逻辑思维的训练,感觉自己在做题的时候总是盲目的键类,最终导致变量之间的关系混乱不堪。
所以在我看来,在每次编程之前,应当先仔细审题,构建起程序的大体框架,认真布局,逐个实现功能。所以当前最重要的是,还是对于题目的分析。程序大体完成后,经常由于逻辑问题,去猜测试点。可以这个月的PTA作业让我深刻感受到了一个好的构架的重要性。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义