Four Operational Program (Java console-based)

Source: Internet
Author: User
Tags arithmetic

First, the title description:
1. Use the-n parameter to control the number of generated topics, such as
Myapp.exe-n 10-o Exercise.txt
10 topics will be generated.
2. Use the-r parameter to control the range of values (natural number, true fraction, and true fraction denominator) in the topic, for example
Myapp.exe-r 10
The arithmetic topic will be generated within 10 (excluding 10). This parameter can be set to 1 or other natural number. This parameter must be given, otherwise the program will error and give help information.
3. If there is a sub-expression like E1÷e2 in the generated topic, the result should be a true fraction.
4. There are no more than 3 operators in each topic.
5. The problem of one run of a program cannot be repeated, that is, no two topics can be transformed into the same topic by means of an arithmetic expression of the finite Exchange + and the X or so. For example, 23 + 45 = and 45 + 23 = are repetitive topics, 6x8 = and 8x6 = are also repetitive topics. The two topics of 2+1 and 1+2+3 are repeated because
+ is left-bound, 1+2+3 equivalent to (1+2) +3, that is, the 1+2 (2+1). However, 1+2+3 and 3+2+1 are two non-repetitive problems, because 1+2+3 is equivalent to (1+2) +3, and 3+2+1 is equivalent to (3+2) +1, and they cannot be changed to the same subject by a finite exchange.
The generated topic is stored in the Exercises.txt file under the current directory of the executing program, in the following format:
1. Arithmetic Topic 1
2. Arithmetic Topic 2
......

  where true scores are in the format of input and output, a true fraction of three-fifths is represented as 3/5, and a true fraction of two and three-eighths represents 2 ' 3/8. 6. While generating the topic, calculate the answers to all the questions and deposit the Answers.txt file in the current directory of the executing program, in the following format: 1. Answer 1 2.    Answer 2 in particular, the operation of the true fraction is shown in the following example: 1/6 + 1/8 = 7/24.    7. The program should be able to support the generation of 10,000 topics. 8. The program supports a given problem file and answer file, determines the correct error in the answer and carries out the quantity statistics, and will output all the problems repeated in the topic, the input parameters are as follows: Myapp.exe-e <exercisefile>.txt-a <answerfil        E>.txt-o Grade.txt Statistics output to file Grade.txt, in the following format: Correct:5 (1, 3, 5, 7, 9) Wrong:5 (2, 4, 6, 8, 10) Repeat:2 Repeatdetail: (1) 2,45+32 Repeat 3,32+45 (2) 5,3+ (2+1) Repe At 7,1+2+3  

Ii. Analysis and Design
1. Generate Random expressions
We need to generate random numbers (integers, fractions, fractional) functions, random operator functions, random parentheses, and string concatenation to generate random expressions.
2. Expression processing calculations
The infix expression is converted to the suffix expression, the suffix expression is segmented, and the operation is performed by the stack, and due to the existence of fractional and band fractions, it needs to be calculated by means of a custom arithmetic rule, in which the number of assimilated components is numerator.
3. Weight-checking of expressions
By reading the answer document, scanning the expression of the same answer to judge, can improve efficiency, and then the same answer to the infix expression to the suffix expression, to determine whether all elements are equal. This method is more efficient, but has limitations. Because I do not through the binary tree processing expression, and then use two fork tree appears very cumbersome, and inefficient.
4. Output to document
Documents that need to be exported have expression Exercises.txt, answer Answers.txt, score and check results Grade.txt.

