第一次博客作业

前三次作业总结


前言

(总结三次题目集的知识点、题量、难度等情况)

1、第一次作业我觉得主要是看大家对Java基础知识点的掌握情况,其中主要涉及到了if-else、switch-case等条件语句和算术表达式的运用。对于我个人来说,前面几题目难度偏低,后面几题的难度中等吧,这些难度的题目设置的题量是刚刚好的。

2、第二次作业就涉及到了对方法的运用及其掌握和Java基础知识点的综合掌握,包括if-else条件判断、while和for循环和排序的综合掌握情况。第二次题目确实比第一次难度大了很多,难度偏难(毕竟我是个渣渣),题量还行。

3、第三次作业就涉及到了对于类的掌握情况,前两题跟第二次作业其实差不多,难度也跟上一次题目差不多,最后一题就真的非常非常非常难啦,还运用到了正则表达式。题量的话,可能是最后一道太难的缘故,所以就没有布置太多题目,个人觉得这种题目是为了拉开差距的,所以希望老师以后少出这类难度的题目吧(好南啊,球球老师啦)。

设计与分析

1、第一次作业有八道题目,我们这里着重介绍第8题:

7-8 判断三角形类型 (20 分)

输入三角形三条边,判断该三角形为什么类型的三角形。

输入格式:

在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[1,200]。

输出格式:

(1)如果输入数据非法,则输出“Wrong Format”;

(2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”;

(3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”;

(4)如果输入数据合法且能够成等腰直角三角形,则输出“Isosceles right-angled triangle”;

(5)如果输入数据合法且能够成等腰三角形,则输出“Isosceles triangle”;

(6)如果输入数据合法且能够成直角三角形,则输出“Right-angled triangle”;

(7)如果输入数据合法且能够成一般三角形,则输出“General triangle”。

分析:

对于这道题目,我主要是运用if-else嵌套循环来实现:首先判断输入的数据是否合法,在判断输入的数据都否能构成三角形,然后再一步一步的对等边等腰和直角三角形进行具体的判断,但是这里要注意对直角三角形的判断,不能用直接用"a^2+b^2=c^2",可能是测试点的原因,要换成"a*a+b*b-c*c<0.1(或者是一个很小很小的数)"。

相应类图:

对于7-8我是直接在主类Main里面进行编辑,所以类图很简单。

SourceMonitor的生成报表:

 

从SourceMonitor的生成报表中可以看出Avg Complexity平均复杂度、Avg Depth平均深度和Max Complexity最大复杂度都超过了绿色区域,说明这段代码还需要改进。

源码:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Scanner in = new Scanner(System.in);
 8         double a=in.nextDouble();
 9         double b=in.nextDouble();
10         double c=in.nextDouble();
11         if((a>200||a<1)||(b>200||b<1||(c>200||c<1))) {
12             System.out.print("Wrong Format");
13         }
14         else {
15             if(a+b>c&&a+c>b&&c+b>a) {
16                 if(a==b&&a==c)//等边
17                     System.out.print("Equilateral triangle");
18                 else {
19                     //等腰
20                     if((a==b)||(a==c)||(b==c)) {
21                         //判断直角
22                         if((a*a+b*b-c*c<0.1)||(a*a+c*c-b*b<0.1)||(b*b+c*c-a*a<0.1))
23                             System.out.print("Isosceles right-angled triangle");
24                         else System.out.print("Isosceles triangle");
25                     }
26                     else {
27                         if((a*a+b*b-c*c<0.1)||(a*a+c*c-b*b<0.1)||(b*b+c*c-a*a<0.1))
28                             System.out.print("Right-angled triangle");
29                         else System.out.print("General triangle");
30                     }
31                 }
32             }
33             else System.out.print("Not a triangle");
34         }
35     }
36 } 

2、第二次作业

(1)7-3:

7-3 判断闰年及星期几 (20 分)

输入年月日的值(均为整型数),输出该年份是否为闰年,同时输出该日期为星期几。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] ; 判断星期几的算法如下:假定公元0001年1月1日为星期一,因此只要计算出当前输入日期离0001年1月1日所差的天数,然后拿这个天数除以7求余数,当余数为0时,为星期日,当余数为1时,为星期一,以此类推,当余数为6时,为星期六。

