-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleCalculatorStatus.cpp
More file actions
154 lines (129 loc) · 3.51 KB
/
Copy pathSimpleCalculatorStatus.cpp
File metadata and controls
154 lines (129 loc) · 3.51 KB
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
//
// Created by seawave on 2021/10/3.
//
#include <cmath>
#include "SimpleCalculatorStatus.h"
SimpleCalculatorStatus::SimpleCalculatorStatus() : currentText("0"), negative(false), decimal(false), operation(0),
leftOperand(0.0), clearing(false) {}
void SimpleCalculatorStatus::addNumber(unsigned int number) {
if (isEmpty() || clearing)
currentText = QChar(number + '0');
else
currentText += QChar(number + '0');
if (negative && currentText[0] != '-' && !isEmpty())
currentText = '-' + currentText;
clearing = false;
}
bool SimpleCalculatorStatus::isEmpty() {
return currentText == "" || currentText == "0";
}
QString SimpleCalculatorStatus::getCurrentText() const {
return currentText;
}
void SimpleCalculatorStatus::backspace() {
if (clearing) {
currentText = "0";
clearing = false;
return;
}
if (isEmpty() && negative)
negative = false;
if (currentText[currentText.length() - 1] == '.')
decimal = false;
currentText.remove(currentText.length() - 1, 1);
if (isEmpty())
currentText = "0";
}
void SimpleCalculatorStatus::clearError() {
currentText = "0";
negative = false;
decimal = false;
clearing = false;
}
void SimpleCalculatorStatus::clear() {
clearError();
operation = 0;
leftOperand = 0.0;
}
void SimpleCalculatorStatus::addPoint() {
if (isEmpty() || clearing)
currentText = "0.";
else
currentText += ".";
decimal = true;
clearing = false;
}
void SimpleCalculatorStatus::changeSign() {
negative = !negative;
if (isEmpty())
currentText = "0";
else {
if (negative && currentText[0] != '-')
currentText = '-' + currentText;
else if (!negative && currentText[0] == '-')
currentText.remove(0, 1);
}
}
bool SimpleCalculatorStatus::isDecimal() const {
return decimal;
}
void SimpleCalculatorStatus::add() {
if (operation)
calculate();
leftOperand = currentText.toDouble();
operation = '+';
clearing = true;
}
void SimpleCalculatorStatus::calculate() {
double rightOperand = currentText.toDouble();
switch (operation) {
case '+':
currentText.setNum(leftOperand + rightOperand);
break;
case '-':
currentText.setNum(leftOperand - rightOperand);
break;
case '*':
currentText.setNum(leftOperand * rightOperand);
break;
case '/':
currentText.setNum(leftOperand / rightOperand);
break;
}
leftOperand = 0.0;
operation = 0;
clearing = true;
}
void SimpleCalculatorStatus::subtract() {
if (operation)
calculate();
leftOperand = currentText.toDouble();
operation = '-';
clearing = true;
}
void SimpleCalculatorStatus::multiply() {
if (operation)
calculate();
leftOperand = currentText.toDouble();
operation = '*';
clearing = true;
}
void SimpleCalculatorStatus::divide() {
if (operation)
calculate();
leftOperand = currentText.toDouble();
operation = '/';
clearing = true;
}
void SimpleCalculatorStatus::sqrt() {
currentText.setNum(std::sqrt(currentText.toDouble()));
clearing = true;
}
void SimpleCalculatorStatus::percent() {
currentText.setNum(currentText.toDouble() / 100.0);
clearing = true;
}
void SimpleCalculatorStatus::reciprocal() {
currentText.setNum(1.0 / currentText.toDouble());
clearing = true;
}