四则运算2
四则运算2
一.设计思想
(1)创建题库
A.将任务分为两个部分,一个为随机产生三元运算,一个为随机产生加减乘除运算。
B.建立两个运算式类,用来作为创建对象的模板,大致分为两类属性,数值,运算符,。
数值根据运算式的数值个数分别为2个,3个。运算符为char类型。
C.定义一个产生一个运算式的函数,内容为:随机产生一个1~4的数,如果是1,则两 个符号为++;如果是2,则两个符号为+-;如果是3,则两个符号为-+;如果是4,则两 个符号为--,再次随机产生三个数,并且通过随机函数的上限值对其进行约束,使其不 会产生大于100的或者小于0的结果。根据运算符产生结果。对一个三元运算式对象进 行赋值,并且返回这个对象。
D.定义一个存贮类,有属性:运算式和结果
E.定义一个校验运算式的函数,参数为一个三元运算式类,内容为:将传入的三元运算 式对象中的三个运算数与运算符连接在一起,变成一个字符串,创建一个存贮对象,将 连接成的字符串赋值给存贮的运算式属性,提取数据库中的所有数据。将数据库中的第 一个数据,也就是运算式,去除空格,进行比较,如果两个字符串内容相同,则不将其 存入数据库。否则将其存入数据库
F.另一个类与之类似。
(2)创建页面
A.创建三个界面,第一个为选择题目与数量,第二个为显示题目与输入文本框,第三个 为运算式与结果。(本方面非由我所做,故不多做描述)
二.源程序代码
package after_class; public class AddAndMultiply { private int firstNum = 0; private int secondNum = 0; private int result = 0; private char operator = 0; public int getFirstNum() { return firstNum; } public void setFirstNum(int firstNum) { this.firstNum = firstNum; } public int getSecondNum() { return secondNum; } public void setSecondNum(int secondNum) { this.secondNum = secondNum; } public int getResult() { return result; } public void setResult(int result) { this.result = result; } public char getOperator() { return operator; } public void setOperator(char operator) { this.operator = operator; } }
package after_class; //加减(可以产生三元的表达式) public class AddAndReduce { private int firstNum = 0; private int secondNum = 0; private int thirdNum = 0; private int result = 0; private char firstOperator = 0; private char secondOperator = 0; public int getFirstNum() { return firstNum; } public void setFirstNum(int firstNum) { this.firstNum = firstNum; } public int getSecondNum() { return secondNum; } public void setSecondNum(int secondNum) { this.secondNum = secondNum; } public int getThirdNum() { return thirdNum; } public void setThirdNum(int thirdNum) { this.thirdNum = thirdNum; } public int getResult() { return result; } public void setResult(int result) { this.result = result; } public char getFirstOperator() { return firstOperator; } public void setFirstOperator(char firstOperator) { this.firstOperator = firstOperator; } public char getSecondOperator() { return secondOperator; } public void setSecondOperator(char secondOperator) { this.secondOperator = secondOperator; } }
package after_class; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Random; import util.DBUtil; public class CreateAddAndMultiplyRandom { public static void CreateAddAndMultiplyOperation(int times) throws SQLException//此处的times指的是产生的式子的个数 { AddAndMultiply once = new AddAndMultiply(); for(int i = 0;i<times;i++) { once = CreateOnceOperation(); while(checkAddAndMultiply(once)) { once = CreateOnceOperation(); } } } //以下部分为存储到数据库中,并且查重。如果重复,返回true。如果不重复,返回false private static boolean checkAddAndMultiply(AddAndMultiply once) { String storeOperation = String.valueOf(once.getFirstNum())+String.valueOf(once.getOperator())+String.valueOf(once.getSecondNum()); String storeResult = String.valueOf(once.getResult()); //获得链接对象 Connection connection = DBUtil.getConnection(); PreparedStatement preparedStatement = null; //准备sql语句 String sql = "select count(*) from t_operation where storeOperation = ? "; //创建语句传输对象 ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, storeOperation); //接收结果集 resultSet = preparedStatement.executeQuery(); //遍历结果集 while(resultSet.next()) { if (resultSet.getInt(1) > 0) { return true; } } sql = "insert into t_operation(storeOperation,storeResult) values(?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, storeOperation); preparedStatement.setString(2, storeResult); preparedStatement.executeUpdate(); return false; } catch(Exception e) { e.printStackTrace(); } return false; } private static AddAndMultiply CreateOnceOperation() throws SQLException { Random random = new Random(); AddAndMultiply once = new AddAndMultiply(); char operator = 0; int firstNum = 0; int secondNum = 0; int result = 0; int selectOperator = random.nextInt(4)+1; if(selectOperator==1) { operator = '+'; firstNum = random.nextInt(99)+1; secondNum = random.nextInt(100-firstNum)+1; result = firstNum+secondNum; } else if(selectOperator==2) { operator = '-'; firstNum = random.nextInt(100)+1; secondNum = random.nextInt(firstNum)+1; result = firstNum-secondNum; } else if(selectOperator==3) { operator = '*'; firstNum = random.nextInt(9)+1; secondNum = random.nextInt(9)+1; result = firstNum*secondNum; } else { operator = '/'; firstNum = random.nextInt(9)+1; secondNum = random.nextInt(9)+1; firstNum = secondNum*firstNum; result = (int)firstNum/(int)secondNum; } once.setFirstNum(firstNum); once.setOperator(operator); once.setSecondNum(secondNum); once.setResult(result); return once; } }
package after_class; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Random; import util.DBUtil; //产生三元的表达式 public class CreateAddRandom { public static void createAddOperation(int times) throws SQLException { AddAndReduce once = new AddAndReduce(); for(int i = 0;i<times;i++) { once = CreateOnceOperation(); while(checkAddAndReduce(once)) { once = CreateOnceOperation(); } } } //以下部分为存储到数据库中,并且查重。如果重复,返回true。如果不重复,返回false private static boolean checkAddAndReduce(AddAndReduce once) { String storeOperation = String.valueOf(once.getFirstNum())+String.valueOf(once.getFirstOperator()) +String.valueOf(once.getSecondNum())+String.valueOf(once.getSecondOperator()) +String.valueOf(once.getThirdNum()); String storeResult = String.valueOf(once.getResult()); //获得链接对象 Connection connection = DBUtil.getConnection(); PreparedStatement preparedStatement = null; //准备sql语句 String sql = "select count(*) from t_operation2 where storeOperation = ? "; //创建语句传输对象 ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,storeOperation); //接收结果集 resultSet = preparedStatement.executeQuery(); //遍历结果集 while(resultSet.next()) { if (resultSet.getInt(1) > 0) { return true; } } sql = "insert into t_operation2(storeOperation,storeResult) values(?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, storeOperation); preparedStatement.setString(2, storeResult); preparedStatement.executeUpdate(); return false; } catch(Exception e) { e.printStackTrace(); } return false; } private static AddAndReduce CreateOnceOperation() throws SQLException { Random random = new Random(); AddAndReduce once = new AddAndReduce(); int firstNum = 0; int secondNum = 0; int thirdNum = 0; int result = 0; char firstOperator = 0; char secondOperator = 0; //首先随机产生两个数,并且把第二个数作为后两个数的上限 int bound = 0;//这个bound就是界限 int selectOperator = random.nextInt(4)+1; if(selectOperator==1) { firstOperator = '+'; secondOperator = '+'; firstNum = random.nextInt(98)+1; bound = 100-firstNum; secondNum = random.nextInt(bound-1)+1; thirdNum = random.nextInt(bound-secondNum)+1; result = firstNum+secondNum+thirdNum; } else if(selectOperator==2) { firstOperator = '+'; secondOperator = '-'; firstNum = random.nextInt(99)+1; bound = 100-firstNum; secondNum = random.nextInt(bound)+1; thirdNum = random.nextInt(firstNum+secondNum)+1; result = firstNum+secondNum-thirdNum; } else if(selectOperator==3) { firstOperator = '-'; secondOperator = '+'; //此处不需要bound firstNum = random.nextInt(100)+1; secondNum = random.nextInt(firstNum)+1; thirdNum = random.nextInt(100-(firstNum-secondNum))+1; result = firstNum-secondNum+thirdNum; } else { firstOperator = '-'; secondOperator = '-'; firstNum = random.nextInt(99)+2; secondNum = random.nextInt(firstNum-1)+1; thirdNum = random.nextInt(firstNum-secondNum)+1; result = firstNum-secondNum-thirdNum; } once.setFirstNum(firstNum); once.setFirstOperator(firstOperator); once.setSecondNum(secondNum); once.setSecondOperator(secondOperator); once.setThirdNum(thirdNum); once.setResult(result); return once; } }
package after_class; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; import dao.IOperationDao; import dao.Operation; import dao.OperationDaoImpl; public class CreateAll { public static void main(String[] args) { Scanner in =new Scanner(System.in); System.out.println("请输入要产生的题目的数量:"); int number = in.nextInt(); try { CreateAddRandom.createAddOperation(number); CreateAddAndMultiplyRandom.CreateAddAndMultiplyOperation(number); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("OK,产生成功!"); } }
package dao; import java.util.List; public interface IOperationDao { //这个函数是用来调取加减三元中的数据,times是要提取式子的数量 public List<Operation> loadAdd(int times); //这个函数是用来调取加减乘除表中的数据,times是要提取式子的数量 public List<Operation> loadMultiply(int times); }
package dao; public class Operation { private int ID; private String storeOperation; private String storeResult; public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getStoreOperation() { return storeOperation; } public void setStoreOperation(String storeOperation) { this.storeOperation = storeOperation; } public String getStoreResult() { return storeResult; } public void setStoreResult(String storeResult) { this.storeResult = storeResult; } }
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Random; import util.DBUtil; public class OperationDaoImpl implements IOperationDao { private static int mark = 0; public List<Operation> loadAll(int times) { ArrayList<Operation> list = new ArrayList<Operation>(); int createAdd = times/10+1; int createMultiply = times-createAdd; list.addAll(loadMultiply(createMultiply)); list.addAll(loadAdd(createAdd)); return list; } //从加减三元中获取数据 public ArrayList<Operation> loadAdd(int times) { Connection connection = DBUtil.getConnection(); //准备sql语句 String sql = "select * from t_operation2"; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; ArrayList<Operation> list = new ArrayList<Operation>(); Operation Operation = null; //作为产生算式的数量 int flag = 1; try { preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); for(int i = 0;i<mark;i++) { resultSet.next(); } mark = (mark+times)%9000; while(resultSet.next()) { Operation = new Operation(); Operation.setID(resultSet.getInt("ID")); Operation.setStoreOperation(resultSet.getString("storeOperation")); Operation.setStoreResult(resultSet.getString("storeResult")); list.add(Operation); flag++; //到达上限,结束 if(flag == times+1) break; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return list; } //从加减乘除表中获取数据 public ArrayList<Operation> loadMultiply(int times) { Connection connection = DBUtil.getConnection(); //准备sql语句 String sql = "select * from t_operation"; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; ArrayList<Operation> list = new ArrayList<Operation>(); Operation Operation = null; //作为产生算式的数量 int flag = 1; try { preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); for(int i = 0;i<mark;i++) { resultSet.next(); } mark = (mark+times)%9000; while(resultSet.next()) { Operation = new Operation(); Operation.setID(resultSet.getInt("ID")); Operation.setStoreOperation(resultSet.getString("storeOperation")); Operation.setStoreResult(resultSet.getString("storeResult")); list.add(Operation); flag++; //到达上限,结束 if(flag == times) break; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(connection); } return list; } }
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { public static Connection getConnection() { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String user = "sa"; String password = "jisuan@10Q"; String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=OnClass"; Connection connection = null; try { connection = DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; } public static void close(Connection connection ) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement ) { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(ResultSet resultSet ) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
(仅限于产生题库)
三.运行结果截图
四.编程总结分析
虽然使用了数据库,但是因为查重的问题,导致数据库运行太慢。还是要对算法进行优化。
五.开发时间记录日志。