Expression in C ++ Bulid

zhaozj2021-02-16  52

// ******************************************************** *************************** //// - * / () arithmetic expression, function // calling Expcalc (Ansistring PEXPR, BOOL & PT) // Enter the PEXPR, BOOL type Pt; // If the expression is correct, Pt is true, and return to the calculation result / * Design thinking According to "Accord Priority Law", refer to Yan Weimin and Wu Weimin's data structure 45 algorithm;

* ///

#ifndef evcalach # define evcalach // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ --------------------------------- # include #include // Used STL Stack Using Namespace Std; Bool Findnumber (Ansistring PEXPR, INT POS, ANSISTRING & PNUMBER, INT & I) {/ **************************************** ************ Function: Remove the digital strings in an expression, PEXPR is an expression, POS is the starting position, pnumber is a digital string; success is returning True, no numbers String returns false;

Overall method: After setting a value string - 0123456789, each character and numerical string of the PEXPR will be determined; if it is placed in PEXPR; pay attention to -. (Indicating that the positive and negative symbol) can only appear. once;

************************************************** / ANSISTRING NUMSET = "- . 0123456789 "; // set the specific value of the numerical strings ansistring TEMS = PEXPR; i = pOS; int pad = 1; // Statistical addition and decrease; int PPOS; int PDEPO = 1; // Statistical decimal point; int Padnum = 1; int pDeponum = 2; pnumber = ""; // initial pnumber; int first = NumSet.pos (TEMS [I]); if (first == 1 || first == 2) Padnum = 2; if (first == 3) PDEPONUM = 2; while (ppos = NumSet.pos (TEMS [i]))> 0 && i <= Tems.length () && pad <= padnum && pdepo <= pdeponum) // When you find - Symbol appears less than 2 times {

IF (PPOS <3) // is - Symbol Location 1, 2 {PAD ; if (Pad> PadNum) Break;} if (PPOS == 3) //. Symbol 3 {PDEPO ; if (PDEPO> PDEPONUM) BREAK } Pnumber = pNumber TEMS [i]; // -. The symbol has secondary I ;} //

If (i == POS) // When the revelation position and the end position indicate that there is no value to be taken to return false; if (pNumber.Length () == 1 && numset.pos (pnumber [1]) <4) // " "OR" - "OR". "Return False; IF (pNumber.Length () == 2 && numset.pos (pnumber [1]) <3 && numSet.pos (pnumber [2]) == 3) //" . " or "-." RETURN FALSE; RETURN TRUE;

}

Int ReadSubstr (Ansistring PEXPR, Int Pos, Ansistring & Psubstr, Int & I, Bool & Cg, Int State)

{/ * ******************************************************* *************** * Return to 0 - indicate is a value, 1 - is the operator, 2 - indicates that the type of type that cannot be identified * State represents - Whether before the number Seeing as a positive or negative; * i is a non-strong success or not I = to the next word first position; ************************ *************************************** / ANSISUSSISTRING OPERTSET = "() - * / # "; ANSISTRING OPAD =" - "; Ansistring Tems = PEXPR; I = POS; PSUBSTR =" ""; cg = false;

IF (state == 0) {IF (FindNumber (TEMS, POS, PSUBSTR, I)) {cg = true; return 0;} psubstr = ""; i = pos; if (OpertSet.pos (TEMS [i]) > 0) {PSUBSTR = TEMS [I]; I ; cg = true; returnif 1;}} else {i = POS; if (OpertSet.POS (TEMS [I])> 0) {PSUBSTR = Tems [i]; i ; cg = true; return 1;} psubstr = ""; i = POS 1; IF (FindNumber (TEMBER (TEMBER (TEMS, POS, PSUBSTR, I)) {cg = true; return 0;}} i = POS 1; Return 2;

}

INT precede (Ansistring Opt1, Ansistring Opt2) {INT OOPOWER [7] [7] = {// -------------------------- / / / OPTR2 / / Accord Priority Table | // OPT1 / - * / () # | // ------------------------- ---- / * * / {2, 2, 0, 0, 0, 2, 2},

/ * - * / {2, 2, 0, 0, 0, 2, 2},

/ * * * / {2, 2, 2, 2, 0, 2, 2},

/ * / * / {2, 2, 2, 2, 0, 2, 2},

/ * (* / {0, 0, 0, 0, 0, 1, -1},

/ *) * / {2, 2, 2, 2, -1, 2, 2},

/ * # * / {0, 0, 0, 0, 0, -1, 1},};

Ansistring Opertset = " - * / () #"; int x = OpertSet.pos (OPT1); int y = OPERTSET.POS (OPT2); Return OOPOWER [X-1] [Y-1];}

/ / -------------------------------------------------------------------------------------------- ----------------------------

/ * Calculate the value of A B, A-B, A * B, A / B

* / float operate (float a, ansistring Oper, float b) {ANSISTRING OPERTSET = " - * /"; switch (OpertSet.pos (Oper)) {Case 1: Return A B;

Case 2: Return A-B;

Case 3: Return A * B;

Case 4: Return A / B;

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

/ ******************************************* In the case of PEXPR, Bool type PT; if the expression is correct, Pt is true, and return to the calculation result ***************************************** *************** / FLOAT EXPCALC (ANSISTRING PEXPR, BOOL & PT) {Pt = true; Ansistring Opertset = " * / () #"; Ansistring Tems = PEXPR "#" Stack Optr; Stack opnd; Ansistring Theta; Optr.push ("#"); Ansistring W; INT POS = 1; INT i; BOOL CG = False; Int oplx = Readsubstr (Tems, POS, W, I, cg, 0); if (! pt) return -10000; int power; float a, b; while (/*! (W.POS ("#" )==1 && Optr.top (). POS ("#") == 1) && * / i <= Tems.Length () 1) {switch (oplx) {case 0: // "value" opnd.push (strtofloat (w)); POS = I; IF (i

Power = precede (Optr.top (), W);

Switch (Power) {case -1: //) (, #), (# there is Return-10000.00; case 0: // <"optr.push (w); POS = I; IF (i

OPLX = ReadSubstr (TEMS, POS, W, I, PT, 0); if (! pt) return -10000;} else {w = "#"; oplx = 1; i ;} Break; case 1: // " = "

Optr.pop (); POS = i; if (i

Else {w = "#"; oplx = 1; i ;}

Break; case 2: // ">" if (optr.empty ()) {Pt = false; returnium 1000;} Theta = optr.top (); optr.pop (); if (OPND.empty ()) { Pt = false; return 10000;} b = opND.TOP (); opnd.pop (); if (opnd.empty ()) {pt = false; returno 10000;} a = opND.TOP (); opnd.pop (); Opnd.push (Operate (A, Theta, B));

POS = i;

Break;

}

Break;

}

Case 2: // is unrecognizable; return-10000;}

} Pt = true; return.com ();

#ENDIF

转载请注明原文地址:https://www.9cbs.com/read-24058.html

New Post(0)