队员:王楗  http://home.cnblogs.com/u/wangjianly/
结组照:

下载地址:http://files.cnblogs.com/files/wangjianly/WangJian.apk

在上次四则运算的基础上,将编写的四则运算发布成安卓版,具体实现功能如下:

1:是否有除数参与运算

2:是否有括号参与运算

3:产生的数值的范围(从1开始)

4:设定出题的数量

解决办法:

这个安卓的程序主要有两个界面,一个是对用户输入的要求进行判断,然后根据要求进行生成随机四则运算。另一个是答题界面。

运行结果截图如下:

源代码如下:

第一个布局代码;

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类中取出表达式到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){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类中取出表达式//将表达式显示到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();}}}

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) {//把表达式用运算符、括号分割成一段一段的,并且分割后的结果包含分隔符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 { @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; } }; /** * 对应的优先级 * @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 stub return 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 ; } }

转载于:https://www.cnblogs.com/X-knight/p/5360770.html

四则运算4(Android版)相关推荐

  1. Android版网易云音乐唱片机唱片磁盘旋转及唱片机机械臂动画关键代码实现思路...

     Android版网易云音乐唱片机唱片磁盘旋转及唱片机机械臂动画关键代码实现思路 先看一看我的代码运行结果. 代码运行起来初始化状态: 点击开始按钮,唱片机的机械臂匀速接近唱片磁盘,同时唱片磁盘也 ...

  2. [原] Unity调用android版新浪微博

    本文提供unity调用微博android版 SDK 分享图片,现有sdk支持路径调用分享图片 雨凇MOMO已实现; 本文教会大家如何通过byte流分享图片(官方API pic true binary ...

  3. 飞行熊猫游戏源码android版

    这款游戏是前一段时间完成的一个项目,飞行熊猫游戏源码android版,飞行熊猫基于cocos2d游戏引擎开发,包括了谷歌admob广告支持,没有任何版权问题,大家可以自由修改和上传应用商店. 1.本游 ...

  4. android 校讯通 源码,校讯通Android版使用说明.doc

    校讯通Android版使用说明 说明: 以下截图均来自PC模拟器,实际效果会根据手机的Android系统版本高低,像素高低,分辨率大小,屏幕尺寸大小而有所差异,最终效果以自身手机为准! 下载校讯通An ...

  5. 京东商城Android版客户端 安装到手机上就能轻松购物

    京东商城据悉将于近日正式推出"京东商城Android版"客户端,显示出国内电子商务平台全面进驻安卓Android智能手机及平板设备平台的趋势愈演愈烈."京东商城Andro ...

  6. android 人生日历,android版人生日历日子怎么用 安卓版人生日历日子使用教程

    人生日历android版新发3.3.05.10版本,新增日子功能,那么android版人生日历日子怎么用呢?今天小编就为大家分享安卓版人生日历日子使用教程,一起来看看吧! 人生日历的日子,设计成四叶草 ...

  7. Android版俄罗斯方块的实现

    学习Android的基本开发也有一段时间了,可是由于没有常常使用Android渐渐的也就忘记了. Android编程学的不深,不过为了对付逆向,可是有时还是会感到力不从心的.毕竟不是一个计算机专业毕业 ...

  8. 网易云音乐Android版使用的开源组件

    转自:http://www.jianshu.com/p/f31ab96a32f3 网易云音乐Android版从第一版使用到现在,全新的 Material Design 界面,更加清新.简洁.同样也是音 ...

  9. 看得见的数据结构Android版之数组表(数据结构篇)

    零.前言: 一讲到装东西的容器,你可能习惯于使用ArrayList和数组,你有想过ArrayList和数组的区别吗? Java的类起名字都不是随便乱起的,一般前面是辅助,后面是实质:ArrayList ...

  10. android+busybox+编译,Android版busybox编译

    Android版busybox编译 1下载busybox源码 2解压 tar -xvf busybox-1.23.2.tar.bz2 3 android版的配置脚本 解压后的源码里,configs文件 ...

最新文章

  1. 机器学习算法常用指标总结
  2. Windows XP Professional系统修复的操作方法
  3. 两周的时间教会我,要低头做人(jQuery实现京东购物车)
  4. java访问远程共享文件
  5. Drools DMN最新开源引擎性能改进
  6. 如何解读Nginx源码
  7. python使用with无需显示关闭文件
  8. matlab中rgb转hsv,matlab实现RGB与HSV(HSB)、HSL和HSI的色彩空间互转
  9. R语言编程艺术(4)R对数据、文件、字符串以及图形的处理
  10. Python学习02 列表 List
  11. 使用MySQL8.0以上版本和MySQL驱动包8.0以上出现的问题
  12. java.sql包是jdbc_sqljdbc4.jar官方下载|
  13. MySQL gtid purge_MySQL中set gtid_purged的行为变更及对备份恢复的影响
  14. 如何让ARM板开机启动Qt
  15. 在win10本地开发springboot项目能上传图片,并能通过URL直接从浏览器访问,但是部署到服务器上后能上传文件,但是通过浏览器无法访问图片
  16. 安装XAMPP端口冲突问题
  17. 喜马拉雅的增量市场,AIOT能够承载多少空间?
  18. 一看就懂的JS抽象语法树
  19. Streams AQ: qmn coordinator waiting for slave to start
  20. 生成用符号拼成的字符//字符画

热门文章

  1. 642-825 认证题库
  2. opencv for python (6) 改变一幅图的特定区域 (往一幅图片上加标志)
  3. 程序员简历优化指南-安晓辉-专题视频课程
  4. 搞ERP的和搞低代码的别鸡同鸭讲,还是走着瞧吧
  5. excel打开提示不适合这台计算机,《win10提示excle安装》 win10 :Excel文件打不开、显示“此应用无法在你的电脑上运行”怎么办?...
  6. C compiler cannot create executables问题
  7. ubuntu 终端显示英文,桌面环境显示中文方法
  8. Win10 20H2系统任务管理器切换到性能页蓝屏解决方案
  9. android studio记账,Android Studio--家庭记账本(三)
  10. 推出更安静的通知权限界面