-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAddOperations.cs
86 lines (74 loc) · 1.96 KB
/
AddOperations.cs
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
namespace PracticeQuestionsSharp.Exercises.Numbers
{
//Write methods for multiply, subtract and divide (*,-,/) using only the add (+) operator.
public static class AddOperations
{
public static int Multiply(this int a, int b)
{
if (a < 0 && b < 0)
{
a = ChangeSign(a);
b = ChangeSign(b);
}
int small = a;
int large = b;
int result = 0;
if (b < a)
{
small = b;
large = a;
}
//We increment by small instead of large because it may be a negative value
for (int i = 0; i < large; ++i)
{
result += small;
}
return result;
}
public static int Subtract(this int a, int b)
{
return a + b.ChangeSign();
}
public static int Divide(this int a, int b)
{
int total = 0;
int i = 0;
int sign = 1;
if (a < 0 && b < 0)
{
a = ChangeSign(a);
b = ChangeSign(b);
}
else if (a < 0)
{
a = a.ChangeSign();
sign = -1;
}
else if (b < 0)
{
b = b.ChangeSign();
sign = -1;
}
while (total < a)
{
total += b;
i += sign;
}
if (total > a) i += sign < 0 ? 1 : -1;
return i;
}
//Assuming we can't use -(number) but we can use -1.
private static int ChangeSign(this int a)
{
int result = 0;
int sign = -1;
if (a < 0) sign = 1;
while (a != 0)
{
result += sign;
a += sign;
}
return result;
}
}
}