usaa21/cv3/program.c
2021-10-20 11:26:28 +02:00

88 lines
2.1 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define LINESIZE 100
struct kalkulacka{
float cislo; };
int main() {
struct kalkulacka number[LINESIZE];
memset(number, 0, sizeof(struct kalkulacka) * LINESIZE);
char line[LINESIZE];
size_t velkost;
memset(line, 0, LINESIZE);
int a = 0;
int i;
int input = 0;
float p;
float vys;
while (fgets(line, LINESIZE, stdin)) {
velkost = strlen(line);
if (velkost == 1 && line[velkost - 1] == '\n') {
break;
}
if (input >= 10){
break;
}
if (line[0] == '+' || line[0] == '-' || line[0] == '*' || line[0] == '/') {
input =+ 2;
if (a == 1){
printf("no enough operands");
vys = 1;
break;
}
if (line[0] == '+') {
number[a - 2].cislo = number[a - 2].cislo + number[a - 1].cislo;
a--;
}
if (line[0] == '-') {
number[a - 2].cislo = number[a - 2].cislo - number[a - 1].cislo;
a--;
}
if (line[0] == '*') {
number[a - 2].cislo = number[a - 2].cislo * number[a - 1].cislo;
a--;
}
if (line[0] == '/') {
if (number[a - 1].cislo == 0){
printf("division by zero");
vys = 1;
a = 0;
}
else {
number[a - 2].cislo = number[a - 2].cislo / number[a - 1].cislo;
a--;
}
}
}
else {
if(line[0] >= '0' && line[0] <= '9') {
p = atof(line);
number[a].cislo = p;
a++;
input++;
}
else {
printf("bad input\n");
vys = 1;
break;
}
}
for (i = 0; i < a; i++){
printf("%.2f ", number[i].cislo);
}
printf("\n");
}
if (vys != 1) {
printf("no input\n");
}
return 0;
}