99th high-programs, through the detailed note
/ *
http://lxleaves.126.com//>
#include
INT Add (int x, int y) {RETURN X Y;} / * Define Mathematical Functions * /
INT SUB (INT X, INT Y) {RETURN X-Y;
INT MUL (INT X, INT Y) {Return x * y;}
INT DIV (int x, int y) {return x / y;}
/ * Define function pointer array, easy to operate * /
Int (* func []) () = {add, sub, mul, div};
/ * Curch = Each current character value, NUM user enters a number of participating operations * /
Int Num, curch;
/ * Note and function pointer array corresponds to * /
CHAR chtbl [] = " - * / () =";
CHAR CORCH [] = " - * / () = 0123456789
/ * Function: Read a useful character, skip useless characters * /
Int getach ()
{
INT I;
While (1)
{
/ * Read the characters in the buffer in order
Curch = GetChar ();
/ * If the buffer returns -1 * /
IF (curch == EOF) return -1;
FOR (i = 0; corch [i] && curch! = corch [i]; i );
/ *
Pay attention to the end of the semicolon, thank Nobush () give me inspiration ^ - ^
! = Tap the priority
Curch! = CORCH [i] Judging whether the read value belongs to the Corch array
Assume that the input is '=':
I = 0, curch! = corch [0] is true
At this time, Corch [0] is not 0, for True, so
Corch [i] && curch! = Corch [i] is true, loop continues
...
i = 6, curch! = corch [0] is false
So Corch [i] && curch! = Corch [i] is false, cycle termination
The reason why Corch [i] && is to deal with false in the end of array end '/ 0', terminate the cycle
* /
IF (i / * At this time I should stop in the location of the input character (in the corch array) If the length of the i Otherwise, only one may be equal to the length of the Corch array, that is, the scanning to the Corch array has not found out Match the match, indicating that a unhappy character is entered, skip here, continue while loop That is to say, I found that the word read is stripped to Return Curch and returns the character read. * / } / * Returns its value if it is the correct character * / Return Curch; } / * Sorting operation symbols and numbers * / Int getId () { INT I; / * This is used to read a complete number, such as 1234, you need to read four times, then calculate Num * / IF (Curch> = '0' && curch <= '9') { For (NUM = 0; Curch> = '0' && curch <= '9'; getach ()) Num = NUM * 10 CURCH-'0 '; / * If a series of numbers are read, the digital value is in NUM * / Return -1; } ELSE / * Otherwise, operator * / { For (i = 0; chtbl [i]; i ) IF (chtbl [i] == curch) Break; / * Matching operator * / / * If not equal to the number (the equal sign is chtbl [6]), you will read the next content, to prepare for the next analysis * / IF (i <= 5) getach (); Return I; / * Return the calculation symbolic mark * / } } / * Calculation * / Int Cal () { INT X1, X2, X3, OP1, OP2, I; i = getId (); / * If i is '(', introducing new recursive, X1 is () value, otherwise x1 is directly equal to the number of read numbers * / IF (i == 4) x1 = CAL (); else x1 = Num; / * Nature is the operation operator after the number * / Op1 = getId (); / * If the operator is ')' and '=', on the one hand ')' Easy to return, on the other hand '=' can be transported End of calculation * / IF (OP1> = 5) Return X1; / * If it is not above, there is still a number after the operator, read * / i = getId (); / * In the same, it is convenient for the merge of multiple recursive results. * / IF (i == 4) x2 = CAL (); Else X2 = Num; Op2 = getId (); / * If it is the addition and subtraction symbol * / While (OP2 <5) { i = getId (); IF (i == 4) x3 = CAL (); else x3 = Num; / * 0, 1 except 2 is 0, 2, 3 except 2 is 1, used to determine the multiplier priority * / IF ((OP1 / 2 == 0) && (OP2 / 2 == 1))) X2 = (* func [op2]) (x2, x3); Else { X1 = (* Func [OP1]) (x1, x2); X2 = x3; OP1 = OP2; } / * It is not convenient to note, go to here Similar to X1 (OP1) X2 (OP2) X3, X1, X2, X3 is three values, OP1 and OP2 are operated If the OP1 is low, the OP2 is high, satisfying (OP1 / 2 == 0) && (OP2 / 2 == 1) is TRUE Working as X1 (OP1) (x2 (op2) x3), pay attention to full wreck parentheses, then assign X2 (OP2) x3 Otherwise, the OP1 priority is the same or higher than OP2, and the X1 is assigned to X1 (OP1) X2, and then X2 = x3, op1 = op2 At this point, there is only a new X1, X2, OP1 is useful. * / / * Read OP2, use to terminate the While loop if it is '=', because OP2 has no loop in value, used to store Temporary variable * / Op2 = getId (); } / * Return to X1 (OP1) X2 value * / Return (* Func [OP1]) (x1, x2); } void main () { Int value; Printf ("please input an expression: / n"); Getach (); While (curch! = '=') / * When not scanned to the equal number * / { VALUE = CAL (); Printf ("THE RESULE IS:% D / N", Value); Printf ("" please input an expression: / n "); Getach (); } }