Update cv3/program.c

This commit is contained in:
Marat Izmailov 2024-10-17 11:38:16 +00:00
parent 2a8785df19
commit b4839ed949

View File

@ -2,114 +2,111 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <ctype.h>
#define STACK_SIZE 10 #define STACK_SIZE 10
struct stack { struct stack {
float values[STACK_SIZE]; float values[STACK_SIZE];
int size; int size;
}; };
void push_stack(struct stack* stack, float value);
float pop_stack(struct stack* stack);
void print_stack(struct stack* stack);
int is_number(const char* str);
int main() { void init_stack(struct stack* s) {
struct stack mystack; memset(s, 0, sizeof(struct stack));
memset(&mystack, 0, sizeof(struct stack));
char input[50];
int empty_input = 0;
while (1) {
if (!fgets(input, sizeof(input), stdin)) {
break;
}
input[strcspn(input, "\n")] = 0;
if (strlen(input) == 0) {
empty_input = 1;
break;
}
if (is_number(input)) {
float value = atof(input);
push_stack(&mystack, value);
} else if (strlen(input) == 1 && strchr("+-*/", input[0])) {
if (mystack.size < 2) {
fprintf(stderr, "Error: Not enough values in stack.\n");
exit(EXIT_FAILURE);
}
float b = pop_stack(&mystack);
float a = pop_stack(&mystack);
float result;
switch (input[0]) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
if (b == 0) {
fprintf(stderr, "Error: Division by zero.\n");
exit(EXIT_FAILURE);
}
result = a / b;
break;
default:
fprintf(stderr, "Error: Invalid operator.\n");
exit(EXIT_FAILURE);
}
push_stack(&mystack, result);
} else {
fprintf(stderr, "Error: Invalid input.\n");
exit(EXIT_FAILURE);
}
print_stack(&mystack);
}
if (empty_input) {
printf("no input\n");
}
return 0;
} }
void push_stack(struct stack* s, float value) {
void push_stack(struct stack* stack, float value) { assert(s->size < STACK_SIZE);
assert(stack->size < STACK_SIZE); s->values[s->size] = value;
stack->values[stack->size] = value; s->size += 1;
stack->size += 1;
} }
float pop_stack(struct stack* stack) {
assert(stack->size > 0); float pop_stack(struct stack* s) {
stack->size -= 1; assert(s->size > 0);
return stack->values[stack->size]; s->size -= 1;
return s->values[s->size];
} }
void print_stack(struct stack* stack) {
for (int i = 0; i < stack->size; i++) { void print_stack(struct stack* s) {
printf("%.2f ", stack->values[i]); for (int i = 0; i < s->size; i++) {
printf("%.2f ", s->values[i]);
} }
printf("\n"); printf("\n");
} }
int is_number(const char* str) { int main() {
char* endptr; struct stack mystack;
strtod(str, &endptr); init_stack(&mystack);
return *endptr == '\0' && endptr != str;
char input[256];
while (1) {
printf("Zadajte číslo alebo operáciu (+, -, *, /): ");
if (!fgets(input, sizeof(input), stdin)) {
break;
}
input[strcspn(input, "\n")] = 0;
char* endptr;
float value = strtof(input, &endptr);
if (endptr != input) {
push_stack(&mystack, value);
print_stack(&mystack);
continue;
}
if (strcmp(input, "+") == 0) {
if (mystack.size < 2) {
printf("Chyba: Nedostatok hodnôt v zásobníku.\n");
break;
}
float b = pop_stack(&mystack);
float a = pop_stack(&mystack);
push_stack(&mystack, a + b);
print_stack(&mystack);
} else if (strcmp(input, "-") == 0) {
if (mystack.size < 2) {
printf("Chyba: Nedostatok hodnôt v zásobníku.\n");
break;
}
float b = pop_stack(&mystack);
float a = pop_stack(&mystack);
push_stack(&mystack, a - b);
print_stack(&mystack);
} else if (strcmp(input, "*") == 0) {
if (mystack.size < 2) {
printf("Chyba: Nedostatok hodnôt v zásobníku.\n");
break;
}
float b = pop_stack(&mystack);
float a = pop_stack(&mystack);
push_stack(&mystack, a * b);
print_stack(&mystack);
} else if (strcmp(input, "/") == 0) {
if (mystack.size < 2) {
printf("Chyba: Nedostatok hodnôt v zásobníku.\n");
break;
}
float b = pop_stack(&mystack);
float a = pop_stack(&mystack);
if (b == 0) {
printf("Chyba: Delenie nulou.\n");
break;
}
push_stack(&mystack, a / b);
print_stack(&mystack);
} else {
printf("Chyba: Neplatný vstup.\n");
break;
}
}
return 0;
} }