前言:第一次大作业虽然题量较大,但都是一些比较基础的题,做起来还是蛮轻松的,难度不大,都是一些简单的输入和输出;第二次大作业相较于第一次大作业题量较少些,题目比第一次实验难了一些,但是还能接受,在第一次大作业上增加了对输入和输出进行判断和处理,时间给的也较为充分,为我们合理考虑了时间;第三次大作业第一题和第二题较为简单,第三题相对来说难一点,我在做第一题的时候有一项答案错误未找到解决方法,第三题求导难度难了点不是很会,没有得到全部的分,还需要继续努力。在这三次大作业中,我基本掌握了Java的编程规则、知识要点和一些小技巧,特别是对面向对象的编程思想和风格有了进一步的认识和体会。同时,因正确的编出程序而带来的成就感让我对编程更加感兴趣。对于在这些实验过程中,请教老师、同学互助、查阅资料等基本的学习方式,使我更加领悟到集体和团队的力量,也树立了敢于攻坚的信心。

 

设计与分析:

题目集一7-8:

该题比较简单,使用几个if循环判断输入的三边是否能够构成三角形和构成什么样的三角形,然后将其对应输出。关键代码如下

 

if(!((x>=1.0)&&(x<=200.0)&&(200.0>=y)&&(y>=1.0)&&(200.0>=z)&&(z>=1.0)))

                 System.out.println("Wrong Format");

              else if(x+y<=z||x+z<=y||y+z<=x)

                            System.out.println("Not a triangle");

          else{

                 if((x==y)&&(x==z)&&(y==z))

                     {

                     System.out.println("Equilateral triangle");

               return;

                     }

                  if(((Math.abs(x*x+y*y-z*z)<=1e-6)||(Math.abs(y*y+z*z-x*x)<=1e-6)||(Math.abs(x*x+z*z-y*y)<=1e-6))&&((x==y)||(x==z)||(y==z)))

                     {

                     System.out.println("Isosceles right-angled triangle");

               return;

                     }

                  if(((x==y)||(x==z)||(y==z)))

                     {

                     System.out.println("Isosceles triangle");

               return;

                     }

                  if(((Math.abs(x*x+y*y-z*z)<=1e-6)||(Math.abs(y*y+z*z-x*x)<=1e-6)||(Math.abs(x*x+z*z-y*y)<=1e-6)))

                     {

                     System.out.println("Right-angled triangle");

               return;

                     }

                  else

                        {

                        System.out.println("General triangle");

               return;

                        }

          }

 

