diff --git a/calculator/Makefile b/calculator/Makefile index fd782fc2f2dda9e5b449842f0a414e3277f36ff0..499490322e597afda4ae594385ebc4d315c824a2 100644 --- a/calculator/Makefile +++ b/calculator/Makefile @@ -1,23 +1,18 @@ cc=gcc -LIBS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak -lm +CFLAGS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak +LFLAGS=-fsanitize=address -fsanitize=leak -lm test :test.x ./test.x -test.x: calc.o test.o - $(cc) -o $@ $^ $(LIBS) - -test.o: test.c - $(cc) -c $^ $(LIBS) +test.x: calc.o test.o stack.o + $(cc) -o $@ $^ $(CFLAGS) $(LFLAGS) main: main.x ./main.x -main.x: calc.o main.c - $(cc) -o $@ $^ $(LIBS) - -calc.o: calc.c calc.h - $(cc) -c $^ $(LIBS) +main.x: calc.o main.o stack.o + $(cc) -o $@ $^ $(CFLAGS) $(LFLAGS) clean: rm -f *.o *.x *.gch diff --git a/calculator/calc.c b/calculator/calc.c index 3532fc787ed00a14b037ff71d0b9ddd1b4b732c2..00da2fb0a097add0f7eb94580895816acf283a48 100644 --- a/calculator/calc.c +++ b/calculator/calc.c @@ -1,51 +1,83 @@ #include "calc.h" +#include "stack.h" #include <stdlib.h> +#include <string.h> -// typedef struct _stack { - -// void** data; -// int top; -// int capacity; - -// } stack; - -void stack_init(stack *stack, int size, int typeSize) { - stack->data = malloc(typeSize * size); - stack->top = -1; - stack->capacity = size; +void malloc_push(stack *s, char item) { + char *c = malloc(sizeof(char)); + *c = item; + push(s, c); } -bool is_empty(stack stack) { return stack.top == -1; } - -bool is_full(stack stack) { return stack.capacity - 1 == stack.top; } - -void *peek(stack stack) { - if (is_empty(stack)) { - return NULL; - } - return stack.data[stack.top]; -} - -int stack_count(stack stack) { return stack.top + 1; } - -void stack_destroy(stack *stack) { - free(stack->data); - stack->top = -1; - stack->capacity = -1; +// void opp(char c, stack s) { + +// if (!is_empty(s) && (getPrio(c) > getPrio(*(char *)peek(s)))) { +// push(&s, &c); +// } +// while (!is_empty(s) && (3 <= getPrio(*(char *)peek(s)))) { +// postfix[j++] = *(char *)pop(&s); +// } +// } +bool isOpperator(char ch) { + return ch == '+' || ch == '-' || ch == 'x' || ch == '/' || ch == '^' || + ch == '*'; } +char *infix_to_postfix(char *infix) { + int infix_size=strlen(infix); + int postfix_size = infix_size + 1; + stack s; + stack_init(&s, infix_size); + + // get final size + // for (int i = 0; i < infix_size; i++) { + // if (infix[i] == '(' || infix[i] == ')') { + // postfix_size--; + // } + // } + + char *postfix = malloc(sizeof(char) * postfix_size); + int j = 0; // index for returned array + + for (int i = 0; i < infix_size; i++) { + if (infix[i] >= '0' && infix[i] <= '9') { // number + postfix[j] = infix[i]; + j++; + } else if (infix[i] == '(') { // ( + push(&s, &infix[i]); + } else if (infix[i] == ')') { // ) + while (!is_empty(s) && *(char *)peek(s) != '(') { + printf("%c\n",*(char*)peek(s)); + postfix[j] = *(char *)pop(&s); + j++; + } + if (!is_empty(s) && *(char *)peek(s) == '(') { //ici ? + pop(&s); + } + } else if (isOpperator(infix[i])) { // opp -void push(stack *stack, void *item) { - if (!is_full(*stack)) { - stack->top++; - stack->data[stack->top] = item; + while (!is_empty(s) && (getPrio(infix[i]) <= getPrio(*(char *)peek(s)))) { + postfix[j] = *(char *)pop(&s); + j++; + } + push(&s, &infix[i]); + }else{ + printf("NULL ahhh!!\n"); + free(postfix); + stack_destroy(&s); + return NULL; + } } -} - -void *pop(stack *stack) { - void *tmp; - tmp = stack->data[stack->top]; - stack->top--; - return tmp; + while (!is_empty(s)) { + postfix[j] = *(char *)pop(&s); + j++; + } + printf("%d\n",j); + postfix[j]='\0'; + printf("%s",postfix); + printf("\n"); + // printf("%d\n",j); + stack_destroy(&s); + return postfix; } int getPrio(char opp) { @@ -62,61 +94,3 @@ int getPrio(char opp) { return 0; } } - -char *toPostfix(char *infix, int len) { - stack s; - char *postfix = malloc(sizeof(char) * len); - int j = 0; - - stack_init(&s, len, sizeof(char*)); - for (int i = 0; i < len; ++i) { - - if (infix[i] == ')') { - while (*(char *)peek(s) != '(') { - postfix[j++] = *(char *)pop(&s); - } - pop(&s); - } else if (infix[i] == '(') { - push(&s, &infix[i]); - } else if (infix[i] == '+' || infix[i] == '-') { - - if (!is_empty(s) && (1 > getPrio(*(char *)peek(s)))) { - push(&s, &infix[i]); - } - while (!is_empty(s) && (1 < getPrio(*(char *)peek(s)))) { - postfix[j++] = *(char *)pop(&s); - } - } else if (infix[i] == 'x' || infix[i] == '/') { - - // printf("print %c\n\n\n\n",peek()) - if (!is_empty(s)) { - printf("not empty\n"); - if ((2 > getPrio(*(char *)peek(s)))) { - //push(&s, &infix[i]); - } -printf("before\n"); - while (!is_empty(s)&&(2 < getPrio(*(char *)peek(s)))) { - printf("looooop\n"); - postfix[j++] = *(char *)pop(&s); - } - } - } else if (infix[i] == '^') { - if (!is_empty(s) && (3 > getPrio(*(char *)peek(s)))) { - push(&s, &infix[i]); - } - while (!is_empty(s) && (3 < getPrio(*(char *)peek(s)))) { - postfix[j++] = *(char *)pop(&s); - } - } else { // numer - postfix[j++] = infix[i]; - printf("nb %c\n", infix[i]); - } - printf("i :%d j: %d stack: %d\n", i, j, stack_count(s)); - } - while (!is_empty(s)) { - postfix[j++] = *(char *)pop(&s); - } - free(s.data); - - return postfix; -} diff --git a/calculator/calc.h b/calculator/calc.h index 1720fc78faacc02c889d52dc53a4e8ddcdbe3223..9e9a342608b95b44dc8c3ead0080275f47a2a0db 100644 --- a/calculator/calc.h +++ b/calculator/calc.h @@ -7,31 +7,7 @@ #include <stdio.h> #include <stdlib.h> - -typedef struct _stack { - - void **data; - int top; - int capacity; - -} stack; - -bool is_empty(stack stack); - -bool is_full(stack stack); - -void* peek(stack stack); - -int stack_count(stack stack); - -void stack_init(stack *stack, int max, int typeSize); - -void stack_destroy(stack *stack); - -void push(stack *stack, void* item); - -void* pop(stack *stack); - +char* infix_to_postfix(char* infix); char* toPostfix(char* infix,int len); int getPrio(char opp); diff --git a/calculator/main.c b/calculator/main.c index 9b307ecf182abbed6ccdad8bb47fdd46948cddaf..747033994ff370bfabf28b7b33aabaac97fc2942 100644 --- a/calculator/main.c +++ b/calculator/main.c @@ -3,14 +3,14 @@ #include <string.h> int main() { -int len=5; -char infix[5]={'a','+','b','x','c'}; -char *postfix=toPostfix(infix, len); -for(int i=0;i<len;i++){ - printf("%c ",postfix[i]); -} -printf("\n"); +char *infix="2/(7-3*2)"; +char *postfix=infix_to_postfix(infix); + +// for(int i=0;i<len;i++){ +// printf("%c ",postfix[i]); +// } +// printf("\n"); free(postfix); // // char *postfix1 = malloc(sizeof(char) * 5); diff --git a/calculator/stack.c b/calculator/stack.c new file mode 100644 index 0000000000000000000000000000000000000000..60887b3d40344cc0a1fbc0a91096af76cb7334b9 --- /dev/null +++ b/calculator/stack.c @@ -0,0 +1,42 @@ +#include "stack.h" +#include <stdlib.h> + + +void stack_init(stack *stack, int size) { + stack->data = malloc(sizeof(void*)* size); + stack->top = -1; + stack->capacity = size; +} + +bool is_empty(stack stack) { return stack.top == -1; } + +bool is_full(stack stack) { return stack.capacity - 1 == stack.top; } + +void *peek(stack stack) { + if (is_empty(stack)) { + return NULL; + } + return stack.data[stack.top]; +} + +int stack_count(stack stack) { return stack.top + 1; } + +void stack_destroy(stack *stack) { + free(stack->data); + stack->top = -1; + stack->capacity = -1; +} + +void push(stack *stack, void *item) { + if (!is_full(*stack)) { + stack->top++; + stack->data[stack->top] = item; + } +} + +void *pop(stack *stack) { + void *tmp; + tmp = stack->data[stack->top]; + stack->top--; + return tmp; +} \ No newline at end of file diff --git a/calculator/stack.h b/calculator/stack.h new file mode 100644 index 0000000000000000000000000000000000000000..445871f460d9e7ca263237ff41d8fbd9f37daa3c --- /dev/null +++ b/calculator/stack.h @@ -0,0 +1,37 @@ +#ifndef _STACK_H_ +#define _STACK_H_ + +#include <assert.h> +#include <math.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +typedef struct _stack { + + void **data; + int top; + int capacity; + +} stack; + + + +bool is_empty(stack stack); + +bool is_full(stack stack); + +void* peek(stack stack); + +int stack_count(stack stack); + +void stack_init(stack *stack, int size); + +void stack_destroy(stack *stack); + +void push(stack *stack, void* item); + +void* pop(stack *stack); + +#endif + diff --git a/calculator/test.c b/calculator/test.c index 8188219b91732e7f05245241559bca18ebce521d..3b687a5eb24fefd8ab5c0ccd435ae595f7002102 100644 --- a/calculator/test.c +++ b/calculator/test.c @@ -1,44 +1,20 @@ -#include <stdlib.h> #include "calc.h" +#include "stack.h" +#include <stdlib.h> +int main() { + stack s; + stack_init(&s, 3); -int main(){ - - stack s; - // stack_init(&s,1,sizeof(int*)); - // int x=3; - // push(&s,&x); - // printf("%d",*(int*)pop(&s)); - - stack s2; - stack_init(&s2,1,sizeof(char*)); - char c='a'; - push(&s2,&c); - printf("%c",*(char*)pop(&s2)); - - stack_init(&s,5,sizeof(int*)); - //char infix[5]={'a','+','b','x','c'}; - char infix[5]={1,2,3,4,5}; - for(int i=0;i<5;i++){ - // char *c=malloc(sizeof(char)); - // *c=infix[i]; - push(&s,&infix[5]); - } - printf("%d",*(int*)pop(&s)); - //char z=*(char*)pop(&s); - - for(int i=0;i<5;i++){ - // char *c=malloc(sizeof(char)); - // *c=infix[i]; - // printf("%c\n\n\n\n ",*(char*)pop(&s)); - //printf("%c",*(char*)pop(&s)); - } - - stack_destroy(&s); - stack_destroy(&s2); - //free(c); - - return EXIT_SUCCESS; + char *str = "abc"; + push(&s, &str[0]); + push(&s, &str[1]); + push(&s, &str[2]); + printf("%c\n", *(char *)pop(&s)); + printf("%c\n", *(char *)pop(&s)); + printf("%c\n", *(char *)pop(&s)); + stack_destroy(&s); + return EXIT_SUCCESS; } \ No newline at end of file