#include #include #include #include #define STACK_SIZE 10 struct stack_glavny { float data[STACK_SIZE]; int size; }; void initialize(struct stack_glavny* stack) { stack->size = -1; } int push(struct stack_glavny* stack, float value) { if (stack->size < STACK_SIZE - 1) { stack->size++; stack->data[stack->size] = value; return true; } else { return false; } } int pop(struct stack_glavny* stack, float* result) { if (stack->size >= 0) { *result = stack->data[stack->size]; stack->size--; return true; } else { return false; } } int operation_insert_result(struct stack_glavny* stack, char operation) { float operand1, operand2, result; if (pop(stack, &operand2) && pop(stack, &operand1)) { switch (operation) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': if (operand2 == 0) { printf("division by zero\n"); return false; } result = operand1 / operand2; break; default: //printf("Chyba: Neplatná operácia.\n"); return false; } if (!push(stack, result)) { //printf("Chyba: Zásobník je plný.\n"); return false; } return true; } else { printf("not enough operands\n"); return false; } } int main() { struct stack_glavny stack; initialize(&stack); char input[20]; while (1) { char* a = fgets(input, sizeof(input), stdin); if (a == NULL){ printf("no input\n"); break; } float value; //strchr aj strrchr hľadá prvý výskyt znaku určeného znakom v reťazci, na ktorý reťazec ukazuje. if (sscanf(input, "%f", &value) == 1 && strchr(input, '.') == strrchr(input, '.')) { if (!push(&stack, value)) { printf("bad input\n"); break; } } else if (input[0] == '+' || input[0] == '-' || input[0] == '*' || input[0] == '/') { if (!operation_insert_result(&stack, input[0])) { break; } } else { printf("bad input\n"); break; } for (int i = 0; i <= stack.size; i++) { printf("%.2f ", stack.data[i]); } printf("\n"); } return 0; }