forked from Salamander0/Progtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework2.c
153 lines (127 loc) · 3.92 KB
/
homework2.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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct binary{
char * number;
size_t length;
}Tbinary;
//exterminate leading zeros
size_t exterminate(char * bin, size_t length){
char * pch = NULL;
long position = 0;
pch = strchr(bin, '1');
if(pch==NULL){
bin[1] = '\0';
length = 2;
}
else{
position = pch-bin;
strcpy(bin, pch);
}
return (length-position);
}
int binaryAdd(Tbinary first, Tbinary second){
int a=0, b=0, sum=0, carry=0;
size_t index = first.length;
first.number[first.length] = '\0';
while((first.length != 0) || (carry != 0)){
if(first.length>1) a= first.number[first.length-2]-'0';
else a = 0;
if(second.length>1) b= second.number[second.length-2]-'0';
else b = 0;
sum = (a+b+carry)%2;
first.number[first.length-1] = (sum)+'0';
carry = (a+b+carry)/2;
if(first.length >0)first.length--;
if(second.length >0)second.length--;
}
exterminate(first.number,index);
printf("Sum: %s\n", first.number);
return EXIT_SUCCESS;
}
int get_number(Tbinary *bin_addr){
char * tmp, * bin;
char ch=1;
int size = 1, index = 0;
bin = bin_addr->number;
while(ch){
ch = getc(stdin);
if((ch == '\n') || (ch == ' ')) ch = '\0';
if((ch-'0' != 0) && (ch-'0' != 1) && (ch != '\0')) return EXIT_FAILURE;
if (size-1 <=index){
size += 5;
tmp = (char *)realloc(bin, size*sizeof(char));
if(tmp == NULL){
return EXIT_FAILURE;
}
bin = tmp;
bin_addr->number = bin;
}
bin[index++] = ch;
}
bin_addr->length = index;
bin_addr->length = exterminate(bin_addr->number, bin_addr->length);
return EXIT_SUCCESS;
}
int main (void)
{
Tbinary bin1 = {bin1.number = NULL, bin1.length = 0};
Tbinary bin2 = {bin2.number = NULL, bin2.length = 0};
//allocate space for first number
bin1.number = (char *)malloc(sizeof(char));
if(bin1.number == NULL)
return EXIT_FAILURE;
//allocate space for second number
bin2.number = (char *)malloc(sizeof(char));
if(bin2.number == NULL){
free(bin1.number);
return EXIT_FAILURE;
}
printf("Enter two binary numbers:\n");
//number1 load
if(get_number(&bin1) != EXIT_SUCCESS){
free(bin1.number);
free(bin2.number);
printf("Invalid input.\n");
return EXIT_FAILURE;
}
//number2 load
if(get_number(&bin2) != EXIT_SUCCESS){
free(bin1.number);
free(bin2.number);
printf("Invalid input.\n");
return EXIT_FAILURE;
}
//add the two numbers
if(bin1.length >= bin2.length){
if(binaryAdd(bin1, bin2) != EXIT_SUCCESS){
free(bin1.number);
free(bin2.number);
printf("Invalid input.\n");
return EXIT_FAILURE;
}
}
else{
if(binaryAdd(bin2, bin1) != EXIT_SUCCESS){
free(bin1.number);
free(bin2.number);
printf("Invalid input.\n");
return EXIT_FAILURE;
}
}
free(bin1.number);
free(bin2.number);
return EXIT_SUCCESS;
}