软件工程结对作业02
设计思想:
将原有的Java程序转化为网页版,可以设计一个html将用户的选择传递下去,在接下来的页面中使用request接收,然后设置几个数组分别传递算式、答案、用户的回答,使用session进行传递数组,在下一页中设置数组接收数据并用for循环和if语句就可以实现答案的校验。这样就三页就完成了。
代码:
new.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <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"> <title>首页</title> </head> <body> <center> <h1 align="center">四则运算</h1> <!-- 传递参数的值给index.jsp处理 --> <form action="index.jsp" method="post"> <br>请输入范围: <input type ="text" name ="range"/> <br> <br>请输入题数: <input type ="text" name ="row"/> <br> <br>请选择功能: <select name="function"> <option value=1>整数加减法</option> <option value=2>整数四则运算</option> <option value=5>多数的四则运算(带括号)</option> </select> <br> <br>请选择子功能: <select name="subfunction"> <option value=1>无负数</option> <option value=2>有负数</option> <option value=3>无余数</option> <option value=4>有余数</option> </select> <br> <input type="submit" name="submit" value="确定"> </form> </center> </body> </html>
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@page import= "test.*" %> <%@page import= "java.util.*" %> <!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> <body> <center> <h1>四则运算</h1> <form action="answer.jsp"method="post"> <% request.setCharacterEncoding("UTF-8"); String f = request.getParameter("function"); String subf = request.getParameter("subfunction"); String ran =request.getParameter("range"); String r=request.getParameter("row"); Operate oper=new Operate(); int h=Integer.parseInt(r); int g=Integer.parseInt(ran); String[] s1=new String[h]; String[] se=new String[h]; int[] answer=new int[h]; Random random = new Random(); char arr[] = {'+','-','*','/','('}; for(int i=0;i<h;i++) { se[i]="se"; se[i]=se[i]+i; if(f.equals("1")&&subf.equals("1")) { String str1=random.nextInt(g)+""+arr[random.nextInt(2)]+""+random.nextInt(g); s1[i]=str1; int t1 = oper.caculate(str1+'#'); answer[i]=t1; out.println(str1+'='); } else if(f.equals("1")&&subf.equals("2")) { int c1=random.nextInt(g); int c2=random.nextInt(g); char w1=arr[random.nextInt(2)]; if(c1>c2) { c1=random.nextInt(g); c2=random.nextInt(g); } String str1 = c1+""+w1+""+c2; s1[i]=str1; int t1 = oper.caculate(str1+'#'); answer[i]=t1; out.println(str1+'='); } else if(f.equals("2")&&subf.equals("3")) { String str3; int u=random.nextInt(g); int v=random.nextInt(g)+1; char w=arr[random.nextInt(4)]; if(u%v==0 && w=='/') { str3 =u+""+w+""+v; } else str3 =u-(u%v)+""+w+""+v; s1[i]=str3; int t1 = oper.caculate(str3+'#'); answer[i]=t1; out.println(str3+'='); } else if(f.equals("2")&&subf.equals("4")) { int u=random.nextInt(g); int v=random.nextInt(g)+1; char w=arr[random.nextInt(4)]; String str3 =u+""+w+""+v; s1[i]=str3; int t1 = oper.caculate(str3+'#'); answer[i]=t1; out.println(str3+'='); } else { String str=new String(); int n =random.nextInt(3)+1; Stack<Integer> S1 = new Stack<Integer>(); Stack<Character> S2 = new Stack<Character>(); for(int j=0;j<n+1;j++) { char w=' '; int e= random.nextInt(g)+1; if( w=='/'&&e==0) { e = random.nextInt(g)+1; } w = arr[random.nextInt(5)]; S1.push(e); S2.push(w); } int k=0; for(int j=0;j<n;j++) { char m = S2.pop(); if(m=='(') { if(j>n-2) str =str+S1.pop()+""+arr[random.nextInt(4)]; else { str =str+S1.pop()+""+arr[random.nextInt(4)]+""+m; k=k+1; } } else str =str+S1.pop()+""+ m; } if(k==0) { str =str+random.nextInt(g)+1; } else if(k==1) str=str+random.nextInt(g)+1+")"; else if(k==2) str=str+random.nextInt(g)+1+"))"; else if(k==3) str=str+random.nextInt(g)+1+")))"; else str=str+random.nextInt(g)+1+"))))"; s1[i]=str; int t1 = oper.caculate(str+'#'); answer[i]=t1; out.println(str+'='); } %> <input type="text" name=<%=se[i] %>><br> <% } session.setAttribute( "v1",s1); session.setAttribute( "v2",answer); %> <br><input type="submit" name="submit" value="提交"> <% %> </form> </center> </body> </html>
answer.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=utf-8"> <title>校验</title> </head> <body> <center> <h1>四则运算</h1> <% request.setCharacterEncoding("UTF-8"); String[]s1 = (String[])session.getAttribute("v1"); int[] answer=(int[])session.getAttribute("v2"); int h=s1.length; String[]se=new String[h]; int counter=0; for(int i=0;i<h;i++) { se[i]="se"; se[i]=se[i]+i; int a =Integer.parseInt(request.getParameter(se[i])); out.println(s1[i]+'='); out.println(a); out.println("<br>"); if(answer[i]==a) { out.println("回答正确!"); out.println("<br>"); counter++; } else { out.println("答案错误!"); out.println("正确答案为:"+answer[i]); out.println("<br>"); } } out.println("<br>"); out.println("您共答对了"+counter+"道题。"); %> <br> <input type="button" value="返回首页" onClick="window.location.href='new.html'"> <input type="button" value="关闭" onClick="window.close();"> </center> </body> </html>
operate.java
package test; import java.util.Stack; public class Operate { private Stack<Character> priStack = new Stack<Character>();// 操作符栈 private Stack<Integer> numStack = new Stack<Integer>();;// 操作数栈 public int caculate(String str) { // 1.判断string当中有没有非法字符 String temp;// 用来临时存放读取的字符 // 2.循环开始解析字符串,当字符串解析完,且符号栈为空时,则计算完成 StringBuffer tempNum = new StringBuffer();// 用来临时存放数字字符串(当为多位数时) StringBuffer string = new StringBuffer().append(str);// 用来保存,提高效率 while (string.length() != 0) { temp = string.substring(0, 1); string.delete(0, 1); // 判断temp,当temp为操作符时 if (!isNum(temp)) { // 1.此时的tempNum内即为需要操作的数,取出数,压栈,并且清空tempNum if (!"".equals(tempNum.toString())) { // 当表达式的第一个符号为括号 int num = Integer.parseInt(tempNum.toString()); numStack.push(num); tempNum.delete(0, tempNum.length()); } // 用当前取得的运算符与栈顶运算符比较优先级:若高于,则因为会先运算,放入栈顶;若等于,因为出现在后面,所以会后计算,所以栈顶元素出栈,取出操作数运算; // 若小于,则同理,取出栈顶元素运算,将结果入操作数栈。 // 判断当前运算符与栈顶元素优先级,取出元素,进行计算(因为优先级可能小于栈顶元素,还小于第二个元素等等,需要用循环判断) while (!compare(temp.charAt(0)) && (!priStack.empty())) { int a = (int) numStack.pop();// 第二个运算数 int b = (int) numStack.pop();// 第一个运算数 char ope = priStack.pop(); int result = 0;// 运算结果 switch (ope) { // 如果是加号或者减号,则 case '+': result = b + a; // 将操作结果放入操作数栈 numStack.push(result); break; case '-': result = b - a; // 将操作结果放入操作数栈 numStack.push(result); break; case '*': result = b * a; // 将操作结果放入操作数栈 numStack.push(result); break; case '/': result = b / a;// 将操作结果放入操作数栈 numStack.push(result); break; } } // 判断当前运算符与栈顶元素优先级, 如果高,或者低于平,计算完后,将当前操作符号,放入操作符栈 if (temp.charAt(0) != '#') { priStack.push(new Character(temp.charAt(0))); if (temp.charAt(0) == ')') {// 当栈顶为'(',而当前元素为')'时,则是括号内以算完,去掉括号 priStack.pop(); priStack.pop(); } } } else // 当为非操作符时(数字) tempNum = tempNum.append(temp);// 将读到的这一位数接到以读出的数后(当不是个位数的时候) } return numStack.pop(); } private boolean isNum(String temp) { return temp.matches("[0-9]"); } /* * 比较当前操作符与栈顶元素操作符优先级,如果比栈顶元素优先级高,则返回true,否则返回false */ private boolean compare(char str) { if (priStack.empty()) { // 当为空时,显然 当前优先级最低,返回高 return true; } char last = (char) priStack.lastElement(); // 如果栈顶为'('显然,优先级最低,')'不可能为栈顶。 if (last == '(') { return true; } switch (str) { case '#': return false;// 结束符 case '(': // '('优先级最高,显然返回true return true; case ')': // ')'优先级最低, return false; case '*': { // '*/'优先级只比'+-'高 if (last == '+' || last == '-') return true; else return false; } case '/': { if (last == '+' || last == '-') return true; else return false; } // '+-'为最低,一直返回false case '+': return false; case '-': return false; } return true; } public static void main(String[] args) { // TODO Auto-generated method stub } }
结果截图:
总结:
本次实验主要是对web网页开发相关技术的应用,再学习了数据的传输和网页跳转技术之后实现起来是会容易一些。
PSP记录日志:
预估表:
版块 | 主页 | 出题页 | 判断页 |
预估时间 | 20 | 180 | 120 |
实际用时:
日期 | 开始时间 | 结束时间 | 中断时间 | 净时间 | 活动 |
4/2 | 9:00 | 10:10 | 70 | 结对首页开发 | |
4/3 | 16:10 | 18:40 | 150 | 将源代码与web结合(查资料) | |
4/4 | 14:20 | 16:50 | 150 | 编写web代码 | |
4/6 | 15:40 | 17:00 | 100 | 编写web代码 | |