int main() { struct Stack * s = Stack_create(true); Stack_push(s, Obj_bool_create(true)); Stack_push(s, Obj_char_create('A')); Stack_push(s, Obj_int_create(42)); Stack_push(s, Obj_double_create(1234.5678)); printf("POP %f\n", *(double*)Stack_pop(s)); printf("POP %d\n", *(int*)Stack_pop(s)); printf("TOP %c\n", *(char*)Stack_top(s)); printf("TOP %c\n", *(char*)Stack_top(s)); Stack_clear(s); printf("EMPTY? %d\n", (int)Stack_isEmpty(s)); Stack_push(s, Obj_bool_create(false)); printf("EMPTY? %d\n", (int)Stack_isEmpty(s)); Stack_push(s, Obj_char_create('B')); printf("POP %c\n", *(char*)Stack_pop(s)); Stack_push(s, Obj_int_create(666)); printf("POP %d\n", *(int*)Stack_pop(s)); Stack_push(s, Obj_double_create(321.654)); printf("TOP %f\n", *(double*)Stack_top(s)); Stack_delete(s); s = NULL; return EXIT_SUCCESS; }
int main(void) { struct Stack *psStack1; struct Stack *psStack2; int iSuccessful; /* Use a Stack object referenced by psStack1. */ psStack1 = Stack_new(); if (psStack1 == NULL) handleMemoryError(); iSuccessful = Stack_push(psStack1, 1.1); if (! iSuccessful) handleMemoryError(); iSuccessful = Stack_push(psStack1, 2.2); if (! iSuccessful) handleMemoryError(); iSuccessful = Stack_push(psStack1, 3.3); if (! iSuccessful) handleMemoryError(); while (! Stack_isEmpty(psStack1)) printf("%g\n", Stack_pop(psStack1)); /***********************************************/ /* Cannot access the fields of *psStack1 here. */ /***********************************************/ Stack_free(psStack1); /* Use a Stack object referenced by psStack2. */ psStack2 = Stack_new(); if (psStack2 == NULL) handleMemoryError(); iSuccessful = Stack_push(psStack2, 4.4); if (! iSuccessful) handleMemoryError(); iSuccessful = Stack_push(psStack2, 5.5); if (! iSuccessful) handleMemoryError(); iSuccessful = Stack_push(psStack2, 6.6); if (! iSuccessful) handleMemoryError(); while (! Stack_isEmpty(psStack2)) printf("%g\n", Stack_pop(psStack2)); /***********************************************/ /* Cannot access the fields of *psStack2 here. */ /***********************************************/ Stack_free(psStack2); return 0; }
/* EVALUATE THE EXPRESSION: */ static void evaluate(Token_T* oTokens, int length) { Stack_T oStack1; Stack_T oStack2; int i; /* Create and use a Stack of tokens. */ assert(oTokens != NULL); oStack1 = Stack_new(); oStack2 = Stack_new(); for (i = 0; i < length; i++) { if (Token_getType(oTokens[i]) == VALUE) Stack_push(oStack1, oTokens[i]); else if (Stack_isEmpty(oStack2)) Stack_push(oStack2, oTokens[i]); else if (precedence(oTokens[i], Stack_top(oStack2))) Stack_push(oStack2, oTokens[i]); else { /*ERROR HANDLING?*/ int result; Token_T value_1 = (Token_T)Stack_top(oStack1); Stack_pop(oStack1); Token_T operator = (Token_T)Stack_top(oStack2); Stack_pop(oStack2); Token_T value_2 = (Token_T)Stack_top(oStack1); Stack_pop(oStack1); switch (Token_getType(operator)) { /*LEFT TO RIGHT EVALUATION*/ case MULT: result = Token_getValue(value_1) * Token_getValue(value_2); break; case DIV: result = Token_getValue(value_2) / Token_getValue(value_1); break; case PLUS: result = Token_getValue(value_1) + Token_getValue(value_2); break; case MINUS: result = Token_getValue(value_2) - Token_getValue(value_1); break; default: assert(0); } Stack_push(oStack1, Token_newToken(VALUE, result)); Stack_push(oStack2, oTokens[i]); } } while (!Stack_isEmpty(oStack2)) { /*ERROR HANDLING? + MODULARITY MUCH????*/ int result; Token_T value_1 = (Token_T)Stack_top(oStack1); Stack_pop(oStack1); Token_T operator = (Token_T)Stack_top(oStack2); Stack_pop(oStack2); Token_T value_2 = (Token_T)Stack_top(oStack1); Stack_pop(oStack1); switch (Token_getType(operator)) { /*LEFT TO RIGHT EVALUATION*/ case MULT: result = Token_getValue(value_1) * Token_getValue(value_2); break; case DIV: result = Token_getValue(value_2) / Token_getValue(value_1); break; case PLUS: result = Token_getValue(value_1) + Token_getValue(value_2); break; case MINUS: result = Token_getValue(value_2) - Token_getValue(value_1); break; default: assert(1); } Stack_push(oStack1, Token_newToken(VALUE, result)); } printf("%d\n", Token_getValue((Token_T)Stack_top(oStack1))); Stack_free(oStack1); Stack_free(oStack2); }