四则运算相关
在Android中实现数学的四则运算,还是很有难度。在老师帮助下,借助递归思想,简单而又准确的实现了预期结果。下面开始分析相关代码:
获取算式的长度
public int getLength() {
return (int) (Math.random() * 3 + 2);
}
//Math.random()生成为[0,1)的double类型数字
//所以(int) (Math.random() * 3 + 2)生成的数字为[2,5)的int数字 也就是2,3,4 以下同理
获取随机数字
public int getNum() {
return (int) (Math.random() * 99 + 1);
//随机生成[1,100)int数字
}
获取随机运算符
List<String> markList;//存储运算符号的list
markList = new ArrayList<>();
markList.add("+");
markList.add("-");
markList.add("×");
markList.add("÷");
public String getMark() {
return markList.get((int) (Math.random() * 4));
//随机生成[0,4)int数字 及0,1,2,3
}
生成随机算式
index作用下文会有说明
String equation;
switch (getLength()) {
case 2:
index=0;
equation = getNum() + getMark() + getNum();
tv_equation.setText(equation+" = "+cal(equation));
break;
case 3:
index=0;
equation = getNum() + getMark() + getNum() + getMark() + getNum();
tv_equation.setText(equation+" = "+cal(equation));
break;
case 4:
index=0;
equation = getNum() + getMark() + getNum() + getMark() + getNum() + getMark() + getNum();
tv_equation.setText(equation+" = "+cal(equation));
break;
}
运算结果(核心算法)
参数equation为传入算式。
在此方法中,首先获取四个运算符*存在于此算式的什么位置,若不存在,则indexof返回-1。
首先以加减符号的位置作判定。若存在加减法,则以加减符号为界限进行字符串裁剪
例如 3+2×4÷3 根据此算法,裁剪为 3 2×4÷3,即 3+(2×4÷3)
特殊说明(大坑)
减法特殊,用数学里面的话说,即变号
例如 3-3-45,若按前半部分算法则为3-(3-45),实际应为3-(3+4*5)
所以我加入index进行判定,每进入自身,也就是递归1次后,把index+1。在减法判定中,对index进行判定,如果index不为0,则说明已经是第二次或更多次递归了。则改变符号。
//返回结果
public float cal(String equation) {
int mark1 = equation.indexOf("+");
int mark2 = equation.indexOf("-");
int mark3 = equation.indexOf("×");
int mark4 = equation.indexOf("÷");
if (mark1 != -1) {
return cal(equation.substring(0, mark1)) + cal(equation.substring(mark1 + 1));
}
if (mark2 != -1) {
if (index++>0){
return cal(equation.substring(0, mark2)) - -cal(equation.substring(mark2 + 1));
}
return cal(equation.substring(0, mark2)) - cal(equation.substring(mark2 + 1));
}
if (mark3 != -1) {
return cal(equation.substring(0, mark3)) * cal(equation.substring(mark3 + 1));
}
if (mark4 != -1) {
return cal(equation.substring(0, mark4)) / cal(equation.substring(mark4 + 1));
}
return Float.parseFloat(equation);
}
全文代码
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_equation;
private Button btn_next;
List<String> markList;//存储运算符号的list
int index ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
markList = new ArrayList<>();
markList.add("+");
markList.add("-");
markList.add("×");
markList.add("÷");
}
private void initView() {
tv_equation = (TextView) findViewById(R.id.tv_equation);
btn_next = (Button) findViewById(R.id.btn_next);
btn_next.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_next:
String equation;
switch (getLength()) {
case 2:
index=0;
equation = getNum() + getMark() + getNum();
tv_equation.setText(equation+" = "+cal(equation));
break;
case 3:
index=0;
equation = getNum() + getMark() + getNum() + getMark() + getNum();
tv_equation.setText(equation+" = "+cal(equation));
break;
case 4:
index=0;
equation = getNum() + getMark() + getNum() + getMark() + getNum() + getMark() + getNum();
tv_equation.setText(equation+" = "+cal(equation));
break;
}
break;
}
}
//获取算式的长度
//Math.random()生成为[0,1)的double类型数字
//所以(int) (Math.random() * 3 + 2)生成的数字为[2,5)的int数字 也就是2,3,4 以下同理
public int getLength() {
return (int) (Math.random() * 3 + 2);
}
//获取随机运算符号
public String getMark() {
return markList.get((int) (Math.random() * 4));
}
//获取数字
public int getNum() {
return (int) (Math.random() * 99 + 1);
}
//返回结果
public float cal(String equation) {
int mark1 = equation.indexOf("+");
int mark2 = equation.indexOf("-");
int mark3 = equation.indexOf("×");
int mark4 = equation.indexOf("÷");
if (mark1 != -1) {
return cal(equation.substring(0, mark1)) + cal(equation.substring(mark1 + 1));
}
if (mark2 != -1) {
if (index++>0){
return cal(equation.substring(0, mark2)) - -cal(equation.substring(mark2 + 1));
}
return cal(equation.substring(0, mark2)) - cal(equation.substring(mark2 + 1));
}
if (mark3 != -1) {
return cal(equation.substring(0, mark3)) * cal(equation.substring(mark3 + 1));
}
if (mark4 != -1) {
return cal(equation.substring(0, mark4)) / cal(equation.substring(mark4 + 1));
}
return Float.parseFloat(equation);
}
}