-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperator.cpp
114 lines (107 loc) · 3.66 KB
/
Operator.cpp
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
/*
Authors: Eduardo Migueis and Rodrigo Smith.
Repository: https://github.com/edumigueis/Base-Calculator
License: MIT License.
*/
#include <iostream>
#include<string>
#include <stdlib.h>
#include "Adder.cpp"
#include "Subtractor.cpp"
#include "Multiplier.cpp"
using std::string;
class Operator {
private:
string memberA, memberB;
int base;
public:
Operator(string a, string b, int numberBase) {
memberA = a;
memberB = b;
base = numberBase;
}
string add() {
if (memberA.find('-') != std::string::npos && memberB.find('-') != std::string::npos) {
memberA = NumberFormatter::removeMinusSign(memberA);
memberB = NumberFormatter::removeMinusSign(memberB);
Adder adder(memberA, memberB, base);
string res = NumberFormatter::addMinusSign(adder.add());
return res;
}
else
if (memberA.find('-') == std::string::npos && memberB.find('-') != std::string::npos) {
memberB = NumberFormatter::removeMinusSign(memberB);
Subtractor sub(this->memberA, this->memberB, base);
return sub.subtract();
} // both will be added as positive
else
if (memberA.find('-') != std::string::npos && memberB.find('-') == std::string::npos) {
memberA = NumberFormatter::removeMinusSign(memberA);
Subtractor sub(this->memberB, this->memberA, base);
return sub.subtract(); //still have to remove minus sign
}
else {
Adder adder(memberA, memberB, base);
return adder.add();
}
}
string subtract() {
if (memberA.find('-') != std::string::npos && memberB.find('-') == std::string::npos) {
memberA = NumberFormatter::removeMinusSign(memberA);
Adder adder(memberA, memberB, base);
string res = NumberFormatter::addMinusSign(adder.add());
return res;
}
else
if (memberA.find('-') == std::string::npos && memberB.find('-') != std::string::npos) {
memberB = NumberFormatter::removeMinusSign(memberB);
Adder adder(memberA, memberB, base);
return adder.add();
} // both will be added as positive
else
if (memberA.find('-') != std::string::npos && memberB.find('-') != std::string::npos) {
memberB = NumberFormatter::removeMinusSign(memberB);
memberA = NumberFormatter::removeMinusSign(memberA);
Subtractor sub(this->memberB, this->memberA, base);
return sub.subtract(); //still have to remove minus sign
}
else {
Subtractor sub(this->memberA, this->memberB, base);
return sub.subtract(); //both numbers are positive and minus operation will be performed
}
}
string multiply() {
if (memberA.find('-') != std::string::npos && memberB.find('-') != std::string::npos) {
Multiplier mult(this->memberA, this->memberB, base);
return mult.multiply();
}
else {
if (memberA.find('-') != std::string::npos && memberB.find('-') || memberB.find('-') != std::string::npos) {
memberB = NumberFormatter::removeMinusSign(memberB);
memberA = NumberFormatter::removeMinusSign(memberA);
Multiplier mult(this->memberA, this->memberB, base);
return NumberFormatter::addMinusSign(mult.multiply());
}
else {
Multiplier mult(this->memberA, this->memberB, base);
return mult.multiply();
}
}
}
string divide() {
if (memberA.find('-') != std::string::npos && memberB.find('-') != std::string::npos) {
//return Divider::divide(this->memberA, this->memberB, base);
}
else {
if (memberA.find('-') != std::string::npos && memberB.find('-') || memberB.find('-') != std::string::npos) {
memberB = NumberFormatter::removeMinusSign(memberB);
memberA = NumberFormatter::removeMinusSign(memberA);
//return Divider::divide(this->memberA, this->memberB, base);
}
else {
//return Divider::divide(this->memberA, this->memberB, base);
}
}
return "";
}
};