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 <stdio.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
///////////////////////////////////////// #include <ctype.h>
#define STACK_SIZE 500 #define STACK_SIZE 500
struct Stack struct Stack
@ -17,13 +18,13 @@ int count_stack(struct Stack* stack);
void push_stack(struct Stack* stack, float value) 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->values[stack->size] = value;
stack->size += 1; stack->size += 1;
} }
float pop_stack(struct Stack* stack) 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]; float value = stack->values[stack->size-1];
stack->size -= 1; stack->size -= 1;
return value; return value;
@ -55,17 +56,11 @@ int main()
do do
{ {
char* p=fgets(buf, 299, stdin); char* p=fgets(buf, 299, stdin);
//printf("{%s}",buf); if(p)
if(!p)
{
//printf("==");
break;
}
else
{ {
char c=buf[0]; char c=buf[0];
float op1, op2, rez; float op1, op2, rez;
if(c=='+'||c=='-'||c=='/'||c=='*') // -45 if(c=='+'||c=='-'||c=='/'||c=='*')
{ {
op2=pop_stack(&mystack); op2=pop_stack(&mystack);
op1=pop_stack(&mystack); op1=pop_stack(&mystack);
@ -83,17 +78,22 @@ int main()
rez=op1/op2; rez=op1/op2;
break; break;
case '*': rez=op1*op2; break; case '*': rez=op1*op2; break;
} //p }
} }
else else
{ {
if(isdigit(c))
rez=atof(buf); rez=atof(buf);
else
{
err=1;
printf("bad input\n");
}
} }
if(!err) if(!err)
{ {
push_stack(&mystack, rez); push_stack(&mystack, rez);
//printf(" %.2f\n", rez);
print_stack(&mystack); print_stack(&mystack);
} }
} }
@ -103,3 +103,7 @@ int main()
printf("no input\n"); printf("no input\n");
return 0; return 0;
} }