usaa24/cv3/program.c

117 lines
2.8 KiB
C
Raw Normal View History

2024-10-12 20:09:28 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-10-17 16:43:40 +00:00
#include <math.h>
2024-10-12 20:09:28 +00:00
2024-10-17 16:36:28 +00:00
#define MAX_RAZMER 10
2024-10-16 22:44:59 +00:00
2024-10-12 20:09:28 +00:00
typedef struct {
double chisla[MAX_RAZMER];
int vershina;
} StEk;
void inicStEk(StEk* stek) {
stek->vershina = 0;
}
int isEmpty(StEk* stek) {
return stek->vershina == 0;
}
2024-10-16 22:44:59 +00:00
2024-10-17 16:36:28 +00:00
int isFull(StEk* stek) {
2024-10-12 20:09:28 +00:00
return stek->vershina == MAX_RAZMER;
}
void push(StEk* stek, double chislo) {
2024-10-17 16:36:28 +00:00
if (isFull(stek)) {
2024-10-12 20:09:28 +00:00
printf("no input\n");
exit(1);
}
stek->chisla[stek->vershina] = chislo;
stek->vershina++;
}
double pop(StEk* stek) {
2024-10-17 16:36:28 +00:00
if (isEmpty(stek)) {
2024-10-12 20:09:28 +00:00
printf("no input\n");
exit(1);
}
return stek->chisla[--stek->vershina];
}
int main() {
StEk stek;
inicStEk(&stek);
char bufer[256];
2024-10-17 16:41:23 +00:00
while (fgets(bufer, sizeof(bufer), stdin) != NULL) {
2024-10-12 20:09:28 +00:00
char* konec;
double chislo = strtod(bufer, &konec);
2024-10-17 16:36:28 +00:00
if (*konec == '\n' && *bufer != '\n' && *bufer != ' ') {
2024-10-12 20:09:28 +00:00
push(&stek, chislo);
2024-10-17 16:36:28 +00:00
} else if (strcmp(konec, "+\n") == 0) {
if (isEmpty(&stek)) {
2024-10-12 20:09:28 +00:00
printf("no input\n");
exit(1);
}
double b = pop(&stek);
2024-10-17 16:36:28 +00:00
if (isEmpty(&stek)) {
2024-10-12 20:09:28 +00:00
printf("no input\n");
exit(1);
}
double a = pop(&stek);
push(&stek, a + b);
2024-10-17 16:36:28 +00:00
} else if (strcmp(konec, "-\n") == 0) {
if (isEmpty(&stek)) {
2024-10-12 20:09:28 +00:00
printf("no input\n");
exit(1);
}
double b = pop(&stek);
2024-10-17 16:36:28 +00:00
if (isEmpty(&stek)) {
2024-10-12 20:09:28 +00:00
printf("no input\n");
exit(1);
}
double a = pop(&stek);
push(&stek, a - b);
2024-10-17 16:36:28 +00:00
} else if (strcmp(konec, "*\n") == 0) {
if (isEmpty(&stek)) {
2024-10-12 20:09:28 +00:00
printf("no input\n");
exit(1);
}
double b = pop(&stek);
2024-10-17 16:36:28 +00:00
if (isEmpty(&stek)) {
2024-10-12 20:09:28 +00:00
printf("no input\n");
exit(1);
}
double a = pop(&stek);
push(&stek, a * b);
2024-10-17 16:36:28 +00:00
} else if (strcmp(konec, "/\n") == 0) {
if (isEmpty(&stek)) {
2024-10-12 20:09:28 +00:00
printf("no input\n");
exit(1);
}
double b = pop(&stek);
2024-10-17 16:36:28 +00:00
if (isEmpty(&stek)) {
2024-10-12 20:09:28 +00:00
printf("no input\n");
exit(1);
}
2024-10-17 16:46:52 +00:00
2024-10-12 20:09:28 +00:00
double a = pop(&stek);
2024-10-17 16:43:40 +00:00
if (b == 0) {
printf("division by zero\n");
2024-10-17 16:46:52 +00:00
return 0;
2024-10-17 16:43:40 +00:00
exit(1);
}
2024-10-12 20:09:28 +00:00
push(&stek, a / b);
} else {
printf("no input\n");
exit(1);
}
2024-10-17 16:36:28 +00:00
for (int i = 0; i < stek.vershina; i++) {
2024-10-12 20:09:28 +00:00
printf("%.2lf ", stek.chisla[i]);
}
printf("\n");
}
2024-10-17 16:41:23 +00:00
printf("no input\n");
2024-10-12 20:09:28 +00:00
return 0;
2024-10-17 16:46:52 +00:00
}