四则运算03

设计思想:

把整数和真分数进行分类,在每个分类下进行输入题目量、数值范围、是否有乘除法、加减法是否有负数、除法是否有余数的操作。

在整数下定义两个随机数,在真分数下定义四个随机数。

如果有乘除法,加减法有负数,除法有余数就直接输出题目。

如果无乘除法,那么输出的结果就只有加减法的题目。

之后把生成的题目写入数据库,然后测试10000条数据

源代码:

package zuoye3;

import java.util.Random;
import java.util.Scanner;
import java.util.Vector;
import java.sql.*;

public class MathHomeWork3 {
    public static int choose,from,to,number,ifChengChu;
    public static int randomArr[]=new int[4];
    public static Random ran=new Random();
    public static char randomCh[]={'+','-','*','÷'};
    public static Expression ex[];
    @SuppressWarnings({  "resource" })
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        System.out.println("题目个数:");
        number=in.nextInt();
        System.out.println("数字范围(从小到大输入两个正整数):");
        from=in.nextInt();
        to=in.nextInt();
        System.out.println("是否有乘除法(1有2无):");
        ifChengChu=in.nextInt();
        DBBean db=new DBBean();
        
        ex=new Expression[number];
        for(int i=0;i<number;i++){
            ex[i]=new Expression();
            choose=ran.nextInt(2)+1;
            ex[i]=creaExpression(choose,ifChengChu);
            if(ex[i]==null)
                i--;
            
        }
        showExpression(ex);
        for(int i=0;i<number;i++){
            db.insertEx(ex[i]);
        }
        System.out.println("是否清空数据表(1是,其他数字否)?");
        int ifClear=in.nextInt();
        if(ifClear==1){
            db.clear();
        }
        else{
            return ;
        }
        db.clear();
    }

    //生成表达式
    public static Expression creaExpression(int choose,int ifChengChu){
        char operator;
        if(ifChengChu==1){
            int oper=ran.nextInt(4);
            operator=randomCh[oper];
        }
        else{
            int oper=ran.nextInt(2);
            operator=randomCh[oper];
        }

        if(choose==1){
            Expression ex=new Expression(true);
            for(int i=0;i<2;i++){
                randomArr[i]=randomNum(from,to);
            }
            if(operator=='-'||operator=='÷'){
                int ra=0,rb=0;
                while(true){
                    ra=randomNum(from,to);
                    rb=randomNum(from,to);
                    if(ra>=rb&&ra%rb==0)
                        break;
                }
                randomArr[0]=ra;
                randomArr[1]=rb;
            }

            ex.push(""+randomArr[0]);
            ex.push(""+operator);
            ex.push(""+randomArr[1]);
            ex.push("=");
            int result=Function.calcuInt(ex.get(0),ex.get(1),ex.get(2));
            String s=""+result;
            ex.setValue(s);
            return ex;

        }
        else if(choose==2){
            Expression ex=new Expression(false);
            if(operator=='-'){
                int newindex=ran.nextInt(2)+2;
                operator=randomCh[newindex];
            }
            FenShu fs[]=new FenShu[2];
            String fenshu[]=new String[2];
            for(int i=0;i<2;i++){
                int a=0,b=0;
                while(a>=b){
                    a=randomNum(from,to);
                    b=randomNum(from,to);
                }
                int kk=Function.fun(a,b);
                a/=kk;
                b/=kk;
                fs[i]=new FenShu(a,b);
                fenshu[i]=fs[i].getFenshu();
            }

            ex.push(""+fenshu[0]);
            ex.push(""+operator);
            ex.push(""+fenshu[1]);
            ex.push("=");
            FenShu result=Function.calcuFenshu(fs[0],""+operator,fs[1]);
            ex.setValue(result.getFenshu());
            return ex;
        }
        else
            return null;
        

    }
    //打印表达式
    public static void showExpression(Expression f[]){
        int len=f.length;
        for(int i=0;i<len;i++){
            System.out.print("No."+(i+1)+": ");
            f[i].show();
            System.out.print("\n");
        }
    }



    //范围生成随机数
    public static int randomNum(int fromA,int toB){
        int x=ran.nextInt(toB-fromA)+fromA+1;
        return x;
    }
}
class Function{
    //求分子分母最大公约数
    public static int fun(int x,int y){
        int min,i;
        if(x>y) min=y;
        else
            min=x;
        for(i=min;i>=1;i--){
            if(x%i==0&&y%i==0){
                break;
            }
        }
        return i;
    }
    //计算整数表达式
    public static int calcuInt(String numleft,String oper,String numright){
        int a=Integer.parseInt(numleft);
        int b=Integer.parseInt(numright);
        if(oper.equals("+"))
            return a+b;
        else if(oper.equals("-"))
            return a-b;
        else if(oper.equals("*"))
            return a*b;
        else if(oper.equals("÷"))
            return a/b;
        else 
            return -1;
    }
    //计算分数表达式
    public static FenShu calcuFenshu(FenShu x,String oper,FenShu y){
        FenShu result=new FenShu();
        if(oper.equals("+")){
            result=addFenshu(x,y);
        }
        else if(oper.equals("÷")){
            result=divideFenshu(x,y);
        }
        else if(oper.equals("*")){
            result=multiFenshu(x,y);
        }
        else result=new FenShu(-1,-1);
        return result;
        
    }
    //分数加
    public static FenShu addFenshu(FenShu x,FenShu y){
        int zi,mu,yue;
        mu=x.getFenmu()*y.getFenmu();
        zi=x.getFenzi()*y.getFenmu()+y.getFenzi()*x.getFenmu();
        yue=Function.fun(zi, mu);
        zi/=yue;
        mu/=yue;
        FenShu fenshu=new FenShu(zi,mu);
        return fenshu;
    }
    //分数除
    public static FenShu divideFenshu(FenShu x,FenShu y){
        int zi,mu,yue;
        mu=x.getFenmu()*y.getFenzi();
        zi=x.getFenzi()*y.getFenmu();
        yue=Function.fun(zi, mu);
        zi/=yue;
        mu/=yue;
        FenShu fenshu=new FenShu(zi,mu);
        return fenshu;
    }
    //分数除
    public static FenShu multiFenshu(FenShu x,FenShu y){
        int zi,mu,yue;
        mu=x.getFenmu()*y.getFenmu();
        zi=x.getFenzi()*y.getFenzi();
        yue=Function.fun(zi, mu);
        zi/=yue;
        mu/=yue;
        FenShu fenshu=new FenShu(zi,mu);
        return fenshu;
    }
}
class Expression{
    private Vector<String> expression;
    private boolean ifInt;
    private String value="";
    public Expression(boolean ifint){
        expression=new Vector<String>();
        ifInt=ifint;
    }
    public Expression() {
        expression=new Vector<String>();
    }
    public void push(String x){
        expression.addElement(x);
    }
    public String get(int i){
        if(expression!=null&&i>=0&&i<expression.size()){
            return expression.get(i);
        }
        else return null;
    }
    public void set(int i,String x){
        if(expression!=null&&i>=0&&i<expression.size()){
            expression.set(i,x);
        }
        else return;
    }
    public void insert(int i,String x){
        if(expression!=null&&i>=0&&i<expression.size()){
            expression.insertElementAt(x,i);
        }
        else return;
    }
    public void add(int i,String x){
        if(expression!=null&&i>=0&&i<expression.size()){
            expression.add(i,x);
        }
        else return ;
    }
    public void delete(int i){
        if(expression!=null&&i>=0&&i<expression.size()){
            expression.removeElementAt(i);
        }
        else return ;
    }
    public void show(){
        for(int i=0;i<expression.size();i++){
            System.out.print(expression.get(i));
        }
        System.out.print(value);
    }
    public int size(){
        if(expression!=null)
            return expression.size();
        else return -1;
    }
    public void setValue(String v){
        value=v;
    }
    public String getValue(){
        return value;
    }
    public boolean getIfint(){
        return ifInt;
    }
}

