On the preliminary realization of arithmetic operation

Source: Internet
Author: User
Tags greatest common divisor

I. Achieving the Goals

    1. Randomly generate the various formulas required by the user, and the calculation is not repeated.
    2. The value range is freely entered by the user.
    3. A calculation consists of multiple data types, including integers, fractions, and real numbers.
    4. Optional multiplication method.
    5. You can choose whether to include parentheses.

Two. Some conditions for software assumptions

    1. Users can clearly understand the concept of positive and negative numbers, true scores and the concept of false scores and other basic mathematical knowledge, there will be no user selected the true score of a given data range of [2,3] such errors.
    2. Assuming that the software is not used for high-end math competitions, the denominator is set to a maximum of 30.

Three. Design ideas

The overall process is the user first given the requirements, then the program to determine the legitimacy of the requirements, for the legitimate needs, Sir into the operand, and then generate the operator, after the weight is combined into a string output. Discuss each of the requirements categories first:

    1. Multiplication and division, due to operator uncertainty, the operator and operand processing is clearly more clear, just for the two cases with or without multiplication different operator random. It is also necessary to randomly vary the number of operators for the number of operands.
    2. Parentheses, it is clear that the parentheses of the two operands are meaningless, whereas for the operators of the three operands, it is meaningless to enclose the sub-type with high precedence, so the parentheses range is determined.
    3. Fractions, the denominator is randomly constructed (the denominator range is [1,30], then the same), and then a real number a from 0 to 1 is created with the mechanism, and the resulting formula for the numerator is "numerator =[b* denominator" ([x] is the x integer part) According to the data range that the user gives to B. Assuming that the user is given an interval of [L,r], where L and R are real numbers, then the scaling method for real A is b=a* (r-l) +l.
    4. Decimals, also using the method of the reduction of the random real number processing a, and then according to the user input after the decimal point, the real number is processed, namely a=[(int) (A * (10^k))]*1.0/(10^k).

Four. Interface design

  Individuals like simple and clear style, so the interface is only two a five cents special effects window. The initial interface is specific as follows:

Calculation Selection Interface:

Of course, is the edit interface in VS, the real interface in the absence of a decimal number when the right two of the space is Visable=false, the same score.

Five. Results test

30 different formulas are generated for each of the different situations, and only the first 10 are intercepted here.

1. Integer [ -5,9].

            

2. False score [1,6]+ multiplication

          

3. True score [ -0.6,0.5]+ brackets

          

4. Decimal [ -23,26.4]+ two digits + multiplication + brackets

          