题目集二7-4:求下一天主要是注意输入数据的边界、注意2月份情况、注意12月份情况、注意每月有30天和31天的情况,二月份需要考虑平年和闰年,月份可能是大月和小月,下一天可能跨年等等。

 

  public static void nextDate(int year,int month,int day){

          if(checkInputValidity(year,month,day)==false){

                 System.out.println("Wrong Format");

                 return;

          }

          else if(checkInputValidity(year,month,day)==true){

                 if((month==1||month==3||month==5||month==7||month==8||month==10)&&day==31){

                        month++;

                        day=1;

                        System.out.println("Next date is:"+year+"-"+month+"-"+day);

               return;

                 }

                 else if((month==1||month==3||month==5||month==7||month==8||month==10)&&day!=31){

                        day++;

                       

                 }

                 if((month==4||month==6||month==9||month==11)&&day==30){

                        month++;

                        day=1;

                        System.out.println("Next date is:"+year+"-"+month+"-"+day);

               return;

                 }

                 else if((month==4||month==6||month==9||month==11)&&day!=30){

                        day++;

                       

                 }

                 if(month==2){

                        if(isLeapYear(year)==true){

                               if(day==29){

                                      month=3;

                                      day=1;

                               }

                               else{

                                      day++;

                               }

                 }

                        else if(isLeapYear(year)==false){

                               if(day==28){

                                      month=3;

                                      day=1;

                               }

                               else{

                                      day++;

                               }

                 }

          }

                 if(month==12&&day==31){

                        year++;

                        day=1;

                        month=1;

                 }

                 else if(month==12&&day!=31){

                        day++;

                 }

     }   

 

题目集二7-5

 

本题需要注意注意n为负值的情况,并判断输入是否合法,和7-4差别不大。

 

public static void nextDate(int year,int month,int day,int n){

                        int sum;

                     if(checkInputValidity(year,month,day)==false){

                               System.out.println("Wrong Format");

                               return;

                        }

                            else if(n<0)

                            {     

                            int  b=n;

                                   n=-n;

                                   sum=day+n;

                                   if(sum>31&&(month==1||month==5||month==7||month==8||month==10))

                                   {

                           month++;

                           sum=sum-31;

 

                                   }

                                   else if(month==2)

                                   {

                                          if(sum>29&&isLeapYear(year))

                                       {

                                    month++;

                                    sum=sum-29;

                                       }

                                          if(sum>28&&isLeapYear(year)==false)

                                          {

                                       month++;

                                       sum=sum-28;

                                          }

                                   }

                                   else if(sum>30&&(month==4||month==6||month==9||month==11))

                                   {

                                           month++;

                                  sum=sum-30;

                                   }

                                   else if(sum>31&&month==12)

                                   {

                                          sum=sum-31;

                                          month=1;

                                          year++;

                                   }

                                          System.out.println(b+" days ago is:"+year+"-"+month+"-"+sum);

                            }

                            else if(n>0)

                            {     

                           

                                sum=day-n;

 

                                   if(sum<0&&(month==12||month==5||month==7||month==10))

                                   {

                           month--;

                           sum=sum+30;

 

                                   }

                                   else if(month==3)

                                   {

                                          if(sum<0&&isLeapYear(year))

                                   {

                                month--;

                                sum=sum+29;

                                   }

                                          if(sum<0&&isLeapYear(year)==false)

                                          {

                                       month--;

                                       sum=sum+28;

                                          }

                                   }

                                   else if(sum<0&&(month==2&&month==4||month==6||month==9||month==11||month==8))

                                   {

                                           month--;

                                  sum=sum+31;

                                   }

                                   else if(sum<0&&month==1)

                                   {

                                          sum=sum+31;

                                          month=12;

                                          year--;

                                   }

                                          System.out.println(n+" days ago is:"+year+"-"+month+"-"+sum);

                            }

                            else if(n==0)

                            {

                                   System.out.println(n+" days ago is:"+year+"-"+month+"-"+day);

                            }

                     }

 

习题集三7-2

 

定义日期类求下一天主要本题需要封装起来,要不然得不了分。

 

public DateUtil getNextNDays(int n){

              DateUtil date=new DateUtil(year,month,day);

              int[] D={31,28,31,30,31,30,31,31,30,31,30,31};

              int all=0;

           if(n>D[date.getMonth()-1]-date.getDay()){

                  all=all+D[date.getMonth()-1]-date.getDay();

          

                         date.setMonth(date.getMonth()+1);

                         date.setDay(date.getDay()+1);

                         if(date.getMonth()==13){

                                date.setMonth(1);

                                date.setYear(date.getYear()+1);

                         }

                         if(date.getDay()==32) {

                                date.setDay(1);

                         }

                          if(isLeapYear(date.getYear())){

                                D[1]=29;

                         }

                          if(isLeapYear(date.getYear())&&date.getDay()==30) {

                                 date.setDay(1);

                          }

                          if(!isLeapYear(date.getYear())&&date.getDay()==29&&date.getDay()==29) {

                                 date.setDay(1);

                          }

                         else

                                D[1]=28;

                         all=all+D[date.getMonth()-1];

                 

//               date.setMonth(date.getMonth()+1);

//               date.setDay(n-all);

                  return date;

           }

           else{

                  date.setDay(date.getDay()+n);

                  return date;

           }

       }

 

习题7-3

本题注意表达式中存在空格的情况(需要先滤掉空格)

其次使用数组或者List(ArrayList或者LinkedList)存储识别的每个表达式中的项

熟练应用正则表达式,而且匹配的目标是纯文本,那么相比于写分析器来说,正则可以更快速的完成工作。还有在捕获字符串的能力,正则也可以很好的完成工作,比如截取url的域名或者其他的内容等等但是1.正则表达式只适合匹配文本字面,不适合匹配文本意义:像匹配url,email这种纯文本的字符就很好,但比如匹配多少范围到多少范围的数字,如果你这个范围很复杂的话用正则就很麻烦。或者匹配html,这个是很多人经常遇到的,写一个复杂匹配html的正则很麻烦,不如使用针对特定意义的处理器来处理(比如写语法分析器,dom分析器等)

2.容易引起性能问题:像.*这种贪婪匹配符号很容易造成大量的回溯,性能有时候会有上百万倍的下降,编写好的正则表达式要对正则引擎执行方式有很清楚的理解才可以

3.正则的替换功能较差:甚至没有基本的截取字符串或者把首字母改变大小写的功能,这对于url重写引擎有时候是致命的影响

式子的拆分

void strCut() {

              // 1.CON 2.TRIB 3.POLPARTA

              String aStr = this.standard(this.polStr);

              // System.out.println("standard--> "+ aStr);

              String patt[] = { CON, TRIB, POLPARTA };

              for (int i = 0; i < patt.length; i++) {

                     Pattern p = Pattern.compile(patt[i]);

                     Matcher m = p.matcher(aStr);

                     while (m.find()) {

                            this.part[this.len] = m.group();

                            this.len++;

                     }

                     aStr = aStr.replaceAll(patt[i], "");

              }

              this.len--;

              for (int i = 0; i < this.len; i++) {

                     // System.out.println(i+1+" ---> "+this.part[i]);

                     this.ratFull.add(this.union(this.part[i]));

                     // System.out.println(this.ratFull);

              }

       }

 

       Vector union(String str) {

              this.sExp = BigInteger.valueOf(0);

              this.cExp = BigInteger.valueOf(0);

              this.aX = BigInteger.valueOf(1);

              this.xExp = BigInteger.valueOf(0);

              Vector sec = new Vector();

              BigInteger op = new BigInteger("1");

              if (str.charAt(0) == '-')

                     op = new BigInteger("-1");

              BigInteger b = new BigInteger("0");

              // sin|cos^e x^e num

 

              String mat[] = { SIN, COS, POL, EXP, NUM };

              Pattern p1 = Pattern.compile(SIN);

              Pattern p2 = Pattern.compile(COS);

              Pattern p3 = Pattern.compile(POL);

              Pattern p4 = Pattern.compile(EXP);

              Pattern p5 = Pattern.compile(NUM);

              // ---------------------------------------------------sin(x)^a

              Matcher m1 = p1.matcher(str);

              while (m1.find()) {

                     String sinStr = m1.group();

                     // System.out.println("=======> "+sinStr);

                     if (sinStr.length() == 6) {

                            b = new BigInteger("1");

                     } else {

                            Matcher e1 = p4.matcher(sinStr);

                            if (e1.find()) {

                                   String s = e1.group();

                                   b = new BigInteger(s);

                            }

                     }

                     this.sExp = this.sExp.add(b);

              }

              str = str.replaceAll(SIN, "");

              // ----------------------------------------------------cos(x)^a

              Matcher m2 = p2.matcher(str);

              while (m2.find()) {

                     String cosStr = m2.group();

                     // System.out.println("=======> "+cosStr);

                     if (cosStr.length() == 6) {

                            b = new BigInteger("1");

                     } else {

                            Matcher e2 = p4.matcher(cosStr);

                            if (e2.find()) {

                                   String s = e2.group();

                                   b = new BigInteger(s);

                            }

                     }

                     this.cExp = this.cExp.add(b);

              }

              str = str.replaceAll(COS, "");

              // ------------------------------------------------------x^a

              Matcher m3 = p3.matcher(str);

              while (m3.find()) {

                     String pStr = m3.group();

                     // System.out.println("=======> "+pStr);

                     if (pStr.length() == 1) {

                            b = new BigInteger("1");

                     } else {

                            Matcher e3 = p4.matcher(pStr);

                            if (e3.find()) {

                                   String s = e3.group();

                                   b = new BigInteger(s);

                            }

                     }

                     this.xExp = this.xExp.add(b);

              }

              str = str.replaceAll(POL, "");

              // ---------------------------------------------------[0-9]

              Matcher m4 = p5.matcher(str);

              while (m4.find()) {

                     String nStr = m4.group();

                     // System.out.println("=======> "+nStr);

                     Matcher e4 = p4.matcher(nStr);

                     if (e4.find()) {

                            String s = e4.group();

                            b = new BigInteger(s);

                            this.aX = this.aX.multiply(b);

                     }

              }

              this.aX = this.aX.multiply(op);

              sec.add(this.aX);

              sec.add(this.xExp);

              sec.add(this.sExp);

              sec.add(this.cExp);

              //System.out.println(sec);

              return sec;

       }

 

}

 

