软件工程个人作业03(四则运算3)

  题目要求:1、定义参数控制生成题目的个数。例如,参数n=10;则将生成10个题目。
       2、定义参数控制题目中数值(自然数、真分数和真分数分母)的范围。例如参数r= 10,将生成10以内(不包括10)的四则运算题目。该参数可以设置为1或其他自然数。该参数必须给定,否则程序报错并给出帮助信息。

                  3、生成的题目中计算过程不能产生负数,也就是说算术表达式中如果存在形如e1 − e2的子表达式,那么e1 ≥ e2。
                  4、生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数。
                  5. 每道题目中出现的运算符个数不超过3个。
                  7.生成的题目存储到数据库中,
                  格式如下:
                 1. 四则运算题目1
                 2. 四则运算题目2
                   ……
                 其中真分数在输入输出时采用如下格式,真分数五分之三表示为3/5,真分数二又八分之三表示为2’3/8。

程序设计思路:定义全局变量num用于自动生成题目到个数,max表示题目的取值范围,存入数据库则用SQL语言可以实现。

源代码如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
import java.util.Scanner;

public class sizeyunsuan {
    private static String driverStr="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static String connStr="jdbc:sqlserver://localhost:1433;DatabaseName=sizeyunsuan";
    private static String dbusername="sa";
    private static String dbpassword="123456";
    private static Connection conn=null; 
    private static  java.sql.Statement stmt=null;
    static int sum1=0;//J_Z题目总数量
    static int sum2=0;//J_F题目总数量
    static int sum3=0;//T_Z题目总数量
    static int sum4=0;//T_F题目总数量
    static int number;
    
