diff --git a/CHANGELOG.md b/CHANGELOG.md index 19527d6..061c249 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# [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 @@ -5,7 +13,6 @@ - functions for the operations with the stack - error handling - # [v2.0.0] 2024-04-08 ## Added @@ -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. \ No newline at end of file +- Power function. diff --git a/Makefile b/Makefile index 4267532..4b35c6b 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 diff --git a/README.md b/README.md index 0a308e4..bc4448a 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -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. @@ -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. diff --git a/src/modules/adt_queue.c b/src/modules/adt_queue.c new file mode 100644 index 0000000..b539a6c --- /dev/null +++ b/src/modules/adt_queue.c @@ -0,0 +1,184 @@ +#include +#include +#include + +#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; +} \ No newline at end of file diff --git a/src/modules/adt_queue.h b/src/modules/adt_queue.h new file mode 100644 index 0000000..7b7d340 --- /dev/null +++ b/src/modules/adt_queue.h @@ -0,0 +1,32 @@ +#ifndef ADT_QUEUE_H +#define ADT_QUEUE_H + +#include + +#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 \ No newline at end of file diff --git a/src/modules/adt_stack.c b/src/modules/adt_stack.c index ced1c6f..bb8d101 100644 --- a/src/modules/adt_stack.c +++ b/src/modules/adt_stack.c @@ -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 ); +} \ No newline at end of file diff --git a/src/modules/adt_stack.h b/src/modules/adt_stack.h index e4b22ab..41b21c1 100644 --- a/src/modules/adt_stack.h +++ b/src/modules/adt_stack.h @@ -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 \ No newline at end of file diff --git a/src/modules/history_helper.c b/src/modules/history_helper.c new file mode 100644 index 0000000..0a465a0 --- /dev/null +++ b/src/modules/history_helper.c @@ -0,0 +1,66 @@ +#include +#include + +#include "adt_queue.h" +#include "misc_helper.h" + +extern Queue * queuePtr; +extern size_t yPosition; +extern int isNeedToCorrectPosition; + +void printHistory( void ) +{ + Queue_print( queuePtr ); +} + +void historyGetLastElement( void ) +{ + char * lastElement = Queue_getLastElement( queuePtr ); + + if ( lastElement != NULL ) + { + handleArgumentString( lastElement ); + } +} + +void clearHistory( void ) +{ + Queue_clear( queuePtr ); +} + +void deleteLastHiostryElement( void ) +{ + Queue_deleteLastElement( queuePtr ); +} + +void historyGetNthElement( void ) +{ + size_t n = 0; + char * nthElement = NULL; + + size_t queueSize = Queue_getSize( queuePtr ); + + Queue_print( queuePtr ); + + n = cgetc() - '0'; + + while ( n <= 0 || n > queueSize ) + { + puts( "Invalid entry number" ); + n = cgetc() - '0'; + } + + nthElement = Queue_getNthElement( queuePtr, n ); + + if ( nthElement != NULL ) + { + handleArgumentString( nthElement ); + } +} + +void historyEditAndExecute( void ) +{ + Queue_print_raw( queuePtr ); + yPosition = wherey(); + isNeedToCorrectPosition = 1; +} \ No newline at end of file diff --git a/src/modules/history_helper.h b/src/modules/history_helper.h new file mode 100644 index 0000000..588271a --- /dev/null +++ b/src/modules/history_helper.h @@ -0,0 +1,12 @@ +#ifndef HISTORY_HELPER_H +#define HISTORY_HELPER_H + +void printHistory( void ); +int handleHistoryFunction( char * token ); +void historyGetLastElement( void ); +void clearHistory( void ); +void deleteLastHiostryElement( void ); +void historyGetNthElement( void ); +void historyEditAndExecute( void ); + +#endif \ No newline at end of file diff --git a/src/modules/misc_helper.c b/src/modules/misc_helper.c index 64e220f..3b632c9 100644 --- a/src/modules/misc_helper.c +++ b/src/modules/misc_helper.c @@ -4,6 +4,10 @@ #include "system_helper.h" #include "adt_stack.h" #include "math_helper.h" +#include "history_helper.h" +#include "adt_queue.h" +#include "stack_helper.h" +#include "operations_helper.h" extern char arg1[ MAX_ARGUMENT_LENGTH ]; extern uint8_t argLength; @@ -11,45 +15,11 @@ extern StackNodePtr stackPtr; extern FP arg1Fp; extern FP arg2Fp; extern FP resultFp; - +extern Queue * queuePtr; bool isShowingResult = false; - -Operation operations[] = -{ - { "fabs", FP_abs }, - { "fatn", FP_atn }, - { "fcos", FP_cos }, - { "fmul10", FP_mul10 }, - { "fdiv10", FP_div10 }, - { "fexp", FP_exp }, - { "fint", FP_int }, - { "flog", FP_log }, - { "fneg", FP_neg }, - { "fsin", FP_sin }, - { "fsqr", FP_sqr }, - { "ftan", FP_tan }, - { "fsgn", FP_sgn }, - { NULL, NULL } // End marker -}; - -Operation stackOperations[] = -{ - { "ss", printStack }, - { "sc", clearStack }, - { "spop", popfromStack }, - { NULL, NULL } // End marker -}; - - -Operator operators[] = -{ - { '+', FP_add }, - { '-', FP_subst }, - { '*', FP_mult }, - { '/', FP_div }, - { ( char ) 0x5e, FP_pwr }, - { NULL, NULL } // End marker -}; +int isNeedToClear = 0; +size_t yPosition = 0; +int isNeedToCorrectPosition = 0; void header( void ) { @@ -64,7 +34,23 @@ void header( void ) void handleArgumentString( char * argumentString ) { char * token; - token = strtok( argumentString, " " ); + char key = argumentString[ 0 ]; + + if ( strcmp( argumentString, "4 8 15 16 23 42" ) == 0 ) + { + puts( "\nYou saved the world again!\n" ); + puts( "I'll see you in another life, brother." ); + puts( "Keep heading towards Swan Station when\nyou're stuck in an everlasting summer\n" ); + + return; + } + + if ( key != 'h' && key != '\0' ) + { + Queue_enqueue( queuePtr, argumentString ); + } + + token = strtok( argumentString, " " ); while ( token != NULL ) { @@ -75,6 +61,12 @@ void handleArgumentString( char * argumentString ) showResult(); printf( "" ); + + if ( isNeedToClear ) + { + free( argumentString ); + isNeedToClear = 0; + } } int handleToken( char * token ) @@ -159,6 +151,15 @@ int handleOperator( char * token ) isShowingResult = false; break; + + case 'h': + if ( handleHistoryFunction( token ) == EXIT_FAILURE ) + { + return EXIT_FAILURE; + } + + isShowingResult = false; + break; default: if ( handleFpOperator( token ) == EXIT_FAILURE ) @@ -174,100 +175,17 @@ int handleOperator( char * token ) return EXIT_SUCCESS; } -int handleFpOperator( char * token ) -{ - Operator * op = operators; - char operators = token[ 0 ]; - - for ( op; op->name != NULL; op++ ) - { - if ( ( op->name == operators ) ) - { - if ( handleTwoOperandOperation( op->func ) == EXIT_FAILURE ) - { - break; - } - Stack_push( &stackPtr, resultFp ); - return EXIT_SUCCESS; - } - } - - puts( "FP operator failure" ); - - return EXIT_FAILURE; -} - -int handleFunction( char * token ) -{ - Operation * op = operations; - - for ( op; op->name != NULL; op++ ) - { - if ( strcmp( token, op->name ) == 0 ) - { - handleOneOperandOperation( op->func ); - Stack_push( &stackPtr, resultFp ); - return EXIT_SUCCESS; - } - } - - puts( "Invalid function" ); - - return EXIT_FAILURE; -} - -int handleStackFunction( char * token ) -{ - Operation * op = stackOperations; - - for ( op; op->name != NULL; op++ ) - { - if ( strcmp( token, op->name ) == 0 ) - { - op->func(); - return EXIT_SUCCESS; - } - } - - puts( "Invalid stack function" ); - - return EXIT_FAILURE; -} - -int handleTwoOperandOperation( void ( *operation )( void ) ) -{ - if ( Stack_pop( &stackPtr, &arg1Fp ) == EXIT_FAILURE ) - { - return EXIT_FAILURE; - } - - if ( Stack_pop( &stackPtr, &arg2Fp ) == EXIT_FAILURE ) - { - return EXIT_FAILURE; - } - - enableBasicRom(); - operation(); - FP_facToResult(); - disableBasicRom(); - - return EXIT_SUCCESS; -} - -void handleOneOperandOperation( void ( *operation )( void ) ) -{ - Stack_pop(&stackPtr, &arg1Fp); - - enableBasicRom(); - operation(); - FP_facToResult(); - disableBasicRom(); -} - void showResult( void ) { if ( isShowingResult ) { + if ( isNeedToCorrectPosition ) + { + gotoy( yPosition ); + puts( "" ); + isNeedToCorrectPosition = 0; + } + if ( Stack_isEmpty( stackPtr ) ) { puts( "Stack is empty [Show result]" ); @@ -285,8 +203,8 @@ void showResult( void ) void getUserInput( char * argumentString ) { - uint8_t len = 0; - printf( "> " ); + size_t len = 0; + printf( ">: " ); if ( fgets( argumentString, MAX_INPUT_LENGTH, stdin ) == NULL ) { @@ -304,29 +222,9 @@ void getUserInput( char * argumentString ) } } -void printStack( void ) -{ - Stack_print( stackPtr ); -} - -void clearStack( void ) -{ - Stack_free( &stackPtr ); -} - -void popfromStack( void ) -{ - Stack_pop(&stackPtr, &resultFp); - - enableBasicRom(); - FP_printResult(); - disableBasicRom(); - puts( "" ); -} - int isValidNumber( const char *str ) { - int i = 0; + size_t i = 0; for ( i = 0; str[ i ]; i++ ) { diff --git a/src/modules/misc_helper.h b/src/modules/misc_helper.h index 195d567..7c98f78 100644 --- a/src/modules/misc_helper.h +++ b/src/modules/misc_helper.h @@ -7,36 +7,13 @@ #define MAX_INPUT_LENGTH 80 -typedef void ( *OperationFunc )( void ); - -typedef struct -{ - char * name; - OperationFunc func; -} Operation; - - -typedef struct -{ - char name; - OperationFunc func; -} Operator; - void header( void ); void handleArgumentString( char * argumentString ); int handleToken( char * token ); int handleNumber( char * token ); -int handleOperator( char * token ); -int handleTwoOperandOperation( void ( * operation )( void ) ); -void handleOneOperandOperation( void ( * operation )( void ) ); +int handleOperator( char * token ); void showResult( void ); void getUserInput( char * argumentString ); -int handleFunction( char * token ); -void printStack( void ); -void clearStack( void ); -int handleFpOperator( char * token ); -int handleStackFunction( char * token ); -void popfromStack( void ); -int isValidNumber( const char *str ); +int isValidNumber( const char * str ); #endif \ No newline at end of file diff --git a/src/modules/operations_helper.c b/src/modules/operations_helper.c new file mode 100644 index 0000000..2706446 --- /dev/null +++ b/src/modules/operations_helper.c @@ -0,0 +1,171 @@ +#include + +#include "operations_helper.h" +#include "math_helper.h" +#include "stack_helper.h" +#include "history_helper.h" +#include "misc_helper.h" +#include "adt_stack.h" +#include "system_helper.h" + +extern StackNodePtr stackPtr; +extern FP resultFp; +extern FP arg1Fp; +extern FP arg2Fp; + +Operator operators[] = +{ + { '+', FP_add }, + { '-', FP_subst }, + { '*', FP_mult }, + { '/', FP_div }, + { ( char ) 0x5e, FP_pwr }, + { NULL, NULL } // End marker +}; + +Operation operations[] = +{ + { "fabs", FP_abs }, + { "fatn", FP_atn }, + { "fcos", FP_cos }, + { "fmul10", FP_mul10 }, + { "fdiv10", FP_div10 }, + { "fexp", FP_exp }, + { "fint", FP_int }, + { "flog", FP_log }, + { "fneg", FP_neg }, + { "fsin", FP_sin }, + { "fsqr", FP_sqr }, + { "ftan", FP_tan }, + { "fsgn", FP_sgn }, + { NULL, NULL } // End marker +}; + +Operation stackOperations[] = +{ + { "ss", printStack }, + { "sc", clearStack }, + { "spop", popfromStack }, + { "sdupe", dupeStack }, + { "sswap", swapStack }, + { NULL, NULL } // End marker +}; + +Operation historyOperations[] = +{ + { "hs", printHistory }, + { "hel", historyGetLastElement }, + { "hc", clearHistory }, + { "hdl", deleteLastHiostryElement }, + { "hen", historyGetNthElement }, + { "hee", historyEditAndExecute }, + { NULL, NULL } // End marker +}; + +int handleFpOperator( char * token ) +{ + Operator * op = operators; + char operator = token[ 0 ]; + + for ( op; op->name != NULL; op++ ) + { + if ( ( op->name == operator ) ) + { + if ( handleTwoOperandOperation( op->func ) == EXIT_FAILURE ) + { + break; + } + Stack_push( &stackPtr, resultFp ); + return EXIT_SUCCESS; + } + } + + puts( "FP operator failure" ); + + return EXIT_FAILURE; +} + +int handleFunction( char * token ) +{ + Operation * op = operations; + + for ( op; op->name != NULL; op++ ) + { + if ( strcmp( token, op->name ) == 0 ) + { + handleOneOperandOperation( op->func ); + Stack_push( &stackPtr, resultFp ); + return EXIT_SUCCESS; + } + } + + puts( "Invalid function" ); + + return EXIT_FAILURE; +} + +int handleStackFunction( char * token ) +{ + Operation * op = stackOperations; + + for ( op; op->name != NULL; op++ ) + { + if ( strcmp( token, op->name ) == 0 ) + { + op->func(); + return EXIT_SUCCESS; + } + } + + puts( "Invalid stack function" ); + + return EXIT_FAILURE; +} + +int handleHistoryFunction( char * token ) +{ + Operation * op = historyOperations; + + for ( op; op->name != NULL; op++ ) + { + if ( strcmp( token, op->name ) == 0 ) + { + op->func(); + return EXIT_SUCCESS; + } + } + + puts( "Invalid history function" ); + + return EXIT_FAILURE; +} + +int handleTwoOperandOperation( void ( *operation )( void ) ) +{ + if ( Stack_pop( &stackPtr, &arg1Fp ) == EXIT_FAILURE ) + { + return EXIT_FAILURE; + } + + if ( Stack_pop( &stackPtr, &arg2Fp ) == EXIT_FAILURE ) + { + return EXIT_FAILURE; + } + + enableBasicRom(); + operation(); + FP_facToResult(); + disableBasicRom(); + + return EXIT_SUCCESS; +} + +void handleOneOperandOperation( void ( *operation )( void ) ) +{ + Stack_pop(&stackPtr, &arg1Fp); + + enableBasicRom(); + operation(); + FP_facToResult(); + disableBasicRom(); +} \ No newline at end of file diff --git a/src/modules/operations_helper.h b/src/modules/operations_helper.h new file mode 100644 index 0000000..177de0f --- /dev/null +++ b/src/modules/operations_helper.h @@ -0,0 +1,26 @@ +#ifndef OPERATIONS_HELPER_H +#define OPERATIONS_HELPER_H + +typedef void ( *OperationFunc )( void ); + +typedef struct +{ + char name; + OperationFunc func; +} Operator; + +typedef struct +{ + char * name; + OperationFunc func; +} Operation; + + +int handleFpOperator( char * token ); +int handleFunction( char * token ); +int handleStackFunction( char * token ); +int handleHistoryFunction( char * token ); +int handleTwoOperandOperation( void ( *operation )( void ) ); +void handleOneOperandOperation( void ( *operation )( void ) ); + +#endif \ No newline at end of file diff --git a/src/modules/stack_helper.c b/src/modules/stack_helper.c new file mode 100644 index 0000000..81372a0 --- /dev/null +++ b/src/modules/stack_helper.c @@ -0,0 +1,35 @@ +#include "adt_stack.h" +#include "system_helper.h" + +extern StackNodePtr stackPtr; +extern FP resultFp; + +void printStack( void ) +{ + Stack_print( stackPtr ); +} + +void clearStack( void ) +{ + Stack_free( &stackPtr ); +} + +void popfromStack( void ) +{ + Stack_pop(&stackPtr, &resultFp); + + enableBasicRom(); + FP_printResult(); + disableBasicRom(); + puts( "" ); +} + +void dupeStack( void ) +{ + Stack_dupe( &stackPtr ); +} + +void swapStack( void ) +{ + Stack_swap( &stackPtr ); +} diff --git a/src/modules/stack_helper.h b/src/modules/stack_helper.h new file mode 100644 index 0000000..2b63547 --- /dev/null +++ b/src/modules/stack_helper.h @@ -0,0 +1,10 @@ +#ifndef STACK_HELPER_H +#define STACK_HELPER_H + +void popfromStack( void ); +void dupeStack( void ); +void swapStack( void ); +void printStack( void ); +void clearStack( void ); + +#endif \ No newline at end of file diff --git a/src/multicalc.c b/src/multicalc.c index 499f456..38dc9c6 100644 --- a/src/multicalc.c +++ b/src/multicalc.c @@ -4,15 +4,17 @@ #include "modules/system_helper.h" #include "modules/misc_helper.h" #include "modules/adt_stack.h" +#include "modules/adt_queue.h" StackNodePtr stackPtr = NULL; - +Queue * queuePtr = NULL; int main ( void ) { char argumentString[ MAX_INPUT_LENGTH ]; char quitFlag = ' '; + queuePtr = Queue_create( MAX_QUEUE_SIZE ); setUpScreen();