-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC2SCalculatorController.cpp
157 lines (132 loc) · 5.14 KB
/
C2SCalculatorController.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
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
#include "C2SCalculatorController.h"
using boost::multiprecision::cpp_int;
using boost::multiprecision::cpp_dec_float_50;
namespace c2s
{
namespace calculator
{
C2SCalculatorController::C2SCalculatorController():m_sCurrentNumber("0"),
m_sPreviousNumber("undefined"),m_sOperation("undefined"),m_sMemory("0"),m_bFloat(false)
{
std::cout<<"Created Now"<<std::endl;
}
C2SCalculatorController::C2SCalculatorController(std::string currentNumber,std::string previousNumber,
std::string operation,std::string memory,bool isFloat):m_sCurrentNumber(currentNumber),
m_sPreviousNumber(previousNumber),m_sOperation(operation),m_sMemory(memory),m_bFloat(isFloat){
}
void C2SCalculatorController::clear(){
m_sCurrentNumber = "undefined";
m_sPreviousNumber ="undefined";
m_sOperation = "undefined";
m_bFloat = false;
}
std::string C2SCalculatorController::getCurrentNumber(){
return m_sCurrentNumber;
}
std::string C2SCalculatorController::getPreviousNumber(){
return m_sPreviousNumber;
}
std::string C2SCalculatorController::getOperation(){
return m_sOperation;
}
std::string C2SCalculatorController::getMemoryValue(){
return m_sMemory;
}
void C2SCalculatorController::perform() {
cpp_int intResult;
cpp_dec_float_50 decResult;
std::string current = m_sCurrentNumber;
try{
if(m_sOperation.compare("add")==0){
if(!m_bFloat){
cpp_int a(m_sPreviousNumber),b(m_sCurrentNumber);
intResult = C2SCalculator<cpp_int>::add(a,b);
} else {
cpp_dec_float_50 a(m_sPreviousNumber),b(m_sCurrentNumber);
decResult = C2SCalculator<cpp_dec_float_50>::add(a,b);
}
m_sCurrentNumber = (m_bFloat)?TOSTR(decResult):TOSTR(intResult);
}
if(m_sOperation.compare("sub")==0){
if(!m_bFloat){
cpp_int a(m_sPreviousNumber),b(m_sCurrentNumber);
intResult = C2SCalculator<cpp_int>::sub(a,b);
} else {
cpp_dec_float_50 a(m_sPreviousNumber),b(m_sCurrentNumber);
decResult = C2SCalculator<cpp_dec_float_50>::sub(a,b);
}
m_sCurrentNumber = (m_bFloat)?TOSTR(decResult):TOSTR(intResult);
}
if(m_sOperation.compare("div")==0){
m_bFloat = true;
cpp_dec_float_50 a(m_sPreviousNumber),b(m_sCurrentNumber);
decResult = C2SCalculator<cpp_dec_float_50>::div(a,b);
m_sCurrentNumber = (m_bFloat)?TOSTR(decResult):TOSTR(intResult);
}
if(m_sOperation.compare("mul")==0){
if(!m_bFloat){
cpp_int a(m_sPreviousNumber),b(m_sCurrentNumber);
intResult = C2SCalculator<cpp_int>::mul(a,b);
} else {
cpp_dec_float_50 a(m_sPreviousNumber),b(m_sCurrentNumber);
decResult = C2SCalculator<cpp_dec_float_50>::mul(a,b);
}
m_sCurrentNumber = (m_bFloat)?TOSTR(decResult):TOSTR(intResult);
}
if(m_sOperation.compare("mod")==0){
if(!m_bFloat){
cpp_int a(m_sPreviousNumber),b(m_sCurrentNumber);
intResult = C2SCalculator<cpp_int>::mod(a,b);
} else {
throw std::invalid_argument( "should be integer to mod" );
}
m_sCurrentNumber = (m_bFloat)?TOSTR(decResult):TOSTR(intResult);
}
if(m_sOperation.compare("sqrt")==0){
m_bFloat = true;
cpp_dec_float_50 num(m_sCurrentNumber);
decResult = C2SCalculator<cpp_dec_float_50>::sqrt(num);
m_sCurrentNumber = (m_bFloat)?TOSTR(decResult):TOSTR(intResult);
}
if(m_sOperation.compare("mempls")==0){
if(!m_bFloat){
cpp_int a(m_sMemory),b(m_sCurrentNumber);
intResult = C2SCalculator<cpp_int>::add(a,b);
} else {
cpp_dec_float_50 a(m_sMemory),b(m_sCurrentNumber);
decResult = C2SCalculator<cpp_dec_float_50>::add(a,b);
}
m_sMemory = (m_bFloat)?TOSTR(decResult):TOSTR(intResult);
return;
}
if(m_sOperation.compare("memmin")==0){
if(!m_bFloat){
cpp_int a(m_sMemory),b(m_sCurrentNumber);
intResult = C2SCalculator<cpp_int>::sub(a,b);
} else {
cpp_dec_float_50 a(m_sMemory),b(m_sCurrentNumber);
decResult = C2SCalculator<cpp_dec_float_50>::sub(a,b);
}
m_sMemory = (m_bFloat)?TOSTR(decResult):TOSTR(intResult);
return;
}
if(m_sOperation.compare("memrcl")==0){
// return m_sMemory;
m_sCurrentNumber = m_sMemory;
}
if(m_sOperation.compare("clear")==0){
clear();
return;
// return m_sCurrentNumber;
}
} catch (const std::exception& e) {
clear();
std::cerr << e.what();
throw std::invalid_argument( "Operation failed!" );
}
// std::cout<<"value of current: "<<m_sCurrentNumber<<std::endl;
m_sPreviousNumber = current;
// return m_sCurrentNumber;
}
}
}