Infix to Postfix

Nie trmasuk tugas kmpuz yg dikumpul tgl 13 jan 2010…
bagi yg bisa buatin lebih simple dari ini dimhon bantuannya..
thx
^^bazt

/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0

typedef struct
{
char data[20];
int tos;
} STACK;

void initStack(STACK *stack);
void get_infix(char infix[]);
void convertToPostfix(char infix[], char postfix[]);
int isOperator(char c);
int precedence(char operator1, char operator2);
int pred_level(char ch);
void push(STACK *stack, char value);
char pop(STACK *stack);
char stackTop(STACK *stack);
int isEmpty(STACK *stack);
int isFull(STACK *stack);
void printResult(char infix[], char postfix[]);
void print_msg(void);

int main(void)
{
char infix[20], postfix[20]=”";
convertToPostfix(infix, postfix);
infix[strlen(infix)-2] = ”;
printResult(infix, postfix);

return EXIT_SUCCESS;
}

void initStack(STACK *stack)
{
stack->tos = -1;
}

void get_infix(char infix[])
{
int i;

printf(“Masukkan Infix max 18 karakter : \n”);
fflush(stdin);

for ( i=0; i<18; )
{
if ( (infix[i] = getchar()) == ‘\n’ )
{
i++;
break;
}
else if ( !(isspace(infix[i])) )
i++;
}

infix[i] = ”;
}

void convertToPostfix(char infix[], char postfix[])
{
int i, length;
int j=0;
char tos_ch;
STACK stack;

initStack(&stack);
get_infix(infix);
length = strlen(infix);

if ( length )
{
push(&stack, ‘(‘);
strcat(infix, “)”);
length++;

for ( i=0; i<length; i++ )
{

if ( isdigit(infix[i]) )
{
postfix[j++] = infix[i];
}

else if ( infix[i] == ‘(‘ )
{
push(&stack, ‘(‘);
}

else if ( isOperator(infix[i]) )
{
while ( TRUE )
{

tos_ch = stackTop(&stack);

if ( tos_ch == ” )
{
printf(“\nInfix Salah\n”);
print_msg();
exit(1);
}
else
{
if ( isOperator(tos_ch) )
{
if ( pred_level(tos_ch) >= pred_level(infix[i]) )
postfix[j++] = pop(&stack);
else
break;
}
else
break;
}
}
push(&stack, infix[i]);
}

else if ( infix[i] == ‘)’ )
{
while ( TRUE )
{

tos_ch = stackTop(&stack);

if ( tos_ch == ” )
{
printf(“\nInfix Salah\n”);
print_msg();
exit(1);
}
else
{
if ( tos_ch != ‘(‘ )
{
postfix[j++] = tos_ch;
pop(&stack);
}
else
{
pop(&stack);
break;
}
}
}
continue;
}
}
}

postfix[j] = ”;
}

int isOperator(char c)
{
if ( c == ‘+’ || c == ‘-’ || c == ‘*’ ||
c == ‘/’ || c == ‘%’ || c == ‘^’ )
{
return TRUE;
}
else
return FALSE;
}

int pred_level(char ch)
{
if ( ch == ‘+’ || ch == ‘-’ )
return 1;
else if ( ch == ‘^’ )
return 3;
else
return 2;
}

int precedence(char operator1, char operator2)
{
if ( pred_level(operator1) > pred_level(operator2) )
return 1;
else if ( pred_level(operator1) < pred_level(operator2) )
return -1;
else
return 0;
}

void push(STACK *stack, char value)
{
if ( !(isFull(stack)) )
{
(stack->tos)++;
stack->data[stack->tos] = value;
}
}

char pop(STACK *stack)
{
char ch;

if ( !(isEmpty(stack)) )
{
ch = stack->data[stack->tos];
(stack->tos)–;
return ch;
}
else
return ”;
}

char stackTop(STACK *stack)
{
if ( !(isEmpty(stack)) )
return stack->data[stack->tos];
else
return ”;
}

int isEmpty(STACK *stack)
{
if ( stack->tos == -1 )
return TRUE;
else
return FALSE;
}
int isFull(STACK *stack)
{

if ( stack->tos == 19 )
return TRUE;
else
return FALSE;
}
void printResult(char infix[], char postfix[])
{
printf(“\n\n”);
printf(“Notasi Infix : %s\n”, infix);
printf(“Notasi Postfix : %s\n\n”, postfix);
print_msg();
}

void print_msg(void)
{
printf(“Terima Kasih…”);
fflush(stdin);
getchar();
}

/*
Nama : Andy Bastian Fauzi
NIMĀ  : 0808605020
*/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.