要求:Main类中必须含有如下方法,签名如下:

public static void main(String[] args);//主方法;
public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型;
public static int numOfDays(int year,int month ,int day) ;//求出year-month-day到0001-1-1的距离天数,返回整型数;
public static String getWhatDay(int days) ; //根据天数返回星期几,其中参数days为天数,整型数,返回星期几的英文单词。

分析:对于这道判断闰年及星期几的题目,考察的主要是对于方法的运用,题目中已经给出了所要写的方法,按照要求一个一个去实现,难度还是可以接受的。题目中主要要注意这几点:一、闰年的概念;二、一定要注意每个月份的日期大都不一样,所以要分开讨论,最好是运用数组;三、就是闰年和平年的日期是不一样的;四、判断输入的数据是否合法。

相应类图:

 

7-3是按照题目所要求的方法去实现题目内容本身,所以类图也相较简单。

SourceMonitor的生成报表:

从SourceMonitor的生成报表中可以看出Avg Complexity平均复杂度、Avg Depth平均深度和Max Complexity最大复杂度也都超过了绿色区域,if-else循环用的太多,也确实说明所测试代码还需要改进。

源码:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Scanner in =new Scanner(System.in);
 8         int year,month,day;
 9         year=in.nextInt();
10         month=in.nextInt();
11         day=in.nextInt();
12         if(isday(year,month,day)) {
13             if(isLeapYear(year))
14                 System.out.println(year+" is a leap year.");
15             else
16                 System.out.println(year+" is not a leap year.");
17             int number=numOfDays(year,month,day);
18             System.out.print(year+"-"+month+"-"+day+" is "+getWhatDay(number)+".");
19         }
20         else
21             System.out.print("Wrong Format");
22     }
23     public static boolean isday(int year,int month,int day) {
24             boolean temp=false;
25             int[] mon1=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};
26             int[] mon2=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
27             if(year>=1820&&year<=2020) {
28                 if(month>0&&month<=12) {
29                     if(isLeapYear(year)) {
30                         if(day<=mon1[month]&&day>0)
31                             temp=true;
32                     }
33                     else {
34                         if(day<=mon2[month]&&day>0)
35                             temp=true;
36                     }
37                 }
38             }
39             return temp;
40     }
41     public static boolean isLeapYear(int year) {
42         //判断year是否为闰年,返回boolean类型;
43         return year%400==0||(year%4==0&&year%100!=0);
44     }
45     public static int numOfDays(int year,int month ,int day) {
46         //求出year-month-day到0001-1-1的距离天数,返回整型数;
47         int total1=0,total2=0;
48         for(int i=1;i<year;i++) {
49             if(isLeapYear(i))
50                 total1+=366;
51             else
52                 total1+=365;
53         }
54         for(int i=1;i<month;i++) {
55             if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
56                 total2+=31;
57             if(i==4||i==6||i==9||i==11)
58                 total2+=30;
59             if(i==2) {
60                 if(isLeapYear(year))
61                     total2+=29;
62                 else
63                     total2+=28;
64             }
65         }
66         return total1+total2+day;
67     }
68     public static String getWhatDay(int days)  {
69         //根据天数返回星期几,其中参数days为天数,整型数,返回星期几的英文单词。
70         if(days%7==0) {
71             return "Sunday";
72         }
73         else if(days%7==1) {
74             return "Monday";
75         }
76         else if(days%7==2) {
77             return "Tuesday";
78         }
79         else if(days%7==3) {
80             return "Wednesday";
81         }
82         else if(days%7==4) {
83             return "Thursday";
84         }
85         else if(days%7==5) {
86             return "Friday";
87         }
88         else 
89             return "Saturday";
90     }
91 }

(2)7-4:

7-4 求下一天 (30 分)

输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法。

要求:Main类中必须含有如下方法,签名如下:

public static void main(String[] args);//主方法
public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型
public static boolean checkInputValidity(int year,int month,int day);//判断输入日期是否合法,返回布尔值
public static void nextDate(int year,int month,int day) ; //求输入日期的下一天

