diff --git a/calculator/calc.c b/calculator/calc.c index 32e4a68aec466c94237075d5a95228f2030f751f..3532fc787ed00a14b037ff71d0b9ddd1b4b732c2 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 e881944d777d1dc29bc85e179b98f9de740e9420..9b307ecf182abbed6ccdad8bb47fdd46948cddaf 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 b35d215a61eb5ba32ccdfcec5eab22430abb3d06..8188219b91732e7f05245241559bca18ebce521d 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