03四则运算
in 归档 with 0 comment

03四则运算

in 归档 with 0 comment

04四则运算
最后由@chen_修改于2017-12-06 21:09

I. 计划
需求描述
二年级的小学生们需要做大量的计算题来锻炼自己的能力,但是家长又会苦恼于出计算题,因为检查结果的正确与错误也是一件很麻烦的事情,于是有了这个项目。
估计开发时间
两天
时间开发日志
12.5 中午12.20-1.49进行简单的编码
12.6 中午12.20-8.34继续进行编码
II. 开发
缺陷日志:
1.像数据库中添加数据时表名出错
2.生成表达式的时候类型转换错误,原因是表达式中引号缺失。
III. 总结
做了后台开发部分,但是做这个项目的时候没有考虑重复的情况,可能会造成题库里面的题目重复。
但是引入查重机制之后会造成生成表达式速度过慢。
这是本项目的一个缺陷,暂时没有更好的解决办法。

<%@page import="com.jaovo.msg.dao.ExerciseDaoImpl"%>
<%@page import="com.jaovo.msg.model.Exercise"%>
<%@page import=" java.util.ArrayList" %>
<%@page import=" java.util.List" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    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">
<%
int m=0;//记录正确的题数;
int n=0;//记录错误的题数;
Exercise exercise=new Exercise();
ExerciseDaoImpl exerciseDaoImpl=new ExerciseDaoImpl();
List<Exercise>list=new ArrayList<Exercise>();
String id=(String)session.getAttribute("id");
String result[]=request.getParameterValues("result");
String Id[]=id.toString().trim().split(" ");

for(int i=0;i<Id.length/2;i++)
{
    exercise=exerciseDaoImpl.loadTest(Integer.parseInt(Id[i]));
    list.add(exercise);
    System.out.println(Id[i]);
}

for(int i=Id.length/2;i<Id.length;i++)
{
    exercise=exerciseDaoImpl.loadTest1(Integer.parseInt(Id[i]));
    list.add(exercise);
}

for(int j=0;j<result.length;j++)
{
    System.out.println(result[j]);
}


%>
<table align="center" border="1" >
 
    <tr>
                       <td>
                                                                           编号
                       </td>
                       <td>
                                                                        题号
                       </td>
                   <td>
                                                                     题目
                  </td>
                  <td>
                                                                      结果
                   </td>
                   <td>
                                                                 正确结果
                   </td>
         
    </tr>
    
    <%
    int i=0;
    for(Exercise exercise1:list){
    %>
          <tr>
                           
                            <td>
                            <%=i+1 %>
                            </td>
                            <td>
                            <input style="text" name="id" value="<%=exercise1.getId() %>" randomly/>
                            </td>
                            <td>
                            <%=exercise1.getExperssion() %>
                            </td>
                            <td>
                           <%
                           if(result[i].equals(list.get(i).getResult()))
                           {
                               m++;
                               %>
                               <font style="color:green ; font-size">
                               <% out.print(result[i]);%>
                               </font><%
                           }
                           else
                           {
                               n++;
                           %>
                           <font style="color:red ; font-size">
                           <% out.print(result[i]);%>
                               </font>
                           <%     
                           }
%>
                            </td>
                           <td>
                           <%=exercise1.getResult() %>
                            </td>
                           </tr>
         <%
    i++;
    }
       
   // session.setAttribute("loginUser",user);
   session.invalidate();
    
    %>
  
    </table>
    
      <br>
    <br>
    <br>
    <table align="center" border="1">
    <tr>
    <td>
       答对的题目
    </td>
    <td>
       答错的题目
    </td>
    </tr>
    <tr>
    <td>
    <input style="text" name="right"  value="<%=m%>"/>
    </td>
     <td>
    <input style="text" name="fault"  value="<%=n%>"/>
    </td>
    </tr>
    </table>>
    </head>
</html>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.jaovo.msg.Util.UserException"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<div >
<form action="list1.jsp" method="get">
<table align="center" border="1">
<tr>
   <td>
   产生题的数量
   </td>
   <td>
   <input type="text" name="number" />
   </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value ="开始答题"/>
</td>
</tr>
</table>
</form>
</div>
<%
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currenTime=new Date();
String time=simpleDateFormat.format(currenTime).toString();
out.print("当前时间:"+time);
%>
</body>
</html>
package com.jaovo.msg.dao;

