123456789

This commit is contained in:
Jozef Slaninka 2018-11-20 00:01:20 +01:00
commit 8af65458e1
4 changed files with 210 additions and 0 deletions

20
Makefile Normal file
View File

@ -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

157
calculator.c Normal file
View File

@ -0,0 +1,157 @@
#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);
}

24
calculator.h Normal file
View File

@ -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

9
main.c Normal file
View File

@ -0,0 +1,9 @@
#include "calculator.h"
#include <stdio.h>
int main(){
polish_calculator();
return 0;
}