软件工程结对作业02(借鉴版)
小组成员:谷伟华、牛俊燕
在编写程序的时候因为不会将Java程序嵌入进jsp中所以借鉴了一个学姐的程序,下面是学姐的程序。但在对学姐程序就行修改时发现了一些问题:
在Newjsp中能调用html中的题目个数的参数
在Myjsp中不能调用html中的题目个数的参数
其中这个参数timugeshu,是我自己后来添加的,用来改变题目个数的,与Java程序无关,只在jsp文件中作用
源程序:
NewFeil.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>四则运算</title> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 <meta name="keywords" content="keyword1,keyword2,keyword3"> 7 <meta name="description" content="this is my page"> 8 9 10 <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> 11 12 </head> 13 <body background="images/A1.jpg"> 14 <div style="width: 100%; font-family: 微软雅黑; text-align: center"> 15 <marquee scrollamount=3 style="COLOR=000000 FILTER: shadow(color=000000 ); 16 FONT-FAMILY: 隶书; FONT-SIZE: 25pt; WIDTH: 100%"> 17 欢迎来到四则运算乐园</marquee></div> 18 <span id="localtime"></span> 19 <script type="text/javascript"> 20 function showLocale(objD) { 21 var str, colorhead, colorfoot; 22 var yy = objD.getYear(); 23 if (yy < 1900) 24 yy = yy + 1900; 25 var MM = objD.getMonth() + 1; 26 if (MM < 10) 27 MM = '0' + MM; 28 var dd = objD.getDate(); 29 if (dd < 10) 30 dd = '0' + dd; 31 var hh = objD.getHours(); 32 if (hh < 10) 33 hh = '0' + hh; 34 var mm = objD.getMinutes(); 35 if (mm < 10) 36 mm = '0' + mm; 37 var ss = objD.getSeconds(); 38 if (ss < 10) 39 ss = '0' + ss; 40 var ww = objD.getDay(); 41 if (ww == 0) 42 colorhead = "<font color=\"#000000\">"; 43 if (ww > 0 && ww < 6) 44 colorhead = "<font color=\"#000000\">"; 45 if (ww == 6) 46 colorhead = "<font color=\"#000000\">"; 47 if (ww == 0) 48 ww = "星期日"; 49 if (ww == 1) 50 ww = "星期一"; 51 if (ww == 2) 52 ww = "星期二"; 53 if (ww == 3) 54 ww = "星期三"; 55 if (ww == 4) 56 ww = "星期四"; 57 if (ww == 5) 58 ww = "星期五"; 59 if (ww == 6) 60 ww = "星期六"; 61 colorfoot = "</font>" 62 str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm 63 + ":" + ss + " " + ww + colorfoot; 64 return (str); 65 } 66 function tick() { 67 var today; 68 today = new Date(); 69 document.getElementById("localtime").innerHTML = showLocale(today); 70 window.setTimeout("tick()", 1000); 71 } 72 tick(); 73 </script> 74 <br /> 75 <h1 align="center">题目选择</h1> 76 <!-- 传递参数的值给NewFile.jsp处理 --> 77 <form action="NewFile.jsp" method="post"> 78 <!-- 互斥选择,值只能有一个 --> 79 <center> 80 <br>请选择上限(分数无上下限): 81 <select name="TopNumber"> 82 <option value=20>20</option> 83 <option value=30>30</option> 84 <option value=50>50</option> 85 <option value=80>80</option> 86 <option value=100>100</option> 87 </select><br> 88 <br>请选择下限(分数无上下限): 89 <select name="BaseNumber"> 90 <option value=0>0</option> 91 <option value=1>1</option> 92 <option value=3>3</option> 93 <option value=5>5</option> 94 <option value=10>10</option> 95 <br> 96 </select><br> 97 <br>请输入题目个数:<input type="text" name="timugeshu"><br><br> 98 <br>请在以下选项中打勾: 99 <br> 100 <tr>请选择需要的题型是否有乘除法: 101 <input type="radio" name="xuanze" value=1>有 102 <input type="radio" name="xuanze" value=2>无 103 </tr> 104 <br> 105 <tr>请选择需要的题型是否有余数: 106 <input type="radio" name="xuanze1" value=3>有 107 <input type="radio" name="xuanze1" value=4>无 108 </tr> 109 <br> 110 <tr>请选择需要的题型是否有括号: 111 <input type="radio" name="xuanze2" value=5>有 112 <input type="radio" name="xuanze2" value=6>无</tr> 113 <br> 114 <br> 115 116 <input type="submit" name="submit" value="提交"> 117 </center> 118 </form> 119 </body> 120 </html>
NewFeil.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ page import="szys.*"%> 4 <% 5 String path = request.getContextPath(); 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 7 %> 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 <title>四则运算</title> 13 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 </head> 23 <body background="images/A2.jpg"> 24 <% 25 int timugeshu=Integer.parseInt((String)request.getParameter("timugeshu")); 26 String[] s=new String[timugeshu]; 27 float k[]=new float[timugeshu]; 28 math a=new math(); 29 request.setCharacterEncoding("UTF-8"); 30 String xuanze = request.getParameter("xuanze"); 31 String xuanze1 = request.getParameter("xuanze1"); 32 String xuanze2 = request.getParameter("xuanze2"); 33 String TopNumber,BaseNumber; 34 TopNumber=request.getParameter("TopNumber"); 35 BaseNumber=request.getParameter("BaseNumber"); 36 a.SetValue(a.StrToNum(TopNumber),a.StrToNum(BaseNumber)); 37 if(xuanze!=null&&xuanze1!=null&&xuanze2!=null){ 38 if(xuanze.equals("1")) 39 { 40 a.SetValue1(1); 41 } 42 if(xuanze1.equals("3")) 43 { 44 a.SetValue2(1); 45 } 46 if(xuanze2.equals("5")) 47 { 48 a.SetValue3(1); 49 } 50 } 51 %> 52 <form action="Myjsp.jsp" method="post"> 53 <center> 54 <% 55 a.main(timugeshu); 56 float temp; 57 String[] se=new String[timugeshu]; 58 for(int i=0;i<timugeshu;i++){ 59 s[i]=a.cs(i); 60 se[i]="name"; 61 se[i]=se[i]+i; 62 k[i]=a.css(i); 63 out.print("<tr>"+s[i]+"</tr>"); 64 out.println("<br>"); 65 66 67 %> 68 <input type="text" name=<%=se[i] %>><br> 69 <% 70 } 71 session.setAttribute( "v1",s); 72 session.setAttribute( "v2",k); 73 %> 74 <br><input type="submit" name="submit" value="提交"> 75 <% %> 76 </center> 77 </form> 78 79 80 81 82 </body> 83 </html>
MyFeil.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="gb2312" contentType="text/html; charset=gb2312"%> 2 <%@ page import="szys.*"%> 3 <%String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 6 <html> 7 <head> 8 <base href="<%=basePath%>"> 9 <title>答题情况</title> 10 <meta http-equiv="pragma" content="no-cache"> 11 <meta http-equiv="cache-control" content="no-cache"> 12 <meta http-equiv="expires" content="0"> 13 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 14 <meta http-equiv="description" content="This is my page"> 15 <!-- 16 <link rel="stylesheet" type="text/css" href="styles.css"> 17 --> 18 </head> 19 20 <body background="images/A3.jpg"> 21 <% request.setCharacterEncoding("UTF-8");%> 22 <center> 23 <% 24 int timugeshu=Integer.parseInt((String)request.getParameter("timugeshu")); 25 String[] name=new String[timugeshu]; 26 String[] sum=new String[timugeshu]; 27 math a=new math(); 28 float sum1[]=new float[timugeshu]; 29 int right=0; 30 String[] s= (String[])session.getAttribute("v1"); 31 float[] k=(float[])session.getAttribute("v2"); 32 33 for(int i=0;i<timugeshu;i++) 34 sum1[i]=0; 35 for(int i=0;i<timugeshu;i++){ 36 name[i]="name"; 37 name[i]=name[i]+i; 38 sum[i]=request.getParameter(name[i]); 39 out.print(s[i]); 40 out.print("<tr>"+sum[i]+"</tr>"); 41 if(sum[i]=="") 42 out.print("未回答!"); 43 else{ 44 if(((int)(k[i]*100))%100!=0) 45 sum1[i]=a.StrToFloat1(sum[i]); 46 else 47 sum1[i]=a.StrToFloat(sum[i]); 48 if(Math.abs(sum1[i]*100-k[i]*100)<4){ 49 out.print(" 回答正确!"); 50 right++; 51 } 52 else 53 out.print(" 回答错误,正确答案是: "+k[i]); 54 } 55 56 out.println("<br>"); 57 } 58 out.print("总共做对 "+right+" 题,正确率为:"+(right/timugeshu)*100+" %"); 59 %><br> 60 <input type="button" value="返回首页" onClick="window.location.href='MyHtml.html'"> 61 <input type="button" value="关闭" onClick="window.close();"> 62 </center> 63 </body> 64 </html>
Math.java
1 package math; 2 import java.util.*; 3 public class math{ 4 static int MAX=100; 5 static String formula = ""; //当前算式 6 static String[] buffer = new String[MAX]; //缓冲区数组 7 static int TopNumber; //上限 8 static int BaseNumber; //下限 9 static int chengchu; //是否有乘除 10 static int Amount; //操作数的个数 11 static int BracketNum; //括号个数 12 static int[] LBraket = new int[2]; //左括号的位置 13 static int[] RBraket = new int[2]; //右括号的位置 14 static int IsNeg; //是否有负数 15 static int IsRem; //是否有余数 16 static int IsBra; //是否有括号 17 static int IsRep; //是否重复 18 static float[] Result= new float[MAX]; //正确结果数组 19 static char lastOp; //记录上个运算符是否为除号 20 21 //优先级数组 22 static char[][] prior = { 23 { '>', '>', '<', '<', '<', '>', '>' }, 24 { '>', '>', '<', '<', '<', '>', '>' }, 25 { '>', '>', '>', '>', '<', '>', '>' }, 26 { '>', '>', '>', '>', '<', '>', '>' }, 27 { '<', '<', '<', '<', '<', '=', ' ' }, 28 { '>', '>', '>', '>', ' ', '>', '>' }, 29 { '<', '<', '<', '<', '<', ' ', '=' } 30 }; 31 32 //将操作符转化为优先级数组的下标 33 static int Change(String Oper) 34 { 35 switch (Oper.charAt(0)) 36 { 37 case '+': return 0; 38 case '-': return 1; 39 case '*': return 2; 40 case '/': return 3; 41 case '(': return 4; 42 case ')': return 5; 43 case '=': return 6; 44 default: return 6; 45 } 46 } 47 static int Change(char Oper) 48 { 49 switch (Oper) 50 { 51 case '+': return 0; 52 case '-': return 1; 53 case '*': return 2; 54 case '/': return 3; 55 case '(': return 4; 56 case ')': return 5; 57 case '=': return 6; 58 default: return 6; 59 } 60 } 61 static //返回优先级的大小 62 char Precede(char Oper, char ch) 63 { 64 return prior[Change(Oper)][Change(ch)]; 65 } 66 67 //计算两个数的结果 68 static float Operate(float first, char oper1, float second) 69 { 70 switch (oper1) 71 { 72 case '+': 73 { 74 return (first + second); 75 } 76 case '-': 77 { 78 if (first-second<0) 79 { 80 IsRep = 1; 81 return 0; 82 } 83 return (first - second); 84 } 85 case '*': 86 { 87 return (first * second); 88 } 89 case '/': 90 { 91 if (second == 0) 92 { 93 IsRep = 1; 94 return 0; 95 } 96 return (first / second); 97 } 98 default: return 0; 99 } 100 } 101 102 //数字的个数 103 static void NumberAmount() 104 { 105 Amount = 2 + (int)((Math.random())*3); 106 } 107 108 //加左括号 随机选择在第几个数字前面加括号 109 static void AddLbracket(){ 110 for (int j = 0; j < 2; j++) 111 LBraket[j] = 0; 112 if (Amount == 2) 113 { 114 BracketNum = 0; 115 } 116 if (Amount == 3){ 117 BracketNum = (int)((Math.random())*2); 118 } 119 if (Amount > 3) 120 { 121 BracketNum = (int)((Math.random())*3); 122 } 123 for (int i = 0; i < BracketNum; i++){ 124 LBraket[i] = 1 + (int)((Math.random())*(Amount - 2)); 125 } 126 } 127 //加右括号 128 static void AddRbracket(){ 129 for (int j = 0; j < 2; j++) 130 RBraket[j] = 0; 131 int choose; 132 int trance; 133 if (BracketNum == 1){ 134 RBraket[0] = LBraket[0] + 1 +(int)((Math.random())* (Amount - LBraket[0])); 135 } 136 if (BracketNum == 2) 137 138 { 139 //把最左边的左括号放在第一个数组中 140 if (LBraket[0] < LBraket[1]) 141 { 142 trance = LBraket[0]; 143 LBraket[0] = LBraket[1]; 144 LBraket[1] = trance; 145 } 146 //当两个左括号之间相差有点远时有2中右括号添加方法 147 if (LBraket[0] - LBraket[1]>2){ 148 choose = (int)((Math.random())*2); 149 if (choose == 0){ 150 RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); 151 RBraket[1] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); 152 } 153 if (choose == 1) 154 { 155 RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); 156 RBraket[1] = LBraket[1] + 1 + (int)((Math.random())*(LBraket[0] - 2)); 157 } 158 } 159 else 160 { 161 RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); 162 RBraket[1] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); 163 if (LBraket[0] == LBraket[1] && RBraket[0] == RBraket[1]){ 164 LBraket[0] = LBraket[1] = 0; 165 RBraket[0] = RBraket[1] = 0; 166 BracketNum = 0; 167 } 168 if (LBraket[1] == 1 && (RBraket[0] == Amount || RBraket[1] == Amount)) 169 { 170 LBraket[0] = LBraket[1] = 0; 171 RBraket[0] = RBraket[1] = 0; 172 BracketNum = 0; 173 } 174 175 } 176 } 177 } 178 179 //随机产生最简真分数 180 static void Score(){ 181 int Left, Right; 182 Left = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1)); 183 Right = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1)); 184 while (Left >= Right || Left == 0) 185 { 186 Left = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1)); 187 Right = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1)); 188 } 189 int max = 1; 190 //求最大公约数 191 for (int i = 2; i <= Left; i++) 192 { 193 if (Left%i == 0 && Right%i == 0) 194 { 195 max = i; 196 } 197 } 198 if (max > 1) 199 { 200 Left /= max; 201 Right /= max; 202 } 203 formula += "(" + Left + "/" + Right + ")"; 204 } 205 206 //随机生成操作符 207 static void Operater() 208 { 209 int choose; 210 char op='+'; 211 if (chengchu== 1) 212 choose = 1 + (int)((Math.random())* 4); 213 else 214 choose = 1 + (int)((Math.random())*2); 215 216 switch (choose) 217 { 218 case 1:{op = '+'; lastOp = '+'; break; } 219 case 2:{op = '-'; lastOp = '-'; break; } 220 case 3:{op = '*'; lastOp = '*'; break; } 221 case 4: 222 { 223 //防止连续除法产生运算误区 224 op = '/'; 225 if (lastOp == '/') 226 IsRep = 1; 227 else 228 lastOp = '/'; 229 break; 230 } 231 } 232 formula += op; 233 } 234 235 //随机生成整数 236 static void Integer(){ 237 int num; 238 num = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1)); 239 formula += num; 240 } 241 //创建算式 242 static void CreateNumber(){ 243 for (int k = 1; k <= Amount; k++) 244 { 245 246 for (int i = 0; i < 2; i++){ 247 if (LBraket[i] == k) 248 formula += "("; 249 } 250 int cho; 251 cho = (int)((Math.random())*2); 252 if (cho == 0) 253 { 254 Integer(); 255 } 256 else 257 Score(); 258 for (int j = 0; j < 2; j++){ 259 if ((RBraket[j] == k) && RBraket[j] != 0) 260 formula += ")"; 261 } 262 if (k == Amount) 263 formula += "="; 264 else 265 Operater(); 266 } 267 } 268 //检查是否重复及判断括号是否添加正确 269 static int Repeat(int time){ 270 buffer[time] = formula; 271 int juege = 0; 272 int trance; 273 for (int i = 0; i < time; i++) 274 { 275 if (buffer[i] == buffer[time]) 276 { 277 juege = 1; 278 break; 279 } 280 } 281 if (IsBra != 1) 282 { 283 if (BracketNum == 1) 284 { 285 if (LBraket[0] == 1 && RBraket[0] == Amount) 286 juege = 1; 287 } 288 if (BracketNum == 2) 289 { 290 if (RBraket[0] < RBraket[1]) 291 { 292 trance = RBraket[0]; 293 RBraket[0] = RBraket[1]; 294 RBraket[1] = trance; 295 } 296 if (LBraket[1] == 1 && RBraket[0] == Amount&&LBraket[0] < RBraket[1]) 297 juege = 1; 298 } 299 } 300 return juege; 301 } 302 static float EvaluateExpression(){ 303 float[] OPND=new float[MAX]; 304 char[] OPTR =new char[MAX]; 305 int Ntop=0; 306 int Ctop=0; 307 OPTR[Ctop++]='='; 308 int count = 0; 309 float Num = 0, first, second; 310 char oper1; 311 char bracket1; 312 while (true) 313 { 314 Num = 0; 315 //读取数字 316 while (formula.charAt(count) >= '0'&&formula.charAt(count) <= '9') 317 { 318 if (formula.charAt(count) == '0') 319 { 320 if (count == 0) 321 OPND[Ntop++]=0; 322 if (count != 0 && !(formula.charAt(count-1) >= '0'&&formula.charAt(count-1) <= '9')) 323 OPND[Ntop++]=0; 324 } 325 Num = Num * 10; 326 Num = Num + formula.charAt(count) - 48; 327 count++; 328 } 329 if (Num > 0) 330 { 331 OPND[Ntop++]=Num; 332 } 333 if (formula.charAt(count) == '='&&OPTR[Ctop-1] == '=') 334 { 335 break; 336 } 337 //判断运算符优先级 338 switch (Precede(OPTR[Ctop-1], formula.charAt(count))) 339 { 340 case '<': 341 { 342 OPTR[Ctop++]=formula.charAt(count); 343 count++; 344 break; 345 } 346 case '>': 347 { 348 oper1=OPTR[--Ctop]; 349 second=OPND[--Ntop]; 350 first=OPND[--Ntop]; 351 OPND[Ntop++]=Operate(first, oper1, second); 352 break; 353 } 354 case '=': 355 { 356 bracket1=OPTR[--Ctop]; 357 count++; 358 } 359 } 360 } 361 return OPND[Ntop-1]; 362 } 363 364 public static void SetValue(int IsMulDlvExist1,int IsRem1,int IsBra1,int TopNumber1,int BaseNumber1) 365 { 366 chengchu=0; 367 BaseNumber=0; 368 TopNumber=10; 369 IsNeg=0; 370 IsRem=0; 371 IsBra=1; 372 373 chengchu=IsMulDlvExist1; 374 TopNumber=TopNumber1; 375 BaseNumber=BaseNumber1; 376 IsRem=IsRem1; 377 IsBra=IsBra1; 378 } 379 public static void SetValue1(int IMDE) 380 { 381 chengchu=IMDE; 382 } 383 public static void SetValue2(int Rem) 384 { 385 IsRem=Rem; 386 } 387 public static void SetValue3(int Bra) 388 { 389 IsBra=Bra; 390 } 391 public static void SetValue(int TopNumber1,int BaseNumber1){ 392 chengchu=0; 393 BaseNumber=0; 394 TopNumber=10; 395 IsNeg=0; 396 IsRem=0; 397 IsBra=1; 398 TopNumber=TopNumber1; 399 BaseNumber=BaseNumber1; 400 401 } 402 403 public static int StrToNum(String x) 404 { 405 int num=0; 406 for(int i=0;i<x.length();i++) 407 { 408 num=num*10+x.charAt(i)-48; 409 } 410 return num; 411 } 412 public static float StrToFloat(String x) 413 { 414 float num=0; 415 int doat=0; 416 int mi=-1; 417 for(int i=0;i<x.length();i++) 418 { 419 if(x.charAt(i)=='.') 420 { 421 doat=1; 422 continue; 423 } 424 if(doat==1) 425 { 426 num=(float) (num+(x.charAt(i)-48)*Math.pow(10,mi)); 427 mi--; 428 }else{ 429 num=num*10+x.charAt(i)-48; 430 } 431 } 432 return num; 433 434 } 435 public static float StrToFloat1(String x) 436 { 437 float first=0; 438 float second=0; 439 int point=0; 440 for(int i=0;i<x.length();i++) 441 { 442 if(x.charAt(i)=='/') 443 { 444 point=1; 445 continue; 446 } 447 if(point==1) 448 { 449 second=second*10+x.charAt(i)-48; 450 }else 451 { 452 first=first*10+x.charAt(i)-48; 453 } 454 } 455 return (first/second); 456 } 457 static int ma() 458 { 459 int OutChoose = 0; 460 int truenum = 0; 461 int choose; 462 boolean flag = true; 463 int range = 0; 464 for (int i = 0; i < 30; i++) 465 { 466 lastOp = '+'; 467 IsRep = 0; 468 NumberAmount(); 469 if (IsBra == 0) 470 { 471 AddLbracket(); 472 AddRbracket(); 473 } 474 CreateNumber(); 475 Result[i] = EvaluateExpression(); 476 while (Repeat(i) == 1 || IsRep == 1 || (IsNeg == 1 && (Result[i] < 0 || Result[i] == -0)) || (IsRem == 1 && ((int)(Result[i]*10000))%10000!=0)) 477 { 478 479 IsRep = 0; 480 lastOp = '+'; 481 formula=""; 482 NumberAmount(); 483 if (IsBra == 0) 484 { 485 AddLbracket(); 486 AddRbracket(); 487 } 488 CreateNumber(); 489 Result[i] = EvaluateExpression(); 490 } 491 buffer[i]=formula; 492 formula=""; 493 } 494 495 return 0; 496 } 497 public static float css(int number){ 498 return Result[number]; 499 } 500 public static String cs(int number){ 501 return buffer[number]; 502 } 503 public static void main() 504 { 505 int a; 506 a=ma(); 507 for (int i = 0; i < 30; i++) 508 { 509 System.out.println(buffer[i]+Result[i]); 510 } 511 512 } 513 }