软件工程结对作业02(借鉴版)
在编写程序的时候因为不会将Java程序嵌入进jsp中所以借鉴了一个学姐的程序,下面是学姐的程序。但在对学姐程序就行修改时发现了一些问题:
在Newjsp中能调用html中的题目个数的参数
在Myjsp中不能调用html中的题目个数的参数
其中这个参数timugeshu,是我自己后来添加的,用来改变题目个数的,与Java程序无关,只在jsp文件中作用
源程序:
NewFeil.html <!DOCTYPE html> <html> <head> <title>四则运算</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="this is my page"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body background="images/A1.jpg"> <div style="width: 100%; font-family: 微软雅黑; text-align: center"> <marquee scrollamount=3 style="COLOR=000000 FILTER: shadow(color=000000 ); FONT-FAMILY: 隶书; FONT-SIZE: 25pt; WIDTH: 100%"> 欢迎来到四则运算乐园</marquee></div> <span id="localtime"></span> <script type="text/javascript"> function showLocale(objD) { var str, colorhead, colorfoot; var yy = objD.getYear(); if (yy < 1900) yy = yy + 1900; var MM = objD.getMonth() + 1; if (MM < 10) MM = ‘0‘ + MM; var dd = objD.getDate(); if (dd < 10) dd = ‘0‘ + dd; var hh = objD.getHours(); if (hh < 10) hh = ‘0‘ + hh; var mm = objD.getMinutes(); if (mm < 10) mm = ‘0‘ + mm; var ss = objD.getSeconds(); if (ss < 10) ss = ‘0‘ + ss; var ww = objD.getDay(); if (ww == 0) colorhead = "<font color=\"#000000\">"; if (ww > 0 && ww < 6) colorhead = "<font color=\"#000000\">"; if (ww == 6) colorhead = "<font color=\"#000000\">"; if (ww == 0) ww = "星期日"; if (ww == 1) ww = "星期一"; if (ww == 2) ww = "星期二"; if (ww == 3) ww = "星期三"; if (ww == 4) ww = "星期四"; if (ww == 5) ww = "星期五"; if (ww == 6) ww = "星期六"; colorfoot = "</font>" str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm + ":" + ss + " " + ww + colorfoot; return (str); } function tick() { var today; today = new Date(); document.getElementById("localtime").innerHTML = showLocale(today); window.setTimeout("tick()", 1000); } tick(); </script> <br /> <h1 align="center">题目选择</h1> <!-- 传递参数的值给NewFile.jsp处理 --> <form action="NewFile.jsp" method="post"> <!-- 互斥选择,值只能有一个 --> <center> <br>请选择上限(分数无上下限): <select name="TopNumber"> <option value=20>20</option> <option value=30>30</option> <option value=50>50</option> <option value=80>80</option> <option value=100>100</option> </select><br> <br>请选择下限(分数无上下限): <select name="BaseNumber"> <option value=0>0</option> <option value=1>1</option> <option value=3>3</option> <option value=5>5</option> <option value=10>10</option> <br> </select><br> <br>请输入题目个数:<input type="text" name="timugeshu"><br><br> <br>请在以下选项中打勾: <br> <tr>请选择需要的题型是否有乘除法: <input type="radio" name="xuanze" value=1>有 <input type="radio" name="xuanze" value=2>无 </tr> <br> <tr>请选择需要的题型是否有余数: <input type="radio" name="xuanze1" value=3>有 <input type="radio" name="xuanze1" value=4>无 </tr> <br> <tr>请选择需要的题型是否有括号: <input type="radio" name="xuanze2" value=5>有 <input type="radio" name="xuanze2" value=6>无</tr> <br> <br> <input type="submit" name="submit" value="提交"> </center> </form> </body> </html> NewFeil.jsp <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="szys.*"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>四则运算</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body background="images/A2.jpg"> <% int timugeshu=Integer.parseInt((String)request.getParameter("timugeshu")); String[] s=new String[timugeshu]; float k[]=new float[timugeshu]; math a=new math(); request.setCharacterEncoding("UTF-8"); String xuanze = request.getParameter("xuanze"); String xuanze1 = request.getParameter("xuanze1"); String xuanze2 = request.getParameter("xuanze2"); String TopNumber,BaseNumber; TopNumber=request.getParameter("TopNumber"); BaseNumber=request.getParameter("BaseNumber"); a.SetValue(a.StrToNum(TopNumber),a.StrToNum(BaseNumber)); if(xuanze!=null&&xuanze1!=null&&xuanze2!=null){ if(xuanze.equals("1")) { a.SetValue1(1); } if(xuanze1.equals("3")) { a.SetValue2(1); } if(xuanze2.equals("5")) { a.SetValue3(1); } } %> <form action="Myjsp.jsp" method="post"> <center> <% a.main(timugeshu); float temp; String[] se=new String[timugeshu]; for(int i=0;i<timugeshu;i++){ s[i]=a.cs(i); se[i]="name"; se[i]=se[i]+i; k[i]=a.css(i); out.print("<tr>"+s[i]+"</tr>"); out.println("<br>"); %> <input type="text" name=<%=se[i] %>><br> <% } session.setAttribute( "v1",s); session.setAttribute( "v2",k); %> <br><input type="submit" name="submit" value="提交"> <% %> </center> </form> </body> </html> MyFeil.jsp <%@ page language="java" import="java.util.*" pageEncoding="gb2312" contentType="text/html; charset=gb2312"%> <%@ page import="szys.*"%> <%String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>答题情况</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body background="images/A3.jpg"> <% request.setCharacterEncoding("UTF-8");%> <center> <% int timugeshu=Integer.parseInt((String)request.getParameter("timugeshu")); String[] name=new String[timugeshu]; String[] sum=new String[timugeshu]; math a=new math(); float sum1[]=new float[timugeshu]; int right=0; String[] s= (String[])session.getAttribute("v1"); float[] k=(float[])session.getAttribute("v2"); for(int i=0;i<timugeshu;i++) sum1[i]=0; for(int i=0;i<timugeshu;i++){ name[i]="name"; name[i]=name[i]+i; sum[i]=request.getParameter(name[i]); out.print(s[i]); out.print("<tr>"+sum[i]+"</tr>"); if(sum[i]=="") out.print("未回答!"); else{ if(((int)(k[i]*100))%100!=0) sum1[i]=a.StrToFloat1(sum[i]); else sum1[i]=a.StrToFloat(sum[i]); if(Math.abs(sum1[i]*100-k[i]*100)<4){ out.print(" 回答正确!"); right++; } else out.print(" 回答错误,正确答案是: "+k[i]); } out.println("<br>"); } out.print("总共做对 "+right+" 题,正确率为:"+(right/timugeshu)*100+" %"); %><br> <input type="button" value="返回首页" onClick="window.location.href=‘MyHtml.html‘"> <input type="button" value="关闭" onClick="window.close();"> </center> </body> </html> Math.java package math; import java.util.*; public class math{ static int MAX=100; static String formula = ""; //当前算式 static String[] buffer = new String[MAX]; //缓冲区数组 static int TopNumber; //上限 static int BaseNumber; //下限 static int chengchu; //是否有乘除 static int Amount; //操作数的个数 static int BracketNum; //括号个数 static int[] LBraket = new int[2]; //左括号的位置 static int[] RBraket = new int[2]; //右括号的位置 static int IsNeg; //是否有负数 static int IsRem; //是否有余数 static int IsBra; //是否有括号 static int IsRep; //是否重复 static float[] Result= new float[MAX]; //正确结果数组 static char lastOp; //记录上个运算符是否为除号 //优先级数组 static char[][] prior = { { ‘>‘, ‘>‘, ‘<‘, ‘<‘, ‘<‘, ‘>‘, ‘>‘ }, { ‘>‘, ‘>‘, ‘<‘, ‘<‘, ‘<‘, ‘>‘, ‘>‘ }, { ‘>‘, ‘>‘, ‘>‘, ‘>‘, ‘<‘, ‘>‘, ‘>‘ }, { ‘>‘, ‘>‘, ‘>‘, ‘>‘, ‘<‘, ‘>‘, ‘>‘ }, { ‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘=‘, ‘ ‘ }, { ‘>‘, ‘>‘, ‘>‘, ‘>‘, ‘ ‘, ‘>‘, ‘>‘ }, { ‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘<‘, ‘ ‘, ‘=‘ } }; //将操作符转化为优先级数组的下标 static int Change(String Oper) { switch (Oper.charAt(0)) { case ‘+‘: return 0; case ‘-‘: return 1; case ‘*‘: return 2; case ‘/‘: return 3; case ‘(‘: return 4; case ‘)‘: return 5; case ‘=‘: return 6; default: return 6; } } static int Change(char Oper) { switch (Oper) { case ‘+‘: return 0; case ‘-‘: return 1; case ‘*‘: return 2; case ‘/‘: return 3; case ‘(‘: return 4; case ‘)‘: return 5; case ‘=‘: return 6; default: return 6; } } static //返回优先级的大小 char Precede(char Oper, char ch) { return prior[Change(Oper)][Change(ch)]; } //计算两个数的结果 static float Operate(float first, char oper1, float second) { switch (oper1) { case ‘+‘: { return (first + second); } case ‘-‘: { if (first-second<0) { IsRep = 1; return 0; } return (first - second); } case ‘*‘: { return (first * second); } case ‘/‘: { if (second == 0) { IsRep = 1; return 0; } return (first / second); } default: return 0; } } //数字的个数 static void NumberAmount() { Amount = 2 + (int)((Math.random())*3); } //加左括号 随机选择在第几个数字前面加括号 static void AddLbracket(){ for (int j = 0; j < 2; j++) LBraket[j] = 0; if (Amount == 2) { BracketNum = 0; } if (Amount == 3){ BracketNum = (int)((Math.random())*2); } if (Amount > 3) { BracketNum = (int)((Math.random())*3); } for (int i = 0; i < BracketNum; i++){ LBraket[i] = 1 + (int)((Math.random())*(Amount - 2)); } } //加右括号 static void AddRbracket(){ for (int j = 0; j < 2; j++) RBraket[j] = 0; int choose; int trance; if (BracketNum == 1){ RBraket[0] = LBraket[0] + 1 +(int)((Math.random())* (Amount - LBraket[0])); } if (BracketNum == 2) { //把最左边的左括号放在第一个数组中 if (LBraket[0] < LBraket[1]) { trance = LBraket[0]; LBraket[0] = LBraket[1]; LBraket[1] = trance; } //当两个左括号之间相差有点远时有2中右括号添加方法 if (LBraket[0] - LBraket[1]>2){ choose = (int)((Math.random())*2); if (choose == 0){ RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); RBraket[1] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); } if (choose == 1) { RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); RBraket[1] = LBraket[1] + 1 + (int)((Math.random())*(LBraket[0] - 2)); } } else { RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); RBraket[1] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); if (LBraket[0] == LBraket[1] && RBraket[0] == RBraket[1]){ LBraket[0] = LBraket[1] = 0; RBraket[0] = RBraket[1] = 0; BracketNum = 0; } if (LBraket[1] == 1 && (RBraket[0] == Amount || RBraket[1] == Amount)) { LBraket[0] = LBraket[1] = 0; RBraket[0] = RBraket[1] = 0; BracketNum = 0; } } } } //随机产生最简真分数 static void Score(){ int Left, Right; Left = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1)); Right = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1)); while (Left >= Right || Left == 0) { Left = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1)); Right = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1)); } int max = 1; //求最大公约数 for (int i = 2; i <= Left; i++) { if (Left%i == 0 && Right%i == 0) { max = i; } } if (max > 1) { Left /= max; Right /= max; } formula += "(" + Left + "/" + Right + ")"; } //随机生成操作符 static void Operater() { int choose; char op=‘+‘; if (chengchu== 1) choose = 1 + (int)((Math.random())* 4); else choose = 1 + (int)((Math.random())*2); switch (choose) { case 1:{op = ‘+‘; lastOp = ‘+‘; break; } case 2:{op = ‘-‘; lastOp = ‘-‘; break; } case 3:{op = ‘*‘; lastOp = ‘*‘; break; } case 4: { //防止连续除法产生运算误区 op = ‘/‘; if (lastOp == ‘/‘) IsRep = 1; else lastOp = ‘/‘; break; } } formula += op; } //随机生成整数 static void Integer(){ int num; num = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1)); formula += num; } //创建算式 static void CreateNumber(){ for (int k = 1; k <= Amount; k++) { for (int i = 0; i < 2; i++){ if (LBraket[i] == k) formula += "("; } int cho; cho = (int)((Math.random())*2); if (cho == 0) { Integer(); } else Score(); for (int j = 0; j < 2; j++){ if ((RBraket[j] == k) && RBraket[j] != 0) formula += ")"; } if (k == Amount) formula += "="; else Operater(); } } //检查是否重复及判断括号是否添加正确 static int Repeat(int time){ buffer[time] = formula; int juege = 0; int trance; for (int i = 0; i < time; i++) { if (buffer[i] == buffer[time]) { juege = 1; break; } } if (IsBra != 1) { if (BracketNum == 1) { if (LBraket[0] == 1 && RBraket[0] == Amount) juege = 1; } if (BracketNum == 2) { if (RBraket[0] < RBraket[1]) { trance = RBraket[0]; RBraket[0] = RBraket[1]; RBraket[1] = trance; } if (LBraket[1] == 1 && RBraket[0] == Amount&&LBraket[0] < RBraket[1]) juege = 1; } } return juege; } static float EvaluateExpression(){ float[] OPND=new float[MAX]; char[] OPTR =new char[MAX]; int Ntop=0; int Ctop=0; OPTR[Ctop++]=‘=‘; int count = 0; float Num = 0, first, second; char oper1; char bracket1; while (true) { Num = 0; //读取数字 while (formula.charAt(count) >= ‘0‘&&formula.charAt(count) <= ‘9‘) { if (formula.charAt(count) == ‘0‘) { if (count == 0) OPND[Ntop++]=0; if (count != 0 && !(formula.charAt(count-1) >= ‘0‘&&formula.charAt(count-1) <= ‘9‘)) OPND[Ntop++]=0; } Num = Num * 10; Num = Num + formula.charAt(count) - 48; count++; } if (Num > 0) { OPND[Ntop++]=Num; } if (formula.charAt(count) == ‘=‘&&OPTR[Ctop-1] == ‘=‘) { break; } //判断运算符优先级 switch (Precede(OPTR[Ctop-1], formula.charAt(count))) { case ‘<‘: { OPTR[Ctop++]=formula.charAt(count); count++; break; } case ‘>‘: { oper1=OPTR[--Ctop]; second=OPND[--Ntop]; first=OPND[--Ntop]; OPND[Ntop++]=Operate(first, oper1, second); break; } case ‘=‘: { bracket1=OPTR[--Ctop]; count++; } } } return OPND[Ntop-1]; } public static void SetValue(int IsMulDlvExist1,int IsRem1,int IsBra1,int TopNumber1,int BaseNumber1) { chengchu=0; BaseNumber=0; TopNumber=10; IsNeg=0; IsRem=0; IsBra=1; chengchu=IsMulDlvExist1; TopNumber=TopNumber1; BaseNumber=BaseNumber1; IsRem=IsRem1; IsBra=IsBra1; } public static void SetValue1(int IMDE) { chengchu=IMDE; } public static void SetValue2(int Rem) { IsRem=Rem; } public static void SetValue3(int Bra) { IsBra=Bra; } public static void SetValue(int TopNumber1,int BaseNumber1){ chengchu=0; BaseNumber=0; TopNumber=10; IsNeg=0; IsRem=0; IsBra=1; TopNumber=TopNumber1; BaseNumber=BaseNumber1; } public static int StrToNum(String x) { int num=0; for(int i=0;i<x.length();i++) { num=num*10+x.charAt(i)-48; } return num; } public static float StrToFloat(String x) { float num=0; int doat=0; int mi=-1; for(int i=0;i<x.length();i++) { if(x.charAt(i)==‘.‘) { doat=1; continue; } if(doat==1) { num=(float) (num+(x.charAt(i)-48)*Math.pow(10,mi)); mi--; }else{ num=num*10+x.charAt(i)-48; } } return num; } public static float StrToFloat1(String x) { float first=0; float second=0; int point=0; for(int i=0;i<x.length();i++) { if(x.charAt(i)==‘/‘) { point=1; continue; } if(point==1) { second=second*10+x.charAt(i)-48; }else { first=first*10+x.charAt(i)-48; } } return (first/second); } static int ma() { int OutChoose = 0; int truenum = 0; int choose; boolean flag = true; int range = 0; for (int i = 0; i < 30; i++) { lastOp = ‘+‘; IsRep = 0; NumberAmount(); if (IsBra == 0) { AddLbracket(); AddRbracket(); } CreateNumber(); Result[i] = EvaluateExpression(); while (Repeat(i) == 1 || IsRep == 1 || (IsNeg == 1 && (Result[i] < 0 || Result[i] == -0)) || (IsRem == 1 && ((int)(Result[i]*10000))%10000!=0)) { IsRep = 0; lastOp = ‘+‘; formula=""; NumberAmount(); if (IsBra == 0) { AddLbracket(); AddRbracket(); } CreateNumber(); Result[i] = EvaluateExpression(); } buffer[i]=formula; formula=""; } return 0; } public static float css(int number){ return Result[number]; } public static String cs(int number){ return buffer[number]; } public static void main() { int a; a=ma(); for (int i = 0; i < 30; i++) { System.out.println(buffer[i]+Result[i]); } } }
时间: 2024-10-22 04:11:07