CreateExe.java package com.example.cal; public class CreateExp { private char SignTemp, Sign[]; private double Num[], NumTemp; private int STop, NTop; private char cal[][]; String tempExp=""; public CreateExp() { // TODO Auto-generated constructor stub Sign = new char[100]; Num = new double[100]; cal = new char[100][100]; } char Operator(int m) // 产生运算符 { int a, b; char oper = '1'; a = (int) (Math.random() * 5); // 产生随机整数用于除四取余后进行四种运算符号和四种数的选择 b = a = (int) (Math.random() * 2); if (m == 2) // 无乘除法情况 { switch (b) // 用于加减运算两种符选择 { case 0: oper = '+'; break; case 1: oper = '-'; break; default: break; } } if (m == 1) // 有乘除法情况 { switch (a) // 用于四则运算四种符号的选择 { case 0: oper = '+'; break; case 1: oper = '-'; break; case 2: oper = '*'; break; case 3: oper = '/'; break; default: break; } } return oper; } void compute() // 计算 { for(int i=0;i<=tempExp.length();i++){ Sign[i]=tempExp.charAt(i); } switch (Sign[STop - 1]) { case '+': Num[NTop - 2] += Num[NTop - 1];// 运算后将结果存入 STop--; NTop--; break; case '-': Num[NTop - 2] -= Num[NTop - 1]; STop--; NTop--; break; case '*': Num[NTop - 2] *= Num[NTop - 1]; STop--; NTop--; break; case '/': // 判断除数是否为0,0的话结束 if (Num[STop - 1] == 0) { return; } else { Num[NTop - 2] /= Num[NTop - 1]; STop--; NTop--; break; } // 其中STop--;NTop--;实现的是将数据弹出栈 case ')': STop = STop - 2; break; // 发现')'对上个运算符进行判断 } } private int i, length; public int getI() { return i; } public void setI(int i) { this.i = i; } public int getlength() { return length; } public void setlength(int length) { this.length = length; } double Run() // 运行函数 { char temp; int p = 0; while (p <= length) // 读入数据 { temp = cal[i][p]; if (temp >= '0' && temp <= '9') { NumTemp = NumTemp * 10 + (double) (temp - 48); } else { Sign[STop] = SignTemp; SignTemp = temp; STop++; // 运算符入栈 if (Sign[STop - 1] != ')' && temp != '(') // 数字入栈 { Num[NTop] = NumTemp; NTop++; NumTemp = 0; } switch (temp) // 进行有关运算优先级别的考虑 { case '+': case '-': while (Sign[STop - 1] != '(' && STop - 1 > 0) compute(); break; // "+""-"的时候考虑"(" case '*': case '/': while (Sign[STop - 1] != '(' && Sign[STop - 1] != '+' && Sign[STop - 1] != '-' && STop - 1 > 0) compute(); break; case '(': break; // 继续读取 case ')': while (Sign[STop - 1] != '(') compute(); break; default: while (STop - 1 > 0) compute(); break; // 结束 } } p++; } return Num[0]; } public String getExp() { int num1, num2, jishu, value = 100, m = 1, kuohao = 1, j, kuo; char oper1; String s_num1, s_num2, s_connect, t; num1 = (int) (Math.random() * value) + 1; num2 = (int) (Math.random() * value) + 1; oper1 = Operator(m); s_num1 = num1 + ""; s_num2 = num2 + ""; s_connect = s_num1 + oper1 + s_num2; jishu = (int) (Math.random() * 4) + 2; if (jishu > 2) // 对随机生成运算式里数值个数判断 { for (j = jishu; j > 2; j--) // 两个数值以上运算式生成 { s_num1 = s_connect; oper1 = Operator(m); num2 = (int) (Math.random() * value) + 1; s_num2 = num2 + ""; if (kuohao == 1) // 随机产生括号 { kuo = (int) (Math.random() * 2); if (kuo == 0) { s_num1 = '(' + s_num1 + ')'; } } kuo = (int) (Math.random() * 2); if (kuo == 0) { t = s_num1; s_num1 = s_num2; s_num2 = t; } s_connect = s_num1 + oper1 + s_num2; } tempExp=s_connect; return s_connect; } return null; } } Exp.java package com.example.cal; import android.util.Log; public class Exp {// 产生表达式及结果 private int item_num = 0;// 0:两项,1:三项 private int sign_num = 0;// 0+,1-,2*,3/ private int sign_num_1 = 0;// 0+,1-,2*,3/ 三项的时候需要两个运算符 private int kuohao = 0;// 0不产生括号,1产生括号 private int kuohao_weizhi = 0;// 0表示前2项有括号,1表示后2项有括号 private int result;// 结果 private StringBuilder exp=new StringBuilder();// 式子 enum Sign { add, subtract, multiply, divide } Exp() { while(true){ if(createExp()==true) { break; } } } public StringBuilder getExp() { return exp; } public void setExp(StringBuilder exp) { this.exp = exp; } public void setResult(int result) { this.result = result; } private boolean createExp() { item_num = (int) (Math.random() * 2); sign_num = (int) (Math.random() * 4); int a = (int) (Math.random() * 100);// 随机生成0-100以内的数 int b = (int) (Math.random() * 100); int c = (int) (Math.random() * 100); kuohao = (int) (Math.random() * 2); Log.e("jhd", item_num+""); Log.e("jhd", sign_num+""); Log.e("jhd", kuohao+""); Log.e("jhd", a+""); Log.e("jhd", b+""); Log.e("jhd", c+""); if (item_num == 0) {// 产生2项的情况 String temp = ""; switch (sign_num) { case 0:// + temp = a + "+" + b; result = a + b; break; case 1:// - temp = a + "-" + b; result = a - b; break; case 2:// * temp = a + "*" + b; result = a * b; break; case 3:// / temp = a + "÷" + b; if (a % b != 0) {// 不整除旧返回false return false; } result = a / b; break; } exp.append(temp); } if (item_num == 1) {// 式子为三项时 sign_num_1 = (int) (Math.random() * 4);// 随机产生一个运算符 if (kuohao == 0) {// 不产生括号 // 比较优先级 String temp; if (sign_num <= 1 && sign_num_1 > 1) {// 先计算后两项 if (calculate(b, sign_num_1, c) == null) { return false; } result = calculate(a, sign_num, calculate(b, sign_num_1, c)); exp.append(a + getSign(sign_num) + b + getSign(sign_num_1) + c); } else {// 计算前两项 if (calculate(a, sign_num, b) == null) { return false; } if (calculate(calculate(a, sign_num, b), sign_num_1, c) == null) { return false; } result = calculate(calculate(a, sign_num, b), sign_num_1, c); exp.append(a + getSign(sign_num) + b + getSign(sign_num_1) + c); } } else{ return false; } } return true; } public Integer calculate(int a, int sign, int b) { Integer temp = null; switch (sign) { case 0: temp = a + b; break; case 1: temp = a - b; break; case 2: temp = a * b; break; case 3: if (a % b != 0) {// 保证能整除 return null; } temp = a / b; break; } return temp; } private String getSign(int sign) { String temp = null; switch (sign) { case 0: temp = "+"; break; case 1: temp = "-"; break; case 2: temp = "*"; break; case 4: temp = "/"; break; } return temp; } public int getResult() {// 得到产生表达式的结果 return result; } /*public String getExpression() {// 得到产生的表达式 //if(get) }*/ } ExpActivity.java package com.example.cal; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class ExpActivity extends Activity implements OnClickListener { private CreateExp expclass; private TextView tv_exp; private EditText et_result; private Button next; private Button exit; private TextView all; private TextView completed; private TextView wrong; private int index = 0;// 当前题目位置 private int wrong_num = 0;// 答错题目数量 private List<Exp> list;// 题目数组,所有题目 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_exp); expclass = new CreateExp();// 随机产生一个2-3项四则运算式子,及式子的结果 // ---初始控件 intViews(); int num = getIntent().getIntExtra("num", 0); if (num != 0) { list = new ArrayList<Exp>(); for (int i = 0; i < num; i++) { list.add(new Exp()); } tv_exp.setText(list.get(index).getExp());// all.setText("共计:"+num); completed.setText("已答:"+(index)); wrong.setText("答错:"); } } private void intViews() { // TODO Auto-generated method stub tv_exp = (TextView) findViewById(R.id.tv_exp); et_result = (EditText) findViewById(R.id.et_result); next = (Button) findViewById(R.id.next_btn); exit = (Button) findViewById(R.id.exit_btn); all = (TextView) findViewById(R.id.all); completed = (TextView) findViewById(R.id.completed); wrong = (TextView) findViewById(R.id.wrong); next.setOnClickListener(new OnClickListener() {// 点击下一题时,判断上一道是否正确,并且显示下一道 @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(et_result.getText().toString()==""){ Toast.makeText(getApplicationContext(), "答案不能为空!", Toast.LENGTH_SHORT).show(); } int result = Integer.valueOf(et_result.getText().toString()); if (index >= list.size()) { Toast.makeText(getApplicationContext(), "题目已经答完!", Toast.LENGTH_SHORT).show(); return; } if (result != list.get(index).getResult()) { wrong_num++; wrong.setText("答错:"+wrong_num); Log.d("jhd", "已答错数目:" + wrong_num); } index++; completed.setText("已答:"+(index)); if(index==list.size()) { Toast.makeText(getApplicationContext(), "题目已经答完!", Toast.LENGTH_SHORT).show(); return; } tv_exp.setText(list.get(index).getExp()); et_result.setText(""); } }); exit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub finish(); } }); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub } } InputActivity.java package com.example.cal; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class InputActivity extends Activity implements OnClickListener{ private EditText et_num; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_input); et_num=(EditText)findViewById(R.id.et_num); btn=(Button)findViewById(R.id.num_btn); btn.setOnClickListener(this); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(et_num!=null){ int n=Integer.valueOf(et_num.getText().toString()); Intent intent=new Intent(InputActivity.this,ExpActivity.class); if(n==0){ Toast.makeText(getApplicationContext(), "请输入大于0的整数!", Toast.LENGTH_SHORT).show(); } intent.putExtra("num", n); startActivity(intent); } } } MainActivity.java package com.example.cal; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button enterBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); enterBtn=(Button)findViewById(R.id.enter_btn); enterBtn.setOnClickListener(this); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,InputActivity.class); startActivity(intent); } }
介绍:
随机产生最多三位数,随机产生运算符,用户可选择出题数目,系统随机产生算式,用户回答,系统可判断正误。
小伙伴的地址在这里:http://www.cnblogs.com/wuyw/