-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
56 lines (48 loc) · 960 Bytes
/
main.cpp
File metadata and controls
56 lines (48 loc) · 960 Bytes
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
#include <iostream>
int negate(int a) {
int result = 0;
int sign = a > 0 ? -1 : 1;
while (a != 0) {
result += sign;
a += sign;
}
return result;
}
int substract(int a, int b) {
return a + negate(b);
}
int multiply(int a, int b) {
if (a < b) {
return multiply(b, a);
}
int sum = 0;
for (int i = 0; i < std::abs(b); i++) {
sum += a;
}
if (b < 0) {
sum = negate(sum);
}
return sum;
}
int divide(int a, int b) {
if (b == 0) {
return -31337;
}
int absA = std::abs(a);
int absB = std::abs(b);
int product = 0;
int x = 0;
while (product + absB <= absA) {
product += absB;
x++;
}
if ((a < 0 && b < 0) || (a > 0 && b > 0)) {
return x;
}
return negate(x);
}
int main() {
std::cout << multiply(5, 10) << std::endl;
std::cout << divide(10, 2) << std::endl;
return 0;
}