Use the array stack to convert an infix expression to a suffix expression

Source: Internet
Author: User

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
 
Typedef struct Mystack * Stack;
 
Struct Mystack {
Int Capacity;/* stack Capacity */
Int Top_of_stack;/* stack top/bottom mark */
Char * Array;/* Array for storing elements in the stack */
};
 
/* Stack creation */
Stack CreateStack (int Max)
{
Stack S;
S = malloc (sizeof (struct Mystack ));
If (S = NULL)
Printf ("Create stack error! \ N ");
 
S-> Array = malloc (sizeof (char) * Max );
If (S-> Array = NULL)
Printf ("Create stack error! \ N ");
 
S-> Capacity = Max;
S-> Top_of_stack = 0;
Return S;
}
 
/* Release stack */
Void DisposeStack (Stack S)
{
If (S! = NULL)
{
Free (S-> Array );
Free (S );
}
}
 
/* Determine whether a stack is empty */
Int IsEmpty (Stack S)
{
Return! S-> Top_of_stack;
}
 
/* Determine whether a stack is full */
Int IsFull (Stack S)
{
If (S-> Top_of_stack = S-> Capacity-1)
Return 1;
Else
Return 0;
}
 
 
/* Data stack */
Int Push (int x, Stack S)
{
If (IsFull (S ))
Printf ("The Stack is full! \ N ");
Else
S-> Array [S-> Top_of_stack ++] = x;
}
 
/* Data output stack */
Int Pop (Stack S)
{
If (IsEmpty (S ))
Printf ("The Stack is empty! \ N ");
Else
S-> Top_of_stack --;
}
 
/* Return the stack top */
Char Top (Stack S)
{
If (! IsEmpty (S ))
Return S-> Array [S-> Top_of_stack-1];
Printf ("The Stack is empty! \ N ");
Return 0;
}
 
/* Calculate the priority */
Int getPriority (char)
{
Switch ()
{
Case '#':
Return 0;
Break;
Case '+ ':
Case '-':
Return 1;
Break;
Case '*':
Case '/':
Return 2;
Break;
Case '(':
Return 3;
Break;
Default:
Break;
}
}
 
