软件工程个人作业03

括号还没有做,只是把自己的网页的其他部分弄好了。

1.设计思想

老师提出了新的要求:

定义: 自然数:0, 1, 2, …。 真分数:1/2, 1/3, 2/3, 1/4, 1’1/2, …。 运算符:+, −, ×, ÷。 括号:(, )。 等号:=。 分隔符:空格(用于四则运算符和等号前后)。

算术表达式: e := n | e1 + e2 | e1 − e2 | e1 × e2 | e1 ÷ e2 | (e), 其中e, e1和e2为表达式,n为自然数或真分数。 四则运算题目:e = ,其中e为算术表达式。

在网页中空格为  运算符使用char类型,表达式和结果都为string。

1、定义参数控制生成题目的个数。 例如,参数n=10;则将生成10个题目。

<input type="text" name="num" value="1">不大于100题 通过输入的值传递到下一个页面,int N = Integer.parseInt(request.getParameter("num"));得到出题数,生成题目。

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

我设置的是位数的控制,通过单选框设置值并传递值,生成算式。

3、生成的题目中计算过程不能产生负数,也就是说算术表达式中如果存在形如e1 − e2的子表达式,那么e1 ≥ e2。

可以控制生成的值不为负,为负则重新生成算式。

4、生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数。

if(shu1 >= shu2)
{
    if(shu1 % shu2 == 0)
         result = shu1/shu2 + "";
    else
         {
             int z = shu1 / shu2;
             result = z + "'" + (shu1 - z * shu2) +"/" + shu2;
         }
}
else
    result = shu1 + "/" + shu2;

格式已按第七个要求设置,存储为String。

5. 每道题目中出现的运算符个数不超过3个。

由于现在做的是两个数之间的运算,未涉及,可以先生成三个字符,然后生成算式时在三个字符中生成。

6.程序一次运行生成的题目不能重复,即任何两道题目不能通过有限次交换+和×左右的算术表达式变换为同一道题目 例如,23 + 45 = 和45 + 23 = 是重复的题目,6 × 8 = 和8 × 6 = 也是重复的题目。3+(2+1)和1+2+3这两个题目是重复的,由于+是左结合的,1+2+3等价于(1+2)+3,也就是3+(1+2),也就是3+(2+1)。但是1+2+3和3+2+1是不重复的两道题,因为1+2+3等价于(1+2)+3,而3+2+1等价于(3+2)+1,它们之间不能通过有限次交换变成同一个题目

for(int j = 0;j < i;j++)
{
    if((c1[i].getShu1() == c1[j].getShu1() && c1[j].getShu2() == c1[i].getShu2()&& 
    c1[i].getFh() == c1[j].getFh()) || (c1[i].getShu2() == c1[j].getShu1() &&
    c1[j].getShu2() == c1[i].getShu1()&& c1[i].getFh() == c1[j].getFh()))
    {
        c1[i] = new Cal_zs(cc, w);
        j = 0;
    }
}

通过判断生成不一样的式子,未设计多位的计算。其实只要前两位不同,后面的也可以不考虑。

7.生成的题目存储到数据库中, 格式如下: 1. 四则运算题目1 2. 四则运算题目2 …… 其中真分数在输入输出时采用如下格式,真分数五分之三表示为3/5,真分数二又八分之三表示为2’3/8。

第四个要求中已设置。

8. 在生成题目的同时,计算出所有题目的答案,并存入数据库文件。 格式如下: 1. 答案1 2. 答案2 特别的,真分数的运算如下例所示:1/6 + 1/8 = 7/24。

附上数据库图片

9. 程序应能支持一万道题目的生成。

可以生成1000道题目,生成时间没有很长。

10. 程序支持对给定的题目文件和答案文件,判定答案中的对错并进行数量统计, 统计结果输出到数据表文件Grade,格式如下: Correct: 5 (1, 3, 5, 7, 9) Wrong: 5 (2, 4, 6, 8, 10) 其中“:”后面的数字5表示对/错的题目的数量,括号内的是对/错题目的编号。为简单起见,假设输入的题目都是按照顺序编号的符合规范的题目。

在网页中加入图片和数据库的查找结果对比。

2.源程序代码

calculat包

