四则运算4(Android版)
生活随笔
收集整理的這篇文章主要介紹了
四则运算4(Android版)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
隊員:王楗? http://home.cnblogs.com/u/wangjianly/ 結(jié)組照:
下載地址:http://files.cnblogs.com/files/wangjianly/WangJian.apk
在上次四則運算的基礎(chǔ)上,將編寫的四則運算發(fā)布成安卓版,具體實現(xiàn)功能如下:
1:是否有除數(shù)參與運算
2:是否有括號參與運算
3:產(chǎn)生的數(shù)值的范圍(從1開始)
4:設(shè)定出題的數(shù)量
解決辦法:
????? 這個安卓的程序主要有兩個界面,一個是對用戶輸入的要求進(jìn)行判斷,然后根據(jù)要求進(jìn)行生成隨機四則運算。另一個是答題界面。
?
運行結(jié)果截圖如下:
源代碼如下:
第一個布局代碼;
package com.wwwjjj.wangjian;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.RadioGroup;public class MainActivity extends Activity implements OnClickListener {private RadioGroup chufa;private RadioGroup kouhao;private EditText range;private EditText exp_num;private Button btn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);chufa = (RadioGroup) findViewById(R.id.rg_chufa);kouhao = (RadioGroup) findViewById(R.id.rg_kuohao);range = (EditText) findViewById(R.id.et_range);exp_num = (EditText) findViewById(R.id.et_exp_num);btn = (Button) findViewById(R.id.btn_next);btn.setOnClickListener(this);}@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubint chufaId = chufa.getCheckedRadioButtonId();int kuohaoId = kouhao.getCheckedRadioButtonId();Boolean haveChufa = false, haveKuohao = false;if (chufaId == R.id.chuafa_yes)haveChufa = true;if (kuohaoId == R.id.kuohao_yes)haveKuohao = true;int r = Integer.valueOf(range.getText().toString());int en = Integer.valueOf(exp_num.getText().toString());Intent intent =new Intent(MainActivity.this,SolutionActivity.class);Bundle bundle=new Bundle();bundle.putBoolean("kuohao", haveKuohao);bundle.putBoolean("chufa", haveChufa);bundle.putInt("range", r);bundle.putInt("exp_num", en);intent.putExtras(bundle);startActivity(intent);} }第二個運行程序界面:
package com.wwwjjj.wangjian;import java.math.BigDecimal;import android.app.Activity; import android.os.Bundle; 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 SolutionActivity extends Activity implements OnClickListener{private TextView exp;private TextView tv_num;private EditText tv_ans;private Button okBtn;private Button nextBtn;private String expString;private Test test;private boolean haveChu;private boolean haveKuohao;private int range;private int exp_num;int chu=2,kuohao=2;double ans;private double result2;private double result;private Integer myans;private int count=1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_solution);haveKuohao=getIntent().getBooleanExtra("kuohao", haveKuohao);haveChu=getIntent().getBooleanExtra("chufa", haveChu);if(haveChu)chu=1;if(haveKuohao)kuohao=1;range=getIntent().getIntExtra("range", range);exp_num=getIntent().getIntExtra("exp_num", exp_num);exp=(TextView)findViewById(R.id.tv_exp);tv_num=(TextView)findViewById(R.id.tv_num);tv_ans=(EditText)findViewById(R.id.et_ans);okBtn=(Button)findViewById(R.id.okBtn);nextBtn=(Button)findViewById(R.id.nextBtn);okBtn.setOnClickListener(this);nextBtn.setOnClickListener(this);//從java類中取出表達(dá)式到expString中tv_num.setText("第"+count+++"題:");test=new Test();expString= test.shu(chu,kuohao,range);//將表達(dá)式顯示到exp (exp.setText(表達(dá)式))exp.setText(expString+"=");result2 = test.computeWithStack(expString);BigDecimal bg2=new BigDecimal(result2);result=bg2.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();}@Overridepublic void onClick(View v0) {// TODO Auto-generated method stubif(v0==okBtn){//從tv_ans中取出答案//判斷答案是否正確//顯示判斷結(jié)果myans=Integer.valueOf(tv_ans.getText().toString());if(myans==result){Toast.makeText(SolutionActivity.this, "回答正確", 0).show();}else{Toast.makeText(SolutionActivity.this, "回答錯誤", 0).show();}}else if(v0==nextBtn){tv_ans.setText("");if(count>exp_num) finish();//從java類中取出表達(dá)式//將表達(dá)式顯示到exp (exp.setText(表達(dá)式))tv_num.setText("第"+count+++"題:");expString= test.shu(chu,kuohao,range);//將表達(dá)式顯示到exp (exp.setText(表達(dá)式))exp.setText(expString+"=");result2 = test.computeWithStack(expString);BigDecimal bg2=new BigDecimal(result2);result=bg2.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();}}}java文件:
package com.wwwjjj.wangjian;//import java.awt.SystemColor; import java.util.HashMap; import java.util.Map; import java.util.Stack; import java.util.StringTokenizer; import java.util.regex.Pattern;public class Test {public double computeWithStack(String computeExpr) {//把表達(dá)式用運算符、括號分割成一段一段的,并且分割后的結(jié)果包含分隔符StringTokenizer tokenizer = new StringTokenizer(computeExpr, "+-*/()", true);Stack<Double> numStack = new Stack<Double>(); //用來存放數(shù)字的棧 Stack<Operator> operStack = new Stack<Operator>(); //存放操作符的棧 Map<String, Operator> computeOper = this.getComputeOper(); //獲取運算操作符 String currentEle; //當(dāng)前元素 while (tokenizer.hasMoreTokens()) { currentEle = tokenizer.nextToken().trim(); //去掉前后的空格 if (!"".equals(currentEle)) { //只處理非空字符 if (this.isNum(currentEle)) { //為數(shù)字時則加入到數(shù)字棧中 numStack.push(Double.valueOf(currentEle)); } else { //操作符 Operator currentOper = computeOper.get(currentEle);//獲取當(dāng)前運算操作符 if (currentOper != null) { //不為空時則為運算操作符 while (!operStack.empty() && operStack.peek().priority() >= currentOper.priority()) { compute(numStack, operStack); } //計算完后把當(dāng)前操作符加入到操作棧中 operStack.push(currentOper); } else {//括號 if ("(".equals(currentEle)) { //左括號時加入括號操作符到棧頂 operStack.push(Operator.BRACKETS); } else { //右括號時, 把左括號跟右括號之間剩余的運算符都執(zhí)行了。 while (!operStack.peek().equals(Operator.BRACKETS)) { compute(numStack, operStack); } operStack.pop();//移除棧頂?shù)淖罄ㄌ?} } } } } // 經(jīng)過上面代碼的遍歷后最后的應(yīng)該是nums里面剩兩個數(shù)或三個數(shù),operators里面剩一個或兩個運算操作符 while (!operStack.empty()) { compute(numStack, operStack); } return numStack.pop(); } /** * 判斷一個字符串是否是數(shù)字類型 * @param str * @return */ private boolean isNum(String str) { String numRegex = "^\\d+(\\.\\d+)?$"; //數(shù)字的正則表達(dá)式 return Pattern.matches(numRegex, str); } /** * 獲取運算操作符 * @return */ private Map<String, Operator> getComputeOper() { return new HashMap<String, Operator>() { // 運算符 private static final long serialVersionUID = 7706718608122369958L; { put("+", Operator.PLUS); put("-", Operator.MINUS); put("*", Operator.MULTIPLY); put("/", Operator.DIVIDE); } }; } private void compute(Stack<Double> numStack, Stack<Operator> operStack) { Double num2 = numStack.pop(); // 彈出數(shù)字棧最頂上的數(shù)字作為運算的第二個數(shù)字 Double num1 = numStack.pop(); // 彈出數(shù)字棧最頂上的數(shù)字作為運算的第一個數(shù)字 Double computeResult = operStack.pop().compute( num1, num2); // 彈出操作棧最頂上的運算符進(jìn)行計算 numStack.push(computeResult); // 把計算結(jié)果重新放到隊列的末端 } /** * 運算符 */ private enum Operator { /** * 加 */ PLUS { @Override public int priority() { return 1; } @Override public double compute(double num1, double num2) { return num1 + num2; } }, /** * 減 */ MINUS { @Override public int priority() { return 1; } @Override public double compute(double num1, double num2) { return num1 - num2; } }, /** * 乘 */ MULTIPLY { @Override public int priority() { return 2; } @Override public double compute(double num1, double num2) { return num1 * num2; } }, /** * 除 */ DIVIDE { @Override public int priority() { return 2; } @Override public double compute(double num1, double num2) { return num1 / num2; } }, /** * 括號 */ BRACKETS { @Override public int priority() { return 0; } @Override public double compute(double num1, double num2) { return 0; } }; /** * 對應(yīng)的優(yōu)先級 * @return */ public abstract int priority(); /** * 計算兩個數(shù)對應(yīng)的運算結(jié)果 * @param num1 第一個運算數(shù) * @param num2 第二個運算數(shù) * @return */ public abstract double compute(double num1, double num2); } //符號函數(shù) //------------------------------------------------------------- static String fuhao(int chu) { String fu; int num_3; if (chu == 1) { num_3 = ((int)(Math.random() * 100)) % 4; if (num_3 == 0) fu = "+"; else if (num_3 == 1) fu = "-"; else if (num_3 == 2) fu = "*"; else fu = "/"; return fu; } else { num_3 = ((int)(Math.random()*20)) % 2; if (num_3 == 0) fu = "+"; else fu = "-"; return fu; } } //分?jǐn)?shù)函數(shù) //------------------------------------------------------------- static String fenshu() { int a1 = 0, b1 = 0, a2 = 0, b2 = 0; a1 = ((int)(Math.random()*(97))); b1 = ((int)(Math.random()*100 - a1)) + a1 + 1; a2 = ((int)(Math.random()* (97))); b2 = ((int)(Math.random()* (100 - a2))) + a2 + 1; String first_a1, second_b1; String first_a2, second_b2; first_a1 = String.valueOf(a1); second_b1 = String.valueOf(b1); first_a2 = String.valueOf(a2); second_b2 = String.valueOf(b2); String all1 = ""; //隨機產(chǎn)生四則運算符 int fu = 0; fu = ((int)(Math.random()*100)) % 2; if (fu == 0) { all1 = "(" + first_a1 + "/" + second_b1 + ")" + "+" + "(" + first_a2 + "/" + second_b2 + ")" ; } else if (fu == 1) { all1 = "(" + first_a1 + "/" + second_b1 + ")" + "-" + "(" + first_a2 + "/" + second_b2 + ")" ; } else if (fu == 2) { all1 = "(" + first_a1 + "/" + second_b1 + ")" + "*" + "(" + first_a2 + "/" + second_b2 + ")" ; } else { all1 = "(" + first_a1 + "/" + second_b1 + ")" + "/" + "(" + first_a2 + "/" + second_b2 + ")" ; } return all1; } /*private static String to_String(int b1) { // TODO Auto-generated method stub return null; }*/ //運算函數(shù) //------------------------------------------------------------- String shu(int chu, int kuohao, int range) { int num_1, num_2; int geshu; int calculate_kuohao=3; String str_first, str_second; String all = ""; int ch1; geshu = ((int)(Math.random()*(4)) + 2); for (int i = 1; i <= geshu; i++) { num_1 = ((int)(Math.random()* (range))) + 1; str_first = String.valueOf(num_1); num_2 = ((int)(Math.random()*(range))) + 1; str_second = String.valueOf(num_2); if ((kuohao == 1)&&(calculate_kuohao!=0)) { ch1 = ((int)(Math.random()*(4))) + 1; switch (ch1){ case 1: { if (all == "") { all = str_first + fuhao(chu) + str_second; } else { all = str_first + fuhao(chu) + all; } }break; case 2: { if (all == "") { all = str_second + fuhao(chu) + str_first; } else { all = all + fuhao(chu) + str_first; } }break; case 3: { if (all == "") { all = "(" + str_first + fuhao(chu) + str_second + ")"; } else { all = "(" + str_first + fuhao(chu) + all + ")"; } calculate_kuohao = calculate_kuohao - 1; }break; case 4: { if (all == ""){ all = "(" + str_second + fuhao(chu) + str_first + ")"; } else { all = "(" + all + fuhao(chu) + str_first + ")"; } calculate_kuohao = calculate_kuohao - 1; }break; } } else { ch1 = ((int)(Math.random()*(2))) + 1; switch (ch1){ case 1: { if (all == "") { all = str_first + fuhao(chu) + str_second; } else { all = str_first + fuhao(chu) + all; } }break; case 2: { if (all == "") { all = str_second + fuhao(chu) + str_first; } else { all = all + fuhao(chu) + str_first; } }break; } } } return all ; } }轉(zhuǎn)載于:https://www.cnblogs.com/X-knight/p/5360770.html
總結(jié)
以上是生活随笔為你收集整理的四则运算4(Android版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多元伯努利分布 multivariate
- 下一篇: Quectel EC200N-CN驱动移