分析:这道题跟7-3相类似,只不过这个换成了求下一天,上一题是求星期几。对于这道题,难点是有很多种情况都要去考虑,参考老师给的一些测试点:

所以在进行代码的编辑时,每种特殊情况都要去考虑。

相应类图:

 

7-4也是按照题目所要求的方法去实现题目内容本身,所以类图也相较简单。

SourceMonitor的生成报表:

 

从SourceMonitor的生成报表中可以看出Avg Complexity平均复杂度、Avg Depth平均深度和Max Complexity最大复杂度也都超过了绿色区域,可能是if-else循环用的太多,所以代码还需改进。

源码:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Scanner in =new Scanner(System.in);
 8         int year,month,day;
 9         year=in.nextInt();
10         month=in.nextInt();
11         day=in.nextInt();
12         if(checkInputValidity(year,month,day)) {
13             nextDate(year,month,day);
14         }
15         else
16             System.out.print("Wrong Format");
17     }
18     public static boolean checkInputValidity(int year,int month,int day) {
19             boolean temp=false;
20             int[] mon1=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};
21             int[] mon2=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
22             if(year>=1820&&year<=2020) {
23                 if(month>0&&month<=12) {
24                     if(isLeapYear(year)) {
25                         if(day<=mon1[month]&&day>0)
26                             temp=true;
27                     }
28                     else {
29                         if(day<=mon2[month]&&day>0)
30                             temp=true;
31                     }
32                 }
33             }
34             return temp;
35     }
36     public static boolean isLeapYear(int year) {
37         //判断year是否为闰年,返回boolean类型;
38         return year%400==0||(year%4==0&&year%100!=0);
39     }
40     public static void nextDate(int year,int month ,int day) {
41         if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
42             if(day<31) {
43                 System.out.print("Next date is:"+year+"-"+month+"-"+(day+1));
44             }
45             else {
46                 if(month<12)
47                     System.out.print("Next date is:"+year+"-"+(month+1)+"-"+1);
48                 if(month==12)
49                     System.out.print("Next date is:"+(year+1)+"-"+1+"-"+1);
50             }
51         }
52         if(month==2) {
53             if(day<28)
54                 System.out.print("Next date is:"+year+"-"+month+"-"+(day+1));
55             if(day==28) {
56                 if(isLeapYear(year)) 
57                     System.out.print("Next date is:"+year+"-"+month+"-"+(day+1));
58                 else
59                     System.out.print("Next date is:"+year+"-"+(month+1)+"-"+1);
60             }
61             else
62                 System.out.print("Next date is:"+year+"-"+(month+1)+"-"+1);
63         }
64         if(month==4||month==6||month==9||month==11) {
65             if(day<30) {
66                 System.out.print("Next date is:"+year+"-"+month+"-"+(day+1));
67             }
68             else {
69                     System.out.print("Next date is:"+year+"-"+(month+1)+"-"+1);
70             }
71         }
72     }
73 }

(4)7-5: 

7-5 求前N天 (30 分)

输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。
注意:不允许使用Java中任何与日期有关的类或方法。

输入格式:

在一行中输入年月日的值以及n的值,可以用一个或多个空格或回车分隔。

输出格式:

  1. 当输入的年、月、日以及n的值非法时,输出“Wrong Format”;

  2. 当输入数据合法时,输出“n days ago is:年-月-日”。

分析:这道题又跟上一道7-4类似,上一题是求下一天,而这个是求前n天,n可为正也可为负,负数就是下n天的意思。所以在进行代码的编写过程中,首先要分成两种情况,一种是n<0,另一种是n>0来写。接下来就是各种情况的分析了,参考老师的一些测试点:

相应类图:

 

7-5也是按照题目所要求的方法去实现题目内容本身,所以类图也相较简单。

SourceMonitor的生成报表:

 

代码太过于冗余,平均深度和复杂度都太高了,代码需改进。

源码:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Scanner in =new Scanner(System.in);
 8         int year,month,day;
 9         year=in.nextInt();
