From 8af65458e17843ea9ba1253a4de550a58a5c3869 Mon Sep 17 00:00:00 2001 From: Jozef Slaninka Date: Tue, 20 Nov 2018 00:01:20 +0100 Subject: [PATCH] 123456789 --- Makefile | 20 +++++++ calculator.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++ calculator.h | 24 ++++++++ main.c | 9 +++ 4 files changed, 210 insertions(+) create mode 100644 Makefile create mode 100644 calculator.c create mode 100644 calculator.h create mode 100644 main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9288634 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +CFLAGS= -std=c99 -g + +all: calculator + +%.o: %.c + gcc -c -o $@ $< $(CFLAGS) + +calculator.o: calculator.c + gcc -c calculator.c -o calculator.o +main.o: main.c + gcc -c main.c -o main.o +calculator: main.o calculator.o + gcc main.o calculator.o -o calculator +clean: + rm *.o calculator + +test: calculator.c tests/test.c + gcc calculator.c tests/test.c tests/unity.c -Itests -o test + ./test + diff --git a/calculator.c b/calculator.c new file mode 100644 index 0000000..9c0016a --- /dev/null +++ b/calculator.c @@ -0,0 +1,157 @@ +#include "calculator.h" +#include +#include +#include +#include +#include + +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); + +} diff --git a/calculator.h b/calculator.h new file mode 100644 index 0000000..1ff2042 --- /dev/null +++ b/calculator.h @@ -0,0 +1,24 @@ +#ifndef CALCULATOR_H +#define CALCULATOR_H + +#define STACK_SIZE 1024 + +struct stack { + float* array; + int size; +}; + +struct stack* create_stack(); + +void delete_stack(struct stack* stack); + +void push_stack_value(struct stack* stack,float value); + +float pop_stack_value(struct stack* stack); + +void print_stack(struct stack* stack); + +void polish_calculator(); + +#endif // CALCULATOR_H + diff --git a/main.c b/main.c new file mode 100644 index 0000000..4892d8e --- /dev/null +++ b/main.c @@ -0,0 +1,9 @@ +#include "calculator.h" +#include + +int main(){ + + polish_calculator(); + return 0; +} +