Update cv3/program.c
This commit is contained in:
parent
b2f01528fb
commit
374ae747b0
@ -11,7 +11,6 @@ struct stack {
|
|||||||
int size;
|
int size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void push_stack(struct stack* stack, float value);
|
void push_stack(struct stack* stack, float value);
|
||||||
float pop_stack(struct stack* stack);
|
float pop_stack(struct stack* stack);
|
||||||
void print_stack(struct stack* stack);
|
void print_stack(struct stack* stack);
|
||||||
@ -23,25 +22,22 @@ int main() {
|
|||||||
|
|
||||||
char input[50];
|
char input[50];
|
||||||
while (1) {
|
while (1) {
|
||||||
printf("Enter a number or an operator (+, -, *, /): ");
|
|
||||||
if (!fgets(input, sizeof(input), stdin)) {
|
if (!fgets(input, sizeof(input), stdin)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
input[strcspn(input, "\n")] = 0;
|
|
||||||
|
|
||||||
|
input[strcspn(input, "\n")] = 0;
|
||||||
|
|
||||||
if (strlen(input) == 0) {
|
if (strlen(input) == 0) {
|
||||||
printf("no input\n");
|
printf("no input\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_number(input)) {
|
if (is_number(input)) {
|
||||||
|
|
||||||
float value = atof(input);
|
float value = atof(input);
|
||||||
push_stack(&mystack, value);
|
push_stack(&mystack, value);
|
||||||
} else if (strlen(input) == 1 && strchr("+-*/", input[0])) {
|
} else if (strlen(input) == 1 && strchr("+-*/", input[0])) {
|
||||||
|
|
||||||
if (mystack.size < 2) {
|
if (mystack.size < 2) {
|
||||||
fprintf(stderr, "Error: Not enough values in stack.\n");
|
fprintf(stderr, "Error: Not enough values in stack.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -84,23 +80,21 @@ int main() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void push_stack(struct stack* stack, float value) {
|
void push_stack(struct stack* stack, float value) {
|
||||||
assert(stack->size < STACK_SIZE);
|
assert(stack->size < STACK_SIZE);
|
||||||
stack->values[stack->size] = value;
|
stack->values[stack->size] = value;
|
||||||
stack->size += 1;
|
stack->size += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
float pop_stack(struct stack* stack) {
|
float pop_stack(struct stack* stack) {
|
||||||
assert(stack->size > 0);
|
assert(stack->size > 0);
|
||||||
stack->size -= 1;
|
stack->size -= 1;
|
||||||
return stack->values[stack->size];
|
return stack->values[stack->size];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void print_stack(struct stack* stack) {
|
void print_stack(struct stack* stack) {
|
||||||
for (int i = 0; i < stack->size; i++) {
|
for (int i = 0; i < stack->size; i++) {
|
||||||
|
|
||||||
printf("%.2f ", stack->values[i]);
|
printf("%.2f ", stack->values[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user