(bikin saya nangis bombay) hahaa,,,lebay :D.
langsung aja kita liat listingnya :
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int karakter,top2,maks,pindah,top,i,j,a,b;
char ulang;
char infiks[20];
char postfiks[20];
char stack2[10];
char hasil[20];
void konversi(int y){
int a,z;
int b = 0;
pindah = 0;
top2 = 0;
fflush(stdin);
for(i=0;i<y;i++){
z = 0;
if(infiks[i]==�(�){
stack2[top2]=infiks[i];
top2 = top2 + 1;
}
else if(infiks[i]==�)'){
a = top2 � 1;
while(stack2[a]!=�(�){
if(a < 0){
break;
}
else{
hasil[b]=stack2[a];
b = b+1;
a = a-1;
top2 = top2 � 1;
}
}
if(a < 0){
gotoxy(3,15);printf(�hasil : error => (kurang kurung buka)�);
break;
}
top2 = top2 � 1;
}
else if(infiks[i]==�+'){
if((stack2[top2-1]==�-')||(stack2[top2-1]==�*')||(stack2[top2-1]==�/')||(stack2[top2-1]==�^')){
hasil[b]=stack2[top2-1];
b = b+1;
stack2[top2-1]=infiks[i];
}
else{
stack2[top2]=infiks[i];
top2 = top2 + 1;
}
}
else if(infiks[i]==�-'){
if((stack2[top2-1]==�+')||(stack2[top2-1]==�*')||(stack2[top2-1]==�/')||(stack2[top2-1]==�^')){
hasil[b]=stack2[top2-1];
b = b+1;
stack2[top2-1]=infiks[i];
}
else{
stack2[top2]=infiks[i];
top2 = top2 + 1;
}
}
else if(infiks[i]==�*'){
if((stack2[top2-1]==�/')||(stack2[top2-1]==�^')){
hasil[b]=stack2[top2-1];
b = b+1;
stack2[top2-1]=infiks[i];
}
else{
stack2[top2]=infiks[i];
top2 = top2 + 1;
}
}
else if(infiks[i]==�/'){
if((stack2[top2-1]==�*')||(stack2[top2-1]==�^')){
hasil[b]=stack2[top2-1];
b = b+1;
stack2[top2-1]=infiks[i];
}
else{
stack2[top2]=infiks[i];
top2 = top2 + 1;
}
}
else if(infiks[i]==�^'){
if(stack2[top2-1]==�^'){
hasil[b]=stack2[top2-1];
b = b+1;
stack2[top2-1]=infiks[i];
}
else{
stack2[top2]=infiks[i];
top2 = top2 + 1;
}
}
else if(infiks[i]==�;'){
if(top2>0){
a = top2 � 1;
while(a>=0){
if(stack2[a]==�(�){
gotoxy(3,15);printf(�hasil : error => (kurang kurung tutup)�);
a = a-1;
break;
}
else{
hasil[b]=stack2[a];
b = b+1;
a = a-1;
top2 = top2 � 1;
}
}
}
}
else{
hasil[b]=infiks[i];
b = b+1;
}
gotoxy(3+(i*2),7);cout<<���;
gotoxy(3+(i*2),8);cout<<infiks[i];
gotoxy(3+(i*2),9);cout<<���;
for(j=(top2-1);j>=0;j�){
gotoxy(3+(i*2),10+z);cout<<stack2[j];
z++;
}
}
gotoxy(3,15);cout<<�hasil : �;
for(i=0;i<b;i++){
gotoxy(11+i,15);cout<<hasil[i];
}
}
void main(){
do{
for(i=0;i<20;i++){
gotoxy(1,i);cout<<� �;
}
maks=0;
top =0;
gotoxy(3,4);printf(�Masukan statement infix (akhiri dengan tanda ;) : �);
gotoxy(3,5);cin>>infiks;
karakter = strlen(infiks);
konversi(karakter);
gotoxy(3,17);cout<<�ulangi program[Y/T]: �;cin>>ulang;
}
while(ulang==�y'||ulang==�Y');
}
* * *
demikian program konversi postfik ke infiks yang saya buat, semoga bisa bermanfaat, ^_^
* * *
sumber : CodyCoding
Posting Komentar
Catatan: Hanya anggota dari blog ini yang dapat mengirim komentar.