blob: dbd46fb4f975314dfc31b43558050830c1330201 (
plain)
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
|
#include "Calculator.h"
#include <AK/Assertions.h>
#include <math.h>
Calculator::Calculator()
{
}
Calculator::~Calculator()
{
}
double Calculator::begin_operation(Operation operation, double argument)
{
double res = 0.0;
switch (operation) {
case Operation::None:
ASSERT_NOT_REACHED();
case Operation::Add:
case Operation::Subtract:
case Operation::Multiply:
case Operation::Divide:
m_saved_argument = argument;
m_operation_in_progress = operation;
return argument;
case Operation::Sqrt:
if (argument < 0.0) {
m_has_error = true;
return argument;
}
res = sqrt(argument);
clear_operation();
break;
case Operation::Inverse:
if (argument == 0.0) {
m_has_error = true;
return argument;
}
res = 1 / argument;
clear_operation();
break;
case Operation::Percent:
res = argument * 0.01;
break;
case Operation::ToggleSign:
res = -argument;
break;
case Operation::MemClear:
m_mem = 0.0;
res = argument;
break;
case Operation::MemRecall:
res = m_mem;
break;
case Operation::MemSave:
m_mem = argument;
res = argument;
break;
case Operation::MemAdd:
m_mem += argument;
res = m_mem;
break;
}
return res;
}
double Calculator::finish_operation(double argument)
{
double res = 0.0;
switch (m_operation_in_progress) {
case Operation::None:
return argument;
case Operation::Add:
res = m_saved_argument + argument;
break;
case Operation::Subtract:
res = m_saved_argument - argument;
break;
case Operation::Multiply:
res = m_saved_argument * argument;
break;
case Operation::Divide:
if (argument == 0.0) {
m_has_error = true;
return argument;
}
res = m_saved_argument / argument;
break;
case Operation::Sqrt:
case Operation::Inverse:
case Operation::Percent:
case Operation::ToggleSign:
case Operation::MemClear:
case Operation::MemRecall:
case Operation::MemSave:
case Operation::MemAdd:
ASSERT_NOT_REACHED();
}
clear_operation();
return res;
}
void Calculator::clear_operation()
{
m_operation_in_progress = Operation::None;
m_saved_argument = 0.0;
}
|