10         month=in.nextInt();
11         day=in.nextInt();
12         int days=in.nextInt();
13         if(checkInputValidity(year,month,day,days)) {
14             nextDate(year,month,day,days);
15         }
16         else
17             System.out.print("Wrong Format");
18     }
19     public static boolean checkInputValidity(int year,int month,int day,int days) {
20             boolean temp=false;
21             int[] mon1=new int[]{0,31,29,31,30,31,30,31,31,30,31,30,31};
22             int[] mon2=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
23             if(days>=-10&&days<=10) {
24                 if(year>=1820&&year<=2020) {
25                     if(month>0&&month<=12) {
26                         if(isLeapYear(year)) {
27                             if(day<=mon1[month]&&day>0)
28                                 temp=true;
29                         }
30                         else {
31                             if(day<=mon2[month]&&day>0)
32                                 temp=true;
33                         }
34                     }
35                 }
36             }
37             return temp;
38     }
39     public static boolean isLeapYear(int year) {
40         //判断year是否为闰年,返回boolean类型;
41         return year%400==0||(year%4==0&&year%100!=0);
42     }
43     public static void nextDate(int year,int month,int day,int days) {
44         if(days<0) {
45             if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
46                 if(day-days<=31) {
47                     System.out.print(days+" days ago is:"+year+"-"+month+"-"+(day-days));
48                 }
49                 else {
50                     if(month<12)
51                         System.out.print(days+" days ago is:"+year+"-"+(month+1)+"-"+(day-days-31));
52                     if(month==12)
53                         System.out.print(days+" days ago is:"+(year+1)+"-"+1+"-"+(day-days-31));
54                 }
55             }
56             else if(month==2) {
57                 if(day-days<=28)
58                     System.out.print(days+" days ago is:"+year+"-"+month+"-"+(day-days));
59                 if(day-days>=29) {
60                     if(isLeapYear(year)) {
61                         if(days-day==29)
62                             System.out.print(days+" days ago is:"+year+"-"+month+"-"+(day-days));
63                         else
64                             System.out.print(days+" days ago is:"+year+"-"+(month+1)+"-"+(day-days-29));
65                     }
66                     else System.out.print(days+" days ago is:"+year+"-"+(month+1)+"-"+(day-days-28));
67                 }
68             }
69             else if(month==4||month==6||month==9||month==11) {
70                 if(day-days<=30)
71                     System.out.print(days+" days ago is:"+year+"-"+month+"-"+(day-days));
72                 else
73                     System.out.print(days+" days ago is:"+year+"-"+(month+1)+"-"+(day-days-30));
74             }
75         }
76         else if(days==0)
77             System.out.print(days+" days ago is:"+year+"-"+month+"-"+day);
78         else {
79             if(day-days>0)
80                 System.out.print(days+" days ago is:"+year+"-"+month+"-"+(day-days));
81             else {
82                 if(month==1) 
83                     System.out.print(days+" days ago is:"+(year-1)+"-"+12+"-"+(day-days+31));
84                 else if(month==3) {
85                     if(isLeapYear(year))
86                         System.out.print(days+" days ago is:"+year+"-"+2+"-"+(day-days+29));
87                     else
88                         System.out.print(days+" days ago is:"+year+"-"+2+"-"+(day-days+28));
89                 }
90                 if(month==2||month==4||month==6||month==8||month==9||month==11)
91                     System.out.print(days+" days ago is:"+year+"-"+(month-1)+"-"+(day-days+31));
92                 else if(month==5||month==7||month==10||month==12)
93                     System.out.print(days+" days ago is:"+year+"-"+(month-1)+"-"+(day-days+30));
94             }
95         }
96     }
97

3、第三次作业

(1)7-2:

7-2 定义日期类 (28 分)

定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。

要求:Date类结构如下图所示:

类图.jpg

输入格式:

在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

  • 当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;

  • 当输入日期合法,输出下一天,格式如下:Next day is:年-月-日

分析:这道题其实就是作业二的7-4,只不过这里要求直接建一个data类来完成求下一天的日期,所以这道题主要就是看我们是否掌握对于类的一些基本使用方法,例如对get和set方法的使用。 

