2024.1.28
今天写了四则运算,写死了
package daima;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class zhu {
public static void main(String[] args) throws IOException {
int[] arr=new int[100];
for (int i = 0; i < arr.length; i++)
{
arr[i]=1;
}
zhu nb=new zhu();
ansss ans=new ansss();
Scanner scanner=new Scanner(System.in);
zidingyi z=new zidingyi();
ernianji e=new ernianji();
si s=new si();
san sa=new san();
while(true)
{
System.out.println("1.自定义四则运算");
System.out.println("2.二年级四则运算");
System.out.println("3.三年级四则运算");
System.out.println("4.四年级四则运算");
System.out.println("5.查看错题本");
System.out.print("请输入");
int key=scanner.nextInt();
switch (key)
{
case 1:
boolean ok1=true;
while(ok1){
z.diao();
System.out.println("是否继续答题 1:是 2:否");
int ak=scanner.nextInt();
if(ak==2)
{
ok1=false;
}
}
break;
case 2:
boolean ok2=true;
while(ok2){
e.er();
System.out.println("是否继续答题 1:是 2:否");
int ak=scanner.nextInt();
if(ak==2)
{
ok2=false;
}
}
break;
case 3:
boolean ok3=true;
while(ok3){
sa.diao();
System.out.println("是否继续答题 1:是 2:否");
int ak=scanner.nextInt();
if(ak==2)
{
ok3=false;
}
}
break;
case 4:
boolean ok4=true;
while(ok4){
s.diao();
System.out.println("是否继续答题 1:是 2:否");
int ak=scanner.nextInt();
if(ak==2)
{
ok4=false;
}
}
break;
case 5:
ArrayList list2=nb.wenjian();
ArrayList list3=nb.wenjian2();
System.out.println("今日错题率为"+(double)list2.size()*100/list3.size()+"%");
for (int i = 0; i < list2.size(); i++)
{
String nm= (String) list2.get(i);
System.out.println(nm+"=");
double a= Double.parseDouble(ans.cal(nm));
double b=scanner.nextDouble();
double c=Math.abs(a-b);
if(c<=0.1){
System.out.println("回答正确");
}else{
arr[i]++;
System.out.println("这是你第"+arr[i]+"次做错");
}
}
break;
}
}
}
public ArrayList wenjian()throws IOException{
ArrayList<String>list=new ArrayList<>();
int count=0;
try(Scanner sc=new Scanner(new FileReader("error.txt"))){
//sc.useDelimiter(" ");
while(sc.hasNext()){
list.add(sc.next());
}
}
return list;
}
public ArrayList wenjian2()throws IOException{
ArrayList<String>list=new ArrayList<>();
Scanner ss=new Scanner(System.in);
try(Scanner sc=new Scanner(new FileReader("sum.txt"))){
//sc.useDelimiter(",");
while(sc.hasNext()){
list.add(sc.next());
}
}
return list;
}
}
package daima;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class ansss {
public String cal(String str) {
// 对表达式进行预处理,并简单验证是否是正确的表达式
// 存放处理后的表达式
List<String> list = new ArrayList<>();
char[] arr = str.toCharArray();
// 存放数字临时变量
StringBuffer tmpStr = new StringBuffer();
for (char c : arr) {
// 如果是数字或小数点,添加到临时变量中
if (c >= '0' && c <= '9') {
tmpStr.append(c);
} else if (c == '.') {
if (tmpStr.indexOf(".") > 0) {
throw new RuntimeException("非法字符");
}
tmpStr.append(c);
}
// 如果是加减乘除或者括号,将数字临时变量和运算符依次放入list中
else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')') {
if (tmpStr.length() > 0) {
list.add(tmpStr.toString());
tmpStr.setLength(0);
}
list.add(c + "");
}
// 如果是空格,跳过
else if (c == ' ') {
continue;
} else {
throw new RuntimeException("非法字符");
}
}
if (tmpStr.length() > 0) {
list.add(tmpStr.toString());
}
// 初始化后缀表达式
List<String> strList = new ArrayList<>();
// 运算过程中,使用了两次栈结构,第一次是将中缀表达式转换为后缀表达式,第二次是计算后缀表达式的值
Stack<String> stack = new Stack<>();
// 声明临时变量,存放出栈元素
String tmp;
// 1. 将中缀表达式转换为后缀表达式
for (String s : list) {
// 如果是左括号直接入栈
if (s.equals("(")) {
stack.push(s);
}
// 如果是右括号,执行出栈操作,依次添加到后缀表达式中,直到出栈元素为左括号,左括号和右括号都不添加到后缀表达式中
else if (s.equals(")")) {
while (!(tmp = stack.pop()).equals("(")) {
strList.add(tmp);
}
}
// 如果是加减乘除,弹出所有优先级大于或者等于该运算符的栈顶元素(栈中肯定没有右括号,认为左括号的优先级最低),然后将该运算符入栈
else if (s.equals("*") || s.equals("/")) {
while (!stack.isEmpty()) {
// 取出栈顶元素
tmp = stack.peek();
if (tmp.equals("*") || tmp.equals("/")) {
stack.pop();
strList.add(tmp);
} else {
break;
}
}
stack.push(s);
} else if (s.equals("+") || s.equals("-")) {
while (!stack.isEmpty()) {
// 取出栈顶元素
tmp = stack.peek();
if (!tmp.equals("(")) {
stack.pop();
strList.add(tmp);
} else {
break;
}
}
stack.push(s);
}
// 如果是数字,直接添加到后缀表达式中
else {
strList.add(s);
}
}
// 最后依次出栈,放入后缀表达式中
while (!stack.isEmpty()) {
strList.add(stack.pop());
}
// 2.计算后缀表达式的值
Stack<BigDecimal> newStack = new Stack<>();
for (String s : strList) {
// 若遇运算符,则从栈中退出两个元素,先退出的放到运算符的右边,后退出的放到运算符左边,
// 运算后的结果再进栈,直到后缀表达式遍历完毕
if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
BigDecimal b1 = newStack.pop();
BigDecimal b2 = newStack.pop();
switch (s) {
case "+":
newStack.push(b2.add(b1));
break;
case "-":
newStack.push(b2.subtract(b1));
break;
case "*":
newStack.push(b2.multiply(b1));
break;
case "/":
newStack.push(b2.divide(b1, 9, BigDecimal.ROUND_HALF_UP));
break;
}
}
// 如果是数字,入栈
else {
newStack.push(new BigDecimal(s));
}
}
// 最后,栈中仅有一个元素,就是计算结果
return newStack.peek().toString();
}
}
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.*;
public class ernianji {
public void er() throws IOException {
int count=0;
file file=new file();
Scanner s=new Scanner(System.in);
int i=0;
while(i!=6)
{
String f="";
int a=creatnum(100);
int b=creatnum(100);
if(a<b){
int temp;
temp=a;
a=b;
b=temp;
}
int c=0;
String h=creatchar();
if(h=="+"){
f =a + h + b;
count++;
c=a+b;
System.out.print(f+"=");
int ans=s.nextInt();
int oo=Math.abs(ans-c);
if(oo<=0.1)
{
if(count%3==0)
{
file.creatfile2(f);
count=0;
}
else{
file.creatfile1(f);
}
}else
{
if(count%3==0)
{
file.creatfile2(f);
file.cuo2(f);
count=0;
}else
{
file.creatfile1(f);
file.cuo1(f);
}
}
i++;
}
if(Objects.equals(h, "-")){
f =a + h + b;
count++;
c=a-b;
System.out.print(f+"=");
int ans=s.nextInt();
int oo=Math.abs(ans-c);
if(oo<=0.1)
{
if(count%3==0)
{
file.creatfile2(f);
count=0;
}
else{
file.creatfile1(f);
}
}else
{
if(count%3==0)
{
file.creatfile2(f);
file.cuo2(f);
count=0;
}else
{
file.creatfile1(f);
file.cuo1(f);
}
}
i++;
}
if(h=="*"){
f =a + h + b;
count++;
c=a*b;
System.out.print(f+"=");
int ans=s.nextInt();
int oo=Math.abs(ans-c);
if(oo<=0.1)
{
if(count%3==0)
{
file.creatfile2(f);
count=0;
}
else{
file.creatfile1(f);
}
}else
{
if(count%3==0)
{
file.creatfile2(f);
file.cuo2(f);
count=0;
}else
{
file.creatfile1(f);
file.cuo1(f);
}
}
i++;
}
if(h=="/"){
f =a + h+ b;
count++;
boolean jk=false;
if(a%b==0){
jk=true;
}
if(jk){
c=a/b;
System.out.print(f+"=");
int ans=s.nextInt();
int oo=Math.abs(ans-c);
if(oo<=0.1)
{
if(count%3==0)
{
file.creatfile2(f);
count=0;
}
else{
file.creatfile1(f);
}
}else
{
if(count%3==0)
{
file.creatfile2(f);
file.cuo2(f);
count=0;
}else
{
file.creatfile1(f);
file.cuo1(f);
}
}
i++;
}
}
}
}
public String creatchar()
{
String ch="";
Random random=new Random();
int key=random.nextInt(4);
switch (key){
case 0:ch="+";break;
case 1:ch="-";break;
case 2:ch="*";break;
case 3:ch="/";break;
}
return ch;
}
public int creatnum(int m)
{
Random random=new Random();
return random.nextInt(1,m);
}
public void creatfile(String s) throws IOException
{
File file=new File("sum.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("sum.txt",true))
{
writer.write(s+"\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
public void creatcuoti(String s) throws IOException
{
File file=new File("error.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("error.txt",true))
{
writer.write(s+"\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
}
package daima;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class file {
public void creatfile1(String s) throws IOException
{
File file=new File("sum.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("sum.txt",true))
{
writer.write(s+" ");
} catch (IOException e)
{
e.printStackTrace();
}
}
public void creatfile2(String s) throws IOException
{
File file=new File("sum.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("sum.txt",true))
{
writer.write(s+"\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
public void cuo1(String s) throws IOException
{
File file=new File("error.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("error.txt",true))
{
writer.write(s+"\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
public void cuo2(String s) throws IOException
{
File file=new File("error.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("error.txt",true))
{
writer.write(s+"\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
}
package daima;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.*;
public class san {
public void diao() throws IOException {
file file=new file();
san o=new san();
Scanner scanner=new Scanner(System.in);
int count=0;
for(int i=0;i<6;i++)
{
count++;
String shizi= o.creatshi(3,100);
System.out.print(shizi+"=");
String anss=o.cal(shizi);
double number = Double.parseDouble(anss);
double a=scanner.nextDouble();
double c=Math.abs(number-a);
if(c<=0.1)
{
if(i%3==0)
{
file.creatfile2(shizi);
count=0;
}
else{
file.creatfile1(shizi);
}
}else
{
if(count%3==0)
{
file.creatfile2(shizi);
file.cuo2(shizi);
count=0;
}else
{
file.creatfile1(shizi);
file.cuo1(shizi);
}
}
}
}
public String creatshi(int n,int m)
{
Scanner s=new Scanner(System.in);
Random random=new Random();
String f="";
f=f+creatnum(m);
boolean kh=false;
int c= random.nextInt(1,n-1);
for(int i=0;i<n;i++)
{
if(kh)
{
if(i==0)
{
f = f + creatchar() + "(" + creatnum(m);
}else if(i==c){
f=f+ creatchar()+creatnum(m)+")";
}else {
f=f+creatchar()+creatnum(m);
}
}else
{
f=f+creatchar()+creatnum(m);
}
}
return f;
}
public String creatchar()
{
String ch="";
Random random=new Random();
int key=random.nextInt(4);
switch (key){
case 0:ch="+";break;
case 1:ch="-";break;
case 2:ch="*";break;
case 3:ch="/";break;
}
return ch;
}
public boolean creatkh()
{
boolean key=false;
Random random=new Random();
int r= random.nextInt(3);
if(r<1){
key=true;
return key;
}
key=false;
return key;
}
public int creatnum(int m)
{
Random random=new Random();
return random.nextInt(1,m);
}
public String cal(String str) {
// 对表达式进行预处理,并简单验证是否是正确的表达式
// 存放处理后的表达式
List<String> list = new ArrayList<>();
char[] arr = str.toCharArray();
// 存放数字临时变量
StringBuffer tmpStr = new StringBuffer();
for (char c : arr) {
// 如果是数字或小数点,添加到临时变量中
if (c >= '0' && c <= '9') {
tmpStr.append(c);
} else if (c == '.') {
if (tmpStr.indexOf(".") > 0) {
throw new RuntimeException("非法字符");
}
tmpStr.append(c);
}
// 如果是加减乘除或者括号,将数字临时变量和运算符依次放入list中
else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')') {
if (tmpStr.length() > 0) {
list.add(tmpStr.toString());
tmpStr.setLength(0);
}
list.add(c + "");
}
// 如果是空格,跳过
else if (c == ' ') {
continue;
} else {
throw new RuntimeException("非法字符");
}
}
if (tmpStr.length() > 0) {
list.add(tmpStr.toString());
}
// 初始化后缀表达式
List<String> strList = new ArrayList<>();
// 运算过程中,使用了两次栈结构,第一次是将中缀表达式转换为后缀表达式,第二次是计算后缀表达式的值
Stack<String> stack = new Stack<>();
// 声明临时变量,存放出栈元素
String tmp;
// 1. 将中缀表达式转换为后缀表达式
for (String s : list) {
// 如果是左括号直接入栈
if (s.equals("(")) {
stack.push(s);
}
// 如果是右括号,执行出栈操作,依次添加到后缀表达式中,直到出栈元素为左括号,左括号和右括号都不添加到后缀表达式中
else if (s.equals(")")) {
while (!(tmp = stack.pop()).equals("(")) {
strList.add(tmp);
}
}
// 如果是加减乘除,弹出所有优先级大于或者等于该运算符的栈顶元素(栈中肯定没有右括号,认为左括号的优先级最低),然后将该运算符入栈
else if (s.equals("*") || s.equals("/")) {
while (!stack.isEmpty()) {
// 取出栈顶元素
tmp = stack.peek();
if (tmp.equals("*") || tmp.equals("/")) {
stack.pop();
strList.add(tmp);
} else {
break;
}
}
stack.push(s);
} else if (s.equals("+") || s.equals("-")) {
while (!stack.isEmpty()) {
// 取出栈顶元素
tmp = stack.peek();
if (!tmp.equals("(")) {
stack.pop();
strList.add(tmp);
} else {
break;
}
}
stack.push(s);
}
// 如果是数字,直接添加到后缀表达式中
else {
strList.add(s);
}
}
// 最后依次出栈,放入后缀表达式中
while (!stack.isEmpty()) {
strList.add(stack.pop());
}
// 2.计算后缀表达式的值
Stack<BigDecimal> newStack = new Stack<>();
for (String s : strList) {
// 若遇运算符,则从栈中退出两个元素,先退出的放到运算符的右边,后退出的放到运算符左边,
// 运算后的结果再进栈,直到后缀表达式遍历完毕
if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
BigDecimal b1 = newStack.pop();
BigDecimal b2 = newStack.pop();
switch (s) {
case "+":
newStack.push(b2.add(b1));
break;
case "-":
newStack.push(b2.subtract(b1));
break;
case "*":
newStack.push(b2.multiply(b1));
break;
case "/":
newStack.push(b2.divide(b1, 9, BigDecimal.ROUND_HALF_UP));
break;
}
}
// 如果是数字,入栈
else {
newStack.push(new BigDecimal(s));
}
}
// 最后,栈中仅有一个元素,就是计算结果
return newStack.peek().toString();
}
public void creatfile(String s) throws IOException
{
File file=new File("sum.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("sum.txt",true))
{
writer.write(s+"\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
public void cuo(String s) throws IOException
{
File file=new File("error.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("error.txt",true))
{
writer.write(s+"\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
}
package daima;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.*;
public class si {
public void diao() throws IOException {
file file=new file();
si o=new si();
Scanner scanner=new Scanner(System.in);
int count=0;
for(int i=0;i<6;i++)
{
count++;
String shizi= o.creatshi(4,100);
System.out.print(shizi+"=");
String anss=o.cal(shizi);
double number = Double.parseDouble(anss);
double a=scanner.nextDouble();
double c=Math.abs(number-a);
if(c<=0.1)
{
if(i%3==0)
{
file.creatfile2(shizi);
count=0;
}
else{
file.creatfile1(shizi);
}
}else
{
if(count%3==0)
{
file.creatfile2(shizi);
file.cuo2(shizi);
count=0;
}else
{
file.creatfile1(shizi);
file.cuo1(shizi);
}
}
}
}
public String creatshi(int n,int m)
{
Scanner s=new Scanner(System.in);
Random random=new Random();
String f="";
f=f+creatnum(m);
boolean kh=false;
if(n>2)
{
kh=creatkh();
}
int c= random.nextInt(1,n-1);
for(int i=0;i<n;i++)
{
if(kh)
{
if(i==0)
{
f = f + creatchar() + "(" + creatnum(m);
}else if(i==c){
f=f+ creatchar()+creatnum(m)+")";
}else {
f=f+creatchar()+creatnum(m);
}
}else
{
f=f+creatchar()+creatnum(m);
}
}
return f;
}
public String creatchar()
{
String ch="";
Random random=new Random();
int key=random.nextInt(4);
switch (key){
case 0:ch="+";break;
case 1:ch="-";break;
case 2:ch="*";break;
case 3:ch="/";break;
}
return ch;
}
public boolean creatkh()
{
boolean key=false;
Random random=new Random();
int r= random.nextInt(3);
if(r<1){
key=true;
return key;
}
key=false;
return key;
}
public int creatnum(int m)
{
Random random=new Random();
return random.nextInt(1,m);
}
public String cal(String str) {
// 对表达式进行预处理,并简单验证是否是正确的表达式
// 存放处理后的表达式
List<String> list = new ArrayList<>();
char[] arr = str.toCharArray();
// 存放数字临时变量
StringBuffer tmpStr = new StringBuffer();
for (char c : arr) {
// 如果是数字或小数点,添加到临时变量中
if (c >= '0' && c <= '9') {
tmpStr.append(c);
} else if (c == '.') {
if (tmpStr.indexOf(".") > 0) {
throw new RuntimeException("非法字符");
}
tmpStr.append(c);
}
// 如果是加减乘除或者括号,将数字临时变量和运算符依次放入list中
else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')') {
if (tmpStr.length() > 0) {
list.add(tmpStr.toString());
tmpStr.setLength(0);
}
list.add(c + "");
}
// 如果是空格,跳过
else if (c == ' ') {
continue;
} else {
throw new RuntimeException("非法字符");
}
}
if (tmpStr.length() > 0) {
list.add(tmpStr.toString());
}
// 初始化后缀表达式
List<String> strList = new ArrayList<>();
// 运算过程中,使用了两次栈结构,第一次是将中缀表达式转换为后缀表达式,第二次是计算后缀表达式的值
Stack<String> stack = new Stack<>();
// 声明临时变量,存放出栈元素
String tmp;
// 1. 将中缀表达式转换为后缀表达式
for (String s : list) {
// 如果是左括号直接入栈
if (s.equals("(")) {
stack.push(s);
}
// 如果是右括号,执行出栈操作,依次添加到后缀表达式中,直到出栈元素为左括号,左括号和右括号都不添加到后缀表达式中
else if (s.equals(")")) {
while (!(tmp = stack.pop()).equals("(")) {
strList.add(tmp);
}
}
// 如果是加减乘除,弹出所有优先级大于或者等于该运算符的栈顶元素(栈中肯定没有右括号,认为左括号的优先级最低),然后将该运算符入栈
else if (s.equals("*") || s.equals("/")) {
while (!stack.isEmpty()) {
// 取出栈顶元素
tmp = stack.peek();
if (tmp.equals("*") || tmp.equals("/")) {
stack.pop();
strList.add(tmp);
} else {
break;
}
}
stack.push(s);
} else if (s.equals("+") || s.equals("-")) {
while (!stack.isEmpty()) {
// 取出栈顶元素
tmp = stack.peek();
if (!tmp.equals("(")) {
stack.pop();
strList.add(tmp);
} else {
break;
}
}
stack.push(s);
}
// 如果是数字,直接添加到后缀表达式中
else {
strList.add(s);
}
}
// 最后依次出栈,放入后缀表达式中
while (!stack.isEmpty()) {
strList.add(stack.pop());
}
// 2.计算后缀表达式的值
Stack<BigDecimal> newStack = new Stack<>();
for (String s : strList) {
// 若遇运算符,则从栈中退出两个元素,先退出的放到运算符的右边,后退出的放到运算符左边,
// 运算后的结果再进栈,直到后缀表达式遍历完毕
if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
BigDecimal b1 = newStack.pop();
BigDecimal b2 = newStack.pop();
switch (s) {
case "+":
newStack.push(b2.add(b1));
break;
case "-":
newStack.push(b2.subtract(b1));
break;
case "*":
newStack.push(b2.multiply(b1));
break;
case "/":
newStack.push(b2.divide(b1, 9, BigDecimal.ROUND_HALF_UP));
break;
}
}
// 如果是数字,入栈
else {
newStack.push(new BigDecimal(s));
}
}
// 最后,栈中仅有一个元素,就是计算结果
return newStack.peek().toString();
}
public void creatfile(String s) throws IOException
{
File file=new File("sum.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("sum.txt",true))
{
writer.write(s+"\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
public void cuo(String s) throws IOException
{
File file=new File("error.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("error.txt",true))
{
writer.write(s+"\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
}
package daima;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.*;
public class zidingyi {
public void diao() throws IOException {
int count=0;
file file=new file();
zidingyi o=new zidingyi();
Scanner scanner=new Scanner(System.in);
System.out.println("请输入操作数:");
int h=scanner.nextInt();
System.out.println("请输入操作数范围:");
int k= scanner.nextInt();
System.out.println("请输入是否需要括号 1:是 2:否");
int lk=scanner.nextInt();
System.out.println("请输入题目数");
int ii=scanner.nextInt();
for(int i=0;i<ii;i++)
{
String shizi="";
if(lk==1)
{
shizi = o.creatshi(h - 1, k);
}else{
shizi=o.creatshi2(h-1,k);
}
System.out.print(shizi+"=");
String anss=o.cal(shizi);
double number = Double.parseDouble(anss);
double a=scanner.nextDouble();
double c=Math.abs(number-a);
if(c<=0.1)
{
if(i%3==0)
{
file.creatfile2(shizi);
}
else{
file.creatfile1(shizi);
}
}else
{
if(count%3==0)
{
file.creatfile2(shizi);
file.cuo2(shizi);
count=0;
}else
{
file.creatfile1(shizi);
file.cuo1(shizi);
}
}
}
}
public String creatshi(int n,int m)
{
Scanner s=new Scanner(System.in);
Random random=new Random();
String f="";
f=f+creatnum(m);
boolean kh=false;
if(n>2)
{
kh=creatkh();
}
int c= random.nextInt(1,n-1);
for(int i=0;i<n;i++)
{
if(kh)
{
if(i==0)
{
f = f + creatchar() + "(" + creatnum(m);
}else if(i==c){
f=f+ creatchar()+creatnum(m)+")";
}else {
f=f+creatchar()+creatnum(m);
}
}else
{
f=f+creatchar()+creatnum(m);
}
}
return f;
}
public String creatshi2(int n,int m)
{
Scanner s=new Scanner(System.in);
Random random=new Random();
String f="";
f=f+creatnum(m);
boolean kh=false;
int c= random.nextInt(1,n-1);
for(int i=0;i<n;i++)
{
if(kh)
{
if(i==0)
{
f = f + creatchar() + "(" + creatnum(m);
}else if(i==c){
f=f+ creatchar()+creatnum(m)+")";
}else {
f=f+creatchar()+creatnum(m);
}
}else
{
f=f+creatchar()+creatnum(m);
}
}
return f;
}
public String creatchar()
{
String ch="";
Random random=new Random();
int key=random.nextInt(4);
switch (key){
case 0:ch="+";break;
case 1:ch="-";break;
case 2:ch="*";break;
case 3:ch="/";break;
}
return ch;
}
public boolean creatkh()
{
boolean key=false;
Random random=new Random();
int r= random.nextInt(3);
if(r<1){
key=true;
return key;
}
key=false;
return key;
}
public int creatnum(int m)
{
Random random=new Random();
return random.nextInt(1,m);
}
public String cal(String str) {
// 对表达式进行预处理,并简单验证是否是正确的表达式
// 存放处理后的表达式
List<String> list = new ArrayList<>();
char[] arr = str.toCharArray();
// 存放数字临时变量
StringBuffer tmpStr = new StringBuffer();
for (char c : arr) {
// 如果是数字或小数点,添加到临时变量中
if (c >= '0' && c <= '9') {
tmpStr.append(c);
} else if (c == '.') {
if (tmpStr.indexOf(".") > 0) {
throw new RuntimeException("非法字符");
}
tmpStr.append(c);
}
// 如果是加减乘除或者括号,将数字临时变量和运算符依次放入list中
else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')') {
if (tmpStr.length() > 0) {
list.add(tmpStr.toString());
tmpStr.setLength(0);
}
list.add(c + "");
}
// 如果是空格,跳过
else if (c == ' ') {
continue;
} else {
throw new RuntimeException("非法字符");
}
}
if (tmpStr.length() > 0) {
list.add(tmpStr.toString());
}
// 初始化后缀表达式
List<String> strList = new ArrayList<>();
// 运算过程中,使用了两次栈结构,第一次是将中缀表达式转换为后缀表达式,第二次是计算后缀表达式的值
Stack<String> stack = new Stack<>();
// 声明临时变量,存放出栈元素
String tmp;
// 1. 将中缀表达式转换为后缀表达式
for (String s : list) {
// 如果是左括号直接入栈
if (s.equals("(")) {
stack.push(s);
}
// 如果是右括号,执行出栈操作,依次添加到后缀表达式中,直到出栈元素为左括号,左括号和右括号都不添加到后缀表达式中
else if (s.equals(")")) {
while (!(tmp = stack.pop()).equals("(")) {
strList.add(tmp);
}
}
// 如果是加减乘除,弹出所有优先级大于或者等于该运算符的栈顶元素(栈中肯定没有右括号,认为左括号的优先级最低),然后将该运算符入栈
else if (s.equals("*") || s.equals("/")) {
while (!stack.isEmpty()) {
// 取出栈顶元素
tmp = stack.peek();
if (tmp.equals("*") || tmp.equals("/")) {
stack.pop();
strList.add(tmp);
} else {
break;
}
}
stack.push(s);
} else if (s.equals("+") || s.equals("-")) {
while (!stack.isEmpty()) {
// 取出栈顶元素
tmp = stack.peek();
if (!tmp.equals("(")) {
stack.pop();
strList.add(tmp);
} else {
break;
}
}
stack.push(s);
}
// 如果是数字,直接添加到后缀表达式中
else {
strList.add(s);
}
}
// 最后依次出栈,放入后缀表达式中
while (!stack.isEmpty()) {
strList.add(stack.pop());
}
// 2.计算后缀表达式的值
Stack<BigDecimal> newStack = new Stack<>();
for (String s : strList) {
// 若遇运算符,则从栈中退出两个元素,先退出的放到运算符的右边,后退出的放到运算符左边,
// 运算后的结果再进栈,直到后缀表达式遍历完毕
if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
BigDecimal b1 = newStack.pop();
BigDecimal b2 = newStack.pop();
switch (s) {
case "+":
newStack.push(b2.add(b1));
break;
case "-":
newStack.push(b2.subtract(b1));
break;
case "*":
newStack.push(b2.multiply(b1));
break;
case "/":
newStack.push(b2.divide(b1, 9, BigDecimal.ROUND_HALF_UP));
break;
}
}
// 如果是数字,入栈
else {
newStack.push(new BigDecimal(s));
}
}
// 最后,栈中仅有一个元素,就是计算结果
return newStack.peek().toString();
}
public void creatfile(String s) throws IOException
{
File file=new File("sum.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("sum.txt",true))
{
writer.write(s+"\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
public void cuo(String s) throws IOException
{
File file=new File("error.txt");
boolean flg=file.createNewFile();
try (Writer writer = new FileWriter("error.txt",true))
{
writer.write(s+"\n");
} catch (IOException e)
{
e.printStackTrace();
}
}
}