在第三次实验的基础上,老师又对四则运算提出了新的要求,实现网页版或安卓的四则运算。
结对开发伙伴:
姓名:程思敏
博客名:鹏程万里之思
博客地址链接:http://home.cnblogs.com/u/pengchengwanli/
网页四则运算要求:
1、生成的题目中计算过程不能产生负数,也就是说算术表达式中如果存在e1-e2的子表达式,那么结果大于等于0;
2、生成的题目中如果存在形式如e1/e2的子表达式,那么其结果应该是真分数。
3、每道题目中出现的运算符个数不超过3个,括号不做详细要求。
设计思路:
1.将原先程序中的结构体改成class文件,依次生成calculator.java、arithmetic.java、Formula.java、subject.java文件。
2.编写MyHtml.html文件,在页面上显示出题数、上下限文本框,以及四个复选框控制出题条件。
3.在MyHtml.html页面上点击确认键,跳转到index.jsp文件,调用calculator.java文件中的函数,产生各道算式,在对应的文本框中输入答案。
4.在index.jsp页面上点击提交按钮,跳转到Myjsp.jsp文件,显示答题结果。
程序代码:
package Calculator; import java.util.*; import Calculator.arithmetic; import Calculator.subject; import Calculator.Formula; import java.lang.*; public class calculator { static int N=100; //public int length = 0;//运算数个数 static int fuh;//表示运算符的数(0、1、2、3分别代表+、-、*、/) static subject sub[]=new subject[N];//所有算式 static int lpoint1;//内层左括号的位置 static int rpoint1;//内层右括号的位置 static int lpoint2;//外层左括号的位置 static int rpoint2;//外层右括号的位置 static Formula Formul[]=new Formula[N]; //定义一个字符串数组 static int Flength; //字符串数组长度 static int i, j; static int Result; //算式的运算结果 static int dataS[]=new int[N]; //存储算式中的数 static String fuhaoS[]=new String[N]; //存储算式中的符号 static int count; //记录答对题数 static int CountNum=-1; //记录对每道题求解 static int CountStr=-1; //记录每道题 static int low; //算式数值范围的最小值 static int high; //算式数值范围的最大值 static int MulAndDiv; //是否有乘除 static int Bracket; //是否有括号 static int Negative; //是否有负数 static int Remainder; //是否有余数 //static int IsRep; //是否重复 static int Results[]= new int[N]; //正确结果数组 //public subject buffer[] = new subject[N]; //缓冲区数组 static String buffer[]=new String[N]; static int LENGTH[]=new int[N]; //记录算式的长度 static int number; //题目数量 //将算式转化为string数组 public void ToString(int geti, int length) { Flength = -1; for (j = 0; j < length; j++) { if (sub[geti].arith[j].left2 != "") { Flength =Flength+ 1; Formul[Flength].str = sub[geti].arith[j].left2; } if (sub[geti].arith[j].left1 != "") { Flength =Flength+ 1; Formul[Flength].str = sub[geti].arith[j].left1; } Flength =Flength+ 1; Formul[Flength].str = String.valueOf(sub[geti].arith[j].member); Formul[Flength].data = sub[geti].arith[j].member; if (sub[geti].arith[j].right1 != "") { Flength =Flength+ 1; Formul[Flength].str = sub[geti].arith[j].right1; } if (sub[geti].arith[j].right2 != "") { Flength =Flength+ 1; Formul[Flength].str = sub[geti].arith[j].right2; } if (sub[geti].arith[j].fuhao != "") { Flength =Flength+ 1; Formul[Flength].str = sub[geti].arith[j].fuhao; } } String bstr=""; for(i=0;i<Flength;i++) { bstr=bstr+Formul[i].str; } CountStr+=1; buffer[CountStr]=bstr; } //比较优先级 public String Precede(String Fuhao1, String Fuhao2) { String FuResult=""; //返回优先级关系">"、"<"、"=" if (Fuhao1 == "+") { if (Fuhao2 == "+" || Fuhao2 == "-" || Fuhao2 == ")" || Fuhao2 == "#") { FuResult = ">"; } else { FuResult = "<"; } } if (Fuhao1 == "-") { if (Fuhao2 == "+" || Fuhao2 == "-" || Fuhao2 == ")" || Fuhao2 == "#") { FuResult = ">"; } else { FuResult = "<"; } } if (Fuhao1 == "*") { if (Fuhao2 == "(") { FuResult = "<"; } else { FuResult = ">"; } } if (Fuhao1 == "/") { if (Fuhao2 == "(") { FuResult = "<"; } else { FuResult = ">"; } } if (Fuhao1 == "(") { if (Fuhao2 == ")") { FuResult = "="; } else { FuResult = "<"; } } if (Fuhao1 == ")") { FuResult = ">"; } if (Fuhao1 == "#") { if (Fuhao2 == "#") { FuResult = "="; } else { FuResult = "<"; } } return FuResult; } //两个数运算 public int Operate(int num1, String theta, int num2) { int InResult; //返回运算结果 if (theta == "+") { InResult = num1 + num2; } else if (theta == "-") { InResult = num1 - num2; } else if (theta == "*") { InResult = num1*num2; } else { InResult = num1 / num2; } return InResult; } //表达式求值 public int ValueResult(int geti, int length) { int i; Flength = 0; String PResult; //优先级比较结果 String theta; //出栈符号 int num1, num2; //出栈运算数 int dalength=0; //算式数据数组的长度 int fulength=0; //算式符号数组的长度 ToString(geti, length); Formul[Flength].str = "#"; //InitStack(OPND); //fuInitStack(OPTR); //fuPush(OPTR, "#"); fuhaoS[fulength]="#"; for (i = 0; i <= Flength; i++) { if (Formul[i].str == "#") { while (fuhaoS[fulength]!= "#") { PResult = Precede(fuhaoS[fulength], Formul[i].str); if (PResult == ">") { //fuPop(OPTR, theta); theta=fuhaoS[fulength]; fulength-=1; //Pop(OPND, num2); num2=dataS[dalength]; dalength-=1; //Pop(OPND, num1); num1=dataS[dalength]; dalength-=1; int num3 = Operate(num1, theta, num2); //Push(OPND, num3); dalength+=1; dataS[dalength]=num3; } else if (PResult == "<") { //fuPush(OPTR, Formul[i].str); fulength+=1; fuhaoS[fulength]=Formul[i].str; } else { //fuPop(OPTR, theta); theta=fuhaoS[fulength]; fulength-=1; } } } else if (Formul[i].str == "(" || Formul[i].str == ")" || Formul[i].str == "+" || Formul[i].str == "-" || Formul[i].str == "*" || Formul[i].str == "/") { PResult = Precede(fuhaoS[fulength], Formul[i].str); if (PResult == ">") { //fuPop(OPTR, theta); theta=fuhaoS[fulength]; fulength-=1; //Pop(OPND, num2); num2=dataS[dalength]; dalength-=1; //Pop(OPND, num1); num1=dataS[dalength]; dalength-=1; int num3 = Operate(num1, theta, num2); //Push(OPND, num3); dalength+=1; dataS[dalength]=num3; PResult = Precede(fuhaoS[fulength], Formul[i].str); if (PResult == "=") { //fuPop(OPTR, theta); continue; theta=fuhaoS[fulength]; fulength-=1;continue; } else { //fuPush(OPTR, Formul[i].str); fulength+=1; fuhaoS[fulength]=Formul[i].str; } } else if (PResult == "<") { //fuPush(OPTR, Formul[i].str); fulength+=1; fuhaoS[fulength]=Formul[i].str; } else { //fuPop(OPTR, theta); theta=fuhaoS[fulength]; fulength-=1; } } else { //Push(OPND, Formul[i].data); dalength+=1; dataS[dalength]=Formul[i].data; } } return dataS[dalength]; } //随机数产生运算符 String Fuhao(int fuh) { String fuh0;//返回运算符号 if (fuh == 0) fuh0 = "+"; else if (fuh == 1) fuh0 = "-"; else if (fuh == 2) fuh0 = "*"; else fuh0 = "/"; return fuh0; } //初始化算式 public void Init(int geti,int length, int high, int low, int TwoOrFour) { Random rand = new Random(); for (j = 0; j < length - 1; j++) { sub[geti].arith[j].left2 = ""; sub[geti].arith[j].left1 = ""; sub[geti].arith[j].right1 = ""; sub[geti].arith[j].right2 = ""; sub[geti].arith[j].member = rand.nextInt() % (high - low) + low; //无乘除法 if (TwoOrFour == 2) { fuh = rand.nextInt() % 2; } //有乘除法 if (TwoOrFour == 4) { fuh = rand.nextInt() % 4; } sub[geti].arith[j].fuhao = Fuhao(fuh); } sub[geti].arith[length - 1].left2 = ""; sub[geti].arith[length - 1].left1 = ""; sub[geti].arith[length - 1].right1 = ""; sub[geti].arith[length - 1].right2 = ""; sub[geti].arith[length - 1].member = rand.nextInt() % (high - low) + low; sub[geti].arith[length - 1].fuhao = "="; for (j = 0; j < length; j++) { if (sub[geti].arith[j].right1 == "" || sub[geti].arith[j].right1 == ")") { if (sub[geti].arith[j].fuhao == "/"&&sub[geti].arith[j].left2 == ""&&sub[geti].arith[j].left1 == ""&&sub[geti].arith[j].right2 == "") { if (sub[geti].arith[j + 1].fuhao == "/"&&sub[geti].arith[j + 1].left2 == ""&&sub[geti].arith[j + 1].left1 == ""&&sub[geti].arith[j + 1].right1 == ""&&sub[geti].arith[j + 1].right2 == "") { sub[geti].arith[j].left1 = "("; sub[geti].arith[j + 1].right1 = ")"; j +=1; } } } } } /* //输出函数 public void Output(int geti, int length) { for (j = 0; j < length; j++) { //cout << sub.arith[j].left2 << sub.arith[j].left1 << sub.arith[j].member << sub.arith[j].right1 << sub.arith[j].right2 << sub.arith[j].fuhao; System.out.print(sub[geti].arith[j].left2); System.out.print(sub[geti].arith[j].left1); System.out.println(sub[geti].arith[j].member); System.out.print(sub[geti].arith[j].right1); System.out.print(sub[geti].arith[j].right2); System.out.print(sub[geti].arith[j].fuhao); } //cout << endl; } */ //括号的产生 public int Bracket(int geti, int length) { Random rand = new Random(); int check = 0; if (length > 2) { int floor = rand.nextInt() % 2 + 1;//产生括号的层数 if (floor == 1) { lpoint1 = rand.nextInt() % (length - 1);//产生左括号的位置 rpoint1 = lpoint1 + rand.nextInt() % (length - 1 - lpoint1);//产生右括号的位置 if (lpoint1 == rpoint1) { if (lpoint1 > 0) { lpoint1 -= 1; } else { if (rpoint1 < length - 1) { rpoint1 +=1; } } } sub[geti].arith[lpoint1].left1 = "("; sub[geti].arith[rpoint1].right1 = ")"; } else { if (length > 3) { lpoint1 = rand.nextInt() % (length - 1);//产生内层左括号的位置 rpoint1 = lpoint1 + rand.nextInt() % (length - 1 - lpoint1);//产生内层右括号的位置 if (lpoint1 == rpoint1) { if (lpoint1 > 0) { lpoint1 -= 1; } else { if (rpoint1 < length - 1) { rpoint1 +=1; } } } sub[geti].arith[lpoint1].left1 = "("; sub[geti].arith[rpoint1].right1 = ")"; if (lpoint1 == 0) { lpoint2 = lpoint1;//产生外层左括号的位置 } else { lpoint2 = lpoint1 - rand.nextInt() % lpoint1;//产生外层左括号的位置 } rpoint2 = rpoint1 + rand.nextInt() % (length - rpoint1);//产生外层右括号的位置 if (lpoint2 == lpoint1&&rpoint2 == rpoint1) { if (lpoint2 > 0) { lpoint2 -= 1; } else { if (rpoint2 < length - 1) { rpoint2 +=1; } } } sub[geti].arith[lpoint2].left2 = "("; sub[geti].arith[rpoint2].right2 = ")"; } else { check = 1; } } } return check; } //判断用户是否答对 public int Right(int result, int answer) { if (answer == result) { return 0; } else { return 1; } //return 0; } //验证除法有无余数 public int change(int geti, int length) { int check = 0; for (j = 0; j < length; j++) { if (sub[geti].arith[j].fuhao == "/"&&sub[geti].arith[j].right1 != ")") { if (sub[geti].arith[j].member%sub[geti].arith[j + 1].member != 0) { check = 1; } } if (sub[geti].arith[j].fuhao == "/"&&sub[geti].arith[j].right1 == ")") { if ((sub[geti].arith[j - 1].member / sub[geti].arith[j].member) % sub[geti].arith[j + 1].member != 0) { check = 1; } } } return check; } //有乘除法、有括号、加减有负数、除法有余数 public void Output1(int number, int low, int high) { int count = 0; for (i = 0; i < number; i++) { int check = 0;//检验产生式是否合格 Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; Init(i, sub[i].length, high, low, 4); Result = ValueResult(i, sub[i].length); check = Bracket(i, sub[i].length); if (Result>high) { check = 1; } if (check == 1) { i = i - 1; continue; } else { //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } } //UserResult(count); } //有乘除法、有括号、加减有负数、除法无余数 public void Output2(int number, int low, int high) { int count = 0; for (i = 0; i < number; i++) { int check = 0;//检验产生式是否合格 Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; int check1 = 0;//检验除法是否有余数 Init(i, sub[i].length, high, low, 4); check = Bracket(i, sub[i].length); check1 = change(i, sub[i].length); Result = ValueResult(i, sub[i].length); if (Result > high) { check = 1; } if (check == 1 || check1 == 1) { i = i - 1; continue; } else { //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } } //UserResult(count); } //有乘除法、有括号、加减无负数、除法有余数 public void Output3(int number, int low, int high) { int count = 0; for (i = 0; i < number; i++) { int check = 0;//检验产生式是否合格 Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; Init(i, sub[i].length, high, low, 4); check = Bracket(i, sub[i].length); Result = ValueResult(i, sub[i].length); if (Result < 0 || Result > high) { check = 1; } if (check == 1) { i = i - 1; continue; } else { //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } } //UserResult(count); } //有乘除法、有括号、加减无负数、除法无余数 public void Output4(int number, int low, int high) { int count = 0; for (i = 0; i < number; i++) { int check = 0;//检验产生式是否合格 int check1 = 0;//检验除法是否有余数 Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; Init(i, sub[i].length, high, low, 4); check = Bracket(i, sub[i].length); check1 = change(i, sub[i].length); Result = ValueResult(i, sub[i].length); if (Result < 0 || Result > high) { check = 1; } if (check == 1 || check1 == 1) { i = i - 1; continue; } else { //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } } //UserResult(count); } //有乘除法、无括号、加减有负数、除法有余数 public void Output5(int number, int low, int high) { int count = 0; for (i = 0; i < number; i++) { int check = 0; Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; Init(i, sub[i].length, high, low, 4); Result = ValueResult(i, sub[i].length); if (Result > high) { check = 1; } if (check == 1) { i = i - 1; continue; } //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } //UserResult(count); } //有乘除法、无括号、加减有负数、除法无余数 public void Output6(int number, int low, int high) { int count = 0; for (i = 0; i < number; i++) { int check = 0; Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; Init(i, sub[i].length, high, low, 4); check = change(i, sub[i].length); Result = ValueResult(i, sub[i].length); if (Result > high) { check = 1; } if (check == 1) { i = i - 1; continue; } //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } //UserResult(count); } //有乘除法、无括号、加减无负数、除法有余数 public void Output7(int number, int low, int high) { int count = 0; for (i = 0; i < number; i++) { int check = 0;//检验 Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; Init(i, sub[i].length, high, low, 4); Result = ValueResult(i, sub[i].length); if (Result < 0 || Result > high) { check = 1; } if (check == 1) { i = i - 1; } else { //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } } //UserResult(count); } //有乘除法、无括号、加减无负数、除法无余数 public void Output8(int number, int low, int high) { int count = 0; for (i = 0; i < number; i++) { int check = 0;//检验 Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; Init(i, sub[i].length, high, low, 4); Result = ValueResult(i, sub[i].length); check = change(i, sub[i].length); if (Result < 0 || Result > high) { check = 1; } if (check == 1) { i = i - 1; continue; } else { //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } } //UserResult(count); } //无乘除法、有括号、加减有负数 public void Output9(int number, int low, int high) { int count = 0; for (i = 0; i < number; i++) { int check = 0;//检验产生式是否合格 Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; Init(i, sub[i].length, high, low, 2); Result = ValueResult(i, sub[i].length); check = Bracket(i, sub[i].length); if (Result>high) { check = 1; } if (check == 1) { i = i - 1; continue; } else { //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } } //UserResult(count); } //无乘除法、有括号、加减无负数 public void Output10(int number, int low, int high) { int count = 0; for (i = 0; i < number; i++) { int check = 0;//检验产生式是否合格 Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; Init(i, sub[i].length, high, low, 2); check = Bracket(i, sub[i].length); Result = ValueResult(i, sub[i].length); if (Result < 0 || Result > high) { check = 1; } if (check == 1) { i = i - 1; continue; } else { //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } } //UserResult(count); } //无乘除法、无括号、加减有负数 public void Output11(int number, int low, int high) { int count = 0; for (i = 0; i < number; i++) { Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; Init(i, sub[i].length, high, low, 2); Result = ValueResult(i, sub[i].length); if (Result>high) { i = i - 1; continue; } //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } //UserResult(count); } //无乘除法、无括号、加减无负数 public void Output12(int number, int low, int high) { //int count = 0;//记录用户答对题数 for (i = 0; i < number; i++) { Random rand = new Random(); sub[i].length = rand.nextInt() % 9 + 2; Init(i, sub[i].length, high, low, 2); int check = 0;//检验 Result = ValueResult(i, sub[i].length); if (Result<0 || Result>high) { check = 1; } if (check == 1) { i = i - 1; continue; } //Output(sub[i], sub[i].length); //LENGTH[CountNum++]=sub[i].length; //buffer[CountNum++]=sub[i]; CountNum+=1; Results[CountNum]=Result; //int answer = Input(); //Right(Result, answer); } //UserResult(count); } public void SetValue(int high1,int low1,int number1){ MulAndDiv=0; Bracket=0; Negative=0; Remainder=0; high=high1; low=low1; number=number1; } public void SetValue1(int MAD) { MulAndDiv=MAD; } public void SetValue2(int Bra) { Bracket=Bra; } public void SetValue3(int Neg) { Negative=Neg; } public void SetValue4(int Rem) { Remainder=Rem; } public int Choose(){ if(MulAndDiv==1&&Bracket==1&&Negative==1&&Remainder==1){ return 1; } else if(MulAndDiv==1&&Bracket==1&&Negative==1&&Remainder==0){ return 2; } else if(MulAndDiv==1&&Bracket==1&&Negative==0&&Remainder==1){ return 3; } else if(MulAndDiv==1&&Bracket==1&&Negative==0&&Remainder==0){ return 4; } else if(MulAndDiv==1&&Bracket==0&&Negative==1&&Remainder==1){ return 5; } else if(MulAndDiv==1&&Bracket==0&&Negative==1&&Remainder==0){ return 6; } else if(MulAndDiv==1&&Bracket==0&&Negative==0&&Remainder==1){ return 7; } else if(MulAndDiv==1&&Bracket==0&&Negative==0&&Remainder==0){ return 8; } else if(MulAndDiv==0&&Bracket==1&&Negative==1&&Remainder==0){ return 9; } else if(MulAndDiv==0&&Bracket==1&&Negative==0&&Remainder==0){ return 10; } else if(MulAndDiv==0&&Bracket==0&&Negative==1&&Remainder==0){ return 11; } else return 12; } public int StrToNum(String strget) { int num=0; for(int i=0;i<strget.length();i++) { num=num*10+strget.charAt(i)-48; } return num; } //输出算式 public String OutputEq(int number1) { return buffer[number1]; } //输出答案 public int OutputAn(int number1) { return Results[number1]; } public int OutputLE(int number1) { return LENGTH[number1]; } public int main() { int chooseWay; //条件筛选结果 chooseWay = Choose(); //条件筛选 switch (chooseWay) { case 1:Output1(number, low, high); break; case 2:Output2(number, low, high); break; case 3:Output3(number, low, high); break; case 4:Output4(number, low, high); break; case 5:Output5(number, low, high); break; case 6:Output6(number, low, high); break; case 7:Output7(number, low, high); break; case 8:Output8(number, low, high); break; case 9:Output9(number, low, high); break; case 10:Output10(number, low, high); break; case 11:Output11(number, low, high); break; case 12:Output12(number, low, high); } return 0; } }