四则运算-安卓版
本次作业的题目:
在之前的四则运算基础上继续团队开发,详细要求如下:
1. 生成的题目中计算过程不能产生负数,也就是说算术表达式中如果存在形如e1 ? e2的子表达式,那么e1 ≥ e2。
2、生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数。
3、每道题目中出现的运算符个数不超过3个,括号不限。
4、程序一次运行生成的题目不能重复,即任何两道题目不能通过有限次交换+和×左右的算术表达式变换为同一道题目。
5、把程序变成一个网页程序或智能手机程序, 用户通过设定参数,就可以得到各种题目,并可实现在线答题并评判。
在历经一个月的折腾后,二柱子的题目终于圆满结束。这次写的是安卓版,因为考虑到用户需求,安卓版的实用性更好,更方便!刚好这学期开设了移动应用开发的课程,顺便练习联系。关键就是把之前的C++代码改写成Java语言的代码,做成安卓app的过程。
CalculatorUtil.java文件:
1 package com.example.cal; 2 import android.annotation.SuppressLint; 3 import java.util.Stack; 4 @SuppressLint("UseValueOf") 5 public class CalculatorUtil { 6 private Stack<Character> priStack = new Stack<Character>();// 操作符栈 7 private Stack<Double> numStack = new Stack<Double>();;// 操作数栈 8 public double caculate(String str) { 9 String temp; 10 StringBuffer tempNum = new StringBuffer(); 11 StringBuffer string = new StringBuffer().append(str); 12 while (string.length() != 0) { 13 temp = string.substring(0, 1); 14 string.delete(0, 1); 15 if (!isNum(temp)) { 16 if (!"".equals(tempNum.toString())) { 17 double num=Double.parseDouble(tempNum.toString()); 18 numStack.push(num); 19 tempNum.delete(0, tempNum.length()); 20 } 21 while (!compare(temp.charAt(0)) && (!priStack.empty())) { 22 double a = numStack.pop(); 23 double b = numStack.pop(); 24 char ope = priStack.pop(); 25 double result = 0; 26 switch (ope) { 27 case '+': 28 result = b + a; 29 numStack.push(result); 30 break; 31 case '-': 32 result = b - a; 33 numStack.push(result); 34 break; 35 case '*': 36 result = b * a; 37 numStack.push(result); 38 break; 39 case '/': 40 result = b / a; 41 numStack.push(result); 42 break; 43 } 44 45 } 46 if (temp.charAt(0) != '#') { 47 priStack.push(new Character(temp.charAt(0))); 48 if (temp.charAt(0) == ')') { 49 priStack.pop(); 50 priStack.pop(); 51 } 52 } 53 } else 54 tempNum = tempNum.append(temp); 55 } 56 return numStack.pop(); 57 } 58 59 private boolean isNum(String temp) { 60 return temp.matches("[0-9]"); 61 } 62 private boolean compare(char str) { 63 if (priStack.empty()) { 64 return true; 65 } 66 char last = (char) priStack.lastElement(); 67 if (last == '(') { 68 return true; 69 } 70 switch (str) { 71 case '#': 72 return false; 73 case '(': 74 return true; 75 case ')': 76 return false; 77 case '*': { 78 if (last == '+' || last == '-') 79 return true; 80 else 81 return false; 82 } 83 case '/': { 84 if (last == '+' || last == '-') 85 return true; 86 else 87 return false; 88 } 89 case '+': 90 return false; 91 case '-': 92 return false; 93 } 94 return true; 95 } 96 97 public static void main(String args[]) { 98 CalculatorUtil operate = new CalculatorUtil(); 99 double t = operate.caculate("(3+4*(4*10-15/9)#"); 100 System.out.println(t); 101 } 102 103 }
CreateExp.java文件:
1 package com.example.cal; 2 3 import java.util.Arrays; 4 5 public class CreateExp { 6 private int a[]; 7 private char s[]; 8 private char sym[]; 9 private char brackets[]; 10 private int num[]; 11 12 public CreateExp() { 13 a = new int[100]; 14 s = new char[4]; 15 s[0] = '+'; 16 s[1] = '-'; 17 s[2] = '*'; 18 s[3] = '/'; 19 sym = new char[100]; 20 brackets = new char[100]; 21 num = new int[100]; 22 } 23 24 public void dfs(int s, int e)// 使用dfs递归添加括号 25 { 26 if ((int) (Math.random() * 4) == 0) // 四分之一的概率,不进行任何操作 27 { 28 return; 29 } 30 if (e - s <= 1)// 只有一个元素或没有元素,不进行任何操作 31 { 32 return; 33 } 34 int s1 = (int) (Math.random() * (e - s - 1)) + s;// 随机生成插入括号的位置 35 int e1 = (int) (Math.random() * (e - s1)) + s1; 36 while (s1 < s || e1 > e || s1 >= e1 || (s1 == s && e1 == e))// 避免无用括号 37 { 38 s1 = (int) (Math.random() * (e - s - 1)) + s; 39 e1 = (int) (Math.random() * (e - s1)) + s1; 40 } 41 if (brackets[s1] == ')' || brackets[e1] == '(') { 42 return; 43 } 44 brackets[s1] = '('; 45 brackets[e1] = ')'; 46 num[s1]++; 47 num[e1]++; 48 dfs(s, s1 - 1);// 插入括号的左边几个元素 49 dfs(e1 + 1, e);// 括号之间的几个元素 50 dfs(s1, e1);// 括号右边的几个元素 51 } 52 53 String Create() { 54 int n; 55 String ans; 56 ans = ""; 57 n = (int) (Math.random() * (3)) + 2; 58 ; 59 Arrays.fill(brackets, '.'); 60 Arrays.fill(num, 0); 61 for (int i = 1; i <= n; i++) { 62 a[i] = (int) (Math.random() * (99)) + 1; 63 sym[i] = s[(int) (Math.random() * (4))]; 64 } 65 dfs(1, n); 66 while ((brackets[1] == '(') && (num[1]-- != 0)) { 67 ans += '('; 68 } 69 ans += a[1] + ""; 70 for (int i = 2; i <= n; i++) { 71 ans += sym[i]; 72 while (brackets[i] == '(' && num[i]-- != 0) { 73 ans += '('; 74 } 75 ans += a[i] + ""; 76 while (brackets[i] == ')' && num[i]-- != 0) { 77 ans += ')'; 78 } 79 } 80 return ans; 81 } 82 }
Exp_result.java文件:
package com.example.cal; import java.io.Serializable; public class Exp_result implements Serializable{ @Override public String toString() { return "Exp_result [exp=" + exp + ", ans=" + ans + ", state=" + state + ", myAns=" + myAns + "]"; } private static final long serialVersionUID = -6935353529177913865L; private String exp; private double ans; private boolean state; private double myAns; public Exp_result() { // TODO Auto-generated constructor stub } public Exp_result(String exp, double ans) { super(); this.exp = exp; this.ans = ans; } public String getExp() { return exp; } public void setExp(String exp) { this.exp = exp; } public double getAns() { return ans; } public void setAns(double ans) { this.ans = ans; } public boolean isState() { return state; } public void setState(boolean state) { this.state = state; } public double getMyAns() { return myAns; } public void setMyAns(double myAns) { this.myAns = myAns; } }
list = (ArrayList<Exp_result>) getIntent().getSerializableExtra("list"); if (list == null) System.out.println("链表为空"); listView = (ListView) findViewById(R.id.list_view); if (list != null) { List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); for (Exp_result er : list) { HashMap<String, Object> item = new HashMap<String, Object>(); item.put("tx_show_exp", er.getExp()); item.put("tx_show_ans", er.getAns()); item.put("tx_show_myAns", er.getMyAns()); if (er.isState()) { item.put("state", R.drawable.right); } else { item.put("state", R.drawable.wrong); } data.add(item); } SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[] { "tx_show_exp", "tx_show_ans", "tx_show_myAns", "state" }, new int[] { R.id.tx_show_exp, R.id.tv_show_ans, R.id.tv_show_myAns, R.id.task_img }); listView.setAdapter(adapter); }
三个重要文件如上。
手机运行效果如下:
团队合照:
总结:能够做出个有点小用处东西,感觉不错,继续努力啊!
http://www.cnblogs.com/wsqJohn/p/5360976.html
队友 : 王世强