OO第一单元总结

Unit1

综述

第一单元的任务主要是进行包含幂运算的表达式化简,在随后几次的迭代开发后可以实现支持幂函数,括号嵌套,有限个自定义函数(及其嵌套),三角函数与求和函数。主体架构采用递归下降算法拆分表达式为Expr(表达式)、Item(项)、Factor(因子),后对于拆分好的字符串进行计算、化简与合并同类项。

一.程序架构分析

1.第一次作业

第一次作业的程序框架如下:

  • 内容简述

    第一次作业是处理简单的单层括号运算问题,实现一个包含加、减、乘与乘方的表达式展开与化简,允许出现至多一层括号.

  • 代码结构简述

    将表达式通过MainClass中定义的Parse函数进行解析化简,是表达式符合正常输入处理的格式,再通过梯度下降法将表达式一步步分解成Expr,Item,Variable,最后完成对Variable的合并输出.

  • 复杂度分析

    Method CogC ev(G) iv(G) v(G)
    homework.homeworkone.Expr.Expr(String) 0.0 1.0 1.0 1.0
    homework.homeworkone.Expr.getItems() 0.0 1.0 1.0 1.0
    homework.homeworkone.Expr.getString() 0.0 1.0 1.0 1.0
    homework.homeworkone.Expr.getVariables() 0.0 1.0 1.0 1.0
    homework.homeworkone.Expr.isItem(String, int, int) 2.0 1.0 4.0 4.0
    homework.homeworkone.Expr.setItems(ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworkone.Expr.setString(String) 0.0 1.0 1.0 1.0
    homework.homeworkone.Expr.setVariables(ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworkone.Expr.splitToTerm(String) 19.0 1.0 12.0 16.0
    homework.homeworkone.Item.calItem() 16.0 1.0 6.0 6.0
    homework.homeworkone.Item.getOperation() 0.0 1.0 1.0 1.0
    homework.homeworkone.Item.getVariables() 0.0 1.0 1.0 1.0
    homework.homeworkone.Item.isPos() 0.0 1.0 1.0 1.0
    homework.homeworkone.Item.Item(String, boolean) 0.0 1.0 1.0 1.0
    homework.homeworkone.Item.setOperation(String) 0.0 1.0 1.0 1.0
    homework.homeworkone.Item.setPos(boolean) 0.0 1.0 1.0 1.0
    homework.homeworkone.Item.setVariables(ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworkone.Item.splitItem() 19.0 1.0 9.0 11.0
    homework.homeworkone.MainClass.delZero(String) 17.0 1.0 9.0 11.0
    homework.homeworkone.MainClass.isNeg(String, int) 3.0 1.0 4.0 4.0
    homework.homeworkone.MainClass.isPos(String, int) 2.0 1.0 3.0 3.0
    homework.homeworkone.MainClass.main(String[]) 0.0 1.0 1.0 1.0
    homework.homeworkone.MainClass.output(ArrayList) 7.0 2.0 5.0 5.0
    homework.homeworkone.MainClass.parse(String) 27.0 1.0 12.0 12.0
    homework.homeworkone.MainClass.sort() 8.0 4.0 5.0 5.0
    homework.homeworkone.Variable.getCoefficient() 0.0 1.0 1.0 1.0
    homework.homeworkone.Variable.getPower() 0.0 1.0 1.0 1.0
    homework.homeworkone.Variable.merge(Variable) 0.0 1.0 1.0 1.0
    homework.homeworkone.Variable.multiply(Variable) 0.0 1.0 1.0 1.0
    homework.homeworkone.Variable.multiVariables(ArrayList, ArrayList) 3.0 1.0 3.0 3.0
    homework.homeworkone.Variable.negative() 0.0 1.0 1.0 1.0
    homework.homeworkone.Variable.setCoefficient(BigInteger) 0.0 1.0 1.0 1.0
    homework.homeworkone.Variable.setPower(int) 0.0 1.0 1.0 1.0
    homework.homeworkone.Variable.toString() 14.0 2.0 7.0 8.0
    homework.homeworkone.Variable.Variable(BigInteger, int) 0.0 1.0 1.0 1.0
    homework.homeworkone.Variable.Variable(String) 2.0 1.0 2.0 2.0

    初次作业复杂度都不是很高,最高的复杂度部分为解析算式的parse函数.

2.第二次作业

第二次作业的程序框架如下:

  • 内容简述

    第二次读入一系列自定义函数的定义以及一个包含简单幂函数、简单三角函数、简单自定义函数调用以及求和函数的表达式,输出恒等变形展开所有括号后的表达式。

    在本次作业中,展开所有括号的定义是:对原输入表达式 EE恒等变形,得到新表达式 E'E′。其中,E'E′ 中不再含有自定义函数与求和函数,且只包含必要的括号

  • 代码结构简述

    将表达式通过MainClass中定义的Parse函数进行解析化简,是表达式符合正常输入处理的格式,再通过梯度下降法将表达式一步步分解成Expr,Item,Variable,最后完成对Variable的合并输出.

    由于第二次作业新加了自定义函数和求和函数以及三角函数

    首先处理自定义函数,建立了Function类利用Function类将自定义函数各项元素分割划分进行单独处理,从而转化为正常的表达式进行下一步计算.

    其次处理求和函数,求和函数的处理与自定义函数大概相同,都是分割因子单独处理,但由于求和函数输入较为单一,故没有单独建类进行计算,而是直接代换

    对于三角函数的处理需要单独定义,因为三角函数自带一层括号所以在处理时要考虑括号是三角函数带的还是表达式自带的括号.

    由于加入了三角函数所以Variable类需要新定义两个HashMap用来储存Sin和Cos,此时HashMap的int存放的是三角函数的指数,key存的是三角函数自带括号里的项.

  • 复杂度分析

    Method CogC ev(G) iv(G) v(G)
    homework.homeworktwo.Expr.Expr(String) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Expr.getItems() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Expr.getString() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Expr.getVariables() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Expr.isItem(String, int, int) 2.0 1.0 4.0 4.0
    homework.homeworktwo.Expr.setItems(ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Expr.setString(String) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Expr.setVariables(ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Expr.splitToTerm(String) 19.0 1.0 11.0 15.0
    homework.homeworktwo.Function.Function(String) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Function.getArgument() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Function.getFunction() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Function.getName() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Function.getPos() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Function.parse(String) 4.0 1.0 3.0 4.0
    homework.homeworktwo.Function.setArgument(ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Function.setFunction(String) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Function.setName(String) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Item.calItem() 16.0 1.0 6.0 6.0
    homework.homeworktwo.Item.getOperation() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Item.getVariables() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Item.isPos() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Item.Item(String, boolean) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Item.setOperation(String) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Item.setPos(boolean) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Item.setVariables(ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Item.splitItem() 24.0 1.0 15.0 17.0
    homework.homeworktwo.MainClass.delZero(String) 17.0 1.0 9.0 11.0
    homework.homeworktwo.MainClass.expand(String) 12.0 1.0 6.0 6.0
    homework.homeworktwo.MainClass.getData(ExprInput) 1.0 1.0 2.0 2.0
    homework.homeworktwo.MainClass.isNeg(String, int) 3.0 1.0 4.0 4.0
    homework.homeworktwo.MainClass.isPos(String, int) 2.0 1.0 3.0 3.0
    homework.homeworktwo.MainClass.main(String[]) 0.0 1.0 1.0 1.0
    homework.homeworktwo.MainClass.output(ArrayList) 7.0 2.0 5.0 5.0
    homework.homeworktwo.MainClass.parse(String) 39.0 1.0 16.0 18.0
    homework.homeworktwo.MainClass.replace(String, ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworktwo.MainClass.replaceAngle(String) 14.0 5.0 7.0 9.0
    homework.homeworktwo.MainClass.replaceCustom(String, ArrayList) 29.0 1.0 12.0 13.0
    homework.homeworktwo.MainClass.replaceSum(String) 23.0 1.0 9.0 11.0
    homework.homeworktwo.MainClass.sort() 9.0 4.0 7.0 7.0
    homework.homeworktwo.Variable.chooseString(StringBuilder) 5.0 1.0 5.0 5.0
    homework.homeworktwo.Variable.getCoefficient() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Variable.getCosMap() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Variable.getPower() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Variable.getSinMap() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Variable.merge(Variable) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Variable.multiply(Variable) 8.0 1.0 5.0 5.0
    homework.homeworktwo.Variable.multiVariables(ArrayList, ArrayList) 3.0 1.0 3.0 3.0
    homework.homeworktwo.Variable.negative() 0.0 1.0 1.0 1.0
    homework.homeworktwo.Variable.parseAngle(String) 7.0 3.0 3.0 4.0
    homework.homeworktwo.Variable.setCoefficient(BigInteger) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Variable.setCosMap(HashMap) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Variable.setPower(int) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Variable.setSinMap(HashMap) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Variable.toString() 32.0 1.0 15.0 16.0
    homework.homeworktwo.Variable.Variable(BigInteger, int, HashMap, HashMap) 0.0 1.0 1.0 1.0
    homework.homeworktwo.Variable.Variable(String) 4.0 1.0 4.0 4.0

    可以看到由于加了对于自定义函数和求和函数以及三角函数的处理,我们的parse解析函数的复杂度上升的很高,以及由于最小项要额外对于sin和cos进行处理,所以Variable重写的toString函数的复杂度也超过了30.

    bug分析

    本次的bug来源于两方面

    强测数据:由于新增的自定义函数与求和函数的替换忘记处理前导零所以导致bug.

    互测数据:由于忘记考虑互测数据的输入指数可以大于8而导致的数据bug.

    3.第三次作业

    第三次作业的程序框架如下:

  • 内容简述

    读入一系列自定义函数的定义以及一个包含幂函数、三角函数、自定义函数调用以及求和函数的表达式,输出恒等变形展开所有括号后的表达式。

    在本次作业中,展开所有括号的定义是:对原输入表达式 EE恒等变形,得到新表达式 E'E′。其中,E'E′ 中不再含有自定义函数与求和函数,且只包含必要的括号(定义见正确性判定部分)。

  • 代码结构简述

    将表达式通过MainClass中定义的Parse函数进行解析化简,是表达式符合正常输入处理的格式,再通过梯度下降法将表达式一步步分解成Expr,Item,Variable,最后完成对Variable的合并输出.

    本次的作业对于自定义函数和求和函数进行了规则改动,由于第二次的建类处理已经能够对于自定义函数的嵌套进行处理,所以这一次作业的主要任务就是如何化简sin和cos内部的表达式因子.

    由于我们的整个操作本来就是化简表达式的过程,所以我将化简表达式因子的步骤重复应用于sin和cos函数内部,将处理好的因子重新储存为sin函数或cos函数的key解决了sin或者cos内部嵌套表达式问题,对于表达式应额外带一层括号的问题,我的方法是如果函数内部本身是一个表达式计算,那不管它的计算结果最后是因子还是表达式,都让他统一带括号,也满足了题目条件,完成正确输出.

  • 复杂度分析

    Method CogC ev(G) iv(G) v(G)
    homework.homeworkthree.Expr.Expr(String) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Expr.getString() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Expr.getVariables() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Expr.isItem(String, int, int) 2.0 1.0 4.0 4.0
    homework.homeworkthree.Expr.setItems(ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Expr.setString(String) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Expr.setVariables(ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Expr.splitToTerm(String) 19.0 1.0 11.0 15.0
    homework.homeworkthree.Function.Function(String) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Function.getArgument() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Function.getFunction() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Function.getName() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Function.getPos() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Function.parse(String) 4.0 1.0 3.0 4.0
    homework.homeworkthree.Function.setArgument(ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Function.setFunction(String) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Function.setName(String) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Item.calItem() 16.0 1.0 6.0 6.0
    homework.homeworkthree.Item.getVariables() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Item.isPos() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Item.Item(String, boolean) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Item.setOperation(String) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Item.setPos(boolean) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Item.setVariables(ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Item.splitItem() 26.0 1.0 17.0 19.0
    homework.homeworkthree.MainClass.dealPower(String) 38.0 1.0 15.0 17.0
    homework.homeworkthree.MainClass.delZero(String) 17.0 1.0 9.0 11.0
    homework.homeworkthree.MainClass.expand(String) 12.0 1.0 6.0 6.0
    homework.homeworkthree.MainClass.getData(ExprInput) 1.0 1.0 2.0 2.0
    homework.homeworkthree.MainClass.isNeg(String, int) 3.0 1.0 4.0 4.0
    homework.homeworkthree.MainClass.isPos(String, int) 2.0 1.0 3.0 3.0
    homework.homeworkthree.MainClass.main(String[]) 0.0 1.0 1.0 1.0
    homework.homeworkthree.MainClass.output(ArrayList) 7.0 2.0 5.0 5.0
    homework.homeworkthree.MainClass.parse(String) 12.0 1.0 6.0 6.0
    homework.homeworkthree.MainClass.replace(String, ArrayList) 0.0 1.0 1.0 1.0
    homework.homeworkthree.MainClass.replaceAngle(String) 32.0 8.0 10.0 12.0
    homework.homeworkthree.MainClass.replaceCustom(String, ArrayList) 29.0 1.0 12.0 13.0
    homework.homeworkthree.MainClass.replaceSum(String) 23.0 1.0 10.0 11.0
    homework.homeworkthree.MainClass.sort() 9.0 4.0 7.0 7.0
    homework.homeworkthree.MainClass.stringSplit(String[], String) 9.0 1.0 6.0 7.0
    homework.homeworkthree.Variable.chooseString(StringBuilder) 5.0 1.0 5.0 5.0
    homework.homeworkthree.Variable.getCoefficient() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Variable.getCosMap() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Variable.getPower() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Variable.getSinMap() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Variable.merge(Variable) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Variable.multiply(Variable) 8.0 1.0 5.0 5.0
    homework.homeworkthree.Variable.multiVariables(ArrayList, ArrayList) 3.0 1.0 3.0 3.0
    homework.homeworkthree.Variable.negative() 0.0 1.0 1.0 1.0
    homework.homeworkthree.Variable.output(ArrayList) 7.0 2.0 5.0 5.0
    homework.homeworkthree.Variable.parseKey(String) 1.0 2.0 1.0 2.0
    homework.homeworkthree.Variable.setCoefficient(BigInteger) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Variable.setCosMap(HashMap) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Variable.setPower(int) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Variable.setSinMap(HashMap) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Variable.sort(Expr) 9.0 4.0 7.0 7.0
    homework.homeworkthree.Variable.toString() 32.0 1.0 15.0 16.0
    homework.homeworkthree.Variable.Variable(BigInteger, int, HashMap, HashMap) 0.0 1.0 1.0 1.0
    homework.homeworkthree.Variable.Variable(String) 13.0 1.0 8.0 9.0

    bug分析

    本次的bug来源于两方面

    强测数据:Item化简问题未考虑三角函数开头.

    互测数据:求和函数的爆int问题.

    心得体会

本单元是对于表达式计算的迭代,一个好的代码结构能够减轻很多的工作量,梯度下降法的使用和单独模块化处理使我的第二次作业和第三次作业都变得更加轻松,让我深刻体会到了面向对象编程对于后续的版本更新和迭代开发的作用,面向过程的编程免不了要不断重构更新,任务繁琐.好的代码框架让人受益良多.对于以后的作业我要更加专注于优化代码结构,让每一次的迭代变为对代码的优化而不是重新写一堆面向过程的代码.

posted @ 2022-03-26 11:25  爱学习的拾柒  阅读(14)  评论(1编辑  收藏  举报