    static Scanner in=new Scanner(System.in);
    static Random random=new Random();
    static char mark[]={'+','-','x','÷'};
    private static int max;
static public int max(int x1,int x2)

{
    if(x1>x2)
    {
        return x1;
    }
    else
    {
        return x2;
        
    }
}
static public int min(int x1,int x2)
{
    if(x1<x2)
    {
        return x1;
    }
    else 
    {
        return x2;
    }
}
static public int mid(int x1,int x2)
{
    int Mid=1;
    for(int i=min(x1,x2);i>1;i--)
    {
        if(x1%i==0||x2%i==0)
        {
            Mid=i;
            break;
        }
    }
    return Mid;
}
static public String insert(String a,char b,int t)
{
    return a.substring(0,t)+b+a.substring(t,a.length());
}
static public int count(int x)
{
    int weishu = 0;
    while(Math.abs(x)%10>0||x/10!=0)
    {
        weishu++;
        x=x/10;
    }
    return weishu;
}
//基础题——整数型    
public static int J_Z()
{

        
        for(int i=sum1;i<number;i++)
        {
            int a=random.nextInt(max);
            int b=random.nextInt(max);
            char c=mark[random.nextInt(4)];
            String formula="";
            int value=0;
            formula+=a;
            formula+=c;
            formula+=b;
            formula+="=";
            if(c=='+')
            {
                value=a+b;
            }
            else if(c=='-')
            {
                while(a<b)//结果不为负数
                {
                    a=random.nextInt(max);
                    b=random.nextInt(max);
                    formula="";
                    formula+=a;
                    formula+=c;
                    formula+=b;
                    formula+="=";
                }
                value=a-b;
                
            }
            else if(c=='x')
            {
                value=a*b;
            }
            else if(c=='÷')
            {
                while(b==0||a%b!=0)
                {
                    a=random.nextInt(max);
                    b=random.nextInt(max);
                    formula="";
                    formula+=a;
                    formula+=c;
                    formula+=b;
                    formula+="=";
                }
                value=a/b;
            }
            String sql="insert into dbo.suanshi1 values('"+i+"','"+formula+"','"+value+"')";
            try
            {
                java.sql.Statement statement=conn.createStatement();
                statement.executeUpdate(sql);
            }
            
            catch(SQLException e)
            {
                e.printStackTrace();
            }
            sum1+=1;
        }
        System.out.println("题目导入数据库成功!");
        return sum1;
    }
//基础题——分数型
public static int J_F()
{
    for(int i=0;i<number;i++)
    {
        int a=random.nextInt(max);
        int b=random.nextInt(max);
        int c=random.nextInt(max);
        int d=random.nextInt(max);
        char m2=mark[random.nextInt(4)];
        String formula="";
        String value="";
        
        formula+='(';
        formula+=a;
        formula+='/';
        formula+=b;
        formula+=')';
        formula+=m2;
        formula+='(';
        formula+=c;
        formula+='/';
        formula+=d;
        formula+=')';
        formula+='=';
        
        //用于重新生成替换不符合要求的题目
        while(a>b||c>d||b==0||d==0)
        {
            a=random.nextInt(max);
            b=random.nextInt(max);
            c=random.nextInt(max);
            d=random.nextInt(max);
            formula="";
            formula+='(';
            formula+=a;
            formula+='/';
            formula+=b;
            formula+=')';
            formula+=m2;
            formula+='(';
            formula+=c;
            formula+='/';
            formula+=d;
            formula+=')';
            formula+='=';
            
        }
        if(m2=='+')
            {
                int bottom,top,top22,bottom2;//
                bottom=b*d;
                top=a*d+b*c;
                
                value="";
                value+='(';
                value+=top;
                value+='/';
                value+=bottom;
                value+=')';
                while(top>=bottom)
                {
                    
                    int T,B,Z;
                    T=top/bottom;
                    B=top-T*bottom;
                    Z=bottom;
                    value="";
                    value+=T;
                    value+=',';
                    value+=B;
                    value+='/';
                    value+=Z;
                    if(top==bottom)
                    {
                        value="";
                        value+=1;
                        break;
                    }
                    break;
                }
                
                for(int i1=min(top,bottom);i1>1;i1--)
                {
                        /*while(top>=bottom)
                        {
                            int T,B,Z;
                            T=top/bottom;
                            B=top-T*bottom;
                            Z=bottom;
                            value="";
                            value+=T;
                            value+=',';
                            value+=B;
                            value+='/';
                            value+=Z;    
                            if(top==bottom)
                            {
                                value="";
                                value+=1;
                                break;
                            }
                        }*/
                    if(top%i1==0&&bottom%i1==0)
                    {
                        value="";
                        top22=top/i1;
                        bottom2=bottom/i1;
                        value="";
                        value+='(';
                        value+=top22;
                        value+='/';
                        value+=bottom2;
                        value+=')';
                    
                        break;
                    }
                }
                
                //top1+top2和bottom通分
                /*for(int i1=min((top1+top2),bottom);i1>1;i1--)
                {
                    if((top1+top2)%bottom==0)
                    {
                        value+=(top1+top2)/bottom;
                    }
                    else if((top1+top2)%i1==0&&bottom%i1==0)
                    {
                        top22=(top1+top2)/i1;
                        bottom2=bottom/i1;
                        value="";
                        value+='(';
                        value+=top22;
                        value+='/';
                        value+=bottom2;
                        value+=')';
                    }
                    else
                    {
                        value="";
                        value+='(';
                        value+=(top1+top2);
                        value+='/';
                        value+=bottom;
                        value+=')';
                    } 
                }*/
                    
            }
            else if(m2=='-')
            {
                int top=a*d-b*c,top2;
                int bottom=b*d,bottom2;
                value="";
                value+='(';
                value+=top;
                value+='/';
                value+=bottom;
                value+=')';
                for(int i2=min(top,bottom);i2>1;i2--)
                {
                    if(top%i2==0&&bottom%i2==0)
                    {
                        value="";
                        top2=top/i2;
                        bottom2=bottom/i2;
                        value+='(';
                        value+=top2;
                        value+='/';
                        value+=bottom2;
                        value+=')';
                        break;
                    }
                    else if(top==0)
                    {
                        value="";
                        value+=0;
                    }
                }
            }
            else if(m2=='x')
            {
                
                
                if(a*c==0)
                {
                    value="";
                    value+=0;
                }
                else
                {
                    int top=a*c,top2;
                    int bottom=b*d,bottom2;
                    value+='(';
                    value+=top;
                    value+='/';
                    value+=bottom;
                    value+=')';
                    for(int i3=min(top,bottom);i3>1;i3--)
                    {
                        if(top%i3==0&&bottom%i3==0)
                        {
                            value="";
                            top2=top/i3;
                            bottom2=bottom/i3;
                            value+='(';
                            value+=top2;
                            value+='/';
                            value+=bottom2;
                            value+=')';
                            break;
                            
                        }
                        
                    }
                }
            }
            else if(m2=='÷')
            {
                value+='(';
                value+=a*d;
                value+='/';
                value+=b*c;
                value+=')';
                while(a*d>=b*c||(b*c)!=0)
                {
                    int T,B,Z;
                    T=(a*d)/(b*c);
                    B=a*d-T*b*c;
                    Z=b*c;
                    value="";
                    value+=T;
                    value+=',';
                    value+=B;
                    value+='/';
                    value+=Z;
                    if(a*d==b*c)
                    {
                        value="";
                        value+=1;
                    }
                    break;
                }
                if(c==0)
                {
                    value="";
                    value+="∞";
                }
                else if(a==0)
                {
                    value="";
                    value+=0;
                }
                
                else
                {
                    int top,top2;
                    int bottom,bottom2;
                    top=a*d;
                    bottom=b*c;
                    for(int i4=min(top,bottom);i4>1;i4--)
                    {
                        if(top%i4==0&&bottom%i4==0)
                        {
                            top2=top/i4;
                            bottom2=bottom/i4;
                            value="";
                            value+='(';
                            value+=top2;
                            value+='/';
                            value+=bottom2;
                            value+=')';
                            /*while(top2>=bottom2)
                            {
                                int T,B,Z;
                                T=top2/bottom2;
                                B=top2-T*bottom;
                                Z=bottom2;
                                value+=T;
                                value+=',';
                                value+=B;
                                value+='/';
                                value+=Z;
                                if(top2==bottom2)
                                {
                                    value="";
                                    value+=1;
                                    break;
                                }
                                break;
                                
                            }*/
                            break;
                            }
                    }
                }
            }
        String sql="insert into dbo.suanshi2 values('"+i+"','"+formula+"','"+value+"')";
        try
        {
            java.sql.Statement statement=conn.createStatement();
            statement.executeUpdate(sql);
        }
        
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        sum2+=1;
    }
        
        
        
        return sum2;
}
//提高题——整数型
public static int T_Z()
{
    for(int i=0;i<number;i++)
    {
        String formula="";
    
        int Num=random.nextInt(10);//算式的数字个数
        while(Num<3)
        {
            Num=random.nextInt(10);
        }
        int a[]=new int [Num];//数字
        int b[]=new int [Num-1];//符号下标
        for(int i1=0;i1<Num;i1++)
        {
            a[i1]=random.nextInt(max);
        }
        for(int i1=0;i1<Num-1;i1++)
        {
            b[i1]=random.nextInt(4);
        }
        for(int i2=0;i2<Num-1;i2++)
        {
            formula+=a[i2];
            
            formula+=mark[b[i2]];
        }
        
        formula+=a[Num-1];
        formula+='=';
        String sql="insert into dbo.suanshi3 values('"+i+"','"+formula+"')";
        try
        {
            java.sql.Statement statement=conn.createStatement();
            statement.executeUpdate(sql);
        }
        
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    
        
        System.out.println(formula);
        sum3+=1;
    }
    System.out.println("导入数据库成功!");
    return sum3;
}


public static int T_F()
{
    for(int i=0;i<number;i++)
    {
        String formula="";
        int Num=random.nextInt(10);
        while(Num<3)
        {
            Num=random.nextInt(10);
        }
        int a[]=new int[Num];
        int b[]=new int[Num];
        int c[]=new int[Num];
        for(int i1=0;i1<Num;i1++)
        {
            a[i1]=random.nextInt(max);
            b[i1]=random.nextInt(max-1)+1;
        }
        for(int i2=0;i2<Num-1;i2++)
        {
            c[i2]=random.nextInt(4);
        }
        for(int i3=0;i3<Num-1;i3++)
        {
            formula+='(';
            formula+=a[i3];
            formula+='/';
            formula+=b[i3];
            formula+=')';
            formula+=mark[c[i3]];
            
        }
        formula+='(';
        formula+=a[Num-1];
        formula+='/';
        formula+=b[Num-1];
        formula+=')';
        formula+='=';
        System.out.println(formula);
        String sql="insert into dbo.suanshi4 values('"+i+"','"+formula+"')";
        try
        {
            java.sql.Statement statement=conn.createStatement();
            statement.executeUpdate(sql);
        }
        
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        sum4+=1;
    }
    System.out.println("导入数据库成功!");
    return sum4;
}
    public static void main(String[] args) throws SQLException {
        // TODO Auto-generated method stub
        //有答案
        while(true)
        {
        
        
System.out.println("基础题还是提高题,请选择:(0:基础,1:提高,2:退出)");
int Chose=in.nextInt();
if(Chose==0)
{
    System.out.println("请输入题目类型:(0:自然数型,1:分数型)");
    int Chose1=in.nextInt();
    System.out.println("请输入题目个数:");
    number=in.nextInt();
    System.out.println("请输入题目中的数值范围:最大数字不超过多少");
    max=in.nextInt();
    if(Chose1==0) 
    {
        try
        {
            Class.forName(driverStr);
            conn=DriverManager.getConnection(connStr,dbusername,dbpassword);
            stmt=conn.createStatement();
            System.out.println("数据库连接成功!");
        }
        catch(Exception ex)
        {
            System.out.println("数据库连接失败!");
        }
        //J_Z();
        System.out.println("题目数量为:"+J_Z()); 
        conn.close();
        stmt.close();
        
    
        
        
    }
    else if(Chose1==1)
    {
        try
        {
            Class.forName(driverStr);
            conn=DriverManager.getConnection(connStr,dbusername,dbpassword);
            stmt=conn.createStatement();
            System.out.println("数据库连接成功!");
        }
        catch(Exception ex)
        {
            System.out.println("数据库连接失败!");
        }
        //J_F();
        System.out.println("题目数量为:"+J_F()); 
        conn.close();
        stmt.close();
    }
    
}
else if(Chose==1)
{
    System.out.println("请输入题目类型:(0:自然数型,1:分数型)");
    int Chose1=in.nextInt();
    System.out.println("请输入题目个数:");
    number=in.nextInt();
    System.out.println("请输入题目中的数值范围:最大数字不超过多少");
    max=in.nextInt();
    if(Chose1==0)
    {
        
        try
        {
            Class.forName(driverStr);
            conn=DriverManager.getConnection(connStr,dbusername,dbpassword);
            stmt=conn.createStatement();
            System.out.println("数据库连接成功!");
        }
        catch(Exception ex)
        {
            System.out.println("数据库连接失败!");
        }
        System.out.println("题目数量为"+T_Z()+"个");
    }
    else if(Chose1==1)
    {
        try
        {
            Class.forName(driverStr);
            conn=DriverManager.getConnection(connStr,dbusername,dbpassword);
            stmt=conn.createStatement();
            System.out.println("数据库连接成功!");
        }
        catch(Exception ex)
        {
            System.out.println("数据库连接失败!");
        }
        System.out.println("题目数量"+T_F()+"个");
    }
}
else
{
    break;
}

        }

    }

}