Int main ()
{
Int I, len;
Char str [100];
Printf ("Please input the Infix expression that you want to change: \ n ");
Scanf ("% s", str );
Len = strlen (str );
/* Create a stack based on the sequence length */
Struct Mystack * my_stack = CreateStack (len + 1 );
/* Use '#' to facilitate unified judgment when the first stack is entered */
Push ('#', my_stack );
For (I = 0; I <len; I ++)
{
If (str [I]> = '0' & str [I] <= '9 ') | (str [I]> = 'A' & str [I] <= 'Z')/* operand */
{
Printf ("% c", str [I]);
}
Else if (str [I]! = ')' & GetPriority (str [I])> getPriority (Top (my_stack)/* When the Top element of the stack has an hour of priority over the next operator, directly stack the external operators */
{
Push (str [I], my_stack );
}
Else if (str [I]! = ')' & GetPriority (str [I]) <= getPriority (Top (my_stack)/* the priority of the Top element of the stack is greater than or equal to the priority of the next operator, at this time, the stack should be released, but ensure that '(' don't go out of the stack */
{
While (getPriority (str [I]) <= getPriority (Top (my_stack) & Top (my_stack )! = '(')
{
Printf ("% c", Top (my_stack ));
Pop (my_stack );
}
Push (str [I], my_stack );
}
Else if (str [I] = ')/* if you encounter a right brace, the top element of the stack will pop up until a left brace is met */
{
While (Top (my_stack )! = '(')
{
Printf ("% c", Top (my_stack ));
Pop (my_stack );
}
Pop (my_stack );
}
}
 
/* Remaining elements in the output stack */
If (! IsEmpty (my_stack ))
{
While (Top (my_stack )! = '#')
{
Printf ("% c", Top (my_stack ));
Pop (my_stack );
}
Printf ("\ n ");
}
DisposeStack (my_stack );
Return 0;
}

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

Typedef struct Mystack * Stack;

Struct Mystack {
Int Capacity;/* stack Capacity */
Int Top_of_stack;/* stack top/bottom mark */
Char * Array;/* Array for storing elements in the stack */
};

/* Stack creation */
Stack CreateStack (int Max)
{
Stack S;
S = malloc (sizeof (struct Mystack ));
If (S = NULL)
Printf ("Create stack error! \ N ");

S-> Array = malloc (sizeof (char) * Max );
If (S-> Array = NULL)
Printf ("Create stack error! \ N ");

S-> Capacity = Max;
S-> Top_of_stack = 0;
Return S;
}

/* Release stack */
Void DisposeStack (Stack S)
{
If (S! = NULL)
{
Free (S-> Array );
Free (S );
}
}

/* Determine whether a stack is empty */
Int IsEmpty (Stack S)
{
Return! S-> Top_of_stack;
}

/* Determine whether a stack is full */
Int IsFull (Stack S)
{
If (S-> Top_of_stack = S-> Capacity-1)
Return 1;
Else
Return 0;
}


/* Data stack */
Int Push (int x, Stack S)
{
If (IsFull (S ))
Printf ("The Stack is full! \ N ");
Else
S-> Array [S-> Top_of_stack ++] = x;
}

/* Data output stack */
Int Pop (Stack S)
{
If (IsEmpty (S ))
Printf ("The Stack is empty! \ N ");
Else
S-> Top_of_stack --;
}

/* Return the stack top */
Char Top (Stack S)
{
If (! IsEmpty (S ))
Return S-> Array [S-> Top_of_stack-1];
Printf ("The Stack is empty! \ N ");
Return 0;
}

/* Calculate the priority */
Int getPriority (char)
{
Switch ()
{
Case '#':
Return 0;
Break;
Case '+ ':
Case '-':
Return 1;
Break;
Case '*':
Case '/':
Return 2;
Break;
Case '(':
Return 3;
Break;
Default:
Break;
}
}

Int main ()
{
Int I, len;
Char str [100];
Printf ("Please input the Infix expression that you want to change: \ n ");
Scanf ("% s", str );
Len = strlen (str );
/* Create a stack based on the sequence length */
Struct Mystack * my_stack = CreateStack (len + 1 );
/* Use '#' to facilitate unified judgment when the first stack is entered */
Push ('#', my_stack );
For (I = 0; I <len; I ++)
{
If (str [I]> = '0' & str [I] <= '9 ') | (str [I]> = 'A' & str [I] <= 'Z')/* operand */
{
Printf ("% c", str [I]);
}
Else if (str [I]! = ')' & GetPriority (str [I])> getPriority (Top (my_stack)/* When the Top element of the stack has an hour of priority over the next operator, directly stack the external operators */
{
Push (str [I], my_stack );
}
Else if (str [I]! = ')' & GetPriority (str [I]) <= getPriority (Top (my_stack)/* the priority of the Top element of the stack is greater than or equal to the priority of the next operator, at this time, the stack should be released, but ensure that '(' don't go out of the stack */
{
While (getPriority (str [I]) <= getPriority (Top (my_stack) & Top (my_stack )! = '(')
{
Printf ("% c", Top (my_stack ));
Pop (my_stack );
}
Push (str [I], my_stack );
}
Else if (str [I] = ')/* if you encounter a right brace, the top element of the stack will pop up until a left brace is met */
{
While (Top (my_stack )! = '(')
{
Printf ("% c", Top (my_stack ));
Pop (my_stack );
}
Pop (my_stack );
}
}

/* Remaining elements in the output stack */
If (! IsEmpty (my_stack ))
{
While (Top (my_stack )! = '#')
{
Printf ("% c", Top (my_stack ));
Pop (my_stack );
}
Printf ("\ n ");
}
DisposeStack (my_stack );
Return 0;
}

Test data:


[Cpp]
Shang @ shang :~ /C $./a. out
Please input the Infix expression that you want to change:
A + B * c + (d * e + f) * g
Abc * + de * f + g * +

Shang @ shang :~ /C $./a. out
Please input the Infix expression that you want to change:
A + B * c + (d * e + f) * g
Abc * + de * f + g * +


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.