usaa21/cv3/program.c

88 lines
2.2 KiB
C
Raw Normal View History

2021-10-19 17:45:32 +00:00
#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;
2021-10-20 08:53:38 +00:00
float vys;
2021-10-19 17:45:32 +00:00
while (fgets(line, LINESIZE, stdin)) {
velkost = strlen(line);
if (velkost == 1 && line[velkost - 1] == '\n') {
break;
}
2021-10-20 08:41:12 +00:00
if (input >= 10){
2021-10-20 09:29:36 +00:00
printf("full stack\n");
vys = 1;
2021-10-19 17:45:32 +00:00
break;
}
if (line[0] == '+' || line[0] == '-' || line[0] == '*' || line[0] == '/') {
input =+ 2;
2021-10-20 09:26:28 +00:00
if (a == 1){
2021-10-20 09:27:48 +00:00
printf("not enough operands\n");
2021-10-20 09:26:28 +00:00
vys = 1;
break;
}
2021-10-19 17:45:32 +00:00
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] == '/') {
2021-10-20 08:51:31 +00:00
if (number[a - 1].cislo == 0){
printf("division by zero");
2021-10-20 08:53:38 +00:00
vys = 1;
a = 0;
2021-10-20 08:51:31 +00:00
}
else {
number[a - 2].cislo = number[a - 2].cislo / number[a - 1].cislo;
a--;
}
}
2021-10-19 17:45:32 +00:00
}
else {
2021-10-20 09:22:24 +00:00
if(line[0] >= '0' && line[0] <= '9') {
2021-10-20 09:18:38 +00:00
p = atof(line);
number[a].cislo = p;
a++;
input++;
}
2021-10-20 09:22:24 +00:00
else {
printf("bad input\n");
vys = 1;
break;
}
2021-10-19 17:45:32 +00:00
}
for (i = 0; i < a; i++){
2021-10-19 17:48:32 +00:00
printf("%.2f ", number[i].cislo);
2021-10-19 17:45:32 +00:00
}
2021-10-20 08:56:09 +00:00
2021-10-20 08:53:38 +00:00
printf("\n");
2021-10-20 08:56:09 +00:00
}
if (vys != 1) {
printf("no input\n");
2021-10-19 17:45:32 +00:00
}
return 0;
}