class FenShu{
    private int fenzi;
    private int fenmu;
    public FenShu(){
        fenzi=1; fenmu=1;
    }
    public FenShu(int x,int y){
        fenzi=x; fenmu=y;
    }
    public int getFenzi(){
        return fenzi;
    }
    public int getFenmu(){
        return fenmu;
    }
    public String getFenshu(){
        String s="["+fenzi+"/"+fenmu+"]";
        return s;
    }
    
}

//数据库操作
class DBBean {
    private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=test";
    private String dbusername = "sa";
    private String dbpassword = "144214";
    private Connection conn = null;
    private PreparedStatement pstmt = null;

    public DBBean()
    {
        try
        {
            Class.forName(driverStr);
            conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
        } 
        catch (Exception ex) {
            System.out.println("数据连接失败!");
        } 
        
    }
    public void insertEx(Expression e){
        if(e.size()!=4 && (!e.getValue().equals(""))){
            System.out.println("插入表达式失败!");
            return ;
        }
        else{
            try{
                String sql = "insert into expression values('" +e.get(0)+ "','" +e.get(1)+ "','" +e.get(2)+ "','" +e.getValue()+ "')";
                pstmt=conn.prepareStatement(sql);
                int rst=pstmt.executeUpdate();
                if(rst!=0){
                    System.out.print("成功插入表达式:");
                    e.show();
                    System.out.print("\n");
                }
                else
                    System.out.println("插入表达式失败!");
            }
            catch(Exception ex){
                System.out.println("插入表达式失败!");
            }

        }
    }
    //清空数据表
    public void clear(){
        try{
            String sql="delete expression";
            pstmt=conn.prepareStatement(sql);
            int rst=pstmt.executeUpdate();
            if(rst!=0){
                System.out.println("成功清空数据表");
            }            
        }
        catch(Exception e){
            System.out.println("delete语句执行失败!");
        }
    }
}

结果截图:

 

 

 

posted @ 2017-03-10 19:06  YOUNG111  阅读(159)  评论(0编辑  收藏  举报