From aebfbd16dfb1a604b3d450b1e546e98233cc91c7 Mon Sep 17 00:00:00 2001 From: medvecky Date: Tue, 9 Apr 2024 21:46:10 +0300 Subject: [PATCH 1/8] implement_stack_operations: - add Stack_print function - fix Stack_free function --- README.md | 3 +-- src/modules/adt_stack.c | 37 ++++++++++++++++++++++++++++++------- src/modules/adt_stack.h | 1 + src/modules/misc_helper.c | 1 + 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c7bd311..57ce35a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ The calculator supports the following operations: | fabs | absolute value of a number | | fatn | arctangent in radians | | fcos | cosine in radians | -| fmul10 | multiply by 10 | +| fmul10 | multiply by 10 | | fdiv10 | divide by 10 | | fexp | exponential function | | fint | get the integer part of a number | @@ -52,7 +52,6 @@ make multicalc The calculator binary, named multicalc.prg, is located in the /bin directory. - ## Operating Manual The calculator can optionally be run using the [VICE](https://vice-emu.sourceforge.io/) emulator. diff --git a/src/modules/adt_stack.c b/src/modules/adt_stack.c index b918762..fc7f965 100644 --- a/src/modules/adt_stack.c +++ b/src/modules/adt_stack.c @@ -2,6 +2,9 @@ #include "adt_stack.h" #include "math_helper.h" +#include "system_helper.h" + +extern FP resultFp; void Stack_push( StackNodePtr * topPtr, FP value ) { @@ -22,7 +25,7 @@ void Stack_push( StackNodePtr * topPtr, FP value ) } } -void Stack_print( StackNodePtr currentPtr ) +void Stack_printDebug( StackNodePtr currentPtr ) { if ( currentPtr == NULL ) { @@ -43,6 +46,26 @@ void Stack_print( StackNodePtr currentPtr ) } } +void Stack_print( StackNodePtr currentPtr ) +{ + if ( currentPtr == NULL ) + { + puts( "The stack is empty" ); + } + else + { + while ( currentPtr != NULL ) + { + memcpy( resultFp, currentPtr->data, sizeof( FP ) ); + enableBasicRom(); + FP_printResult(); + disableBasicRom(); + puts( "" ); + currentPtr = currentPtr->nextPtr; + } + } +} + void Stack_pop( StackNodePtr * topPtr, FP * popValue ) { StackNodePtr tempPtr; @@ -77,13 +100,13 @@ void Stack_getTop( StackNodePtr topPtr, FP * topValue) void Stack_free( StackNodePtr * topPtr ) { - StackNodePtr tempPtr = *topPtr; - StackNodePtr nextPtr; - while ( tempPtr != NULL ) + StackNodePtr tempPtr; + + while ( *topPtr != NULL) { - nextPtr = ( *tempPtr ).nextPtr; + tempPtr = *topPtr; + *topPtr = ( *topPtr )->nextPtr; free( tempPtr ); - tempPtr = nextPtr; - } + } } diff --git a/src/modules/adt_stack.h b/src/modules/adt_stack.h index 7cee467..baa7c79 100644 --- a/src/modules/adt_stack.h +++ b/src/modules/adt_stack.h @@ -17,6 +17,7 @@ typedef struct stackNode StackNode; typedef StackNode *StackNodePtr; void Stack_push( StackNodePtr * topPtr, FP value ); +void Stack_printDebug( StackNodePtr currentPtr ); void Stack_print( StackNodePtr currentPtr ); void Stack_pop( StackNodePtr * topPtr, FP * popValue ); bool Stack_isEmpty( StackNodePtr topPtr ); diff --git a/src/modules/misc_helper.c b/src/modules/misc_helper.c index 38c6061..8a7da17 100644 --- a/src/modules/misc_helper.c +++ b/src/modules/misc_helper.c @@ -85,6 +85,7 @@ void handleArgumentString( char * argumentString ) showResult(); } + Stack_print( stackPtr ); Stack_free( &stackPtr ); } From 20fc9e9eef1353e55bb8a3446f973bfcc866dd16 Mon Sep 17 00:00:00 2001 From: medvecky Date: Wed, 10 Apr 2024 21:57:36 +0300 Subject: [PATCH 2/8] implement_stack_operation_handling: * add handling for stack show * add handling for celar stack * improve showResult behaviour * improve showStack behaviour --- src/modules/adt_stack.c | 2 + src/modules/misc_helper.c | 112 +++++++++++++++++++++++++++----------- src/modules/misc_helper.h | 4 ++ 3 files changed, 87 insertions(+), 31 deletions(-) diff --git a/src/modules/adt_stack.c b/src/modules/adt_stack.c index fc7f965..a313c95 100644 --- a/src/modules/adt_stack.c +++ b/src/modules/adt_stack.c @@ -54,6 +54,7 @@ void Stack_print( StackNodePtr currentPtr ) } else { + puts( "--> stack top <--" ); while ( currentPtr != NULL ) { memcpy( resultFp, currentPtr->data, sizeof( FP ) ); @@ -63,6 +64,7 @@ void Stack_print( StackNodePtr currentPtr ) puts( "" ); currentPtr = currentPtr->nextPtr; } + puts( "--> stack bottom <--" ); } } diff --git a/src/modules/misc_helper.c b/src/modules/misc_helper.c index 8a7da17..5fcbea5 100644 --- a/src/modules/misc_helper.c +++ b/src/modules/misc_helper.c @@ -10,6 +10,8 @@ extern FP arg1Fp; extern FP arg2Fp; extern FP resultFp; +bool isShowingResult = false; + Operation operations[] = { { "fabs", FP_abs }, @@ -28,6 +30,14 @@ Operation operations[] = { NULL, NULL } // End marker }; +Operation stackOperations[] = +{ + { "ss", printStack }, + { "sc", clearStack }, + { NULL, NULL } // End marker +}; + + Operator operators[] = { { '+', FP_add }, @@ -74,19 +84,8 @@ void handleArgumentString( char * argumentString ) token = strtok( NULL, " " ); } + showResult(); printf( "" ); - - if ( Stack_isEmpty( stackPtr ) ) - { - puts( "Stack is empty" ); - } - else - { - showResult(); - } - - Stack_print( stackPtr ); - Stack_free( &stackPtr ); } void handleNumber( char * token ) @@ -112,7 +111,6 @@ void handleNumber( char * token ) void handleOperator( char * token ) { char operator = token[ 0 ]; - Operator * op = operators; if ( Stack_isEmpty( stackPtr ) ) { @@ -120,24 +118,42 @@ void handleOperator( char * token ) } else { - if ( operator == 'f' ) + switch ( operator ) { - handleFunction( token ); - Stack_push( &stackPtr, resultFp ); - return; + case 'f': + handleFunction( token ); + isShowingResult = true; + break; + + case 's': + handleStackFunction( token ); + isShowingResult = false; + break; + + default: + handleFpOperator( token ); + isShowingResult = true; + break; } - for ( op; op->name != NULL; op++ ) + } +} + +void handleFpOperator( char * token ) +{ + Operator * op = operators; + char operators = token[ 0 ]; + + for ( op; op->name != NULL; op++ ) + { + if ( ( op->name == operators ) ) { - if ( ( op->name == operator ) ) - { - handleTwoOperandOperation( op->func ); - Stack_push( &stackPtr, resultFp ); - return; - } + handleTwoOperandOperation( op->func ); + Stack_push( &stackPtr, resultFp ); + return; } + } - puts( "Invalid operator" ); - } + puts( "Invalid FP operator" ); } void handleFunction( char * token ) @@ -149,12 +165,29 @@ void handleFunction( char * token ) if ( strcmp( token, op->name ) == 0 ) { handleOneOperandOperation( op->func ); + Stack_push( &stackPtr, resultFp ); return; } } puts( "Invalid function" ); } +void handleStackFunction( char * token ) +{ + Operation * op = stackOperations; + + for ( op; op->name != NULL; op++ ) + { + if ( strcmp( token, op->name ) == 0 ) + { + op->func(); + return; + } + } + + puts( "Invalid stack function" ); +} + void handleTwoOperandOperation( void ( *operation )( void ) ) { Stack_pop(&stackPtr, &arg1Fp); @@ -178,11 +211,18 @@ void handleOneOperandOperation( void ( *operation )( void ) ) void showResult( void ) { - Stack_pop( &stackPtr, &resultFp ); - enableBasicRom(); - FP_printResult(); - disableBasicRom(); - puts( "" ); + if ( Stack_isEmpty( stackPtr ) ) + { + puts( "Stack is empty" ); + } + else if ( isShowingResult ) + { + Stack_getTop( stackPtr, &resultFp ); + enableBasicRom(); + FP_printResult(); + disableBasicRom(); + puts( "" ); + } } void getUserInput( char * argumentString ) @@ -205,3 +245,13 @@ void getUserInput( char * argumentString ) argumentString[ --len ] = '\0'; } } + +void printStack( void ) +{ + Stack_print( stackPtr ); +} + +void clearStack( void ) +{ + Stack_free( &stackPtr ); +} diff --git a/src/modules/misc_helper.h b/src/modules/misc_helper.h index 51fbec4..c1a68db 100644 --- a/src/modules/misc_helper.h +++ b/src/modules/misc_helper.h @@ -31,5 +31,9 @@ void handleOneOperandOperation( void ( *operation )( void ) ); void showResult( void ); void getUserInput( char * argumentString ); void handleFunction( char * token ); +void printStack( void ); +void clearStack( void ); +void handleFpOperator( char * token ); +void handleStackFunction( char * token ); #endif \ No newline at end of file From 2ff3807adb5a3e7a6578b275e21da864b7623165 Mon Sep 17 00:00:00 2001 From: medvecky Date: Thu, 11 Apr 2024 19:17:04 +0300 Subject: [PATCH 3/8] implement_stack_operation_handling: * add spop operation --- src/modules/misc_helper.c | 11 +++++++++++ src/modules/misc_helper.h | 1 + 2 files changed, 12 insertions(+) diff --git a/src/modules/misc_helper.c b/src/modules/misc_helper.c index 5fcbea5..170b0b0 100644 --- a/src/modules/misc_helper.c +++ b/src/modules/misc_helper.c @@ -34,6 +34,7 @@ Operation stackOperations[] = { { "ss", printStack }, { "sc", clearStack }, + { "spop", popfromStack }, { NULL, NULL } // End marker }; @@ -255,3 +256,13 @@ void clearStack( void ) { Stack_free( &stackPtr ); } + +void popfromStack( void ) +{ + Stack_pop(&stackPtr, &resultFp); + + enableBasicRom(); + FP_printResult(); + disableBasicRom(); + puts( "" ); +} diff --git a/src/modules/misc_helper.h b/src/modules/misc_helper.h index c1a68db..1cdb491 100644 --- a/src/modules/misc_helper.h +++ b/src/modules/misc_helper.h @@ -35,5 +35,6 @@ void printStack( void ); void clearStack( void ); void handleFpOperator( char * token ); void handleStackFunction( char * token ); +void popfromStack( void ); #endif \ No newline at end of file From 1c4847cf76f7c416bc7b0e820f51c53aa22ec2f7 Mon Sep 17 00:00:00 2001 From: medvecky Date: Thu, 11 Apr 2024 21:01:54 +0300 Subject: [PATCH 4/8] =?UTF-8?q?implement=5Fstack=5Foperation=5Fhandling?= =?UTF-8?q?=D0=96=20*=20add=20numbers=20validation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/math_helper.h | 2 +- src/modules/misc_helper.c | 66 ++++++++++++++++++++++++++++++--------- src/modules/misc_helper.h | 3 +- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/modules/math_helper.h b/src/modules/math_helper.h index c49fbf8..e09e2eb 100644 --- a/src/modules/math_helper.h +++ b/src/modules/math_helper.h @@ -3,7 +3,7 @@ #include -#define MAX_ARGUMENT_LENGTH 12 +#define MAX_ARGUMENT_LENGTH 20 typedef uint8_t FP[5]; diff --git a/src/modules/misc_helper.c b/src/modules/misc_helper.c index 170b0b0..176aa88 100644 --- a/src/modules/misc_helper.c +++ b/src/modules/misc_helper.c @@ -1,3 +1,5 @@ +#include + #include "misc_helper.h" #include "system_helper.h" #include "adt_stack.h" @@ -68,13 +70,21 @@ void handleArgumentString( char * argumentString ) { if ( isdigit( ( unsigned char )token[ 0 ] ) ) { - handleNumber( token ); + if ( handleNumber( token ) == EXIT_FAILURE ) + { + isShowingResult = false; + break; + } } else { if ( ( unsigned char )token[ 0 ] == '-' && isdigit( ( unsigned char )token[ 1 ] ) ) { - handleNumber( token ); + if ( handleNumber( token ) == EXIT_FAILURE ) + { + isShowingResult = false; + break; + } } else { @@ -89,12 +99,18 @@ void handleArgumentString( char * argumentString ) printf( "" ); } -void handleNumber( char * token ) +int handleNumber( char * token ) { if ( strlen( token ) >= sizeof( arg1 ) ) { puts( "Number is too long" ); - return; + return EXIT_FAILURE; + } + + if ( !isValidNumber( token ) ) + { + puts( "Invalid number format" ); + return EXIT_FAILURE; } strncpy( arg1, token, sizeof( arg1 ) - 1 ); @@ -107,6 +123,8 @@ void handleNumber( char * token ) disableBasicRom(); Stack_push( &stackPtr, arg1Fp ); + + return EXIT_SUCCESS; } void handleOperator( char * token ) @@ -115,7 +133,7 @@ void handleOperator( char * token ) if ( Stack_isEmpty( stackPtr ) ) { - puts( "Stack is empty" ); + puts( "Stack is empty [handle operator begin]" ); } else { @@ -212,17 +230,20 @@ void handleOneOperandOperation( void ( *operation )( void ) ) void showResult( void ) { - if ( Stack_isEmpty( stackPtr ) ) + if ( isShowingResult ) { - puts( "Stack is empty" ); - } - else if ( isShowingResult ) - { - Stack_getTop( stackPtr, &resultFp ); - enableBasicRom(); - FP_printResult(); - disableBasicRom(); - puts( "" ); + if ( Stack_isEmpty( stackPtr ) ) + { + puts( "Stack is empty [Show result]" ); + } + else + { + Stack_getTop( stackPtr, &resultFp ); + enableBasicRom(); + FP_printResult(); + disableBasicRom(); + puts( "" ); + } } } @@ -266,3 +287,18 @@ void popfromStack( void ) disableBasicRom(); puts( "" ); } + +int isValidNumber( const char *str ) +{ + int i = 0; + + for ( i = 0; str[ i ]; i++ ) + { + if ( !isdigit( str[ i ] ) && str[ i ] != '-' && + str[ i ] != 'e' && str[ i ] != '.' ) + { + return false; + } + } + return true; +} diff --git a/src/modules/misc_helper.h b/src/modules/misc_helper.h index 1cdb491..7697517 100644 --- a/src/modules/misc_helper.h +++ b/src/modules/misc_helper.h @@ -24,7 +24,7 @@ typedef struct void header( void ); void handleArgumentString( char * argumentString ); -void handleNumber( char * token ); +int handleNumber( char * token ); void handleOperator( char * token ); void handleTwoOperandOperation( void ( *operation )( void ) ); void handleOneOperandOperation( void ( *operation )( void ) ); @@ -36,5 +36,6 @@ void clearStack( void ); void handleFpOperator( char * token ); void handleStackFunction( char * token ); void popfromStack( void ); +int isValidNumber( const char *str ); #endif \ No newline at end of file From 6626f075105215416a71c058409907fabd6333f8 Mon Sep 17 00:00:00 2001 From: Aleksejs Medveckis Date: Fri, 12 Apr 2024 16:03:56 +0300 Subject: [PATCH 5/8] implement_stack_operation_handling: * rreafactor handleArgumentString function --- src/modules/misc_helper.c | 44 +++++++++++++++++++-------------------- src/modules/misc_helper.h | 9 ++++---- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/modules/misc_helper.c b/src/modules/misc_helper.c index 176aa88..a374160 100644 --- a/src/modules/misc_helper.c +++ b/src/modules/misc_helper.c @@ -68,29 +68,7 @@ void handleArgumentString( char * argumentString ) while ( token != NULL ) { - if ( isdigit( ( unsigned char )token[ 0 ] ) ) - { - if ( handleNumber( token ) == EXIT_FAILURE ) - { - isShowingResult = false; - break; - } - } - else - { - if ( ( unsigned char )token[ 0 ] == '-' && isdigit( ( unsigned char )token[ 1 ] ) ) - { - if ( handleNumber( token ) == EXIT_FAILURE ) - { - isShowingResult = false; - break; - } - } - else - { - handleOperator( token ); - } - } + if ( handleToken( token ) == EXIT_FAILURE ) break; token = strtok( NULL, " " ); } @@ -99,6 +77,26 @@ void handleArgumentString( char * argumentString ) printf( "" ); } +int handleToken( char * token ) +{ + if ( isdigit( ( unsigned char )token[ 0 ]) || + ( ( unsigned char )token[ 0 ] == '-' && + isdigit( ( unsigned char )token[ 1 ] ) ) ) + { + if ( handleNumber( token ) == EXIT_FAILURE ) + { + isShowingResult = false; + return EXIT_FAILURE; + } + } + else + { + handleOperator( token ); + } + + return EXIT_SUCCESS; +} + int handleNumber( char * token ) { if ( strlen( token ) >= sizeof( arg1 ) ) diff --git a/src/modules/misc_helper.h b/src/modules/misc_helper.h index 7697517..3bf3630 100644 --- a/src/modules/misc_helper.h +++ b/src/modules/misc_helper.h @@ -23,10 +23,11 @@ typedef struct } Operator; void header( void ); -void handleArgumentString( char * argumentString ); -int handleNumber( char * token ); -void handleOperator( char * token ); -void handleTwoOperandOperation( void ( *operation )( void ) ); +void handleArgumentString(char *argumentString); +int handleToken( char * token ); +int handleNumber(char *token); +void handleOperator(char *token); +void handleTwoOperandOperation(void (*operation)(void)); void handleOneOperandOperation( void ( *operation )( void ) ); void showResult( void ); void getUserInput( char * argumentString ); From 43f931e6a2d26843a6841863486585f9ca6bd69c Mon Sep 17 00:00:00 2001 From: medvecky Date: Fri, 12 Apr 2024 20:34:36 +0300 Subject: [PATCH 6/8] implement_stack_operation_handling: * add error handling --- src/modules/adt_stack.c | 18 +++++----- src/modules/adt_stack.h | 2 +- src/modules/misc_helper.c | 72 ++++++++++++++++++++++++++++++--------- src/modules/misc_helper.h | 16 ++++----- 4 files changed, 74 insertions(+), 34 deletions(-) diff --git a/src/modules/adt_stack.c b/src/modules/adt_stack.c index a313c95..ced1c6f 100644 --- a/src/modules/adt_stack.c +++ b/src/modules/adt_stack.c @@ -21,7 +21,7 @@ void Stack_push( StackNodePtr * topPtr, FP value ) else { FP_printRaw( value ); - puts( "not inserted. No memory available.\n" ); + puts( "[STACK]: not inserted. No memory available." ); } } @@ -50,11 +50,11 @@ void Stack_print( StackNodePtr currentPtr ) { if ( currentPtr == NULL ) { - puts( "The stack is empty" ); + puts( "[STACK]: Empty" ); } else { - puts( "--> stack top <--" ); + puts( "--> stack's top <--" ); while ( currentPtr != NULL ) { memcpy( resultFp, currentPtr->data, sizeof( FP ) ); @@ -64,24 +64,26 @@ void Stack_print( StackNodePtr currentPtr ) puts( "" ); currentPtr = currentPtr->nextPtr; } - puts( "--> stack bottom <--" ); + puts( "--> stack's bottom <--" ); } } -void Stack_pop( StackNodePtr * topPtr, FP * popValue ) +int Stack_pop( StackNodePtr * topPtr, FP * popValue ) { StackNodePtr tempPtr; if ( Stack_isEmpty( *topPtr ) ) { - puts( "Stack is empty. Cannot pop value." ); - return; + puts( "[STACK]: Empty" ); + return EXIT_FAILURE; } tempPtr = *topPtr; memcpy( *popValue, ( *topPtr )->data, sizeof( FP ) ); *topPtr = ( *topPtr )->nextPtr; free( tempPtr ); + + return EXIT_SUCCESS; } bool Stack_isEmpty( StackNodePtr topPtr ) @@ -93,7 +95,7 @@ void Stack_getTop( StackNodePtr topPtr, FP * topValue) { if ( Stack_isEmpty( topPtr ) ) { - puts( "Stack is empty. Cannot pop value." ); + puts( "[STACK]: Empty" ); return; } diff --git a/src/modules/adt_stack.h b/src/modules/adt_stack.h index baa7c79..e4b22ab 100644 --- a/src/modules/adt_stack.h +++ b/src/modules/adt_stack.h @@ -19,7 +19,7 @@ typedef StackNode *StackNodePtr; void Stack_push( StackNodePtr * topPtr, FP value ); void Stack_printDebug( StackNodePtr currentPtr ); void Stack_print( StackNodePtr currentPtr ); -void Stack_pop( StackNodePtr * topPtr, FP * popValue ); +int Stack_pop( StackNodePtr * topPtr, FP * popValue ); bool Stack_isEmpty( StackNodePtr topPtr ); void Stack_getTop( StackNodePtr topPtr, FP * topValue ); void Stack_free( StackNodePtr * topPtr ); diff --git a/src/modules/misc_helper.c b/src/modules/misc_helper.c index a374160..64e220f 100644 --- a/src/modules/misc_helper.c +++ b/src/modules/misc_helper.c @@ -91,7 +91,11 @@ int handleToken( char * token ) } else { - handleOperator( token ); + if ( handleOperator( token ) == EXIT_FAILURE ) + { + isShowingResult = false; + return EXIT_FAILURE; + } } return EXIT_SUCCESS; @@ -125,37 +129,52 @@ int handleNumber( char * token ) return EXIT_SUCCESS; } -void handleOperator( char * token ) +int handleOperator( char * token ) { char operator = token[ 0 ]; if ( Stack_isEmpty( stackPtr ) ) { - puts( "Stack is empty [handle operator begin]" ); + puts( "[handleOperator]: Stack is empty." ); + return EXIT_FAILURE; } else { switch ( operator ) { case 'f': - handleFunction( token ); + if ( handleFunction( token ) == EXIT_FAILURE ) + { + return EXIT_FAILURE; + } + isShowingResult = true; break; case 's': - handleStackFunction( token ); + if ( handleStackFunction( token ) == EXIT_FAILURE ) + { + return EXIT_FAILURE; + } + isShowingResult = false; break; default: - handleFpOperator( token ); + if ( handleFpOperator( token ) == EXIT_FAILURE ) + { + return EXIT_FAILURE; + } + isShowingResult = true; break; } } + + return EXIT_SUCCESS; } -void handleFpOperator( char * token ) +int handleFpOperator( char * token ) { Operator * op = operators; char operators = token[ 0 ]; @@ -164,16 +183,21 @@ void handleFpOperator( char * token ) { if ( ( op->name == operators ) ) { - handleTwoOperandOperation( op->func ); + if ( handleTwoOperandOperation( op->func ) == EXIT_FAILURE ) + { + break; + } Stack_push( &stackPtr, resultFp ); - return; + return EXIT_SUCCESS; } } - puts( "Invalid FP operator" ); + puts( "FP operator failure" ); + + return EXIT_FAILURE; } -void handleFunction( char * token ) +int handleFunction( char * token ) { Operation * op = operations; @@ -183,13 +207,16 @@ void handleFunction( char * token ) { handleOneOperandOperation( op->func ); Stack_push( &stackPtr, resultFp ); - return; + return EXIT_SUCCESS; } } + puts( "Invalid function" ); + + return EXIT_FAILURE; } -void handleStackFunction( char * token ) +int handleStackFunction( char * token ) { Operation * op = stackOperations; @@ -198,22 +225,33 @@ void handleStackFunction( char * token ) if ( strcmp( token, op->name ) == 0 ) { op->func(); - return; + return EXIT_SUCCESS; } } puts( "Invalid stack function" ); + + return EXIT_FAILURE; } -void handleTwoOperandOperation( void ( *operation )( void ) ) +int handleTwoOperandOperation( void ( *operation )( void ) ) { - Stack_pop(&stackPtr, &arg1Fp); - Stack_pop(&stackPtr, &arg2Fp); + 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 ) ) diff --git a/src/modules/misc_helper.h b/src/modules/misc_helper.h index 3bf3630..195d567 100644 --- a/src/modules/misc_helper.h +++ b/src/modules/misc_helper.h @@ -23,19 +23,19 @@ typedef struct } Operator; void header( void ); -void handleArgumentString(char *argumentString); +void handleArgumentString( char * argumentString ); int handleToken( char * token ); -int handleNumber(char *token); -void handleOperator(char *token); -void handleTwoOperandOperation(void (*operation)(void)); -void handleOneOperandOperation( void ( *operation )( void ) ); +int handleNumber( char * token ); +int handleOperator( char * token ); +int handleTwoOperandOperation( void ( * operation )( void ) ); +void handleOneOperandOperation( void ( * operation )( void ) ); void showResult( void ); void getUserInput( char * argumentString ); -void handleFunction( char * token ); +int handleFunction( char * token ); void printStack( void ); void clearStack( void ); -void handleFpOperator( char * token ); -void handleStackFunction( char * token ); +int handleFpOperator( char * token ); +int handleStackFunction( char * token ); void popfromStack( void ); int isValidNumber( const char *str ); From 7e9fcc5e4344471b56cdf9ba89e7e6b33a8b04e0 Mon Sep 17 00:00:00 2001 From: Aleksejs Medveckis Date: Mon, 15 Apr 2024 13:42:14 +0300 Subject: [PATCH 7/8] handle_division_by_zero: * add a check for division by zero --- src/modules/math_helper.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/modules/math_helper.c b/src/modules/math_helper.c index 522dd83..9e53980 100644 --- a/src/modules/math_helper.c +++ b/src/modules/math_helper.c @@ -99,6 +99,12 @@ void FP_mult( void ) void FP_div( void ) { + if ( arg1Fp[ 0 ] == 0 && arg1Fp[ 1 ] == 0 && arg1Fp[ 2 ] == 0 && arg1Fp[ 3 ] == 0 && arg1Fp[ 4 ] == 0 ) + { + puts( "[FP]: Division by zero" ); + return; + } + FP_arg1ToFac(); __asm__ ( "lda #<%v", arg2Fp ); __asm__ ( "ldy #>%v", arg2Fp ); From f437271278591ba2c2045ef9a1177a9fd0ff3cce Mon Sep 17 00:00:00 2001 From: Aleksejs Medveckis Date: Mon, 15 Apr 2024 15:52:22 +0300 Subject: [PATCH 8/8] handle_division_by_zero: * update documentation --- CHANGELOG.md | 9 ++++++++- README.md | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f56a75d..43b07d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,4 +16,11 @@ ## Changed - Optimized command handling functions -- Updated README.md \ No newline at end of file +- Updated README.md + +# [v2.1.0] 2024-04-15 + +## Added + +- functions for the operations with the stack +- error handling diff --git a/README.md b/README.md index 57ce35a..0a308e4 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ 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 | 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.