Expression debugging

xiaoxiao2021-03-05  24

It turns out that the debugging procedure is much more difficult than the writing program!

It took less than 2 hours to finish the program, and the debugged me almost 4 hours, there are still a lot of imperfect, the next one is not let the program support the two-digit operation!

I don't want to write the debugging process, too hard!

Below is the result I modified

Oh ~ · Recently I have to deal with the string!

/ ************************************************** ********* This algorithm uses the "Operation Symbol Priority" process to obtain a given expression of 1.00! ------ Navybluestudio **************************************************** *************** //// # include // # include // Defining Stack Data Type # Define Addsize 10 # Define Error 0 # Define False 0 # define overflow 0 # define true 1typedef struct {char * top; // Pointer CHAR * BASE; / / Pointer Int Stacksize; / / Defining Stack of Stack; // Stack Basic Operation Void InitStack (STACK &); // Initialization Stack Operation Void Push (Stack &, Char); // Stack char Gettop (stack); // Get Stack Top Element Void Pop (Stack &, Char &);

INT COMPARE (Char); Char Operate (Char, Char, Char); Char Precede (Char, Char); /

/ / Implementation Some /// EvaluateExpression Function Main Complete Expression Question Issues // Expression is the value of the value of the value of the required value int evactionExpression (char * expression) {// to save the operator, OPND to save Stack Optr , Opnd; InitStack (OPTR); Push (Optr, '#'); // # symbol represents the bottom INITSTACK (OPND);

/ / Define Temp to save temporary symbols or operands char TEMP; int i = 0; Temp = Expression [i];

// Judgment If it is ## End cycle while (Gettop! = '#') {// compare is used to compare the number of operands or operators if the operand returns True If the operator returns false if (compare (Temp)) {push (OPND, TEMP); Temp = Expression [ i];} else // precede is used to compare the priority of the stack top operator and new operator Level Switch (Precede (Gettop (Opter (OpterTr), Temp)) {CASE '<': // Priority Low Push (Optr, Temp) of Top; Temp = Expression [ I]; Break; Case '=': // Remorse the process and accept the next character char x; POP (OPTR, X); Temp = Expression [ i]; Break; Case '>': // Repends and puts the arithmetic result into the stack char sIGN; Char Num1, Num2; POP (OPTR, SIGN); POP (OPND, NUM1); PUP (OPND, Num2); Push (OPND, Operate (Num1, Sign, Num2)); // Find problems, to convert digital conversion Break;}} return gettop;}}} int compare (char isoperatornum) {// compares whether it is an operand. . The premise is that the operand is within 0 to 12 // If it is not in this range as an operator to see IF ((isoperatornum-48)> = 0 && (isoperatornum-48) <= 12) {Return True;} Else Return false;}

Char Operate (CHAR NUM1, CHAR SIGN, CHAR NUM2) {// Note To return to the character pattern // first convert to a numeric shape and then convert to a character line returns int Integer1, Integer2; Integer1 = Num1-48; integer2 = Num2-48 ; If (' ' == Sign) Return (Integer1 Integer2) 48; ELSE IF ('-' == Sign) Return (Integer1-Integer2) 48; Else IF ('*' == SIGN) Return Integer1 * Integer2 48; Else Return INTEGER1 / INTEGER2 48;} // The priority of the comparison operator This is more troublesome Char Precede (char sign1, char sign2) {switch (siGn1) {casse ' ': casse ' - ': 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 (! (! (!) EXIT (OVERFLOW); // Assignment failed S.TOP = S.BASE; / / The top of the stack is empty SS TACKSIZE = 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 addize * sizeof (char)); if (! (!) EXIT (OVERFLOW); S.TOP = S.BASE S.STACKSIZE; S. Stacksize = addsize;} * (S.TOP ) = Sign;} char Gettop (stack s) {// Decision stack is empty if (S.Base == S.top) Return 0; char ch; // ch is Used to save CH = * (S.TOP-1); // There is a problem with the problem in evening to debug, don't mess with me Return CH;}

Void Pop (Stack & S, Char & Receive) {if (S.TOP == S.Base) EXIT (0); Receive = * (S.top-1); S.TOP -

}

INT main () {// Test Because this program uses a string form in a string, it is not possible to save a value of the two-digit value. // Tomorrow to improve, use the form of the character array or the size of the double string to store the element char * TEMP = "1 * (2 4) * 2 #"; char m = evataExpression (TEMP); int J = M -48; std :: cout << "The value of your required expression is:"

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

New Post(0)