// $Id: calc.cpp,v 1.26 2001/09/27 08:24:57 wyy Exp $ // // William Emmanuel S. YU // Ateneo de Manila University, Philippines // // QT C++ Calculator // Copyright (C) 2002 William Emmanuel S. Yu // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // #include "calc.h" #include #ifdef DEBUG #include #endif FCalcMethod::FCalcMethod( QWidget* parent, const char* name, bool modal, WFlags fl ) : FCalc( parent, name, modal, fl ) { if ( !name ) setName( "FCalcMethod" ); myString = new QString(); myMonitor = 1; operand1 = 0; operand2 = 0; myOperator = 0; } FCalcMethod::~FCalcMethod() { } // perform square root void FCalcMethod::psqrt() { #ifdef DEBUG cout << "Data:" << operand1 << " " << operand2 << " " << myOperator << endl; #endif QString s2; s2 = Display->text(); bool ok; operand2 = s2.toDouble ( &ok ); result = sqrt(operand2); Display->clear(); myString->setNum ( result, 'g', 6 ); Display->insert ( *myString ); myMonitor = 1; operand1 = result; operand2 = 0; } void FCalcMethod::allClear() { Display->clear(); } /* allClear */ void FCalcMethod::add() { #ifdef DEBUG cout << "Data:" << operand1 << " " << operand2 << " " << myOperator << endl; #endif QString s; s = Display->text(); Display->clear(); bool ok; operand1 = s.toDouble ( &ok ); myOperator = 1; } /* add() */ void FCalcMethod::subtract() { #ifdef DEBUG cout << "Data:" << operand1 << " " << operand2 << " " << myOperator << endl; #endif QString s; s = Display->text(); Display->clear(); bool ok; operand1 = s.toDouble ( &ok ); myOperator = 2; } /* subtract() */ void FCalcMethod::divide() { #ifdef DEBUG cout << "Data:" << operand1 << " " << operand2 << " " << myOperator << endl; #endif QString s; s = Display->text(); Display->clear(); bool ok; operand1 = s.toDouble ( &ok ); myOperator = 4; } /* divide() */ void FCalcMethod::modulo() { #ifdef DEBUG cout << "Data:" << operand1 << " " << operand2 << " " << myOperator << endl; #endif QString s; s = Display->text(); Display->clear(); bool ok; operand1 = s.toDouble ( &ok ); myOperator = 5; } /* modulo() */ void FCalcMethod::multiply() { #ifdef DEBUG cout << "Data:" << operand1 << " " << operand2 << " " << myOperator << endl; #endif QString s; s = Display->text(); Display->clear(); bool ok; operand1 = s.toDouble ( &ok ); myOperator = 3; } /* multiply() */ void FCalcMethod::equals() { QString s2; s2 = Display->text(); bool ok; operand2 = s2.toDouble ( &ok ); #ifdef DEBUG cout << "Data:" << operand1 << " " << operand2 << " " << myOperator << endl; #endif if ( myOperator == 1 ) result = operand1 + operand2; else if ( myOperator == 2 ) result = operand1 - operand2; else if ( myOperator == 3 ) result = operand1 * operand2; else if ( myOperator == 4 ) result = operand1 / operand2; else if ( myOperator == 5 ) { int a = (int) operand1; int b = (int) operand2; result = a % b; } #ifdef DEBUG cout << "Data (result):" << result << endl; #endif Display->clear(); myString->setNum ( result, 'f', 6 ); Display->insert ( *myString ); myMonitor = 1; operand1 = result; operand2 = 0; } /* equals() */ void FCalcMethod::backSpace() { Display->backspace(); } /* backSpace() */ // Prints a zero on the display panel. void FCalcMethod::print0() { if ( myMonitor == 0 ) Display->insert ( tr ("0") ); else if ( myMonitor == 1 ) { Display->clear(); Display->insert ( tr ("0") ); myMonitor = 0; } } /* print0() */ // Prints a "1" on the display panel. void FCalcMethod::print1() { if ( myMonitor == 0 ) Display->insert ( tr ("1") ); else { Display->clear(); Display->insert ( tr ("1") ); myMonitor = 0; } } /* print1() */ // Prints a "2" on the display panel. void FCalcMethod::print2() { if ( myMonitor == 0 ) Display->insert ( tr ("2") ); else { Display->clear(); Display->insert ( tr ("2") ); myMonitor = 0; } } /* print2() */ // Prints a "3" on the display panel. void FCalcMethod::print3() { if ( myMonitor == 0 ) Display->insert ( tr ("3") ); else { Display->clear(); Display->insert ( tr ("3") ); myMonitor = 0; } } /* print3() */ // Prints a "4" on the display panel. void FCalcMethod::print4() { if ( myMonitor == 0 ) Display->insert ( tr ("4") ); else { Display->clear(); Display->insert ( tr ("4") ); myMonitor = 0; } } /* print4() */ // Prints a "5" on the display panel. void FCalcMethod::print5() { if ( myMonitor == 0 ) Display->insert ( tr ("5") ); else { Display->clear(); Display->insert ( tr ("5") ); myMonitor = 0; } } /* print5() */ // Prints a "6" on the display panel. void FCalcMethod::print6() { if ( myMonitor == 0 ) Display->insert ( tr ("6") ); else { Display->clear(); Display->insert ( tr ("6") ); myMonitor = 0; } } /* print6() */ // Prints a "7" on the display panel. void FCalcMethod::print7() { if ( myMonitor == 0 ) Display->insert ( tr ("7") ); else { Display->clear(); Display->insert ( tr ("7") ); myMonitor = 0; } } /* print7() */ // Prints an "8" on the display panel. void FCalcMethod::print8() { if ( myMonitor == 0 ) Display->insert ( tr ("8") ); else { Display->clear(); Display->insert ( tr ("8") ); myMonitor = 0; } } /* print8() */ // Prints a "9" on the display panel. void FCalcMethod::print9() { if ( myMonitor == 0 ) Display->insert ( tr ("9") ); else { Display->clear(); Display->insert ( tr ("9") ); myMonitor = 0; } } /* print9() */ // Prints a dot if there isn't any yet. void FCalcMethod::printDot() { if ( myMonitor == 0 ) Display->insert ( tr (".") ); else { Display->clear(); Display->insert ( tr (".") ); myMonitor = 0; } } /* printDot() */ void FCalcMethod::printSign() { QString s; s = Display->text(); Display->clear(); bool ok; double d = s.toDouble ( &ok ); double dd = d * -1; myString->setNum ( dd, 'g', 6 ); Display->insert ( *myString); myMonitor = 1; } /* printSign() */ void FCalcMethod::push() { Display->clear(); } /* push() */