package formulaPlus;
import java.util.*;
public class RandomFormula {
public int numberTotal;//运算操作数
public int formulaTotal;//运算式数量
public int numberRange;//数值范围
public boolean includeMulAndDiv;//是否包含乘除
public boolean includeNegNum;//是否包含负数
public RandomFormula() {
this.numberTotal = 2;
this.formulaTotal = 30;
this.numberRange = 100;
this.includeMulAndDiv = false;
this.includeNegNum = false;
}
public RandomFormula(int numberTotal, int formulaTotal, int numberRange, boolean includeMulAndDiv,
boolean includeNegNum) {
this.numberTotal = numberTotal;
this.formulaTotal = formulaTotal;
this.numberRange = numberRange;
this.includeMulAndDiv = includeMulAndDiv;
this.includeNegNum = includeNegNum;
}
// 获取随机数
public int getRandomNumber() {
Random rand = new Random();
if (this.includeNegNum) {
return (rand.nextInt(this.numberRange) + 1) * (rand.nextDouble() > 0.5 ? 1 : -1);
} else {
return rand.nextInt(this.numberRange) + 1;
}
}
//得到一个随机的运算符
public String getRandomOperator() {
Random rand = new Random();
String[] operations = { "+", "-", "*", "/" };
return operations[rand.nextInt((this.includeMulAndDiv == true) ? 4 : 2)];
}
// 生成算式
public String generateFormula() {
String formula = "";
for (int i = 0; i < this.numberTotal; i++) {
if (i >= this.numberTotal - 1) {
formula += random_kuohao(this.getRandomNumber())+")";
continue;
}
formula +="("+ random_kuohao(this.getRandomNumber()) + " " + this.getRandomOperator() + " ";
}
return formula;
}
//生成算式集合
public List<String> getFormulas() {
List<String> set = new ArrayList<String>();
set.add(this.generateFormula());
String str="/";
while (set.size() <= this.formulaTotal){
for(int i=0;i<set.size();i++)
{
String formula=this.generateFormula();
if(str.equals(formula))
if(this.getAnswer(formula)%1!=0)
continue;
if(!formula.equalsIgnoreCase(set.get(i)))
{
set.add(formula);
break;
}
}
}
return set;
}
//若负数,加括号
public String random_kuohao(int num) {
if (this.getRandomNumber()%2==0) {
return "(" + num ;
}
return +num+")" ;
}
//生成算式结果?
public int getAnswer(String formula) {
int length = 0;
String[] formulaArr = formula.split(" ");
String operators = "+-*/";
Stack<Integer> opNumbers = new Stack<Integer>();
Stack<String> opOperators = new Stack<String>();
opOperators.add("#");
while (length < formulaArr.length) {
String op = formulaArr[length++];
if (operators.indexOf(op) > -1) {
String sign = opOperators.peek();
int priority = compare(op, sign);
if (priority >= 0) {
opNumbers.add(compute(opOperators, opNumbers));
opOperators.add(op);
} else {
opOperators.add(op);
}
continue;
}
opNumbers.add(Integer.parseInt(op.replace("(", "").replace(")", "")));
}
while (opOperators.peek() != "#") {
opNumbers.add(compute(opOperators, opNumbers));
}
return opNumbers.pop();
}
//比较运算优先级
public int compare(String operator1, String operator2) {
int res = 0;
switch (operator1) {
case "+":
case "-":
if (operator2.equals("+") || operator2.equals("-") || operator2.equals("*") || operator2.equals("/")) {
res = 1;
} else {
res = -1;
}
break;
case "*":
case "/":
if (operator2.equals("*") || operator2.equals("/")) {
res = 1;
} else {
res = -1;
}
break;
}
return res;
}
//算式求值
public int compute(Stack<String> opOperators, Stack<Integer> opNumbers) {
int num2 = opNumbers.pop();
int num1 = opNumbers.pop();
String _op = opOperators.pop();
int result = 0;
switch (_op) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
}
return result;
}
//生成算式结果数组
public int[] getAnswers(List<String> set) {
int[] arr = new int[set.size()];
int i = 0;
for (String str : set) {
arr[i++] = getAnswer(str);
}
return arr;
}
public List<Integer> getAnswer0sList(List<String> set) {
List<Integer> arr = new ArrayList<Integer>();
for (String str : set) {
arr.add( getAnswer(str));
}
return arr;
}
}
package formulaPlus;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class formula {
public void runing(int numtotal,int formtotal,int numrange,boolean muland,boolean negnum) {
int time=5;//one fromula using time
Scanner in=new Scanner(System.in);
System.out.println("请输入 one fromula use time:");
time=in.nextInt();
RandomFormula B=new RandomFormula (numtotal,formtotal,numrange, muland,negnum);
/*try {
RandomFormula B=new RandomFormula (numtotal,formtotal,numrange, muland,negnum);
}catch() {
RandomFormula B=new RandomFormula();
}*/
List<String> test = new ArrayList<String>();
List<String> fals = new ArrayList<String>();
List<Integer> fals_anws = new ArrayList<Integer>();
test=B.getFormulas();
double correctrat=0;
List<Integer> anwser = B.getAnswer0sList(test);
while(true)
{
do {
int all=test.size();
correctrat=0;
if(all==0)
break;
//long startTime = System.currentTimeMillis();
//执行方法
System.out.println(test.size());//test use
for(int i=0;i<test.size();i++)
System.out.println(i+1+". "+test.get(i)+"=");//test use
System.out.println("=");//test use
for(int i=0;i<test.size();i++)
{
/*long endTime = System.currentTimeMillis();// judge time
float excTime = (float) (endTime - startTime) / 1000;
System.out.println("time:"+excTime+" s");
if(excTime>formtotal*2){
System.out.println("超时");
break;
}*/
System.out.print(i+1+". "+test.get(i)+"= ");
System.out.print(" 答案:");
if(anwser.get(i)==in.nextInt())
{
System.out.println(" 答案 is:(tset use)"+anwser.get(i));
correctrat++;
test.remove(i);
anwser.remove(i);
}
fals=test;
fals_anws.addAll(anwser);
/*endTime = System.currentTimeMillis();
excTime = (float) (endTime - startTime) / 1000;
System.out.println("time:"+excTime+" s");
if(excTime>formtotal*time){
System.out.println("超时");
break;
}*/
}
System.out.println("是做下一套题还是做错题 ture or false");
if(in.nextBoolean())
{
break;
}
System.out.println(correctrat/all);
System.out.println("全对了吗,有没有超时,没通过继续吧");
}while((correctrat/=test.size())!=1);
System.out.println("好吧,你过了。");
System.out.println("是否继续 true or false");
if(in.nextBoolean());
break;
}
System.out.println("是否查看错题加答案 true or false");
if(in.nextBoolean());
for(int i=0;i<fals.size();i++)
System.out.println(i+". "+fals.get(i)+"="+fals_anws.get(i));
System.out.println("是否根据错题进行答题 true or false");
if(in.nextBoolean())
{
do {
for(int i=0;i<fals.size();i++)
System.out.println(i+1+". "+fals.get(i)+"=");//test use
System.out.println("=");//test use
for(int i=0;i<test.size();i++)
{
System.out.print(i+1+". "+fals.get(i)+"= ");
if(fals_anws.get(i)==in.nextInt())
{
fals.remove(i);
fals_anws.remove(i);
}
}
System.out.println("是做下一套题还是做错题 ture or false");
if(in.nextBoolean())
{
break;
}
}while(true);
}
}
}
package formulaPlus;
import java.util.*;
public class FormulaPlus {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
/*int numtotal;//运算操作数
int formtotal;//运算式数量
int numrange;//数值范围
boolean muland=false;//是否包含乘除
boolean negnum=false;//是否包含负数
int time=5;//one fromula using time*/
int choice;//0 自主定义 1一年级 2二年级 3三年级 4四年级 5五年级
System.out.println("请输入选择 //0 自主定义 2二年级 3三年级 4四年级 ");
choice=in.nextInt();
switch(choice)
{
case 0:{
formula A=new formula();
System.out.println("请输入 运算操作数 运算式数量 数值范围 /*是否包含乘除 是否包含负数*/ <-(input true or false)");
A.runing(in.nextInt(), in.nextInt(), in.nextInt(), in.nextBoolean(), in.nextBoolean());
break;
}
case 2:{
formula A=new formula();
System.out.println("请输入 算式数量");
A.runing(2, in.nextInt(), 100, true, false);
break;
}
case 3:{
formula A=new formula();
System.out.println("请输入 算式数量");
A.runing(4, in.nextInt(), 1000, true, false);
break;
}
case 4:{
formula A=new formula();
System.out.println("请输入 算式数量");
A.runing(5, in.nextInt(), 1000, true, false);
break;
}
}
}
}