四则运算系统要求升级了:
是否有乘除法;
是否有括号(最多可以支持十个数参与计算);
数值范围;
加减有无负数;
除法有无余数!
· 思路:是在原有的网页版的基础上进行改动的,不过,改动的比较大,基本改动如下:
1.首先要把随机生成的四则运算改成多元运算,先前默认的是二元运算,所以方法都要改,随机生成一个数,然后生成一个运算符号,都是累加在String上。随机生成的数比运算符号要多一个!
2.要实现数值范围要求,随机生成的上限100,改成用户自己定。
3.实现有无乘除。随机数上限为2无乘除,上限为4有乘除。
4.结果有无负数,余数。必须要求有方法可以计算随机生成的四则运算,对结果进行要求,将结果存放在数组中。设计方法,可以计算String类型的多项式(带括号的),可以计算出余数的方法,要注意的是,余数只是二元运算才有,二元以上不应该此要求!
· 对应的代码如下:
(1)输入界面代码
<%@ 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>
<h2>题目样式:1.整数四则运算 2.真分数四则运算</h2>
选择:<input type="text" name="choose"><br>
<h2>题目样式:1.有乘除 2.无乘除</h2>
选择:<input type="text" name="choose3"><br>
<h2>题目样式:请输入几元运算</h2>
选择:<input type="text" name="choose1"><br>
<h2>题目样式:请输入范围</h2>
选择:<input type="text" name="choose2"><br>
<h2>题目样式:1.有负数 2.无负数</h2>
选择:<input type="text" name="choose4"><br>
<h2>题目样式:1.有余数 2.无余数</h2>
选择:<input type="text" name="choose6"><br>
<h2>题目样式:1.显示答案 2.不显示答案</h2>
选择:<input type="text" name="choose5"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
(2)计算.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;
}
else
{
ss = ss + intVal2+"/"+intVal1;
}
}
if(i < num1)
ys = (int)(Math.random()*num3+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;
}
%>
<body>
<%
JspWriter mout=pageContext.getOut();
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");//有无余数
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);
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);
StringCaculate re = new StringCaculate();
double result = re.fun1(ss);
double r = result-(int)result;
for(int j=0;j<i;j++)
{
if(ss.equals(str[i-1])||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>");
}
if(cho6 == 1)//有余数
for(int i=1;i<=num;i++)
{
int state = 0;
while(state == 0)
{
ss = ztimu(cho1,cho2,cho3);
StringCaculate re = new StringCaculate();
double result = re.fun1(ss);
double r = result-(int)result;
for(int j=0;j<i;j++)
{
if(cho1>2)//三元以上无余数
{
if(ss.equals(str[i-1]))
{
state = 0;
}
else
{
str[i] = ss;
st[i] = result;
state = 1;
}
}
if(cho1<=2)
{
if(ss.equals(str[i-1]))
{
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);
StringCaculate re = new StringCaculate();
double result = re.fun1(ss);
double r = result-(int)result;
for(int j=0;j<i;j++)
{
if(ss.equals(str[i-1])||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>");
}
if(cho6 == 1)//有余数
for(int i=1;i<=num;i++)
{
int state = 0;
while(state == 0)
{
ss = ztimu(cho1,cho2,cho3);
StringCaculate re = new StringCaculate();
double result = re.fun1(ss);
double r = result-(int)result;
for(int j=0;j<i;j++)
{
if(cho1>2)//三元以上无余数
{
if(ss.equals(str[i-1])||result<0)
{
state = 0;
}
else
{
str[i] = ss;
st[i] = result;
state = 1;
}
}
if(cho1<=2)
{
if(ss.equals(str[i-1])||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>");
}
}
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];
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[i-1]))
{
state = 0;
}
else
{
st[i] = ss;
state = 1;
}
}
}
mout.print(i+" 、 ");
mout.print(st[i]);
mout.print("= ?");
mout.print("<br>");
}
}
%>
</body>
</html>
(3)建类包代码如下:
package tianaoweb.com;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class Caculate {
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);
}
}
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)
{
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);
}
}
·结果截图: