"小明爸爸的四则运算" Android_版
程序一早就写完了,一直没时间写博客,时间也快截止了,还是来写了。
一,需求分析
初步计划是写一个简单的自动控制器给出n道简单的加减乘除的四则运算题目(含表达式和答案),为了方便使用及推广,使用平台android.编译平台eclipse.
用户给出需要的题目量n以及题目内数字个数m,程序取得需求生成n个题目类的实例对象,设置对象的属性数字个数为m,并生成四则运算表达式和答案
二,概要设计
算法方面无特殊,全是基本操作
三,详细设计及编码
安卓基本开发 + 基础程序设计
ActivityMain类
主界面没什么好说的。代码也没几行,获取基本的组件,设置属性,over
EquationActivity类
答题界面,通过主界面交互得到的数据创建适配器,动态显示题目列表
1 package com.magician.mathmagic.ui; 2 3 import android.app.ActionBar; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.widget.ListView; 9 10 import com.magician.mathmagic.adapter.AdapterEquation; 11 import com.magician.mathmagic.controller.Intelligent; 12 import com.magician.mathmagic.model.Equation; 13 14 public class EquationActivity extends Activity { 15 16 private ListView listEquation; 17 private AdapterEquation adapterEquation; 18 private Intelligent intelligent; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_equation); 24 25 ActionBar actionBar = getActionBar(); 26 actionBar.setDisplayHomeAsUpEnabled(true); 27 28 initView(); 29 initData(); 30 } 31 32 private void initView() { 33 34 listEquation = (ListView) findViewById(R.id.list_equation_equationlist); 35 } 36 37 private void initData() { 38 39 int quantity, grade, numbers, bracket; 40 41 Bundle bundle = getIntent().getExtras(); 42 quantity = bundle.getInt("quantity"); 43 grade = bundle.getInt("grade"); 44 numbers = bundle.getInt("numbers"); 45 bracket = bundle.getInt("bracket"); 46 47 intelligent = new Intelligent(quantity, grade, numbers, bracket); 48 adapterEquation = new AdapterEquation(EquationActivity.this, intelligent); 49 int i = 0; 50 for (Equation equation : intelligent.getEquationList()) { 51 i++; 52 System.out.println(i + " " + equation.toString() + " " + equation.getAnswer()); 53 } 54 listEquation.setAdapter(adapterEquation); 55 } 56 57 @Override 58 public boolean onCreateOptionsMenu(Menu menu) { 59 // Inflate the menu; this adds items to the action bar if it is present. 60 getMenuInflater().inflate(R.menu.equation, menu); 61 return true; 62 } 63 64 @Override 65 public boolean onOptionsItemSelected(MenuItem item) { 66 switch (item.getItemId()) { 67 case android.R.id.home: 68 finish(); 69 return true; 70 } 71 return false; 72 } 73 }
AdapterEquation类
自定义的列表适配器,通过界面层传输的数据给出相应的处理之后返回界面效果给界面层
主要是创建一个管理者对象管理四则运算。
1 /************************************************************************************** 2 * 3 * [Project] 4 * MathMagic 5 * [Package] 6 * com.magician.mathmagic.adapter 7 * [FileName] 8 * AdapterEquation.java 9 * [Copyright] 10 * Copyright 2014 brotherYang All Rights Reserved. 11 * [History] 12 * @Version : 1.0.0 13 * @User : Administrator 14 * @Time : 2014-10-7 - 下午8:15:08 15 * @Auther : brotherYang (include18883287887@gamil.com) 16 * @Record : Create 17 * 18 **************************************************************************************/ 19 20 package com.magician.mathmagic.adapter; 21 22 import java.util.HashMap; 23 import java.util.LinkedList; 24 import java.util.Map; 25 26 import android.app.Activity; 27 import android.content.Context; 28 import android.text.Editable; 29 import android.text.TextWatcher; 30 import android.view.LayoutInflater; 31 import android.view.View; 32 import android.view.ViewGroup; 33 import android.widget.BaseAdapter; 34 import android.widget.Button; 35 import android.widget.EditText; 36 import android.widget.TextView; 37 import android.widget.Toast; 38 39 import com.magician.mathmagic.controller.Intelligent; 40 import com.magician.mathmagic.model.Equation; 41 import com.magician.mathmagic.ui.R; 42 43 public class AdapterEquation extends BaseAdapter{ 44 45 private LinkedList<Map<String, String>> aEquationItemData; 46 private Map<String, String> aEditorValue; 47 private Map<String, String> aAnswer; 48 49 private Intelligent intelligent; 50 private Equation equation; 51 private LayoutInflater aInflater; 52 private ViewHolder aHolder; 53 private Activity aContext; 54 55 public AdapterEquation (Context context, Intelligent intelligent) { 56 this.intelligent = intelligent; 57 this.aContext = (Activity) context; 58 this.aInflater = (LayoutInflater) context 59 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 60 61 init(); 62 } 63 64 // 初始化 65 private void init() { 66 aEquationItemData = new LinkedList<Map<String,String>>(); 67 aEditorValue = new HashMap<String, String>(); 68 aAnswer = new HashMap<String, String>(); 69 aEditorValue.clear(); 70 aAnswer.clear(); 71 72 for (int i = 0; i < getCount(); i++) { 73 aEquationItemData.add(i, null); 74 } 75 } 76 77 @Override 78 public int getCount() { 79 return intelligent.getEquationList().size(); 80 } 81 82 @Override 83 public Object getItem(int position) { 84 return intelligent.getEquationList().get(position); 85 } 86 87 @Override 88 public long getItemId(int position) { 89 return position; 90 } 91 92 @Override 93 public View getView(final int position, View convertView, ViewGroup parent) { 94 if (convertView == null) { 95 convertView = aInflater.inflate(R.layout.list_item_equation, null); 96 aHolder = new ViewHolder(); 97 98 aHolder.vBtonSubmit = (Button) convertView.findViewById(R.id.bton_equation_submit); 99 aHolder.vTexvWitchOne = (TextView) convertView.findViewById(R.id.texv_equation_witchone); 100 aHolder.vTexvEquation = (TextView) convertView.findViewById(R.id.texv_equation_content); 101 aHolder.vEditYourAnswer = (EditText) convertView.findViewById(R.id.edit_equation_youranswer); 102 aHolder.vTexvAnswer = (TextView) convertView.findViewById(R.id.texv_equation_rightanswer); 103 aHolder.vEditYourAnswer.setTag(position); 104 aHolder.vBtonSubmit.setTag(position); 105 convertView.setTag(aHolder); 106 convertView.setId(position); 107 ClearHolder(aHolder); 108 } else { 109 aHolder = (ViewHolder) convertView.getTag(); 110 ClearHolder(aHolder); 111 aHolder.vEditYourAnswer.setTag(position); 112 aHolder.vBtonSubmit.setTag(position); 113 } 114 115 equation = intelligent.getEquationList().get(position); 116 aHolder.vRightAnswer = (equation.getAnswer()); 117 aHolder.vTexvWitchOne.setText((position+1) + " : "); 118 aHolder.vTexvEquation.setText(equation.toString()); 119 120 class ATextWatcher implements TextWatcher { 121 public ATextWatcher(ViewHolder holder) { 122 mHolder = holder; 123 } 124 125 private ViewHolder mHolder; 126 127 @Override 128 public void onTextChanged(CharSequence s, int start, 129 int before, int count) { 130 } 131 132 @Override 133 public void beforeTextChanged(CharSequence s, int start, 134 int count, int after) { 135 } 136 137 @Override 138 public void afterTextChanged(Editable s) { 139 if (s != null && !"".equals(s.toString())) { 140 int position = (Integer) mHolder.vEditYourAnswer.getTag(); 141 aEditorValue.put(String.valueOf(position), s.toString()); 142 // 当EditText数据发生改变的时候存到data变量中 143 } 144 } 145 } 146 147 aHolder.vEditYourAnswer.addTextChangedListener(new ATextWatcher(aHolder)); 148 aHolder.vEditYourAnswer.setText(null == aEditorValue.get(String.valueOf(position)) ? "" : aEditorValue.get(String.valueOf(position))); 149 aHolder.vTexvAnswer.setText(null == aEquationItemData.get(position) ? "" : aEquationItemData.get(position).get("answer")); 150 aHolder.vBtonSubmit.setEnabled(!intelligent.getEquationList().get(position).isRight()); 151 aHolder.vBtonSubmit.setOnClickListener(new View.OnClickListener() { 152 @Override 153 public void onClick(View v) { 154 155 // TODO Auto-generated method stub 156 aHolder.vYourAnswer = aEditorValue.get(String.valueOf(position)); 157 aHolder.vRightAnswer = (intelligent.getEquationList() 158 .get(position).getAnswer()); 159 int tPosition = (Integer) v.getTag(); 160 161 if (null == (aHolder.vYourAnswer)) { 162 Toast.makeText(aContext, "题目 " + (tPosition+1) +" 答案为空!", Toast.LENGTH_SHORT).show(); 163 return; 164 } 165 intelligent.getEquationList().get(tPosition).setAnswered(true); 166 if (Double.valueOf(aHolder.vYourAnswer) == (aHolder.vRightAnswer)) { 167 168 aAnswer = new HashMap<String, String>(); 169 aAnswer.put("answer", "回答正确!"); 170 aEquationItemData.remove(tPosition); 171 aEquationItemData.add(tPosition, aAnswer); 172 intelligent.getEquationList().get(tPosition).setRight(true); 173 } else { 174 aAnswer = new HashMap<String, String>(); 175 aAnswer.put("answer", "回答错误!正确答案是 : " + aHolder.vRightAnswer); 176 aEquationItemData.remove(tPosition); 177 aEquationItemData.add(tPosition, aAnswer); 178 } 179 notifyDataSetChanged(); 180 } 181 }); 182 183 return convertView; 184 } 185 186 /** 187 * 188 */ 189 private void ClearHolder(ViewHolder holder) { 190 // TODO Auto-generated method stub 191 holder.vTexvWitchOne.setText(""); 192 holder.vTexvEquation.setText(""); 193 holder.vTexvAnswer.setText(""); 194 holder.vEditYourAnswer.setText(""); 195 } 196 197 public final class ViewHolder { 198 public View vBaseView; 199 public int vPosition; 200 public TextView vTexvWitchOne; 201 public TextView vTexvEquation; 202 public TextView vTexvAnswer; 203 public EditText vEditYourAnswer; 204 public Button vBtonSubmit; 205 206 public String vEquation; 207 public String vYourAnswer; 208 public double vRightAnswer; 209 } 210 }
Intelligent类
管理者,适配器传输的数据给出相应的动作,创建四则运算方程式
1 /** 2 * 方程式集合 3 */ 4 private LinkedList<Equation> equationList; 5 6 /** 7 * 初始化题目数量 8 */ 9 private int quantity; 10 11 /** 12 * 构造方程式 13 */ 14 public Intelligent(int quantity, int grade, int numbers, int bracket) { 15 // TODO Auto-generated constructor stub 16 this.quantity = quantity; 17 18 equationList = new LinkedList<Equation>(); 19 for (int i = 0; i < quantity; i++) { 20 equationList.addLast(new Equation(grade, numbers, bracket)); 21 } 22 }
Equation类
方程式,通过初始化各属性生成四则运算表达式以及答案。
1 /************************************************************************************** 2 * 3 * [Project] 4 * MathMagic 5 * [Package] 6 * com.magician.mathmagic.model 7 * [FileName] 8 * Equation.java 9 * [Copyright] 10 * Copyright 2014 brotherYang All Rights Reserved. 11 * [History] 12 * @Version : 1.0.0 13 * @User : Administrator 14 * @Time : 2014-10-7 - 下午7:22:16 15 * @Auther : brotherYang (include18883287887@gamil.com) 16 * @Record : Create 17 * 18 **************************************************************************************/ 19 20 package com.magician.mathmagic.model; 21 22 import java.text.DecimalFormat; 23 import java.util.LinkedList; 24 import java.util.Random; 25 26 public class Equation { 27 28 /** 29 * 用于存放数的列表 30 */ 31 private LinkedList<String> numberList; 32 33 /** 34 * 用于存放計算符号的列表 35 */ 36 private LinkedList<String> arithmeticList; 37 38 private LinkedList<String> calculateList; 39 40 /** 41 * 初始化题目难度 - 年级(1-6) 42 */ 43 private int grade; 44 45 /** 46 * 初始化几个数字 - 个数(2-3) 47 */ 48 private int numbers; 49 50 /** 51 * 初始化是否有括号 52 */ 53 private int bracket; 54 55 /** 56 * 初始化括号数量 57 */ 58 private int numBracket; 59 60 /** 61 * 算法 - ( " + ", " - ", " * ", " / ") 62 */ 63 public static class Arithmetic { 64 65 public static final int plus = 0; 66 public static final int minus = 1; 67 public static final int multiply = 2; 68 public static final int divide = 3; 69 public static final int bracket = 0; 70 public static final int nobracket = 1; 71 public static final int randombracket = 2; 72 public static final int[] maxnumber = { 10, 30, 60, 100, 500, 1000 }; 73 public static final double wrongnumber = -2147483647; 74 }; 75 76 /** 77 * 方程式的答案 78 */ 79 private double answer; 80 81 /** 82 * 方程式的字符串 83 */ 84 private StringBuilder stringEquation; 85 86 /** 87 * 是否回答 88 */ 89 private boolean isAnswered; 90 91 /** 92 * 是否回答正确 93 */ 94 private boolean isRight; 95 96 /** 97 * @param grade 98 * @param numbers 99 * @param isBracket 100 */ 101 public Equation(int grade, int numbers, int bracket) { 102 this.grade = grade; 103 this.numbers = numbers; 104 this.bracket = bracket; 105 106 if (bracket == Arithmetic.randombracket) { 107 bracket = new Random().nextInt(2); 108 } 109 110 if (bracket == Arithmetic.nobracket) { 111 numBracket = 0; 112 } else { 113 numBracket = new Random().nextInt(numbers - 1); 114 } 115 116 isAnswered = false; 117 isRight = false; 118 numberList = new LinkedList<String>(); 119 arithmeticList = new LinkedList<String>(); 120 calculateList = new LinkedList<String>(); 121 answer = Arithmetic.wrongnumber; 122 stringEquation = new StringBuilder(); 123 124 for (int i = 0; i < numbers; i++) { 125 numberList.add(String.valueOf((new Random() 126 .nextInt(Arithmetic.maxnumber[3]) + 1))); 127 stringEquation.append(numberList.get(i) + " "); 128 if (i < numbers - 1) { 129 arithmeticList.add(generateArithmetic()); 130 stringEquation.append(arithmeticList.get(i) + " "); 131 } 132 } 133 134 calculateAnswer(); 135 } 136 137 /** 138 * 139 */ 140 private String generateArithmetic() { 141 // TODO Auto-generated method stub 142 143 int intArithmetic = new Random().nextInt(4); 144 145 switch (intArithmetic) { 146 case Arithmetic.plus: 147 return "+"; 148 case Arithmetic.minus: 149 return "-"; 150 case Arithmetic.multiply: 151 return "*"; 152 case Arithmetic.divide: 153 return "/"; 154 default: 155 return null; 156 } 157 } 158 159 /** 160 * (non-Javadoc) 161 * 162 * @see java.lang.Object#toString() 163 */ 164 @Override 165 public String toString() { 166 // TODO Auto-generated method stub 167 return stringEquation.toString(); 168 } 169 170 /** 171 * 172 */ 173 private void calculateAnswer() { 174 // TODO Auto-generated method stub 175 176 for (int i = 0; i < numberList.size(); i++) { 177 calculateList.addLast(numberList.get(i)); 178 while (i < arithmeticList.size()) { 179 if (arithmeticList.get(i).equals("*")) { 180 calculateList.addLast(String.valueOf(Double 181 .valueOf(calculateList.removeLast()) 182 * Double.valueOf(numberList.remove(i + 1)))); 183 arithmeticList.remove(i); 184 } else if (arithmeticList.get(i).equals("/")) { 185 calculateList.addLast(String.valueOf(Double 186 .valueOf(calculateList.removeLast()) 187 / Double.valueOf(numberList.remove(i + 1)))); 188 arithmeticList.remove(i); 189 } else { 190 calculateList.addLast(arithmeticList.get(i)); 191 break; 192 } 193 } 194 } 195 196 for (String it : calculateList) { 197 System.out.println(" content " + it); 198 } 199 200 while (!calculateList.isEmpty()) { 201 if (answer == Arithmetic.wrongnumber) { 202 answer = Double.valueOf(calculateList.removeFirst()); 203 continue; 204 } 205 String cString = calculateList.removeFirst(); 206 if (cString.equals("+")) { 207 answer += Double.valueOf(calculateList.removeFirst()); 208 continue; 209 } else if (cString.equals("-")) { 210 answer -= Double.valueOf(calculateList.removeFirst()); 211 continue; 212 } else { 213 System.out.println("Error!"); 214 } 215 } 216 217 // 转换保留最后2为小数 218 DecimalFormat df = new DecimalFormat(".##"); 219 answer = Double.valueOf(df.format(answer)); 220 } 221 222 /** 223 * @return the numbers 224 */ 225 public int getNumbers() { 226 return numbers; 227 } 228 229 /** 230 * @param numbers 231 * the numbers to set 232 */ 233 public void setNumbers(int numbers) { 234 this.numbers = numbers; 235 } 236 237 /** 238 * @return the grade 239 */ 240 public int getGrade() { 241 return grade; 242 } 243 244 /** 245 * @param grade 246 * the grade to set 247 */ 248 public void setGrade(int grade) { 249 this.grade = grade; 250 } 251 252 /** 253 * @return the isBracket 254 */ 255 public int getBracket() { 256 return bracket; 257 } 258 259 /** 260 * @param isBracket 261 * the isBracket to set 262 */ 263 public void setBracket(int bracket) { 264 this.bracket = bracket; 265 } 266 267 /** 268 * @return the answer 269 */ 270 public double getAnswer() { 271 return answer; 272 } 273 274 /** 275 * @param answer 276 * the answer to set 277 */ 278 public void setAnswer(double answer) { 279 this.answer = answer; 280 } 281 282 /** 283 * @return the isAnswered 284 */ 285 public boolean isAnswered() { 286 return isAnswered; 287 } 288 289 /** 290 * @param isAnswered 291 * the isAnswered to set 292 */ 293 public void setAnswered(boolean isAnswered) { 294 this.isAnswered = isAnswered; 295 } 296 297 /** 298 * @return the isRight 299 */ 300 public boolean isRight() { 301 return isRight; 302 } 303 304 /** 305 * @param isRight 306 * the isRight to set 307 */ 308 public void setRight(boolean isRight) { 309 this.isRight = isRight; 310 } 311 }
四,测试及性能测试
经过测试,界面不友好,人机交互不友好,存在逻辑上的小bug.
基本操作无误,可以使用。
性能测试
也算是一个性能分析吧,不过也看不出什么来
五,总结
这只是一次小小的软件工程的作业,但里面收获的真的很多,有软件设计的基本流程的进一步巩固,android平台快速开发的经验,代码规范性,软件设计的思想等等,收获颇丰,期待下一次实战。
软件地址:
http://121.42.29.54/upload/MathMagic.apk
百度云盘:
http://pan.baidu.com/s/1bntShm3