05四则运算二
in 归档 with 0 comment

05四则运算二

in 归档 with 0 comment

05课堂练习
最后由@chen_修改于2017-11-28 17:53

I. psp0级
II. 1.计划
主要工作:需求描述,估计开发时间,填写项目计划数据,填写时间记录日志。
III. 2.开发
主要工作:设计程序,实现设计,编译程序,修复并记录所发现的缺陷,并填写缺陷日志,调试程序,修复并记录所发现的缺陷,并填写缺陷记录日志,填写时间记录日志。
IV. 总结
主要任务:汇总时间,缺陷和规模数据填写完称项目计划总结表。

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("-----------------------------------------");
        
        }
    }

}
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;


public class MyException {

}
package com.jaovo.msg.dao;

import java.util.ArrayList;
import java.util.Random;

//import javax.enterprise.concurrent.ManageableThread;

public class RandomString {

    static    private String op1=null;
    static    private String op2=null;
    static    private char opr;
    static int max=100;
    static    public ArrayList newRandomString(int ch){

        double op1 = 0,op2=0;
        
//        ((int)(Math.random()*4));//���������ͬ������ʽ��
        
        switch(ch){
            
            case 0:{//�����ӷ�

                 op1=Math.random()*100+1;
                 op2=Math.random()*(100-op1)+1;
                 opr='+';
            }
                break;
            case 1:{//����
                
                op1=Math.random()*100+1;
                op2=Math.random()*100+1;
                if(op2-op1>0){
                    double temp;
                    temp=op1;
                    op1=op2;
                    op2=temp;
                }
                opr='-';    
            }break;
            case 2:{//�˷�
                op1=Math.random()*10+1;
                op2=Math.random()*10+1;
                opr='*';
            }break;
            case 3:{//����
                
                op1=Math.random()*10+1;
                op2=Math.random()*10+1;
                op1=(int)op1*(int)op2;
                
                opr='/';                
            }break;
            
        }
        
    ArrayList list=new ArrayList();    
    list.add((int)op1);
    list.add(opr);
    list.add((int)op2);
    
        return list;
        
    }
    
    static public String toString(ArrayList list){
        
        String s="";
        for(int i=0;i<list.size();i++){
            if(i%2==0){
                s=s+list.get(i);
                }else{
                s=s+String.valueOf(list.get(i))  ;
                }
        
                //Integer.toString((int)list.get(0))+list.get(1)+Integer.toString((int)list.get(2));
        
        }
        return s;
        
        
    }
    
    static    public ArrayList newRandomString(){

        double op1 = 0,op2=0;
        
//        ((int)(Math.random()*4));//���������ͬ������ʽ��
        switch(((int)(Math.random()*4))){
            
            case 0:{//�����ӷ�

                 op1=Math.random()*100+1;
                 op2=Math.random()*(100-op1)+1;
                 opr='+';
            }
                break;
            case 1:{//����
                
                op1=Math.random()*100+1;
                op2=Math.random()*100+1;
                if(op2-op1>0){
                    double temp;
                    temp=op1;
                    op1=op2;
                    op2=temp;
                }
                opr='-';    
            }break;
            case 2:{//�˷�
                op1=Math.random()*10+1;
                op2=Math.random()*10+1;
                opr='*';
            }break;
            case 3:{//����
                
                op1=Math.random()*10+1;
                op2=Math.random()*10+1;
                op1=(int)op1*(int)op2;
                
                opr='/';                
            }break;
            
        }
        
    ArrayList list=new ArrayList();    
    list.add((int)op1);
    list.add(opr);
    list.add((int)op2);
    
        return list;
        
    }
    
    static public ArrayList newHarderRandomString(){
        
        ArrayList list=new ArrayList();
        char opr1 = '&',opr2='&';
        double op1,op2,op3;
        double c1=Math.random();
        double c2=Math.random();
        if(c1>=0.5){
            opr1='+';
        }else{
            opr1='-';
        }
        if(c2>=0.5){
            opr2='+';
        }else{
            opr2='-';
        }
        //���opr1��opr2
        //System.out.println("*********************");
//        System.out.println("opr1:"+opr1);
//        System.out.println("opr2:"+opr2);
//        System.out.println("*********************");
        //�������жϽ����100����
        if((opr1=='+')&&(opr2=='+')){//����ʱ
            
            op1=Math.random()*33;
            op2=Math.random()*33;
            op3=Math.random()*33;
            list.add((int)op1);
            list.add(opr1);
            list.add((int)op2);
            list.add(opr2);
            list.add((int)op3);    
            
    
        }else if((opr1=='-')&&(opr2=='-')){
            
            op3=Math.random()*100;
            op2=Math.random()*(100-op3);
            int min=(int)op2+(int)op3;
            Random random=new Random();
            op1=random.nextInt(max-min)+min;
            list.add((int)op1);
            list.add(opr1);
            list.add((int)op2);
            list.add(opr2);
            list.add((int)op3);

        }else if((opr1=='-')&&(opr2=='+')){
            
            list=RandomString.newRandomString(1);//1����
            list.add(opr2);
            op3=Math.random()*(100-((int)list.get(0)-(int)list.get(2)))+1;
            list.add((int)op3);
            
        }else if((opr1=='+')&&(opr2=='-')){
            
            list=RandomString.newRandomString(0);//0Ϊ�ӷ�
            list.add(opr2);
            op3=Math.random()*(((int)list.get(0)+(int)list.get(2)))+1;
            list.add((int)op3);
        }
        
        return list;
    } 

}
package com.jaovo.msg.model;

