home *** CD-ROM | disk | FTP | other *** search
- /*
- Program to test the resourcs for the static text, edit box,
- and push button controls.
- The program uses these controls to implement a command-line
- oriented calculator application (COCA)
- */
-
- #include <owl\applicat.h>
- #include <owl\framewin.h>
- #include <owl\dialog.h>
- #include <owl\window.rh>
- #include "rwdlg4.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
-
- const MaxEditLen = 40;
-
- // declare the custom application class as
- // a subclass of TApplication
- class TWinApp : public TApplication
- {
- public:
- TWinApp() : TApplication() {}
-
- protected:
- virtual void InitMainWindow();
- };
-
- // expand the functionality of TWindow by
- // deriving class TMainWindow
- class TMainWindow : public TWindow
- {
- public:
-
- TMainWindow() : TWindow(0, 0, 0) {}
-
- protected:
- //---------------- member functions -------------------
-
- // handle Calc menu option
- void CMCalc();
-
- void CMExit()
- { Parent->SendMessage(WM_CLOSE); }
-
- // handle closing the window
- virtual BOOL CanClose();
-
- // declare the message map macro
- DECLARE_RESPONSE_TABLE(TMainWindow);
-
- };
-
- class TCalcDialog : public TDialog
- {
- public:
-
- TCalcDialog(TWindow* parent, TResId resID);
-
- protected:
-
- // math error flag
- BOOL InError;
-
- //---------------- member functions -------------------
-
- // handle the calculation
- void HandleCalcBtn();
-
- // declare the message map macro
- DECLARE_RESPONSE_TABLE(TCalcDialog);
- };
-
- DEFINE_RESPONSE_TABLE1(TMainWindow, TWindow)
- EV_COMMAND(CM_CALC, CMCalc),
- END_RESPONSE_TABLE;
-
-
- DEFINE_RESPONSE_TABLE1(TCalcDialog, TDialog)
- EV_COMMAND(IDC_CALC_BTN, HandleCalcBtn),
- END_RESPONSE_TABLE;
-
- TCalcDialog::TCalcDialog(TWindow* parent, TResId resID) :
- TWindow(0, 0, 0), TDialog(parent, resID)
- {
- // clear the InError flag
- InError = FALSE;
- }
-
- void TCalcDialog::HandleCalcBtn()
- {
- double x, y, z;
- char opStr[MaxEditLen+1];
- char s[MaxEditLen+1];
-
- // obtain the string in the Operand1 edit box
- GetDlgItemText(IDC_OPERAND1_BOX, s, MaxEditLen);
- // convert the string in the edit box
- x = atof(s);
-
- // obtain the string in the Operand2 edit box
- GetDlgItemText(IDC_OPERAND2_BOX, s, MaxEditLen);
- // convert the string in the edit box
- y = atof(s);
-
- // obtain the string in the Operator edit box
- GetDlgItemText(IDC_OPERATOR_BOX, opStr, MaxEditLen);
-
- // clear the error message box
- SetDlgItemText(IDC_ERRMSG_BOX, "");
- InError = FALSE;
-
- // determine the requested operation
- if (strcmp(opStr, "+") == 0)
- z = x + y;
- else if (strcmp(opStr, "-") == 0)
- z = x - y;
- else if (strcmp(opStr, "*") == 0)
- z = x * y;
- else if (strcmp(opStr, "/") == 0) {
- if (y != 0)
- z = x / y;
- else {
- z = 0;
- InError = TRUE;
- SetDlgItemText(IDC_ERRMSG_BOX, "Division-by-zero error");
- }
- }
- else if (strcmp(opStr, "^") == 0) {
- if (x > 0)
- z = exp(y * log(x));
- else {
- InError = TRUE;
- SetDlgItemText(IDC_ERRMSG_BOX,
- "Cannot raise the power of a negative number");
- }
- }
- else {
- InError = TRUE;
- SetDlgItemText(IDC_ERRMSG_BOX, "Invalid operator");
- }
- // display the result if no error has occurred
- if (!InError) {
- sprintf(s, "%g", z);
- SetDlgItemText(IDC_RESULT_BOX, s);
- }
- }
-
- void TMainWindow::CMCalc()
- {
- TCalcDialog* pDlg = new TCalcDialog(this, TResId(IDD_CALC_DLG));
- pDlg->Execute();
- }
-
- BOOL TMainWindow::CanClose()
- {
- return MessageBox("Want to close this application?",
- "Query", MB_YESNO | MB_ICONQUESTION) == IDYES;
- }
-
- void TWinApp::InitMainWindow()
- {
- MainWindow = new TFrameWindow(0,
- "Command-Oriented Calculator Application",
- new TMainWindow);
- // load the menu resource
- MainWindow->AssignMenu(TResId(EXITMENU));
- // enable the keyboard handler
- MainWindow->EnableKBHandler();
- }
-
- int OwlMain(int /* argc */, char** /*argv[] */)
- {
- TWinApp app;
- return app.Run();
- }
-
-
-