Philippine College of Science and Technology
Nalsian, Calasiao, Pangasinan
First Semester 2016
Visual C++ Calculator
Computer Fundamentals & Programming
(MTW 4:00-6:00)
Submitted By: G5
Allan Bernard Dela Vega
Angelo Delos Santos
Mark Joseph Fernandez
John Carlo Quilantang
Jerome Laudeza
Source Code:
// VCPP_11Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "VCPP_11.h"
#include "VCPP_11Dlg.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVCPP_11Dlg dialog
CVCPP_11Dlg::CVCPP_11Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CVCPP_11Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVCPP_11Dlg)
m_strValue = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CVCPP_11Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVCPP_11Dlg)
DDX_Text(pDX, IDC_SCREEN, m_strValue);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CVCPP_11Dlg, CDialog)
//{{AFX_MSG_MAP(CVCPP_11Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_ONE, OnBtnOne)
ON_BN_CLICKED(IDC_BTN_TWO, OnBtnTwo)
ON_BN_CLICKED(IDC_BTN_THREE, OnBtnThree)
ON_BN_CLICKED(IDC_BTN_FOUR, OnBtnFour)
ON_BN_CLICKED(IDC_BTN_FIVE, OnBtnFive)
ON_BN_CLICKED(IDC_BTN_SIX, OnBtnSix)
ON_BN_CLICKED(IDC_BTN_SEVEN, OnBtnSeven)
ON_BN_CLICKED(IDC_BTN_EIGHT, OnBtnEight)
ON_BN_CLICKED(IDC_BTN_NINE, OnBtnNine)
ON_BN_CLICKED(IDC_BTN_ZERO, OnBtnZero)
ON_BN_CLICKED(IDC_BTN_POINT, OnBtnPoint)
ON_BN_CLICKED(IDC_BTN_PLUS, OnBtnPlus)
ON_COMMAND(IDR_HELP_ABOUT, OnHelpAbout)
ON_BN_CLICKED(IDC_BTN_EQUAL, OnBtnEqual)
ON_BN_CLICKED(IDC_BTN_ON, OnBtnOn)
ON_BN_CLICKED(IDC_BTN_CE, OnBtnCe)
ON_BN_CLICKED(IDC_BTN_BACKSPACE, OnBtnBackspace)
ON_COMMAND(IDR_FILE_EXIT, OnFileExit)
ON_BN_CLICKED(IDC_BTN_DIVIDE, OnBtnDivide)
ON_BN_CLICKED(IDC_BTN_MULTIPLY, OnBtnMultiply)
ON_BN_CLICKED(IDC_BTN_MINUS, OnBtnMinus)
ON_BN_CLICKED(IDC_BTN_REMAINDER, OnBtnRemainder)
ON_BN_CLICKED(IDC_BTN_PLUSMINUS, OnBtnPlusminus)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVCPP_11Dlg message handlers
BOOL CVCPP_11Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX,
strAboutMenu);
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
// Set big icon
// Set small icon
// TODO: Add extra initialization here
m_nFlag=0;
m_dOperand_1=0;
m_dOperand_2=0;
m_cOperator=NULL;
m_bFlag=false;
m_strValue="0";
UpdateData(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CVCPP_11Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CVCPP_11Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CVCPP_11Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CVCPP_11Dlg::OnBtnOne()
{
// TODO: Add your control notification handler code here
UpdateData( );
if(m_bFlag==true)
{
m_strValue="";
m_bFlag=false;
}
if(m_nFlag==1 || m_nFlag==3)
m_nFlag=2;
if(m_strValue=="0")
m_strValue="1";
else
m_strValue+="1";
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnTwo()
{
// TODO: Add your control notification handler code here
UpdateData( );
if(m_bFlag==true)
{
m_strValue="";
m_bFlag=false;
}
if(m_nFlag==1 || m_nFlag==3)
m_nFlag=2;
if(m_strValue=="0")
m_strValue="2";
else
m_strValue+="2";
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnThree()
{
// TODO: Add your control notification handler code here
UpdateData( );
if(m_bFlag==true)
{
m_strValue="";
m_bFlag=false;
}
if(m_nFlag==1 || m_nFlag==3)
m_nFlag=2;
if(m_strValue=="0")
m_strValue="3";
else
m_strValue+="3";
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnFour()
{
// TODO: Add your control notification handler code here
UpdateData( );
if(m_bFlag==true)
{
m_strValue="";
m_bFlag=false;
}
if(m_nFlag==1 || m_nFlag==3)
m_nFlag=2;
if(m_strValue=="0")
m_strValue="4";
else
m_strValue+="4";
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnFive()
{
// TODO: Add your control notification handler code here
UpdateData( );
if(m_bFlag==true)
{
m_strValue="";
m_bFlag=false;
}
if(m_nFlag==1 || m_nFlag==3)
m_nFlag=2;
if(m_strValue=="0")
m_strValue="5";
else
m_strValue+="5";
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnSix()
{
// TODO: Add your control notification handler code here
UpdateData( );
if(m_bFlag==true)
{
m_strValue="";
m_bFlag=false;
}
if(m_nFlag==1 || m_nFlag==3)
m_nFlag=2;
if(m_strValue=="0")
m_strValue="6";
else
m_strValue+="6";
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnSeven()
{
// TODO: Add your control notification handler code here
UpdateData( );
if(m_bFlag==true)
{
m_strValue="";
m_bFlag=false;
if(m_nFlag==1 || m_nFlag==3)
m_nFlag=2;
if(m_strValue=="0")
m_strValue="7";
else
m_strValue+="7";
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnEight()
{
// TODO: Add your control notification handler code here
UpdateData( );
if(m_bFlag==true)
{
m_strValue="";
m_bFlag=false;
}
if(m_nFlag==1 || m_nFlag==3)
m_nFlag=2;
if(m_strValue=="0")
m_strValue="8";
else
m_strValue+="8";
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnNine()
{
// TODO: Add your control notification handler code here
UpdateData( );
if(m_bFlag==true)
{
m_strValue="";
m_bFlag=false;
}
if(m_nFlag==1 || m_nFlag==3)
m_nFlag=2;
if(m_strValue=="0")
m_strValue="9";
else
m_strValue+="9";
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnZero()
{
// TODO: Add your control notification handler code here
UpdateData( );
if(m_bFlag==true)
{
m_strValue="";
m_bFlag=false;
}
if(m_nFlag==1 || m_nFlag==3)
m_nFlag=2;
if(m_strValue=="0")
m_strValue="0";
else
m_strValue+="0";
UpdateData(false);
void CVCPP_11Dlg::OnBtnPoint()
{
// TODO: Add your control notification handler code here
UpdateData( );
if(m_bFlag==true)
{
m_strValue="";
m_bFlag=false;
}
if(m_nFlag==1 || m_nFlag==3)
m_nFlag=2;
int length=m_strValue.GetLength();
int flag=0;
for(int count=0;count<length;count++)
{
if(m_strValue.GetAt(count)=='.')
flag++;
}
if(flag==0)
m_strValue+=".";
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnPlus()
{
// TODO: Add your control notification handler code here
UpdateData( );
m_bFlag=true;
if(m_nFlag==0 || m_nFlag==3)
{
char *Ptr;
m_dOperand_1=strtod(m_strValue,&Ptr);
m_nFlag=1;
}
else if(m_nFlag==2)
{
char *Ptr;
m_dOperand_2=strtod(m_strValue,&Ptr);
double result=(m_dOperand_1+m_dOperand_2);
char aValue[50]={NULL};
gcvt(result,25,aValue);
m_strValue=aValue;
m_dOperand_1=result;
m_nFlag=3;
}
m_cOperator='+';
UpdateData(false);
GetDlgItem(IDC_SCREEN)->SetFocus( );
}
void CVCPP_11Dlg::OnHelpAbout()
{
// TODO: Add your command handler code here
CAboutDlg dlgAbout;
dlgAbout.DoModal( );
}
void CVCPP_11Dlg::OnBtnEqual()
{
// TODO: Add your control notification handler code here
if((m_nFlag==2 || m_nFlag==3) && m_cOperator!='|')
{
UpdateData( );
char *ptr=NULL;
double temp=m_dOperand_1;
if((m_cOperator=='-' || m_cOperator=='/') && m_nFlag==3)
{
m_dOperand_1=strtod(m_strValue,&ptr);
m_dOperand_2=temp;
}
else
m_dOperand_2=strtod(m_strValue,&ptr);
double result=0;
switch(m_cOperator)
{
case '+' : result=(m_dOperand_1+m_dOperand_2);
break;
case '-' : result=(m_dOperand_1-m_dOperand_2);
break;
case '*' : result=(m_dOperand_1*m_dOperand_2);
break;
case '/' : result=(m_dOperand_1/m_dOperand_2);
break;
case '%' : result=fmod(m_dOperand_1,m_dOperand_2);
break;
}
char Buffer[50]={NULL};
gcvt(result,25,Buffer);
m_strValue=Buffer;
if((m_cOperator=='-' || m_cOperator=='/') && m_nFlag==3)
m_dOperand_1=temp;
else if(m_nFlag==2)
m_dOperand_1=m_dOperand_2;
if(m_cOperator=='%' && result==0)
m_nFlag=0;
else
m_nFlag=3;
m_bFlag=true;
UpdateData(false);
GetDlgItem(IDC_SCREEN)->SetFocus( );
}
}
void CVCPP_11Dlg::OnBtnOn()
{
// TODO: Add your control notification handler code here
m_nFlag=0;
m_bFlag=false;
m_strValue="0";
m_cOperator=NULL;
m_dOperand_1=0;
m_dOperand_2=0;
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnCe()
{
// TODO: Add your control notification handler code here
if(m_nFlag==0 || m_nFlag==2)
m_strValue="0";
UpdateData(false);
}
void CVCPP_11Dlg::OnBtnBackspace()
{
// TODO: Add your control notification handler code here
BOOL CVCPP_11Dlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam==13 || pMsg->wParam==187)
{
OnBtnEqual( );
return true;
}
else if(pMsg->wParam==107)
{
OnBtnPlus( );
return true;
}
else if(pMsg->wParam==96)
{
OnBtnZero( );
return true;
}
else if(pMsg->wParam==97)
{
OnBtnOne( );
return true;
}
else if(pMsg->wParam==98)
{
OnBtnTwo( );
return true;
}
else if(pMsg->wParam==99)
{
OnBtnThree( );
return true;
}
else if(pMsg->wParam==100)
{
OnBtnFour( );
return true;
}
else if(pMsg->wParam==101)
{
OnBtnFive( );
return true;
}
else if(pMsg->wParam==102)
{
OnBtnSix( );
return true;
}
else if(pMsg->wParam==103)
{
OnBtnSeven( );
return true;
}
else if(pMsg->wParam==104)
{
OnBtnEight( );
return true;
}
else if(pMsg->wParam==105)
{
OnBtnNine( );
return true;
}
else if(pMsg->wParam==110)
{
OnBtnPoint( );
return true;
}
else if(pMsg->wParam==106)
{
OnBtnMultiply( );
return true;
}
else if(pMsg->wParam==109)
{
OnBtnMinus( );
return true;
}
else if(pMsg->wParam==111)
{
OnBtnDivide( );
return true;
}
else if(pMsg->wParam==53)
{
OnBtnRemainder( );
return true;
else if(pMsg->wParam==120)
{
OnBtnPlusminus( );
return true;
}
else if(pMsg->wParam==8)
{
OnBtnBackspace( );
return true;
}
else if(pMsg->wParam==27)
{
OnBtnOn( );
return true;
}
else if(pMsg->wParam==46)
{
OnBtnCe( );
return true;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
void CVCPP_11Dlg::OnFileExit()
{
// TODO: Add your command handler code here
SendMessage(WM_CLOSE);
}
void CVCPP_11Dlg::OnBtnDivide()
{
// TODO: Add your control notification handler code here
UpdateData( );
m_bFlag=true;
if(m_nFlag==0 || m_nFlag==3)
{
char *Ptr;
m_dOperand_1=strtod(m_strValue,&Ptr);
m_nFlag=1;
}
else if(m_nFlag==2)
{
char *Ptr;
m_dOperand_2=strtod(m_strValue,&Ptr);
double result=(m_dOperand_2/m_dOperand_1);
char aValue[50]={NULL};
gcvt(result,25,aValue);
m_strValue=aValue;
m_dOperand_1=result;
m_nFlag=3;
}
m_cOperator='/';
UpdateData(false);
GetDlgItem(IDC_SCREEN)->SetFocus( );
}
void CVCPP_11Dlg::OnBtnMultiply()
{
// TODO: Add your control notification handler code here
UpdateData( );
m_bFlag=true;
if(m_nFlag==0 || m_nFlag==3)
{
char *Ptr;
m_dOperand_1=strtod(m_strValue,&Ptr);
m_nFlag=1;
}
else if(m_nFlag==2)
{
char *Ptr;
m_dOperand_2=strtod(m_strValue,&Ptr);
double result=(m_dOperand_1*m_dOperand_2);
char aValue[50]={NULL};
gcvt(result,25,aValue);
m_strValue=aValue;
m_dOperand_1=result;
m_nFlag=3;
}
m_cOperator='*';
UpdateData(false);
GetDlgItem(IDC_SCREEN)->SetFocus( );
}
void CVCPP_11Dlg::OnBtnMinus()
{
// TODO: Add your control notification handler code here
UpdateData( );
m_bFlag=true;
if(m_nFlag==0 || m_nFlag==3)
{
char *Ptr;
m_dOperand_1=strtod(m_strValue,&Ptr);
m_nFlag=1;
}
else if(m_nFlag==2)
{
char *Ptr;
m_dOperand_2=strtod(m_strValue,&Ptr);
double result=(m_dOperand_1-m_dOperand_2);
char aValue[50]={NULL};
gcvt(result,25,aValue);
m_strValue=aValue;
m_dOperand_1=result;
m_nFlag=3;
}
m_cOperator='-';
UpdateData(false);
GetDlgItem(IDC_SCREEN)->SetFocus( );
}
void CVCPP_11Dlg::OnBtnRemainder()
{
// TODO: Add your control notification handler code here
UpdateData( );
m_bFlag=true;
if(m_nFlag==0 || m_nFlag==3)
{
char *Ptr;
m_dOperand_1=strtod(m_strValue,&Ptr);
m_nFlag=1;
}
else if(m_nFlag==2)
{
char *Ptr;
m_dOperand_2=strtod(m_strValue,&Ptr);
double result=fmod(m_dOperand_1,m_dOperand_2);
char aValue[50]={NULL};
gcvt(result,25,aValue);
m_strValue=aValue;
m_dOperand_1=result;
if(result==0)
m_nFlag=0;
else
m_nFlag=3;
}
m_cOperator='%';
UpdateData(false);
GetDlgItem(IDC_SCREEN)->SetFocus( );
}
void CVCPP_11Dlg::OnBtnPlusminus()
{
// TODO: Add your control notification handler code here
UpdateData( );
m_bFlag=true;
char *Ptr;
m_dOperand_1=strtod(m_strValue,&Ptr);
double result=(m_dOperand_1*(-1));
char aValue[50]={NULL};
gcvt(result,25,aValue);
m_strValue=aValue;
m_nFlag=0;
m_cOperator='|';
UpdateData(false);
GetDlgItem(IDC_SCREEN)->SetFocus( );
}
Output: