From 4745d778aede297ccaaeaaf4421d654d8109ca3e Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Tue, 18 Dec 2007 20:04:30 +0000 Subject: Make the buttons do stuff. git-svn-id: svn://mattst88.com/svn/calculator/trunk@6 40705d3a-cdd4-446d-8ced-669f9f7342eb --- calculator.cpp | 40 +++++++++++++++++++++++++++++++++++++++- calculator.h | 1 - 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/calculator.cpp b/calculator.cpp index 45ab6ce..8a5c5b3 100755 --- a/calculator.cpp +++ b/calculator.cpp @@ -37,7 +37,15 @@ CalculatorWindow::CalculatorWindow( const Rect &rFrame, } void CalculatorWindow::OnCommand(const MessageCommand *pMessage) { - switch (pMessage->GetID()) { + 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); + + switch (message) { case BUTTON_ZERO: case BUTTON_ONE: case BUTTON_TWO: @@ -48,18 +56,48 @@ void CalculatorWindow::OnCommand(const MessageCommand *pMessage) { case BUTTON_SEVEN: case BUTTON_EIGHT: case BUTTON_NINE: + c[0] += message; + if (line->Compare("0.0")) { + pText->Append((const char *)&c); + } else { + pText->Set((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: case BUTTON_POINT: +/* if(line->find(point)) { // FIXME: figure out how to do this more directly + pText->Append(point); + }*/ + if (!strchr(line->c_str(), '.')) { + pText->Append(point); + } + break; case BUTTON_SIGN: + if (line->c_str()[0] != '-') { + pText->Insert(minus, Point(0, 0)); + } else { + pText->Remove(Point(0,0)); + } break; case BUTTON_CLEAR: + operation = 'n'; + first_operand = 0.0; pText->Set("0.0"); break; } + printf("line: %s; first_operand: %f; operator: %c\n", line->c_str(), first_operand, operation); } Calculator::Calculator(int argc, char* argv[]): diff --git a/calculator.h b/calculator.h index 87cd49a..2332669 100755 --- a/calculator.h +++ b/calculator.h @@ -37,5 +37,4 @@ class CalculatorWindow : public ApplicationWindow { void OnCommand( const MessageCommand *pMessage); TextView * pText; - }; -- cgit v1.2.3