diff --git a/calculator/Makefile b/calculator/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..fd782fc2f2dda9e5b449842f0a414e3277f36ff0 --- /dev/null +++ b/calculator/Makefile @@ -0,0 +1,23 @@ +cc=gcc +LIBS=-Wextra -Wall -g -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) + +main: main.x + ./main.x + +main.x: calc.o main.c + $(cc) -o $@ $^ $(LIBS) + +calc.o: calc.c calc.h + $(cc) -c $^ $(LIBS) + +clean: + rm -f *.o *.x *.gch diff --git a/calculator/calc.c b/calculator/calc.c new file mode 100644 index 0000000000000000000000000000000000000000..32e4a68aec466c94237075d5a95228f2030f751f --- /dev/null +++ b/calculator/calc.c @@ -0,0 +1,122 @@ +#include "calc.h" +#include <stdlib.h> + +// typedef struct _stack { + +// void** data; +// int top; +// int capacity; + +// } stack; + +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_init(stack *stack, int size, int typeSize) { + stack->data = malloc(typeSize * size); + stack->top = -1; + stack->capacity = size; +} + +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; +} + +int getPrio(char opp) { + switch (opp) { + case '+': + case '-': + return 1; + case 'x': + case '/': + return 2; + case '^': + return 3; + default: + 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 new file mode 100644 index 0000000000000000000000000000000000000000..1720fc78faacc02c889d52dc53a4e8ddcdbe3223 --- /dev/null +++ b/calculator/calc.h @@ -0,0 +1,41 @@ +#ifndef _CALC_H_ +#define _CALC_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 max, int typeSize); + +void stack_destroy(stack *stack); + +void push(stack *stack, void* item); + +void* pop(stack *stack); + +char* toPostfix(char* infix,int len); + +int getPrio(char opp); + + + +#endif \ No newline at end of file diff --git a/calculator/main.c b/calculator/main.c new file mode 100644 index 0000000000000000000000000000000000000000..e881944d777d1dc29bc85e179b98f9de740e9420 --- /dev/null +++ b/calculator/main.c @@ -0,0 +1,34 @@ +#include "calc.h" +#include <stdio.h> +#include <string.h> +int main() { + +// stack s; +// stack_init(&s, 5, sizeof(int)); +// int *a = malloc(sizeof(int)); +// *a = 3; +// push(&s, a); +// printf("%d \n", *(int *)peek(s)); +// stack_destroy(&s); +// free(a); +char infix1[]="A+(B/C–D^E"; +int len=strlen(infix1); +printf("%d\n",len); +//char infix[5]={'a','+','b','x','c'}; +char *postfix=toPostfix(infix1, len); + +for(int i=0;i<len;i++){ + printf("%c ",postfix[i]); +} +printf("\n"); +free(postfix); + +// char *postfix1 = malloc(sizeof(char) * 5); +// stack s; +// int j = 0; + +// stack_init(&s, 5, sizeof(char)); +// postfix[0] = *((char *)pop(&s)); + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/calculator/test.c b/calculator/test.c new file mode 100644 index 0000000000000000000000000000000000000000..b35d215a61eb5ba32ccdfcec5eab22430abb3d06 --- /dev/null +++ b/calculator/test.c @@ -0,0 +1,10 @@ +#include <stdlib.h> +#include "calc.h" + + + +int main(){ + + return EXIT_SUCCESS; + +} \ No newline at end of file