Today, I saw an expression of an expression, I remembered that the data structure teacher made us write an expression calculated when the data structure class was not written. I feel that it is an angry.
/ * ------------------- Expression Caculator ---------------- Method: Continued operators into follow-up: 23 56 / (102-100) * ((36-24) / (8-6)) Convert: 23 | 56 | 102 || - | / | * | 36 | 24 | - | 8 | 6 | | / | * | The subsequent expressions on the stack are reversed. Take the figure on the top of the stack in another stack. If you encounter an operator, use it to calculate the first two numbers of the digital stack. Algorithm reference: 9CBS-VBPROG column - string expressions Calculate C # program Node: Do not support more than two '-' continuous; do not support one yuan operator ('-a', '! A' is converted by converting " (0-a) ',' (0! A) 'is calculated); not supported by an operator greater than one, does not support function; return type does not meet the biggest principles (all float type); support the following operators: , -: Add or subtraction operation; &, |: logic and or; *, /,%: Multiple breaks (where the rest is converted into long);!, ^: Logic is not multiplied (,): The operator priority above parentheses is low to high. Overall rating: Due to the unsupported function, the expansion function is limited; there is no object-oriented technology, it is not conducive to management and optimization; because the priority function, the calculator is separated, the binary operator is very Easy to expand. Summary: Inspector, analyzer, calculator, conver design is classified (or calculator to take a function pointer, using the overload operator of the library); the operator, the operand is set to class; correct the above node and evaluation Disadvantages and errors. * /
#include
Struct Err {string disc; err (const string & s) {disc = "er>" s;}}; // custom exception type
INT getPro (CHAR S) {Case ' ': Case '-': Return 1; Case '&': Case '|': Return 2; Case '*': Case '/': Case ' % ': Return 3; Case'! ': Return 4; Case' ^ ': Return 5; default: return -1;}} // getPro: Get prorioty of operatrs. Return -1 if s isn't an operators float Cacutwo (Const Float & Fa, Const Float & FB, Const Char & Oper) {Switch (OPER) {CASE ' ': Return Fa FB; Case '-': Return FA-FB; Case '&': Return Fa && Fb; Case ' |: Return Fa || FB; Case '*': Return Fa * FB; Case '/': Return Fa / FB; Case '%': Return long (fa)% long (fb); case '!': Return! FB; Case '^': Return Pow (Fa, FB); Default: Return 0;}} // Calculator: Calculate the calculation of two numbers.
INT ChecKexp (String & S) {Try {Int Dot; // There is more than one decimal point INT Br = 0; // Brand Check INT BRS = -1, BRE = -1; // Brand Start and end label (separate respectively Point to the left bracket and right brackets) int NS = -1, ne = -1; // Digital start and end label (point to the start and end of a number) FOR (INT i = 0; i
IF (ns> 0) {// Check '-' and '!' single operator, and replace it into corresponding binocular IF (S.AT (NS-1) == '-') {IF (NS > 1) {IF (S.AT (NS-2) == '(' || getPro (S.AT (NS-2))> 0) {S.Replace (NS-1, 2 NE-NS, "(0 -" S.SUBSTR (NS, NE-NS 1) ")") "); i = 3;}} else {s.replace (0, 2 NE-NS," (0 - " S.SUBSTR (NS, NE-NS 1) ")") "); i = 3;}} f (s.at (ns-1) == '!') {IF (ns> 1) {IF ( S.AT (NS-2) == '(' || getPro (S.AT (NS-2))> 0) {S.Replace (NS-1, 2 NE-NS, "(0!" S.SUBSTR (NS, NE-NS 1) ")") "); i = 3;}} else {s.replace (0, 2 ne-ns," (0! " S.SUBSTR (NS, NE-NS 1) ")"); i = 3;}}}. } IF (s.at (i) == '(') {// Check the left bracket
Br ; BRS = I; if (i> 0) {IF (isdigit (S.AT (i))) throw Err (S.AT (I) "'(' error!");} Continue;} IF S.at (i) == ')') {// Check the right bracket br -; IF (br) throw Err ("parentheses does not match!"); BRE = i; if (BRS> 0) { // Check the '-' and '!' Single operator, and replace it into the corresponding binocular IF (S.AT (BRS-1) == '-') {if (BRS> 1) {IF (S .at (BRS-2) == '(' || getPro (S.AT (BRS-2))> 0) {S.Replace (BRS-1, 2 BRE-BRS, "(0 -" S .SUBSTR (BRS, BRE-BRS 1) ")") ")"); i = 3;}} else {s.replace (0,2 breed "(0 -" S.SUBSTR (BRS, BRE -BRS 1) ")"); i = 3;}}} f (s.at (brs-1) == '!') {if (brs> 1) {if (S.AT (BRS-2 ) == '(' || getPro (S.AT (BRS-2))> 0) {S.Replace (BRS-1, 2 B RE-BRS, "(0!" S.SUBSTR (BRS, BRE-BRS 1) ")") "); i = 3;}} else {s.replace (0, 2 BRE-BRS," ( 0! " S.Substr (BRS, BRE-BRS 1) ") ")") "); i = 3;}}} CONTINUE;}} IF (br! = 0) throw Err (" Brand does not match! ") Return 1;
} catch (err e) {CERR << E.DISC << Endl; Return 0;}} //} / * int Atofx (const string & t, float & s) {try {ife (t.length () == 0) RETURN 0; int Dot = 0, BGR0 = 0; for (int I = 0; i
OPERANDS.PUSH (Operbuf); if (i == T.Length ()) Break; I -;} // DILL NUMBERS IF (T.at (i) == '(') {OPERS.PUSH (') ');} // dill' ('Operbuf = ""; if (GetPro (T.at (i))> 0) {if (OPERS.SIZE ()> 0) {if (OPERS.TOP () == '(' || getPro (OPERS.TOP ())
Operands) {Try {Float Numa, Numb; Stack