aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Santos <dacs.git@brilhante.top>2022-03-09 14:06:02 +0000
committerDaniel Santos <dacs.git@brilhante.top>2022-03-09 14:06:02 +0000
commitdcc93c0ba6fd640456544e911bcdcf5d6e66a688 (patch)
treef79f7540eeda3d2c1c4bacc02ecd5f93c3e2bd6b
parentef7e6f99a265339336bf02305f9d25cfedbf04e7 (diff)
downloadnumericx-c-dcc93c0ba6fd640456544e911bcdcf5d6e66a688.tar.gz
add TO_UNITS_ON_THE_END and FROM_UNITS_ON_THE_END
* add reverse_string() * number variable instead of number_first/number_last * add flags TO_UNITS_ON_THE_END and FROM_UNITS_ON_THE_END Signed-off-by: Daniel Santos <dacs.git@brilhante.top>
-rw-r--r--numericx.c55
1 files changed, 49 insertions, 6 deletions
diff --git a/numericx.c b/numericx.c
index 87cde70..2a611d8 100644
--- a/numericx.c
+++ b/numericx.c
@@ -10,6 +10,18 @@
#define DEBUG false
#endif
+#ifdef TO_UNITS_ON_THE_END
+#define TO_UNITS_ON_THE_END true
+#else
+#define TO_UNITS_ON_THE_END false
+#endif
+
+#ifdef FROM_UNITS_ON_THE_END
+#define FROM_UNITS_ON_THE_END true
+#else
+#define FROM_UNITS_ON_THE_END false
+#endif
+
typedef struct NumeralPtr
{
char const* symbol;
@@ -83,15 +95,43 @@ is_the_same(numeral_ptr* numeral, char* number_arg)
void
print_numeral(numeral_ptr* numeral)
{
- while( !(numeral == NULL) )
+ if( TO_UNITS_ON_THE_END )
{
- printf("%c", *(numeral->symbol));
- numeral = numeral->next;
+ while( !(numeral->next == NULL) )
+ numeral = numeral->next;
+
+ while( !(numeral == NULL) )
+ {
+ printf("%c", *(numeral->symbol));
+ numeral = numeral->previous;
+ }
+ }
+ else
+ {
+ while( !(numeral == NULL) )
+ {
+ printf("%c", *(numeral->symbol));
+ numeral = numeral->next;
+ }
}
printf("\n");
}
+void
+reverse_string(char* string)
+{
+ char tmp;
+ char* string_end = string + strlen(string);
+
+ while ( --string_end > string )
+ {
+ tmp = *string;
+ *string++ = *string_end;
+ *string_end = tmp;
+ }
+}
+
int
main(int argc, char* argv[])
{
@@ -121,15 +161,17 @@ main(int argc, char* argv[])
char* to_first = to;
char* to_last = to + (strlen(to) - 1);
- char* number_first = number;
- char* number_last = number + (strlen(number) - 1);
+ if( FROM_UNITS_ON_THE_END )
+ {
+ reverse_string(number);
+ }
/* initializing counting and result */
numeral_ptr* counting = new_digit(NULL, from_first);
numeral_ptr* result = new_digit(NULL, to_first);
/* increments until it finishes */
- while( !is_the_same(counting, number_first) )
+ while( !is_the_same(counting, number) )
{
if(DEBUG)
{
@@ -152,6 +194,7 @@ main(int argc, char* argv[])
print_numeral(counting);
printf("result: ");
}
+
print_numeral(result);
return EXIT_SUCCESS;