diff --git a/cv3/program.c b/cv3/program.c index 4cee380..14b341c 100644 --- a/cv3/program.c +++ b/cv3/program.c @@ -1,7 +1,8 @@ #include #include #include -///////////////////////////////////////// +#include + #define STACK_SIZE 500 struct Stack @@ -17,13 +18,13 @@ int count_stack(struct Stack* stack); void push_stack(struct Stack* stack, float value) { - assert(stack->size < STACK_SIZE); // Program spadne, ak zapisuje mimo + assert(stack->size < STACK_SIZE); stack->values[stack->size] = value; stack->size += 1; } float pop_stack(struct Stack* stack) { - assert(stack->size > 0); // Program spadne, ak číta mimo + assert(stack->size > 0); float value = stack->values[stack->size-1]; stack->size -= 1; return value; @@ -55,17 +56,11 @@ int main() do { char* p=fgets(buf, 299, stdin); - //printf("{%s}",buf); - if(!p) - { - //printf("=="); - break; - } - else + if(p) { char c=buf[0]; float op1, op2, rez; - if(c=='+'||c=='-'||c=='/'||c=='*') // -45 + if(c=='+'||c=='-'||c=='/'||c=='*') { op2=pop_stack(&mystack); op1=pop_stack(&mystack); @@ -83,17 +78,22 @@ int main() rez=op1/op2; break; case '*': rez=op1*op2; break; - } //p + } } else { - rez=atof(buf); + if(isdigit(c)) + rez=atof(buf); + else + { + err=1; + printf("bad input\n"); + } } if(!err) { push_stack(&mystack, rez); - //printf(" %.2f\n", rez); print_stack(&mystack); } } @@ -102,4 +102,8 @@ int main() if(!err) printf("no input\n"); return 0; -} \ No newline at end of file +} + + + +