Six. Implementing the Code

  1. The user selects the interface code, mainly contains to judge the demand legitimacy and the various needs marking processing and so on.

     Public double MIN, MAX;        public bool Mulandde, bracket, fraction,decimals;        public int frac,dec;            Public Form2 () {InitializeComponent ();            Groupbox1.visible = false; Label3.            Visible = false;            Textbox3.visible = false;            Mulandde = Bracket = Fraction = decimals = false;            Frac = 0;        Dec =-1;            private void Checkbox4_checkedchanged (object sender, EventArgs e) {if (checkbox4.checked)                {groupbox1.visible = true;            fraction = true;                } else {groupbox1.visible = false;            fraction = false; }} private void Checkbox5_checkedchanged (object sender, EventArgs e) {if (checkbox5.chec ked) {label3.                Visible = true;                Textbox3.visible = true;            Decimals = true;       }     else {label3.                Visible = false;                Textbox3.visible = false;            decimals = false;  }} private void Button1_Click (object sender, EventArgs e) {//min MAX string s            = TextBox1.Text; if (s.length = = 0) {MessageBox.Show ("Fill in error, please re-fill", NULL, MessageBoxButtons.OK, MESSAGEBOXICON.WA                rning);            Return            } MIN = Double.Parse (s);            s = TextBox2.Text; if (s.length = = 0) {MessageBox.Show ("Fill in error, please re-fill", NULL, MessageBoxButtons.OK, MESSAGEBOXICON.WA                rning);            Return            } MAX = Double.Parse (s); Fraction if (fraction && frac = = 0) {MessageBox.Show ("Fill in error, please re-fill", NULL, M                Essageboxbuttons.ok, messageboxicon.warning);            Return        }//decimals if (decimals) {        s = textbox3.text; if (s.length = = 0) {MessageBox.Show ("Fill in error, please re-fill", NULL, MessageBoxButtons.OK, Messagebo                    xicon.warning);                Return } for (int i = 0; i < s.length; i++) if (S[i] < ' 0 ' | | s[i] > ' 9 ') {MessageBox.                    Show ("Fill in error, please re-fill", NULL, MessageBoxButtons.OK, messageboxicon.warning);                Return } dec = Int.            Parse (s); } this.        Close ();  private void Radiobutton1_checkedchanged (object sender, EventArgs e) {if (radiobutton1.checked)        Frac = 1; private void Radiobutton2_checkedchanged (object sender, EventArgs e) {if (radiobutton2.checked        ) Frac = 2;        } private void Checkbox1_checkedchanged (object sender, EventArgs e) {mulandde =!mulandde; } private void Checkbox2_checkedchanGed (object sender, EventArgs e) {bracket =!bracket; }

2. The main interface code, mainly the generation and printing of operands and operators.

        const int maxn=30;        Form2 form;        string[] equation;        double[][] num;        String[][] Cnum;        Char[][] op;            Public Form1 () {InitializeComponent ();            Equation = new STRING[MAXN];            num = new double[maxn][];            for (int i = 0; i < MAXN; i++) num[i] = new DOUBLE[3];            OP = new char[maxn][];            for (int i = 0; i < MAXN; i++) op[i] = new CHAR[2];            Cnum=new string[maxn][];        for (int i = 0; i < MAXN; i++) cnum[i] = new STRING[3]; } private void _sort (int ith) {if (Num[ith][0] < num[ith][1]) {Double t = num [ITH]                 [0]; Num[ith][0] = num[ith][1];            NUM[ITH][1] = t;                } if (num[ith][2]! = 0 && num[ith][1] < num[ith][2]) {Double t = num[ith][1]; NUM[ITH][1] = num[ith][2];            NUM[ITH][2] = t;  }          if (Num[ith][0] < num[ith][1]) {Double t = num[ith][0]; Num[ith][0] = num[ith][1];            NUM[ITH][1] = t; }}//generate the topic private void Button1_Click (object sender, EventArgs e) {while (listBox1 .            Items.Count! = 0) ListBox1.Items.Remove (listbox1.items[0]);            Modal form random r=new random ();            form = new Form2 (); Form.            ShowDialog ();            int top = 2;            if (form.mulandde) top = 4;                    for (int i = 0; i < MAXN; i++) {//constructs different operands while (true) {                    Build_equ (i);                    _sort (i);                    BOOL Flag=true; for (int j = 0; J < i; j + +) if (num[i][0] = = Num[j][0] && num[i][1] = num[j][1] && num[i][2] = = Num[j] [2])                    {flag = false; } if (flag) BreaK                }//construction operator int tt = R.next (top);                    Switch (TT) {case 0:op[i][0] = ' + '; Case 1:op[i][0] = '-';                    Break Case 2:op[i][0] = ' * ';                    Break Case 3:op[i][0] = '/';                Break                    } if (num[i][2]! = 0) {TT = R.next (top);                        Switch (TT) {case 0:op[i][1] = ' + '; Case 1:op[i][1] = '-';                        Break Case 2:op[i][1] = ' * ';                        Break Case 3:op[i][1] = '/';                    Break                }} else {op[i][1] = '; } if (Form.bracket&&num[i][2]!=0&&r.next (3) ==1) {Char A                    = Op[i][0], B = op[i][1]; if ((A = = ' + ' | | a = = '-') && (b = = ' + ' | | b = = '-')) | |                    (A = = ' * ' | | a = = '/') && (b = = ' * ' | | b = = '/')))                        {Equation[i] = "" + cnum[i][0] + "" + Op[i][0] + "(" + cnum[i][1 ");                        Equation[i] + = "+ op[i][1] +" "+ cnum[i][2];                    Equation[i] + = ") =";                            } else {if (a = = ' + ' | | a = = '-')} {                            Equation[i] = "(" + cnum[i][0] + "" + op[i][0] + "" + cnum[i][1];                            Equation[i] + = ")" + op[i][1] + "" + cnum[i][2];                        Equation[i] + = "="; } else {Equation[i] = "" + cnum[i][0] + "" + Op[i][0] + "(" + cnum[i                            ][1];                            Equation[i] + = "+ op[i][1] +" "+ cnum[i][2];                        Equation[i] + = ") =";            }                    }    } else {Equation[i] = "" + cnum[i][0] + "" + op[i][0] + "" + cnum[                    I][1];                    if (cnum[i][2]! = "") {Equation[i] + = "" + op[i][1] + "+ cnum[i][2];                } Equation[i] + = "="; }}} private void Button2_Click (object sender, EventArgs e) {for (int i = 0; i < MAXN;                    i++) ListBox1.Items.Add (Equation[i]); }//Construct the function of the first POS calculation operand private void build_equ (int pos) {//test Random R = new Ran            Dom (); Double L=form. Min,r=form.            MAX; if (form.fraction) {if (Form.frac = = 1) {//true fraction int F                    Z1, FZ2, FM1, FM2;                    Double T;                    FM1 = R.next (29) + 2;                    FM2 = R.next (29) + 2;t = r.nextdouble () * (r-l) + L;                    Num[pos][0] = t;                    FZ1 = (int) (FM1 * t);                    t = r.nextdouble () * (r-l) + L;                    FZ2 = (int) (FM2 * t);                    int _gcd = GCD (FZ1, FM1);                    FZ1/= _GCD;                    FM1/= _GCD;                    _GCD = gcd (fz2, FM2);                    FZ2/= _GCD;                    FM2/= _GCD;                    NUM[POS][1] = t;                    if (fm1<0) cnum[pos][0] = "-" + fz1 + '/' + (-FM1);                    else cnum[pos][0] = "" + fz1 + '/' + FM1;                    if (fm2<0) cnum[pos][1] = "" + fz2 + '/' + (-FM2);                    else cnum[pos][1] = "" + fz2 + '/' + fm2;                        if (R.next (2) = = 1) {FM1 = R.next (19) + 2;                        t = r.nextdouble () * (r-l) + L; Num[POS]                        [2] = t;                        FZ1 = (int) (FM1 * t);                        _GCD = gcd (FZ1, FM1);                        FZ1/= _GCD;                        FM1/= _GCD;                    CNUM[POS][2] = "" + fz1 + '/' + FM1;                        } else {num[pos][2] = 0;                    CNUM[POS][2] = "";                    }} else {//false score int fz1, FZ2, FM1, FM2;                    Double T;                    FM1 = R.next (19) + 2;                    FM2 = R.next (19) + 2;                    t = r.nextdouble () * (r-l) + L;                    Num[pos][0] = t;                    FZ1 = (int) (FM1 * t);                    t = r.nextdouble () * (r-l) + L;                    FZ2 = (int) (FM2 * t);                    NUM[POS][1] = t;                    int _gcd = GCD (FZ1, FM1);                    FZ1/= _GCD;         FM1/= _GCD;           _GCD = gcd (fz2, FM2);                    FZ2/= _GCD;                    FM2/= _GCD;                    if (FM1 < 0) Cnum[pos][0] = "-" + fz1 + '/' + (-FM1);                    else cnum[pos][0] = "" + fz1 + '/' + FM1;                    if (FM2 < 0) cnum[pos][1] = "" + fz2 + '/' + (-FM2);                    else cnum[pos][1] = "" + fz2 + '/' + fm2;                        if (R.next (2) = = 1) {FM1 = R.next (19) + 2;                        t = r.nextdouble () * (r-l) + L;                        NUM[POS][2] = t;                        FZ1 = (int) (FM1 * t);                        _GCD = gcd (FZ1, FM1);                        FZ1/= _GCD;                        FM1/= _GCD;                    CNUM[POS][2] = "" + fz1 + '/' + FM1;         } else {num[pos][2] = 0;               CNUM[POS][2] = ""; }}} and else if (form.decimals) {//decimal double                A, B;                int d = 1;                for (int i = 1; I <= form.dec; i++) d *= 10;                A = R.nextdouble () * (r-l) + L;                b = r.nextdouble () * (r-l) + L;                Num[pos][0] = A;                NUM[POS][1] = b;                A = (int) (A * d);                b = (int) (b * d);                Cnum[pos][0] = "" + (A/d);                CNUM[POS][1] = "" + (b/d);                    if (R.next (2) = = 1) {a = R.nextdouble () * (r-l) + L;                    NUM[POS][2] = A;                    A = (int) (A * d);                CNUM[POS][2] = "" + (A/d);                    } else {num[pos][2] = 0;                CNUM[POS][2] = ""; }} else {//wholenumber int A, b;                A = (int) (R.next (int) (R-L)) + L);                b = (int) (R.next (int) (R-L)) + L);                Num[pos][0] = A;                NUM[POS][1] = b;                Cnum[pos][0] = "" + A;                CNUM[POS][1] = "" + B;                    if (R.next (2) = = 1) {a = (int) (R.next (int) (R-L)) + L);;                    NUM[POS][2] = A;                CNUM[POS][2] = "" + A;                    } else {num[pos][2] = 0;                CNUM[POS][2] = ""; }}} private int gcd (int a, int b) {//Beg A and B greatest common divisor int r            = 1;                while (r!=0) {r = a% B; A = b;            b = r;        } return A; }

Seven. Some impressions after the project

Before studying in school is to write some specific topics, its purpose and scope are very strong, and the problem is single, and this project is only a arithmetic, but when many small and simple problems linked together will become complex, the analysis of the problem will become difficult, The various constraints in the beginning of the abstraction of the problem brought to their own no small trouble, these troubles are not unexpected ways, but do not know how to combine certain methods. Therefore, for the software engineering this subject has a deeper understanding of the current cognition is not the core of the project to solve a specific technical problem, but the reasonable coordination of user requirements, and establish a convenient analysis maintenance problem model. Have not pointed out also ask the teacher to correct ~ ~

On the preliminary realization of arithmetic operation

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.