package calculat;
import java.util.Random;

public class Cal_zfs{
    private char fh;
    private String shu1;
    private String shu2;
    private String result;
    
    public char getFh() {
        return fh;
    }

    public String getResult() {
        return result;
    }

    public String getShu1() {
        return shu1;
    }

    public String getShu2() {
        return shu2;
    }
    public Cal_zfs()
    {
        
    }
    
    public Cal_zfs(boolean cc, int w)
    {
        int fzi1;
        int fmu1;
        int fzi2;
        int fmu2;
        char [] fu = new char [4];//生成符号,控制有无乘除
        fu[0] = '-';
        fu[1] = '+';
        fu[2] = '/';
        fu[3] = '*';
        if(cc)
            fh = fu[new Random().nextInt(4)];
        else
            fh = fu[new Random().nextInt(2)];
        
        
        if(w == 1)
        {    
            fmu1 = new Random().nextInt(9) + 1;
            fmu2 = new Random().nextInt(9) + 1;
        }
        else if(w == 2)
        {
            fmu1 = new Random().nextInt(99) + 1;
            fmu2 = new Random().nextInt(99) + 1;
        }
        else
        {
            fmu1 = new Random().nextInt(999) + 1;
            fmu2 = new Random().nextInt(999) + 1;
        }
        
        fzi1 = new Random().nextInt(fmu1) + 1;
        fzi2 = new Random().nextInt(fmu2) + 1;
        int gy = gys(fzi1, fmu1);
        int gy2 = gys(fzi2, fmu2);
        if(fzi1 == fmu1)
            shu1 = "1";
        else
            shu1 = fzi1 / gy + "/" + fmu1 / gy;
        if(fzi2 == fmu2)
            shu2 = "1";
        else
            shu2 = fzi2 / gy2 + "/" + fmu2 / gy2;
        
        int fm = 0, fz = 0;
        if(fh == '+')
        {
            fm = fmu1 * fmu2;
            fz = fmu1 * fzi2 + fmu2 * fzi1;
        }
        else if(fh == '-')
        {
            fm = fmu1 * fmu2;
            fz = fmu2 * fzi1 - fmu1 * fzi2;
        }
        else if(fh == '*')
        {
            fm = fmu1 * fmu2;
            fz = fzi1 * fzi2;
        }
        else
        {
            fm = fmu1 * fzi2;
            fz = fzi1 * fmu2;
        }
        
        int g = gys(fz, fm);
        if(fz == fm)
            result = "1";
        else
            result = fz / g + "/" + fm / g;
    }
    
    public int gys(int s1, int s2)
    {
        int gy = 1;
        for(int i = 1; i <= (s1 < s2 ? s1 : s2); i++)
        {
            if(s1 % i == 0 && s2 % i == 0)
                gy = i;
        }
        return gy;
    }
    
    public String show()
    {
        return shu1 + " " + fh + " " + shu2;
    }
}
Cal_zfs.java
package calculat;
import java.util.Random;

public class Cal_zs{
    private char fh;
    private int shu1;
    private int shu2;
    private String result;
    
    public String getResult() {
        return result;
    }
    public Cal_zs()
    {}
    public char getFh() {
        return fh;
    }

    public int getShu1() {
        return shu1;
    }

    public int getShu2() {
        return shu2;
    }
    public Cal_zs(boolean cc,int wei)
    {
        char [] fu = new char [4];//生成符号,控制有无乘除
        fu[0] = '-';
        fu[1] = '+';
        fu[2] = '/';
        fu[3] = '*';
         
        if(cc)
            fh = fu[new Random().nextInt(4)];
        else
            fh = fu[new Random().nextInt(2)];
        if(wei == 1)
        {    
            shu1 = new Random().nextInt(9) + 1;
            shu2 = new Random().nextInt(9) + 1;
        }
        else if(wei == 2)
        {
            shu1 = new Random().nextInt(99) + 1;
            shu2 = new Random().nextInt(99) + 1;
        }
        else
        {
            shu1 = new Random().nextInt(999) + 1;
            shu2 = new Random().nextInt(999) + 1;
        }
        
        if(fh == '+')
            result = shu1 + shu2 + "";
        else if(fh == '-')
            result = shu1 - shu2 + "";
        else if(fh == '*')
            result = shu1 * shu2 + "";
        else
        {
            if(shu1 >= shu2)
            {
                if(shu1 % shu2 == 0)
                    result = shu1/shu2 + "";
                else
                {
                    int z = shu1 / shu2;
                    result = z + "'" + (shu1 - z * shu2) +"/" + shu2;
                }
            }
            else
                result = shu1 + "/" + shu2;
        }
    }
    
