This commit is contained in:
Deinerovych 2024-10-16 22:38:56 +02:00
parent 5bc9d6139c
commit 3be4df45de

View File

@ -33,7 +33,7 @@ float calculator(float n1, float n2, char operation) {
return n1 / n2; return n1 / n2;
} else { } else {
printf("division by zero\n"); printf("division by zero\n");
return n1; exit(0);
} }
} }
return 0; return 0;
@ -43,7 +43,8 @@ void push(float number) {
if (stack_top < STACK_SIZE - 1) { if (stack_top < STACK_SIZE - 1) {
stack[++stack_top] = number; stack[++stack_top] = number;
} else { } else {
printf("stack overflow\n"); printf("Error: stack overflow\n");
exit(0);
} }
} }
@ -51,21 +52,19 @@ float pop() {
if (stack_top >= 0) { if (stack_top >= 0) {
return stack[stack_top--]; return stack[stack_top--];
} else { } else {
printf("stack is empty\n"); printf("Error: stack is empty\n");
return 0; exit(0);
} }
} }
void print_stack() { void print_stack() {
if (stack_top >= 0) { for (int i = 0; i <= stack_top; i++) {
for (int i = 0; i <= stack_top; i++) { printf("%.2f", stack[i]);
printf("%.2f", stack[i]); if (i < stack_top) {
if (i < stack_top) { printf(" ");
printf(" ");
}
} }
printf(" \n");
} }
printf(" \n");
} }
int main() { int main() {
@ -93,13 +92,10 @@ int main() {
float n1 = pop(); float n1 = pop();
char operation = arr[0]; char operation = arr[0];
float result = calculator(n1, n2, operation); float result = calculator(n1, n2, operation);
if (!(operation == '/' && n2 == 0)) { push(result);
push(result); print_stack();
print_stack();
}
} }
} }
printf("no input\n"); printf("no input\n");
return 0; return 0;