Hallo sob... kali ini Nova posting tentang aplikasi stack untuk mengkonversi dari notasi postfix ke notasi prefix pada bahasa pemrograman C.
Sedikit cerita nih tentang bentuknotasi pada postfix,infix dan prefix.
infix = operand operator operand, misalnya A+B
prefix = operator operand operand. misalnya +AB
postfix=operand operand operator. misalnya AB+
Untuk mengkonversi dari postfix ke prefix, program dalam bahasa C nya ada di bawah ini:
#include<stdio.h>
#include
#include
#define SIZE 30
typedef struct stack
{
char s[SIZE][SIZE], dest[SIZE] ;
char t1[2], t2[2] ;
char ch1[SIZE], ch2[SIZE], ch3[SIZE] ;
int i, top ;
}postfix ;
void inisialisasi ( postfix * ) ;
void expression ( postfix *, char * ) ;
void push ( postfix *, char * ) ;
void pop ( postfix *, char * ) ;
void trans ( postfix * ) ;
void tampil ( postfix ) ;
#include
#include
#define SIZE 30
typedef struct stack
{
char s[SIZE][SIZE], dest[SIZE] ;
char t1[2], t2[2] ;
char ch1[SIZE], ch2[SIZE], ch3[SIZE] ;
int i, top ;
}postfix ;
void inisialisasi ( postfix * ) ;
void expression ( postfix *, char * ) ;
void push ( postfix *, char * ) ;
void pop ( postfix *, char * ) ;
void trans ( postfix * ) ;
void tampil ( postfix ) ;
void main( )
{
postfix PF ;
char exp[SIZE] ;
clrscr( ) ;
inisialisasi( &PF ) ;
printf (“\n\t\t Masukkan notasi postfix:” ) ;
gets ( exp ) ;
expression ( &PF, exp ) ;
trans ( &PF ) ;
printf ( “\n\t\tHasil konversi dari postfix ke prefix: “ ) ;
tampil ( PF ) ;
getch( ) ;
}
void inisialisasi ( postfix *p )
{
p -> i = 0 ;
p -> top = -1 ;
strcpy ( p -> dest, “” ) ;
}
void expression ( postfix *p, char *c )
{
strcpy ( p -> dest, c ) ;
}
void push ( postfix *p, char *str )
{
if ( p -> top == SIZE - 1 )
printf ( “\nStack penuh” ) ;
else
{
p -> top++ ;
strcpy ( p -> s[p -> top], str ) ;
}
}
void pop ( postfix *p, char *a )
{
if ( p -> top == -1 )
printf ( “\nStack kosong.” ) ;
else
{
strcpy ( a, p -> s[p -> top] ) ;
p -> top— ;
}
}
void trans ( postfix *p )
{
while ( p -> dest[p -> i] != ‘\0’ )
{
if ( p -> dest[p -> i] == ‘ ‘)
p -> i++ ;
if( p -> dest[p -> i] == ‘%’ || p -> dest[p -> i] == ‘*’ ||
p -> dest[p -> i] == ‘-’ || p -> dest[p -> i] == ‘+’ ||
p -> dest[p -> i] == ‘/’ || p -> dest[p -> i] == ‘$’ )
{
pop ( p, p -> ch2 ) ;
pop ( p, p -> ch3 ) ;
p -> t1[0] = p -> dest[ p -> i] ;
p -> t1[1] = ‘\0’ ;
strcpy ( p -> ch1, p -> t1 ) ;
strcat ( p -> ch1, p -> ch3 ) ;
strcat ( p -> ch1, p -> ch2 ) ;
push ( p, p -> ch1 ) ;
}
else
{
p -> t1[0] = p -> dest[p -> i] ;
p -> t1[1] = ‘\0’ ;
strcpy ( p -> t2, p -> t1 ) ;
push ( p, p -> t2 ) ;
}
p -> i++ ;
}
}
void tampil ( postfix p )
{
char *t = p.s[0] ;
while ( *t )
{
printf ( “%c “, *t ) ;
t++ ;
}
}
{
postfix PF ;
char exp[SIZE] ;
clrscr( ) ;
inisialisasi( &PF ) ;
printf (“\n\t\t Masukkan notasi postfix:” ) ;
gets ( exp ) ;
expression ( &PF, exp ) ;
trans ( &PF ) ;
printf ( “\n\t\tHasil konversi dari postfix ke prefix: “ ) ;
tampil ( PF ) ;
getch( ) ;
}
void inisialisasi ( postfix *p )
{
p -> i = 0 ;
p -> top = -1 ;
strcpy ( p -> dest, “” ) ;
}
void expression ( postfix *p, char *c )
{
strcpy ( p -> dest, c ) ;
}
void push ( postfix *p, char *str )
{
if ( p -> top == SIZE - 1 )
printf ( “\nStack penuh” ) ;
else
{
p -> top++ ;
strcpy ( p -> s[p -> top], str ) ;
}
}
void pop ( postfix *p, char *a )
{
if ( p -> top == -1 )
printf ( “\nStack kosong.” ) ;
else
{
strcpy ( a, p -> s[p -> top] ) ;
p -> top— ;
}
}
void trans ( postfix *p )
{
while ( p -> dest[p -> i] != ‘\0’ )
{
if ( p -> dest[p -> i] == ‘ ‘)
p -> i++ ;
if( p -> dest[p -> i] == ‘%’ || p -> dest[p -> i] == ‘*’ ||
p -> dest[p -> i] == ‘-’ || p -> dest[p -> i] == ‘+’ ||
p -> dest[p -> i] == ‘/’ || p -> dest[p -> i] == ‘$’ )
{
pop ( p, p -> ch2 ) ;
pop ( p, p -> ch3 ) ;
p -> t1[0] = p -> dest[ p -> i] ;
p -> t1[1] = ‘\0’ ;
strcpy ( p -> ch1, p -> t1 ) ;
strcat ( p -> ch1, p -> ch3 ) ;
strcat ( p -> ch1, p -> ch2 ) ;
push ( p, p -> ch1 ) ;
}
else
{
p -> t1[0] = p -> dest[p -> i] ;
p -> t1[1] = ‘\0’ ;
strcpy ( p -> t2, p -> t1 ) ;
push ( p, p -> t2 ) ;
}
p -> i++ ;
}
}
void tampil ( postfix p )
{
char *t = p.s[0] ;
while ( *t )
{
printf ( “%c “, *t ) ;
t++ ;
}
}
Maka output dari programnya adalah seperti berikut ini:
Selamat mencoba...
12 komentar:
waaah..gimana cara buatnya nov? mantab...
gimana buatnya nov? mantab...
buat apa ling maksud ling2???
programnya ling??
aq copas ling... dari oom google :37
hehehe :)
kodenya panjang ya, sorry terlambat
@Sepak Bola News=soccer: hehe... :)
emang panjang sob...
namanya juga program...
thanks sob :)
ini pake C++ bukan ya??.nice share dh..
@C-C:iya sob pake bahasa pemrograman C sob :)
makasih sob :)
sist punya source code konversi prefix ke postfix ga tp dengan menggunakan "Programmers Notepad", klo ada kirim k email ane hendra.unpam90@gmail.com yah... thx
salam IT
sist klo punya source code konversi prefix ke postfix pake software "programmer notepad" kirim k email yah hendra.unpam90@gmail.com
Ada konversi dari infix ke postfix ngak ???? atau infix ke prefix
Ada konversi dari infix ke postfix ngak ???? atau infix ke prefix
gimana
Posting Komentar