158 lines
2.6 KiB
C
158 lines
2.6 KiB
C
#include "calculator.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
|
|
struct stack* create_stack() {
|
|
|
|
struct stack* s = calloc(1,sizeof(struct stack));
|
|
|
|
s-> array = (float*) calloc(STACK_SIZE,sizeof(float));
|
|
s-> size = 0;
|
|
|
|
return s;
|
|
}
|
|
|
|
void delete_stack(struct stack* stack) {
|
|
|
|
free(stack->array);
|
|
free(stack);
|
|
}
|
|
/**
|
|
* Ulozenie hodnoty do zasobnika.
|
|
* Funkcia upravi velkost zasobnika a vlozi novu hodnotu.
|
|
*
|
|
* Zabezpecte pomocou funkcie assert aby program spadok ak v zasobniku nie je dost miesta.
|
|
*
|
|
* @arg zasobnik do ktoreho sa ma hodnota ulozit.
|
|
* @arg hodnota, kotra sa ma ulozit.
|
|
*/
|
|
void push_stack_value(struct stack* stack,float value) {
|
|
|
|
if (stack->size >= STACK_SIZE)
|
|
{ //Dynamic allocation
|
|
|
|
float* new_array = (float*) calloc(stack->size+1,sizeof(float));
|
|
|
|
memcpy(new_array,stack->array,stack->size*sizeof(float));
|
|
|
|
new_array[stack->size++] = value;
|
|
|
|
stack -> array = new_array;
|
|
|
|
}
|
|
|
|
else
|
|
stack-> array[stack->size++] = value;
|
|
}
|
|
|
|
/**
|
|
* Vybratie hodnty zo zasobnika. Funkcia zmensi velkost zasobnika.
|
|
* Zabezpecte, aby program spadol ka v asobniku nie je doststok hodnot. (velkost nesmie byt mensia ko nula)
|
|
*
|
|
* @arg zasobnik
|
|
*
|
|
* @retrun hodnota na vrchu zasobnika.
|
|
*/
|
|
float pop_stack_value(struct stack* stack) {
|
|
|
|
assert(stack->size>0);
|
|
|
|
float val = stack->array[stack->size-1];
|
|
stack-> array[stack->size--]=0;
|
|
|
|
return val;
|
|
}
|
|
|
|
|
|
/**
|
|
* Vypis obsahu zasobnika.
|
|
*/
|
|
void print_stack(struct stack* stack) {
|
|
int i;
|
|
for ( i = 0; i < stack-> size; i++)
|
|
|
|
printf("%.2f ",stack->array[i]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
static bool is_float(char* str) {
|
|
|
|
return
|
|
(str[0]=='+'||str[0]=='-'||str[0]=='*'||str[0]=='/')
|
|
? false : true;
|
|
}
|
|
|
|
/**
|
|
* Cela polska kalkulacka.
|
|
*/
|
|
void polish_calculator() {
|
|
|
|
char* input= calloc(100,sizeof(char));
|
|
struct stack* stack = create_stack();
|
|
|
|
|
|
while(1)
|
|
{
|
|
|
|
scanf("%s",input);
|
|
|
|
if (is_float(input))
|
|
{
|
|
push_stack_value(stack,atof(input));
|
|
print_stack(stack);
|
|
}
|
|
|
|
else
|
|
{
|
|
|
|
if (stack->size<2)
|
|
{
|
|
printf("Nedostatok hodnot v stacku\n");
|
|
break;
|
|
}
|
|
|
|
|
|
switch(input[0])
|
|
{
|
|
|
|
case '+':
|
|
push_stack_value(stack,pop_stack_value(stack)
|
|
+ pop_stack_value(stack));
|
|
break;
|
|
|
|
case '-':
|
|
push_stack_value(stack,pop_stack_value(stack)
|
|
- pop_stack_value(stack));
|
|
break;
|
|
|
|
case '*':
|
|
push_stack_value(stack,pop_stack_value(stack)
|
|
* pop_stack_value(stack));
|
|
break;
|
|
|
|
case '/':
|
|
push_stack_value(stack,pop_stack_value(stack)
|
|
/ pop_stack_value(stack));
|
|
break;
|
|
|
|
default: printf("Nespravny operand\n");
|
|
|
|
}
|
|
|
|
print_stack(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
free(input);
|
|
delete_stack(stack);
|
|
|
|
}
|