Skip to content
Snippets Groups Projects
Commit 2bac22c5 authored by thib's avatar thib
Browse files

ok

parent 954a1170
No related branches found
No related tags found
No related merge requests found
...@@ -22,8 +22,63 @@ bool isOpperator(char ch) { ...@@ -22,8 +22,63 @@ bool isOpperator(char ch) {
return ch == '+' || ch == '-' || ch == 'x' || ch == '/' || ch == '^' || return ch == '+' || ch == '-' || ch == 'x' || ch == '/' || ch == '^' ||
ch == '*'; ch == '*';
} }
void print_array(char *arr, int len) {
for (int i = 0; i < len; i++) {
printf("%c", arr[i]);
}
printf("\n");
}
void print_stack_double(stack s){
while(!is_empty(s)){
printf("%.2f ",*(double*)pop(&s));
}
}
double eval_postfix(char* postfix) {
printf("%s\n\n\n",postfix);
stack s;
int len=strlen(postfix);
stack_init(&s, len);
double res, nb1, nb2;
for (int i = 1; i < len; i++) {
//printf("%c\n",postfix[i]);
if (postfix[i] < '0' || '9' < postfix[i]) { //if operator
nb1 = *(double*)pop(&s);
nb2 = *(double*)pop(&s);
}
switch (postfix[i]) {
case '+':
res = nb2 + nb1;
break;
case '-':
res = nb2 - nb1;
break;
case 'x':
case '*':
res = nb2 * nb1;
break;
case '/':
res = nb2 / nb1;
break;
case '^':
res= pow(nb1,nb2);
break;
default:
res = atoi(postfix);
}
push(&s, &res);
}
print_stack_double(s);
printf("%f\n", *(double*)pop(&s));
stack_destroy(&s);
return 12;
}
char *infix_to_postfix(char *infix) { char *infix_to_postfix(char *infix) {
int infix_size=strlen(infix); int infix_size = strlen(infix);
printf("input: %s\n", infix);
int postfix_size = infix_size + 1; int postfix_size = infix_size + 1;
stack s; stack s;
stack_init(&s, infix_size); stack_init(&s, infix_size);
...@@ -34,10 +89,10 @@ char *infix_to_postfix(char *infix) { ...@@ -34,10 +89,10 @@ char *infix_to_postfix(char *infix) {
// postfix_size--; // postfix_size--;
// } // }
// } // }
char *postfix = malloc(sizeof(char) * postfix_size); char *postfix = malloc(sizeof(char) * postfix_size);
int j = 0; // index for returned array int j = 0; // index for returned array
//print_array(postfix, postfix_size);
for (int i = 0; i < infix_size; i++) { for (int i = 0; i < infix_size; i++) {
if (infix[i] >= '0' && infix[i] <= '9') { // number if (infix[i] >= '0' && infix[i] <= '9') { // number
postfix[j] = infix[i]; postfix[j] = infix[i];
...@@ -46,21 +101,21 @@ char *infix_to_postfix(char *infix) { ...@@ -46,21 +101,21 @@ char *infix_to_postfix(char *infix) {
push(&s, &infix[i]); push(&s, &infix[i]);
} else if (infix[i] == ')') { // ) } else if (infix[i] == ')') { // )
while (!is_empty(s) && *(char *)peek(s) != '(') { while (!is_empty(s) && *(char *)peek(s) != '(') {
printf("%c\n",*(char*)peek(s)); // printf("%c\n", *(char *)peek(s));
postfix[j] = *(char *)pop(&s); postfix[j] = *(char *)pop(&s);
j++; j++;
} }
if (!is_empty(s) && *(char *)peek(s) == '(') { //ici ? if (!is_empty(s) && *(char *)peek(s) == '(') { // ici ?
pop(&s); pop(&s);
} }
} else if (isOpperator(infix[i])) { // opp } else if (isOpperator(infix[i])) {
// opp
while (!is_empty(s) && (getPrio(infix[i]) <= getPrio(*(char *)peek(s)))) { while (!is_empty(s) && (getPrio(infix[i]) <= getPrio(*(char *)peek(s)))) {
postfix[j] = *(char *)pop(&s); postfix[j] = *(char *)pop(&s);
j++; j++;
} }
push(&s, &infix[i]); push(&s, &infix[i]);
}else{ } else {
printf("NULL ahhh!!\n"); printf("NULL ahhh!!\n");
free(postfix); free(postfix);
stack_destroy(&s); stack_destroy(&s);
...@@ -71,10 +126,10 @@ char *infix_to_postfix(char *infix) { ...@@ -71,10 +126,10 @@ char *infix_to_postfix(char *infix) {
postfix[j] = *(char *)pop(&s); postfix[j] = *(char *)pop(&s);
j++; j++;
} }
printf("%d\n",j); // printf("%d\n", j);
postfix[j]='\0'; postfix[j] = '\0';
printf("%s",postfix); printf("output: %s\n", postfix);
printf("\n");
// printf("%d\n",j); // printf("%d\n",j);
stack_destroy(&s); stack_destroy(&s);
return postfix; return postfix;
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
char* infix_to_postfix(char* infix); char* infix_to_postfix(char* infix);
char* toPostfix(char* infix,int len); char* toPostfix(char* infix,int len);
double eval_postfix(char* postfix);
int getPrio(char opp); int getPrio(char opp);
......
...@@ -4,9 +4,9 @@ ...@@ -4,9 +4,9 @@
int main() { int main() {
char *infix="2/(7-3*2)"; char *infix="7+2*3";
char *postfix=infix_to_postfix(infix); char *postfix=infix_to_postfix(infix);
eval_postfix(postfix);
// for(int i=0;i<len;i++){ // for(int i=0;i<len;i++){
// printf("%c ",postfix[i]); // printf("%c ",postfix[i]);
// } // }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment