Modern Software Engineering _ the first week of practice _ the 1th question _

Source: Internet
Author: User
Tags gcd greatest common divisor

The first problem is the requirement to implement a program that automatically generates subtraction arithmetic topics for pupils. You can extend it later as a website or Android app or iOS app or Win10 app.

My thinking is quite simple. The environment is Java jdk1.8;ide for IntelliJ Community Edition.

First, we do not consider the case of parentheses, then the symbol is only + 、-、 *,/four; the number involved consists of two types: integers or fractions.

1. We note that in an expression, there is always a rule that "number of numbers is more than operator 1". Thus, it is natural to think of creating an array with a fixed length of n to hold numbers, each of which can be randomly generated, controlling the maximum number of occurrences in the equation by Maxofnumber variables. Create an array of length n-1 character to hold the operation symbol.

2. The next step is to initialize the array number and character. Number array Each element is controlled by Maxofnumber maximum, Minofnumber control minimum, hasfraction control whether a fractional random generation occurs; character array each element is controlled only by the type variable, only the multiplication, There are 3 types of subtraction that are randomly generated.

For example: N is 5,minofnumber for 0,maxofnumber and 10,hasfraction for the occurrence of fractions, type is only for addition and subtraction: 5/7+8/7-1/7-2/7

3. The next step is the calculation of the results. For pure integers, the result is simple. By looking up the data, I found that the calculation of pure integer strings depended on the ScriptEngine class to complete. Examples of function use are:

1     //integer expression evaluates to the correct result 2 public      String Calintresult () {3         scriptengine se = new Scriptenginemanager (). Getenginebyname ("JavaScript"); 4         try {5             double result = (double) se.eval (Formular); 6             return< c10> string.valueof (Result.doublevalue ()); 7         } catch }10 return null;      

However, for fractions, this method is not possible. For example, 1/5+2/3 such a formula, by string rendering certainly does not work. Rely on the original mathematical solution method, realize the need to use greatest common divisor and least common multiple completion. Example: the least common multiple of 12 and 8 are obtained. The greatest common divisor of 12 and 8 are 4,12x8÷4=24, so the least common multiple of the two numbers is 24. The following is a simple implementation of the fractional subtraction method.

1 public classfraction {2 static int numera = 0; 3 static int Deomina = 0; 4 5 public static voidMain (string[] args) {6 int a1 = 9; 7 int a2 = 10; 8 int B1 = 3; 9 int b2 = 11, New Fraction (). Fracadd (A1,A2,B1,B2);//Result: 11/20 System.out.println ("Fractional addition operation:" +numera+ "/" +Deomina), new fraction (). Fracsub (A1,A2,B1,B2);//Fractional subtraction System.out.println ("Fractional subtraction:" +numera+ "/" +Deomina); fraction (). Fracmul (A1,A2,B1,B2);//score multiplied by System.out.println ("Fractional multiplication:" +numera+ "/" +Deomina), fraction (). Fractdiv (A1,A2,B1,B2);//Fractions divided by System.out.println ("fractional division operation:" +numera+ "/" +Deomina); 18}19 20//define fractional addition functions of public void Fracadd (int first_numerator, int first_denominator, int second_numrator, intSecond_denominator) {intLCM, gcd;23 LCM = LCM (First_denominator,second_denominator),//Call for least common multiple function least common multiple Numera = (lcm/first_denominator) *first_numerator+ (lcm/second_denominator) * second_numrator;//non-degenerate molecule and Deomina = LCM; The denominator of the non-simplification is gcd = gcd (Numera,deomina); Need to call the function of greatest common divisor numera = NUMERA/GCD; After the reduction of molecular Deomina = DEOMINA/GCD; The denominator after simplification 29}30 31//define fractional subtraction function, public void fracsub (int first_numerator,int first_denominator,int Second_numrator,intSecond_denominator) {intLCM, gcd;34 LCM = LCM (First_denominator,second_denominator),//need to call least common multiple function for least common multiple Numera = (lcm/first_denominator) *first_numerator-(lcm/second_denominator) * second_numrator;//The molecular difference of the non-simplification of Deomina = LCM; The denominator of the non-degenerate GCD = gcd (Numera,deomina); Need to call the function of greatest common divisor numera = NUMERA/GCD; The molecular deomina after simplification = DEOMINA/GCD; The denominator after simplification 40}41 42//define fractional multiplication function Fracmul public void (int first_numerator,int first_denominator,int Second_numrator,intSecond_denominator) {intZ, m, gcd;45 z = first_numerator *Second_numrator; First_denominator m = *Second_denominator; GCD =GCD (Z,M); Numera = z/gcd Deomina = m/gcd 50}51 52//Define Fractional division function fractdiv public void (int first_numerator,int first_denominator,int Second_numrator,intsecond_denominator) {Si intZ, A, m, gcd;55 a =Second_denominator; Second_denominator =Second_numrator; Second_numrator = A; z = first_numerator *  second_numrator; m = first_denominator *  second_denominator; GCD =
                                     
                                       gcd (z,m); Numera = z/
                                       gcd; Deomina = m/ gcd;}64  Greatest common divisor 65//Request for the public static int gcd (int m,int  n {int i = 2;//define integer variable I, which is the loop variable, the value of the int  g, min; min = m>n?  n:m;70 g = 1;//Greatest common divisor initial value 171 while (I <= min)//Judging condition, which has been circulating to the smaller of the two number of the end of the  {mm-i = 0 && n% i = = 0 ) at a time  {i;7 m = m/ 6 n = n/ i;77 g = g *  i;78 } i++ ; + } Bayi return  g; }83 84//Request least common multiple function 85 public static int LCM (int m,int  n) {Retu int  g, l; g = gcd (m,n);//call for greatest common divisor function, L = m * N/ G; RN  l; * } * *             
                                     

4. The automatic generation of integers and fractions is OK, and the method of calculating results is also realized. Put the specific code for the other class below. This code, together with the above code, implements the requirements of topic one.

1 public classAutoformula {2 static String Formular = newString (); 3 static String result = newString (); 4 intnumerator; 5 intdenominator; 6 7 public static voidMain (string[] args) {8/* 9 * parameter mode is symbolic, 0 is plus and minus, 1 is multiplication, 2 is subtraction, 10 * parameter hasfraction is the control of the number of points, 11 * parameter numofcharacter is the number of symbols; 12 * Reference The number minofnumber is the smallest value appearing in the equation; 13 * The Maxofnumber of the parameter is the largest value appearing in the equation; */New Autoformula (). Generate (0, 1, 3, 0, 10); 16System.out.println (Formular); 17SYSTEM.OUT.PRINTLN (result); 18} public void generate (int mode, int hasfraction, int numofcharacter, int minofnumber, intMaxofnumber) {Numofnumber int = numofcharacter + 1; string[] character = newString[numofcharacter]; string[] Number = newString[numofnumber]; StringBuilder StringBuilder = newStringBuilder (); 26 27//Initialize symbol array for (int i=0; i<character.length; i++) {Character[i] =Generatecharacter (mode); 30} 31//Initialize array of numbers for (int i=0; i<number.length; i++) {Number[i] =Generatenumber (Hasfraction, Minofnumber, Maxofnumber); 34} 35//link symbol and digital stringbuilder.append (number[0]); PNS for (int i=0; i<character.length; i++) {Stringbuilder.append (Character[i] + number[i+1]); 39} Formular =Stringbuilder.tostring (); if (hasfraction = = 0) {result =Calintresult (); ()} else{result =Calfractionresult (number, character); 46} 47} 48 49 50//3 Random Generation mode, random generation of addition, subtraction, multiplication, subtraction symbol is public String generatecharacter (intmode) {Random random = newJava.util.Random (); intI Switch(mode) {0://+-Random.nextint (2); (i==0)? "+" : "-"; 1 case://*/i = Random.nextint (2); Return (i==0)? "*" : "/"; 2 case://+-*/i = Random.nextint (4); (i==0)? "+": (i==1)? "-": (i==2)? "*" : "/"; 67} to return null; 69} 70 71 72//2 random generation mode, randomly generated integer, fractional generatenumber public String (int mode, int min, intMax) {Random random = newJava.util.Random (); The switch(mode) {Case 0: 77//randomly generated integer with int num = min +Random.nextint (max); (num = = 0) {//integers 0 too simple num = min +Random.nextint (max); 81} The returnString.valueof (num); Case 1: 84//randomly generated fractional-intnumerator; intdenominator; The numerator do {//numerator is 0 too simple, the denominator cannot be 0 or 1, the numerator denominator cannot be equal to + = min +Random.nextint (max); denominator = min +Random.nextint (max); Numerator = = 0 | | denominator = = 0 | | denominator = = 1 | | denominator = =numerator); String.valueof return (numerator) + "/" +String.valueof (denominator); 92}, return null; 94} 95 96 97//The correct calculation result of an integer expression 98 publicString Calintresult () {scriptengine se = new Scriptenginemanager (). Getenginebyname ("JavaScript"); try{101 Double result =(Double) Se.eval (Formular); 102 returnString.valueof (Result.doublevalue ()); 103} catch(Scriptexception e) {104E.printstacktrace (); 105}106 return NULL; 107}108 109 110///fractional expression Correct calculation result 111 publicString Calfractionresult (string[] number, string[] character) {Int. INTfirst_numerator;113 intfirst_denominator;114 intsecond_numrator;115 intsecond_denominator;116 Splitfraction (number[0]); 117 First_numerator =numerator;118 First_denominator =denominator;119 for (int i=0; i<character.length; i++) {Splitfraction (number[i+1]); 121 Second_numrator =numerator;122 Second_denominator =denominator;123 fraction fraction = newFraction (); 124 switch(Character[i]) {case "+": 126Fraction.fracadd (First_numerator, First_denominator, Second_numrator, second_denominator); 127 First_numerator =fraction.numera;128 First_denominator =fraction.deomina;129 breakThe case "-": 131 NewFraction (). Fracsub (First_numerator, First_denominator, Second_numrator, second_denominator); first_numerator =fraction.numera;133 First_denominator =fraction.deomina;134 break; 135 Case "*": 136 NewFraction (). Fracmul (First_numerator, First_denominator, Second_numrator, second_denominator); 137 First_numerator =fraction.numera;138 First_denominator =fraction.deomina;139 break"/": 141 NewFraction (). Fractdiv (First_numerator, First_denominator, Second_numrator, second_denominator); 142 First_numerator =fraction.numera;143 first_denominator = fraction.deomina;144 break; 145 }146 }147 148 if (first_nume Rator = = 0) {149 return "0"; ")" else if (first_numerator = = first_denominator) {151 return "1";} els E {153 return first_numerator + "/" + first_denominator;154 }155 }156 157 158//Divide fractions 159 public void splitfraction (String fraction) {if (Fraction.contains ("/")) {161 string[] Tmpnumber = Fraction.split ("/") c15>); 162 numerator = Integer.parseint (tmpnumber[0]); 163 denominator = Integer.parseint (tmpnumber[1]); 164} else {//integers are the same as direct numerator denominator 165 numerator = integer.parseint (fraction); 166 denominator = numerator;167 }168  }169                 

5. To do this, you may ask:

Why do you want to implement it in the Java language? Wouldn't it be easier to implement using the Python language?

A: Well, Python implementations are really simpler, but the last thing we need to do is to implement it as a website or app. If implemented as a Web site, the Java web based on Spring Mvc+spring+hibernate/mybatis has more powerful logical representations and a more coupled, more stable technology architecture. Java's strong object-oriented language features are also better able to help the team to strict code writing, the software engineering team for collaborative development, Python weak grammar features are not conducive to the specification code, in the later maintenance of more powerful features will inevitably be complex. PHP can only be used for website construction, and later is not conducive to the transition to Android applications, so it is not within our scope of consideration. NET can only be used for Microsoft server, this is for the love of Linux I ... Can't stand it.

Why do you want to use the IntelliJ IDE? Eclipse or MyEclipse, why don't you?

A: This question has been discussed thoroughly. The reason I like IntelliJ is that it's very well-developed code hinting, and even when you're writing CSS or JavaScript scripts, it has the syntax hints for HTML5 features, not to mention the ingenious hints of Java classes, packages, functions, and, in addition, the unconditional support of MAVEN, Gradle These Java project management tools, you must point to a capital of praise.

6. Since we are going to make this service a Web site, and it is based on the Java SSH Framework, then the front-end I use the bootstrap framework, easy to get started, for this project, enough.

The GitHub code hosting address for our team is: https://github.com/NorthWolives/. It contains the underlying expression generation code and the current Web site implementation of the code.

This is my front page simple implementation: http://server.malab.cn/PupilLearn/

The first page is as follows, hope the future children like:

Date: September 9, 2016

School of Computer Science and technology, Tianjin University

Modern Software Engineering _ the first week of practice _ the 1th question _

Related Article

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.