OO第一次博客作业
(1)前言:
第一次题目集总结:第一次题目集考察的主要是Java的基础语法,题目集总共有八道题,对于我个人来说不是很难,但是有一个知识点我不是很清楚,导致我有一道题卡了很久。这是我们学习面向对象程序设计所做的第一个题目集,老师并没有将题目集的难度设置的特别大,但想要做完还是要花些时间的。我感觉这次题目集的意义就是让我们对于Java的基础语法有一点了解,对于新手来说这是一场从C语言到Java的一次过渡。
第二次题目集总结:第二次题目集考察的是Java中字符串和字符数的使用,还有Java中的方法(类似C语言中的函数),题量就五道题,如果说前面两道题考察的是知识点的话,那么后面那三道题考察的就是同学们的细心与耐心了。这次题目集我是中午开始写的,到晚上就写好了。前两道题大概用了一个半小时,后三题大概花了四五个小时,主要是很多细节的地方没有注意到,以至于很多bug找了很长时间。这次题目集的思路并不难,但想要很快的完成倒是难度不小。
第三次题目集总结:第三次题目集考察的是Java中类的设计与使用,题量虽然比较少就三道题,但第三题的难度却是很大。前面两道题我一小时之内就写完了,但是写第三题却是花了我很长时间,我从早上的九点中开始构思,写到下午的三点中才写完大概,到四点钟接着写,写到晚上也没有完成。就得了35分。第二天晚上写到了45分。这次题集的难度还是比较大的,很多同学都没完成。
(2)设计与分析:
第一次题目集:
这是第一次题目集的第七题,编写Java程序实现对输入数的排序与输出。
通过PowerDesigner的逆向工程生成的类图分析可以看出只用了一个主方法解决问题。
通过SourceMonitor的生成报表内容分析出代码的效率也不是很高,由图可以得出该代码的平均复杂度为3,分支语句的百分比为14.3,最大复杂度为3,最复杂的方法有七行代码。本题采用了Java
Arrays中的sort()方法,提高了代码的简洁性和可读性。
由Kiviat Graph可以看出在方法的使用上面做的不是很好,代码的整体协调性比较低,在注释的使用这一块尤为突出。
package 第一次题目集;
import java.util.Scanner;
import java.util.Arrays;
public class Main7 {
public static void main(String[] args){
Scanner input = new Scanner (System.in);
int a = input.nextInt();
int[] arr = new int[a];
for(int i = 0;i<a;i++){
arr[i] = input.nextInt();
}
Arrays.sort(arr,0,a);
System.out.print("The sorted numbers are:");
for(int k=0;k<a;k++)
System.out.print(arr[k] + " ");
}
}
这是第一次题目集的第八题,编写Java程序判断输入的有关三角形的数据,输出该数据能否组成三角形并判断是什么三角形。
通过PowerDesigner的逆向工程生成的类图可以看出解决该问题我依旧是只用了一个主方法。
通过SourceMonitor的生成报表内容可以分析出该代码的平均复杂度为18,分支语句的使用百分比为34.8,最大复杂度为18。由提交的代码可以看出,由于if,else的多次使用导致代码的复杂度过高,最大复杂度都达到了18。由Kiviat Graph可以看出该程序的复杂度过高了,并且方法的使用也没到位。
package 第一次题目集; import java.util.Scanner; public class Main8 { public static void main(String[] args) { Scanner input = new Scanner(System.in); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); if (a < 1 || a > 200 || b < 1 || b > 200 || c < 1 || c > 200) System.out.println("Wrong Format"); else if (a + b <= c || a + c <= b || b + c <= a || a - b >= c || a - c >= b || b - c >= a || b - a >= c || c - a >= b || c - b >= a) System.out.println("Not a triangle"); else if (a == b || b == c || a == c) { if (a == b && a == c) System.out.println("Equilateral triangle"); else if (a * a + b * b - c * c < 0.00000001 || a * a + c * c - b * b < 0.00000001 || b * b + c * c - a * a < 0.0000001) System.out.println("Isosceles right-angled triangle"); else if (a == b || a == c || b == c) System.out.println("Isosceles triangle"); } else if (a * a + b * b == c * c || b * b + c * c == a * a || a * a + c * c == b * b) System.out.println("Right-angled triangle"); else System.out.println("General triangle"); } }
第二次题目集:
本次题目的要求是编写程序计算输入日期的下一天
通过PowerDesigner的逆向工程生成的类图可以看出该类用了四个方法,分别是判断输入的年份是否是润年,第二个方法是判断输入的日期是否合法,第三个方法是计算下一天,最后一个是主方法。
通过SourceMonitor的生成报表内容可以分析出该代码的平均复杂度为14,分支语句的使用百分比为44.4,最大复杂度为25。由于在public static boolean checkInputValidity(int year, int month, int day)方法和 public static void nextDate(int year, int month, int day)方法中使用了大量的if,else语句,使得该代码的最大复杂度达到了25。主要是一开始判断日期合法性的时候没有想到用数组来储存最大天数,所以导致代码的复杂度过高。不然的话用一个if()语句就可以解决日期合法问题。由Kiviat Graph可以看出该代码较前面的代码有了一些提升,但在注释方面和类的使用方面还是有不足。
package 第二次题目集; import java.util.Scanner; public class Main4 { public static boolean isLeapYear(int year) {// 判断year是否为闰年,返回boolean类型; boolean nm = true; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) nm = true; else nm = false; return nm; } public static boolean checkInputValidity(int year, int month, int day) {// 判断输入日期是否合法,返回布尔值 boolean u = true; if (year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31) { u = false; } if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) if (day > 31) { u = false; } if (month == 4 || month == 6 || month == 9 || month == 11) if (day > 30) { u = false; } if (month == 2) { if (isLeapYear(year)) { if (day > 29) { u = false; } } else if (day > 28) { u = false; } } return u; } public static void nextDate(int year, int month, int day) { // 求输入日期的下一天 if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10) { if (day == 31) { System.out.print("Next date is:" + year + "-" + (month + 1) + "-1"); } else System.out.print("Next date is:" + year + "-" + month + "-" + (day + 1)); } else if (month == 4 || month == 6 || month == 9 || month == 11) { if (day == 30) { System.out.print("Next date is:" + year + "-" + (month + 1) + "-1"); } else System.out.print("Next date is:" + year + "-" + month + "-" + (day + 1)); } else if (month == 12) { if (day == 31) System.out.print("Next date is:" + (year + 1) + "-1-1"); else System.out.print("Next date is:" + year + "-" + month + "-" + (day + 1)); } else if (month == 2) { if (isLeapYear(year)) { if (day == 29) System.out.print("Next date is:" + year + "-" + (month + 1) + "-1"); else System.out.print("Next date is:" + year + "-" + month + "-" + (day + 1)); } else { if (day == 28) System.out.print("Next date is:" + year + "-" + (month + 1) + "-1"); else System.out.print("Next date is:" + year + "-" + month + "-" + (day + 1)); } } } public static void main(String[] args) { Scanner input = new Scanner(System.in); int year = input.nextInt(); int month = input.nextInt(); int day = input.nextInt(); if (!(checkInputValidity(year, month, day))) { System.out.print("Wrong Format"); System.exit(0); } nextDate(year, month, day); } }
本次题目的要求是就输入日期的前N天
通过PowerDesigner的逆向工程生成的类图可以看出该类用了四个方法,第一个方法是判断输入的年份是否是润年,第二个检查日期是否合法,第三个则是计算该日期的前N天,第四个就是主函数了。该题目的难点在于判断的复杂程度,要对不同月份的前N天进行判断,还要对前N天是否跨年进行判断。
通过SourceMonitor的生成报表内容可以分析出该代码的平均复杂度为19.5,分支语句的使用百分比为41.4,最大复杂度为47。平均复杂度对于最大复杂度来说不算很高, 由于public static boolean checkInputValidity(int year, int month, int day)方法和public static void ChangeDate(int year, int month, int day, int n)方法的同时使用,大量的判断语句导致了最大复杂度的提高。主要是没有想到更为合理的算法来计算输入日期的下一天,所以用了大量的if语句。由Kiviat Graph可以看出该代码在注释方面和类的使用方面还是有不足。
package 第二次题目集; import java.util.Scanner; public class Main5 { public static boolean isLeapYear(int year) {// 判断year是否为闰年,返回boolean类型; boolean nm = true; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) nm = true; else nm = false; return nm; } public static boolean checkInputValidity(int year, int month, int day) {// 判断输入日期是否合法,返回布尔值 boolean u = true; if (year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31) { u = false; } if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) if (day > 31) { u = false; } if (month == 4 || month == 6 || month == 9 || month == 11) if (day > 30) { u = false; } if (month == 2) { if (isLeapYear(year)) { if (day > 29) { u = false; } } else if (day > 28) { u = false; } } return u; } public static void ChangeDate(int year, int month, int day, int n) { if (n < 0) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10) { if ((day - n) > 31) { System.out.print(n + " days ago is:" + year + "-" + (month + 1) + "-" + (day - n - 31)); System.exit(0); } else { System.out.print(n + " days ago is:" + year + "-" + month + "-" + (day - n)); System.exit(0); } } else if (month == 4 || month == 6 || month == 9 || month == 11) { if ((day - n) > 30) { System.out.print(n + " days ago is:" + year + "-" + (month + 1) + "-" + (day - n - 30)); System.exit(0); } } else if (month == 2) { if (isLeapYear(year)) { if ((day - n) > 29) { System.out.print(n + " days ago is:" + year + "-" + (month + 1) + "-" + (day - n - 29)); System.exit(0); } } else { if ((day - n) > 28) { System.out.print(n + " days ago is:" + year + "-" + (month + 1) + "-" + (day - n - 28)); System.exit(0); } } } else if (month == 12) { if ((day - n) > 31) { System.out.print(n + " days ago is:" + (year + 1) + "-1" + "-" + (day - n - 31)); System.exit(0); } } } else if (n == 0) { System.out.print(n + " days ago is:" + year + "-" + month + "-" + day); System.exit(0); } else if (n > 0) { if (month == 1) { if ((day - n) <= 0) { System.out.print(n + " days ago is:" + (year - 1) + "-12" + "-" + (day - n + 31)); System.exit(0); } else { System.out.print(n + " days ago is:" + year + "-" + month + "-" + (day - n)); System.exit(0); } } else if (month == 3) { if ((day - n) <= 0) { if (isLeapYear(year)) { System.out.print(n + " days ago is:" + year + "-2" + "-" + (day - n + 29)); System.exit(0); } else { System.out.print(n + " days ago is:" + year + "-2" + "-" + (day - n + 28)); System.exit(0); } } else { System.out.print(n + " days ago is:" + year + "-" + month + "-" + (day - n)); System.exit(0); } } else if (month == 2 || month == 4 || month == 6 || month == 8 || month == 8 || month == 9 || month == 11) { if ((day - n) <= 0) { System.out.print(n + " days ago is:" + year + "-" + (month - 1) + "-" + (day - n + 31)); System.exit(0); } else { System.out.print(-n + " days ago is:" + year + "-" + month + "-" + (day - n)); System.exit(0); } } else if (month == 5 || month == 7 || month == 10 || month == 12) { if ((day - n) <= 0) { System.out.print(n + " days ago is:" + year + "-" + (month - 1) + "-" + (day - n + 30)); System.exit(0); } else { System.out.print(n + " days ago is:" + year + "-" + month + "-" + (day - n)); System.exit(0); } } } } public static void main(String[] args) { Scanner input = new Scanner(System.in); int year = input.nextInt(); int month = input.nextInt(); int day = input.nextInt(); int n = input.nextInt(); if (!(checkInputValidity(year, month, day))) { System.out.print("Wrong Format"); System.exit(0); } ChangeDate(year, month, day, n); } }
第三次题目集:
本题的需求是判断日期的合法性并输出下一天。
通过PowerDesigner的逆向工程生成的类图可以看出解决该问题用了两个类,一个是主类,里面包含了一个主方法,另一个是Data类,在这个类中,定义了了三个私有变量,使用定义public void set(int year, int month, int day)方法向Data类里面传入日期,再通过定义 public int get1(), public int get2()和 public int get3()方法将类里面的日期返还给主函数,通过定义 public boolean isLeapYear(int year)方法判断年份是否为闰年,通过定义 public boolean checkInputValidity()方法判断日期是否合法,一切准备就绪后再由主函数计算下一天。
通过SourceMonitor的生成报表内容可以分析出该代码的平均复杂度为7.29,分支语句的使用百分比为37.4,最大复杂度为26。由于计算下一天是需要判断语句比较少,所以复杂度较前一题来说降低了。但仔细分析过后发现代码还是有些冗余,例如计算部分不应该放在主函数中,判断日期的合法性算法还是太过复杂。
package 第三次题目集; import java.util.Scanner; class Date { private int year; private int month; private int day; public void set(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int get1() { return year; } public int get2() { return month; } public int get3() { return day; } public boolean isLeapYear(int year) {// 判断year是否为闰年,返回boolean类型; boolean nm = true; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) nm = true; else nm = false; return nm; } public boolean checkInputValidity() {// 判断输入日期是否合法,返回布尔值 boolean u = true; if (year < 1900 || year > 2000 || month < 1 || month > 12 || day < 1 || day > 31) { u = false; } if (month == 4 || month == 6 || month == 9 || month == 11) if (day > 30) { u = false; } if (month == 2) { if (isLeapYear(year)) { if (day > 29) { u = false; } } else if (day > 28) { u = false; } } return u; } } public class Main2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); Date kj = new Date(); kj.set(input.nextInt(), input.nextInt(), input.nextInt()); int year = kj.get1(); int month = kj.get2(); int day = kj.get3(); if (!kj.checkInputValidity()) { System.out.print("Date Format is Wrong"); System.exit(0); } if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10) { if (day == 31) { System.out.print("Next day is:" + year + "-" + (month + 1) + "-1"); } else System.out.print("Next day is:" + year + "-" + month + "-" + (day + 1)); } else if (month == 4 || month == 6 || month == 9 || month == 11) { if (day == 30) { System.out.print("Next day is:" + year + "-" + (month + 1) + "-1"); } else System.out.print("Next day is:" + year + "-" + month + "-" + (day + 1)); } else if (month == 12) { if (day == 31) System.out.print("Next day is:" + (year + 1) + "-1-1"); else System.out.print("Next day is:" + year + "-" + month + "-" + (day + 1)); } else if (month == 2) { if (kj.isLeapYear(year)) { if (day == 29) System.out.print("Next day is:" + year + "-" + (month + 1) + "-1"); else System.out.print("Next day is:" + year + "-" + month + "-" + (day + 1)); } else { if (day == 28) System.out.print("Next day is:" + year + "-" + (month + 1) + "-1"); else System.out.print("Next day is:" + year + "-" + month + "-" + (day + 1)); } } input.close(); } }
本题的需求是求输入多项式的导数,是目前题目集中难度最大的一题。
通过PowerDesigner的逆向工程生成的类图可以看出解决这个问题用了两个类。第一个类主要是判断输入的表达式是否合法,并输出不合法的结果。第二个类的内容比较多,处理判断,计算,输出。
通过SourceMonitor的生成报表内容可以分析出该代码的平均复杂度为18,分支语句的使用百分比为28.6,最大复杂度为18。本题的算法难度较大,先是判断该表达式是否合法,再用字符串的替代方法将表达式中的空格全都去除。再将表达式分割成多个单项式,接着对单向式的正负进行判断,再判断单项式的类型,接着提取系数,再计算,通过前面对单向式正负的判断输出相应的正负号和表达式。由于该算法的写法比较简洁,算法复杂度并不高。但由于算法比较难,相应的注释和方法的使用还是有些欠缺。
package 第三次题目集; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.math.BigInteger; class Find { public Find(String s) { String str1 = "(([+-]?[1-9][0-9]*)?" + "(\\*?[+-]?x(\\^([+-]?[1-9][0-9]*))?)?)+"; if (Pattern.matches("[0-9]*", s)) { System.out.print("0"); System.exit(0); } if (!Pattern.matches(str1, s)) { System.out.print("Wrong Format"); System.exit(0); } } } public class Main3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str = input.nextLine(); String s = str.replaceAll("\\s+", ""); s = s.replace("[+-]?[0-9]+", ""); new Find(s); String str2 = "[-]*[+]*([1-9]+[0-9]*)*[*]*[x]([\\^][-]*[+]*)*([1-9]+[0-9]*)*|[-]*[+]*[0-9]*"; Pattern p = Pattern.compile(str2); Matcher m = p.matcher(s); int j = -1; String b1 = "0", b2 = "0"; String[] string = new String[30]; while (m.find()) { if (Pattern.matches("[+-]?[0-9]+", m.group()) || m.group() == null) { continue; } j++; string[j] = m.group(); } int x = j; for (j = 0; j < x; j++) { int flag1 = 0, flag2 = 0, sign1 = 0, sign2 = 0; if (string[j].matches("([-][1-9][0-9]*\\*x)?" + "(\\*?([-]x)?(\\^([+]?[1-9][0-9]*))?)?")) { flag1 = 1; } else if (string[j].matches("([-][1-9][0-9]*\\*x)?" + "(\\*?([-]x^)?((\\^)?([-][1-9][0-9]*))?)?")) { flag2 = 1; } else if (string[j].matches("([+]?[1-9][0-9]*\\*x)?" + "[+]?(x)?\\^([-][1-9][0-9]*)")) { flag1 = flag2 = 1; } // System.out.println(flag1 + " "+ flag2); if (string[j].matches("[-]*[+]*\\d+\\*x")) {// 指数为一,系数不为一 string[j] = string[j].replace("*x", ""); string[j] = string[j].replaceAll("[-+]?", ""); sign2 = 1; } else if (string[j].matches("[-]*[+]*x")) {// 指数和系数都为一 string[j] = "1"; sign2 = 1; } else if (string[j].matches("[+]?[-]?[0-9]*[*]?x\\^[+]?[-]?[0-9]*")) { String b = string[j]; String b3 = b.replaceAll("[-]*[+]*[0-9]*[*]?x\\^[-+]?", "");// 指数 String b4 = b.replaceAll("[*]?x\\^[-]*[+]*[0-9]*", "");// 系数 b4 = b4.replaceAll("[+-]?", ""); BigInteger w3 = new BigInteger(b3);// 指数 BigInteger w2 = new BigInteger("1"); BigInteger w = new BigInteger("1");// 系数 if (b4.equals("")) { w = w3.multiply(w); } else { BigInteger w4 = new BigInteger(b4); w = w3.multiply(w4); } b1 = String.valueOf(w); if (flag2 == 1) { w3 = w3.add(w2); } else { w3 = w3.subtract(w2); } b2 = String.valueOf(w3); sign1 = 1; } // 开始输出// if (flag1 == 1) { System.out.print("-"); } else if (flag1 == 0 && j != 0 && sign2 == 0 && !b1.equals("1") && !b2.equals("0")) { System.out.print("+"); } if (sign1 == 0) { System.out.print(string[j]); } else if (sign1 == 1 && !b1.equals("1") && !b2.equals("0")) { System.out.print(b1 + "*x"); } else if (b1.equals("1") && !b2.equals("0")) { System.out.print("1"); } else if (b2.equals("0")) { System.out.print(b1); } if (!b2.equals("1") && sign1 == 1 && !b2.equals("0")) { if (flag2 == 1) { System.out.print("^-" + b2); } else if (flag2 == 0) { System.out.print("^" + b2); } } } } }
(3)踩坑心得:对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空
(1)在题目集一中对直角三角形的判断不正确导致卡了很久,因为在平方后会出现精度丢失的情况,所以必须采用如下的形式判断。
(2)在写题目集二的最后一题时。太着急,然后代码的复杂程度也比较大。不但写了垃圾代码,并且成功的效率还比较低。再要写代码的时候,先构思出大概的代码的结构,不断的去优化脑子中的代码思路,等到可以写出非常优美的代码的时候,才可以动手去写。
(4)改进建议:
(1):在题目集二中关于字符串的合并写的代码太过冗余,后面用排序算法几行就解决了问题。
import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String[] args){ Scanner input = new Scanner (System.in); int a = input.nextInt(); int[] arr = new int[a]; for(int i = 0;i < a;i++) arr[i] = input.nextInt(); int b = input.nextInt(); int[] brr = new int[b]; for(int i = 0;i < b;i++) brr[i] = input.nextInt(); int c = a + b; int[] crr = new int[c]; for(int i = 0;i < c;i++){ if(i < a) crr[i] = arr[i]; else crr[i] = brr[i-a]; } // int x = 0,y = 0; // for(int i = 0;i < c;i++){ // if(x < a&&y < b){ // if(arr[x] < brr[y]){ // crr[i] = arr[x]; // x++; // } // else if(arr[x] > brr[y]){ // crr[i] = brr[y]; // y++; // } // else{ // crr[i] = arr[x]; // i++; // crr[i] = arr[x]; // x++; // y++; // } // } // else if(x == a && y < b){ // crr[i] = brr[y]; // y++; // } // else if(y == b && x < a){ // crr[i] = arr[x]; // x++; // } // } Arrays.sort(crr); for(int i = 0;i < c;i++) System.out.print(crr[i] + " "); } }
(2)由于判断日期合法的算法写的太冗余之后,又将判断日期的算法重新又优化了一遍,使用数组储存月份中的最大电路,然后判断2月的日期,然后直接用一个if语句就可以把问题解决。
public boolean checkInputValidity() { boolean y =true ; int[] mon_maxnum= new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31}; if(isLeapYear(year)) { mon_maxnum[2] = 29; } if(year < 1900 ||year > 2000 || month < 1|| month > 12 || day > mon_maxnum[month]){ y = false; }else { y = true; } return y; }
(3)在一开始写这道题的时候,由于急于求成,采用了非常野蛮的方式,尽管写到了45分,但是足足写了有300行,并且可读性非常差,这些垃圾代码自己看得都不顺眼。到后面自己又重新写了一遍,将之前的if else的复杂的判断与标记全部都删除了。衣服的作用只是判断它的正负号,并且判断它的形式。主要的匹配还是用正则表达示。花了挺长时间去研究正则表达式,匹配它的正负号,匹配它的系数匹配它的指数。然后再用替换函数将正负号去除,再将其余的东西去除,然后就可以提取出它的指数和系数。之后再判断,计算就可以了。不过又了解到一个同学是用字符串的相关方法直接提取字符串中的内容。再将他赋值给Biginteger的对象,再计算就可以使代码非常简洁用了优化过后的算法写到了100行之内,优化后的代码在分析题目集三有粘贴。
import java.util.*; import java.util.regex.Pattern; import java.util.regex.Matcher; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); if(input.hasNext()) { String str = input.nextLine(); Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if( isNum.matches() ){ System.out.print("0"); System.exit(0); } String s = str.replaceAll("\\s+",""); char[] arr = s.toString().toCharArray(); int nmd=0; for(int y = 0;y<arr.length;y++){ if(arr[y] <='9' && arr[y] >= '0') nmd++; } if(nmd==(arr.length-1)){ System.out.print("0"); System.exit(0); } int sign2 = 0,sign=0,sign3 = 0; for(int i = 0; i < arr.length;i++) {//开始操作 int flag1 = 0, flag3 = 0, flag4 = 0,flag6 = 0,flag7=0,flag5=0,flag2=0; int num = i; long sum = 0, sum2 = 0; if(arr[0] == '0') { System.out.print("Wrong Format"); System.exit(0); }for(int x=0;x<arr.length;x++){//................系数为0; if(arr[x]=='+'||arr[x]=='-') if(arr[x+1] =='0') { System.out.print("Wrong Format"); System.exit(0); } } //开始遍历 if(arr[i] == '-') {//有负号_1 i++; flag1 = 1; }else if(arr[i] == '+') i++; while(arr[i] <='9' && arr[i] >= '0') { flag2++; if(i<arr.length-1) { i++; if(i==arr.length-1) {//后常数项 if(sign2 == 0) System.out.print("0"); System.exit(0); } } if(arr[i]=='+'||arr[i]=='-') {//常数项 flag5=1; } }if(flag5==1) { i--; sign =1 ; continue; } if(arr[i] == 'x'){//系数为+-1 if(i==arr.length-1) { System.out.print(arr[num] + "1"); System.exit(0); } i++; flag7 = 1; if(arr[i]=='+'||arr[i]=='-') { System.out.print(arr[num]); System.out.print("1"); i--; continue; } } if(arr[i] == '*' && arr[i+1] == 'x') {//导数不为0 i += 2; if(i==arr.length) { if(sign3 == 0) System.out.print(arr[num]); num++; while(arr[num] <='9' && arr[num] >= '0') { System.out.print(arr[num]); if(num<i-2) num++; } System.exit(0); } } else if(arr[i]!='^'&&arr[i]!='+'){ sign3 = 1; i--; continue; } if(i<arr.length) { if(arr[i] == '^') {//次方位x_4 i++; flag4 = 1; } if(arr[i] == '0' && flag4 == 1){//指数为0 System.out.print("Wrong Format"); System.exit(0); } else if(arr[i] == '+' && flag4 == 1) { i++; flag3 = i; while(arr[i] <='9' && arr[i] >='0') { i++; } }else if(arr[i] == '-' && flag4 == 1) { i++; if(flag1==1) flag1=0; else flag1 = 1; flag6 = 1;//指数为负 flag3 = i;//起始点 while(arr[i] <='9' && arr[i] >= '0'&&i < arr.length-1) { i++; } if(arr[i] >'9' || arr[i] < '0') { i--; } } else if(arr[i] <='9' && arr[i] >= '0'){ flag3=i; while(arr[i] <='9' && arr[i] >= '0'&&i < arr.length-1) { i++; } if(arr[i] =='+' || arr[i] == '-') {/////////////... i--; } }else if(flag4 == 0) {//无指数 System.out.print(arr[num]); num++; if(flag7==1&&arr[num]=='x') { System.out.print("1"); i--; } else { while(arr[num] <='9' && arr[num] >= '0') { System.out.print(arr[num]); num++; if(arr[num]=='*'){ break; } } }i--; continue; } ////////////////////////////////输出///////////////////////////// if(sign3==1) {//一次方 num++; sign3 =0; } if(num==0) {//首项 if(flag1==0) { if(flag6==0&&arr[num]=='+') { num++; }else if(flag6==1&&arr[num]=='-') { num++; } }else { if(flag6==0) { System.out.print("-"); num++; }else if(arr[num]=='+'){ System.out.print("-"); num++; }else { System.out.print("-"); } } } else if(sign==1) {//伪首项 sign=0; if(flag1==0) { if(flag6==0&&arr[num]=='+') { num++; }else { num++; } }else { if(flag6==0) { System.out.print("-"); num++; }else if(arr[num]=='+'){ System.out.print("-"); num++; }else { System.out.print("-"); } } } else{//非首项 if(flag1==0) { System.out.print("+"); num++; }else { System.out.print("-"); num++; } } if(flag7==1) { sum=1; while(arr[flag3] <='9' && arr[flag3] >= '0') { sum2 = sum2*10 + (arr[flag3]-'0')*10; if(flag3 < arr.length-1) flag3++; else if(flag3 == arr.length-1) { break; } } sum2 /= 10; System.out.print(sum*sum2); }else if(flag2<=10) {//大数判断 if(arr[num] <= '9' && arr[num] >= '0') { while(arr[num] <='9' && arr[num] >= '0') { sum = sum*10+(int)(arr[num]-'0')*10; num++; } sum /= 10; } while(arr[flag3] <='9' && arr[flag3] >= '0') { sum2 = sum2*10 + (arr[flag3]-'0')*10; if(flag3 < arr.length-1) flag3++; else if(flag3 == arr.length-1) { break; } } sum2 /= 10; System.out.print(sum*sum2); } else {//大数计算 int x =0; if(arr[num] <= '9' && arr[num] >= '0') { while(arr[num] <='9' && arr[num] >= '0'&&x<=7) { x++; sum = sum*10+(int)(arr[num]-'0')*10; num++; } sum /= 10; } long sum3=0; if(arr[num] <= '9' && arr[num] >= '0') { while(arr[num] <='9' && arr[num] >= '0') { sum3 = sum3*10+(int)(arr[num]-'0')*10; num++; } sum3 /= 10; } while(arr[flag3] <='9' && arr[flag3] >= '0') { sum2 = sum2*10 + (arr[flag3]-'0')*10; if(flag3 < arr.length-1) flag3++; else if(flag3 == arr.length-1) { break; } } sum2 /= 10; System.out.print((int)sum*sum2+sum2*sum3/(int)Math.pow(10,flag2-8)); System.out.print(sum2*sum3%(int)Math.pow(10,flag2-8)); } System.out.print("*x"); if(flag4 == 1) { if(flag6 == 1) { System.out.print("^-"); System.out.print(sum2+1);//注意,测试 sign2 = 1; }else { System.out.print("^"); System.out.print(sum2-1);//注意,测试 sign2 = 1; } } } //System.out.println(i); } } } }
(5)总结:这三次题目集对我的帮助非常大,首先是让我学会了java的基本语法,然后再学会了类,然后对面向对象程序设计有一定初步的了解,但是对于类的使用,方法的使用,类的设计,还需要进一步的学习和研究。老师上课风趣幽默,会讲许多课堂外面的有关就业,以后方向的问题。对许多东西他的教学方式他也是非常的独特,他只是上课给你开个头,然后学的东西都是要下课去学,很多东西他也不讲,比如说正则表达式他就不讲。我觉得实验的话有点鸡肋,为什么不能放在题目集里一起让我们做呢?然后课上的话,我觉得老师可以再讲的细节一点。很多东西老师只是给我们讲解了一点,但是基础比较差的同学还是一点头绪都没有。希望老师能看到我这个建议,在以后的今后的课程当中,能够讲的更加细一点,讲的速度慢一点。