20150421 作业5 四则运算 测试与封装 5.1
import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class fengzhuang {// Software Engineering 軟件工程 作業2 static String jia = "+"; static String jian = "-"; static String cheng = "*"; static String chu = "/"; String shizi9 = null; RN daan9; double daan12; String shuru; double shuruZH; int zhengque, cuowu; public double strto12(String daan) { return Math.round(Double.parseDouble(daan) * 100) / 100.0; } public int panduan(String[] input, String[] Answer) { int count = 0; for (int i = 0; i < Answer.length; i++) { if (Double.parseDouble(input[i]) == Double.parseDouble(Answer[i]) || Double.parseDouble(input[i]) == Math.round(Double .parseDouble(Answer[i]) * 100) / 100.0) count++; } return count; } public void setcuowu(int cuowu) { this.cuowu = cuowu; } public void setzhengque(int zhengque) { this.zhengque = zhengque; } public int getcuowu() { return cuowu; } public int getzhengque() { return zhengque; } public boolean panduandaan() { if (daan9.getSign() == 0 && (getshuruZH() == getdaan9number() || getshuruZH() == getdaan12())) return true; else if (daan9.getSign() == 2 && getshuruZH() == getdaan9number()) return true; else return false; } public void setshuru(String shuru) { this.shuru = shuru; } public String getshuru() { return shuru; } public double str_daanTodouble_daan12(String daan) { return 1;// !!! } public void setshuruZH() { shuruZH = Input(getshuru()).getNumber(); } public double getshuruZH() { return shuruZH; } public void setshizi9(String shizi) { shizi9 = shizi; } public String getshizi9() { return shizi9; } public void setdaan12byStr(String daan) { daan12 = Math.round(Double.parseDouble(daan) * 100) / 100.0; } public void setdaan12(double daan) { daan12 = Math.round(daan * 100) / 100.0; } public double getdaan12() { return daan12; } public void setdaan9(RN daan) { daan9 = daan; setdaan12(daan9.getNumber()); } public RN getdaan9() { return daan9; } public double getdaan9number() { return daan9.getNumber(); } public RN Input(String input) { RN temp = this.new RN(); if (input.contains("/")) { temp.setMolecular(Integer.parseInt(input.substring(0, input.indexOf("/")))); temp.setDenominator(Integer.parseInt(input.substring(input .indexOf("/") + 1))); temp.setSign(2); } else { temp.setDecimal(Double.parseDouble(input)); temp.setSign(0); } return temp; } public double jiechengdaan(int n, double all) { if (n < 0) return 0; else if (n == 0) return 1; else if (n == 1) return all; else return jiechengdaan(n - 1, n * all); } public double jiecheng() { int jiecheng = new Random().nextInt(10); System.out.print(jiecheng + "!="); return jiechengdaan(jiecheng, 1); } public void fangfa(ArrayList<String> A, ArrayList<RN> O, String what) {//这里是将式子求出答案过程的方法 封装起来了!!!!! int index = A.indexOf(what); if (what.equals(cheng)) { O.set(index, new RN(O.get(index).getNumber() * O.get(index + 1).getNumber(), 0)); } else if (what.equals(chu)) { O.set(index, new RN(O.get(index).getNumber() / O.get(index + 1).getNumber(), 0)); } else if (what.equals(jia)) { O.set(index, new RN(O.get(index).getNumber() + O.get(index + 1).getNumber(), 0)); } else { O.set(index, new RN(O.get(index).getNumber() - O.get(index + 1).getNumber(), 0)); } O.remove(index + 1); A.remove(index); }//这里是将式子求出答案过程的方法 封装起来了!!!!! public RN getAnswer(RN[] Operand, String[] AS) { ArrayList<String> A = new ArrayList<String>(); ArrayList<RN> O = new ArrayList<RN>(); System.out.println(); for (String as : AS) { A.add(as); // System.out.print(as + " "); } System.out.println(); for (RN op : Operand) { O.add(op); // System.out.print(op.getNumber() + " "); } System.out.println(); while (A.get(0) != "=") { if (A.contains(cheng) || A.contains(chu)) { if (((A.contains(cheng) ^ A.contains(chu)) == true ? (A .contains(cheng) == true ? true : false) : (A .indexOf(cheng) < A.indexOf(chu) ? true : false))) fangfa(A, O, cheng);//此处调用了封装的方法 else fangfa(A, O, chu);//此处调用了封装的方法 } else if (A.contains(jia) || A.contains(jian)) { if (((A.contains(jia) ^ A.contains(jian)) == true ? (A .contains(jia) == true ? true : false) : (A .indexOf(jia) < A.indexOf(jian) ? true : false))) fangfa(A, O, jia);//此处调用了封装的方法 else fangfa(A, O, jian);//此处调用了封装的方法 } else System.out.println("error!!"); O.trimToSize(); A.trimToSize(); } return O.get(0); } public void GA() {// Generation algorithm 生成算式 int number = new Random().nextInt(4) + 2; RN[] Operand = new RN[number]; String[] AS = new String[number]; AS[number - 1] = "="; String shizi2 = ""; for (int i = 0; i < number; i++) {// 生成 number 个操作数 Operand[i] = new RN(); switch (new Random().nextInt(2) + 1) {// 這裏設置了Int(2)+1略過 小數 // ifInt(3)就會出現小數 case 0:// 小数[0~1.0) *100 do Operand[i] .setDecimal(((int) (new Random().nextDouble() * 19900)) / 100.0 - 99); while (Operand[i].getDecimal() == 0); Operand[i].setSign(0); break; case 1:// 整数[0~n) do Operand[i].setInteger(new Random().nextInt(199) - 99); while (Operand[i].getInteger() == 0); Operand[i].setSign(1); break; case 2:// 分数 do Operand[i].setMolecular(new Random().nextInt(199) - 99); while (Operand[i].getMolecular() == 0); Operand[i].setDenominator(new Random().nextInt(99) + 1); Operand[i].setSign(2); break; default: System.exit(0); } if (i > 0) switch (new Random().nextInt(4)) { case 0: AS[i - 1] = jia; break; case 1: AS[i - 1] = jian; break; case 2: AS[i - 1] = cheng; break; case 3: AS[i - 1] = chu; break; default: System.exit(0); } } for (int i = 0; i < number; i++) { shizi2 = shizi2.concat((Operand[i].getNumber() < 0 ? "(" : "") + Operand[i].toString() + (Operand[i].getNumber() < 0 ? ")" : "") + " " + AS[i] + " "); } setshizi9(shizi2); setdaan9(getAnswer(Operand, AS)); } public class F {// Fraction 分數 int Molecular;// 分子 int Denominator = 0;// 分母 void setMolecular(int Molecular) { this.Molecular = Molecular; } int getMolecular() { return Molecular; } void setDenominator(int Denominator) { this.Denominator = Denominator; } int getDenominator() { return Denominator; } double getAll() { return (double) Molecular / Denominator; } public String toString() { return Molecular + "/" + Denominator; } } public class RN {// Random Number 隨機數 double Decimal;// 小數 int Integer;// 整數 F Fraction = new F();// 分數 int Sign;// 標誌該對象中存儲的是什麼數 public RN() { } public RN(double Decimal, int Sign) { this.Decimal = Decimal; this.Sign = Sign; } void setDecimal(double Decimal) { this.Decimal = Decimal; } double getDecimal() { return Decimal; } void setInteger(int Integer) { this.Integer = Integer; } int getInteger() { return Integer; } void setMolecular(int Molecular) { this.Fraction.setMolecular(Molecular); } int getMolecular() { return this.Fraction.getMolecular(); } void setDenominator(int Denominator) { this.Fraction.setDenominator(Denominator); } int getDenominator() { return this.Fraction.getDenominator(); } void setSign(int Sign) { this.Sign = Sign; } int getSign() { return Sign; } double getNumber() { switch (Sign) { case 0: return Decimal; case 1: return Integer; case 2: return Fraction.getAll(); default: return -1; } } public String toString() { return Fraction.getDenominator() == 0 ? (Integer == 0 ? Decimal + "" : Integer + "") : Fraction.toString(); } } public static void main(String [] args){ fengzhuang test = new fengzhuang(); test.GA(); System.out.println(test.getshizi9()); Scanner in =new Scanner(System.in); //System.out.println("答案是:"+test.getdaan12()); test.setshuru(in.next()); test.setshuruZH(); if(test.panduandaan()) System.out.println("true"); else System.out.println("false"); } }
其实我这个程序很多方法都已经封装好了,因为机房没有用jsp的环境,所以我在这个java类里面随便写了个main函数来调用我的程序里面的各种方法,可能main函数里面实现的功能比较简陋。但是足以体现出封装 计算答案的那个类了。
即 fangfa(ArrayList<String>, ArrayList<RN>, String),
getAnswer(RN[], String[]) 中会频繁用到以上方法。
结对同伴:
姓名 | 学号 | 博客地址 |
叶子鹏 | 201306114420 | http://www.cnblogs.com/kazehanaai/ |
王佳宁 | 201306114434 | http://www.cnblogs.com/paopaotai/ |