    public String show()
    {
        return shu1 + " " + fh + " " + shu2;
    }
    
    public boolean isInteger(){ 
        double r, s1 = shu1, s2 = shu2;
        if(fh == '/')
        {    
            r = s1 / s2;
            String str = r + "";
            for(int i = 0;i < str.length();i++)
            {
                if(str.charAt(i) == '.' && i == str.length() - 2 && str.charAt(i + 1) == '0')
                    return true;
            }
            return false;
        }
        else
            return true;
        
         
    } 
}
Cal_zs.java
package calculat;

public class shu {
    
    private Cal_zs [] c1 = new Cal_zs [100];
    private Cal_zfs [] c2 = new Cal_zfs [100];
    boolean cc;
    int N;
    int wei;

    public void setN(int n) {
        N = n;
    }

    public void setwei(int w)
    {
        wei = w;
    }
    public void setCc(boolean cc) {
        this.cc = cc;
    }

    public shu()
    {
        
    }
    
    public shu(boolean cc, int w, boolean fs, boolean ys)
    {
        for(int i = 0;i < 100;i++)
        {
            c1[i] = new Cal_zs(cc, w);
            while((ys == false && yu(c1[i].getResult())) || (fs == false && c1[i].getResult().charAt(0) == '-'))
            {
                c1[i] = new Cal_zs(cc, w);
            }
            for(int j = 0;j < i;j++)
            {
                if((c1[i].getShu1() == c1[j].getShu1() && c1[j].getShu2() == c1[i].getShu2()&& 
                    c1[i].getFh() == c1[j].getFh()) || (c1[i].getShu2() == c1[j].getShu1() &&
                    c1[j].getShu2() == c1[i].getShu1()&& c1[i].getFh() == c1[j].getFh()))
                {
                    c1[i] = new Cal_zs(cc, w);
                    j = 0;
                }
            }
            
            c2[i] = new Cal_zfs(cc, w);
            while(fs == false && c2[i].getResult().charAt(0) == '-')
            {
                c2[i] = new Cal_zfs(cc, w);
            }
            for(int k = 0;k < i;k++)
            {
                if((c2[i].getShu1() == c2[k].getShu1() && c2[k].getShu2() == c2[i].getShu2() && 
                        c2[i].getFh() == c2[k].getFh()) || (c2[i].getShu2() == c2[k].getShu1() && 
                        c2[k].getShu2() == c2[i].getShu1() && c2[i].getFh() == c2[k].getFh()))
                {
                    c2[i] = new Cal_zfs(cc, w);
                    k = 0;
                }
            }
        }
    }
    
    public boolean yu(String str)
    {
        for(int i = 0;i < str.length(); i++)
        {
            if(str.charAt(i) == '/')
                return true;
        }
        return false;
    }
    
    public String show1(int i)
    {
        return c1[i].show();
    }
    public String show2(int i)
    {
        return c2[i].show();
    }

    public String result1(int i)
    {
        return c1[i].getResult() + "";
    }
    public String result2(int i)
    {
        return c2[i].getResult() + "";
    }
}
shu.java

com.db包

DB.java

package com.db;

import java.sql.*;

public class DB {
    private static final String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=calculate";
    private static final String dbusername = "sa";
    private static final String dbpassword = "4980";
    private static Connection coon = null;

    public static Connection getCoon() {
        return coon;
    }

    static{
        try{
            Class.forName(driverStr);
            coon = DriverManager.getConnection(connStr, dbusername, dbpassword);
        } catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (SQLException e){
            e.printStackTrace();
        }
    }
    