这道题还是比较难的,自己没有很好的思路,正则表达式也没有学的特别好。

 

采坑心得

Arrays.asList是会返回一个ArrayList对象,但是该类不是常见的java.util.ArrayList类。java.util.Arrays.ArrayList 类具有set(),get(),contains()等方法,但是不具有任何添加或移除元素的任何方法。因为该类的大小(size)是固定的。所以为了创建出一个真正的 java.util.ArrayList,

 

ArrayList arrayList = new ArrayList(Arrays.asList(arr));

ArrayList的构造方法可以接受一个Collection类型的对象,而我们的 java.util.Arrays.ArrayList 正好也是它的一个子类。更加高效的代码示例是:

 

ArrayList arrayList = new ArrayList(arr.length);

提交至pta不能加包名不然会有很特殊的答案错误

改进建议:希望题目可以更加清楚,输出的答案格式不那么死。

总结:“ java,入门是一件容易的事,进阶需要有一颗坚持学习的心,但是最困难的事情是一直坚持。”

我最开始学习java,不是因为java广受欢迎,也不是因为用java薪水高,也不是因为java入门容易,而是因为在学校,学习java是一门必修课,必须要学,所以我就学了。学的时候给我的感觉是,好枯燥,运行结果看的都是控制器显示出来的,没有炫酷的界面,全是英文,一点兴趣都没有。相信很多人也跟我一样,初学java的时候感觉好枯燥,但是我想说学习本来就是一个枯燥的过程,你要想办法如何把枯燥转变

Posted on 2021-04-04 23:58  满洲里有海  阅读(595)  评论(0编辑  收藏  举报