20172315 "Java Programming" course pair programming exercises _ Arithmetic second-week phase summary

Source: Internet
Author: User
Tags arithmetic

Related process infix suffix and suffix calculation test

Topic generation

Calculate the correct rate

Key code Explanation

Converts an infix expression to a suffix expression and evaluates the suffix expression

Import Java.util.stack;import Java.util.regex.pattern;public class Stringtoarithmetic {public stringtoarithmetic () { }//computes the infix expression string to get the result public static double stringtoarithmetic (string string) {return suffixtoarithmetic (inf    Ixtosuffix (string)); }//Convert infix expression to postfix expression public static String Infixtosuffix (string exp) {stack<character> s = new stack<        Character> ();        string suffix = "" to output the suffix expression strings; 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);                Switch (CH) {case ": break;                    Case ' (': S.push (CH);                Break                        Case ' + ': Case '-': while (s.size ()! = 0) {temp = S.pop ();                    if (temp = = ' (') {s.push (');        Break                    } suffix + = temp;                    } s.push (CH);                Break                        Case ' x ': Case ' ÷ ': while (s.size ()! = 0) {temp = S.pop ();                            if (temp = = ' + ' | | temp = = '-' | | temp = = ' (') {s.push (temp);                        Break                        } else {suffix + = temp;                    }} s.push (CH);                Break                        Case ') ': while (!s.isempty ()) {temp = S.pop ();                        if (temp = = ' (') {break;                        } else {suffix + = temp;                }} break; Default:suffix + = ch;                   Break        }} while (S.size ()! = 0) {suffix + = s.pop ();    } return suffix; } public static double suffixtoarithmetic (String exp) {Pattern pattern = pattern.compile ("\\d+| |        (\\d+\\.\\d+) ");        String[] strings = Exp.split ("");        stack<double> stack = new stack<double> ();            for (int i = 0; i < strings.length; i++) {if (Strings[i].equals ("")) {continue;            } if (Pattern.matcher (Strings[i]). Matches ()) {Stack.push (double.parsedouble (strings[i)));                } else {Double y = stack.pop ();                Double x = Stack.pop ();            Stack.push (Calculate (x, Y, strings[i]));    }} return Stack.pop ();        } private static Double calculate (double x, double y, string string) {//TODO auto-generated method stub            if (String.Trim (). Equals ("+")) {return x + y;        } if (String.Trim (). Equals ("-")) {return x-y;        } if (String.Trim (). Equals ("X")) {return x * y;        } if (String.Trim () equals ("÷")) {return x/y;    } return (double) 0; }

Create topic class

    public class Createquestions {public static string create (Int. level) {String add = "+", sub = "-", multi =        "x", div = "÷";        String result= "";        int r1= (int) (Math.random () *9+1);        int r2= (int) (Math.random () *9+1);        int r3= (int) (Math.random () *9+1);        three integers int fenzi1= (int) (Math.random () *9+1);        int fenmu1= (int) (Math.random () *9+1);        String fenshu1=fenzi1+ "/" +FENMU1;        int fenzi2= (int) (Math.random () *9+1);        int fenmu2= (int) (Math.random () *9+1);        String fenshu2=fenzi2+ "/" +FENMU2;        int fenzi3= (int) (Math.random () *9+1);        int fenmu3= (int) (Math.random () *9+1);        String fenshu3=fenzi3+ "/" +FENMU3;        Three score int suiji1= (int) (Math.random ());        The first operator int suiji11= (int) (Math.random ());        generates parentheses int suiji2= (int) (Math.random ());                The second operator if (level>=1) {if (suiji11==0&&level==2&&suiji1!=0&&suiji1!=1) result+="(";            if (level==3) result+=fenshu1;            else RESULT+=R1;            if (Suiji1 = = 0) result+=add;            if (suiji1 = = 1) result+=sub;            if (suiji1==2) Result+=multi;            if (suiji1==3) Result+=div;            if (suiji11==1&&level==2&&suiji2!=0&&suiji2!=1) result+= "(";            if (level==3) result+=fenshu2;            else RESULT+=R2;        if (suiji11==0&&level==2&&suiji1!=0&&suiji1!=1) result+= ")";            } if (level>=2) {if (suiji2==0) Result+=add;            if (Suiji2 = = 1) result+=sub;            if (suiji2==2) Result+=multi;            if (suiji2==3) Result+=div;            if (level==3) result+=fenshu3; else RESULT+=R3;           if (suiji11==1&&suiji2!=0&&suiji2!=1) result+= ")";    } return result; }

Main class

Import Java.util.scanner;public class Calculate {public static void main (string[] args) {System.out.println ("output        The number of questions you want to generate ");        Scanner scan = new Scanner (system.in);        int Tishu = Scan.nextint ();        System.out.println ("Enter the level you want to generate the title (enter 1 or 2 or 3)");        int Dengji = Scan.nextint ();        Call a loop double answer;        int Zhengquelv = 0;        double[] Daan = new Double[tishu]; Switch (Dengji) {case 1:for (int i = 0; i < Tishu; i++) {String a = CRE                    Atequestions.create (1);                    String B = a + "=";                    System.out.print (b);                    Answer = scan.nextdouble ();                    Daan[i] = stringtoarithmetic.stringtoarithmetic (a);                if (answer = = Daan[i]) zhengquelv++;            }break;            Case 2:for (int i = 0; i < Tishu; i++) {String a = createquestions.create (2);        String B = a + "=";                    System.out.print (b);                    Answer = Scan.nextint ();                    Daan[i] = stringtoarithmetic.stringtoarithmetic (a);                if (answer = = Daan[i]) zhengquelv++;            }break;                    Case 3:for (int i = 0; i < Tishu; i++) {String a = createquestions.create (3);                    String B = a + "=";                    System.out.print (b);                    Answer = Scan.nextint ();                    Daan[i] = stringtoarithmetic.stringtoarithmetic (a);                if (answer = = Daan[i]) zhengquelv++;        }break; } if (dengji<1| |        dengji>3) System.out.println ("Problem level input error");            if (Dengji >= 1&&dengji <= 3) {System.out.println ("answer is");      for (double Daanwei:daan) System.out.print (Daanwei + "");      SYSTEM.OUT.PRINTLN ("Correct rate for" + Zhengquelv + "/" + Tishu); }    }}
Difficulties encountered and their solutions

There are some minor problems in judging the correct rate of the problem.

After discussion in the group, the solution was obtained.

Write the part of the project that you are responsible for

The writing of the calculation class of infix suffix and suffix

Division of individual contribution (percentage contribution to each member of the group, contribution of each member to 100%)

20172315 Hu Zhihui 30%
20172312 Peng Lin 30%
20172318 Tai Yue 40%

Make a comment on a pair of small partners blog links (Focus on where to improve)

Peng Lin
Ludasui

Give the project a code cloud link to give the group pair programming photos

20172315 "Java Programming" course pair programming exercises _ Arithmetic second-week phase summary

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.