Обновить cv3/program.c

This commit is contained in:
Yevhen Kozirovskyi 2024-10-17 16:36:28 +00:00
parent 4debe4d069
commit e713389776

View File

@ -2,13 +2,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define MAX_RAZMER 100 #define MAX_RAZMER 10
typedef struct { typedef struct {
double chisla[MAX_RAZMER]; double chisla[MAX_RAZMER];
int vershina; int vershina;
} StEk; } StEk;
@ -19,13 +16,13 @@ void inicStEk(StEk* stek) {
int isEmpty(StEk* stek) { int isEmpty(StEk* stek) {
return stek->vershina == 0; return stek->vershina == 0;
} }
int isFull(StEk* stek) {
int isFull(StEk* stek) {
return stek->vershina == MAX_RAZMER; return stek->vershina == MAX_RAZMER;
} }
void push(StEk* stek, double chislo) { void push(StEk* stek, double chislo) {
if(isFull(stek)) { if (isFull(stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
@ -33,9 +30,8 @@ void push(StEk* stek, double chislo) {
stek->vershina++; stek->vershina++;
} }
double pop(StEk* stek) { double pop(StEk* stek) {
if(isEmpty(stek)) { if (isEmpty(stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
@ -43,58 +39,57 @@ double pop(StEk* stek) {
} }
int main() { int main() {
StEk stek; StEk stek;
inicStEk(&stek); inicStEk(&stek);
char bufer[256]; char bufer[256];
while(fgets(bufer, sizeof(bufer), stdin)) { while (fgets(bufer, sizeof(bufer), stdin)) {
char* konec; char* konec;
double chislo = strtod(bufer, &konec); double chislo = strtod(bufer, &konec);
if(*konec == '\n' && *bufer != '\n' && *bufer != ' ') { if (*konec == '\n' && *bufer != '\n' && *bufer != ' ') {
push(&stek, chislo); push(&stek, chislo);
} else if(strcmp(konec, "+\n") == 0) { } else if (strcmp(konec, "+\n") == 0) {
if(isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
double b = pop(&stek); double b = pop(&stek);
if(isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
double a = pop(&stek); double a = pop(&stek);
push(&stek, a + b); push(&stek, a + b);
} else if(strcmp(konec, "-\n") == 0) { } else if (strcmp(konec, "-\n") == 0) {
if(isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
double b = pop(&stek); double b = pop(&stek);
if(isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
double a = pop(&stek); double a = pop(&stek);
push(&stek, a - b); push(&stek, a - b);
} else if(strcmp(konec, "*\n") == 0) { } else if (strcmp(konec, "*\n") == 0) {
if(isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
double b = pop(&stek); double b = pop(&stek);
if(isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
double a = pop(&stek); double a = pop(&stek);
push(&stek, a * b); push(&stek, a * b);
} else if(strcmp(konec, "/\n") == 0) { } else if (strcmp(konec, "/\n") == 0) {
if(isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
double b = pop(&stek); double b = pop(&stek);
if(isEmpty(&stek)) { if (isEmpty(&stek)) {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
@ -104,7 +99,7 @@ int main() {
printf("no input\n"); printf("no input\n");
exit(1); exit(1);
} }
for(int i = 0; i < stek.vershina; i++) { for (int i = 0; i < stek.vershina; i++) {
printf("%.2lf ", stek.chisla[i]); printf("%.2lf ", stek.chisla[i]);
} }
printf("\n"); printf("\n");