-
Notifications
You must be signed in to change notification settings - Fork 0
/
101-mul.c
232 lines (194 loc) · 4.56 KB
/
101-mul.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
int find_len(char *str);
char *create_xarray(int size);
char *iterate_zeroes(char *str);
void get_prod(char *prod, char *mult, int digit, int zeroes);
void add_nums(char *final_prod, char *next_prod, int next_len);
/**
* find_len - Finds the length of a string.
* @str: The string to be measured.
*
* Return: The length of the string.
*/
int find_len(char *str)
{
int len = 0;
while (*str++)
len++;
return (len);
}
/**
* create_xarray - Creates an array of chars and initializes it with
* the character 'x'. Adds a terminating null byte.
* @size: The size of the array to be initialized.
*
* Description: If there is insufficient space, the
* function exits with a status of 98.
* Return: A pointer to the array.
*/
char *create_xarray(int size)
{
char *array;
int index;
array = malloc(sizeof(char) * size);
if (array == NULL)
exit(98);
for (index = 0; index < (size - 1); index++)
array[index] = 'x';
array[index] = '\0';
return (array);
}
/**
* iterate_zeroes - Iterates through a string of numbers containing
* leading zeroes until it hits a non-zero number.
* @str: The string of numbers to be iterate through.
*
* Return: A pointer to the next non-zero element.
*/
char *iterate_zeroes(char *str)
{
while (*str && *str == '0')
str++;
return (str);
}
/**
* get_digit - Converts a digit character to a corresponding int.
* @c: The character to be converted.
*
* Description: If c is a non-digit, the function
* exits with a status of 98.
* Return: The converted int.
*/
int get_digit(char c)
{
int digit = c - '0';
if (digit < 0 || digit > 9)
{
printf("Error\n");
exit(98);
}
return (digit);
}
/**
* get_prod - Multiplies a string of numbers by a single digit.
* @prod: The buffer to store the result.
* @mult: The string of numbers.
* @digit: The single digit.
* @zeroes: The necessary number of leading zeroes.
*
* Description: If mult contains a non-digit, the function
* exits with a status value of 98.
*/
void get_prod(char *prod, char *mult, int digit, int zeroes)
{
int mult_len, num, tens = 0;
mult_len = find_len(mult) - 1;
mult += mult_len;
while (*prod)
{
*prod = 'x';
prod++;
}
prod--;
while (zeroes--)
{
*prod = '0';
prod--;
}
for (; mult_len >= 0; mult_len--, mult--, prod--)
{
if (*mult < '0' || *mult > '9')
{
printf("Error\n");
exit(98);
}
num = (*mult - '0') * digit;
num += tens;
*prod = (num % 10) + '0';
tens = num / 10;
}
if (tens)
*prod = (tens % 10) + '0';
}
/**
* add_nums - Adds the numbers stored in two strings.
* @final_prod: The buffer storing the running final product.
* @next_prod: The next product to be added.
* @next_len: The length of next_prod.
*/
void add_nums(char *final_prod, char *next_prod, int next_len)
{
int num, tens = 0;
while (*(final_prod + 1))
final_prod++;
while (*(next_prod + 1))
next_prod++;
for (; *final_prod != 'x'; final_prod--)
{
num = (*final_prod - '0') + (*next_prod - '0');
num += tens;
*final_prod = (num % 10) + '0';
tens = num / 10;
next_prod--;
next_len--;
}
for (; next_len >= 0 && *next_prod != 'x'; next_len--)
{
num = (*next_prod - '0');
num += tens;
*final_prod = (num % 10) + '0';
tens = num / 10;
final_prod--;
next_prod--;
}
if (tens)
*final_prod = (tens % 10) + '0';
}
/**
* main - Multiplies two positive numbers.
* @argv: The number of arguments passed to the program.
* @argc: An array of pointers to the arguments.
*
* Description: If the number of arguments is incorrect or one number
* contains non-digits, the function exits with a status of 98.
* Return: Always 0.
*/
int main(int argc, char *argv[])
{
char *final_prod, *next_prod;
int size, index, digit, zeroes = 0;
if (argc != 3)
{
printf("Error\n");
exit(98);
}
if (*(argv[1]) == '0')
argv[1] = iterate_zeroes(argv[1]);
if (*(argv[2]) == '0')
argv[2] = iterate_zeroes(argv[2]);
if (*(argv[1]) == '\0' || *(argv[2]) == '\0')
{
printf("0\n");
return (0);
}
size = find_len(argv[1]) + find_len(argv[2]);
final_prod = create_xarray(size + 1);
next_prod = create_xarray(size + 1);
for (index = find_len(argv[2]) - 1; index >= 0; index--)
{
digit = get_digit(*(argv[2] + index));
get_prod(next_prod, argv[1], digit, zeroes++);
add_nums(final_prod, next_prod, size - 1);
}
for (index = 0; final_prod[index]; index++)
{
if (final_prod[index] != 'x')
putchar(final_prod[index]);
}
putchar('\n');
free(next_prod);
free(final_prod);
return (0);
}