相应类图:

按照题目要求给出的方法,一个一个去实现,运用了面向对象的思维构造了Data类。

SourceMonitor的生成报表:

只有最大复杂度有点大,整体代码质量还好。

源码:

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Scanner in =new Scanner(System.in);
 8         Date date1=new Date();
 9         date1.setYear(in.nextInt());
10         date1.setMonth(in.nextInt());
11         date1.setDay(in.nextInt());
12         if(date1.checklnputValidity(date1.getYear(),date1.getMonth(),date1.getDay())) {
13             System.out.print("Next day is:");
14             date1.getNextDate();
15         }
16         else
17             System.out.print("Date Format is Wrong");
18     }
19 }    
20 class Date{
21     int year;
22     int month;
23     int day;
24     int[] mon_maxnum=new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
25     int[] mon_minnum=new int[] {0,31,29,31,30,31,30,31,31,30,31,30,31};
26     Date(){
27         
28     }
29     Date(int year,int month,int day){
30         this.year=year;
31         this.month=month;
32         this.day=day;
33     }
34     public int getYear() {
35         return year;
36     }
37     public void setYear(int year) {
38         this.year=year;
39     }
40     public int getMonth() {
41         return month;
42     }
43     public void setMonth(int month) {
44         this.month=month;
45     }
46     public int getDay() {
47         return day;
48     }
49     public void setDay(int day) {
50         this.day=day;
51     }
52     public boolean isLeapYear(int year) {
53         return year%400==0||(year%4==0&&year%100!=0);
54     }
55     public boolean checklnputValidity(int year,int month,int day) {
56         boolean temp=false;
57         if(this.year>=1900&&this.year<=2000) {
58             if(this.month<=12&&this.month>=1) {
59                 if(isLeapYear(this.year)) {
60                     if(this.day<=mon_minnum[this.month]&&this.day>0)
61                         temp=true;
62                 }
63                 else {
64                     if(this.day<=mon_maxnum[this.month]&&this.day>0)
65                         temp=true;
66                 }
67             }
68             
69         }
70         return temp;
71     }
72     public void getNextDate() {
73         if(isLeapYear(this.year)) {
74             if(this.day<mon_minnum[this.month])
75                 System.out.print(this.year+"-"+this.month+"-"+(this.day+1));
76             else if(this.day==mon_minnum[this.month]&&this.month!=12) {
77                 System.out.print(this.year+"-"+(this.month+1)+"-"+1);
78             }
79             else {
80                 System.out.print((this.year+1)+"-"+1+"-"+1);
81             }
82         }
83         else {
84             if(this.day<mon_maxnum[this.month])
85                 System.out.print(this.year+"-"+this.month+"-"+(this.day+1));
86             else if(this.day==mon_maxnum[this.month]&&this.month!=12) {
87                 System.out.print(this.year+"-"+(this.month+1)+"-"+1);
88             }
89             else {
90                 System.out.print((this.year+1)+"-"+1+"-"+1);
91             }
92         }
93     }
94 }

(2)7-3:

7-3 一元多项式求导(类设计) (50 分)

编写程序性,实现对简单多项式的导函数进行求解。

输入格式:

在一行内输入一个待计算导函数的表达式,以回车符结束。

输出格式:

  1. 如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”。
  2. 如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;
  • 当某一项为“0”时,则该项不需要显示,但如果整个导函数结果为“0”时,则显示为“0”;
  • 当输出结果第一项系数符号为“+”时,不输出“+”;
  • 当指数符号为“+”时,不输出“+”;
  • 当指数值为“0”时,则不需要输出“x^0”,只需要输出其系数即可。

分析:该题为前三题目集中难度最高的题,需要掌握如何正确使用正则表达表达式去判断输入是否正确以及对目标字符串的分割处理,先分割出单个式子,再判断是否为常数式另外处理,分割出系数和次数然后求导处理即可。注意得去判断一些特殊的情况。

相应类图:

整体运用了很多类,所以类图较复杂。

SourceMonitor的生成报表:

只有最大复杂度有点大,整体代码还好。

