软件工程个人作业01

Posted ╄冷丶夜♂

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件工程个人作业01相关的知识,希望对你有一定的参考价值。

(已改编为网页版)

程序设计思想:

创建表达式类Expression、运算数类RationalNumber、表达式随机生成器类ExpressionRandom、表达式组成元素类Element

每个Element由一个运算数(RationalNumber) + 一个运算符(String)构成

对每一个表达式,按照以下规则将其拆分:

如表达式“2+3-4/7

 一个运算数+一个运算符作为一个元素(Element,详见Element.java),如即2+、3-

每个表达式为2个或三个Element构成的列表(List),最后一个元素(Element)没有跟着的运算符,补充"#"即表达式“2+3-4/7”由一个列表{2+、3-、4/7#}表示。

源程序代码:

RationalNumber.java

  1 /*******运算数类*******/
  2 
  3 package model;
  4 public class RationalNumber {
  5     private int denominator;    //分母
  6     private int numerator;        //分子
  7     
  8     /***构造函数,整数看做分母为1的分数***/
  9     public RationalNumber(int numerator) {
 10         this.numerator = numerator;
 11         denominator = 1;
 12     }
 13     /****构造函数,第一个参数为分子,第二个为分母****/
 14     public RationalNumber(int numerator,int denominator) {
 15         this.denominator = denominator;
 16         this.numerator = numerator;
 17     }
 18     public int getDenominator() {
 19         return denominator;
 20     }
 21     public void setDenominator(int denominator) {
 22         this.denominator = denominator;
 23     }
 24     public int getNumerator() {
 25         return numerator;
 26     }
 27     public void setNumerator(int numerator) {
 28         this.numerator = numerator;
 29     }
 30     
 31     /***判断两个分数是否相等***/
 32     @Override
 33     public boolean equals(Object obj) {
 34         // TODO Auto-generated method stub
 35         RationalNumber rn = (RationalNumber)obj;
 36         if(this.denominator == rn.denominator && this.numerator == rn.numerator) {
 37             return true;
 38         }else return false;
 39     }
 40     
 41     /*****分数相加*****/
 42     public RationalNumber plus(RationalNumber rb) {
 43         int addNum1 = this.numerator * rb.denominator;
 44         int addNum2 = rb.numerator * this.denominator;
 45         int demo = this.denominator * rb.denominator;
 46         RationalNumber value = new RationalNumber(addNum1 + addNum2,demo);
 47         value.simply();
 48         return value;
 49     }
 50     
 51     /*****分数相减*****/
 52     public RationalNumber minus(RationalNumber rb) {
 53         int addNum1 = this.numerator * rb.denominator;
 54         int addNum2 = rb.numerator * this.denominator;
 55         int demo = this.denominator * rb.denominator;
 56         RationalNumber value = new RationalNumber(addNum1 - addNum2,demo);
 57         value.simply();
 58         return value;
 59     }
 60     
 61     /*****分数相乘*****/
 62     public RationalNumber multiply(RationalNumber rb) {
 63         RationalNumber value = new RationalNumber(this.numerator * rb.numerator,this.denominator * rb.denominator);
 64         value.simply();
 65         return value;
 66     }
 67     
 68     /*****分数相除*****/
 69     public RationalNumber divide(RationalNumber rb) {
 70         RationalNumber value = new RationalNumber(this.numerator * rb.denominator,this.denominator * rb.numerator);
 71         value.simply();
 72         return value;
 73     }
 74     
 75     /*****分数化简*****/
 76     public void simply() {
 77         if(numerator == 0) {
 78             denominator = 1;
 79             return ;
 80         }
 81         
 82         int small,big,temp;
 83         if(numerator > denominator) {
 84             small = denominator;
 85             big = numerator;
 86         }else {
 87             big = denominator;
 88             small = numerator;
 89         }
 90         while(small != 0) {
 91             temp = big % small;
 92             big = small;
 93             small = temp;
 94         }
 95         numerator /= big;
 96         denominator /= big;
 97     }
 98     
 99     /****取得字符串****/
100     @Override
101     public String toString() {
102         if(denominator == 1)
103             return String.valueOf(numerator);
104         else return String.valueOf(numerator + "/" + denominator);
105     }
106     
107     /****取得html格式字符串****/
108     public String toHtmlDiv() {
109         String str = null;
110         if(denominator == 1)
111             str = "<div class=\'nomal\'>" + this.numerator + "</div>";
112         else
113             str = "<div class=\'fraction\'><div>" + this.numerator + "</div><div style=\'border-top:2px solid black;\'>" + this.denominator + "</div></div>";
114         return str;
115     }
116 }
 1 /**每个Element由一个运算数(RationalNumber) + 一个运算符(String)构成:
 2  * 具体代码如下:
 3  */
 4 
 5 package model;
 6 
 7 public class Element{
 8     
 9     RationalNumber fraction;            //运算数
10     String operator;                    //运算符
11     //构造方法
12     public Element(RationalNumber fraction, String operator) {
13         this.fraction = fraction;
14         this.operator = operator;
15     }
16     //运算数的get/set方法
17     public RationalNumber getFraction() {
18         return fraction;
19     }
20     public void setFraction(RationalNumber fraction) {
21         this.fraction = fraction;
22     }
23     //运算符的get/set方法
24     public String getOperator() {
25         return operator;
26     }
27     public void setOperator(String operator) {
28         this.operator = operator;
29     }
30     //重载equals方法,判读两个元素是否相同
31     @Override
32     public boolean equals(Object obj) {
33         Element element = (Element)obj;
34         if(this.fraction.equals(element) && this.operator.equals(element.operator))
35             return true;
36         else return false;
37     }
38     
39     
40 }

Expression.java

  1 /**对每一个表达式,按照以下规则将其拆分:
  2  * 如表达式“2+3-4/7”
  3  * 一个运算数+一个运算符作为一个元素(Element,详见Element.java),如即2+、3-
  4  * 每个表达式为2个或三个Element构成的列表(List),
  5  * 最后一个元素(Element)没有跟着的运算符,补充"#"
  6  * 
  7  * 即表达式“2+3-4/7”由一个列表{2+、3-、4/7#}表示。
  8  */
  9 
 10 package model;
 11 
 12 import java.util.List;
 13 
 14 public class Expression {
 15     private List<Element> expr;                    //Element构成的列表,表示一个表达式
 16     
 17     public Expression(List<Element> expr) {        //构造方法,参数为List<Element>类型
 18         this.expr = expr;
 19     }
 20 
 21     //表达式列表的get/set方法
 22     public List<Element> getExpr() {
 23         return expr;
 24     }
 25 
 26     public void setExpr(List<Element> expr) {
 27         this.expr = expr;
 28     }
 29 
 30     //得到表达式字符串,如"1+2/4/7"
 31     @Override
 32     public String toString() {
 33         String exprStr = new String();
 34         for(Element e:expr) {
 35             exprStr += e.getFraction().toString();
 36             if(!e.getOperator().equals("#"))
 37                 exprStr += e.getOperator();
 38         }
 39         return exprStr;
 40     }
 41 
 42     /****取得Html格式字符串
 43      * 如"<div class=\'nomal\'>7</div><div class=\'nomal\'>×</div><div class=\'nomal\'>1</div><div class=\'nomal\'>+</div><div class=\'nomal\'>86</div><div class="nomal">=</div>"
 44      * 在html中直接加入toHtmlDiv()生成的字符串,可规范显示该表达式
 45      */
 46     public String toHtmlDiv() {
 47         String exprStr = new String();
 48         for(Element e:expr) {            //将每个元素转为html格式div,详见Element.java
 49             exprStr += e.getFraction().toHtmlDiv();
 50             if(!e.getOperator().equals("#"))
 51             {
 52                 //存运算符时以"*、/"表示乘除运算符,此处转换为"×、÷"
 53                 if(e.getOperator().equals("*"))
 54                     exprStr += "<div class=\'nomal\'>×</div>";
 55                 else if(e.getOperator().equals("/"))
 56                     exprStr += "<div class=\'nomal\'>÷</div>";
 57                 else
 58                     exprStr += "<div class=\'nomal\'>"+ e.getOperator() + "</div>";
 59             }
 60         }
 61         exprStr += "<div class=\\"nomal\\">=</div>";
 62         System.out.println(exprStr);
 63         return exprStr;
 64     }
 65     
 66     /****两个算数表达式是否相同****/
 67     @Override
 68     public boolean equals(Object obj) {
 69         Expression expr = (Expression)obj;
 70         if(this.expr.size() != expr.expr.size())
 71             return false;
 72         for(int i = 0;i < this.expr.size();i++)
 73         {
 74             if(!this.expr.get(i).equals(expr.expr.get(i)))
 75                 return false;
 76         }
 77         return true;
 78     }
 79     
 80     /****计算结果,表达式不合法时,返回null****/
 81     public RationalNumber getResult() {
 82         RationalNumber rn = null;                //结果
 83         int length = this.getExpr().size();        //运算数个数
 84         String op1 = this.getExpr().get(0).operator,op2;        //第1、2个运算符
 85         if(length == 2)                            //二元运算
 86             rn = calc(this.getExpr().get(0).fraction,op1,this.getExpr().get(1).fraction);
 87         else {
 88             op2 = this.getExpr().get(1).operator;
 89             RationalNumber rn_temp;                //中间数
 90             //运算符优先级判断
 91             if((op1.equals("+") || op1.equals("-"))&&(op2.equals("*")||op2.equals("/"))){
 92                 rn_temp = calc(this.getExpr().get(1).fraction,op2,this.getExpr().get(2).fraction);
 93                 if(rn_temp == null)return null;
 94                 //if(rn_temp == null || rn_temp.getDenominator() > 100 || rn_temp.getNumerator() > 100 || rn_temp.getDenominator() < 0 || rn_temp.getNumerator() < 0 || rn_temp.getDenominator() == 0|| (rn_temp.getDenominator() != 1 && rn_temp.getNumerator() >= rn_temp.getDenominator())) return null;
 95                 rn = calc(this.getExpr().get(0).fraction,op1,rn_temp);
 96             }else {
 97                 rn = calc(this.getExpr().get(0).fraction,op1,this.getExpr().get(1).fraction);
 98                 if(rn == null)return null;
 99                 rn = calc(rn,op2,this.getExpr().get(2).fraction);
100             }
101         }
102         
103         if(rn == null || rn.getDenominator() > 100 || rn.getNumerator() > 100 || rn.getDenominator() < 0 || rn.getNumerator() < 0 || rn.getDenominator() == 0|| (rn.getDenominator() != 1 && rn.getNumerator() >= rn.getDenominator()))
104             return null;
105         else
106             return rn;
107     }
108     
109     /****根据单个运算符计算单个结果***/
110     private RationalNumber calc(RationalNumber rn1,String o,RationalNumber rn2) {
111         RationalNumber rn = null;
112         if(rn1.getDenominator() > 100 || rn1.getNumerator() > 100 || rn1.getDenominator() < 0 || rn1.getNumerator() < 0 || rn1.getDenominator() == 0|| (rn1.getDenominator() != 1 && rn1.getNumerator() >= rn1.getDenominator()) || rn2.getDenominator() > 100 || rn2.getNumerator() > 100 || rn2.getDenominator() < 0 || rn2.getNumerator() < 0 || rn2.getDenominator() == 0 || (rn2.getDenominator() != 1 && rn2.getNumerator() >= rn2.getDenominator())){
113             rn = null;
114         }else {
115             if(o.equals("+"))
116                 rn = rn1.plus(rn2);
117             else if(o.equals("-"))
118                 rn = rn1.minus(rn2);
119             else if(o.equals("*"))
120                 rn = rn1.multiply(rn2);
121             else if(o.equals("/"))
122             {
123                 if(rn2.getNumerator() != 0)
124                     rn = rn1.divide(rn2);
125                 else return null;
126             }
127         }
128         return rn;
129     }
130 }

ExpressionRandom.java

 1 /******题目(也称为运算表达式)随机生成器*****/
 2 
 3 package model;
 4 
 5 import java.util.ArrayList;
 6 import java.util.List;
 7 
 8 public class ExpressionRandom {
 9     /***定义运算符****/
10     private static String[] operator = {"+","-","*","/"};
11     
12     /****随机得到一个表达式,elementNum为元素个数,isFraction表示是否支持分数计算*****/
13     public static Expression getExpression(int elementNum,boolean isFraction) {
14         /***表达式列表***/
15         List<Element> expr = new ArrayList<Element>();
16         int i,nume,demo;        //循环变量,分子,分母
17         /***随机生成elementNum个Element(随机分数 + 运算符),并添加到expr***/
18         for(i = 0;i < elementNum;i++) {
19             nume = (int) (Math.random() * 101);
20             if(isFraction)
21                 demo = (int) (Math.random() * 100 + 1);        //分母不能为0
22             else demo = 1;
23             String oper = operator[(int)(Math.random() * 4)];
24             RationalNumber rb = new RationalNumber(nume,demo);
25             rb.simply();
26             expr.add(new Element(rb,oper));
27         }
28         expr.get(i-1).setOperator("#");
29         /****通过expr构造运算表达式,并返回****/
30         return new Expression(expr);
31     }
32     
33     /****得到经过过滤的表达式*****/
34     public static Expression getValidExpression(int elementNum,boolean isFraction) {
35         Expression expr = null;
36         do {
37             expr = getExpression(elementNum,isFraction);
38         }while(expr.getResult() == null);                //(当表达式不合法时,返回计算结果为null)
39         return expr;
40     }
41     
42     /******测试主函数********/
43     public static void main(String[] args) {
44         for(int i = 0;i < 100;) {
45             Expression expr = ExpressionRandom.getExpression(2, true);
46             if(expr.getResult() != null) {
47                 System.out.println(expr.toString() + "=" + expr.getResult() + "\\n");
48                 i++;
49             }
50         }
51     }
52 }

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="css/style.css"/>
<title>小学生四则运算</title>

</head>
<body>
<img alt="asdfsadf" src="img/pdf.png">
    <form action="arithmetic.jsp" class="option">
        <center><h2>题目定制</h2></center>
        <p>请选择题目类型:
            <select name="elementNum">
                <以上是关于软件工程个人作业01的主要内容,如果未能解决你的问题,请参考以下文章

软件工程个人作业01

软件工程个人作业01

软件工程个人作业01

软件工程个人作业01

软件工程个人作业01

软件工程个人作业01