Skip to content

Commit

Permalink
Merge pull request #33 from medvecky/develop
Browse files Browse the repository at this point in the history
History
  • Loading branch information
medvecky authored Apr 28, 2024
2 parents 69c3c9b + edc41ab commit 07a7b1e
Show file tree
Hide file tree
Showing 16 changed files with 696 additions and 183 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# [v3.0.0] 2024-04-28

## Added

- possibility to use history of operations
- sswap command for stack, same as in Forth
- sdupe command for stack, same as in Forth

# [v2.1.0] 2024-04-15

## Added

- functions for the operations with the stack
- error handling


# [v2.0.0] 2024-04-08

## Added
Expand All @@ -19,10 +26,9 @@
- Optimized command handling functions
- Updated README.md


# [v1.0.0] 2024-04-02

## Added

- Basic four arithmetic operations: addition, subtraction, multiplication, and division.
- Power function.
- Power function.
37 changes: 35 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
multicalc: build build/multicalc.o build/system_helper.o build/math_helper.o build/misc_helper.o build/adt_stack.o
cl65 build/multicalc.o build/system_helper.o build/math_helper.o build/misc_helper.o build/adt_stack.o -o bin/multicalc.prg
multicalc: build build/multicalc.o\
build/system_helper.o\
build/math_helper.o\
build/misc_helper.o\
build/adt_stack.o\
build/adt_queue.o\
build/history_helper.o\
build/stack_helper.o\
build/operations_helper.o

cl65 build/multicalc.o\
build/system_helper.o\
build/math_helper.o\
build/misc_helper.o\
build/adt_stack.o\
build/adt_queue.o\
build/history_helper.o\
build/stack_helper.o\
build/operations_helper.o -o bin/multicalc.prg

build/multicalc.o: src/multicalc.c
cl65 -c src/multicalc.c
Expand All @@ -17,10 +34,26 @@ build/misc_helper.o: src/modules/misc_helper.c
cl65 -c src/modules/misc_helper.c
@mv src/modules/misc_helper.o build/misc_helper.o

build/history_helper.o: src/modules/history_helper.c
cl65 -c src/modules/history_helper.c
@mv src/modules/history_helper.o build/history_helper.o

build/adt_stack.o: src/modules/adt_stack.c
cl65 -c src/modules/adt_stack.c
@mv src/modules/adt_stack.o build/adt_stack.o

build/adt_queue.o: src/modules/adt_queue.c
cl65 -c src/modules/adt_queue.c
@mv src/modules/adt_queue.o build/adt_queue.o

build/stack_helper.o: src/modules/stack_helper.c
cl65 -c src/modules/stack_helper.c
@mv src/modules/stack_helper.o build/stack_helper.o

build/operations_helper.o: src/modules/operations_helper.c
cl65 -c src/modules/operations_helper.c
@mv src/modules/operations_helper.o build/operations_helper.o

build:
@mkdir -p build
@mkdir -p bin
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The calculator supports the following operations:
| / | division |
| * | multiplication |
|| power |
| | |
| fabs | absolute value of a number |
| fatn | arctangent in radians |
| fcos | cosine in radians |
Expand All @@ -26,9 +27,19 @@ The calculator supports the following operations:
| fsqr | square root |
| ftan | tangent in radians |
| fsgn | gives autonomous of the algebraic sign the number (-1; 0; 1) |
| | |
| ss | print the content of the stack |
| sc | clear the stack |
| spop | pop the first value of the stack |
| sdupe | duplicate the top element of the stack |
| sswap | swap the top two elements of the stack |
| | |
| hs | show the content of the history |
| hel | execute the last history entry |
| hc | clear the history |
| hdl | delete the last entry from the history |
| hen | execute the nth record from the history |
| hee | edit an entry in the history and execute after editing |

Additionally, it accommodates floating-point numbers up to nine digits in both decimal (e.g., 3.14) and scientific (e.g., 8.9e-5) notations.

