·主要思路:设计三个Jsp,分别是控制出题的参数,出题及输入题目答案,处理输入的答案。创建一个类包,将涉及到的方法连接数据库,处理数据库中表的数据,计算有无括号的整数运算,计算求余运算的整数运算,给式子加括号,求最大公约数,计算有无括号的真分数运算。其中,将出题的方法写在出题的jsp中,利用<%! %>的方法封装出题方法,主程序利用if结构来实现出题的相应格式。
·代码如下:
NewFile.jsp
<%@ 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=ISO-8859-1"> <title>出题系统</title> </head> <body > <h2>出题系统</h2> <form action="check.jsp" method="post"> 出题数量:<input type="text" name = "username"><br> 题目样式:请输入几元运算<br> 输入: <input type="text" name="choose1"><br> 题目样式:请输入范围<br> 输入:<input type="text" name="choose2"><br><hr> 题目样式:1.整数四则运算 2.真分数四则运算<br> 选择: 有<input type="radio" name="choose"value=1> 无<input type="radio" name="choose"value=2><br> 题目样式:1.有乘除 2.无乘除<br> 选择: 有<input type="radio" name="choose3"value=1> 无<input type="radio" name="choose3"value=2><br> 题目样式:1.有负数 2.无负数<br> 选择: 有<input type="radio" name="choose4"value=1> 无<input type="radio" name="choose4"value=2><br> 题目样式:1.有余数 2.无余数<br> 选择: 有<input type="radio" name="choose6"value=1> 无<input type="radio" name="choose6"value=2><br> 题目样式:1.有括号 2.无括号<br> 选择: 有<input type="radio" name="choose7"value=1> 无<input type="radio" name="choose7"value=2><br> 题目样式:1.显示答案 2.不显示答案<br> 选择: 有<input type="radio" name="choose5"value=1> 无<input type="radio" name="choose5"value=2><br> <input type="submit" value="出题"> </form> </body> </html>
check.jsp
<%@ page language="java" import="java.sql.*" import="java.util.*" contentType="text/html;; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import= "tianaoweb.com.* "%> <!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> <%! public static String ztimu(int num1,int num2,int num3) {//num1是几元运算,num2是范围,num3是有无乘除法 int intVal1 = 0; int ys = 0; String ss = ""; for(int i=1;i<=num1;i++) { intVal1 = (int)(Math.random()*num2+1); if(i < num1) { if(num3 == 2) ys = (int)(Math.random()*num3+1); else ys = (int)(Math.random()*(num3+3)+1); } else ys = 0; ss = ss + intVal1; if(ys==0) { ss = ss+""; } if(ys==1){ ss = ss +"+"; } if(ys==2){ ss = ss +"-"; } if(ys==3){ ss = ss +"*"; } if(ys==4){ ss = ss +"÷"; } } return ss; } %> <%! public static String ftimu(int num1,int num2,int num3) {//num1是几元运算,num2是范围,num3是有无乘除 int intVal1 = 0; int intVal2 = 0; int ys = 0; String ss = ""; for(int i=1;i<=num1;i++) { intVal1 = 0; intVal2 = 0; while(intVal1 == intVal2) { intVal1 = (int)(Math.random()*num2+1); intVal2 = (int)(Math.random()*num2+1); if(intVal1<intVal2) { ss = ss + intVal1+"/"+intVal2; } if(intVal1>intVal2) { ss = ss + intVal2+"/"+intVal1; } } if(i < num1) { if(num3 == 2) ys = (int)(Math.random()*num3+1); else ys = (int)(Math.random()*(num3+3)+1); } else { ys = 0; } if(ys==0) { ss = ss+""; } if(ys==1){ ss = ss +"+"; } if(ys==2){ ss = ss +"-"; } if(ys==3){ ss = ss +"*"; } if(ys==4){ ss = ss +"÷"; } } return ss; } %> <%! public static String fcal(String ss ,int n,int m) { fencal fen = new fencal(); String sz = fencal.shizi(ss, n, m); String fmm = fencal.fenmu(ss, n, m); double fm = Double.parseDouble(fmm); StringCaculate sc = new StringCaculate(); double fz = sc.fun2(sz); int fz1=(int)fz; int fm1=(int)fm; int gy = fen.getGongYueShu(fz1,fm1); String result = (fz1/gy) +"/"+(fm1/gy); return result; } %> <body> <form action="ans.jsp" method="post"> <% JspWriter mout=pageContext.getOut(); Action ac = new Action(); String us = request.getParameter("username");//出题数量 String ch = request.getParameter("choose");//整数四则运算还是真分数 String ch1 = request.getParameter("choose1");//几元运算 String ch2 = request.getParameter("choose2");//范围 String ch3 = request.getParameter("choose3");//有无乘除 String ch4 = request.getParameter("choose4");//有无负数 String ch5 = request.getParameter("choose5");//显示答案 String ch6 = request.getParameter("choose6");//有无余数 String ch7 = request.getParameter("choose7");//有无括号 int num = Integer.parseInt(us); int cho = Integer.parseInt(ch); int cho1 = Integer.parseInt(ch1); int cho2 = Integer.parseInt(ch2); int cho3 = Integer.parseInt(ch3); int cho4 = Integer.parseInt(ch4); int cho5 = Integer.parseInt(ch5); int cho6 = Integer.parseInt(ch6); int cho7 = Integer.parseInt(ch7); if(cho == 1)//整数题 { mout.print(num+"道整数题目"); mout.print("<br>"); String[] str = new String[num+1];//题目 double[] st = new double[num+1];//答案 double[] s = new double[num+1];//余数 str[0]=""; String ss = ""; if(cho4 == 1)//有负数 { if(cho6 == 2)//无余数 for(int i=1;i<=num;i++) { int state = 0; while(state == 0) { ss = ztimu(cho1,cho2,cho3); if(cho7 == 1)//有括号 { Kuohao k = new Kuohao(); ss =k.kh(ss,cho1); } StringCaculate re = new StringCaculate(); double result = re.fun2(ss); double r = result-(int)result; for(int j=0;j<i;j++) { if(ss.equals(str[j])||r!=0.0) { state = 0; } else { str[i] = ss; st[i] = result; state = 1; } } } mout.print(i+" 、 "); mout.print(str[i]); mout.print("= ?"); mout.print("<br>"); session.setAttribute("flag",i); %> 计算结果 :<input type="text" name="content<%=i %>" id="input"> <% mout.print("<br>"); } if(cho6 == 1)//有余数 for(int i=1;i<=num;i++) { int state = 0; while(state == 0) { ss = ztimu(cho1,cho2,cho3); if(cho7 == 1)//有括号 { Kuohao k = new Kuohao(); ss =k.kh(ss,cho1); } StringCaculate re = new StringCaculate(); double result = re.fun2(ss); double r = result-(int)result; for(int j=0;j<i;j++) { if(cho1>2)//三元以上无余数 { if(ss.equals(str[j])) { state = 0; } else { str[i] = ss; st[i] = result; state = 1; } } if(cho1<=2) { if(ss.equals(str[j])) { state = 0; } else { str[i] = ss; st[i] = (int)result; state = 1; if(r==0) s[i]=0; else { Caculate ree = new Caculate(); s[i] = ree.fun1(ss); } } } } } mout.print(i+" 、 "); mout.print(str[i]); mout.print("= ?"); mout.print("<br>"); } } if(cho4 == 2)//无负数 { if(cho6 == 2)//无余数 for(int i=1;i<=num;i++) { int state = 0; while(state == 0) { ss = ztimu(cho1,cho2,cho3); if(cho7 == 1)//有括号 { Kuohao k = new Kuohao(); ss =k.kh(ss,cho1); } StringCaculate re = new StringCaculate(); double result = re.fun2(ss); double r = result-(int)result; for(int j=0;j<i;j++) { if(ss.equals(str[j])||r!=0.0||result<0) { state = 0; } else { str[i] = ss; st[i] = result; state = 1; } } } mout.print(i+" 、 "); mout.print(str[i]); mout.print("= ?"); mout.print("<br>"); %> 计算结果 :<input type="text" name="content<%=i %>" id="input"> <% mout.print("<br>"); } if(cho6 == 1)//有余数 for(int i=1;i<=num;i++) { int state = 0; while(state == 0) { ss = ztimu(cho1,cho2,cho3); if(cho7 == 1)//有括号 { Kuohao k = new Kuohao(); ss =k.kh(ss,cho1); } StringCaculate re = new StringCaculate(); double result = re.fun2(ss); double r = result-(int)result; for(int j=0;j<i;j++) { if(cho1>2)//三元以上无余数 { if(ss.equals(str[j])||result<0) { state = 0; } else { str[i] = ss; st[i] = result; state = 1; } } if(cho1<=2) { if(ss.equals(str[j])||result<0) { state = 0; } else { str[i] = ss; st[i] = (int)result; state = 1; if(r==0) s[i]=0; else { Caculate ree = new Caculate(); s[i] = ree.fun1(ss); } } } } } mout.print(i+" 、 "); mout.print(str[i]); mout.print("= ?"); mout.print("<br>"); } } String[] newst = new String[num+1]; for(int i=1;i<=num;i++) newst[i] = (int)st[i]+""; for(int number = 1;number<=num;number++) ac.add(number,str[number],newst[number]); %><input type="submit" value="提交"> <% session.setAttribute("str",newst); session.setAttribute("st",str); if(cho5 == 1) { mout.print("<br>"); mout.print("参考答案"); mout.print("<br>"); for(int m=1;m<=num;m++) { mout.print(m+" 、 "+st[m]); if(s[m] != 0) mout.print("······"+s[m]); mout.print("<br>"); } } } if(cho == 2)//真分数 { mout.print(num+"道真分数题目"); mout.print("<br>"); String[] st = new String[num+1]; String[] st1 = new String[num+1]; st[0]=""; String ss = ""; for(int i=1;i<=num;i++) { int state = 0; while(state == 0) { ss = ftimu(cho1,cho2,cho3); for(int j=0;j<i;j++) { if(ss.equals(st[j])) { state = 0; } else { st[i] = ss; String ans = fcal(ss,cho1,cho7); st1[i] = ans; state = 1; } } } mout.print(i+" 、 "); mout.print(st[i]); mout.print("= ?"); mout.print("<br>"); session.setAttribute("flag",i); %> <%-- 题号 :<input type="text" name="num+<%=i %>" id="input"> --%> 计算结果 :<input type="text" name="content<%=i %>" id="input"> <% mout.print("<br>"); } for(int number = 1;number<=num;number++) ac.add(number,st[number],st1[number]); %><input type="submit" value="提交"> <% session.setAttribute("str",st1); session.setAttribute("st",st); if(cho5 == 1) { mout.print("<br>"); mout.print("参考答案"); mout.print("<br>"); for(int m=1;m<=num;m++) { mout.print(m+" 、 "+st1[m]); mout.print("<br>"); } } } %> </body> </html>
ans.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import= "tianaoweb.com.* "%> <!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=ISO-8859-1"> <title>ans.jsp</title> </head> <body> <% JspWriter mout=pageContext.getOut(); int date = (int)request.getSession().getAttribute("flag"); String[] an = new String[date+1]; for(int i=1;i<=date;i++) { String answer = request.getParameter("content"+i); an[i] = answer; System.out.println(date); //Action ac = new Action(); //ac.addans(i, answer); } String[] str=(String[])session.getAttribute("str");//答案 String[] st=(String[])session.getAttribute("st"); for(int i=1;i<=date;i++) { System.out.println(str[i]); } int correct = 0; int wrong = 0; %><p>做错的题如下:</p><% for(int i=1;i<=date;i++) { if(str[i].equals(an[i])) { correct++; } else { wrong++; mout.print(wrong+" 、 "+st[i]+" = "+an[i]); mout.print("正确答案为 "+str[i]); mout.print("<br>"); } } %><p>本次测验结果如下:</p><% mout.print("共错"+wrong+"道题"); mout.print("<br>"); mout.print("共对"+correct+"道题"); mout.print("<br>"); %> </body> </html>
Main.java
package tianaoweb.com; import java.sql.*; public class Main { public static Connection getConn(){ String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; Connection dbConn = null; String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=哈哈"; String userName="sa"; String userPwd="123456"; try { Class.forName(driverName); System.out.println("加载驱动成功!"); }catch(Exception e){ e.printStackTrace(); System.out.println("加载驱动失败!"); } try{ dbConn=DriverManager.getConnection(dbURL,userName,userPwd); System.out.println("连接数据库成功!"); }catch(Exception e) { e.printStackTrace(); System.out.print("SQL Server连接失败!"); } return dbConn; } }
Action.java
package tianaoweb.com; import java.sql.*; public class Action { public void add(int num,String str1,String str2) throws SQLException{ Connection conn = Main.getConn(); //获取数据库连接 Statement st = conn.createStatement(); String sql = "insert into calTable([number],[question],[answer]) values('"+ num +"','" + str1 + "','" + str2 + "')"; st.executeUpdate(sql); st.close(); conn.close(); } /*public void addans(int num,String str1) throws SQLException{ System.out.println(num + " "+str1); Connection conn = Main.getConn(); //获取数据库连接 Statement st = conn.createStatement(); String sql = "update calTable set own = '"+str1+"' where number = '"+num+"'"; st.executeUpdate(sql); st.close(); conn.close(); }*/ }
Caculate.java
package tianaoweb.com; import java.util.HashMap; import java.util.Map; import java.util.Stack; public class Caculate { /*public static void main(String[] args) { // TODO Auto-generated method stub String str1="22+4*(2+3)"; double result=fun2(str1); System.out.println(result); }*/ public static Map pro=new HashMap(); public static void init() { pro.put('+', 1); pro.put('-', 1); pro.put('*', 2); pro.put('÷', 2); } public static int getIndex(String str) { int index1=(str.indexOf('+')==-1?str.length():str.indexOf('+')); int index2=(str.indexOf('-')==-1?str.length():str.indexOf('-')); int index3=(str.indexOf('*')==-1?str.length():str.indexOf('*')); int index4=(str.indexOf('÷')==-1?str.length():str.indexOf('÷')); int index=index1<index2?index1:index2; index=index<index3?index:index3; index=index<index4?index:index4; return index; } public static double cal(char op,double num1,double num2) { switch(op) { case '+': return num1+num2; case '-': return num1-num2; case '*': return num1*num2; default: return num1%num2; } } public static double fun1(String str) { init(); Stack st1=new Stack(); Stack st2=new Stack(); int fop=0; while(str.length()>0) { int index=getIndex(str); st1.push(Double.parseDouble(str.substring(0,index))); if(index!=str.length()) { char op=str.charAt(index); str=str.substring(index+1); while(true) { if((int)pro.get(op)>fop) { st2.push(op); fop=(int)pro.get(op); break; } else { double num2= (double) st1.pop(); double num1=(double) st1.pop(); double result=cal((char)st2.pop(),num1,num2); st1.push(result); if(st2.size()==0) { st2.push(op); fop=(int)pro.get(op); break; } char cop=(char) st2.pop(); fop=(int)pro.get(cop); st2.push(cop); } } } else { break; } } while(st2.size()!=0) { double num2=(double) st1.pop(); double num1=(double) st1.pop(); char op=(char) st2.pop(); st1.push(cal(op,num1,num2)); } double result=(double) st1.pop(); return result; } public static double fun2(String str) { while(str.indexOf('(')!=-1) { int left=0; int right=str.length(); char op; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='(') { left=i; } if(str.charAt(i)==')') { right=i; break; } } str=str.substring(0,left)+fun1(str.substring(left+1,right))+str.substring(right+1); } return fun1(str); } }
StringCaculate.java
package tianaoweb.com; import java.util.HashMap; import java.util.Map; import java.util.Stack; public class StringCaculate { public static Map pro=new HashMap(); public static void init() { pro.put('+', 1); pro.put('-', 1); pro.put('*', 2); pro.put('÷', 2); } public static int getIndex(String str) { int index1=(str.indexOf('+')==-1?str.length():str.indexOf('+')); int index2=(str.indexOf('-')==-1?str.length():str.indexOf('-')); int index3=(str.indexOf('*')==-1?str.length():str.indexOf('*')); int index4=(str.indexOf('÷')==-1?str.length():str.indexOf('÷')); int index=index1<index2?index1:index2; index=index<index3?index:index3; index=index<index4?index:index4; return index; } public static double cal(char op,double num1,double num2) { switch(op) { case '+': return num1+num2; case '-': return num1-num2; case '*': return num1*num2; default: return num1/num2; } } public static double fun1(String str){ if(str.charAt(0)=='-'){ return fun1("0"+str); } int q=-1,qq=-1; for(int i=0;i<str.length();i++){ if(str.charAt(i)=='-'&&str.charAt(i+1)=='-'){ q=i; } if(str.charAt(i)=='+'&&str.charAt(i+1)=='-'){ qq=i; } } if(q!=-1){ return fun1(str.substring(0,q)+"+"+str.substring(q+2,str.length())); } if(qq!=-1){ return fun1(str.substring(0,qq)+"-"+str.substring(qq+2,str.length())); } init(); Stack st1=new Stack(); Stack st2=new Stack(); int fop=0; while(str.length()>0){ int index=getIndex(str); st1.push(Double.parseDouble(str.substring(0,index))); if(index!=str.length()) { char op=str.charAt(index); str=str.substring(index+1); while(true){ if((int)pro.get(op)>fop) { st2.push(op); fop=(int)pro.get(op); break; } else { double num2= (double) st1.pop(); double num1=(double) st1.pop(); double result=cal((char)st2.pop(),num1,num2); st1.push(result); if(st2.size()==0) { st2.push(op); fop=(int)pro.get(op); break; } char cop=(char) st2.pop(); fop=(int)pro.get(cop); st2.push(cop); } } } else break; } while(st2.size()!=0) { double num2=(double) st1.pop(); double num1=(double) st1.pop(); char op=(char) st2.pop(); st1.push(cal(op,num1,num2)); } double result=(double) st1.pop(); return result; } public static double fun2(String str){ while(str.indexOf('(')!=-1) { int left=0; int right=str.length(); char op; for(int i=0;i<str.length();i++) { if(str.charAt(i)=='(') left=i; if(str.charAt(i)==')') { right=i; break; } } double result=fun1(str.substring(left+1,right)); System.out.println(result); str=str.substring(0,left)+result+str.substring(right+1); System.out.println(str); } return fun1(str); } }
fencal.java
package tianaoweb.com; public class fencal { public static String fenmu(String ss,int n,int m) {//n 几元,m 有无括号 ss = '='+ss+'='; String result=""; String cal = ""; int num = n*2+1; if(m == 1) { num = num + 2; } int length = ss.length(); int[] in = new int[num];//操作符下标数组 String[] str = new String[n];//分母数组 String[] str1 = new String[n];//分子数组 int i=0;//操作符计数器 int j=0;//字符串计数器 int z=0;//分母数组计数器 double fm=1; for(j=0;j<length;j++) { if(ss.charAt(j)=='+'||ss.charAt(j)=='÷'||ss.charAt(j)=='-'||ss.charAt(j)=='*'||ss.charAt(j)=='('||ss.charAt(j)==')'||ss.charAt(j)=='/'||ss.charAt(j)=='=') { in[i] = j; i++; } } /*for(i=0;i<num;i++) System.out.println(in[i]);*/ for(i=0;i<num;i++) { if(ss.charAt(in[i]) == '/') { str[z] = ss.substring(in[i]+1, in[i+1]); z++; } } /*for(z=0;z<n;z++) System.out.println(str[z]);*/ for(z=0;z<n;z++) { double fmm = Double.parseDouble(str[z]); fm = fm*fmm; } System.out.println("分母为"+fm); return ""+fm; } public static String shizi(String ss,int n,int m) { String shizi = ""; ss = '='+ss+'='; String result=""; String cal = ""; int num = n*2+1; if(m == 1) { num = num + 2; } int length = ss.length(); int[] in = new int[num];//操作符下标数组 String[] str = new String[n];//分母数组 String[] str1 = new String[n];//分子数组 int i=0;//操作符计数器 int j=0;//字符串计数器 int z=0;//分母数组计数器 double fm=1; for(j=0;j<length;j++) { if(ss.charAt(j)=='+'||ss.charAt(j)=='÷'||ss.charAt(j)=='-'||ss.charAt(j)=='*'||ss.charAt(j)=='('||ss.charAt(j)==')'||ss.charAt(j)=='/'||ss.charAt(j)=='=') { in[i] = j; i++; } } for(i=0;i<num;i++) System.out.println(in[i]); for(i=0;i<num;i++) { if(ss.charAt(in[i]) == '/') { str[z] = ss.substring(in[i]+1, in[i+1]); z++; } } /*for(z=0;z<n;z++) System.out.println(str[z]);*/ for(z=0;z<n;z++) { double fmm = Double.parseDouble(str[z]); fm = fm*fmm; } System.out.println("分母为"+fm); z=0; for(i=0;i<num;i++) { if(ss.charAt(in[i]) == '/') { str1[z] = ss.substring(in[i-1]+1, in[i]); z++; } } for(z=0;z<n;z++) { double fmm = Double.parseDouble(str[z]); double fzz = Double.parseDouble(str1[z]); double fz = fm/fmm*fzz; str1[z] = ""+fz; } for(z=0;z<n;z++) System.out.println("分子"+str1[z]); z=0; int numm = n-1; if(m == 1) numm = numm +2; String[] str2 =new String[numm]; for(i=0;i<num;i++) { if(ss.charAt(in[i])!='='&&ss.charAt(in[i])!='/') { str2[z] = ""+ss.charAt(in[i]); z++; } } /*for(z=0;z<numm;z++) System.out.println(str2[z]);*/ if(m == 1) { for(i=0;i<numm;i++) { if(str2[i].equals("(")&&i>0) { j=i-1; str2[j] = str2[j] + str2[i]; break; } } for(j=j+1;j<numm-1;j++) str2[j]=str2[j+1]; } if(m == 1) { for(i=0;i<numm;i++) { if(str2[i].equals(")")&&i<numm-1) {System.out.println("zou"); j=i+1; str2[i] = str2[i]+str2[j]; break; } } for(;j<numm-1;j++) str2[j]=str2[j+1]; } for(z=0;z<numm;z++) System.out.println(str2[z]); if(str2[0].equals("(")) { for(i=0;i<n-1;i++) { shizi = shizi + str2[i]+""+ str1[i]; } shizi = shizi + str2[i]+str1[n-1]; } else if(str2[numm-1].equals(")")) { for(i=0;i<n-1;i++) { shizi = shizi + str1[i]+ ""+ str2[i]; } shizi = shizi +str1[n-1]+str2[i]; } else { for(i=0;i<n-1;i++) { shizi = shizi + str1[i]+""+ str2[i]; } shizi = shizi + str1[n-1]; } return shizi; } public static int getGongYueShu(int a, int b){ int t = 0; if(a<b){ t =a; a=b; b=t; } int c=a%b; if(c==0) { return b; } else return getGongYueShu(a,c); } }
结果截图:
(1)整数运算
(2)真分数运算