四则运算《安卓版》04
生活随笔
收集整理的這篇文章主要介紹了
四则运算《安卓版》04
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
結對小伙伴:張勛?? http://www.cnblogs.com/X-knight/
下載地址:http://files.cnblogs.com/files/wangjianly/WangJian.apk
在上次四則運算的基礎上,將編寫的四則運算發布成安卓版,具體實現功能如下:
1:是否有除數參與運算
2:是否有括號參與運算
3:產生的數值的范圍(從1開始)
4:設定出題的數量
解決辦法:
????? 這個安卓的程序主要有兩個界面,一個是對用戶輸入的要求進行判斷,然后根據要求進行生成隨機四則運算。另一個是答題界面。
代碼:
第一個布局代碼;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:background="@drawable/first"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="有無乘除?"tools:ignore="HardcodedText" /><RadioGroupandroid:id="@+id/rg_chufa"android:layout_width="fill_parent"android:layout_height="wrap_content" ><RadioButtonandroid:id="@+id/chuafa_yes"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="有"tools:ignore="HardcodedText" /><RadioButtonandroid:id="@+id/chuafa_no"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="無"tools:ignore="HardcodedText" /></RadioGroup><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="有無括號?"tools:ignore="HardcodedText" /><RadioGroupandroid:id="@+id/rg_kuohao"android:layout_width="fill_parent"android:layout_height="wrap_content" ><RadioButtonandroid:id="@+id/kuohao_yes"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="有"tools:ignore="HardcodedText" /><RadioButtonandroid:id="@+id/kuohao_no"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="無"tools:ignore="HardcodedText" /></RadioGroup><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="輸入數值范圍:"tools:ignore="HardcodedText" /><EditText android:id="@+id/et_range"android:layout_width="fill_parent"android:layout_height="wrap_content"android:inputType="number"/><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="產生多少式子:"tools:ignore="HardcodedText" /><EditText android:id="@+id/et_exp_num"android:layout_width="fill_parent"android:layout_height="wrap_content"android:inputType="number"/><Buttonandroid:id="@+id/btn_next"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="確認"tools:ignore="HardcodedText" /></LinearLayout>?
第二個布局界面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:background="@drawable/beijing"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".SolutionActivity" android:orientation="vertical"><TextViewandroid:id="@+id/tv_num"android:layout_width="fill_parent"android:layout_height="wrap_content"/><TextView android:id="@+id/tv_exp"android:layout_width="fill_parent"android:layout_height="wrap_content"/><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="輸入結果:"tools:ignore="HardcodedText" /><EditTextandroid:id="@+id/et_ans"android:layout_width="fill_parent"android:layout_height="wrap_content"tools:ignore="TextFields" /><Buttonandroid:id="@+id/okBtn"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="確認"tools:ignore="HardcodedText" /><Buttonandroid:id="@+id/nextBtn"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="下一題"tools:ignore="HardcodedText" /></LinearLayout>?
java文件1:
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) {//把表達式用運算符、括號分割成一段一段的,并且分割后的結果包含分隔符StringTokenizer tokenizer = new StringTokenizer(computeExpr, "+-*/()", true);Stack<Double> numStack = new Stack<Double>(); //用來存放數字的棧Stack<Operator> operStack = new Stack<Operator>(); //存放操作符的棧Map<String, Operator> computeOper = this.getComputeOper(); //獲取運算操作符String currentEle; //當前元素while (tokenizer.hasMoreTokens()) {currentEle = tokenizer.nextToken().trim(); //去掉前后的空格if (!"".equals(currentEle)) { //只處理非空字符if (this.isNum(currentEle)) { //為數字時則加入到數字棧中 numStack.push(Double.valueOf(currentEle));} else { //操作符Operator currentOper = computeOper.get(currentEle);//獲取當前運算操作符if (currentOper != null) { //不為空時則為運算操作符while (!operStack.empty() && operStack.peek().priority() >= currentOper.priority()) {compute(numStack, operStack);}//計算完后把當前操作符加入到操作棧中 operStack.push(currentOper);} else {//括號if ("(".equals(currentEle)) { //左括號時加入括號操作符到棧頂 operStack.push(Operator.BRACKETS);} else { //右括號時, 把左括號跟右括號之間剩余的運算符都執行了。while (!operStack.peek().equals(Operator.BRACKETS)) {compute(numStack, operStack);}operStack.pop();//移除棧頂的左括號 }}}}}// 經過上面代碼的遍歷后最后的應該是nums里面剩兩個數或三個數,operators里面剩一個或兩個運算操作符while (!operStack.empty()) {compute(numStack, operStack);}return numStack.pop();}/*** 判斷一個字符串是否是數字類型* @param str* @return*/private boolean isNum(String str) {String numRegex = "^\\d+(\\.\\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(); // 彈出數字棧最頂上的數字作為運算的第二個數字Double num1 = numStack.pop(); // 彈出數字棧最頂上的數字作為運算的第一個數字Double computeResult = operStack.pop().compute(num1, num2); // 彈出操作棧最頂上的運算符進行計算numStack.push(computeResult); // 把計算結果重新放到隊列的末端 }/*** 運算符*/private enum Operator {/*** 加*/PLUS {@Overridepublic int priority() {return 1; }@Overridepublic double compute(double num1, double num2) {return num1 + num2; }},/*** 減*/MINUS {@Overridepublic int priority() {return 1; }@Overridepublic double compute(double num1, double num2) {return num1 - num2; }},/*** 乘*/MULTIPLY {@Overridepublic int priority() {return 2; }@Overridepublic double compute(double num1, double num2) {return num1 * num2; }},/*** 除*/DIVIDE {@Overridepublic int priority() {return 2; }@Overridepublic double compute(double num1, double num2) {return num1 / num2; }},/*** 括號*/BRACKETS {@Overridepublic int priority() {return 0; }@Overridepublic double compute(double num1, double num2) {return 0; }};/*** 對應的優先級* @return*/public abstract int priority();/*** 計算兩個數對應的運算結果* @param num1 第一個運算數* @param num2 第二個運算數* @return*/public abstract double compute(double num1, double num2);}//符號函數//-------------------------------------------------------------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;}}//分數函數//-------------------------------------------------------------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 = "";//隨機產生四則運算符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 stubreturn null;}*///運算函數//-------------------------------------------------------------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 ;}}?
java文件2:
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);} }?
?java文件3
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;int dui=0;int cuo=0;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類中取出表達式到expString中tv_num.setText("第"+count+++"題:");test=new Test();expString= test.shu(chu,kuohao,range);//將表達式顯示到exp (exp.setText(表達式))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中取出答案//判斷答案是否正確//顯示判斷結果myans=Integer.valueOf(tv_ans.getText().toString());if(myans==result){dui=dui+1;Toast.makeText(SolutionActivity.this, "回答正確,您已經答對了"+dui+"道題,答錯了"+cuo+"道題", 0).show();}else{cuo=cuo+1;Toast.makeText(SolutionActivity.this, "回答錯誤,您已經答對了"+dui+"道題,答錯了"+cuo+"道題", 0).show();}}else if(v0==nextBtn){tv_ans.setText("");if(count>exp_num) finish();//從java類中取出表達式//將表達式顯示到exp (exp.setText(表達式))tv_num.setText("第"+count+++"題:");expString= test.shu(chu,kuohao,range);//將表達式顯示到exp (exp.setText(表達式))exp.setText(expString+"=");result2 = test.computeWithStack(expString);BigDecimal bg2=new BigDecimal(result2);result=bg2.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();}}}?
?
運行結果;
?
?
| 項目計劃總結: | ? | ? | ? | ? | ? | |
| ? | ? | ? | ? | ? | ? | ? |
| 日期\任務 | 聽課 | 編寫程序 | 查閱資料 | 日總計 | ? | ? |
| 星期一 | ? | 4 | 2 | 6 | ? | ? |
| 星期二 | ? | ? | ? | ? | ? | ? |
| 星期三 | ? | 3 | 2 | 5 | ? | ? |
| 星期四 | ? | 4 | 2 | 6 | ? | ? |
| 星期五 | ? | 2 | 2 | 4 | ? | ? |
| 星期六 | ? | 1 | 1 | 1 | ? | ? |
| 星期日 | ? | 3 | 0.5 | 3.5 | ? | ? |
| 周總計 | ? | 16 | 9.5 | 25.5 | ? | ? |
| ? | ? | ? | ? | ? | ? | ? |
| 時間記錄日志: | ? | ? | ? | ? | ? | |
| ? | ? | ? | ? | ? | ? | ? |
| 日期 | 開始時間 | 結束時間 | 中斷時間 | 靜時間 | 活動 | 備注 |
| 4月1日 | 14:00 | 16:10 | 10 | 120 | 查閱資料/編程 | 修改程序 |
| 4月2日 | 8:20 | 10:40 | 20 | 120 | 編寫程序 | 將程序改為JAVA語言 |
| 4月3日 | 16:30 | 20:30 | 30 | 210 | 查閱資料 | 查資料和修改代碼 |
| 4月4日 | 14:00 | 21:00 | 60 | 3600 | 編程 | 修改和編寫代碼 |
| 4月5日 | ? | ? | ? | ? | ? | ? |
| 4月6日 | 10:00 | 17:30 | 90 | 3600 | 調試 | 最終調試 |
| ? | 18:00 | 18:30 | ? | 30 | 博客 | 撰寫博客 |
| ? | ? | ? | ? | ? | ? | ? |
| 缺陷記錄日志: | ? | ? | ? | ? | ? | |
| ? | ? | ? | ? | ? | ? | ? |
| 日期 | 編號 | 引入階段 | 排除階段 | 修復時間&問題描述 | ? | ? |
| 4月1日 | 1 | 編碼 | 編譯 | 對原先的四則運算進行了修改,優化了代碼 | ? | ? |
| 4月2日 | ? | 編碼 | 編譯 | 將代碼轉化為JAVA語言 | ? | ? |
| 4月3日 | 2 | 編碼 | 編譯 | 對JAVA語言中出現的錯誤進行了修改和排除 | ? | ? |
| 4月4日 | ? | 編碼 | 編譯 | 對JAVA語言中出現的錯誤進行了修改和排除 | ? | ? |
| 4月4日 | ? | 編碼 | 編譯 | 排除錯誤 | ? | ? |
| 4月6日 | 3 | 編碼 | 編譯 | 進行最后的修改,對界面進行了美化 | ? | ? |
總結:?這次由于自己是第一次將c++編寫的程序發布成Android版本,由于自己對Java語言掌握的額不是很好,所以在過程中,遇到了許多的困難,列入在界面之間進行參數的傳遞,以及添加監視空間等,通過查閱資料以及尋找相關的例子和詢問其他同學,最終還是解決了自己所遇到的問題。
轉載于:https://www.cnblogs.com/wangjianly/p/5360689.html
總結
以上是生活随笔為你收集整理的四则运算《安卓版》04的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视频教程-实用通俗易懂的设计模式-软件设
- 下一篇: 赵玉开的技术博客