    public void closeCon(Connection con) {
        if(coon!=null){
            try {
                coon.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        try {
            DB.getCoon();
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>出题</title>
    <style type="text/css">
    h2{color:red}
    </style>
</head>
<body bgcolor="#E9E9E9">
    <h2 align="center">出题器</h2>
    <form name="form1" action="chuti.jsp" method="post">
        <table align="center">
            <tr>
                <td>题目类型:</td>
                <td>
                    <select name="lei">
                      <option value="整数" selected>整数</option>
                      <option value="真分数">真分数</option>
                      <option value="混合">混合</option>
                    </select>
                </td>  
            </tr>
            <tr>
                <td>是否有乘除法:</td>
                <td>
                    <input type="radio" name="cc" value="无" checked><input type="radio" name="cc" value="有"></td>
            </tr>
            <tr>
                <td>是否有括号:</td>
                <td>
                    <input type="radio" name="kh" value="无" checked><input type="radio" name="kh" value="有"></td>
            </tr>
            <tr>
                <td>数值范围:</td>
                <td>
                    <select name="wei">
                      <option value="一位数" selected>一位数</option>
                      <option value="两位数">两位数</option>
                      <option value="三位数">三位数</option>
                    </select>
                </td>  
            </tr>
            <tr>
                <td>加减有无负数:</td>
                <td>
                    <input type="radio" name="fushu" value="无" checked><input type="radio" name="fushu" value="有"></td>
            </tr>
            <tr>
                <td>除法有无余数:</td>
                <td>
                    <input type="radio" name="yushu" value="无" checked><input type="radio" name="yushu" value="有"></td>
            </tr>
            <tr>
                <td>题量</td>
                <td>
                    <input type="text" name="num" value="1">不大于100题
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="开始出题!"></td>
            </tr>
        </table>
    </form>
</body>
</html>
first.jsp

chuti.jsp

<%@ page language="java" import="java.util.*, java.sql.*, calculat.shu" contentType="text/html; charset=gb2312"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<fmt:requestEncoding value="gb2312"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
    h2{color:red}
    
    .tb1{
    border-left-style:solid;
    border-bottom-style:solid;
    border-top-style:solid;
    border-right-style:solid;
    border-2px;
    }
    
    #submit{
        height: 30px;
        background: rgba(0,0,0,0.1);
        border:solid #ccc 1px;
        border-radius: 3px;
    }
    
    #submit:hover{
        cursor: pointer;
        background:#D8D8D8;
    }
</style>
<script type="text/javascript">
  var i = 0;
  var Color = new Array("#0000FF", "#99FF00", "#660033", "#CC66CC", "#FFFF33");
  function change(){
      if(i > Color.length - 1)
          i = 0;
      table1.style.borderColor = Color[i];
      i++;
      setTimeout("change()", 500);
  }
</script>
    <title>出题</title>
</head>
<body onload="change()">
    <h1 align="center">题目</h1>
      <table id="table1" align="center" class="tb1">
          <tr>
              <td>题目类型:</td>
              <td>${param.lei}</td>
              <td>&nbsp;&nbsp;</td>
              <td>是否有乘除法:</td>
              <td>${param.cc}</td>
          </tr>
          <tr>
              <td>是否有括号:</td>
              <td>${param.kh}</td>
              <td>&nbsp;&nbsp;</td>
              <td>数值范围:</td>
              <td>${param.wei}</td>
          </tr>
          <tr>
              <td>加减有无负数:</td>
              <td>${param.fushu}</td>
              <td>&nbsp;&nbsp;</td>
              <td>除法有无余数:</td>
              <td>${param.yushu}</td>
          </tr>
      </table>
      
      <h2 align="center">请填写答案:</h2>
      <form name="form2" action="result.jsp" method="post">
          <table align="center">
          <tr>
              <th>题目</th>
              <th>&nbsp;</th>
              <th>答案</th>
          </tr>
          
          <jsp:useBean id="c" class="calculat.shu" scope="request"/>
          <jsp:useBean id="db" class="com.db.DB" scope="request"/>
          <%
          boolean cc = true;//有无乘除,并赋值
          if((request.getParameter("cc")).equals("无"))
          {
              cc = false;
          }
          int w;
          if(request.getParameter("wei").equals("一位数"))
          {
              w = 1;
          }
          else if(request.getParameter("wei").equals("两位数"))
          {
              w = 2;
          }
          else
          {
              w = 3;
          }
           
          boolean fs = true;
          if(request.getParameter("fushu").equals("无"))//负数
          {
              fs = false;
          }
          boolean ys = true;
          if(request.getParameter("yushu").equals("无"))//余数
          {
              ys = false;
          }
          
          //生成算式
          c = new shu(cc, w, fs, ys);
          int N = Integer.parseInt(request.getParameter("num"));//出题数
          c.setN(N);
          
          String str = request.getParameter("lei");//出题类型
          c.setCc(cc);
         
          //清空数据库中前N个元素
          for(int i = 0;i < N;i++)
          {
              String sql="delete from subject where num=" + (i+1);//生成一条sql语句
              Statement stmt = db.getCoon().createStatement();
              stmt.execute(sql);
          }
          
          //生成算式,并存储在数据库中
          String s = "", q = "";
          for(int i = 0;i < N;i++)
            {
            out.print("<tr><td>");
              if(str.equals("整数"))
              {
                  q = c.show1(i);
                  out.print(q + "</td>");
                  s = c.result1(i);
              }
              else if(str.equals("真分数"))
              {
                  q = c.show2(i);
                  out.print(q + "</td>");
                  s = c.result2(i);
              }
              else
              {
                  if(i % 2 == 0)
                  {
                      q = c.show1(i);
                      out.print(q + "</td>");
                      s = c.result1(i);
                  }
                  else
                  {
                      q = c.show2(i);
                      out.print(q + "</td>");
                      s = c.result2(i);
                  }
              }
              String sql = "insert into subject (num, question,answer) values (?,?,?)";
              PreparedStatement pstmt = null;
              pstmt = db.getCoon().prepareStatement(sql);
              pstmt.setString(1, i+1+"");
              pstmt.setString(2, q);
              pstmt.setString(3, s);
              pstmt.executeUpdate();
          %>
             <td>=</td>
             <td><input name="result<%=i %>" id="result" type="text">
             </td></tr>
          <%} %>
          <tr>
          <td>
              <input type="hidden" name="num" value="${param.num}">
          </td>
          </tr>
          <tr>
              <td></td>
              <td></td>
              <td><input type="submit" id="submit" value="点击提交答案"></td>
          </tr>
          </table>
      </form>
</body>
</html>

result.jsp

<%@ page language="java" import="java.sql.*" contentType="text/html; charset=gb2312"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>结果</title>
</head>
<body>
    <h1 align="center">成绩</h1>
    <table align="center">
    <jsp:useBean id="db" class="com.db.DB" scope="request"/>
    <%
        int N = Integer.parseInt(request.getParameter("num"));//出题数
        String x = "";
        for(int i = 0;i < N;i++)
        {
            x = (String)request.getParameter("result"+i);
            String sq = "select * from subject where num=?";
             PreparedStatement st = null;
             st = db.getCoon().prepareStatement(sq);
             st.setString(1, i+1+"");
             ResultSet rs = st.executeQuery();
            while(rs.next())
            {
                out.print("<tr><td>" + rs.getString(2) + "</td><td>&nbsp;=&nbsp;</td>");
                if(rs.getString(3).equals(x))
                {
                    out.print("<td>" + x + "</td>");
                    %>
                    <td><img alt="正确" src="images/accept.png"></td>
                    </tr>
                    <% 
                }
                else 
                {
                    out.print("<td>" + x + "</td>");
                    %>
                    <td><img alt="错误" src="images/gif-0130.gif">正确答案为<%=rs.getString(3) %></td>
                    </tr>
                    <% 
                }
            }
        }
     %>
    </table>
</body>
</html>

图片:

3.运行结果截图

出题器

题目类型:
是否有乘除法:
是否有括号:
数值范围:
加减有无负数:
除法有无余数:
题量 不大于100题
 

4.编程总结分析

自己学习了大量的知识,独立思考,查询资料解决问题,自己独立开发的一个项目,学会了很多,一次很愉快的编程!在编程过程中有许许多多的问题,但是都会克服的!继续向一个软件工程师奋斗!

5.按PSP0级的要求记录开发过程中的时间记录日志

原文地址:https://www.cnblogs.com/fylove/p/6567750.html