public class Exercise {

    private int id;
    private String experssion;
    private String  result;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getExperssion() {
        return experssion;
    }
    public void setExperssion(String experssion) {
        this.experssion = experssion;
    }
    public String getResult() {
        return result;
    }
    public void setResult(String result) {
        this.result = result;
    }  
    
}
package com.jaovo.msg.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 {
            //1 鍔犺浇椹卞姩
            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 = "123456zz";
        String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=HN";
        Connection connection = null;
        try {
            //2 鍒涘缓閾炬帴瀵硅薄connection
             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();
        }
    }
    
}
package com.jaovo.msg.Util;

public class UserException extends RuntimeException{

    public UserException() {
        super();
        // TODO Auto-generated constructor stub
    }

    public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        // TODO Auto-generated constructor stub
    }

    public UserException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public UserException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public UserException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }
    
}
<%@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.Random"%>
<%@page import="com.jaovo.msg.model.Exercise"%>
<%@page import="com.jaovo.msg.dao.ExerciseDaoImpl"%>
<%@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">
    <SCRIPT LANGUAGE="JavaScript">  
 var maxtime = 10 //半个小时,按秒计算,自己调整!  
 function CountDown(){  
 if(maxtime>=0){  
 
 seconds = Math.floor(maxtime%10);  
 msg = "距离结束还有"+seconds+"秒";  
 document.all["timer"].innerHTML=msg;  
 if(maxtime == 5) alert('注意,还有5秒!');  
 --maxtime;  
 }  
 else{  
 clearInterval(timer);  
 alert("时间到,结束!");
   document.getElementById('Tj').click();
   }  
 }  
 timer = setInterval("CountDown()",1000);  
 

 </SCRIPT> 
 </head>

<body>
<%
int a=Integer.parseInt(request.getParameter("number"));
ExerciseDaoImpl exerciseDaoImpl=new ExerciseDaoImpl();
List<Exercise>list=new ArrayList<Exercise>();

Exercise exercise=new Exercise();
String id="";
String result="";
Random random=new Random();

for(int i=0;i<a/2;i++)
{
    int b=random.nextInt(98)+1;
    id=id+" "+b;
    exercise=exerciseDaoImpl.loadTest(b);
    
    list.add(exercise);
}
for(int j=a/2;j<a;j++)
{
    int c=random.nextInt(98)+1;
    id=id+" "+c;
    exercise=exerciseDaoImpl.loadTest1(c);
   
    list.add(exercise);
}
 session.setAttribute("id", id);
 
%>
<div id="timer" style="color:red"></div> 
<form action="checkresult.jsp" method="get">
<table align="center" border="1" >
 
    <tr>
                       <td>
                                                                           编号
                       </td>
                       <td>
                                                                        题号
                       </td>
                   <td>
                                                                     题目
                  </td>
                  <td>
                                                                      结果
                   </td>
         
         
    </tr>
    
    <%
    int i=1;
    for(Exercise exercise1:list){
    %>
          <tr>
                           
                            <td>
                            <%=i %>
                            </td>
                            <td>
                            <input style="text" name="id" value="<%=exercise1.getId() %>" />
                            </td>
                            <td>
                            <%=exercise1.getExperssion() %>
                            </td>
                            <td>
                           <input style="text" name="result"   />
                            </td>
                           
                            
                           </tr>
         <%
    i++;
    }
       
   // session.setAttribute("loginUser",user);
  
    
         %>
         <tr>
         <td align="center" colspan="4">
         <input type="submit" value="提交" name="Tj" id="Tj"/>
         </td>
         </tr>
         </table>
</body>
</html>
<%@ 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>
    <title>用户登录页面</title>
</head>
<body>

    <%String res=(String)request.getAttribute("error");
    if(res=="null"||res==null)
    {
        res="";
    }
    %>
    <%=res%>
    <form action="logincheck.jsp" method="get">
        <table align="center" border="1" width="500">
            <tr>
                <td>用户名称 : </td>
                <td>
                    <input type="text" name="username" />
                </td>
            </tr>
                <tr>
                <td>用户密码:</td>
                <td>
                    <input type="password" name="password" />
                </td>
                
            </tr>
            <tr align="center">
                <td colspan="2">
                    <input type="submit" value="登录" />
                    <input type="reset" value="重置" />
                    <input type="button" value="注册" onClick="window.location.href='addInput.jsp'"/>    
                </td>
            </tr>
        </table>
    </form>
</body>
</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>
Responses