93 lines
1.8 KiB
C
93 lines
1.8 KiB
C
#include <stdio.h>
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
////@@@@
|
|
#define STACK_SIZE 500
|
|
|
|
struct Stack
|
|
{
|
|
float values[STACK_SIZE];
|
|
int size;
|
|
};
|
|
|
|
void push_stack(struct Stack* stack, float value);
|
|
float pop_stack(struct Stack* stack);
|
|
int count_stack(struct Stack* stack);
|
|
|
|
|
|
void push_stack(struct Stack* stack, float value)
|
|
{
|
|
assert(stack->size < STACK_SIZE); // Program spadne, ak zapisuje mimo
|
|
stack->values[stack->size] = value;
|
|
stack->size += 1;
|
|
}
|
|
float pop_stack(struct Stack* stack)
|
|
{
|
|
assert(stack->size > 0); // Program spadne, ak číta mimo
|
|
float value = stack->values[stack->size-1];
|
|
stack->size -= 1;
|
|
return value;
|
|
}
|
|
int count_stack(struct Stack* stack)
|
|
{
|
|
|
|
}
|
|
|
|
void print_stack(struct Stack* stack)
|
|
{
|
|
for (int i=0; i<stack->size; i++)
|
|
{
|
|
printf("%.2f ", stack->values[i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
struct Stack mystack;
|
|
mystack.size=0;
|
|
//memset(&mystack,0,sizeof(struct Stack));
|
|
|
|
|
|
char buf[505];
|
|
|
|
do
|
|
{
|
|
char* p=fgets(buf, 299, stdin);
|
|
//printf("{%s}",buf);
|
|
if(!p)
|
|
{
|
|
//printf("==");
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
char c=buf[0];
|
|
float op1, op2, rez;
|
|
if(c=='+'||c=='-'||c=='/'||c=='*') // -45
|
|
{
|
|
op2=pop_stack(&mystack);
|
|
op1=pop_stack(&mystack);
|
|
switch(c)
|
|
{
|
|
case '+': rez=op1+op2; break;
|
|
case '-': rez=op1-op2; break;
|
|
case '/': rez=op1/op2; break;
|
|
case '*': rez=op1*op2; break;
|
|
} //p
|
|
}
|
|
else
|
|
{
|
|
rez=atof(buf);
|
|
}
|
|
push_stack(&mystack, rez);
|
|
//printf(" %.2f\n", rez);
|
|
print_stack(&mystack);
|
|
}
|
|
|
|
}while(1);
|
|
|
|
return 0;
|
|
}
|