Update cv3/program.c

This commit is contained in:
Yurii Yakovenko 2024-10-14 20:08:56 +00:00
parent a7a8432f46
commit 5461b48be4

View File

@ -1,7 +1,8 @@
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
/////////////////////////////////////////
#include <ctype.h>
#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;
}
}