第二次Java随笔
前言:在本次作业中,我的完成量不是特别好,特别是关于图形的迭代作业。在学习上,我也接触到了许多新的知识点。
例如Map、Stream......等等内容,继续加油吧。
pta作业集及超星作业的分析。
一、pta图形类设计迭代作业。
这是我觉得我完成的最差的题目,没有质量,混分也混不到多少。
作业从四月五号开始,一共迭代了四次。
1 import java.util.Scanner; 2 import java.util.regex.PatternSyntaxException; 3 public class Main{ 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 String s; 7 s = input.nextLine(); 8 if(judgeInput(s)==0){ 9 String[] ss=s.split(" "); 10 String[]s1=ss[0].split(","); 11 String[]s2=ss[1].split(","); 12 if(judgeDouble(s1[0])&&judgeDouble(s1[1])&&judgeDouble(s2[0])&&judgeDouble(s2[1])){ 13 double x1=getDouble(s1[0]); 14 double y1=getDouble(s1[1]); 15 double x2=getDouble(s2[0]); 16 double y2=getDouble(s2[1]); 17 double l=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 18 System.out.println(l); 19 } 20 else { 21 System.out.println("Wrong Format"); 22 } 23 } 24 else if(judgeInput(s)==1){ 25 System.out.println("wrong number of points"); 26 } 27 else { 28 System.out.println("Wrong Format"); 29 } 30 } 31 32 public static int judgeInput(String s){ 33 int length=s.length(); 34 int a=0,b=0; 35 String s1,s2; 36 a=s.indexOf(' '); 37 if(a==-1){ 38 return 2; 39 } 40 s1=s.substring(0,a); 41 s2=s.substring(a+1); 42 int length2=s2.length(); 43 for(int i=0;i<length2;i++){ 44 if(s2.charAt(i)==' '){ 45 return 1; 46 } 47 } 48 if(s1.indexOf(',')==-1||s1.indexOf(',')==-1){ 49 return 2; 50 } 51 else { 52 a=s1.indexOf(','); 53 b=s2.indexOf(','); 54 if(s1.indexOf(',',a+1)!=-1||s2.indexOf(',',b+1)!=-1){ 55 return 2; 56 } 57 } 58 return 0; 59 } 60 61 public static double getDouble(String s){ 62 if(s.charAt(0)>='0'&&s.charAt(0)<='9'){ 63 return Double.parseDouble(s); 64 } 65 else if(s.charAt(0)=='+'&&(s.charAt(1)>='0'&&s.charAt(1)<='9')){ 66 String[] a= s.split("\\+"); 67 return Double.parseDouble(a[1]); 68 } 69 else if(s.charAt(0)=='-'&&(s.charAt(1)>='0'&&s.charAt(1)<='9')){ 70 String[] a=s.split("-"); 71 return -Double.parseDouble(a[1]); 72 } 73 return 0; 74 } 75 76 public static boolean judgeDouble(String s) { 77 if ((s.charAt(0) == '+' || s.charAt(0) == '-') && (s.charAt(1) < '0' || s.charAt(1) > '9')) { 78 return false; 79 } else if ((s.charAt(0) < '0' || s.charAt(0) > '9') && s.charAt(0) != '+' && s.charAt(0) != '-') { 80 return false; 81 } 82 int a=s.indexOf('.'); 83 if(s.indexOf('.',a+1)!=-1) 84 return false; 85 int l=s.length(); 86 if(a!=-1){ 87 if(s.charAt(a-1)<'0'||s.charAt(a-1)>'9'){ 88 return false; 89 } 90 if(a==l-1){ 91 return false; 92 } 93 } 94 if(s.charAt(0)=='0'){ 95 if(l==1){ 96 return true; 97 } 98 else { 99 if(s.charAt(1)!='.'){ 100 return false; 101 } 102 } 103 } 104 return true; 105 } 106 }
这是我的代码,
采坑心得:其中最大的难点应该是提取输入的数据吧,而且当时并没有学正则表达式,也有过想去学,但是刚接触时无法接受
所以大部分提取代码还是靠indexOf方法和split方法来对字符串进行切割。
1 import java.util.Scanner; 2 public class Main { 3 public static void main(String[] args) { 4 Line1 l1=new Line1(); 5 Line2 l2=new Line2(); 6 Line3 l3=new Line3(); 7 Scanner input = new Scanner(System.in); 8 String s; 9 s = input.nextLine(); 10 String []ss=s.split(":"); 11 switch (ss[0]){ 12 case "1":l1.getSlope(ss[1]); 13 break; 14 case "2":l2.getDistance(ss[1]); 15 break; 16 case "3":l2.judgePoint(ss[1]); 17 break; 18 case "4":l3.judgeLine(ss[1]); 19 break; 20 case "5":l3.getPoint(ss[1]); 21 break; 22 default:System.out.println("Wrong Format"); 23 } 24 } 25 } 26 class Line{ 27 public double getDouble(String s){ 28 if(s.charAt(0)>='0'&&s.charAt(0)<='9'){ 29 return Double.parseDouble(s); 30 } 31 else if(s.charAt(0)=='+'&&(s.charAt(1)>='0'&&s.charAt(1)<='9')){ 32 String[] a= s.split("\\+"); 33 return Double.parseDouble(a[1]); 34 } 35 else if(s.charAt(0)=='-'&&(s.charAt(1)>='0'&&s.charAt(1)<='9')){ 36 String[] a=s.split("-"); 37 return -Double.parseDouble(a[1]); 38 } 39 return 0; 40 } 41 public boolean judgeDouble(String s) { 42 int len=s.length(); 43 for(int i=0;i<len;i++){ 44 if((s.charAt(i)>'9'||s.charAt(i)<'0')&&s.charAt(i)!='+'&&s.charAt(i)!='-'&&s.charAt(i)!='.') 45 return false; 46 } 47 if ((s.charAt(0) == '+' || s.charAt(0) == '-') && (s.charAt(1) < '0' || s.charAt(1) > '9')) { 48 return false; 49 } else if ((s.charAt(0) < '0' || s.charAt(0) > '9') && s.charAt(0) != '+' && s.charAt(0) != '-') { 50 return false; 51 } 52 int a=s.indexOf('.'); 53 if(s.indexOf('.',a+1)!=-1) 54 return false; 55 int l=s.length(); 56 if(a!=-1){ 57 if(s.charAt(a-1)<'0'||s.charAt(a-1)>'9'){ 58 return false; 59 } 60 if(a==l-1){ 61 return false; 62 } 63 } 64 if(s.charAt(0)=='0'){ 65 if(l==1){ 66 return true; 67 } 68 else { 69 if(s.charAt(1)!='.'){ 70 return false; 71 } 72 } 73 } 74 return true; 75 } 76 } 77 class Line1 { 78 Line l=new Line(); 79 public void getSlope(String s) { 80 if (judgeInput(s) == 0) { 81 String[] ss = s.split(" "); 82 String[] s1 = ss[0].split(","); 83 String[] s2 = ss[1].split(","); 84 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1])) { 85 double x1 = l.getDouble(s1[0]); 86 double y1 = l.getDouble(s1[1]); 87 double x2 = l.getDouble(s2[0]); 88 double y2 = l.getDouble(s2[1]); 89 if(x1==x2){ 90 if(y1==y2) 91 System.out.println("points coincide"); 92 else 93 System.out.println("Slope does not exist"); 94 } 95 else if(x1!=x2){ 96 double k=(y2-y1)/(x2-x1); 97 System.out.println(k); 98 } 99 } 100 else 101 System.out.println("Wrong Format"); 102 } else if (judgeInput(s) == 1) 103 System.out.println("wrong number of points"); 104 else 105 System.out.println("Wrong Format"); 106 } 107 public int judgeInput(String s){ 108 int length=s.length(); 109 int a=0,b=0; 110 String s1,s2; 111 a=s.indexOf(' '); 112 if(a==-1){ 113 if(s.indexOf(",")!=-1){ 114 String ss=s.substring(0,s.indexOf(",")); 115 String sss=s.substring(s.indexOf(",")+1); 116 if(l.judgeDouble(ss)&&l.judgeDouble(sss)) 117 return 1; 118 else return 2;} 119 else return 2; 120 } 121 s1=s.substring(0,a); 122 s2=s.substring(a+1); 123 int length2=s2.length(); 124 for(int i=0;i<length2;i++){ 125 if(s2.charAt(i)==' '){ 126 return 1; 127 } 128 } 129 if(s1.indexOf(',')==-1||s2.indexOf(',')==-1){ 130 return 2; 131 } 132 else { 133 a=s1.indexOf(','); 134 b=s2.indexOf(','); 135 if(s1.indexOf(',',a+1)!=-1||s2.indexOf(',',b+1)!=-1){ 136 return 2; 137 } 138 } 139 return 0; 140 } 141 } 142 class Line2{ 143 Line l=new Line(); 144 public void getDistance(String s){ 145 if(judgeInput(s)==0){ 146 String[] ss=s.split(" "); 147 String[] s1 = ss[0].split(","); 148 String[] s2 = ss[1].split(","); 149 String[] s3 = ss[2].split(","); 150 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1])) { 151 double x1 = l.getDouble(s1[0]); 152 double y1 = l.getDouble(s1[1]); 153 double x2 = l.getDouble(s2[0]); 154 double y2 = l.getDouble(s2[1]); 155 double x3 = l.getDouble(s3[0]); 156 double y3 = l.getDouble(s3[1]); 157 if(x3==x2){ 158 if(y3==y2) 159 System.out.println("points coincide"); 160 else 161 System.out.println(Math.abs(x1-x3)); 162 } 163 else { 164 double k=(y3-y2)/(x3-x2); 165 double b=y3-k*x3; 166 System.out.println(Math.abs(k*x1-y1+b)/Math.sqrt(k*k+1)); 167 } 168 } 169 else 170 System.out.println("Wrong Format"); 171 } 172 else if (judgeInput(s) == 1) 173 System.out.println("wrong number of points"); 174 else 175 System.out.println("Wrong Format"); 176 } 177 public void judgePoint(String s){ 178 if(judgeInput(s)==0){ 179 String[] ss=s.split(" "); 180 String[] s1 = ss[0].split(","); 181 String[] s2 = ss[1].split(","); 182 String[] s3 = ss[2].split(","); 183 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1])) { 184 double x1 = l.getDouble(s1[0]); 185 double y1 = l.getDouble(s1[1]); 186 double x2 = l.getDouble(s2[0]); 187 double y2 = l.getDouble(s2[1]); 188 double x3 = l.getDouble(s3[0]); 189 double y3 = l.getDouble(s3[1]); 190 if(x3==x2){ 191 if(y3==y2) 192 System.out.println("points coincide"); 193 else if(x1==x2) 194 System.out.println("true"); 195 else 196 System.out.println("false"); 197 } 198 else { 199 double k=(y3-y2)/(x3-x2); 200 double b=y3-k*x3; 201 if(k*x1-y1+b==0) 202 System.out.println("true"); 203 else 204 System.out.println("false"); 205 } 206 } 207 else 208 System.out.println("Wrong Format"); 209 } 210 else if (judgeInput(s) == 1) 211 System.out.println("wrong number of points"); 212 else 213 System.out.println("Wrong Format"); 214 } 215 public int judgeInput(String s){ 216 int length=s.length(); 217 int a=0,b=0; 218 String s1,s2,s3; 219 String ss; 220 a=s.indexOf(' '); 221 if(a==-1){ 222 if(s.indexOf(",")!=-1){ 223 ss=s.substring(0,s.indexOf(",")); 224 String sss=s.substring(s.indexOf(",")+1); 225 if(l.judgeDouble(ss)&&l.judgeDouble(sss)) 226 return 1; 227 else return 2;} 228 else return 2; 229 } 230 s1=s.substring(0,a); 231 ss=s.substring(a+1); 232 b=s.indexOf(" ",a+1); 233 if(b==-1){ 234 if(ss.indexOf(",")!=-1){ 235 String ssss=ss.substring(0,ss.indexOf(",")); 236 String sss=ss.substring(ss.indexOf(",")+1); 237 if(l.judgeDouble(ssss)&&l.judgeDouble(sss)) 238 return 1; 239 else return 2;} 240 else return 2; 241 } 242 s2=s.substring(a+1,b); 243 s3=s.substring(b+1); 244 int length2=s2.length(); 245 int length3=s3.length(); 246 for(int i=0;i<length2;i++){ 247 if(s2.charAt(i)==' '){ 248 return 1; 249 } 250 } 251 for(int i=0;i<length3;i++){ 252 if(s3.charAt(i)==' '){ 253 return 1; 254 } 255 } 256 if(s1.indexOf(',')==-1||s2.indexOf(',')==-1||s3.indexOf(',')==-1){ 257 return 2; 258 } 259 else { 260 a=s1.indexOf(','); 261 b=s2.indexOf(','); 262 int c=s3.indexOf(','); 263 if(s1.indexOf(',',a+1)!=-1||s2.indexOf(',',b+1)!=-1||s3.indexOf(',',c+1)!=-1){ 264 return 2; 265 } 266 } 267 return 0; 268 } 269 } 270 class Line3{ 271 Line l=new Line(); 272 public void getPoint(String s){ 273 if(judgeInput(s)==0){ 274 String[] ss=s.split(" "); 275 String[] s1 = ss[0].split(","); 276 String[] s2 = ss[1].split(","); 277 String[] s3 = ss[2].split(","); 278 String[] s4 = ss[3].split(","); 279 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1]) && l.judgeDouble(s4[0]) && l.judgeDouble(s4[1])) { 280 double x1 = l.getDouble(s1[0]); 281 double y1 = l.getDouble(s1[1]); 282 double x2 = l.getDouble(s2[0]); 283 double y2 = l.getDouble(s2[1]); 284 double x3 = l.getDouble(s3[0]); 285 double y3 = l.getDouble(s3[1]); 286 double x4 = l.getDouble(s4[0]); 287 double y4 = l.getDouble(s4[1]); 288 String t="true"; 289 String f="false"; 290 if((x1==x2&&y1==y2)||(x3==x4&&y3==y4)) 291 System.out.println("points coincide"); 292 else if(x1==x2&&x3==x4){ 293 System.out.println("is parallel lines,have no intersection point"); 294 } 295 else if(x1==x2&&x3!=x4){ 296 double k2=(y4-y3)/(x4-x3); 297 double b2=y4-k2*x4; 298 double x0=x1; 299 if(((k2*x0+b2)<max(y1,y2)&&(k2*x0+b2)>min(y1,y2))||((k2*x0+b2)<max(y3,y4)&&(k2*x0+b2)>min(y3,y4))) 300 System.out.println(x0+","+(k2*x0+b2)+" true"); 301 else System.out.println((float)x0+","+(float)(k2*x0+b2)+" false"); 302 } 303 else if(x1!=x2&&x3==x4){ 304 double k1=(y2-y1)/(x2-x1); 305 double b1=y2-k1*x2; 306 double x0=x3; 307 if(((k1*x0+b1)<max(y1,y2)&&(k1*x0+b1)>min(y1,y2))||((k1*x0+b1)<max(y3,y4)&&(k1*x0+b1)>min(y3,y4))) 308 System.out.println(x0+","+(k1*x0+b1)+" true"); 309 else System.out.println((float)x0+","+(float)(k1*x0+b1)+" false"); 310 } 311 else { 312 double k1=(y2-y1)/(x2-x1); 313 double b1=y2-k1*x2; 314 double k2=(y4-y3)/(x4-x3); 315 double b2=y4-k2*x4; 316 if(k1==k2) 317 System.out.println("is parallel lines,have no intersection point"); 318 else { 319 double x0=(b2-b1)/(k1-k2); 320 if(((k1*x0+b1)<max(y1,y2)&&(k1*x0+b1)>min(y1,y2))||((k1*x0+b1)<max(y3,y4)&&(k1*x0+b1)>min(y3,y4))) 321 System.out.println((float)x0+","+(float)(k1*x0+b1)+" true"); 322 else System.out.println((float)x0+","+(float)(k1*x0+b1)+" false"); 323 } 324 } 325 } 326 else 327 System.out.println("Wrong Format"); 328 } 329 else if (judgeInput(s) == 1) 330 System.out.println("wrong number of points"); 331 else 332 System.out.println("Wrong Format"); 333 } 334 public void judgeLine(String s){ 335 if(judgeInput(s)==0){ 336 String[] ss=s.split(" "); 337 String[] s1 = ss[0].split(","); 338 String[] s2 = ss[1].split(","); 339 String[] s3 = ss[2].split(","); 340 String[] s4 = ss[3].split(","); 341 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1]) && l.judgeDouble(s4[0]) && l.judgeDouble(s4[1])) { 342 double x1 = l.getDouble(s1[0]); 343 double y1 = l.getDouble(s1[1]); 344 double x2 = l.getDouble(s2[0]); 345 double y2 = l.getDouble(s2[1]); 346 double x3 = l.getDouble(s3[0]); 347 double y3 = l.getDouble(s3[1]); 348 double x4 = l.getDouble(s4[0]); 349 double y4 = l.getDouble(s4[1]); 350 if((x1==x2&&y1==y2)||(x3==x4&&y3==y4)) 351 System.out.println("points coincide"); 352 else if(x1==x2&&x3==x4) 353 System.out.println("true"); 354 else if(((y2-y1)/(x2-x1))==(y4-y3)/(x4-x3)) 355 System.out.println("true"); 356 else System.out.println("false"); 357 } 358 else 359 System.out.println("Wrong Format"); 360 } 361 else if (judgeInput(s) == 1) 362 System.out.println("wrong number of points"); 363 else 364 System.out.println("Wrong Format"); 365 } 366 public int judgeInput(String s){ 367 int length=s.length(); 368 int a=0,b=0,c=0,d=0; 369 String s1,s2,s3,s4,ss,sss; 370 a=s.indexOf(' '); 371 if(a==-1){ 372 if(s.indexOf(",")!=-1){ 373 ss=s.substring(0,s.indexOf(",")); 374 sss=s.substring(s.indexOf(",")+1); 375 if(l.judgeDouble(ss)&&l.judgeDouble(sss)) 376 return 1; 377 else return 2;} 378 else return 2; 379 } 380 s1=s.substring(0,a); 381 ss=s.substring(a+1); 382 b=s.indexOf(" ",a+1); 383 if(b==-1){ 384 if(ss.indexOf(",")!=-1){ 385 String ssss=ss.substring(0,ss.indexOf(",")); 386 sss=ss.substring(ss.indexOf(",")+1); 387 if(l.judgeDouble(ssss)&&l.judgeDouble(sss)) 388 return 1; 389 else return 2;} 390 else return 2; 391 } 392 s2=s.substring(a+1,b); 393 sss=s.substring(b+1); 394 c=s.indexOf(" ",b+1); 395 if(c==-1){ 396 if(sss.indexOf(",")!=-1){ 397 String ssss=sss.substring(0,sss.indexOf(",")); 398 String sssss=sss.substring(sss.indexOf(",")+1); 399 if(l.judgeDouble(ssss)&&l.judgeDouble(sssss)) 400 return 1; 401 else return 2;} 402 else return 2; 403 } 404 s3=s.substring(b+1,c); 405 s4=s.substring(c+1); 406 int length2=s2.length(); 407 int length3=s3.length(); 408 int length4=s4.length(); 409 for(int i=0;i<length2;i++){ 410 if(s2.charAt(i)==' '){ 411 return 1; 412 } 413 } 414 for(int i=0;i<length3;i++){ 415 if(s3.charAt(i)==' '){ 416 return 1; 417 } 418 } 419 for(int i=0;i<length4;i++){ 420 if(s4.charAt(i)==' '){ 421 return 1; 422 } 423 } 424 if(s1.indexOf(',')==-1||s2.indexOf(',')==-1||s3.indexOf(',')==-1||s4.indexOf(',')==-1){ 425 return 2; 426 } 427 else { 428 a=s1.indexOf(','); 429 b=s2.indexOf(','); 430 c=s3.indexOf(','); 431 d=s4.indexOf(','); 432 if(s1.indexOf(',',a+1)!=-1||s2.indexOf(',',b+1)!=-1||s3.indexOf(',',c+1)!=-1||s4.indexOf(',',d+1)!=-1){ 433 return 2; 434 } 435 } 436 return 0; 437 } 438 public double max(double x,double y){ 439 if(x>=y) 440 return x; 441 else 442 return y; 443 } 444 public double min(double x,double y){ 445 if(x<=y) 446 return x; 447 else 448 return y; 449 } 450 }
踩坑心得: 从这里代码开始复杂起来,但由于开始设计的结构就是不行的,导致重复代码存在相当多,但是算法还是比较简单。我这测试点也是
还有一个没有过(由于测试点不开放,我也找不出错误在哪)。
1 import java.util.Scanner; 2 import java.math.BigDecimal; 3 public class Main { 4 public static void main(String[] args) { 5 Triangle1 t1 = new Triangle1(); 6 Triangle2 t2 = new Triangle2(); 7 Triangle3 t3 = new Triangle3(); 8 Scanner input = new Scanner(System.in); 9 String s; 10 s = input.nextLine(); 11 String []ss=s.split(":"); 12 switch (ss[0]){ 13 case "1":t1.judgeTriangle1(ss[1]); 14 break; 15 case "2":t1.getData(ss[1]); 16 break; 17 case "3":t1.judgeTriangle2(ss[1]); 18 break; 19 case "4":t3.pointNos(ss[1]); 20 break; 21 case "5":t2.getPoint(ss[1]); 22 break; 23 default:System.out.println("Wrong Format"); 24 } 25 } 26 } 27 class Line{ 28 public double getDouble(String s){ 29 if(s.charAt(0)>='0'&&s.charAt(0)<='9'){ 30 return Double.parseDouble(s); 31 } 32 else if(s.charAt(0)=='+'&&(s.charAt(1)>='0'&&s.charAt(1)<='9')){ 33 String[] a= s.split("\\+"); 34 return Double.parseDouble(a[1]); 35 } 36 else if(s.charAt(0)=='-'&&(s.charAt(1)>='0'&&s.charAt(1)<='9')){ 37 String[] a=s.split("-"); 38 return -Double.parseDouble(a[1]); 39 } 40 return 0; 41 } 42 public boolean judgeDouble(String s) { 43 int len=s.length(); 44 for(int i=0;i<len;i++){ 45 if((s.charAt(i)>'9'||s.charAt(i)<'0')&&s.charAt(i)!='+'&&s.charAt(i)!='-'&&s.charAt(i)!='.') 46 return false; 47 } 48 if ((s.charAt(0) == '+' || s.charAt(0) == '-') && (s.charAt(1) < '0' || s.charAt(1) > '9')) { 49 return false; 50 } else if ((s.charAt(0) < '0' || s.charAt(0) > '9') && s.charAt(0) != '+' && s.charAt(0) != '-') { 51 return false; 52 } 53 int a=s.indexOf('.'); 54 if(s.indexOf('.',a+1)!=-1) 55 return false; 56 int l=s.length(); 57 if(a!=-1){ 58 if(s.charAt(a-1)<'0'||s.charAt(a-1)>'9'){ 59 return false; 60 } 61 if(a==l-1){ 62 return false; 63 } 64 } 65 if(s.charAt(0)=='0'){ 66 if(l==1){ 67 return true; 68 } 69 else { 70 if(s.charAt(1)!='.'){ 71 return false; 72 } 73 } 74 } 75 return true; 76 } 77 } 78 class Triangle1 { 79 Line l=new Line(); 80 public void judgeTriangle1(String s){ 81 if(judgeInput(s)==0){ 82 String[] ss=s.split(" "); 83 String[] s1 = ss[0].split(","); 84 String[] s2 = ss[1].split(","); 85 String[] s3 = ss[2].split(","); 86 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1])) { 87 double x1 = l.getDouble(s1[0]); 88 double y1 = l.getDouble(s1[1]); 89 double x2 = l.getDouble(s2[0]); 90 double y2 = l.getDouble(s2[1]); 91 double x3 = l.getDouble(s3[0]); 92 double y3 = l.getDouble(s3[1]); 93 if(((x1==x2)&&(y1==y2))||((x1==x3)&&(y1==y3))||((x2==x3)&&(y2==y3))) 94 System.out.println("data error"); 95 else if(x1==x2&&x2==x3) 96 System.out.println("data error"); 97 else { 98 double l1=Math.sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1)); 99 double l2=Math.sqrt((y3-y2)*(y3-y2)+(x3-x2)*(x3-x2)); 100 double l3=Math.sqrt((y3-y1)*(y3-y1)+(x3-x1)*(x3-x1)); 101 if(l1==l2||l2==l3||l1==l3) 102 System.out.print("true "); 103 else System.out.print("false "); 104 if(l1==l2&&l2==l3) 105 System.out.print("true"); 106 else System.out.print("false"); 107 } 108 } 109 else 110 System.out.println("Wrong Format"); 111 } 112 else if (judgeInput(s) == 1) 113 System.out.println("wrong number of points"); 114 else 115 System.out.println("Wrong Format"); 116 } 117 public void getData(String s){ 118 if(judgeInput(s)==0){ 119 String[] ss=s.split(" "); 120 String[] s1 = ss[0].split(","); 121 String[] s2 = ss[1].split(","); 122 String[] s3 = ss[2].split(","); 123 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1])) { 124 double x1 = l.getDouble(s1[0]); 125 double y1 = l.getDouble(s1[1]); 126 double x2 = l.getDouble(s2[0]); 127 double y2 = l.getDouble(s2[1]); 128 double x3 = l.getDouble(s3[0]); 129 double y3 = l.getDouble(s3[1]); 130 if(((x1==x2)&&(y1==y2))||((x1==x3)&&(y1==y3))||((x2==x3)&&(y2==y3))) 131 System.out.println("data error"); 132 else if(x1==x2&&x2==x3) 133 System.out.println("data error"); 134 else { 135 double l1=Math.sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1)); 136 double l2=Math.sqrt((y3-y2)*(y3-y2)+(x3-x2)*(x3-x2)); 137 double l3=Math.sqrt((y3-y1)*(y3-y1)+(x3-x1)*(x3-x1)); 138 double l=l1+l2+l3; 139 String l_=String.format("%.6f",l); 140 l_=new BigDecimal(l_).stripTrailingZeros().toPlainString(); 141 l=Double.parseDouble(l_); 142 System.out.print(l); 143 System.out.print(" "); 144 if(x1==x2) { 145 double d = Math.abs(x3 - x1); 146 double S=d*l1/2; 147 String S_=String.format("%.6f",S); 148 S_=new BigDecimal(S_).stripTrailingZeros().toPlainString(); 149 S=Double.parseDouble(S_); 150 System.out.print(S); 151 System.out.print(" "); 152 } 153 else { 154 double k=(y2-y1)/(x2-x1); 155 double b=y2-k*x2; 156 double d=Math.abs(k*x3-y3+b)/Math.sqrt(k*k+1); 157 double S=d*l1/2; 158 String S_=String.format("%.6f",S); 159 S_=new BigDecimal(S_).stripTrailingZeros().toPlainString(); 160 S=Double.parseDouble(S_); 161 System.out.print(S); 162 System.out.print(" "); 163 } 164 String x0=String.format("%.6f",(x1+x2+x3)/3); 165 String y0=String.format("%.6f",(y1+y2+y3)/3); 166 x0=new BigDecimal(x0).stripTrailingZeros().toPlainString(); 167 y0=new BigDecimal(y0).stripTrailingZeros().toPlainString(); 168 double x=Double.parseDouble(x0); 169 double y=Double.parseDouble(y0); 170 System.out.print(x+","+y); 171 } 172 } 173 else 174 System.out.println("Wrong Format"); 175 } 176 else if (judgeInput(s) == 1) 177 System.out.println("wrong number of points"); 178 else 179 System.out.println("Wrong Format"); 180 } 181 public void judgeTriangle2(String s){ 182 if(judgeInput(s)==0){ 183 String[] ss=s.split(" "); 184 String[] s1 = ss[0].split(","); 185 String[] s2 = ss[1].split(","); 186 String[] s3 = ss[2].split(","); 187 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1])) { 188 double x1 = l.getDouble(s1[0]); 189 double y1 = l.getDouble(s1[1]); 190 double x2 = l.getDouble(s2[0]); 191 double y2 = l.getDouble(s2[1]); 192 double x3 = l.getDouble(s3[0]); 193 double y3 = l.getDouble(s3[1]); 194 if(((x1==x2)&&(y1==y2))||((x1==x3)&&(y1==y3))||((x2==x3)&&(y2==y3))) 195 System.out.println("data error"); 196 else if(x1==x2&&x2==x3) 197 System.out.println("data error"); 198 else { 199 double l1=Math.sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1)); 200 double l2=Math.sqrt((y3-y2)*(y3-y2)+(x3-x2)*(x3-x2)); 201 double l3=Math.sqrt((y3-y1)*(y3-y1)+(x3-x1)*(x3-x1)); 202 double x=l1*l1+l2*l2-l3*l3; 203 double y=l1*l1+l3*l3-l2*l2; 204 double z=l2*l2+l3*l3-l1*l1; 205 if(Math.abs(x)<0.0000001||Math.abs(y)<0.0000001||Math.abs(z)<0.000001){ 206 System.out.print("false true false"); 207 } 208 else if(x<0||y<0||z<0){ 209 System.out.print("true false false"); 210 } 211 else { 212 System.out.print("false false true"); 213 } 214 } 215 } 216 else 217 System.out.println("Wrong Format"); 218 } 219 else if (judgeInput(s) == 1) 220 System.out.println("wrong number of points"); 221 else 222 System.out.println("Wrong Format"); 223 } 224 public int judgeInput(String s){ 225 int length=s.length(); 226 int a=0,b=0; 227 String s1,s2,s3; 228 String ss; 229 a=s.indexOf(' '); 230 if(a==-1){ 231 if(s.indexOf(",")!=-1){ 232 ss=s.substring(0,s.indexOf(",")); 233 String sss=s.substring(s.indexOf(",")+1); 234 if(l.judgeDouble(ss)&&l.judgeDouble(sss)) 235 return 1; 236 else return 2;} 237 else return 2; 238 } 239 s1=s.substring(0,a); 240 ss=s.substring(a+1); 241 b=s.indexOf(" ",a+1); 242 if(b==-1){ 243 if(ss.indexOf(",")!=-1){ 244 String ssss=ss.substring(0,ss.indexOf(",")); 245 String sss=ss.substring(ss.indexOf(",")+1); 246 if(l.judgeDouble(ssss)&&l.judgeDouble(sss)) 247 return 1; 248 else return 2;} 249 else return 2; 250 } 251 s2=s.substring(a+1,b); 252 s3=s.substring(b+1); 253 int length2=s2.length(); 254 int length3=s3.length(); 255 for(int i=0;i<length2;i++){ 256 if(s2.charAt(i)==' '){ 257 return 1; 258 } 259 } 260 for(int i=0;i<length3;i++){ 261 if(s3.charAt(i)==' '){ 262 return 1; 263 } 264 } 265 if(s1.indexOf(',')==-1||s2.indexOf(',')==-1||s3.indexOf(',')==-1){ 266 return 2; 267 } 268 else { 269 a=s1.indexOf(','); 270 b=s2.indexOf(','); 271 int c=s3.indexOf(','); 272 if(s1.indexOf(',',a+1)!=-1||s2.indexOf(',',b+1)!=-1||s3.indexOf(',',c+1)!=-1){ 273 return 2; 274 } 275 } 276 return 0; 277 } 278 } 279 class Triangle2 { 280 Line l = new Line(); 281 public void getPoint(String s){ 282 if(judgeInput(s)==0){ 283 String[] ss=s.split(" "); 284 String[] s1 = ss[0].split(","); 285 String[] s2 = ss[1].split(","); 286 String[] s3 = ss[2].split(","); 287 String[] s4 = ss[3].split(","); 288 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1]) && l.judgeDouble(s4[0]) && l.judgeDouble(s4[1])) { 289 double x1 = l.getDouble(s1[0]); 290 double y1 = l.getDouble(s1[1]); 291 double x2 = l.getDouble(s2[0]); 292 double y2 = l.getDouble(s2[1]); 293 double x3 = l.getDouble(s3[0]); 294 double y3 = l.getDouble(s3[1]); 295 double x4 = l.getDouble(s4[0]); 296 double y4 = l.getDouble(s4[1]); 297 if(((x2==x3)&&(y2==y3))||((x2==x4)&&(y2==y4))||((x3==x4)&&(y3==y4))) 298 System.out.print("data error"); 299 else if(x2==x3||x2==x4||x3==x4) { 300 if (x2 == x3 && x2 == x4) 301 System.out.print("data error"); 302 else if (x2 == x3 && x2 != x4) { 303 if (x1 == x2) { 304 if (y1 > max(y2, y3) || y1 < min(y2, y3)) { 305 System.out.print("outof the triangle"); 306 } else System.out.print("on the triangle"); 307 } 308 else{ 309 double k2=(y4-y2)/(x4-x2); 310 double k3=(y4-y3)/(x4-x3); 311 double b2=y4-k2*x4; 312 double b3=y4-k3*x4; 313 double y_2=k2*x1+b2; 314 double y_3=k3*x1+b3; 315 if((y_2<=max(y2,y4)&&y_2>=min(y2,y4))&&(y_3<=max(y3,y4)&&y_3>=min(y3,y4))) 316 System.out.print("outof the triangle"); 317 else if((y_2<=max(y2,y4)&&y_2>=min(y2,y4))||(y_3<=max(y3,y4)&&y_3>=min(y3,y4))) 318 System.out.print("in the triangle"); 319 else System.out.print("outof the triangle"); 320 } 321 } else if (x2 == x4 && x2 != x3) { 322 if (x1 == x2) { 323 if (y1 > max(y2, y4) || y1 < min(y2, y4)) { 324 System.out.print("outof the triangle"); 325 } else System.out.print("on the triangle"); 326 } 327 else{ 328 double k1=(y3-y2)/(x3-x2); 329 double k3=(y4-y3)/(x4-x3); 330 double b1=y3-k1*x3; 331 double b3=y3-k3*x3; 332 double y_1=k1*x1+b1; 333 double y_3=k3*x1+b3; 334 if((y_1<=max(y2,y3)&&y_1>=min(y2,y3))&&(y_3<=max(y2,y4)&&y_3>=min(y2,y4))) 335 System.out.print("outof the triangle"); 336 else if((y_1<=max(y2,y3)&&y_1>=min(y2,y3))||(y_3<=max(y2,y4)&&y_3>=min(y2,y4))) 337 System.out.print("in the triangle"); 338 else System.out.print("outof the triangle"); 339 } 340 } else if (x3 == x4 && x2 != x3) { 341 if (x1 == x3) { 342 if (y1 > max(y3, y4) || y1 < min(y3, y4)) { 343 System.out.print("outof the triangle"); 344 } else System.out.print("on the triangle"); 345 } 346 else{ 347 double k1=(y3-y2)/(x3-x2); 348 double k2=(y4-y2)/(x4-x2); 349 double b1=y2-k1*x2; 350 double b2=y2-k2*x2; 351 double y_1=k1*x1+b1; 352 double y_2=k2*x1+b2; 353 if((y_1<=max(y2,y3)&&y_1>=min(y2,y3))&&(y_2<=max(y2,y3)&&y_2>=min(y2,y3))) 354 System.out.print("outof the triangle"); 355 else if((y_1<=max(y2,y3)&&y_1>=min(y2,y3))||(y_2<=max(y2,y3)&&y_2>=min(y2,y3))) 356 System.out.print("in the triangle"); 357 else System.out.print("outof the triangle"); 358 } 359 } 360 } 361 else { 362 double k1=(y3-y2)/(x3-x2); 363 double k2=(y4-y2)/(x4-x2); 364 double b1=y2-k1*x2; 365 double b2=y2-k2*x2; 366 double k3=(y4-y3)/(x4-x3); 367 double b3=y3-k3*x3; 368 if((y1==k1*x1+b1)&&(y1<=max(y2,y3))&&(y1>=min(y2,y3))) 369 System.out.print("on the triangle"); 370 else if((y1==k2*x1+b2)&&(y1<=max(y2,y4))&&(y1>=min(y2,y4))) 371 System.out.print("on the triangle"); 372 else if((y1==k3*x1+b3)&&(y1<=max(y3,y4))&&(y1>=min(y3,y4))) 373 System.out.print("on the triangle"); 374 double y_1=k1*x1+b1; 375 double y_2=k2*x1+b2; 376 double y_3=k3*x1+b3; 377 if ((y_1<=max(y1,y2)&&y_1>=min(y1,y2))&&!((y_2<=max(y2,y3)&&y_2>=min(y2,y3)))&&!((y_3<=max(y3,y4)&&y_3>=min(y3,y4)))) 378 System.out.print("in the triangle"); 379 else if(!(y_1<=max(y1,y2)&&y_1>=min(y1,y2))&&((y_2<=max(y2,y3)&&y_2>=min(y2,y3)))&&!((y_3<=max(y3,y4)&&y_3>=min(y3,y4)))) 380 System.out.print("in the triangle"); 381 else if(!(y_1<=max(y1,y2)&&y_1>=min(y1,y2))&&!((y_2<=max(y2,y3)&&y_2>=min(y2,y3)))&&((y_3<=max(y3,y4)&&y_3>=min(y3,y4)))) 382 System.out.print("in the triangle"); 383 else System.out.print("outof the triangle"); 384 } 385 } 386 else 387 System.out.println("Wrong Format"); 388 } 389 else if (judgeInput(s) == 1) 390 System.out.println("wrong number of points"); 391 else 392 System.out.println("Wrong Format"); 393 } 394 public int judgeInput(String s){ 395 int length=s.length(); 396 int a=0,b=0,c=0,d=0; 397 String s1,s2,s3,s4,ss,sss; 398 a=s.indexOf(' '); 399 if(a==-1){ 400 if(s.indexOf(",")!=-1){ 401 ss=s.substring(0,s.indexOf(",")); 402 sss=s.substring(s.indexOf(",")+1); 403 if(l.judgeDouble(ss)&&l.judgeDouble(sss)) 404 return 1; 405 else return 2;} 406 else return 2; 407 } 408 s1=s.substring(0,a); 409 ss=s.substring(a+1); 410 b=s.indexOf(" ",a+1); 411 if(b==-1){ 412 if(ss.indexOf(",")!=-1){ 413 String ssss=ss.substring(0,ss.indexOf(",")); 414 sss=ss.substring(ss.indexOf(",")+1); 415 if(l.judgeDouble(ssss)&&l.judgeDouble(sss)) 416 return 1; 417 else return 2;} 418 else return 2; 419 } 420 s2=s.substring(a+1,b); 421 sss=s.substring(b+1); 422 c=s.indexOf(" ",b+1); 423 if(c==-1){ 424 if(sss.indexOf(",")!=-1){ 425 String ssss=sss.substring(0,sss.indexOf(",")); 426 String sssss=sss.substring(sss.indexOf(",")+1); 427 if(l.judgeDouble(ssss)&&l.judgeDouble(sssss)) 428 return 1; 429 else return 2;} 430 else return 2; 431 } 432 s3=s.substring(b+1,c); 433 s4=s.substring(c+1); 434 int length2=s2.length(); 435 int length3=s3.length(); 436 int length4=s4.length(); 437 for(int i=0;i<length2;i++){ 438 if(s2.charAt(i)==' '){ 439 return 1; 440 } 441 } 442 for(int i=0;i<length3;i++){ 443 if(s3.charAt(i)==' '){ 444 return 1; 445 } 446 } 447 for(int i=0;i<length4;i++){ 448 if(s4.charAt(i)==' '){ 449 return 1; 450 } 451 } 452 if(s1.indexOf(',')==-1||s2.indexOf(',')==-1||s3.indexOf(',')==-1||s4.indexOf(',')==-1){ 453 return 2; 454 } 455 else { 456 a=s1.indexOf(','); 457 b=s2.indexOf(','); 458 c=s3.indexOf(','); 459 d=s4.indexOf(','); 460 if(s1.indexOf(',',a+1)!=-1||s2.indexOf(',',b+1)!=-1||s3.indexOf(',',c+1)!=-1||s4.indexOf(',',d+1)!=-1){ 461 return 2; 462 } 463 } 464 return 0; 465 } 466 public double max(double x,double y){ 467 if(x>=y) 468 return x; 469 else 470 return y; 471 } 472 public double min(double x,double y){ 473 if(x<=y) 474 return x; 475 else 476 return y; 477 } 478 } 479 class Triangle3 { 480 Line l=new Line(); 481 public void pointNos(String s){ 482 if(judgeInput(s)==0){ 483 String[] ss=s.split(" "); 484 String[] s1 = ss[0].split(","); 485 String[] s2 = ss[1].split(","); 486 String[] s3 = ss[2].split(","); 487 String[] s4 = ss[3].split(","); 488 String[] s5 = ss[4].split(","); 489 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1]) && l.judgeDouble(s4[0]) && l.judgeDouble(s4[1])&&l.judgeDouble(s5[0])&&l.judgeDouble(s5[1])) { 490 double x1 = l.getDouble(s1[0]); 491 double y1 = l.getDouble(s1[1]); 492 double x2 = l.getDouble(s2[0]); 493 double y2 = l.getDouble(s2[1]); 494 double x3 = l.getDouble(s3[0]); 495 double y3 = l.getDouble(s3[1]); 496 double x4 = l.getDouble(s4[0]); 497 double y4 = l.getDouble(s4[1]); 498 double x5 = l.getDouble(s5[0]); 499 double y5 = l.getDouble(s5[1]); 500 double S=getS(x3,y3,x4,y4,x5,y5); 501 double k1=0, k2=0, k3=0, b1 = 0, b2=0, b3=0,k=0,b=0; 502 if(x1==x2&&y1==y2){ 503 System.out.print("points coincide"); 504 System.exit(0); 505 } 506 if(x1!=x2){ 507 k = (y2 - y1) / (x2 - x1); 508 b = y2 - k * x2;} 509 if (((x3 == x4) && (y3 == y4)) || ((x3 == x5) && (y3 == y5)) || ((x4 == x5) && (y4 == y5))){ 510 System.out.print("data error");System.exit(0);} 511 else if (x3 == x4 && x4 == x5){ 512 System.out.print("data error");System.exit(0);} 513 else if (y3 == y4 && y4 == y5){ 514 System.out.print("data error");System.exit(0);} 515 if (x3 == x4) { 516 k1 = 0; 517 b1 = x3; 518 } else if (x3 == x5) { 519 k2 = 0; 520 b2 = x3; 521 } else if (x4 == x5) { 522 k3 = 0; 523 b3 = x4; 524 } else { 525 k1 = (y4 - y3) / (x4 - x3); 526 k2 = (y5 - y3) / (x5 - x3); 527 k3 = (y5 - y4) / (x5 - x4); 528 b1 = y3 - k1 * x3; 529 b2 = y3 - k2 * x3; 530 b3 = y4 - k3 * x4; 531 } 532 double x_1=(b1-b)/(k-k1); 533 double y_1=k*x_1+b; 534 double x_2=(b2-b)/(b-b2); 535 double y_2=k*x_2+b; 536 double x_3=(b3-b)/(b-b3); 537 double y_3=k*x_3+b; 538 if ((y_1 <= max(y3, y4) && y_1 >= min(y3, y4)) && (y_2 <= max(y3, y5) && y_2 >= min(y3, y5)) && !((y_3 <= max(y4, y5) && y_3 >= min(y4, y5)))) { 539 if(y_1==y_2) 540 System.out.print("1"); 541 else { 542 double S2=getS(x3,y3,x_1,y_1,x_2,y_2); 543 double S3=S-S2; 544 String S_2=String.format("%.6lf",S2); 545 S_2=new BigDecimal(S_2).stripTrailingZeros().toPlainString(); 546 S2=Double.parseDouble(S_2); 547 String S_3=String.format("%.6lf",S3); 548 S_3=new BigDecimal(S_3).stripTrailingZeros().toPlainString(); 549 S3=Double.parseDouble(S_3); 550 System.out.print("2 "+max(S3,S2)+" "+min(S3,S2)); 551 } 552 } 553 else if((y_1 <= max(y3, y4) && y_1 >= min(y3, y4)) && !(y_2 <= max(y3, y5) && y_2 >= min(y3, y5)) && ((y_3 <= max(y4, y5) && y_3 >= min(y4, y5)))){ 554 if(y_1==y_2) 555 System.out.print("1"); 556 else { 557 double S2=getS(x4,y4,x_1,y_1,x_2,y_2); 558 double S3=S-S2; 559 String S_2=String.format("%.6lf",S2); 560 S_2=new BigDecimal(S_2).stripTrailingZeros().toPlainString(); 561 S2=Double.parseDouble(S_2); 562 String S_3=String.format("%.6lf",S3); 563 S_3=new BigDecimal(S_3).stripTrailingZeros().toPlainString(); 564 S3=Double.parseDouble(S_3); 565 System.out.print("2 "+max(S3,S2)+" "+min(S3,S2)); 566 } 567 } 568 else if((y_1 <= max(y3, y4) && y_1 >= min(y3, y4)) && (y_2 <= max(y3, y5) && y_2 >= min(y3, y5)) && !((y_3 <= max(y4, y5) && y_3 >= min(y4, y5)))){ 569 if(y_1==y_2) 570 System.out.print("1"); 571 else { 572 double S2=getS(x5,y5,x_1,y_1,x_2,y_2); 573 double S3=S-S2; 574 String S_2=String.format("%.6lf",S2); 575 S_2=new BigDecimal(S_2).stripTrailingZeros().toPlainString(); 576 S2=Double.parseDouble(S_2); 577 String S_3=String.format("%.6lf",S3); 578 S_3=new BigDecimal(S_3).stripTrailingZeros().toPlainString(); 579 S3=Double.parseDouble(S_3); 580 System.out.print("2 "+max(S3,S2)+" "+min(S3,S2)); 581 } 582 } 583 else if((y_1 <= max(y3, y4) && y_1 >= min(y3, y4)) && !(y_2 <= max(y3, y5) && y_2 >= min(y3, y5)) && !((y_3 <= max(y4, y5) && y_3 >= min(y4, y5)))) 584 System.out.print("1"); 585 else if(!(y_1 <= max(y3, y4) && y_1 >= min(y3, y4)) && !(y_2 <= max(y3, y5) && y_2 >= min(y3, y5)) && ((y_3 <= max(y4, y5) && y_3 >= min(y4, y5)))) 586 System.out.print("1"); 587 else if(!(y_1 <= max(y3, y4) && y_1 >= min(y3, y4)) && (y_2 <= max(y3, y5) && y_2 >= min(y3, y5)) && !((y_3 <= max(y4, y5) && y_3 >= min(y4, y5)))) 588 System.out.print("1"); 589 else if(!(y_1 <= max(y3, y4) && y_1 >= min(y3, y4)) && !(y_2 <= max(y3, y5) && y_2 >= min(y3, y5)) && !((y_3 <= max(y4, y5) && y_3 >= min(y4, y5)))) 590 System.out.print("0"); 591 592 } 593 else 594 System.out.println("Wrong Format"); 595 } 596 else if (judgeInput(s) == 1) 597 System.out.println("wrong number of points"); 598 else 599 System.out.println("Wrong Format"); 600 } 601 public int judgeInput(String s){ 602 int length=s.length(); 603 int a=0,b=0,c=0,d=0,e=0; 604 String s1,s2,s3,s4,ss,sss,ssss,sssss; 605 a=s.indexOf(' '); 606 if(a==-1){ 607 if(s.indexOf(",")!=-1){ 608 ss=s.substring(0,s.indexOf(",")); 609 sss=s.substring(s.indexOf(",")+1); 610 if(l.judgeDouble(ss)&&l.judgeDouble(sss)) 611 return 1; 612 else return 2;} 613 else return 2; 614 } 615 s1=s.substring(0,a); 616 ss=s.substring(a+1); 617 b=s.indexOf(" ",a+1); 618 if(b==-1){ 619 if(ss.indexOf(",")!=-1){ 620 ssss=ss.substring(0,ss.indexOf(",")); 621 sss=ss.substring(ss.indexOf(",")+1); 622 if(l.judgeDouble(ssss)&&l.judgeDouble(sss)) 623 return 1; 624 else return 2;} 625 else return 2; 626 } 627 s2=s.substring(a+1,b); 628 sss=s.substring(b+1); 629 c=s.indexOf(" ",b+1); 630 if(c==-1){ 631 if(sss.indexOf(",")!=-1){ 632 ssss=sss.substring(0,sss.indexOf(",")); 633 sssss=sss.substring(sss.indexOf(",")+1); 634 if(l.judgeDouble(ssss)&&l.judgeDouble(sssss)) 635 return 1; 636 else return 2;} 637 else return 2; 638 } 639 s3=s.substring(b+1,c); 640 ssss=s.substring(c+1); 641 d=s.indexOf(" ",c+1); 642 if(d==-1){ 643 if(ssss.indexOf(",")!=-1){ 644 sssss=ssss.substring(0,ssss.indexOf(",")); 645 String ssssss=ssss.substring(ssss.indexOf(",")+1); 646 if(l.judgeDouble(sssss)&&l.judgeDouble(ssssss)) 647 return 1; 648 else return 2;} 649 else return 2; 650 } 651 s4=s.substring(c+1,d); 652 String s5=s.substring(d+1); 653 int length2=s2.length(); 654 int length3=s3.length(); 655 int length4=s4.length(); 656 int length5=s5.length(); 657 for(int i=0;i<length2;i++){ 658 if(s2.charAt(i)==' '){ 659 return 1; 660 } 661 } 662 for(int i=0;i<length3;i++){ 663 if(s3.charAt(i)==' '){ 664 return 1; 665 } 666 } 667 for(int i=0;i<length4;i++){ 668 if(s4.charAt(i)==' '){ 669 return 1; 670 } 671 } 672 for(int i=0;i<length5;i++){ 673 if(s5.charAt(i)==' '){ 674 return 1; 675 } 676 } 677 if(s1.indexOf(',')==-1||s2.indexOf(',')==-1||s3.indexOf(',')==-1||s4.indexOf(',')==-1||s5.indexOf(',')==-1){ 678 return 2; 679 } 680 else { 681 a=s1.indexOf(','); 682 b=s2.indexOf(','); 683 c=s3.indexOf(','); 684 d=s4.indexOf(','); 685 e=s5.indexOf(','); 686 if(s1.indexOf(',',a+1)!=-1||s2.indexOf(',',b+1)!=-1||s3.indexOf(',',c+1)!=-1||s4.indexOf(',',d+1)!=-1||s5.indexOf(',',e+1)!=-1){ 687 return 2; 688 } 689 } 690 return 0; 691 } 692 public double max(double x,double y){ 693 if(x>=y) 694 return x; 695 else 696 return y; 697 } 698 public double min(double x,double y){ 699 if(x<=y) 700 return x; 701 else 702 return y; 703 } 704 public double getS(double x1,double y1,double x2,double y2,double x3,double y3){ 705 double l1=Math.sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1)); 706 double l2=Math.sqrt((y3-y1)*(y3-y1)+(x3-x1)*(x3-x1)); 707 double l3=Math.sqrt((y2-y3)*(y2-y3)+(x2-x3)*(x2-x3)); 708 double p=l1+l2+l3; 709 return Math.sqrt(p*(p-l1)*(p-l2)*(p-l3)); 710 } 711 }
踩坑心得:从这开始就有许多种情况,导致脑子不太好的我选择了摆烂,这些代码在我眼中也是垃圾代码,逻辑什么的毫无规则,
算法错误明显,和老师发出来的类图相比较就是有着天壤之别
https://images.ptausercontent.com/a0811a0d-75e7-40a0-8bc4-67df62b2d9f1.pdf
以上时作业详细的要求。
1 import java.util.HashSet; 2 import java.util.Scanner; 3 import java.math.BigDecimal; 4 import java.util.Set; 5 public class Main { 6 public static void main(String[] args) { 7 Triangle2 t2 = new Triangle2(); 8 Triangle3 t3 = new Triangle3(); 9 Scanner input = new Scanner(System.in); 10 String s; 11 s = input.nextLine(); 12 String []ss=s.split(":"); 13 switch (ss[0]){ 14 case "1":t2.judge1(ss[1]); 15 break; 16 case "2":t2.judge2(ss[1]); 17 break; 18 case "3":t2.judge3(ss[1]); 19 break; 20 case"4":System.out.print("not a quadrilateral or triangle"); 21 break; 22 case "5":t3.pointNos(ss[1]); 23 break; 24 default:System.out.println("Wrong Format"); 25 } 26 } 27 } 28 class Line{ 29 public double getDouble(String s){ 30 if(s.charAt(0)>='0'&&s.charAt(0)<='9'){ 31 return Double.parseDouble(s); 32 } 33 else if(s.charAt(0)=='+'&&(s.charAt(1)>='0'&&s.charAt(1)<='9')){ 34 String[] a= s.split("\\+"); 35 return Double.parseDouble(a[1]); 36 } 37 else if(s.charAt(0)=='-'&&(s.charAt(1)>='0'&&s.charAt(1)<='9')){ 38 String[] a=s.split("-"); 39 return -Double.parseDouble(a[1]); 40 } 41 return 0; 42 } 43 public boolean judgeDouble(String s) { 44 int len=s.length(); 45 for(int i=0;i<len;i++){ 46 if((s.charAt(i)>'9'||s.charAt(i)<'0')&&s.charAt(i)!='+'&&s.charAt(i)!='-'&&s.charAt(i)!='.') 47 return false; 48 } 49 if ((s.charAt(0) == '+' || s.charAt(0) == '-') && (s.charAt(1) < '0' || s.charAt(1) > '9')) { 50 return false; 51 } else if ((s.charAt(0) < '0' || s.charAt(0) > '9') && s.charAt(0) != '+' && s.charAt(0) != '-') { 52 return false; 53 } 54 int a=s.indexOf('.'); 55 if(s.indexOf('.',a+1)!=-1) 56 return false; 57 int l=s.length(); 58 if(a!=-1){ 59 if(s.charAt(a-1)<'0'||s.charAt(a-1)>'9'){ 60 return false; 61 } 62 if(a==l-1){ 63 return false; 64 } 65 } 66 if(s.charAt(0)=='0'){ 67 if(l==1){ 68 return true; 69 } 70 else { 71 if(s.charAt(1)!='.'){ 72 return false; 73 } 74 } 75 } 76 return true; 77 } 78 } 79 80 class Triangle2 { 81 Line l = new Line(); 82 public void judge1(String s){ 83 if(judgeInput(s)==0){ 84 String[] ss=s.split(" "); 85 String[] s1 = ss[0].split(","); 86 String[] s2 = ss[1].split(","); 87 String[] s3 = ss[2].split(","); 88 String[] s4 = ss[3].split(","); 89 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1]) && l.judgeDouble(s4[0]) && l.judgeDouble(s4[1])) { 90 double x1 = l.getDouble(s1[0]); 91 double y1 = l.getDouble(s1[1]); 92 double x2 = l.getDouble(s2[0]); 93 double y2 = l.getDouble(s2[1]); 94 double x3 = l.getDouble(s3[0]); 95 double y3 = l.getDouble(s3[1]); 96 double x4 = l.getDouble(s4[0]); 97 double y4 = l.getDouble(s4[1]); 98 if(((y4==y3)&&(x4==x3))||((y4==y2)&&(x4==x2))||((y4==y1)&&(x4==x1))||((y2==y3)&&(x2==x3))||((y1==y3)&&(x1==x3))||((y1==y2)&&(x1==x2))) 99 System.out.println("points coincide"); 100 else { 101 if ((y4 - y3) * (x4 - x2) == (y4 - y2) * (x4 - x3)) 102 System.out.print("false"); 103 else if ((y4 - y3) * (x4 - x1) == (y4 - y1) * (x4 - x3)) 104 System.out.print("false"); 105 else if ((y4 - y2) * (x4 - x1) == (y4 - y1) * (x4 - x2)) 106 System.out.print("false"); 107 else if ((y3 - y2) * (x3 - x1) == (y3 - y1) * (x3 - x2)) 108 System.out.print("false"); 109 else { 110 System.out.print("true"); 111 if(((y2-y1)/(x2-x1)==(y4-y3)/(x4-x3))&&((y4-y1)/(x4-x1)==(y3-y2)/(x3-x2))) 112 System.out.print(" true"); 113 else if(((y3-y1)/(x4-x2)==(y3-y1)/(x4-x2))&&((y4-y1)/(x4-x1)==(y3-y2)/(x3-x2))) 114 System.out.print(" true"); 115 else System.out.print(" false"); 116 } 117 } 118 119 } 120 else 121 System.out.println("Wrong Format"); 122 } 123 else if (judgeInput(s) == 1) 124 System.out.println("wrong number of points"); 125 else 126 System.out.println("Wrong Format"); 127 } 128 public void judge2(String s){ 129 if(judgeInput(s)==0){ 130 String[] ss=s.split(" "); 131 String[] s1 = ss[0].split(","); 132 String[] s2 = ss[1].split(","); 133 String[] s3 = ss[2].split(","); 134 String[] s4 = ss[3].split(","); 135 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1]) && l.judgeDouble(s4[0]) && l.judgeDouble(s4[1])) { 136 double x1 = l.getDouble(s1[0]); 137 double y1 = l.getDouble(s1[1]); 138 double x2 = l.getDouble(s2[0]); 139 double y2 = l.getDouble(s2[1]); 140 double x3 = l.getDouble(s3[0]); 141 double y3 = l.getDouble(s3[1]); 142 double x4 = l.getDouble(s4[0]); 143 double y4 = l.getDouble(s4[1]); 144 if(((y4==y3)&&(x4==x3))||((y4==y2)&&(x4==x2))||((y4==y1)&&(x4==x1))||((y2==y3)&&(x2==x3))||((y1==y3)&&(x1==x3))||((y1==y2)&&(x1==x2))) 145 System.out.println("not a quadrilateral"); 146 else { 147 if ((y4 - y3) * (x4 - x2) == (y4 - y2) * (x4 - x3)) 148 System.out.print("not a quadrilateral"); 149 else if ((y4 - y3) * (x4 - x1) == (y4 - y1) * (x4 - x3)) 150 System.out.print("not a quadrilateral"); 151 else if ((y4 - y2) * (x4 - x1) == (y4 - y1) * (x4 - x2)) 152 System.out.print("not a quadrilateral"); 153 else if ((y3 - y2) * (x3 - x1) == (y3 - y1) * (x3 - x2)) 154 System.out.print("not a quadrilateral"); 155 else { 156 double l1 = Math.sqrt((y2-y1)*(y2-y1)+(x2-x1)*(x2-x1)); 157 double l2 = Math.sqrt((y3-y1)*(y3-y1)+(x3-x1)*(x3-x1)); 158 double l3 = Math.sqrt((y4-y1)*(y4-y1)+(x4-x1)*(x4-x1)); 159 double l4 = Math.sqrt((y3-y2)*(y3-y2)+(x3-x2)*(x3-x2)); 160 double l5 = Math.sqrt((y4-y3)*(y4-y3)+(x4-x3)*(x4-x3)); 161 double l6 = Math.sqrt((y4-y2)*(y4-y2)+(x4-x2)*(x4-x2)); 162 Set<Double> set = new HashSet<>(); 163 set.add(l1); 164 set.add(l2); 165 set.add(l3); 166 set.add(l4); 167 set.add(l5); 168 set.add(l6); 169 if(set.size()==2){ 170 System.out.print("true true true"); 171 } 172 else if(set.size()==3){ 173 if(l1==l2&&l2==l3) 174 System.out.print("true false false"); 175 else if(l1==l2&&l1==l4) 176 System.out.print("true false false"); 177 else if(l1==l2&&l1==l5) 178 System.out.print("true false false"); 179 else if(l1==l2&&l1==l6) 180 System.out.print("true false false"); 181 else if(l2==l3&&l2==l4) 182 System.out.print("true false false"); 183 else if(l2==l3&&l2==l5) 184 System.out.print("true false false"); 185 else if(l2==l3&&l2==l6) 186 System.out.print("true false false"); 187 else if(l3==l4&&l3==l5) 188 System.out.print("true false false"); 189 else if(l3==l4&&l3==l6) 190 System.out.print("true false false"); 191 else if(l4==l5&&l5==l6) 192 System.out.print("true false false"); 193 else 194 System.out.print("false true false"); 195 } 196 else System.out.print("false false false"); 197 } 198 } 199 200 } 201 else 202 System.out.println("Wrong Format"); 203 } 204 else if (judgeInput(s) == 1) 205 System.out.println("wrong number of points"); 206 else 207 System.out.println("Wrong Format"); 208 } 209 public void judge3(String s){ 210 if(judgeInput(s)==0){ 211 String[] ss=s.split(" "); 212 String[] s1 = ss[0].split(","); 213 String[] s2 = ss[1].split(","); 214 String[] s3 = ss[2].split(","); 215 String[] s4 = ss[3].split(","); 216 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1]) && l.judgeDouble(s4[0]) && l.judgeDouble(s4[1])) { 217 double x1 = l.getDouble(s1[0]); 218 double y1 = l.getDouble(s1[1]); 219 double x2 = l.getDouble(s2[0]); 220 double y2 = l.getDouble(s2[1]); 221 double x3 = l.getDouble(s3[0]); 222 double y3 = l.getDouble(s3[1]); 223 double x4 = l.getDouble(s4[0]); 224 double y4 = l.getDouble(s4[1]); 225 if(((y4==y3)&&(x4==x3))||((y4==y2)&&(x4==x2))||((y4==y1)&&(x4==x1))||((y2==y3)&&(x2==x3))||((y1==y3)&&(x1==x3))||((y1==y2)&&(x1==x2))) 226 System.out.println("points coincide"); 227 else { 228 if ((y4 - y3) * (x4 - x2) == (y4 - y2) * (x4 - x3)) 229 System.out.print("not a quadrilateral"); 230 else if ((y4 - y3) * (x4 - x1) == (y4 - y1) * (x4 - x3)) 231 System.out.print("not a quadrilateral"); 232 else if ((y4 - y2) * (x4 - x1) == (y4 - y1) * (x4 - x2)) 233 System.out.print("not a quadrilateral"); 234 else if ((y3 - y2) * (x3 - x1) == (y3 - y1) * (x3 - x2)) 235 System.out.print("not a quadrilateral"); 236 else { 237 238 double z1 = ((x2 - x1) * (y4 - y1) - (x4 - x1) * (y2 - y1)); 239 double z2 = ((x4 - x1) * (y3 - y1) - (x3 - x1) * (y4 - y1)); 240 double z3 = ((x4 - x2) * (y3 - y2) - (x3 - x2) * (y4 - y2)); 241 double z4 = ((x3 - x2) * (y1 - y2) - (x1 - x2) * (y3 - y2)); 242 if((z1 * z2 * z3 * z4 > 0)) 243 System.out.print("true "); 244 else System.out.print("false "); 245 double k1 = (y1-y3)/(x1-x3); 246 double k2 = (y2-y4)/(x2-x4); 247 double b1 = y1-k1*x1; 248 double b2 = y2-k2*x2; 249 double l1 = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); 250 double l2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)); 251 double l3 = Math.sqrt((x4-x3)*(x4-x3)+(y4-y3)*(y4-y3)); 252 double l4 = Math.sqrt((x4-x1)*(x4-x1)+(y4-y1)*(y4-y1)); 253 double l5 = Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)); 254 double c = l1+l2+l3+l4; 255 double p1 = (l1+l2+l5)/2; 256 double S1 = Math.sqrt(p1*(p1-l1)*(p1-l2)*(p1-l5)); 257 double p2 = (l3+l4+l5)/2; 258 double S2 = Math.sqrt(p2*(p2-l3)*(p2-l4)*(p2-l5)); 259 double S = S1+S2; 260 String S_=String.format("%.3f",S); 261 S_=new BigDecimal(S_).stripTrailingZeros().toPlainString(); 262 S=Double.parseDouble(S_); 263 String c_ = String.format("%.3f",c); 264 c_ = new BigDecimal(c_).stripTrailingZeros().toPlainString(); 265 c = Double.parseDouble(c_); 266 System.out.print(c+" "+S); 267 } 268 } 269 270 } 271 else 272 System.out.println("Wrong Format"); 273 } 274 else if (judgeInput(s) == 1) 275 System.out.println("wrong number of points"); 276 else 277 System.out.println("Wrong Format"); 278 } 279 public int judgeInput(String s){ 280 int length=s.length(); 281 int a=0,b=0,c=0,d=0; 282 String s1,s2,s3,s4,ss,sss; 283 a=s.indexOf(' '); 284 if(a==-1){ 285 if(s.indexOf(",")!=-1){ 286 ss=s.substring(0,s.indexOf(",")); 287 sss=s.substring(s.indexOf(",")+1); 288 if(l.judgeDouble(ss)&&l.judgeDouble(sss)) 289 return 1; 290 else return 2;} 291 else return 2; 292 } 293 s1=s.substring(0,a); 294 ss=s.substring(a+1); 295 b=s.indexOf(" ",a+1); 296 if(b==-1){ 297 if(ss.indexOf(",")!=-1){ 298 String ssss=ss.substring(0,ss.indexOf(",")); 299 sss=ss.substring(ss.indexOf(",")+1); 300 if(l.judgeDouble(ssss)&&l.judgeDouble(sss)) 301 return 1; 302 else return 2;} 303 else return 2; 304 } 305 s2=s.substring(a+1,b); 306 sss=s.substring(b+1); 307 c=s.indexOf(" ",b+1); 308 if(c==-1){ 309 if(sss.indexOf(",")!=-1){ 310 String ssss=sss.substring(0,sss.indexOf(",")); 311 String sssss=sss.substring(sss.indexOf(",")+1); 312 if(l.judgeDouble(ssss)&&l.judgeDouble(sssss)) 313 return 1; 314 else return 2;} 315 else return 2; 316 } 317 s3=s.substring(b+1,c); 318 s4=s.substring(c+1); 319 int length2=s2.length(); 320 int length3=s3.length(); 321 int length4=s4.length(); 322 for(int i=0;i<length2;i++){ 323 if(s2.charAt(i)==' '){ 324 return 1; 325 } 326 } 327 for(int i=0;i<length3;i++){ 328 if(s3.charAt(i)==' '){ 329 return 1; 330 } 331 } 332 for(int i=0;i<length4;i++){ 333 if(s4.charAt(i)==' '){ 334 return 1; 335 } 336 } 337 if(s1.indexOf(',')==-1||s2.indexOf(',')==-1||s3.indexOf(',')==-1||s4.indexOf(',')==-1){ 338 return 2; 339 } 340 else { 341 a=s1.indexOf(','); 342 b=s2.indexOf(','); 343 c=s3.indexOf(','); 344 d=s4.indexOf(','); 345 if(s1.indexOf(',',a+1)!=-1||s2.indexOf(',',b+1)!=-1||s3.indexOf(',',c+1)!=-1||s4.indexOf(',',d+1)!=-1){ 346 return 2; 347 } 348 } 349 return 0; 350 } 351 public double max(double x,double y){ 352 if(x>=y) 353 return x; 354 else 355 return y; 356 } 357 public double min(double x,double y){ 358 if(x<=y) 359 return x; 360 else 361 return y; 362 } 363 } 364 class Triangle3 { 365 Line l=new Line(); 366 public void pointNos(String s){ 367 if(judgeInput(s)==0){ 368 String[] ss=s.split(" "); 369 String[] s1 = ss[0].split(","); 370 String[] s2 = ss[1].split(","); 371 String[] s3 = ss[2].split(","); 372 String[] s4 = ss[3].split(","); 373 String[] s5 = ss[4].split(","); 374 if (l.judgeDouble(s1[0]) && l.judgeDouble(s1[1]) && l.judgeDouble(s2[0]) && l.judgeDouble(s2[1]) && l.judgeDouble(s3[0]) && l.judgeDouble(s3[1]) && l.judgeDouble(s4[0]) && l.judgeDouble(s4[1])&&l.judgeDouble(s5[0])&&l.judgeDouble(s5[1])) { 375 double x1 = l.getDouble(s2[0]); 376 double y1 = l.getDouble(s2[1]); 377 double x2 = l.getDouble(s3[0]); 378 double y2 = l.getDouble(s3[1]); 379 double x3 = l.getDouble(s4[0]); 380 double y3 = l.getDouble(s4[1]); 381 double x4 = l.getDouble(s5[0]); 382 double y4 = l.getDouble(s5[1]); 383 double x5 = l.getDouble(s1[0]); 384 double y5 = l.getDouble(s1[1]); 385 if (((y4 == y3) && (x4 == x3)) || ((y4 == y2) && (x4 == x2)) || ((y4 == y1) && (x4 == x1)) || ((y2 == y3) && (x2 == x3)) || ((y1 == y3) && (x1 == x3)) || ((y1 == y2) && (x1 == x2))) 386 System.out.println("points coincide"); 387 else { 388 if ((y4 - y3) * (x4 - x2) == (y4 - y2) * (x4 - x3)&&(y4 - y3) * (x4 - x1) == (y4 - y1) * (x4 - x3)) 389 System.out.print("not a quadrilateral or triangle"); 390 else if ((y4 - y3) * (x4 - x2) == (y4 - y2) * (x4 - x3)) 391 System.out.print("in the triangle"); 392 else if ((y4 - y3) * (x4 - x1) == (y4 - y1) * (x4 - x3)) 393 System.out.print("in the triangle"); 394 else if ((y4 - y2) * (x4- x1) == (y4 - y1) * (x4 - x2)) 395 System.out.print("in the triangle"); 396 else if ((y3 - y2) * (x3 - x1) == (y3 - y1) * (x3 - x2)) 397 System.out.print("in the triangle"); 398 else System.out.print("in the quadrilateral"); 399 400 } 401 } 402 else 403 System.out.println("Wrong Format"); 404 } 405 else if (judgeInput(s) == 1) 406 System.out.println("wrong number of points"); 407 else 408 System.out.println("Wrong Format"); 409 } 410 public int judgeInput(String s){ 411 int length=s.length(); 412 int a=0,b=0,c=0,d=0,e=0; 413 String s1,s2,s3,s4,ss,sss,ssss,sssss; 414 a=s.indexOf(' '); 415 if(a==-1){ 416 if(s.indexOf(",")!=-1){ 417 ss=s.substring(0,s.indexOf(",")); 418 sss=s.substring(s.indexOf(",")+1); 419 if(l.judgeDouble(ss)&&l.judgeDouble(sss)) 420 return 1; 421 else return 2;} 422 else return 2; 423 } 424 s1=s.substring(0,a); 425 ss=s.substring(a+1); 426 b=s.indexOf(" ",a+1); 427 if(b==-1){ 428 if(ss.indexOf(",")!=-1){ 429 ssss=ss.substring(0,ss.indexOf(",")); 430 sss=ss.substring(ss.indexOf(",")+1); 431 if(l.judgeDouble(ssss)&&l.judgeDouble(sss)) 432 return 1; 433 else return 2;} 434 else return 2; 435 } 436 s2=s.substring(a+1,b); 437 sss=s.substring(b+1); 438 c=s.indexOf(" ",b+1); 439 if(c==-1){ 440 if(sss.indexOf(",")!=-1){ 441 ssss=sss.substring(0,sss.indexOf(",")); 442 sssss=sss.substring(sss.indexOf(",")+1); 443 if(l.judgeDouble(ssss)&&l.judgeDouble(sssss)) 444 return 1; 445 else return 2;} 446 else return 2; 447 } 448 s3=s.substring(b+1,c); 449 ssss=s.substring(c+1); 450 d=s.indexOf(" ",c+1); 451 if(d==-1){ 452 if(ssss.indexOf(",")!=-1){ 453 sssss=ssss.substring(0,ssss.indexOf(",")); 454 String ssssss=ssss.substring(ssss.indexOf(",")+1); 455 if(l.judgeDouble(sssss)&&l.judgeDouble(ssssss)) 456 return 1; 457 else return 2;} 458 else return 2; 459 } 460 s4=s.substring(c+1,d); 461 String s5=s.substring(d+1); 462 int length2=s2.length(); 463 int length3=s3.length(); 464 int length4=s4.length(); 465 int length5=s5.length(); 466 for(int i=0;i<length2;i++){ 467 if(s2.charAt(i)==' '){ 468 return 1; 469 } 470 } 471 for(int i=0;i<length3;i++){ 472 if(s3.charAt(i)==' '){ 473 return 1; 474 } 475 } 476 for(int i=0;i<length4;i++){ 477 if(s4.charAt(i)==' '){ 478 return 1; 479 } 480 } 481 for(int i=0;i<length5;i++){ 482 if(s5.charAt(i)==' '){ 483 return 1; 484 } 485 } 486 if(s1.indexOf(',')==-1||s2.indexOf(',')==-1||s3.indexOf(',')==-1||s4.indexOf(',')==-1||s5.indexOf(',')==-1){ 487 return 2; 488 } 489 else { 490 a=s1.indexOf(','); 491 b=s2.indexOf(','); 492 c=s3.indexOf(','); 493 d=s4.indexOf(','); 494 e=s5.indexOf(','); 495 if(s1.indexOf(',',a+1)!=-1||s2.indexOf(',',b+1)!=-1||s3.indexOf(',',c+1)!=-1||s4.indexOf(',',d+1)!=-1||s5.indexOf(',',e+1)!=-1){ 496 return 2; 497 } 498 } 499 return 0; 500 } 501 public double max(double x,double y){ 502 if(x>=y) 503 return x; 504 else 505 return y; 506 } 507 public double min(double x,double y){ 508 if(x<=y) 509 return x; 510 else 511 return y; 512 } 513 }
这是最近的一次迭代作业(希望是最后一次),我也是照常摆烂。
以上作业的踩坑心得:
通过里面我还是学到许多多西的,例如正则表达式、怎么不补空位来保留几位小数、以及关于图形类设计的方法。
以上代码都是垃圾代码,应作业要求才拿出来的。
二、超星作业的链表
由于在上学期c语言学的不够特别透彻,用Java来写链表还是有些难度的。
源码:
由于这个作业是迭代作业,我电脑里只有之后双链表的代码
1 public interface DoubleLinkedListImpl<E> { 2 3 /** 4 5 * 判断链表是否为空 6 7 * @return 8 9 */ 10 11 public boolean isEmpty(); 12 13 14 15 /** 16 17 * 获取当前链表节点数量 18 19 * @return 节点数 20 21 */ 22 23 public int getSize(); 24 25 26 27 /** 28 29 * 获取链表中第index个位置的节点的data值 30 31 * @param index:节点在链表中的位置 32 33 * @return:返回该节点的data值 34 35 */ 36 37 public E getData(int index); 38 39 40 41 /** 42 43 * 删除链表最后一个节点 44 45 */ 46 47 public void remove(); 48 49 50 51 /** 52 53 *删除链表中第index位置的节点 54 55 * @param index:节点在链表中的位置 56 57 */ 58 59 public void remove(int index); 60 61 62 63 /** 64 65 * 在链表的第index个位置之前插入一个节点,值为theElement,index∈[1,size] 66 67 * @param index:插入节点在链表中的位置 68 69 * @param theElement:新插入节点的data值 70 71 */ 72 73 public void add(int index, E theElement); 74 75 76 77 /** 78 79 * 在链表尾插入节点,插入节点data值为element 80 81 * @param element 82 83 */ 84 85 public void add(E element); 86 87 88 89 /** 90 91 * 输出链表 92 93 */ 94 95 public void printList(); 96 97 98 99 /** 100 101 * 获取第一个节点的data值 102 103 * @return 104 105 */ 106 107 public E getFirst(); 108 109 110 111 /** 112 113 * 获取链表最后一个节点的data值 114 115 * @return 116 117 */ 118 119 public E getLast(); 120 121 } 122 public class DoubleLinkedList<E> implements DoubleLinkedListImpl<E> { 123 private Node<E> head;//头结点 124 private Node<E> curr;//当前节点 125 private Node<E> tail;//尾节点 126 private int size; 127 128 @Override 129 public boolean isEmpty() { 130 131 return this.size==0; 132 } 133 134 @Override 135 public int getSize() { 136 137 return this.size; 138 } 139 140 @Override 141 public E getData(int index) { 142 143 if(index < 1 || index > this.size) { 144 System.out.println("input error"); 145 146 } 147 148 else { 149 curr = head; 150 for (int i = 1; i < index; i++) { 151 curr = curr.getNext(); 152 } 153 154 return curr.getData(); 155 } 156 return null; 157 } 158 159 @Override 160 public void remove() { 161 if (size==0){ 162 System.out.println("input error"); 163 } 164 165 else if(size==1){ 166 head=null; 167 size--; 168 } 169 else { 170 curr = head; 171 for (int i = 1; i < size-1; i++) { 172 curr = curr.getNext(); 173 } 174 tail = curr; 175 size--; 176 } 177 } 178 179 180 @Override 181 public void remove(int index) { 182 183 if (index < 1 || index > this.size) { 184 System.out.println("input error"); 185 186 } else { 187 curr = head; 188 189 if (index == 1) { 190 curr = head.getNext(); 191 head.setNext(curr.getNext()); 192 curr.setPrevious(null); 193 } else { 194 for (int i = 1; i < index - 1; i++) { 195 curr = curr.getNext(); 196 } 197 198 curr.setNext(curr.getNext().getNext()); 199 curr.getNext().getNext().setPrevious(curr); 200 } 201 202 if (index == size) { 203 tail = curr; 204 } 205 206 size--; 207 } 208 } 209 210 @Override 211 public void add(int index, E theElement) { 212 213 if (index < 1 || index > this.size + 1) { 214 System.out.println("input error"); 215 } else { 216 Node<E> newNode = new Node<>(); 217 newNode.setData(theElement); 218 newNode.setNext(null); 219 220 if (size == 0) { 221 head = newNode; 222 tail = newNode; 223 } else if (index == size + 1) { 224 tail.setNext(newNode); 225 newNode.setPrevious(tail); 226 tail = newNode; 227 } else { 228 Node<E> tempNode = head; 229 for (int i = 1; i < index - 1; i++) { 230 tempNode = tempNode.getNext(); 231 } 232 tempNode.getNext().setPrevious(newNode); 233 newNode.setPrevious(tempNode); 234 newNode.setNext(tempNode.getNext()); 235 tempNode.setNext(newNode); 236 } 237 238 size++; 239 } 240 } 241 242 243 @Override 244 public void add(E element) { 245 Node<E> newNode = new Node<>(); 246 newNode.setNext(null); 247 newNode.setData(element); 248 if (size==0){ 249 head = newNode; 250 tail = newNode; 251 } 252 else { 253 tail.setNext(newNode); 254 newNode.setPrevious(tail); 255 tail = newNode; 256 } 257 size++; 258 } 259 260 261 @Override 262 public void printList() { 263 264 curr = head; 265 for(int i = 1; i <= size;i ++) { 266 System.out.print(curr.getData() + " "); 267 curr = curr.getNext(); 268 } 269 270 System.out.println(""); 271 } 272 273 274 @Override 275 public E getFirst() { 276 if (size==0){ 277 return null; 278 } 279 return head.getData(); 280 } 281 282 283 @Override 284 public E getLast() { 285 if (size==0){ 286 return null; 287 } 288 return tail.getData(); 289 } 290 } 291 public class Node<E>{ 292 public E getData() { 293 return data; 294 } 295 296 public void setData(E data) { 297 this.data = data; 298 } 299 300 private E data; 301 private Node<E> next; 302 private Node<E> previous; 303 304 public Node<E> getPrevious() { 305 return previous; 306 } 307 308 public void setPrevious(Node<E> previous) { 309 this.previous = previous; 310 } 311 // public void setO(E o) { 312 // this.o = o; 313 // } 314 315 public Node<E> getNext() { 316 return next; 317 } 318 319 public void setNext(Node<E> next) { 320 this.next = next; 321 } 322 323 } 324 import java.util.Scanner; 325 public class Main{ 326 327 public static void main(String[] args) { 328 DoubleLinkedList<Integer> list = new DoubleLinkedList<>(); 329 Scanner input = new Scanner(System.in); 330 menu(); 331 int a = input.nextInt(); 332 while (true){ 333 switch (a){ 334 case 1:System.out.println("链表长度为:"+list.getSize());break; 335 case 2:int index = input.nextInt(); 336 System.out.println("链表第"+index+"位为:"+list.getData(index));break; 337 case 3:index = input.nextInt(); 338 list.remove(index);break; 339 case 4:int value = input.nextInt(); 340 list.add(value);break; 341 case 5:value = input.nextInt(); 342 index = input.nextInt(); 343 list.add(index,value); 344 break; 345 case 6:list.printList();break; 346 case 7:System.out.println("链表第一位为:"+list.getFirst());break; 347 case 8:System.out.println("链表最后一位为:"+list.getLast());break; 348 case 9:list.remove();break; 349 case 10:System.exit(0);break; 350 default:System.out 351 .println("input error");break; 352 353 } 354 menu(); 355 a= input.nextInt(); 356 } 357 } 358 public static void menu(){ 359 System.out.println("1.链表长度"); 360 System.out.println("2.查询指定节点"); 361 System.out.println("3.删除指定节点"); 362 System.out.println("4.尾部增加节点"); 363 System.out.println("5.指定位置增加节点(先输入值,后输入节点位置)"); 364 System.out.println("6.打印链表"); 365 System.out.println("7.获取第一个的值"); 366 System.out.println("8.获取最后一个的值"); 367 System.out.println("9.删除最后一个"); 368 System.out.println("10.退出"); 369 } 370 371 }
踩坑心得:
我写完代码之后,感觉这个相比c语言还是用处更大而且更加的好写一点(虽然java中已经有这方面的内容了)。
里面对于泛型的使用是比较好的一点,代码也没有多好,也就将就看下的程度。
三、期中考试
期中考试有三道题目,三道迭代作业,感觉相比平时的作业相比来说更简单。
1 import java.util.Scanner; 2 public class Main { 3 public static void main(String[] args){ 4 Scanner input = new Scanner(System.in); 5 double x1 =0; 6 double x2 =0; 7 double y1=0; 8 double y2=0; 9 x1= input.nextDouble(); 10 y1 = input.nextDouble(); 11 x2= input.nextDouble(); 12 y2= input.nextDouble(); 13 String color = input.next(); 14 Point point1 = new Point(x1,y1); 15 Point point2 = new Point(x2,y2); 16 Line line = new Line(color,point1,point2); 17 if(x1<=0||x1>200||x2<=0||x2>200||y1<=0||y1>200||y2<=0||y2>200) 18 System.out.println("Wrong Format"); 19 else 20 line.display(); 21 } 22 } 23 class Point { 24 25 public Point(double x, double y) { 26 this.x = x; 27 this.y = y; 28 } 29 30 public Point() { 31 } 32 33 private double x; 34 private double y; 35 36 public double getX() { 37 return x; 38 } 39 40 public void setX(double x) { 41 this.x = x; 42 } 43 44 public double getY() { 45 return y; 46 } 47 48 public void setY(double y) { 49 this.y = y; 50 } 51 } 52 class Line { 53 54 private String color; 55 56 public Line(){ 57 58 } 59 60 public Line(String color, Point point1, Point point2) { 61 this.color = color; 62 this.point1 = point1; 63 this.point2 = point2; 64 } 65 66 Point point1; 67 Point point2; 68 69 public String getColor() { 70 return color; 71 } 72 73 public void setColor(String color) { 74 this.color = color; 75 } 76 77 public Point getPoint1() { 78 return point1; 79 } 80 81 public void setPoint1(Point point1) { 82 this.point1 = point1; 83 } 84 85 public Point getPoint2() { 86 return point2; 87 } 88 89 public void setPoint2(Point point2) { 90 this.point2 = point2; 91 } 92 93 public double getDistance(){ 94 double distance = 0; 95 distance = Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY())); 96 return distance; 97 } 98 99 public void display(){ 100 System.out.println("The line's color is:"+getColor()); 101 System.out.println("The line's begin point's Coordinate is:"); 102 System.out.println("("+String.format("%.2f",point1.getX())+","+String.format("%.2f",point1.getY())+")"); 103 System.out.println("The line's end point's Coordinate is:"); 104 System.out.println("("+String.format("%.2f",point2.getX())+","+String.format("%.2f",point2.getY())+")"); 105 System.out.println("The line's length is:"+String.format("%.2f",getDistance())); 106 107 } 108 }
第一题还是比较简单的。
这是我的类图:
1 import java.util.Scanner; 2 public class Main { 3 public static void main(String[] args){ 4 Scanner input = new Scanner(System.in); 5 double x1 =0; 6 double x2 =0; 7 double y1=0; 8 double y2=0; 9 x1= input.nextDouble(); 10 y1 = input.nextDouble(); 11 x2= input.nextDouble(); 12 y2= input.nextDouble(); 13 String color = input.next(); 14 Element point1 = new Point(x1,y1); 15 Element point2 = new Point(x2,y2); 16 Element line = new Line(color, (Point) point1,(Point) point2); 17 Element plane = new Plane(color); 18 if(x1<=0||x1>200||x2<=0||x2>200||y1<=0||y1>200||y2<=0||y2>200) 19 System.out.println("Wrong Format"); 20 else { 21 point1.display(); 22 point2.display(); 23 line.display(); 24 plane.display(); 25 } 26 } 27 } 28 class Point extends Element{ 29 30 public Point(double x, double y) { 31 this.x = x; 32 this.y = y; 33 } 34 35 public Point() { 36 } 37 38 private double x; 39 private double y; 40 41 public double getX() { 42 return x; 43 } 44 45 public void setX(double x) { 46 this.x = x; 47 } 48 49 public double getY() { 50 return y; 51 } 52 53 public void setY(double y) { 54 this.y = y; 55 } 56 57 @Override 58 public void display(){ 59 System.out.println("("+String.format("%.2f",getX())+","+String.format("%.2f",getY())+")"); 60 } 61 62 } 63 class Line extends Element{ 64 65 private String color; 66 67 public Line(){ 68 69 } 70 71 public Line(String color, Point point1, Point point2) { 72 this.color = color; 73 this.point1 = point1; 74 this.point2 = point2; 75 } 76 77 Point point1 = new Point(); 78 Point point2 = new Point(); 79 80 public String getColor() { 81 return color; 82 } 83 84 public void setColor(String color) { 85 this.color = color; 86 } 87 88 public Point getPoint1() { 89 return point1; 90 } 91 92 public void setPoint1(Point point1) { 93 this.point1 = point1; 94 } 95 96 public Point getPoint2() { 97 return point2; 98 } 99 100 public void setPoint2(Point point2) { 101 this.point2 = point2; 102 } 103 104 public double getDistance(){ 105 double distance = 0; 106 distance = Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY())); 107 return distance; 108 } 109 110 @Override 111 public void display(){ 112 System.out.println("The line's color is:"+getColor()); 113 System.out.println("The line's begin point's Coordinate is:"); 114 System.out.println("("+String.format("%.2f",point1.getX())+","+String.format("%.2f",point1.getY())+")"); 115 System.out.println("The line's end point's Coordinate is:"); 116 System.out.println("("+String.format("%.2f",point2.getX())+","+String.format("%.2f",point2.getY())+")"); 117 System.out.println("The line's length is:"+String.format("%.2f",getDistance())); 118 119 } 120 121 } 122 abstract class Element { 123 public void display(){ 124 125 } 126 127 } 128 class Plane extends Element{ 129 String color; 130 131 public Plane(String color) { 132 this.color = color; 133 } 134 135 public Plane() { 136 } 137 138 public String getColor() { 139 return color; 140 } 141 142 public void setColor(String color) { 143 this.color = color; 144 } 145 146 @Override 147 public void display(){ 148 System.out.println("The Plane's color is:"+getColor()); 149 } 150 }
第二题就是添加一个父类,也没太大难度
这是我的类图
1 import java.util.Scanner; 2 import java.util.ArrayList; 3 public class Main { 4 public static void main(String[] args){ 5 Scanner input = new Scanner(System.in); 6 GeometryObject list = new GeometryObject(); 7 double x1 =0; 8 double x2 =0; 9 double y1=0; 10 double y2=0; 11 String color; 12 int choice = 0; 13 choice = input.nextInt(); 14 while (choice!=0){ 15 switch(choice) { 16 case 1://insert Point object into list 17 x1= input.nextDouble(); 18 y1 = input.nextDouble(); 19 Element point = new Point(x1,y1); 20 if(x1<=0||x1>200||y1<=0||y1>200) 21 System.out.println("Wrong Format"); 22 else { 23 list.add(point);} 24 break; 25 case 2://insert Line object into list 26 x1= input.nextDouble(); 27 y1 = input.nextDouble(); 28 x2= input.nextDouble(); 29 y2= input.nextDouble(); 30 color = input.next(); 31 if(x1<=0||x1>200||x2<=0||x2>200||y1<=0||y1>200||y2<=0||y2>200) 32 System.out.println("Wrong Format"); 33 else { 34 Element point1 = new Point(x1,y1); 35 Element point2 = new Point(x2,y2); 36 Line line = new Line(color,(Point)point1,(Point)point2 ); 37 list.add(line);} 38 break; 39 case 3://insert Plane object into list 40 color = input.next(); 41 Element plane = new Plane(color); 42 list.add(plane); 43 break; 44 case 4://delete index - 1 object from list 45 int index = input.nextInt(); 46 list.remove(index); 47 } 48 choice = input.nextInt(); 49 } 50 for(int i =0;i<list.getList().size();i++){ 51 list.getList().get(i).display(); 52 } 53 } 54 } 55 class Point extends Element{ 56 57 public Point(double x, double y) { 58 this.x = x; 59 this.y = y; 60 } 61 62 public Point() { 63 } 64 65 private double x; 66 private double y; 67 68 public double getX() { 69 return x; 70 } 71 72 public void setX(double x) { 73 this.x = x; 74 } 75 76 public double getY() { 77 return y; 78 } 79 80 public void setY(double y) { 81 this.y = y; 82 } 83 84 @Override 85 public void display(){ 86 System.out.println("("+String.format("%.2f",getX())+","+String.format("%.2f",getY())+")"); 87 } 88 89 } 90 class Line extends Element{ 91 92 private String color; 93 94 public Line(){ 95 96 } 97 98 public Line(String color, Point point1, Point point2) { 99 this.color = color; 100 this.point1 = point1; 101 this.point2 = point2; 102 } 103 104 Point point1; 105 Point point2; 106 107 public String getColor() { 108 return color; 109 } 110 111 public void setColor(String color) { 112 this.color = color; 113 } 114 115 public Point getPoint1() { 116 return point1; 117 } 118 119 public void setPoint1(Point point1) { 120 this.point1 = point1; 121 } 122 123 public Point getPoint2() { 124 return point2; 125 } 126 127 public void setPoint2(Point point2) { 128 this.point2 = point2; 129 } 130 131 public double getDistance(){ 132 double distance = 0; 133 distance = Math.sqrt((point1.getX()-point2.getX())*(point1.getX()-point2.getX())+(point1.getY()-point2.getY())*(point1.getY()-point2.getY())); 134 return distance; 135 } 136 137 @Override 138 public void display(){ 139 System.out.println("The line's color is:"+getColor()); 140 System.out.println("The line's begin point's Coordinate is:"); 141 System.out.println("("+String.format("%.2f",point1.getX())+","+String.format("%.2f",point1.getY())+")"); 142 System.out.println("The line's end point's Coordinate is:"); 143 System.out.println("("+String.format("%.2f",point2.getX())+","+String.format("%.2f",point2.getY())+")"); 144 System.out.println("The line's length is:"+String.format("%.2f",getDistance())); 145 146 } 147 148 } 149 abstract class Element { 150 public void display(){ 151 152 } 153 154 } 155 class Plane extends Element{ 156 String color; 157 158 public Plane(String color) { 159 this.color = color; 160 } 161 162 public Plane() { 163 } 164 165 public String getColor() { 166 return color; 167 } 168 169 public void setColor(String color) { 170 this.color = color; 171 } 172 173 @Override 174 public void display(){ 175 System.out.println("The Plane's color is:"+getColor()); 176 } 177 } 178 class GeometryObject { 179 180 ArrayList<Element> list = new ArrayList<>(); 181 182 public void add(Element element){ 183 if (list.isEmpty()){ 184 list.add(0,element); 185 } 186 else { 187 list.add(list.size(),element); 188 } 189 } 190 191 public void remove(int index){ 192 if(index>list.size()||index<=0){ 193 194 } 195 else { 196 list.remove(index-1); 197 } 198 } 199 200 public ArrayList<Element> getList(){ 201 202 return list; 203 204 } 205 }
一开始我以为这个容器类的add和remove要自己写,后来才发觉出端倪,感觉比较贼的一点就是将链表开始的0变成了1。
这是我的类图
本次作业到此结束,希望自己以后越来越努力。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!