1. Project Requirements
A) In addition to integers, support the arithmetic of true fractions. (Example: 1/6 + 1/8 = 7/24)
b) Allow the program to accept the user to enter the answer and determine the right and wrong. Finally, the total number of pairs/errors is given.
c) Gradually expand the functionality and the types of expressions that can be supported, and finally, you want to support the following types of topics (up to 10 operators, the number of parentheses is unlimited)
25-3 * 4-2/2 + 89 =?
1/2 + 1/3-1/4 =?
(5-4) * (3 +28) =?
d) can batch out more than 100 of the topics, saved in a text file, and ensure that the problem can not be repeated,
2. Project implementation
A) development environment
Development tools: MyEclipse, jdk1.8, Windows 7
Development language: Java
b) project design
1) Main UML diagram
2) Main code
A. Determine whether two numbers are coprime
1 public static Boolean iscoprime (int m,int n) {2 int a=0; 3 int b=0; 4 int c=0; 5 if (M &G T N) {6 a = m; 7 B = N; 8}else{9 a=n;10 b= }12 while ((c = a% b)! = 0) {a =
b;14 B =
}16 if (b==1) {# return True }19 return false;
B. Score Numerator
/** * @param exp Single fraction expression * @returned String returns the fractional expression after numerator * * /public static String aftersimplify ( String exp) { string[] num=exp.split ("/"); int Cop=integer.parseint (num[0]);//numerator int deno=integer.parseint (num[1]);//Denominator if (cop==0) {//numerator is 0 Return "0"; } int max=0; while (Max!=1) { max=maxmultiple (COP, Deno);//returns COP, Deno greatest common divisor cop/=max; deno/=Max;} if ( Deno==1) {return cop+ "";//Score value is 1}else{return cop+ "/" +deno;//the score after simplification }" /c16>
C. infix expression converted to suffix expression
/** * @param infix infix expression * @returned String suffix expression * */public staticString Infix2postfix (String infix) {string postfix= ""; int length=Infix.length (); Stack st = newStack (); String C; for (int i = 0; i < length; i++) {c = infix.substring (i, i+1); if (c. Equals ("(") {St.push (c);} else if (c.equals (")") {while (!st.peek (). Equals ("(") {postfix+= (St.pop () + "#")); } st.pop (); }else{Try{Integer.parseint (c);//Determines whether the read character is a number for (int j=0;j<5;j++) {//or, if read to a number, continues to read backwards until the operator is read to String c_temp= ""; if ((i+j+2) >length) {//determines whether to reach the end of the input string break; } c_temp=infix.substring (i+j+1,i+j+2); Try{Integer.parseint (c_temp);//Determine if the unique character is a digital c+=c_temp; continue;} catch (Exception e) {break;}} i+= (C.length ()-1); postfix+= (c+ "#");} Catch(Exception e) {while (!st.empty () && (Comparepri ((String) St.peek (), c) >= 0)) {postfix + = (st.pop ()+"#"); } st.push (c); }}} while (!st.empty ()) {///input stack all remaining elements postfix + = (St.pop () + "#");} return postfix;}/** * @param op1 OP2 operator *@r Eturn int OP1, OP2 precedence comparison result * */public static int Comparepri (string op1, String op2) {if (Op1. Equals ("(")) {Retu Rn-1;} else if (Op1 equals ("+") | | op1. Equals ("-")) {if (Op2.equals ("*") | | op2.equals ("/")) {return-1;}} else if (op1. Equals ("*") | | Op1. Equals ("/")) {if (op2. Equals ("+") | | op2. Equals ("-")) {return 1;}} return 0;}
D. Expression evaluation (integral and fractional)
/** * @param exp Arithmetic expression * @return The simplest (fractional) form of the expression meta-calculation result * */PublicString Calculate (String exp) {int a,b,result=0; String first,second,temp = ""; IntFirst_cop,first_deno,second_cop,second_deno; Stack s=newStack (); String[] C=exp.split ("#"); Fractional Evaluation if (Exp.contains ("/") {for (int i=0;i<c.length;i++) {if (!) ( C[i].contains ("+") | | C[i].contains ("-") | | C[i].contains ("*") | | C[i].contains ("/")) {S.push (c[i]+ "/1");//Converts the integer to a fractional form with a denominator of 1 continue; }else{second=(String) S.pop (); first=(String) S.pop (); First_cop=integer.parseint (First.split ("/") [0]);//The molecular first_deno=integer.parseint of the first fraction (First.split ("/") [1]);// The denominator of the first fraction second_cop=integer.parseint (Second.split ("/") [0]);//The molecular second_deno=integer.parseint of the second fraction (Second.split ( "/") [1]);//The denominator of the second score if (C[i].equals ("+")) {//Fractions add temp= (First_cop*second_deno+second_cop*first_deno) + "/" + (First_ deno*Second_deno); }else if (c[i].equals ("-")) {//Fractions subtract temp= (First_cop*second_deno-second_cop*first_deno) + "/" + (first_deno*Second_deno); }else if (c[i].equals ("*")) {//Fractions multiplied temp= (FIRST_COP*SECOND_COP) + "/" + (first_deno*second_deno);} else if (c[i].equals ("/")) {//Fractions divide temp= (First_cop*second_deno) + "/" + (first_deno*second_cop);} s.push (temp);// Press the result of the calculation into the stack} }//To return the final result numerator returns simplify.aftersimplify ((String) s.pop ());} else{//integer evaluates for (int i=0;i<c.length;i++) {try{integer.parseint (c[i]);//Determines whether it is a digital s.push (C[i]); Continue;} Catch(Exception e) {b=integer.parseint ((String) s.pop ()), A=integer.parseint ((String) s.pop ()); if (c[ I].equals ("+")) {result=a+B;} else if (c[i].equals ("-")) {result=a-B;} else if (c[i].equals ("*")) {result=a*B;} s.push (result+ "");}}}//return integer operation result return result+ "";}
E. Random generation of pure, pure fraction, mixed-expression
if (Type.equals ("1")) {//integer operation for (int i = 0; i < count; i++) {//Generate count of officialQuestions.add (B.zhengshi (min, max)); }}else if (Type.equals ("2")) {//fractional operation for (int i = 0; i < count; i++) {//Generate count of fractionsQuestions.add (B.fenshi (Deno)); }}else if (Type.equals ("3")) {//mixed operation for (int i = 0; i < count; i++) {//Generate count of mixed meta-expressions int Length=r.nextint (4) +3;//newly added symbol is the most 6 String op,exp= ""; for (int j=0;j<length;j++{Op=ops[r.nextint (2)];//randomly selects the operation symbol ' + ' or ' * ' if (op.equals ("+")) {if (R.nextint (2) ==1) {Exp=exp+b.zhengshu (min, max) + "+"; }else{Exp=exp+b.fenshu (Deno) + "+"; }}else if (Op.equals ("*") {if (Exp.length () ==0) {Exp=r.nextint (9) +2+ "+"; } if (R.nextint (2) ==1) {//randomly selects an integer or integer String item=B.zhengshi (min, max); while (Item.contains ("*") | | Item.contains ("/")) {item=B.zhengshi (min, max); } item= "(" +item+ ")"; if (Exp.substring (0,exp.length ()-1). Contains ("+") | | Exp.substring (0,exp.length ()-1). Contains ("-") ) &&!exp.substring (0,exp.length ()-1). Contains ("*") &&!exp.substring (0,exp.length ()-1). Contains ("/")) {exp= "(" +exp.substring (0,exp.length ()-1) + ") *" +item+ "+";} else{exp=exp.substring (0,exp.length ()-1) + "*" +item+ "+";}} else{String item=b.zhengshu (min, max), if ((Exp.substring (0,exp.length ()-1). Contains ("+") | | Exp.substring (0,exp.length ()-1). Contains ("-")) &&!exp.substring (0,exp.length ()-1). Contains ("*") &&!exp.substring (0,exp.length ()-1). Contains ("/")) {exp= "(" +exp.substring (0,exp.length ()-1) + ") *" + item+ "+";} else{exp=exp.substring (0,exp.length ()-1) + "*" +item+ "+";}}} if (!exp.equals (") && (Exp.subsequence (Exp.length ()-1, Exp.length ()). Equals (" + "))) {exp=exp.substring (0,exp.length ()-1);//reject the plus sign at the end of an expression}
c) Test Results
1) Integer operation
2) Fractional element calculation
3) Mixed operation
3. Summary
At present, the demand has been basically achieved, the experimental results are more satisfactory. But there are shortcomings, such as: The problem of duplicate detection, the problem of generating rules to improve the preferential treatment. These issues will be resolved gradually in the next phase.
Chapter One-the first question (pupil arithmetic)--by Guo Qingyun