Third, the function realization

    1. Main program Main.java main code: SYSTEM.OUT.PRINTLN ("---------------Four Operation program---------------");    System.out.println ("-N: Generate the number of questions");    System.out.println ("-r: Parameter value range");    System.out.println ("-G: View test Results");    System.out.println ("Do: Execute Program");    System.out.println ("Please enter directive:");    Scanner in =new Scanner (system.in);                while (In.hasnext ()) {switch (In.next ()) {case "-n": System.out.println ("Please enter the number of topics to generate:");                N=in.nextint ();            Break                Case "-R": System.out.println ("Enter the numeric range of the operand:");                M=in.nextint ();            Break Case "-G": fo.        Filec (File2, File3, file4);            The answer is compared with the document, and the result is written to grade document break; Case ' do ': for (int i=0;i<n;i++) {String S=ex.   Createxp (n,m), fstr;                    Generates a random expression and solves a String rus=its.suffixtoarithmetic (Its.infixtosuffix (s));               fstr=i+1+ ":" +s+ "\ r \ n";                 Fo.            Filew (File1, FSTR);                    Expression written to document Fstr=i+1+ ":" +rus+ "\ r \ n"; Fo.            Filew (File2, FSTR);            The answer is written to the document} break; DEFAULT:SYSTEM.OUT.PRINTLN ("Invalid instruction!                ");        Break       } System.out.println ("Please enter directive:");                          2. Random expressions generate the main code:/* Generate an expression randomly */public String createxp (int n, int m) {String exp=creatnum (m);        Random number of operands rd=new random ();        int T=rd.nextint (2);                              Boolean flag=false;        Whether to generate parentheses if (t>0) Flag=creatkh ();                    for (int i=0;i<=t;i++) {//Generate string type infix expression if (flag==true) {if (i==0) {                Exp=exp+creatchar () + "(" +creatnum (m);                }else {Exp=exp+creatchar () +creatnum (m) + ")"; }}else{Exp=exp+creatchar () +creatnum (m);    }} return exp;        }/* Randomly generated operand */public String creatnum (int m) {String s= "";        Random rd=new random (); Switch (Rd.nextint (2)) {//Random type: integer, fractional case 0:s=integer.tostring (Rd.nex    TInt (m-1) +1);            Integer break;                Case 1://fractional int A, b;                A=rd.nextint (m-1) +1;                B=rd.nextint (m-2) +2;                            S=dating (A, b);        Fractional numerator processing break;    } return s;        }/* Randomly generated operator */public String Creatchar () {string s= "";        Random rd=new random ();            Switch (Rd.nextint (4)) {case 0:s= "+";            Case 1:s= "-";            Case 2:s= "*";        Case 3:s= "÷";    } return s; }/* Scores are numerator */public String Dating (int a,int b) {String s= "";        int gongyinshu=1,c;        c=a/b;        A=a%b;        if (c<0) {//If the band score is negative, this score does not have a minus sign a=a*-1;                  } for (int i = 1; i <= A; i++) {//Minimum number of conventions if (a% i = = 0 && b% i = = 0) {              Gongyinshu = i;                            }} A=a/gongyinshu;        Generate the simplest fraction b=b/gongyinshu;        if (a==0) {s=integer.tostring (c);        }else if (c==0) {s=integer.tostring (a) + "/" +integer.tostring (b);        }else{s=integer.tostring (c) + "'" "+integer.tostring (a) +"/"+integer.tostring (b);    } return s;        }/* Randomly generates parentheses */public boolean Creatkh () {Boolean flag=false;        Random rd=new random ();        if (Rd.nextint (3) <1)//The probability of generating the expansion number is 1/3 flag=true;    return flag; 3. Expression processing Main code:/* infix expression-to-suffix expression */public String Infixtosuffix (string exp) {stack&Lt                character> s = new stack<character> ();                                         Create the operator stack String suffix = "";                                  The suffix expression string to output int length = Exp.length ();                 The length of the input infix expression for (int i = 0; i < length; i++) {char temp;                               char ch = exp.charat (i);                        Gets each character of the infix expression and makes a judgment switch (CH) {case ' (': S.push (CH);                    Break                        Case ' + '://Encounters ' + '-', pops all the operators in the stack until the opening parenthesis is reached, and outputs to the queue to go to case '-':                        suffix + = "";                            while (S.size ()! = 0) {temp = S.pop ();                                if (temp = = ' (') {s.push (');                            Break } suffix + = tEmp                        suffix + = "";                        } s.push (CH);                    Break                        Case ' * '://If it is multiplication sign or division sign, all sequences will pop up until a good, minus, open parenthesis is encountered, and then the operator is pressed into the stack "÷":                        suffix + = "";                            while (S.size ()! = 0) {temp = S.pop ();                                 if (temp = = ' + ' | | temp = = '-' | | temp = = ' (') {s.push (temp);                            Break                                 } else {suffix + = temp;                           suffix + = "";                        }} s.push (CH);                    Break                            Case ') ': while (!s.isempty ()) {temp = S.pop ();                        if (temp = = ' (') {             Break                                    } else {suffix + = "";                            suffix + = temp;                    }} break;                         Default:suffix + = ch;                 Break                    }} while (S.size ()! = 0) {//If the stack is not empty, the remaining operator pops up once to the output sequence                        suffix + = "";             suffix + = s.pop ();         } return suffix; }/* Computes the suffix expression */public String suffixtoarithmetic (String exp) {string[] strings = Exp.split ("                  ");          Break the string by space stack<string> Stack = new stack<string> (); operand stack for (int i = 0; i < strings.length; i++) {if (Strings[i].equals ("+") | | Strings[i].equals ("-") | | Strings[i].equals ("*") | |   Strings[i].equals ("÷")) {                  String Y=stack.pop ();                     Read to the operator, extract the top of the stack of two operands, the first out operand is the number after the operator String X=stack.pop ();      String rus=calculate (x, Y, strings[i]);                                                  Call the custom arithmetic rule stack.push (RUS);                      if (Rus.equals ("no Solution"))//Divisor 0 Returns the no solution return RUS;                 }else{Stack.push (Strings[i]);         }} return Stack.pop (); }/* Custom arithmetic Rule */Public String Calculate (string x, string y, String ch) {} Note: Arithmetic procedure Code is more, does not show    。 4. File operation and expression check-weight code does not show

Iv. Display of results
Command selection:

An expression document:

Answer Document:

Answer Document:

Results Document:

10,000 Test Questions:

V. Summary of the Experiment
The main point of this programming is to deal with the expression, the focus is on the score, with the processing of fractions, the specific solution is to extract each of its parts of the integer, stored in several parameters, through the conversion between the parameters to achieve the calculation of the score, so as to achieve the expression calculation. There are quite a few problems encountered in the process, such as the divisor is 0,6÷ (3-3), the solution is to calculate the division, the judgment, if the divisor of 0 directly return the result "no solution".

VI. PSP Table

  PSP2.1 Personal software Process Stages time Senior Student time planning plan                            2 2 Estimate Estimate how much time this task will take. Development development Analysis requirements (including learning techniques) 2 1 Des                        IGN Spec Generation design Document 1 1 design Review review 2                           1 Coding Standard Code specification 0 0 Design specific                          3 4 Coding Specific codes Review code review 2 1 Test Tests (self-test, modify code, Commit changes 2 4 Re Porting Report 1 2  

Vii. Source Code
Code Cloud Project Address: https://gitee.com/liangs96_master/FourOperations

Four Operational Program (Java console-based)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.