1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#ifdef DEBUG
#define DEBUG true
#else
#define DEBUG false
#endif
typedef struct NumeralPtr
{
char const* symbol;
struct NumeralPtr *next;
struct NumeralPtr *previous;
} numeral_ptr;
char* program_name = NULL;
/*
* Creates new digit for numeral_ptr
* Return pointer to new numeral_ptr
* Returns NULL in case of no memory
*/
numeral_ptr*
new_digit(numeral_ptr* last_numeral, char const* to_first)
{
numeral_ptr* starting_point = malloc(sizeof(numeral_ptr));
if(starting_point == NULL)
{
fprintf(stderr, "error: %s: %s!", program_name, strerror(ENOMEM));
return NULL;
}
starting_point->symbol = to_first;
starting_point->previous = last_numeral;
starting_point->next = NULL;
return starting_point;
}
void
increment(numeral_ptr* numeral, char* num_first, char* num_last)
{
bool cycled = false;
if( numeral->symbol == num_last )
{
if( numeral->next == NULL )
numeral->next = new_digit(numeral, num_first);
else
increment(numeral->next, num_first, num_last);
cycled = true;
}
if( cycled )
numeral->symbol = num_first;
else
++(numeral->symbol);
}
bool
is_same_size(numeral_ptr* numeral, char* number_string)
{
for( size_t i = strlen(number_string) ; !(i == 0) ; ++i )
{
if(numeral == NULL)
return false;
numeral = numeral->next;
}
return (numeral == NULL) ? true : false;
}
bool
is_the_same(numeral_ptr* numeral, char* number_arg)
{
while( !(numeral == NULL) )
{
if( *(numeral->symbol) == *(number_arg) )
{
numeral = numeral->next;
++(number_arg);
continue;
}
return false;
}
return (*number_arg == '\0') ? true : false;
}
void
print_numeral(numeral_ptr* numeral)
{
while( !(numeral == NULL) )
{
printf("%c", *(numeral->symbol));
numeral = numeral->next;
}
printf("\n");
}
int
main(int argc, char* argv[])
{
program_name = argv[0];
/* Numeral System variables from MACROS */
char* from = malloc( (strlen(FROM_NUMERICALS) + 1) * sizeof(char) );
char* to = malloc( (strlen(TO_NUMERICALS) + 1) * sizeof(char) );
strcpy(from, FROM_NUMERICALS);
strcpy(to, TO_NUMERICALS);
/* Number variables to be converted */
char* number = argv[1];
/* _first and _last variables */
char* from_first = from;
char* from_last = from + (strlen(from) - 1);
char* to_first = to;
char* to_last = to + (strlen(to) - 1);
char* number_first = number;
char* number_last = number + (strlen(number) - 1);
/* 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) )
{
if(DEBUG)
{
puts("----");
print_numeral(result);
}
increment(result, to_first, to_last);
if(DEBUG)
print_numeral(counting);
increment(counting, from_first, from_last);
}
if(DEBUG)
puts("----");
/* print */
if(DEBUG)
{
printf("counting: ");
print_numeral(counting);
printf("result: ");
}
print_numeral(result);
return EXIT_SUCCESS;
}
|