程序运行前需要在数据库sqlserver2008中新建一个名为sizeyunxuan的数据库文件,在文件中建立四个表,用于存入四种题目。四种题目分别为1:两位整数的四则运算(可以存入答案),两位分数的四则运算(可以存入答案),多位整数的四则运算(无答案),多位分数的四则运算(无答案)。四个表分别为:suanshi1,suanshi2,suanshi3,suanshi4.

程序运行结果:

 

周活动总结表

日期/任务

听课

阅读课本

课下学习

日总计

周日3.26

周一

120m

30m

30m

150m

周二

40m

40m

周三

50m

50m

周四

120m

40m

20m

180m

周五

40m

40m

周六

40m

40m

周总计

240m

70m

2100m

540m

时间记录日志:

          

日期

开始时间

结束时间

中断时间

净时间

活动

备注

C

U

3.6

14:00

16:00

10m

110m

上课,编程

软件工程课

 

 

3.7

19:00

21:00

20m

100m

看书,编程

写软件工程作业

 

 

3.8

19:00

21:00

15m

105m

看书,编程

javaweb

 

 

3.9

16:00

18:00

10m

110m

上课

Javaweb上课

 

 

3.10

18:00

20:00

30m

90m

构思,写java

写四则运算程序3

 

 

原文地址:https://www.cnblogs.com/yibao/p/6604514.html