Last active
March 10, 2019 20:56
-
-
Save alperen/5d8d986bc2a3fd7c985a2cf27893eb02 to your computer and use it in GitHub Desktop.
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> | |
struct stack{ | |
int top; | |
int count; | |
int* arr; | |
}; | |
void init(struct stack *ptr, int size){ | |
ptr->top = -1; | |
ptr->count = size; | |
ptr->arr = malloc(sizeof(int) * size); | |
printf("STACK INITED\n"); | |
} | |
void push(struct stack *ptr, int val){ | |
if(ptr->count == ptr->top){ | |
printf("STACK FULL"); | |
}else{ | |
ptr->top++; | |
ptr->arr[ptr->top] = val; | |
printf("PUSHED %d TOP:%d\n",val,ptr->top); | |
} | |
} | |
int pop(struct stack *ptr){ | |
int x; | |
if(ptr->top == -1){ | |
printf("STACK EMPTY"); | |
}else{ | |
x = ptr->arr[ptr->top]; | |
printf("POPPED %d TOP:%d\n",x,ptr->top); | |
} | |
return x; | |
} | |
int main(int argc, char *argv[]) { | |
struct stack myStack; | |
init(&myStack,50); | |
push(&myStack,1998); | |
push(&myStack,1974); | |
push(&myStack,12556); | |
pop(&myStack); | |
push(&myStack,1987); | |
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
#include <stdio.h> | |
#include <stdlib.h> | |
struct stack{ | |
int top; | |
int count; | |
char* arr; | |
}; | |
void init(struct stack *ptr, int size){ | |
ptr->top = -1; | |
ptr->count = size; | |
ptr->arr = malloc(sizeof(char) * size); | |
printf("STACK INITED\n"); | |
} | |
void push(struct stack *ptr, char val){ | |
if(ptr->count == ptr->top){ | |
printf("STACK FULL"); | |
}else{ | |
ptr->top++; | |
ptr->arr[ptr->top] = val; | |
//printf("PUSHED %d TOP:%c\n",val,ptr->top); | |
} | |
} | |
char pop(struct stack *ptr){ | |
char x = ""; | |
if(ptr->top == -1){ | |
printf("STACK UNDERFLOW"); | |
}else{ | |
x = ptr->arr[ptr->top]; | |
ptr->top -= 1; | |
//printf("POPPED %d TOP:%d\n",x,ptr->top); | |
} | |
return x; | |
} | |
int isOperator(char op){ | |
switch(op){ | |
case '+': | |
case '-': | |
case '*': | |
case '/': | |
return 1; | |
break; | |
} | |
return 0; | |
} | |
int getOperatorWeight(char op){ | |
switch(op){ | |
case '+': | |
case '-': | |
return 1; | |
break; | |
case '*': | |
case '/': | |
return 2; | |
break; | |
case ')': | |
case '(': | |
return 0; | |
break; | |
default: | |
return -1; | |
break; | |
} | |
} | |
char lastest(struct stack *ptr){ | |
if(ptr->top == -1){ | |
return -1; | |
} | |
return ptr->arr[ptr->top]; | |
} | |
int isEmpty(struct stack *ptr){ | |
return (ptr->top == -1) ? 1 : 0; | |
} | |
int main(int argc, char *argv[]) { | |
struct stack myStack; | |
init(&myStack,50); | |
char ifade[] = "A+B-C+D/R"; | |
int i; | |
for(i = 0;i<9;i++){ | |
char s = ifade[i]; | |
if(isOperator(s)){ | |
if(isEmpty(&myStack) || s == '('){ | |
push(&myStack,s); | |
continue; | |
} | |
if(s == ')'){ | |
pop(&myStack); | |
while(1){ | |
char x = pop(&myStack); | |
if(x == '(') | |
break; | |
printf("%c",x); | |
} | |
continue; | |
} | |
int weight = getOperatorWeight(s); | |
int lastestWeight = getOperatorWeight(lastest(&myStack)); | |
if(lastestWeight >= weight){ | |
printf("%c",pop(&myStack)); | |
push(&myStack,s); | |
}else{ | |
push(&myStack,s); | |
} | |
}else{ | |
printf("%c",s); | |
} | |
} | |
if(!isEmpty(&myStack)){ | |
while(!isEmpty(&myStack)){ | |
printf("%c",pop(&myStack)); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment