Update cv3/program.c

This commit is contained in:
Marat Izmailov 2024-10-17 12:19:39 +00:00
parent 45b15ad033
commit 768000285e

View File

@ -1,116 +1,81 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#define STACK_SIZE 10 #define STACK_SIZE 10
double stack[STACK_SIZE];
int top = 0;
struct stack { void print_stack() {
float values[STACK_SIZE]; for (int i = 0; i < top; i++) {
int size; printf("%.2f ", stack[i]);
};
void init_stack(struct stack* s) {
memset(s, 0, sizeof(struct stack));
}
void push_stack(struct stack* s, float value) {
assert(s->size < STACK_SIZE);
s->values[s->size] = value;
s->size += 1;
}
float pop_stack(struct stack* s) {
assert(s->size > 0);
s->size -= 1;
return s->values[s->size];
}
void print_stack(struct stack* s) {
for (int i = 0; i < s->size; i++) {
printf("%.2f ", s->values[i]);
} }
printf("\n"); printf("\n");
} }
int push(double value) {
int is_number(const char* str) { if (top >= STACK_SIZE) {
char* endptr; printf("full stack\n");
strtof(str, &endptr); return 0;
return endptr != str && *endptr == '\0'; }
stack[top++] = value;
print_stack();
return 1;
} }
int pop(double *value) {
int main() { if (top <= 0) {
struct stack mystack; printf("not enough operands\n");
init_stack(&mystack); return 0;
}
char input[20]; *value = stack[--top];
return 1;
while (1) {
printf("Enter a number or an operation (+, -, *, /): ");
if (!fgets(input, sizeof(input), stdin)) {
break;
} }
int apply_operation(char op) {
double a, b;
input[strcspn(input, "\n")] = 0; if (!pop(&b) || !pop(&a)) {
return 0;
if (is_number(input)) {
float value = strtof(input, NULL);
push_stack(&mystack, value);
print_stack(&mystack);
} else {
if (mystack.size < 2) {
printf("Error: Not enough values on the stack to perform operation.\n");
break;
} }
float b = pop_stack(&mystack);
float a = pop_stack(&mystack);
float result = 0;
switch (input[0]) { double result;
case '+': switch (op) {
result = a + b; case '+': result = a + b; break;
break; case '-': result = a - b; break;
case '-': case '*': result = a * b; break;
result = a - b;
break;
case '*':
result = a * b;
break;
case '/': case '/':
if (b == 0) { if (b == 0) {
printf("Error: Division by zero.\n"); printf("division by zero\n");
return 0;
push_stack(&mystack, a);
push_stack(&mystack, b);
print_stack(&mystack);
continue;
} }
result = a / b; result = a / b;
break; break;
default: default:
printf("Error: Unknown operation '%s'.\n", input); printf("bad input\n");
return 0;
push_stack(&mystack, a); }
push_stack(&mystack, b);
print_stack(&mystack); return push(result);
continue; }
}
int main() {
push_stack(&mystack, result); char input[100];
print_stack(&mystack);
} while (scanf("%s", input) == 1) {
} char *end;
double value = strtod(input, &end);
if (*end == '\0') {
if (!push(value)) return 0;
} else if (input[1] == '\0' && strchr("+-*/", input[0])) {
if (!apply_operation(input[0])) return 0;
} else {
printf("bad input\n");
return 0;
}
}
printf("no input\n");
return 0; return 0;
} }