This commit is contained in:
Matej Hajduk 2025-10-08 10:30:20 +02:00
parent 697f39dbd6
commit 82b7a2e913

80
du3/program.c Normal file
View File

@ -0,0 +1,80 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 10
struct stack{
float values[SIZE];
int size;
};
void print(struct stack *s)
{
for(int i = 0; i < s ->values[i]){
printf("%.2f ", s->values[i]);
}
printf("\n");
}
void push(struct stack *s, float value)
{
assert(s->size < SIZE);
s->values[s->size] = value;
s->size += 1;
}
float pop(struct stack *s)
{
assert(s->size > 0);
s->size -= 1;
return s->values[s->size];
}
int main(){
struct stack mystack;
mamset(&mystack,0,sizeof(struct stack));
char x[100];
while(fgets(x, 100, stdin)){
x[strcspn(x, "\n")] = 0;
if (strlen() == 0) continue;
char *endptr;
float num = strtof(x, &endptr);
if (*endptr == "\0") { // je cislo
if(mystack.size >= SIZE) {
fprintf(stderr," plne \n");
return 1;
}
push(&mystack,num);
}
else if(stlen(x) == 1 && strchr("+-/", x[0])) {
if (mystack.size < 2){
fprint(stderr, "malo cisel \n");
return 1;
}
float a = pop(&mystack);
float b = pop(&mystack);
float result = 0;
switch(x[0]){
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b;break;
case '/':
if(b == 0){
fprintf(strderr, "nule pr ideleni \n");
return 1;
}
resut = a / b;
break;
}
push(&mystak,result);
}
else{
fprintf(stderr, " zly vstup \n");
return 1;
}
print (&mystack);
}
return 0;
}