源码:

  1 import java.util.Scanner;
  2 import java.util.ArrayList;
  3 import java.util.LinkedList;
  4 import java.math.BigInteger;
  5 public class Main {
  6     public static void main(String[] args) {
  7         String str = new String();
  8         Scanner input = new Scanner(System.in);
  9         str = input.nextLine();
 10         Polynomial pl = new Polynomial(str);
 11         if(!pl.isInteger(str)) {
 12             System.out.println("Wrong Format");
 13         }
 14         else {
 15             Expression ex = new Expression(pl.getpolynomial());
 16             if(str.matches(" *(\\-|\\+)? *( *[1-9] *(\\d *)*) *")) {
 17                 System.out.println("0");
 18             }
 19             else {
 20                 System.out.println(ex.toString());
 21             }
 22         }
 23     }
 24 }
 25 
 26 class Term {
 27     
 28     public Term getDerivative() {
 29         return new Term();
 30     }
 31 }
 32 class PowerFunctionTerm extends Term{
 33     private BigInteger coeff;
 34     private BigInteger index;
 35     public PowerFunctionTerm() {
 36         
 37     }
 38     public PowerFunctionTerm(BigInteger coeff, BigInteger index) {
 39         super();
 40         this.coeff = coeff;
 41         this.index = index;
 42     }
 43     public BigInteger getCoeff() {
 44         return coeff;
 45     }
 46     public void setCoeff(BigInteger coeff) {
 47         this.coeff = coeff;
 48     }
 49     public BigInteger getIndex() {
 50         return index;
 51     }
 52     public void setIndex(BigInteger index) {
 53         this.index = index;
 54     }
 55     public PowerFunctionTerm getDerivative() {
 56         PowerFunctionTerm pft = new PowerFunctionTerm();
 57         pft.setCoeff(coeff.multiply(index));
 58         pft.setIndex(index.subtract(BigInteger.valueOf(1)));
 59         return pft;
 60     }
 61     public String toString() {
 62         if(getDerivative().getIndex().equals(BigInteger.valueOf(0))) {
 63             return (getDerivative().getCoeff().toString());
 64         }
 65         else if(getDerivative().getIndex().equals(BigInteger.valueOf(1))) {
 66             return (getDerivative().getCoeff() + "*x");
 67         }
 68         else {
 69             return (getDerivative().getCoeff() + "*x^" + getDerivative().getIndex());
 70         }
 71     }
 72 }
 73 class ConstantTerm extends Term{
 74     private BigInteger value;
 75     public ConstantTerm() {
 76         
 77     }
 78     public ConstantTerm(BigInteger value) {
 79         super();
 80         this.value = value;
 81     }
 82     public BigInteger getValue() {
 83         return value;
 84     }
 85     public void setValue(BigInteger value) {
 86         this.value = value;
 87     }
 88     public ConstantTerm getDerivative() {
 89         return new ConstantTerm(new BigInteger("0"));
 90     }
 91     public String toString() {
 92         return "";
 93     }
 94 }
 95 class Expression {
 96     private ArrayList<BigInteger> list = new ArrayList<>();
 97     StringBuilder sb = new StringBuilder();
 98     public Expression() {
 99         
100     }
101     public Expression(ArrayList<BigInteger> list) {
102         this.list = list;
103     }
104     public String toString() {
105         for(int i = 0;i<list.size();i += 2) {
106             PowerFunctionTerm pft = new PowerFunctionTerm(list.get(i),list.get(i+1));
107             ConstantTerm ct = new ConstantTerm();
108             if( i==0 && (list.get(i+1) != BigInteger.valueOf(0)) ) {
109                 sb.append(pft.toString());
110             }
111             else if(i == 2 && ( list.get(i-1) == BigInteger.valueOf(0) ) && (pft.getCoeff().multiply(pft.getIndex()).compareTo(BigInteger.valueOf(0))>0 && (list.get(i+1) != BigInteger.valueOf(0)))) {
112                 sb.append(pft.toString());
113             }
114             else if( pft.getCoeff().multiply(pft.getIndex()).compareTo(BigInteger.valueOf(0))>0 && (list.get(i+1) != BigInteger.valueOf(0)) ) {
115                 sb.append("+");
116                 sb.append(pft.toString());
117             }
118             else if(list.get(i+1) != BigInteger.valueOf(0) ) {
119                 sb.append(pft.toString());
120             }
121             else {
122                 sb.append(ct.toString());
123             }
124         }
125         return sb.toString();
126     }
127 }
128 class Polynomial {
129     private String data = new String();
130     public Polynomial() {
131         
132     }
133     public Polynomial(String data) {
134         this.data = data;
135     }
136     public boolean isInteger(String s) {
137          if(s.matches(" *((\\-|\\+)? *(((( *[2-9] *|( *[1-9] *(\\d *){1,})) *\\* *)? *x *( *\\^ *((- *[1-9] *(\\d *)*)|(( *[2-9] *|( *[1-9] *(\\d *){1,})))))? *)|( *[1-9] *(\\d *)*)))( *(\\+|-) *(((( *[2-9] *|( *[1-9] *(\\d *){1,})) *\\* *)? *x *( *\\^ *(( *-[1-9] *(\\d *)*)|(( *[2-9] *|( *[1-9] *(\\d *){1,})))))? *)|( *[1-9] *(\\d *)*)))* *")) {
138             return true;
139         }
140         return false;
141     }
142     public ArrayList<BigInteger> getpolynomial(){
143         BigInteger num = new BigInteger("0");
144         BigInteger symbol = new BigInteger("1");
145         int mark = 0;
146         ArrayList<BigInteger> list = new ArrayList<>();
147         String s = data.replaceAll(" ", "");
148         for(int i = 0;i<s.length();i++) {
149             if( i!=s.length() && s.charAt(i)>= '0' && s.charAt(i)<= '9'){
150                 num = num.multiply(BigInteger.valueOf(10));
151                 num = num.add(BigInteger.valueOf(s.charAt(i)-'0'));
152             }
153             else if(s.regionMatches(i,"-", 0, 1)) {
154                 if(i != 0 && (!s.regionMatches(i-1,"^", 0, 1)) && mark == 0) {
155                     list.add(num.multiply(symbol));
156                     list.add(BigInteger.valueOf(0));
157                 }
158                 else if(i != 0 && (!s.regionMatches(i-1,"^", 0, 1)) ) {
159                     list.add(num.multiply(symbol));
160                 }
161                 num = BigInteger.valueOf(0);
162                 symbol = BigInteger.valueOf(-1);
163                 if(i == 0)
164                     mark = 0;
165                 else if(s.regionMatches(i-1,"^", 0, 1)){
166                     mark = 1;
167                 }
168                 else {
169                     mark = 0;
170                 }
171                 continue;
172             }
173             else if(s.regionMatches(i,"+", 0, 1)) {
174                 if(i != 0 && (!s.regionMatches(i-1,"^", 0, 1)) && mark == 0) {
175                     list.add(num.multiply(symbol));
176                     list.add(BigInteger.valueOf(0));
177                 }
178                 else if(i != 0 && (!s.regionMatches(i-1,"^", 0, 1)) ) {
179                     list.add(num.multiply(symbol));
180                 }
181                 num = BigInteger.valueOf(0);
182                 symbol = BigInteger.valueOf(1);
183                 mark = 0;
184                 continue;
185             }
186             else if(s.regionMatches(i,"*", 0, 1)) {
187                 list.add(num.multiply(symbol));
188                 num = BigInteger.valueOf(0);
189                 symbol = BigInteger.valueOf(1);
190             }
191             else if(s.regionMatches(i,"x", 0, 1)) {
192                 if(i == 0) {
193                     list.add(BigInteger.valueOf(1));
194                 }
195                 else if( s.regionMatches(i-1,"+", 0, 1)) {
196                     list.add(BigInteger.valueOf(1));
197                 }
198                 else if(s.regionMatches(i-1,"-", 0, 1)) {
199                     list.add(BigInteger.valueOf(-1));
200                 }
201                 if( (!s.regionMatches(i+1,"+", 0, 1)) && (!s.regionMatches(i+1,"-", 0, 1)) && (!s.regionMatches(i+1,"^", 0, 1)) ) {
202                     list.add(BigInteger.valueOf(1));
203                 }
204                 else if( s.regionMatches(i+1,"-", 0, 1) || s.regionMatches(i+1,"+", 0, 1) ) {
205                     list.add(BigInteger.valueOf(1));
206                 }
207                 continue;
208             }
209             else if(s.regionMatches(i,"^", 0, 1)) {
210                 num = BigInteger.valueOf(0);
211                 symbol = BigInteger.valueOf(1);
212                 mark = 1;
213                 continue;
214             }
215         }
216         list.add(num.multiply(symbol));
217         if(mark == 0) {
218             list.add(BigInteger.valueOf(0));
219         }
220         return list;
221     }
222     public BigInteger getIndex(String s) {
223         return new BigInteger(s);
224     }
225     public BigInteger getCoeff(String s) {
226         return new BigInteger(s);
227     }
228 }