Expand Down Expand Up @@ -91,3 +102,14 @@ RPN expressions should be entered with a single space separating tokens, includi
#√( 2 * 300.51 / 9.8 )
300.51 2 * 9.8 / fsqr
```

### History operations

The calculator stores the last five operations entered.

- To view the history, use the ``` hs ``` command.
- To execute the most recent command from the history, use the ``` hel ``` command.
- To execute a specific command from the history, use the ``` hen ``` command. After entering ``` hen ```, the calculator displays the history and waits for you to enter the number of the entry you wish to execute.
- To modify and execute commands from the history, use the ``` hee ``` command.

After entering the ``` hee ``` command, the actions from the history will be displayed on the screen. Navigate through the list using the cursor keys. Press the **RETURN** key to send the selected record for execution. To ensure correct display, use the ``` hee ``` command before each call to edit the history.
184 changes: 184 additions & 0 deletions src/modules/adt_queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "adt_queue.h"

extern int isNeedToClear;

Queue * Queue_create( size_t capacity )
{
Queue * q = ( Queue * )malloc( sizeof( Queue ) );
q->head = q->tail = NULL;
q->size = 0;
q->capacity = capacity;
return q;
}

void Queue_print( Queue * q )
{
size_t index = 1;
Node * current = q->head;

while ( current != NULL )
{
printf( "%d. %s\n", index++, current->data );
current = current->next;
}

puts( "" );
}

void Queue_print_raw( Queue * q )
{
size_t index = 1;
Node * current = q->head;

while ( current != NULL )
{
puts( current->data );
current = current->next;
}

puts( "" );
}

void Queue_enqueue( Queue * q, const char * value )
{
Node * newNode = ( Node * )malloc( sizeof( Node ) );

if ( q->size == q->capacity )
{
// Queue is at capacity, dequeue the oldest element
Node * temp = q->head;
q->head = q->head->next;

if ( q->head == NULL )
{
q->tail = NULL;
}

free( temp->data ); // Free the memory allocated for the string
free( temp );

q->size--;
}

// Add the new element
newNode->data = malloc( strlen( value ) + 1 );
strcpy( newNode->data, value );
newNode->next = NULL;

if ( q->tail == NULL )
{
q->head = q->tail = newNode;
}
else
{
q->tail->next = newNode;
q->tail = newNode;
}

q->size++;
}

char * Queue_getLastElement( Queue * q )
{
char * copy = NULL;

isNeedToClear = 1;

if ( q->tail == NULL )
{
puts( "QUEUE: Empty" );
return NULL;
}

// Create a copy of the string
copy = malloc( strlen( q->tail->data ) + 1 );
strcpy( copy, q->tail->data );

return copy;
}

void Queue_clear( Queue * q )
{
Node * current = q->head;
Node * next;

while ( current != NULL )
{
next = current->next;
free( current->data ); // Free the memory allocated for the string
free(current);
current = next;
}

q->head = q->tail = NULL;
q->size = 0;
}

void Queue_deleteLastElement( Queue * q )
{
Node * current = q->head;

if ( q->tail == NULL )
{
puts( "QUEUE: Empty" );
return;
}

if ( q->head == q->tail )
{
free( q->tail->data );
free( q->tail );
q->head = q->tail = NULL;
q->size = 0;
return;
}

while ( current->next != q->tail )
{
current = current->next;
}

free( q->tail->data );
free( q->tail );

q->tail = current;
q->tail->next = NULL;
q->size--;
}

size_t Queue_getSize( Queue * q )
{
return q->size;
}

char * Queue_getNthElement( Queue * q, size_t n )
{
size_t index = 0;
Node * current = q->head;
char * copy = NULL;

isNeedToClear = 1;

n--;

if ( n >= q->size )
{
puts( "QUEUE: Index out of range" );
return NULL;
}

while ( current != NULL && index < n )
{
current = current->next;
index++;
}

copy = malloc( strlen( current->data ) + 1 );
strcpy( copy, current->data );

return copy;
}
32 changes: 32 additions & 0 deletions src/modules/adt_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef ADT_QUEUE_H
#define ADT_QUEUE_H

#include <stddef.h>

#define MAX_QUEUE_SIZE 5

typedef struct Node
{
char * data; // Data field is now a pointer to a character (string)
struct Node * next;
} Node;

typedef struct Queue
{
Node * head;
Node * tail;
size_t size; // Track the size of the queue
size_t capacity; // Maximum capacity of the queue
} Queue;

Queue * Queue_create( size_t capacity );
void Queue_print( Queue * q );
void Queue_enqueue( Queue * q, const char * value );
char * Queue_getLastElement( Queue * q );
void Queue_clear( Queue * q );
void Queue_deleteLastElement( Queue * q );
size_t Queue_getSize( Queue * q );
char * Queue_getNthElement( Queue * q, size_t n );
void Queue_print_raw( Queue * q );

#endif
37 changes: 37 additions & 0 deletions src/modules/adt_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,40 @@ void Stack_free( StackNodePtr * topPtr )
}
}

void Stack_dupe( StackNodePtr * topPtr )
{
FP topValue;

if ( Stack_isEmpty( *topPtr ) )
{
puts( "[STACK]: Empty" );
return;
}

Stack_getTop( *topPtr, &topValue );
Stack_push( topPtr, topValue );
}

void Stack_swap( StackNodePtr * topPtr )
{
FP topValue;
FP secondValue;

if ( Stack_isEmpty( *topPtr ) )
{
puts( "[STACK]: Empty" );
return;
}

Stack_pop( topPtr, &topValue );

if ( Stack_isEmpty( *topPtr ) )
{
Stack_push( topPtr, topValue );
return;
}

Stack_pop( topPtr, &secondValue );
Stack_push( topPtr, topValue );
Stack_push( topPtr, secondValue );
}
2 changes: 2 additions & 0 deletions src/modules/adt_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ int Stack_pop( StackNodePtr * topPtr, FP * popValue );
bool Stack_isEmpty( StackNodePtr topPtr );
void Stack_getTop( StackNodePtr topPtr, FP * topValue );
void Stack_free( StackNodePtr * topPtr );
void Stack_dupe( StackNodePtr * topPtr );
void Stack_swap( StackNodePtr * topPtr );

#endif
Loading

0 comments on commit 07a7b1e

Please sign in to comment.