Returns the inverse polish expression.

Source: Internet
Author: User

The Code is as follows !!!

[Code = C/C ++] [/code]
/* Expression suffix representation and evaluation */
# Include <stdio. h>

Typedef int ElemType;/* considering that the invisible elevation during the char operation will overflow, it is defined as int, which only wastes memory space */

# Define MAXNUM 16

Struct stack
{
ElemType data [MAXNUM];
Int top;
};

Void StackInit (struct stack * stack)
{
Int I = 0;
For (; I <MAXNUM; I ++)
{
Stack-> data [I] = 0;
}
Stack-> top = 0;/* the bottom of the stack starts from the index 0 */
}

Void StackPush (struct stack * stack, ElemType c)
{
If (MAXNUM = stack-> top)/* a maximum of MAXNUM-1 elements in the stack */
{
Printf ("The stack is full ");
Return;
}
Stack-> data [stack-> top ++] = c;
}

ElemType StackPop (struct stack * stack)
{
If (0 = stack-> top)
{
Printf ("The stack is empty");/* If The stack is empty, 0 is returned */
Return 0;
}
Return stack-> data [-- stack-> top];
}

Void PostfixEvaluation (struct stack * stack)
{
Return;/* this function is very simple and all the troubles have been solved. I really don't want to complete it */
}

Int ChangeToPostfix (char * str)
{
Int I = 0, flag = 0;/* flag is used to mark the appearance of continuous numbers. I did not expect a better method */
Int c, ch;

Struct stack ch_stack;
Struct stack op_stack;

StackInit (& ch_stack );
StackInit (& op_stack );

While (! = (C = * (str + I)/* Note: if it is a static string, it indicates the end condition. If it is input by the user, the ending condition is */
{
If (* = c) | (/= c) | (= c ))
{
Flag = 0;
StackPush (& op_stack, c );
}
Else if () = c)
{
Flag = 0;
While ((! = (C = StackPop (& op_stack )))
{
StackPush (& ch_stack, c );
}
If (0 = op_stack.top)
{
Printf ("the (hasnt found when the) come in! ");
Return-1;
}
}
Else if (+ = c) | (-= c ))
{
Flag = 0;
/* + And-low priority. all operators in the operator stack (if any) need to be introduced */
If (0! = Op_stack.top)/* You can not add the top check here. In this way, you can find that 0 returned by StackPop error is picked up */
{/* Forced to be helpless. You have to check the top value here and cannot expect StackPop */
While ((! = (Ch = StackPop (& op_stack )))
{
StackPush (& ch_stack, ch );
If (0 = op_stack.top)
{
Break;
}
}
}
StackPush (& op_stack, c );
}
Else if (c> = 0) & (c <= 9)/* special processing is required for two or more consecutive numbers in the expression */
{
If (0 = flag)
{
StackPush (& ch_stack, (c-0 ));
Flag = 1;
}
Else
{
StackPush (& ch_stack, 10 * StackPop (& ch_stack) + (c-0 ));
}
}
I ++;
}
While (0! = Op_stack.top)/* Expression Processing ends. all operators in the operator Stack are pushed to the character stack */
{
StackPush (& ch_stack, StackPop (& op_stack ));
}

PostfixEvaluation (& ch_stack);/* This function may not be properly put here. However, if the task is completed, isn't it OK? Do you care? */

/* Just test */
For (I = 0; I <ch_stack.top; I ++)
{
If (+ = ch_stack.data [I])
{
Printf ("+ ..");
}
Else if (-= ch_stack.data [I])
{
Printf ("-..");
}
Else if (* = ch_stack.data [I])
{
Printf ("*..");
}
Else if (/= ch_stack.data [I])
{
Printf ("/..");
}
Else
{
Printf ("% d ..", ch_stack.data [I]);
}
}

Return 0;
}


Int main (void)
{
Char str [] = "12 + 34*435-5/1 ";
/* Just test */
Printf ("The result shocould be :");
Printf ("12 34 435 * + 5 1/-[= 8]");

If (-1 = ChangeToPostfix (str ))
{
Printf ("ChangeToPostfix () error ");
Return 1;
}
Return 0;
}

Related Article

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.