采坑心得

1、第一件事就是在PTA上提交Java代码,主类名一定要取Main(好家伙,不会只有我一个人在提交第一题时一直提示编译错误吧,我还以为我连两个数相加这种类型的题目我都不会写啦,C白学了,呜呜呜呜呜呜呜);

2、像第一次作业的7-8,直角三角形的测试点我死活过不去,绞尽脑汁也想不到,条件竟然不能写成a*a+b*b=c*c,确实没考虑浮点数运算不能完全等于;

3、第二次作业的7-3,要先判断数据是否合法,我脑子抽了直接对年份、月份和日期分开讨论,直接一个if循环完事儿,到代码过不去,去瞅了瞅别人的才知道不能这样,年份、月份和日期都是挂钩了的,就比如要是年份是平年的话日期就不可能是2月29;

4、第二次作业的7-3、7-4和7-5其实都是一种类型的题目,都是求日期,会写了其中一道其他两道应该没有什么大问题,但就是要注意闰年和平年的区别、每个月份的日期也不一致,一下不注意就很容易漏掉某种情况;

5、第三次作业的7-2就跟前面第二次作业的7-4一样,只不过这里要定义data类,基本构造类的一些方法注意一下就好;

6、而最后一道题目的话,本题需要考虑多种情况——常数项、系数和指数为负数,系数和指数为+-1时、输入格式错误等等,在写代码前应该把整个实现过程构思好,而不是想到什么写什么,这样可以避免走很多弯路!

