summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcalculator.cpp78
-rwxr-xr-xcalculator.h41
2 files changed, 78 insertions, 41 deletions
diff --git a/calculator.cpp b/calculator.cpp
index 8a5c5b3..f8c48d0 100755
--- a/calculator.cpp
+++ b/calculator.cpp
@@ -1,35 +1,36 @@
-#include <cstdlib>
#include "calculator.h"
CalculatorWindow::CalculatorWindow( const Rect &rFrame,
const String& szTitle,
unsigned int nWindowLayoutFlags,
unsigned int uiFlags
- ): ApplicationWindow(rFrame, szTitle, nWindowLayoutFlags)
-{
+ ): ApplicationWindow(rFrame, szTitle, nWindowLayoutFlags, 0, uiFlags)
+{
+ Clear();
+
pText = new TextView(this, Rect(15, 30, 160, 50), WINDOW_LAYOUT_FOLLOW_RIGHT , TEXTVIEW_FLAG_SINGLE_LINE);
pText->Enable(false);
pText->Set("0.0");
-
+
Button * pButton;
-
+
pButton = new Button(this, Rect(Point(15, 60), Point(40, 82)), "7", 0, new MessageCommand(BUTTON_SEVEN));
pButton = new Button(this, Rect(Point(45, 60), Point(70, 82)), "8", 0, new MessageCommand(BUTTON_EIGHT));
pButton = new Button(this, Rect(Point(75, 60), Point(100, 82)), "9", 0, new MessageCommand(BUTTON_NINE));
pButton = new Button(this, Rect(Point(105, 60), Point(160, 82)), "Clear", 0, new MessageCommand(BUTTON_CLEAR));
-
+
pButton = new Button(this, Rect(Point(15, 87), Point(40, 109)), "4", 0, new MessageCommand(BUTTON_FOUR));
pButton = new Button(this, Rect(Point(45, 87), Point(70, 109)), "5", 0, new MessageCommand(BUTTON_FIVE));
pButton = new Button(this, Rect(Point(75, 87), Point(100, 109)), "6", 0, new MessageCommand(BUTTON_SIX));
pButton = new Button(this, Rect(Point(105, 87), Point(130, 109)), "+", 0, new MessageCommand(BUTTON_PLUS));
pButton = new Button(this, Rect(Point(135, 87), Point(160, 109)), "-", 0, new MessageCommand(BUTTON_MINUS));
-
+
pButton = new Button(this, Rect(Point(15, 114), Point(40, 136)), "1", 0, new MessageCommand(BUTTON_ONE));
pButton = new Button(this, Rect(Point(45, 114), Point(70, 136)), "2", 0, new MessageCommand(BUTTON_TWO));
pButton = new Button(this, Rect(Point(75, 114), Point(100, 136)), "3", 0, new MessageCommand(BUTTON_THREE));
pButton = new Button(this, Rect(Point(105, 114), Point(130, 136)), "\u00D7", 0, new MessageCommand(BUTTON_TIMES));
pButton = new Button(this, Rect(Point(135, 114), Point(160, 136)), "\u00F7", 0, new MessageCommand(BUTTON_DIVIDE));
-
+
pButton = new Button(this, Rect(Point(15, 141), Point(40, 163)), "0", 0, new MessageCommand(BUTTON_ZERO));
pButton = new Button(this, Rect(Point(45, 141), Point(70, 163)), ".", 0, new MessageCommand(BUTTON_POINT));
pButton = new Button(this, Rect(Point(75, 141), Point(100, 164)), "\u00B1", 0, new MessageCommand(BUTTON_SIGN));
@@ -39,11 +40,10 @@ CalculatorWindow::CalculatorWindow( const Rect &rFrame,
void CalculatorWindow::OnCommand(const MessageCommand *pMessage) {
const String minus = String("-");
const String point = String(".");
- static char operation = 'n';
- static double first_operand = 0.0;
char c[2] = {'0', '\0'};
int message = pMessage->GetID();
- String * line = pText->GetDocument()->GetLine(0);
+
+ line = pText->GetDocument()->GetLine(0);
switch (message) {
case BUTTON_ZERO:
@@ -57,25 +57,20 @@ void CalculatorWindow::OnCommand(const MessageCommand *pMessage) {
case BUTTON_EIGHT:
case BUTTON_NINE:
c[0] += message;
- if (line->Compare("0.0")) {
- pText->Append((const char *)&c);
- } else {
+ if (new_operand) {
pText->Set((const char *)&c);
+ new_operand = false;
+ } else {
+ pText->Append((const char *)&c);
}
break;
case BUTTON_PLUS:
- operation = '+';
- break;
case BUTTON_MINUS:
- operation = '-';
- break;
case BUTTON_TIMES:
- operation = '*';
- break;
case BUTTON_DIVIDE:
- operation = '/';
- break;
case BUTTON_EQUALS:
+ doOperation(message);
+ break;
case BUTTON_POINT:
/* if(line->find(point)) { // FIXME: figure out how to do this more directly
pText->Append(point);
@@ -92,12 +87,45 @@ void CalculatorWindow::OnCommand(const MessageCommand *pMessage) {
}
break;
case BUTTON_CLEAR:
- operation = 'n';
- first_operand = 0.0;
+ Clear();
pText->Set("0.0");
break;
}
- printf("line: %s; first_operand: %f; operator: %c\n", line->c_str(), first_operand, operation);
+ printf("line: %s; total: %.0f; operand: %.0f; operator: %d\n", line->c_str(), total, operand, operation);
+}
+
+void CalculatorWindow::Clear() {
+ operation = BUTTON_PLUS;
+ total = 0.0;
+ operand = 0.0;
+ new_operand = true;
+}
+
+void CalculatorWindow::doOperation(int next_operation) {
+ char str[32] = {0};
+
+ operand = atof(line->c_str());
+
+ switch (operation) {
+ case BUTTON_PLUS:
+ total += operand;
+ break;
+ case BUTTON_MINUS:
+ total -= operand;
+ break;
+ case BUTTON_TIMES:
+ total *= operand;
+ break;
+ case BUTTON_DIVIDE:
+ total /= operand;
+ break;
+ }
+ snprintf(str, 32, "%f", total);
+ pText->Set((const char *)&str);
+
+ operation = next_operation;
+ operand = 0.0;
+ new_operand = true;
}
Calculator::Calculator(int argc, char* argv[]):
diff --git a/calculator.h b/calculator.h
index 2332669..e043274 100755
--- a/calculator.h
+++ b/calculator.h
@@ -1,27 +1,28 @@
#include <gui/ApplicationWindow.h>
#include <gui/Application.h>
#include <gui/TextView.h>
+#include <cstdlib>
using namespace SkyGI;
#define BUTTON_ZERO 0
#define BUTTON_ONE 1
#define BUTTON_TWO 2
-#define BUTTON_THREE 3
+#define BUTTON_THREE 3
#define BUTTON_FOUR 4
#define BUTTON_FIVE 5
-#define BUTTON_SIX 6
-#define BUTTON_SEVEN 7
-#define BUTTON_EIGHT 8
+#define BUTTON_SIX 6
+#define BUTTON_SEVEN 7
+#define BUTTON_EIGHT 8
#define BUTTON_NINE 9
#define BUTTON_PLUS 10
-#define BUTTON_MINUS 11
-#define BUTTON_TIMES 12
-#define BUTTON_DIVIDE 13
+#define BUTTON_MINUS 11
+#define BUTTON_TIMES 12
+#define BUTTON_DIVIDE 13
#define BUTTON_EQUALS 14
-#define BUTTON_POINT 15
+#define BUTTON_POINT 15
#define BUTTON_SIGN 16
-#define BUTTON_CLEAR 17
+#define BUTTON_CLEAR 17
class Calculator : public Application {
public:
@@ -30,11 +31,19 @@ class Calculator : public Application {
class CalculatorWindow : public ApplicationWindow {
public:
- CalculatorWindow( const Rect &rFrame,
- const String& szTitle,
- unsigned int nWindowLayoutFlags,
- unsigned int uiFlags = 0);
- void OnCommand( const MessageCommand *pMessage);
-
- TextView * pText;
+ CalculatorWindow( const Rect &rFrame,
+ const String& szTitle,
+ unsigned int nWindowLayoutFlags,
+ unsigned int uiFlags = 0);
+ void OnCommand( const MessageCommand *pMessage);
+ private:
+ void Clear();
+ void doOperation( int next_operation);
+
+ TextView * pText;
+ String * line;
+ double total;
+ double operand;
+ int operation;
+ bool new_operand;
};