Update du3/program.c

This commit is contained in:
Denis Landa 2025-10-16 17:25:01 +00:00
parent 953e42ee1b
commit f8efe6e386

View File

@ -18,7 +18,7 @@ void print_stack(struct stack *s) {
void push_stack(struct stack *s, float value) {
if (s->size >= STACK_SIZE) {
printf("full stack\n");
printf("no input\n");
exit(0);
}
s->values[s->size] = value;
@ -27,7 +27,7 @@ void push_stack(struct stack *s, float value) {
float pop_stack(struct stack *s) {
if (s->size <= 0) {
printf("not enough operands\n");
printf("no input\n");
exit(0);
}
s->size--;
@ -45,7 +45,7 @@ int main() {
strcmp(input, "*") == 0 || strcmp(input, "/") == 0) {
if (s.size < 2) {
printf("not anough operands\n");
printf("no input\n");
exit(0);
}
@ -53,6 +53,11 @@ int main() {
float a = pop_stack(&s);
float result;
if (strcmp(input, "/") == 0 && b == 0) {
printf("division by zero\n");
exit(0);
}
if (strcmp(input, "+") == 0) result = a + b;
else if (strcmp(input, "-") == 0) result = a - b;
else if (strcmp(input, "*") == 0) result = a * b;
@ -65,14 +70,14 @@ int main() {
char *endptr;
float val = strtof(input, &endptr);
if (*endptr != '\0') {
printf("division by zero\n");
printf("no input\n");
exit(0);
}
push_stack(&s, val);
print_stack(&s);
}
}
printf("no input\n");
printf("no input\n");
return 0;
}