Created
January 7, 2013 20:29
-
-
Save CaglarGonul/4478116 to your computer and use it in GitHub Desktop.
infix to postfix conversion in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "Yigit.h" | |
#include <string.h> | |
int oncelik(char islec) { | |
switch(islec) { | |
case '+': | |
case '-': | |
return 1; | |
case '*': | |
case '/': | |
return 2; | |
case '^': | |
return 3; | |
default: | |
return 0; | |
} | |
printf("(oncelik) hatali:%c\n",islec); | |
exit(-1); | |
} | |
char *in2post(char *ifade) { | |
struct Yigit *PostfixStack = yaratYigit(100); | |
struct Yigit *OperatorStack = yaratYigit(100); | |
int elementPos = 0; | |
while(ifade[elementPos] != 'x'){ | |
int priority = oncelik(ifade[elementPos]); | |
if(priority ==0){ | |
ekleYigit(PostfixStack,ifade[elementPos]); | |
}else{ | |
if(bosmuYigit(OperatorStack)==1){ | |
ekleYigit(OperatorStack,ifade[elementPos]); | |
}else{ | |
int priorityTepeOperator = oncelik(tepedekiYigit(OperatorStack)); | |
if(priority>priorityTepeOperator){ | |
ekleYigit(OperatorStack,ifade[elementPos]); | |
}else{ | |
while((priority<=priorityTepeOperator) && bosmuYigit(OperatorStack) != 1){ | |
char tepeOperator = cikarYigit(OperatorStack); | |
ekleYigit(PostfixStack,tepeOperator); | |
priorityTepeOperator = oncelik(tepedekiYigit(OperatorStack)); | |
} | |
ekleYigit(OperatorStack,ifade[elementPos]); | |
} | |
} | |
} | |
elementPos++; | |
} | |
while(bosmuYigit(OperatorStack)!=1){ | |
char tepeOperator = cikarYigit(OperatorStack); | |
ekleYigit(PostfixStack,tepeOperator); | |
} | |
return PostfixStack->dizi; | |
} | |
int main() { | |
char infix[8] = {'3', '*' , '5' , '-' , '6' , '/' , '2','x'}; | |
printf("%s",in2post(infix)); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Yigit { | |
int tepe; /* yigitin en üstündeki elemanin indeksi */ | |
int boy; /* yigitin alabilecegi en çok eleman sayisi */ | |
char *dizi; /* yigit yaratilirken verilen boyda bir dizi */ | |
}; | |
struct Yigit *yaratYigit(int boy); | |
void ekleYigit(struct Yigit *y, char X); | |
char cikarYigit(struct Yigit *y); | |
int dolumuYigit(struct Yigit *y); | |
int bosmuYigit(struct Yigit *y); | |
char tepedekiYigit(struct Yigit *y); | |
void yazdirYigit(struct Yigit *y); | |
struct Yigit *yaratYigit(int boy) { | |
struct Yigit *y= (struct Yigit *)malloc(sizeof(struct Yigit)); | |
y->tepe= -1; /* bos oldugunu gösterir */ | |
y->boy= boy; /* yigitin boy bilgisi saklanir */ | |
y->dizi= (char *)malloc(sizeof(char)*boy); | |
return y; | |
} | |
void ekleYigit(struct Yigit *y, char X) { | |
y->tepe++; | |
y->dizi[ y->tepe]= X; | |
} | |
char cikarYigit(struct Yigit *y) { | |
y->tepe--; | |
return y->dizi[ y->tepe+1]; | |
} | |
int dolumuYigit(struct Yigit *y) { | |
if(y->tepe >= (y->boy-1)) return 1; /* evet dolu */ | |
return 0; /* hayır dolu değil */ | |
} | |
int bosmuYigit(struct Yigit *y) { | |
if(y->tepe == -1) return 1; /* evet boş */ | |
return 0; /* hayır boş değil */ | |
} | |
char tepedekiYigit(struct Yigit *y) { | |
return y->dizi[y->tepe]; | |
} | |
void yazdirYigit(struct Yigit *y) { | |
int j; | |
for(j=0; j<=y->tepe; j++){ | |
printf("%c",y->dizi[j]); | |
} | |
printf("\n"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment