2024-10-14 11:44:22 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#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:11:19 +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:11:19 +00:00
|
|
|
bool je_platna_operacia(char operacia) {
|
|
|
|
return operacia == '+' || operacia == '-' || operacia == '*' || operacia == '/';
|
|
|
|
}
|
|
|
|
|
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 12:25:31 +00:00
|
|
|
break;
|
2024-10-14 11:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float hodnota;
|
|
|
|
char operacia;
|
|
|
|
|
2024-10-14 12:58:04 +00:00
|
|
|
if (sscanf(vstup, "%f", &hodnota) == 1) {
|
2024-10-14 11:44:22 +00:00
|
|
|
if (vloz(&kalkulacka, hodnota)) {
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
|
|
|
}
|
|
|
|
} else if (sscanf(vstup, "%c", &operacia) == 1) {
|
2024-10-14 13:11:19 +00:00
|
|
|
if (!je_platna_operacia(operacia)) {
|
|
|
|
printf("bad input\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2024-10-14 11:44:22 +00:00
|
|
|
switch (operacia) {
|
|
|
|
case '+':
|
|
|
|
if (scitat(&kalkulacka)) {
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
2024-10-14 12:47:08 +00:00
|
|
|
} else {
|
|
|
|
printf("no input\n");
|
2024-10-14 12:58:04 +00:00
|
|
|
return 0;
|
2024-10-14 11:44:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
if (odcitat(&kalkulacka)) {
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
2024-10-14 12:47:08 +00:00
|
|
|
} else {
|
|
|
|
printf("no input\n");
|
2024-10-14 12:58:04 +00:00
|
|
|
return 0;
|
2024-10-14 11:44:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
if (nasobit(&kalkulacka)) {
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
2024-10-14 12:47:08 +00:00
|
|
|
} else {
|
|
|
|
printf("no input\n");
|
2024-10-14 12:58:04 +00:00
|
|
|
return 0;
|
2024-10-14 11:44:22 +00:00
|
|
|
}
|
|
|
|
break;
|
2024-10-14 13:11:19 +00:00
|
|
|
case '/':
|
|
|
|
if (!delit(&kalkulacka)) {
|
|
|
|
printf("division by zero\n");
|
2024-10-14 12:58:04 +00:00
|
|
|
return 0;
|
2024-10-14 12:04:21 +00:00
|
|
|
}
|
2024-10-14 13:11:19 +00:00
|
|
|
zobraz_zasobnik(&kalkulacka);
|
2024-10-14 11:44:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2024-10-14 13:11:19 +00:00
|
|
|
printf("no input\n");
|
2024-10-14 12:58:04 +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
|
|
|
}
|