结对小伙伴:张勋   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相关推荐

  1. 四则运算安卓版ver.mk2

    做了一点微小的变动....非常微小...不过美观点了就是...那就是----加背景! 变更后的部分代码: 1 <LinearLayout xmlns:android="http://s ...

  2. 好分数阅卷3.0_好分数app下载-好分数网查成绩安卓版app v3.6.4.1-清风安卓软件网...

    好分数网查成绩安卓版app:是一个提供快速成绩查询服务的平台,让用户可以随时随地查看成绩,以及分析离自己理想的大学相差的分数.在这里,可以查看自己的试卷,分析错题,快速提高学习.好分数app,查成绩也 ...

  3. 智能生活 App 垂直品类- IPC SDK 架构及快速集成配置(安卓版)

    除了通用设备功能的应用开发,针对部分常见的全屋智能场景设备,智能生活 App SDK 提供了单独的垂直品类 SDK.包括智能摄像机 SDK.智能门锁 SDK.扫地机机器人 SDK.智能照明控制 SDK ...

  4. 安卓可以用计算机隐藏照片吗,用美图看看安卓版随心隐藏私人图片!

    手机本来就是一件很私人的物品,所以,如果存在手里的图片随便都可以让人翻阅的话,那私密性就没有任何保障了.如何不让其他人轻松就能看到.但自己可以随意调出手机里的私人图片呢?答案就是用美图看看安卓版的图片 ...

  5. android手机播放pc音乐播放器,最强手机音乐播放器?Foobar2K安卓版体验

    说到最强大的PC音乐播放器,相信很多朋友,特别是HiFi发烧友,会把选票投给Foobar2000.的确,在PC平台上,Foobar2000的优势非常巨大.例如它能够自由定制界面,虽然原生界面很简陋,但 ...

  6. 安卓版文字扫描识别软件

    安卓版文字扫描识别软件 文字识别软件被越来越多的人使用,在使用的过程中也发现了一些问题.总结这些问题发现,很多人对软件能够批量识别这个问题比较关注.如果实现批量识别就可以节省时间.但是一些软件还不能实 ...

  7. 首款AI看球机器人亮相北京,已上线IOS版和安卓版

    3月9日,魔方元科技在北京举办"机器人陪你看世界杯"为主题的产品沟通分享会,发布了其自主研发的产品"AI球".据悉,"AI球"是首款立足于足 ...

  8. reddit android app,reddit安卓版app

    reddit安卓版app是一款社交聊天app软件,趣味互动丰富新闻内容,最佳排行兴趣排序,全新新闻社交方式,感兴趣的小伙伴快来下载reddit安卓版app体验吧. reddit安卓版app介绍 Red ...

  9. 谷歌浏览器安卓版_谷歌翻译(在线翻译)下载-谷歌翻译下载安装安卓版v5.12.0...

    软件介绍 谷歌翻译安卓版是一款可以很快进行翻译的app,用户可以利用文档扫描,快速翻译,出国旅游,商务翻译都可以使用这款软件.谷歌翻译安卓版功能很全面,超多的语音翻译可以选择,实现在线翻译,是全世界通 ...

最新文章

  1. Leetcode812.Largest Triangle Area最大三角形面积
  2. SQL语言学习(六)分组函数学习
  3. 多线程存数据mysql_java 多线程存储数据库
  4. ASP.NET 5 入门(1) - 建立和开发ASP.NET 5 项目
  5. linux 批量部署 pdf,Linux服务之批量部署篇
  6. encipher.min.php,陌屿授权系统(5.7)最新版 网站授权 - 下载 - 搜珍网
  7. 单独安装想要的office_安装OFFICE不再求人,最省心的方法
  8. 【Flink】数据传输 挖个坑 把自己埋了 ClassCastException String cannot be cast to [LJava.lang.String
  9. python and or优先级_python的and和or优先级
  10. BaseService代码示例
  11. 物联网核心安全系列——智能门锁安全问题
  12. pytorch CNN
  13. 故宫元宵灯会票务系统崩溃背后:年游客达1700万人
  14. php文件打开老是自动下载
  15. Simon Game实现过程记录
  16. week8-csp-B(HRZ学英语)
  17. Matlab微分方程求解
  18. PowerBuilder常用函数
  19. EEGLAB系列教程5:数据预处理2(ICA去伪迹)
  20. 【MM小贴士】关于MR21修改物料价格与账期的关系

热门文章

  1. 1-6月中国ADAS供应商占比9% 又一家零部件巨头全面布局智驾新赛道
  2. hadoop进入退出安全模式
  3. android 中文编码
  4. 详解生物地理学优化(BBO)算法(一)
  5. 9.7-一定要开始学了
  6. nodejs request库拉取jsp接口 gb2312、GBK中文乱码解决方法
  7. MacOS-MacAPP使用Main.storyboard启动视图程序踩坑
  8. linux下检查是否安装过某软件包(gcc,pcre-devel,zlib-devel,openssl-devel)
  9. 视频采集工具 youtube-dl 接口介绍
  10. 【Cover Letter 】SCI 投稿加分必备,手把手教你写 投稿Cover Letter