Ceriwis  

Go Back   Ceriwis > HOBI > Komputer & Teknologi > Programming

Programming Share, tanya jawab, saling bantu antar programmer dengan berbagai macam bahasa pemrograman.

Reply
 
Thread Tools
  #1  
Old 20th November 2011
Permenkaret Permenkaret is offline
Ceriwiser
 
Join Date: Nov 2011
Posts: 351
Rep Power: 14
Permenkaret mempunyai hidup yang Normal
Default [ask] Logika Syntax C ini

gan ane baru belajar bahasa pemrograman C nih, tapi ane dah puyeng banget pas mulai-mulai mengenai pointer.







nah program dibawah ini merupakan program untuk merubah infix menjadi postfix gan. mohon bantuannya karna ane bayak yang gak mengerti gan






Spoiler for cekidot:






#include

#include

#include

#include



#define MAX 50



struct infix

{

char target[MAX] ;

char stack[MAX] ;

char *s, *t ;

int top ;

} ;



void initinfix ( struct infix * ) ;

void setexpr ( struct infix *, char * ) ;

void push ( struct infix *, char ) ;

char pop ( struct infix * ) ;

void convert ( struct infix * ) ;

int priority ( char ) ;

void show ( struct infix ) ;



void main( )

{

struct infix p ;

char expr[MAX] ;



initinfix ( &p ) ;



clrscr( ) ;



printf ( "\nEnter an expression in infix form: " ) ;

gets ( expr ) ;



setexpr ( &p, expr ) ;

convert ( &p ) ;



printf ( "\nThe postfix expression is: " ) ;

show ( p ) ;



getch( ) ;

}



/* initializes structure elements */

void initinfix ( struct infix *p )

{

p -> top = -1 ;

strcpy ( p -> target, "" ) ;

strcpy ( p -> stack, "" ) ;

p -> t = p -> target ;

p -> s = "" ;

}



/* sets s to point to given expr. */

void setexpr ( struct infix *p, char *str )

{

p -> s = str ;

}



/* adds an operator to the stack */

void push ( struct infix *p, char c )

{

if ( p -> top == MAX )

printf ( "\nStack is full.\n" ) ;

else

{

p -> top++ ;

p -> stack[p -> top] = c ;

}

}



/* pops an operator from the stack */

char pop ( struct infix *p )

{

if ( p -> top == -1 )

{

printf ( "\nStack is empty.\n" ) ;

return -1 ;

}

else

{

char item = p -> stack[p -> top] ;

p -> top-- ;

return item ;

}

}



/* converts the given expr. from infix to postfix form */

void convert ( struct infix *p )

{

char opr ;



while ( *( p -> s ) )

{

if ( *( p -> s ) == ' ' || *( p -> s ) == '\t' )

{

p -> s++ ;

continue ;

}

if ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )

{

while ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )

{

*( p -> t ) = *( p -> s ) ;

p -> s++ ;

p -> t++ ;

}

}

if ( *( p -> s ) == '(' )

{

push ( p, *( p -> s ) ) ;

p -> s++ ;

}



if ( *( p -> s ) == '*' || *( p -> s ) == '+' || *( p -> s ) == '/' || *( p -> s ) == '%' || *( p -> s ) == '-' || *( p -> s ) == '$' )

{

if ( p -> top != -1 )

{

opr = pop ( p ) ;

while ( priority ( opr ) >= priority ( *( p -> s ) ) )

{

*( p -> t ) = opr ;

p -> t++ ;

opr = pop ( p ) ;

}

push ( p, opr ) ;

push ( p, *( p -> s ) ) ;

}

else

push ( p, *( p -> s ) ) ;

p -> s++ ;

}



if ( *( p -> s ) == ')' )

{

opr = pop ( p ) ;

while ( ( opr ) != '(' )

{

*( p -> t ) = opr ;

p -> t++ ;

opr = pop ( p ) ;

}

p -> s++ ;

}

}



while ( p -> top != -1 )

{

char opr = pop ( p ) ;

*( p -> t ) = opr ;

p -> t++ ;

}



*( p -> t ) = '\0' ;

}



/* returns the priority of an operator */

int priority ( char c )

{

if ( c == '$' )

return 3 ;

if ( c == '*' || c == '/' || c == '%' )

return 2 ;

else

{

if ( c == '+' || c == '-' )

return 1 ;

else

return 0 ;

}

}



/* displays the postfix form of given expr. */

void show ( struct infix p )

{

printf ( " %s", p.target ) ;

}











mohon bantuannya yah gan, dimana lagi ane bisa mencari informasi selain di kaskus

:ceriwislov e:



Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


 


All times are GMT +7. The time now is 11:12 PM.


no new posts