Questions and codes:
/*2015, College of Computer and Control engineering, Yantai University
* Author: Ersanli
* Completion Date: October 7, 2015
* Problem Description: The algorithm of converting an infix expression to the corresponding suffix expression is realized by using the basic operation of the stack in the sqstack.h. For example, input (56-20)/(4+2), output suffix expression:: 56#20#-4#2#+/requires the number to be appended with #.
* Input Description: infix expression
* result output: suffix expression */
(1) header file:
#ifndef sqstack_h_included #define sqstack_h_included #define MAXSIZE #define MAXOP 7 typedef int ELEMTYPE;
typedef struct {elemtype data[maxsize]; int top; Stack pointer} sqstack; The sequence stack type defines the struct//set operator precedence {char ch; operator int pri; Priority} lpri[]= {' = ', 0},{' (', 1},{' * ', 5},{'/', 5},{' + ', 3},{'-', 3},{') ', 6}}, rpri[]= {' = ', 0},{' (', 6},{' * ', 4},{'/', 4}
, {' + ', 2},{'-', 2},{') ', 1}}; void Initstack (Sqstack *&s); Initialize stack void Destroystack (Sqstack *&s); Destroy stack bool Stackempty (Sqstack *s); Whether the stack is empty int stacklength (Sqstack *s); Returns the number of elements in the stack-the stack length bool Push (Sqstack *&s,elemtype e); into the stack bool POPs (Sqstack *&s,elemtype &e); Out of stack bool GetTop (sqstack *s,elemtype &e); Take stack top data element void Dispstack (Sqstack *s);
Output stack void multibaseoutput (int number,int base);
int Leftpri (char op);
int Rightpri (char op);
BOOL Inop (char ch);
int precede (char op1,char OP2);
void Trans (char *exp,char postexp[]); #endif//sqstack_h_included
(2) source program:
#include <stdio.h> #include <malloc.h> #include "SqStack.h" void Initstack (Sqstack *&s) {s= (sqstack
*) malloc (sizeof (sqstack));
s->top=-1; } void Destroystack (Sqstack *&s) {free (s);} int stacklength (Sqstack *s)//Returns the number of elements in the stack-stack length {return (s->top+
1); } bool Stackempty (Sqstack *s) {return (S->TOP==-1);} bool Push (Sqstack *&s,elemtype e) {if (S->top==ma
XSIZE-1)//full case, ie overflow on stack return false;
s->top++;
s->data[s->top]=e;
return true;
The case of bool Pop (Sqstack *&s,elemtype &e) {if (s->top==-1)//stack is empty, that is, the stack overflows with return false;
e=s->data[s->top];
s->top--;
return true;
} bool GetTop (Sqstack *s,elemtype &e) {if (s->top==-1)//stack is empty, that is, the stack overflows with return false;
e=s->data[s->top];
return true;
} void Dispstack (Sqstack *s)//output stack {int i;
for (i=s->top;i>=0;i--) printf ("%c", S->data[i]);
printf ("\ n"); } INT Leftpri (char op)//left operator op priority {int i;
for (i=0; i<maxop; i++) if (LPRI[I].CH==OP) return lpri[i].pri;
} int Rightpri (char op)//Right operator op priority {int i;
for (i=0; i<maxop; i++) if (RPRI[I].CH==OP) return rpri[i].pri; } bool Inop (char ch)//Determine if CH is operator {if (ch== ' (' | | ch== ') ' | | ch== ' + ' | | ch== '-' | | ch== ' * ' | |
'/') return true;
else return false;
} int Precede (char Op1,char op2)//op1 and OP2 operator Precedence comparison result {if (Leftpri (OP1) ==rightpri (OP2)) return 0;
else if (Leftpri (OP1) <rightpri (OP2)) return-1;
else return 1; } void Trans (char *exp,char postexp[])//Converts an arithmetic expression exp to a suffix expression postexp {sqstack *opstack; Defines the operator stack int i=0;
I as subscript elemtype ch of the postexp; Initstack (Opstack);
Use initialization stack operation to allocate space for stack, be sure to do Push (opstack, ' = '); while (*exp!= ')//exp expression does not finish scanning when the loop {if (! Inop (*EXP))//numeric characters{while (*exp>= ' 0 ' && *exp<= ' 9 ')//is determined as the number {postexp[i++]
=*exp;
exp++; } postexp[i++]= ' # '; Use # to identify a value string to end} else//For the case of operator {GetTop (opstack, ch);
operator switch with top of stack (precede (ch, *exp)) {case-1://top of stack operator low priority: in-stack
Push (Opstack, *exp); exp++;
Continue to scan for other characters break; Case 0://Only brackets satisfy this condition Pop (opstack, ch); Will (Stack exp++;
Continue to scan for other characters break;
Case 1://rewind stack and output to postexp postexp[i++]=ch;
Pop (Opstack, ch);
Break
}}}//while (*exp!= ') Pop (Opstack, ch);
while (ch!= ' = ')//At this time exp scan is complete, back to ' = ' until {postexp[i++]=ch;
Pop (Opstack, ch); } postexp[i]= ' + '; To givePostexp expression add end-of-identity destroystack (opstack); }
(3) Debug function:
#include <stdio.h>
#include "SqStack.h"
int main ()
{
char exp[]= "(56-20)/(4+2)"; You can change exp to keyboard input
char postexp[200];
Trans (exp,postexp);
printf ("Infix expression:%s\n", exp);
printf ("Suffix expression:%s\n", postexp);
return 0;
}
Operation Result:
Summary of Knowledge points:
Stack......
Learning experience:
The process is still a bit complicated, and there are many places that are not very clear.