1.在源代码的基础上,增加了选项,其执行为随机若干个数,与随机数代表的字符相连,成为一个字符串。将其转换为后则表达式,并放入栈中运算。
2.代码
package 课堂;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.text.NumberFormat;
public class TheAirthmeticOperation_Fuction
{
private final static char ADD = '+';
private final static char SUBTRACT = '-';
private final static char MULTIPLY = '*';
private final static char DIVIDE = '/';
private Stack <Character> stack;//构造函数初始化栈
public TheAirthmeticOperation_Fuction()
{
stack = new Stack<Character>();
}
Scanner sc = new Scanner(System.in);
public int Randomnumber(int n)//重写的随机函数,避免随机出零。
{
int number = 0;
while(number == 0)
{
number = new Random().nextInt(n);
}
return number;
}
public int Setnumber()//设置数组,及习题数量。
{
int n = 30;
System.out.println("输入数组数量");
n = sc.nextInt();
return n;
}
public int Setrange()//设置随机数范围。
{
int n = 100;
System.out.println("输入数值范围");
n = sc.nextInt();
return n;
}
public int Set_mul_and_div()//设置习题中是否有乘除法。
{
int n = 0;int m = 2;
System.out.println("是否有乘除法:(0:无 1:有)");
n = sc.nextInt();
if(n == 0)
{
m = 2;
}
else
m = 4;
return m;
}
public int Printmethod()//设置输出习题方式。
{
int n = 0;
System.out.println("打印方式:(0:直接输出 1:打印到txt)");
n = sc.nextInt();
return n;
}
public int Is_customization()//是否定制多数运算。
{
int n = 0;
System.out.println("是否定制多数运算:(0:否 1:是)");
n = sc.nextInt();
return n;
}
public int customization_number()//设置多数运算数量。
{
int n = 0;
System.out.println("输入多数运算数量");
n = sc.nextInt();
return n;
}
public int customization_brackets()//设置多数运算数量。
{
int n = 0;
System.out.println("十位数运算是否有括号:(0:否 1:是)");
n = sc.nextInt();
return n;
}
public String customization(int n)//定制多数习题。
{
int []Array = new int[10];//定义一个数组。
int []a = new int[10];//控制数与数之间符号。
int []b = new int[20];//控制在某位值是否出现括号。
String []brackets = new String[20];//用于存放括号。
String str = "";
String str1 = "";
for(int i = 0;i < 10;i++)//随机数。
{
a[i] = new Random().nextInt(4);
Array[i] = Randomnumber(20);
}
for(int i = 0;i < 20;i++)//初始化括号数组。
{
brackets[i] = "";
}
if(n == 1)//用户选择定制。
{
for(int i = 0;i < 20;i += 2)
{
b[i] = new Random().nextInt(4);/*为b[i生成随机数,当b[i]为零时,
brackets[i]添加左括号,brackets[i + 3]添加右括号,括号套括号未考虑。*/
if(i >= 0 && i < 17)//i > 16 时,没有位置添加右括号,遂向下执行。
{
if(b[i] == 0)
{
brackets[i] += "(";
brackets[i + 3] += ")";
i += 2;
}
}
}
}
for(int i = 0;i < 9;i++)//将数,括号,符号相连。
{
str += brackets[2 * i] + Array[i] + brackets[2 * i + 1];
str = Symbol(str,a[i]);
}
str += Array[9] + brackets[19];
str1 = analysisString(str);
System.out.println(str1);
int result = evaluate(str1);//计算
System.out.println("\n这个表达式的值是:" + result);
str += " =";
return str;
}
private String analysisString(String str)
{
String str1 = "";
for (int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if (c == '+' || c == '-')
{
if (stack.isEmpty() || stack.peek() == '(')
{
stack.push(c);
}
else
{
while (!stack.isEmpty()&& (stack.peek() == '*' || stack.peek() == '/'|| stack.peek() == '+' || stack.peek() == '-'))
{
/*System.out.print(stack.pop());*/
str1 += stack.pop() + " ";
}
stack.push(c);
}
}
else if (c == '*' || c == '/')
{
if (stack.isEmpty() || stack.peek() == '+'|| stack.peek() == '-' || stack.peek() == '(')
{
stack.push(c);
}
else
{
while (!stack.isEmpty()
&& (stack.peek() == '/' || stack.peek() == '*'))
{
/*System.out.print(stack.pop());*/
str1 += stack.pop() + " ";
}
stack.push(c);
}
}
else if (c == '(')
{
stack.push(c);
}
else if (c == ')')
{
char temp = ' ';
while ((temp = stack.pop()) != '(')
{
/*System.out.print(temp);*/
str1 += temp + " ";
}
}
else
{
/*System.out.print(c);*/
str1 += c + " ";
}
}
if (!stack.isEmpty())
{
while (!stack.isEmpty())
{
System.out.print(stack.pop());
}
}
return str1;
}
public int evaluate(String expresion)
{
double op1,op2;//临时变量,保存操作符和中间结果
Character result = 0;
String token;//字符
@SuppressWarnings("resource")
Scanner parser = new Scanner(expresion);//循环获取每一个字符
while (parser.hasNext())
{
token = parser.next();
//如果是表达式,就弹出
if(isOperator(token))
{
op2 = (stack.pop()).charValue();
op1 = (stack.pop()).charValue();//计算中间结果
result = evaluateSingleOperator(token.charAt(0),op1,op2);//将中间结果压入栈
stack.push(result);
}
else//不是操作符就直接压入栈
{
stack.push((char) Integer.parseInt(token));
}
}
return result;
}
private Character evaluateSingleOperator(char operator,double op1,double op2)
{
Character result = 0;//分情况计算
switch(operator)
{
case ADD:
result = (char) (op1 + op2);
break;
case SUBTRACT:
result = (char) (op1 - op2);
break;
case MULTIPLY:
result = (char) (op1 * op2);
break;
case DIVIDE:
result = (char) (op1 / op2);
break;
}
return result;
}
private boolean isOperator(String token)
{
return(token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/"));
}
public void Setremainder(int []Array1,int []Array2,int num,int n2)
{//若设置除法无余数,另Array1[i] < Array2[i]。
if(n2 == 2)
{
return;
}
int n = 0;
System.out.println("除法是否有余数:(0:无 1:有)");
n = sc.nextInt();
if(n == 0)
{
for(int i = 0;i < num;i++)
{
Absolutevaluecomparison(Array1,Array2,i);
}
}
else
return;
}
public void Setnegativenumber(int []Array1,int []Array2,int num)
{//若设置有负数,随机为Array[i]添加符号,i为随机
int n = 0;int stochastic1 = 0;int stochastic2 = 0;
System.out.println("运算有无负数:(0:无 1:有)");
n = sc.nextInt();
if(n == 0)
{
return;
}
else
{
while(true)
{
stochastic1 = Randomnumber(num);
stochastic2 = Randomnumber(num);
Array1[stochastic1] = -Array1[stochastic1];
Array2[stochastic2] = -Array2[stochastic2];
if(stochastic1 == 1)//当stochastic1 = 1 时,跳出循环。
{
break;
}
}
}
}
public void Absolutevaluecomparison(int []Array1,int []Array2,int i)
{//使两个数最小的数放在Array[i]中,保证无余数或分数为真分数。
if(Math.abs(Array1[i]) > Math.abs(Array2[i]))
{
int t = Array1[i];
Array1[i] = Array2[i];
Array2[i] = t;
}
}
public int[] Isrepetition(int[] Array,int num,int n1)
{//删除Array数组中重复元素,缺省数重新随机(此处未考虑随机后还有相等情况,因重复几率较低)。
int length = 1;
boolean flag = false;
for(int i = 1;i < Array.length;i++)
{
for(int j = 0;j < length;j++)
{
if(Array[i] == Array[j])
{
flag = true;
}
}
if(!flag)
{
Array[length] = Array[i];
length++;
}
flag = false;
}
int[] newArray = new int[num];
System.arraycopy(Array,0,newArray,0,length);
for(int i = length;i < num;i++)
{
newArray[i] = Randomnumber(n1);
}
return newArray;
}
public String Symbol(String str,int value)
{//根据值为str添加符号。
if(value == 0)
{
str += " + ";
}
if(value == 1)
{
str += " - ";
}
if(value == 2)
{
str += " * ";
}
if(value == 3)
{
str += " / ";
}
return str;
}
public void integerOperationAnswer(int number1,int number2,int value)
{
NumberFormat DF = NumberFormat.getNumberInstance();
System.out.println("请输入结果(给出两位小数)");
double answer = 0;
double result = 0;
String s1 = "1",s2 = "0";
if(value != 3)
{
answer = sc.nextInt();
}
else
{
answer = sc.nextFloat();
DF.setMaximumFractionDigits(2);
s1 = DF.format(answer);
}
if(value == 0)
{
result = number1 + number2;
}
if(value == 1)
{
result = number1 - number2;
}
if(value == 2)
{
result = number1 * number2;
}
if(value == 3)
{
double number3 = number1;
double number4 = number2;
result = (double)(number3 / number4);
s2= DF.format(result);
}
if(result == answer && value != 3)
{
System.out.println("结果正确");
}
else if(s1.equals(s2) && value == 3)
{
System.out.println("结果正确");
}
else
System.out.println("结果错误");
}
public void fractionOperationAnswer(int number1,int number2,int number3,int number4,int value)
{
int numberator = 0;
int denominator = 0;
String result = "1";
String answer;
System.out.println("请输入结果(给出最简分数)");
answer = sc.next();
if((number1 < 0 && number2 < 0) || (number1 > 0 && number2 < 0))
{
number1 = -number1;
number2 = -number2;
}
if((number3 < 0 && number4 < 0) || (number3 > 0 && number4 < 0))
{
number3 = -number3;
number4 = -number4;
}
if(value == 0)
{
number1 *= number4;
number3 *= number2;
numberator = number1 + number3;
denominator = number2 * number4;
}
if(value == 1)
{
number1 *= number4;
number3 *= number2;
numberator = number1 - number3;
denominator = number2 * number4;
}
if(value == 2)
{
numberator = number1 * number3;
denominator = number2 * number4;
}
if(value == 3)
{
int n = number3;
number3 = number4;
number4 = n;
numberator = number1 *