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) {
|
|
|
|
return false;
|
|
|
|
}
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (sscanf(vstup, "%f", &hodnota) == 1) {
|
|
|
|
if (vloz(&kalkulacka, hodnota)) {
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
|
|
|
}
|
|
|
|
} else if (sscanf(vstup, "%c", &operacia) == 1) {
|
|
|
|
switch (operacia) {
|
|
|
|
case '+':
|
|
|
|
if (scitat(&kalkulacka)) {
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
2024-10-14 12:25:31 +00:00
|
|
|
} else {
|
|
|
|
printf("no input\n");
|
|
|
|
return 0;
|
2024-10-14 11:44:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
if (odcitat(&kalkulacka)) {
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
2024-10-14 12:25:31 +00:00
|
|
|
} else {
|
|
|
|
printf("no input\n");
|
|
|
|
return 0;
|
2024-10-14 11:44:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
if (nasobit(&kalkulacka)) {
|
|
|
|
zobraz_zasobnik(&kalkulacka);
|
2024-10-14 12:25:31 +00:00
|
|
|
} else {
|
|
|
|
printf("no input\n");
|
|
|
|
return 0;
|
2024-10-14 11:44:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '/':
|
2024-10-14 11:54:21 +00:00
|
|
|
if (!delit(&kalkulacka)) {
|
2024-10-14 12:38:06 +00:00
|
|
|
if (delit==false){
|
|
|
|
printf("divison by zero\n");
|
|
|
|
return 0;
|
2024-10-14 12:36:08 +00:00
|
|
|
}
|
2024-10-14 12:36:29 +00:00
|
|
|
else{
|
2024-10-14 12:36:08 +00:00
|
|
|
printf("no input\n");
|
2024-10-14 12:25:31 +00:00
|
|
|
return 0;
|
2024-10-14 12:36:29 +00:00
|
|
|
}
|
2024-10-14 12:04:21 +00:00
|
|
|
}
|
2024-10-14 12:25:31 +00:00
|
|
|
zobraz_zasobnik(&kalkulacka);
|
2024-10-14 11:44:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2024-10-14 12:25:31 +00:00
|
|
|
printf("no input\n");
|
2024-10-14 11:44:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2024-10-14 12:01:43 +00:00
|
|
|
|
2024-10-14 11:44:22 +00:00
|
|
|
return 0;
|
2024-10-14 12:25:31 +00:00
|
|
|
}
|