Update du3/program.c
This commit is contained in:
parent
953e42ee1b
commit
f8efe6e386
@ -18,7 +18,7 @@ void print_stack(struct stack *s) {
|
|||||||
|
|
||||||
void push_stack(struct stack *s, float value) {
|
void push_stack(struct stack *s, float value) {
|
||||||
if (s->size >= STACK_SIZE) {
|
if (s->size >= STACK_SIZE) {
|
||||||
printf("full stack\n");
|
printf("no input\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
s->values[s->size] = value;
|
s->values[s->size] = value;
|
||||||
@ -27,7 +27,7 @@ void push_stack(struct stack *s, float value) {
|
|||||||
|
|
||||||
float pop_stack(struct stack *s) {
|
float pop_stack(struct stack *s) {
|
||||||
if (s->size <= 0) {
|
if (s->size <= 0) {
|
||||||
printf("not enough operands\n");
|
printf("no input\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
s->size--;
|
s->size--;
|
||||||
@ -45,7 +45,7 @@ int main() {
|
|||||||
strcmp(input, "*") == 0 || strcmp(input, "/") == 0) {
|
strcmp(input, "*") == 0 || strcmp(input, "/") == 0) {
|
||||||
|
|
||||||
if (s.size < 2) {
|
if (s.size < 2) {
|
||||||
printf("not anough operands\n");
|
printf("no input\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,6 +53,11 @@ int main() {
|
|||||||
float a = pop_stack(&s);
|
float a = pop_stack(&s);
|
||||||
float result;
|
float result;
|
||||||
|
|
||||||
|
if (strcmp(input, "/") == 0 && b == 0) {
|
||||||
|
printf("division by zero\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
if (strcmp(input, "+") == 0) result = a + b;
|
if (strcmp(input, "+") == 0) result = a + b;
|
||||||
else if (strcmp(input, "-") == 0) result = a - b;
|
else 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;
|
char *endptr;
|
||||||
float val = strtof(input, &endptr);
|
float val = strtof(input, &endptr);
|
||||||
if (*endptr != '\0') {
|
if (*endptr != '\0') {
|
||||||
printf("division by zero\n");
|
printf("no input\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
push_stack(&s, val);
|
push_stack(&s, val);
|
||||||
print_stack(&s);
|
print_stack(&s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("no input\n");
|
|
||||||
|
|
||||||
|
printf("no input\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user