00001 #include "calculator.h" 00002 #include <qlineedit.h> 00003 #include <qtextedit.h> 00004 00005 00041 /* 00042 * Constructs a Calculator which is a child of 'parent', with the 00043 * name 'name' and widget flags set to 'f' 00044 */ 00045 Calculator::Calculator( QWidget* parent, const char* name, WFlags fl ) 00046 : CalculatorDlg( parent, name, fl ), enteredOperand( 0 ), enteredOperation( 0 ) 00047 { 00048 eCurrentState->setText( stateName(currentState()) ); 00049 operands.push( 0 ); 00050 showStackTop(); // display initial 0 00051 00052 eLog->setTextFormat( LogText ); 00053 eLog->setMaxLogLines( 1000 ); 00054 } 00055 00056 /* 00057 * Destroys the object and frees any allocated resources 00058 */ 00059 Calculator::~Calculator() 00060 { 00061 // no need to delete child widgets, Qt does it all for us 00062 } 00063 00064 void Calculator::showStackTop() 00065 { 00066 eResult->setText( QString::number(operands.top()) ); 00067 } 00068 00069 /* 00070 * public slot 00071 */ 00072 void Calculator::slotNumber(int n) 00073 { 00074 eLog->append( tr("Calculator::slotNumber(<b>%1</b>)").arg(n) ); 00075 enteredOperand = n; 00076 A( Number ); 00077 eCurrentState->setText( stateName(currentState()) ); 00078 } 00079 00080 /* 00081 * public slot 00082 */ 00083 void Calculator::slotPlus() 00084 { 00085 eLog->append( tr("Calculator::slotPlus()") ); 00086 if( enteredOperation ) 00087 delete enteredOperation; 00088 enteredOperation = new OpPlus( this ); 00089 A( BinaryOp ); 00090 eCurrentState->setText( stateName(currentState()) ); 00091 } 00092 00093 /* 00094 * public slot 00095 */ 00096 void Calculator::slotMinus() 00097 { 00098 eLog->append( tr( "Calculator::slotMinus()" ) ); 00099 if( enteredOperation ) 00100 delete enteredOperation; 00101 enteredOperation = new OpMinus( this ); 00102 A( BinaryOp ); 00103 eCurrentState->setText( stateName(currentState()) ); 00104 } 00105 00106 /* 00107 * public slot 00108 */ 00109 void Calculator::slotMul() 00110 { 00111 eLog->append( tr( "Calculator::slotMul()" ) ); 00112 if( enteredOperation ) 00113 delete enteredOperation; 00114 enteredOperation = new OpMul( this ); 00115 A( BinaryOp ); 00116 eCurrentState->setText( stateName(currentState()) ); 00117 } 00118 00119 /* 00120 * public slot 00121 */ 00122 void Calculator::slotDiv() 00123 { 00124 eLog->append( tr( "Calculator::slotDiv()" ) ); 00125 if( enteredOperation ) 00126 delete enteredOperation; 00127 enteredOperation = new OpDiv( this ); 00128 A( BinaryOp ); 00129 eCurrentState->setText( stateName(currentState()) ); 00130 } 00131 00132 /* 00133 * public slot 00134 */ 00135 void Calculator::slotClear() 00136 { 00137 eLog->append( tr( "Calculator::slotClear()" ) ); 00138 A( Clear ); 00139 eCurrentState->setText( stateName(currentState()) ); 00140 } 00141 00142 /* 00143 * public slot 00144 */ 00145 void Calculator::slotEqual() 00146 { 00147 eLog->append( tr( "Calculator::slotEqual()" ) ); 00148 A( ResultRequested ); 00149 eCurrentState->setText( stateName(currentState()) ); 00150 } 00151 00152 00153 void Calculator::saveNumber() 00154 { 00155 operands.push( 0 ); 00156 } 00157 00158 void Calculator::addNumber() 00159 { 00160 operands.top() = operands.top() * 10 + enteredOperand; 00161 showStackTop(); 00162 } 00163 00164 void Calculator::clear() 00165 { 00166 operands.clear(); 00167 operands.push( 0 ); 00168 operations.clear(); 00169 showStackTop(); 00170 } 00171 00172 void Calculator::saveOp() 00173 { 00174 operations.push( enteredOperation ); 00175 enteredOperation = 0; 00176 } 00177 00178 void Calculator::showResult() 00179 { 00180 Q_ASSERT( !operations.isEmpty() ); 00181 Operation* op = operations.pop(); 00182 (*op)(); 00183 delete op; 00184 showStackTop(); 00185 } 00186 00187 00188 void Calculator::OpPlus::operator()() 00189 { 00190 int n2 = calc()->popOperand(); 00191 int n1 = calc()->popOperand(); 00192 calc()->pushOperand( n1 + n2 ); 00193 } 00194 void Calculator::OpMinus::operator()() 00195 { 00196 int n2 = calc()->popOperand(); 00197 int n1 = calc()->popOperand(); 00198 calc()->pushOperand( n1 - n2 ); 00199 } 00200 void Calculator::OpMul::operator()() 00201 { 00202 int n2 = calc()->popOperand(); 00203 int n1 = calc()->popOperand(); 00204 calc()->pushOperand( n1 * n2 ); 00205 } 00206 void Calculator::OpDiv::operator()() 00207 { 00208 int n2 = calc()->popOperand(); 00209 int n1 = calc()->popOperand(); 00210 calc()->pushOperand( n1 / n2 ); 00211 }