The 24-point program written last time has not been fixed yet.
Now I plan to start the expression from the grassroots level to solve my problem! Then there is good to do it.
Below is the code I have last night and I have just been chaos.
Haven't tested it yet!
/ ************************************************** ********* This algorithm uses the "Operation Symbol Priority" process to obtain a given expression of 1.00! *********************************************************** ******** /// Defining Stack Data Type #define addsize 10typedef struct {char * top; // Pointer CHAR * base; // Pointer Int stacksize; // definition The size of the stack} stack; / / Stack Basic Operation Void InitAck (Stack &); // Initialization Stack Operation Void Push (stack, char); // Stack char Gettop (stack); // Get top elements
Bool Compare (CHAR); int Operate (int, char, int); char precede (char, char); // evataExpression function Mainly completed expression request issues // expression is the value of the required value INT EvaluateExpression ( CHAR * Expression) {// Use OPTR to save the operator, OPND to save Stack Optr, Opnd; InitStack (OPTR); Push (Optr, '#'); // # symbol Indicates the bottom INITACK (OPND);
/ / Define Temp to save temporary symbols or operands char TEMP; int i = 0; Temp = Expression [i];
While (Gettop (Opter)! = '#' || Temp! = ';') {// compare is used to compare the operand or operator if the operand returns true if it is the operator Returns false IF (Compare (TEMP)) {Push (OPND, TEMP); Temp = Expression [ i];} else // precede is used to compare the priority Switch (Precede (PRECEDE (Gettop (OPTR) of the stack top operator and new operator. Temp)) {CASE '<': // Priority Low Push (OPTR, TEMP) of the top of the top; Temp = Expression [ i]; Break; Case '=': // Remorse Process and accept the next character POP (OPTR, Char X); Temp = Expression [ i]; Break; Case '<': // Repends and puts the calculation result into the stack POP (Optr, Char Sign); POP (OPND, CHAR NUM1) POP (OPND, CHAR NUM2); Push (OPND, Operate (NNM1, Sign, Num2); Break;}} Return Gettop (OPND);} Bool Compare (Char isoperatornum) {// Compare is an operand .. Prerequisite Is the operand in the range of 0 to 12 // If it is not in this range, it will look at IF ((isoperatornum-48)> = 0 && (isoperatornum-48) <= 12) {Return True;} else return False;}
INT OPERATE (INT NUM1, CHAR SIGN, INT NUM2 {IF (' ' == SIGN) RETURN NUM1 NUM2; Else IF ('-' == Sign) Return Num1-Num2; Else IF ('*' == Sign) Return Num1 * Num2; Else Return Num1 / Num2;} // The priority of the comparison operator This step is more troubles Luo Zhar precede (char sign1, char sign2) {switch (Sign1) {CASE ' ': Case ' - ': Switch (Sign2) {CASE' ': Case' - ': Case') ': Case' # ': Return'> '; Case' * ': Case' / ': Case' (': return " <';} Case' * ': case' / ': switch (Sign2) {CASE' ': Case' - ': Case' * ': Case' / ': Case') ': Case' # ': return '>'; Case '(': Return '<';} case '(': switch (sign2) {copy ')') ': return' = '; default: return' <';} case') ': Return' > '; Case' # ': switch (siGN2) {case' # ': return' = '; default: return' <';}}} void initstack (stack & s) {// At the time I pay attention to S.BASE = (char *) Malloc (16 * sizeof (char)); if (! (! (s-> base)) exit (overflow); // Assignment failed S.Base = S.TOP; / / The top of the stack is empty S.STACKSIZE = 16;
Void Push (Stack & S, Char Sign) {// Start Judge whether the stack is full if ((S.TOP-S.BASE)> = S.Stacksize) {// If full of additional storage S.Base = (char *) Realloc (s.Stacksize addsize) * sizeof (char); if (! (! (! (! (!) EXIT (OVERFLOW); S.TOP = S.BASE S.STACKSIZE; S.STACKSIZE = Addsize;} S.TOP = Sign; TOP ;} char Gettop (stack s) {// Decision stack is empty if (S.Base == S.top) Return error; char ch; // ch is used Save CH = S.TOP; TOP ---; Return CH;}