四则运算-安卓版
本次作业的题目:
在之前的四则运算基础上继续团队开发,详细要求如下:
1. 生成的题目中计算过程不能产生负数,也就是说算术表达式中如果存在形如e1 ? e2的子表达式,那么e1 ≥ e2。
2、生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数。
3、每道题目中出现的运算符个数不超过3个,括号不限。
4、程序一次运行生成的题目不能重复,即任何两道题目不能通过有限次交换+和×左右的算术表达式变换为同一道题目。
5、把程序变成一个网页程序或智能手机程序, 用户通过设定参数,就可以得到各种题目,并可实现在线答题并评判。
在历经一个月的折腾后,二柱子的题目终于圆满结束。这次写的是安卓版,因为考虑到用户需求,安卓版的实用性更好,更方便!刚好这学期开设了移动应用开发的课程,顺便练习联系。关键就是把之前的C++代码改写成Java语言的代码,做成安卓app的过程。
三个重要文件如下:
CalculatorUtil.java文件:
package com.example.cal; import android.annotation.SuppressLint; import java.util.Stack; @SuppressLint("UseValueOf") public class CalculatorUtil { private Stack<Character> priStack = new Stack<Character>();// 操作符栈 private Stack<Double> numStack = new Stack<Double>();;// 操作数栈 public double caculate(String str) { String temp; StringBuffer tempNum = new StringBuffer(); StringBuffer string = new StringBuffer().append(str); while (string.length() != 0) { temp = string.substring(0, 1); string.delete(0, 1); if (!isNum(temp)) { if (!"".equals(tempNum.toString())) { double num=Double.parseDouble(tempNum.toString()); numStack.push(num); tempNum.delete(0, tempNum.length()); } while (!compare(temp.charAt(0)) && (!priStack.empty())) { double a = numStack.pop(); double b = numStack.pop(); char ope = priStack.pop(); double result = 0; switch (ope) { case '+': result = b + a; numStack.push(result); break; case '-': result = b - a; numStack.push(result); break; case '*': result = b * a; numStack.push(result); break; case '/': result = b / a; numStack.push(result); break; } } if (temp.charAt(0) != '#') { priStack.push(new Character(temp.charAt(0))); if (temp.charAt(0) == ')') { priStack.pop(); priStack.pop(); } } } else tempNum = tempNum.append(temp); } return numStack.pop(); } private boolean isNum(String temp) { return temp.matches("[0-9]"); } private boolean compare(char str) { if (priStack.empty()) { return true; } char last = (char) priStack.lastElement(); if (last == '(') { return true; } switch (str) { case '#': return false; case '(': return true; case ')': return false; case '*': { if (last == '+' || last == '-') return true; else return false; } case '/': { if (last == '+' || last == '-') return true; else return false; } case '+': return false; case '-': return false; } return true; } public static void main(String args[]) { CalculatorUtil operate = new CalculatorUtil(); double t = operate.caculate("(3+4*(4*10-15/9)#"); System.out.println(t); } }
CreateExp.java文件:
package com.example.cal; import java.util.Arrays; public class CreateExp { private int a[]; private char s[]; private char sym[]; private char brackets[]; private int num[]; public CreateExp() { a = new int[100]; s = new char[4]; s[0] = '+'; s[1] = '-'; s[2] = '*'; s[3] = '/'; sym = new char[100]; brackets = new char[100]; num = new int[100]; } public void dfs(int s, int e)// 使用dfs递归添加括号 { if ((int) (Math.random() * 4) == 0) // 四分之一的概率,不进行任何操作 { return; } if (e - s <= 1)// 只有一个元素或没有元素,不进行任何操作 { return; } int s1 = (int) (Math.random() * (e - s - 1)) + s;// 随机生成插入括号的位置 int e1 = (int) (Math.random() * (e - s1)) + s1; while (s1 < s || e1 > e || s1 >= e1 || (s1 == s && e1 == e))// 避免无用括号 { s1 = (int) (Math.random() * (e - s - 1)) + s; e1 = (int) (Math.random() * (e - s1)) + s1; } if (brackets[s1] == ')' || brackets[e1] == '(') { return; } brackets[s1] = '('; brackets[e1] = ')'; num[s1]++; num[e1]++; dfs(s, s1 - 1);// 插入括号的左边几个元素 dfs(e1 + 1, e);// 括号之间的几个元素 dfs(s1, e1);// 括号右边的几个元素 } String Create() { int n; String ans; ans = ""; n = (int) (Math.random() * (3)) + 2; ; Arrays.fill(brackets, '.'); Arrays.fill(num, 0); for (int i = 1; i <= n; i++) { a[i] = (int) (Math.random() * (99)) + 1; sym[i] = s[(int) (Math.random() * (4))]; } dfs(1, n); while ((brackets[1] == '(') && (num[1]-- != 0)) { ans += '('; } ans += a[1] + ""; for (int i = 2; i <= n; i++) { ans += sym[i]; while (brackets[i] == '(' && num[i]-- != 0) { ans += '('; } ans += a[i] + ""; while (brackets[i] == ')' && num[i]-- != 0) { ans += ')'; } } return ans; } }
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; } }
手机运行效果如下:
团队合照:
队友:高逸凡
博客链接:http://www.cnblogs.com/yifan2016/p/5361063.html
app下载链接:小学生四则运算
总结:能够做出个有点小用处东西,感觉不错,继续努力啊!