The first chapter "Introduction" of modern software engineering 1th--contestant, Zhang Gongcheng

Source: Internet
Author: User
Tags greatest common divisor

Step one: like Chiu, spend 20 minutes writing a command-line "software" that automatically generates arithmetic topics for primary schools , respectively, to meet the requirements below. The following requirements can be specified in the form of command-line arguments:

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):

Our first idea is to unify integers and fractions to create a number. It contains the numerator and denominator, and when the number is an integer, the denominator is 1, which facilitates subsequent operations. This allows us to reduce a division operation, leaving only addition, subtraction, and multiplication. We define the public functions in the class. The units we calculate later become classes.

classnumber{Private:    intnumerator;//molecule    intdenominator;//Denominator Public: Number () {} number (inta) {Numerator=A; Denominator=1; } number (intAintb) {Numerator=A; Denominator=b; }    voidPrin_number () {if(Numerator = =0) {printf ("0"); }        Else{            if(Denominator = =1) {printf ("%d", numerator); }            Else{printf ("%d/%d", numerator, denominator);        }}} number plus (number n1, number N2) {number re; intDivisor//Greatest Common DivisorRe.denominator= N1.denominator *N2.denominator; Re.numerator= N1.numerator * N2.denominator + n2.numerator *N1.denominator; Divisor=greatest_common_divisor (Re.denominator, re.numerator); Re.denominator= Re.denominator/Divisor; Re.numerator= Re.numerator/Divisor; returnre;        } number minus (number n1, number N2) {number re; intDivisor//Greatest Common DivisorRe.denominator= N1.denominator *N2.denominator; Re.numerator= N1.numerator * N2.denominator-n2.numerator *N1.denominator; if(Re.numerator! =0) {divisor=greatest_common_divisor (Re.denominator, re.numerator); Re.denominator= Re.denominator/Divisor; Re.numerator= Re.numerator/Divisor; }        returnre;        } number Mul (number n1, number N2) {number re; intDivisor//Greatest Common DivisorRe.denominator= N1.denominator *N2.denominator; Re.numerator= N1.numerator *N2.numerator; if(Re.numerator! =0) {divisor=greatest_common_divisor (Re.denominator, re.numerator); Re.denominator= Re.denominator/Divisor; Re.numerator= Re.numerator/Divisor; }        returnre; }};

After defining the class, we know that the arithmetic symbol is particularly much, so we are going to use the data structure of "stack" at this time. The following is the basic function of the "stack" data structure.

#include"stdafx.h"#include"iostream"using namespacestd; #include"Caculating.h"#defineStack_init_size 100#defineStackincrement 10#defineERROR 0#defineOK 1#defineOVERFLOW-2typedef number STATUS;TYPEDEFCharSelemtype;typedefstruct{Selemtype*Base; Selemtype*top; Selemtype stacksize;} Sqstack; Status Initstack (Sqstack&S) {S.Base=NewSelemtype[stack_init_size]; if(! S.Base){        returnERROR; } s.top= S.Base; S.stacksize=stack_init_size; returnOK;} Status GetTop (Sqstack S, Selemtype&e) {    if(S.top = = S.)Base)returnERROR; E= * (S.top-1); returnOK;} Status Push (Sqstack&S, Selemtype e) {    if(S.top-s.Base>=s.stacksize) {S.Base=NewSelemtype[stack_init_size +Stackincrement]; if(! S.Base)returnOVERFLOW; S.top= S.Base+s.stacksize; S.stacksize+=stackincrement; }    *s.top++ =e; returnOK;} Status Pop (Sqstack&S)    {Selemtype E; if(S.top = = S.)Base)returnERROR; E= *--S.top; returne;} Status Print (Sqstack S) {Selemtype*p =S.top;  while(P! = S.Base) {cout<< *--p; } cout<<Endl; returnOK;} Status Destroy (Sqstack&R) {Delete S.Base; S.top=NULL; S.Base=NULL; S.stacksize=0; returnOK;}
View Code

Then we have to use two stacks to do the operation, one stack is the stack, the other is the symbol stack. At this point, we first define the priority of the symbol.

intJudgement (Charch) {    Switch(CH) { Case'#':return 0; Break;  Case'+': Case'-':return 1; Break;  Case'*':return 2; Break;  Case'/':return 3; Break;  Case')':return 4; Break;  Case'(':return 5; Break; default:return 6; Break; }}

Modern software Engineering the first chapter "Introduction" 1th--contestant, Zhang Gongcheng

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.