-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiply.c
160 lines (131 loc) · 2.5 KB
/
multiply.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/********* DEFINED CONSTANTS ********/
#define MAX 32
/********* FUNCTION DECLARATION *********/
void multiply(int m[], int q[], int len1, int len2);
int ripple(int m[], int a[], int ind);
int fulladd(int x, int y, int z, int a[], int ind);
void reverse(int str[], int len);
/********* MAIN STARTS HERE *********/
int main(int argc, char **argv)
{
int i = 0, len1, len2;
int m[MAX], q[MAX];
char *str1, *str2;
if (argc != 3)
{
fprintf(stderr, "Usage: %s <Multiplicand> <Multiplier>.\n", argv[0]);
exit(1);
}
str1 = argv[1];
str2 = argv[2];
len1 = strlen(str1);
len2 = strlen(str2);
while (*str1 != '\0')
{
m[i] = (int)(*str1 - '0');
str1++;
i++;
}
i = 0;
while (*str2 != '\0')
{
q[i] = (int)(*str2 - '0');
str2++;
i++;
}
multiply(m, q, len1, len2);
exit(0);
}
/********* FUNCTION DEFINITION *********/
void multiply(int m[], int q[], int len1, int len2)
{
int i = 0, j = 0, k, l, n = 0, flen;
int a[MAX], ac[MAX];
reverse(m, len1);
reverse(q, len2);
if (len1 < len2)
{
for (j = len1; j < len2; j++)
{
m[j] = 0;
}
len1 = len2;
}
else if (len2 < len1)
{
for (j = len2; j < len1; j++)
{
q[j] = 0;
}
len2 = len1;
}
for (j = 0; j < len1+len2; j++)
{
a[j] = 0;
}
while (i < len2)
{
for (j = 0; j < n; j++)
{
ac[j] = 0;
}
for (k = j,l = 0; k < j+len2; k++,l++)
{
ac[k] = m[l];
}
if (q[i] == 1)
{
flen = ripple(ac, a, k);
}
n++;
i++;
}
for (i = flen; i >= 0; i--)
{
printf("%d", a[i]);
}
printf(" is the resultant value.\n");
return ;
}
int ripple(int m[], int a[], int ind)
{
int i = 0, x1, x2, carry = 0;
while (i < ind)
{
x1 = m[i];
x2 = a[i];
carry = fulladd(x1, x2, carry, a, i);
i++;
}
if (carry == 1)
{
a[i] = carry;
}
else
{
i = i - 1;
}
return i;
}
int fulladd(int x, int y, int z, int a[], int ind)
{
int sum, carry;
sum = (x ^ y ^ z);
carry = ((x & y) | (y & z) | (x & z));
a[ind] = sum;
return carry;
}
void reverse(int str[], int len)
{
int c, i, j;
for (i = 0, j = (len-1); i < j; i++, j--)
{
c = str[i];
str[i] = str[j];
str[j] = c;
}
return ;
}