From e5d98251ff1a2c13501f085e4432aa03a1f6a5c5 Mon Sep 17 00:00:00 2001 From: thib <tempo2riz@gmail.com> Date: Wed, 17 Mar 2021 00:47:22 +0100 Subject: [PATCH] aled --- calculator/calc.c | 14 +++++++------- calculator/main.c | 18 ++++-------------- calculator/test.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/calculator/calc.c b/calculator/calc.c index 32e4a68..3532fc7 100644 --- a/calculator/calc.c +++ b/calculator/calc.c @@ -9,6 +9,12 @@ // } stack; +void stack_init(stack *stack, int size, int typeSize) { + stack->data = malloc(typeSize * 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; } @@ -22,12 +28,6 @@ void *peek(stack stack) { 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; @@ -68,7 +68,7 @@ char *toPostfix(char *infix, int len) { char *postfix = malloc(sizeof(char) * len); int j = 0; - stack_init(&s, len, sizeof(char)); + stack_init(&s, len, sizeof(char*)); for (int i = 0; i < len; ++i) { if (infix[i] == ')') { diff --git a/calculator/main.c b/calculator/main.c index e881944..9b307ec 100644 --- a/calculator/main.c +++ b/calculator/main.c @@ -3,19 +3,9 @@ #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); +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]); @@ -23,7 +13,7 @@ for(int i=0;i<len;i++){ printf("\n"); free(postfix); -// char *postfix1 = malloc(sizeof(char) * 5); +// // char *postfix1 = malloc(sizeof(char) * 5); // stack s; // int j = 0; diff --git a/calculator/test.c b/calculator/test.c index b35d215..8188219 100644 --- a/calculator/test.c +++ b/calculator/test.c @@ -5,6 +5,40 @@ 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; } \ No newline at end of file -- GitLab