2024-10-14 11:44:22 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
2024-10-14 13:54:52 +00:00
|
|
|
#include <ctype.h>
|
2024-10-14 13:52:30 +00:00
|
|
|
#include <string.h>
|
2024-10-14 11:44:22 +00:00
|
|
|
|
|
|
|
#define MAX_VELKOST 10
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
float zasobnik[MAX_VELKOST];
|
|
|
|
int vrchol;
|
|
|
|
} KalkulackaZasobnik;
|
|
|
|
|
|
|
|
void inicializuj(KalkulackaZasobnik *kalkulacka) {
|
|
|
|
kalkulacka->vrchol = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool vloz(KalkulackaZasobnik *kalkulacka, float hodnota) {
|
|
|
|
if (kalkulacka->vrchol >= MAX_VELKOST - 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
kalkulacka->zasobnik[++kalkulacka->vrchol] = hodnota;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool vyber(KalkulackaZasobnik *kalkulacka, float *a, float *b) {
|
|
|
|
if (kalkulacka->vrchol < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*a = kalkulacka->zasobnik[kalkulacka->vrchol--];
|
|
|
|
*b = kalkulacka->zasobnik[kalkulacka->vrchol--];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool scitat(KalkulackaZasobnik *kalkulacka) {
|
|
|
|
float a, b;
|
|
|
|
if (vyber(kalkulacka, &a, &b)) {
|
|
|
|
return vloz(kalkulacka, b + a);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool odcitat(KalkulackaZasobnik *kalkulacka) {
|
|
|
|
float a, b;
|
|
|
|
if (vyber(kalkulacka, &a, &b)) {
|
|
|
|
return vloz(kalkulacka, b - a);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nasobit(KalkulackaZasobnik *kalkulacka) {
|
|
|
|
float a, b;
|
|
|
|
if (vyber(kalkulacka, &a, &b)) {
|
|
|
|
return vloz(kalkulacka, a * b);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool delit(KalkulackaZasobnik *kalkulacka) {
|
|
|
|
float a, b;
|
|
|
|
if (vyber(kalkulacka, &a, &b)) {
|
2024-10-14 12:38:06 +00:00
|
|
|
if (a == 0) {
|
2024-10-14 13:52:30 +00:00
|
|
|
return false;
|
2024-10-14 12:38:06 +00:00
|
|
|
}
|
2024-10-14 11:44:22 +00:00
|
|
|
return vloz(kalkulacka, b / a);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zobraz_zasobnik(KalkulackaZasobnik *kalkulacka) {
|
|
|
|
for (int i = 0; i <= kalkulacka->vrchol; i++) {
|
|
|
|
printf("%.2f ", kalkulacka->zasobnik[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2024-10-14 13:23:54 +00:00
|
|
|
bool je_platna_operacia(char operacia) {
|
|
|
|
return operacia == '+' || operacia == '-' || operacia == '*' || operacia == '/';
|
|
|
|
}
|
|
|
|
|
2024-10-14 14:01:49 +00:00
|
|
|
|
2024-10-14 13:54:52 +00:00
|
|
|
bool je_platne_cislo(const char *vstup) {
|
|
|
|
char *koniec;
|
2024-10-14 14:01:49 +00:00
|
|
|
strtof(vstup, &koniec);
|
|
|
|
|
2024-10-14 13:54:52 +00:00
|
|
|
|
|
|
|
return *koniec == '\n' || *koniec == '\0';
|
|
|
|
}
|
|
|
|
|
2024-10-14 14:01:49 +00:00
|
|
|
|
2024-10-14 13:57:21 +00:00
|
|
|
bool dost_operantov(KalkulackaZasobnik *kalkulacka) {
|
2024-10-14 14:07:37 +00:00
|
|
|
return kalkulacka->vrchol <= 1;
|
2024-10-14 13:57:21 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 11:44:22 +00:00
|
|
|
int main() {
|
|
|
|
KalkulackaZasobnik kalkulacka;
|
|
|
|
inicializuj(&kalkulacka);
|
|
|
|
char vstup[100];
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
fgets(vstup, sizeof(vstup), stdin);
|
|
|
|
|
|
|
|
if (vstup[0] == '\n') {
|
2024-10-14 13:52:30 +00:00
|
|
|
break;
|
2024-10-14 11:44:22 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 13:54:52 +00:00
|
|
|
if (je_platne_cislo(vstup)) {
|
2024-10-14 14:01:49 +00:00
|
|
|
float hodnota = strtof(vstup, NULL);
|
2024-10-14 13:54:52 +00:00
|
|
|
vloz(&kalkulacka, hodnota);
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
2024-10-14 13:52:30 +00:00
|
|
|
}
|
2024-10-14 14:02:59 +00:00
|
|
|
else if (strlen(vstup) == 2 && je_platna_operacia(vstup[0])) {
|
2024-10-14 13:54:52 +00:00
|
|
|
char operacia = vstup[0];
|
2024-10-14 11:44:22 +00:00
|
|
|
|
2024-10-14 13:57:21 +00:00
|
|
|
if (!dost_operantov(&kalkulacka)) {
|
|
|
|
printf("not enough operands\n");
|
2024-10-14 14:03:43 +00:00
|
|
|
return 0;
|
2024-10-14 13:57:21 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 13:27:53 +00:00
|
|
|
switch (operacia) {
|
|
|
|
case '+':
|
2024-10-14 14:02:59 +00:00
|
|
|
if (scitat(&kalkulacka)) {
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
|
|
|
} else {
|
|
|
|
printf("no input\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-10-14 13:27:53 +00:00
|
|
|
break;
|
|
|
|
case '-':
|
2024-10-14 14:02:59 +00:00
|
|
|
if (odcitat(&kalkulacka)) {
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
|
|
|
} else {
|
|
|
|
printf("no input\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-10-14 13:27:53 +00:00
|
|
|
break;
|
|
|
|
case '*':
|
2024-10-14 14:02:59 +00:00
|
|
|
if (nasobit(&kalkulacka)) {
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
|
|
|
} else {
|
|
|
|
printf("no input\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-10-14 13:27:53 +00:00
|
|
|
break;
|
2024-10-14 14:02:59 +00:00
|
|
|
case '/': {
|
|
|
|
float a, b;
|
|
|
|
if (vyber(&kalkulacka, &a, &b)) {
|
|
|
|
if (a == 0) {
|
|
|
|
printf("division by zero\n");
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
vloz(&kalkulacka, b / a);
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("no input\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-10-14 13:27:53 +00:00
|
|
|
break;
|
2024-10-14 14:02:59 +00:00
|
|
|
}
|
2024-10-14 11:44:22 +00:00
|
|
|
}
|
2024-10-14 13:44:34 +00:00
|
|
|
} else {
|
2024-10-14 13:35:21 +00:00
|
|
|
printf("bad input\n");
|
2024-10-14 13:52:30 +00:00
|
|
|
return 0;
|
2024-10-14 11:44:22 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-14 12:01:43 +00:00
|
|
|
|
2024-10-14 11:44:22 +00:00
|
|
|
return 0;
|
2024-10-14 12:41:28 +00:00
|
|
|
}
|