改进建议

1、对于第二次作业和第三次作业的那些关于日期计算的题目,我代码中基本都是用if循环来解决问题,一个一个的情况列出来,这样代码显得太过于冗余,其实可以用一些数组啊去解决,比如我之前对于月份都是靠if来实现,其实这里就可以用数组存储每个月份的日期,就可以不用一个一个月份单独拎出来,减少了代码的复杂度;

2、写代码需要加上必要的注释并保证代码编写的规范性,既然使用java语言,那就需要充分利用java的封装性,隐藏对象的属性和实现细节,编写方法时要从对象的身上多去考虑,提高程序的简洁性、建立各个对象之间的松耦合关系。

总结

收获:在没有实例化的对象,只要求实现某一种功能或多种功能时,只需要设计算法,注重过程实现,则使用面向过程编程会更加的简便且易于理解。但是在有实例的对象时,或者对象之间有多种聚合的关系时,则使用面向对象编程会更加易于描述对象的功能与操作,更易于理解对象之间的关系。且通过对类的封装,让一个类易于搬运并在其他的包中使用。

不足:在写代码的过程中,逻辑要严谨,思路要清晰,否则极易出现逻辑上的错误。对于编程的基础知识要牢牢掌握,否则在出现错误时很难发现错在哪,花费大量的时间,效率极低。

对于课程的建议:我觉得老师对于pta上的题目难度可以适量的降低,但是题量可以稍微增加一些,太难的题目对打击学生的自信心,而相较于基础的题目更能让我们巩固基础知识,循序渐进。

posted @ 2021-10-15 20:24  爱吃巧克力慕斯蛋糕  阅读(52)  评论(0)    收藏  举报