import java.util.ArrayList;

public class CheckAnswer {
    
static    public String calaulateAnswer(ArrayList list){
        
    if(list.size()==3){
        char opr=(char)list.get(1);
        switch(opr){
        
        case '+':return Integer.toString((int)list.get(0)+(int)list.get(2));
        
        case '-':return Integer.toString((int)list.get(0)-(int)list.get(2));
        
        case '*':return Integer.toString((int)list.get(0)*(int)list.get(2));
        
        case '/':return  Integer.toString((int)list.get(0)/(int)list.get(2));
        
        default :return null;
        }
    }else{//Ϊ��λ���ֵļӼ�
        
        
        char opr1=(char)list.get(1);
        char opr2=(char)list.get(3);
        
        if(opr1=='+'){
            if(opr2=='+'){
                return Integer.toString((int)list.get(0)+(int)list.get(2)+(int)list.get(4));    
            }else{
                return Integer.toString((int)list.get(0)+(int)list.get(2)-(int)list.get(4));
            }
            
        }else{
            if(opr2=='+'){
                return Integer.toString((int)list.get(0)-(int)list.get(2)+(int)list.get(4));
            }
            else{
                return Integer.toString((int)list.get(0)-(int)list.get(2)-(int)list.get(4));
            }
            
        }
        
        
        
    }    
    }
    
    
}
package com.jaovo.msg.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.model.Exercise;


public class ExerciseDaoImpl {
    public Exercise loadTest(int id) {
        Connection connection = DBUtil.getConnection();
        //閸戝棗顦瑂ql鐠囶厼褰�
        String sql = "select * from Table2  where id = ?";
        //閸掓稑缂撶拠顓炲綖娴肩姾绶�电钖�
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Exercise exercise = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                exercise = new Exercise();
                exercise.setId(id);
                
                exercise.setExperssion(resultSet.getString("expression"));
                exercise.setResult(resultSet.getString("result"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  exercise;
    }




//鑾峰彇涓変綅鏁板瓧锛�
public Exercise loadTest1(int id) {
    Connection connection = DBUtil.getConnection();
    //閸戝棗顦瑂ql鐠囶厼褰�
    String sql = "select * from Table1  where id = ?";
    //閸掓稑缂撶拠顓炲綖娴肩姾绶�电钖�
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    Exercise exercise = null;
    try {
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        resultSet = preparedStatement.executeQuery();
        while(resultSet.next()) {
            exercise = new Exercise();
            exercise.setId(id);
            
            exercise.setExperssion(resultSet.getString("expression"));
            exercise.setResult(resultSet.getString("result"));
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally {
        DBUtil.close(resultSet);
        DBUtil.close(preparedStatement);
        DBUtil.close(connection);
    }
    return  exercise;
}
}
package com.jaovo.msg.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) throws SQLException {
        // TODO Auto-generatesd method stub
        
        String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//��������
        String connectDB= "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jaovo_msg";//����Դ
        try
          {
           Class.forName(JDriver);//�������ݿ����棬���ظ����ַ���������
          }catch(ClassNotFoundException e)
          {
           //e.printStackTrace();
           System.out.println("�������ݿ�����ʧ��");
           System.exit(0);
          }
        System.out.println("���ݿ������ɹ�");
        
        String user="sa";
           String password="123456";
           Connection con = null;
        try {
            con = DriverManager.getConnection(connectDB,user,password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }//�������ݿ����
           System.out.println("�������ݿ�ɹ�");
           Statement stmt = null;
           try {
             stmt=con.createStatement();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }//����SQL�������
        //---------------------------------------------------------------------------------------//
        RandomString s=new RandomString();
        ArrayList list=new ArrayList();
        long i=0;
        while(i!=99){
        //list=s.newRandomString();
        list=s.newHarderRandomString();
        i++;
        System.out.println("-----------------------------------------");
        System.out.println("��ţ�"+i);
        
        stmt.executeUpdate("insert into Table2(expression,result)values('"+RandomString.toString(list)+"','"+CheckAnswer.calaulateAnswer(list)+"')");

        System.out.println("����ʽ�ӣ�"+RandomString.toString(list));    
        System.out.println(CheckAnswer.calaulateAnswer(list));
        System.out.println("-----------------------------------------");
        
        }
    }

}
Responses