?? 計算器dlg.cpp
字號:
// 計算器Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "計算器.h"
#include "計算器Dlg.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define pi 3.1415926535897932384626433832795
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
int m_ncomflag=0; //記錄鍵入運算符的次數
double m_numold=0; //記錄上次運算的結果
double m_dnum; //記錄當前的數據
BOOL m_bnext=TRUE; //記錄鍵入數據后是否鍵入過運算符
double m_mc=0; //存放記憶數據
typedef enum{bin=2,oct=8,dec=10,hex=16}syst; //進制
syst m_sys;
typedef enum{angle,rad}jiaodu;
jiaodu m_jiao;
typedef enum{add,sub,mul,dev,Mod,x_y,And,Or,Xor,Lsh}operation;
operation m_oper;
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()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_strbnum = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Text(pDX, IDC_EDITDIS, m_strbnum);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON0, OnEnternum)
ON_BN_CLICKED(IDC_C, OnC)
ON_BN_CLICKED(IDC_CE, OnCe)
ON_BN_CLICKED(IDC_eql, OnEquall)
ON_BN_CLICKED(IDC_add, OnComputer)
ON_BN_CLICKED(IDC_Int, OnInt)
ON_BN_CLICKED(IDC_asin, Onasin)
ON_BN_CLICKED(IDC_acos, Onacos)
ON_BN_CLICKED(IDC_atan, Onatan)
ON_BN_CLICKED(IDC_tanh, Ontanh)
ON_BN_CLICKED(IDC_sin, Onsin)
ON_BN_CLICKED(IDC_cos, Oncos)
ON_BN_CLICKED(IDC_tan, Ontan)
ON_BN_CLICKED(IDC_sinh, Onsinh)
ON_BN_CLICKED(IDC_cosh, Oncosh)
ON_BN_CLICKED(IDC_sqr, Onsqr)
ON_BN_CLICKED(IDC_x_2, Onx2)
ON_BN_CLICKED(IDC_x_3, Onx3)
ON_BN_CLICKED(IDC_1_x, On1x)
ON_BN_CLICKED(IDC_n_, Onn)
ON_BN_CLICKED(IDC_log, Onlog)
ON_BN_CLICKED(IDC_ln, Onln)
ON_BN_CLICKED(IDC_e_x, Onex)
ON_BN_CLICKED(IDC_pi, Onpi)
ON_BN_CLICKED(IDC_Mplus, OnMplus)
ON_BN_CLICKED(IDC_MS, OnMs)
ON_BN_CLICKED(IDC_MR, OnMr)
ON_BN_CLICKED(IDC_MC, OnMc)
ON_BN_CLICKED(IDC_plus_minus, Onplusminus)
ON_BN_CLICKED(IDC_hex, Onhex)
ON_BN_CLICKED(IDC_dec, Ondec)
ON_BN_CLICKED(IDC_oct, Onoct)
ON_BN_CLICKED(IDC_bin, Onbin)
ON_BN_CLICKED(IDC_Backspace, OnBackspace)
ON_BN_CLICKED(IDC_Not, OnNot)
ON_BN_CLICKED(IDC_angle, Onangle)
ON_BN_CLICKED(IDC_rad, Onrad)
ON_BN_CLICKED(IDC_BUTTON1, OnEnternum)
ON_BN_CLICKED(IDC_BUTTON2, OnEnternum)
ON_BN_CLICKED(IDC_BUTTON3, OnEnternum)
ON_BN_CLICKED(IDC_BUTTON4, OnEnternum)
ON_BN_CLICKED(IDC_BUTTON5, OnEnternum)
ON_BN_CLICKED(IDC_BUTTON6, OnEnternum)
ON_BN_CLICKED(IDC_BUTTON7, OnEnternum)
ON_BN_CLICKED(IDC_BUTTON8, OnEnternum)
ON_BN_CLICKED(IDC_BUTTON9, OnEnternum)
ON_BN_CLICKED(IDC_BUTTONA, OnEnternum)
ON_BN_CLICKED(IDC_BUTTONB, OnEnternum)
ON_BN_CLICKED(IDC_BUTTONC, OnEnternum)
ON_BN_CLICKED(IDC_BUTTOND, OnEnternum)
ON_BN_CLICKED(IDC_BUTTONE, OnEnternum)
ON_BN_CLICKED(IDC_BUTTONF, OnEnternum)
ON_BN_CLICKED(IDC_Dot, OnEnternum)
ON_BN_CLICKED(IDC_sub, OnComputer)
ON_BN_CLICKED(IDC_mul, OnComputer)
ON_BN_CLICKED(IDC_dev, OnComputer)
ON_BN_CLICKED(IDC_Mod, OnComputer)
ON_BN_CLICKED(IDC_Or, OnComputer)
ON_BN_CLICKED(IDC_And, OnComputer)
ON_BN_CLICKED(IDC_Xor, OnComputer)
ON_BN_CLICKED(IDC_Lsh, OnComputer)
ON_BN_CLICKED(IDC_Exp, OnExp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers
BOOL CMyDlg::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); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CheckRadioButton(IDC_bin,IDC_hex,IDC_dec);
CheckRadioButton(IDC_angle,IDC_rad,IDC_rad);
m_ncomflag=0;
m_dnum=0;
m_sys=dec;
m_jiao=rad;
m_strbnum.Format("%.12lf",m_dnum);//精度最高保留到小數點后6位
m_strbnum.TrimRight('0');//去除右邊起連續出現的0
m_strbnum.TrimRight('.');//去除右邊出現的.
UpdateData(FALSE);
GetDlgItem(IDC_BUTTONA)->EnableWindow(false);
GetDlgItem(IDC_BUTTONB)->EnableWindow(false);
GetDlgItem(IDC_BUTTONC)->EnableWindow(false);
GetDlgItem(IDC_BUTTOND)->EnableWindow(false);
GetDlgItem(IDC_BUTTONE)->EnableWindow(false);
GetDlgItem(IDC_BUTTONF)->EnableWindow(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyDlg::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 CMyDlg::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 CMyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyDlg::OnEnternum()
{
// TODO: Add your control notification handler code here
CString strnum,strold;
char ch;
int a,i;
this->GetDlgItemText(IDC_EDITDIS,strold);
this->GetFocus()->GetWindowText(strnum);
if(m_bnext)
{
this->SetDlgItemText(IDC_EDITDIS,strnum);
m_bnext=FALSE;
}
else
{
if(strold=="0"&&strnum!=".")
this->SetDlgItemText(IDC_EDITDIS,strnum);
else
this->SetDlgItemText(IDC_EDITDIS,strold+strnum);
}
UpdateData(TRUE);
if(m_sys==dec)
m_dnum=atof(m_strbnum);
else
{
m_dnum=0;
a=(int)m_sys;
for(i=0;i<m_strbnum.GetLength();i++)
{ ch=m_strbnum[i];
if(ch>='0'&&ch<='9')
m_dnum=m_dnum*a+(ch-'0');
else
if(ch>='A'&&ch<='F')
m_dnum=m_dnum*a+(ch-'A'+10);
}
}
}
double computer(double num,double m_dnum, operation m_oper)
{
int a,b,c;
double result;
switch(m_oper)
{
case add:
result=num+m_dnum;
break;
case sub:
result=num-m_dnum;
break;
case mul:
result=num*m_dnum;
break;
case dev:
if(m_dnum==0)
{AfxMessageBox("除數為零,出錯");
result=0;
exit;
}
else
result=num/m_dnum;
break;
case Mod:
if(m_dnum==0)
{AfxMessageBox("函數結果未定義,出錯");
result=0;
exit;
}
else
result=fmod(num,m_dnum);
break;
case x_y:
m_dnum=pow(num,m_dnum);
break;
case And:
a=int(m_dnum);
b=int(m_numold);
result=a&b;
break;
case Or:
a=int(m_dnum);
b=int(m_numold);
result=a|b;
break;
case Xor:
a=int(m_dnum);
b=int(m_numold);
result=a^b;
break;
case Lsh:
a=int(m_dnum);
b=int(m_numold);
result=a<<b;
break;
}
return result;
}
void CMyDlg::OnComputer()
{
// TODO: Add your control notification handler code here
CString stroper;
char ch;
int a,i;
m_ncomflag++;
m_bnext=TRUE;
if(m_ncomflag>1)
{
m_numold=computer(m_numold,m_dnum,m_oper);
this->GetFocus()->GetWindowText(stroper);
if(stroper=="+")m_oper=add;
if(stroper=="-")m_oper=sub;
if(stroper=="*")m_oper=mul;
if(stroper=="/")m_oper=dev;
if(stroper=="Mod")m_oper=Mod;
if(stroper=="x^y")m_oper=x_y;
if(stroper=="And")m_oper=And;
if(stroper=="Or")m_oper=Or;
if(stroper=="Xor")m_oper=Xor;
if(stroper=="Lsh")m_oper=Lsh;
}
else
{
this->GetFocus()->GetWindowText(stroper);
if(stroper=="+")m_oper=add;
if(stroper=="-")m_oper=sub;
if(stroper=="*")m_oper=mul;
if(stroper=="/")m_oper=dev;
if(stroper=="Mod")m_oper=Mod;
if(stroper=="x^y")m_oper=x_y;
if(stroper=="And")m_oper=And;
if(stroper=="Or")m_oper=Or;
if(stroper=="Xor")m_oper=Xor;
if(stroper=="Lsh")m_oper=Lsh;
m_numold=m_dnum;}
m_dnum=m_numold;
if(m_sys==dec)
{
m_strbnum.Format("%.12lf",m_dnum);//精度最高保留到小數點后6位
m_strbnum.TrimRight('0');//去除右邊起連續出現的0
m_strbnum.TrimRight('.');//去除右邊出現的.
UpdateData(FALSE);
m_bnext=TRUE;
}
else
{
if(m_sys==hex)
m_strbnum.Format("%X",long(m_dnum));
else
if(m_sys==oct)
m_strbnum.Format("%o",long(m_dnum));
if(m_sys==bin)
{
unsigned int num,bitMask=0x8000;//掩碼
int i;
num=(unsigned int)m_dnum;
m_strbnum="";
for (i=1;i<=16;i++)
{
m_strbnum=m_strbnum+(num&bitMask ? "1" : "0" );
bitMask >>=1;
}
}
UpdateData(FALSE);
}
}
void CMyDlg::OnEquall()
{
// TODO: Add your control notification handler code here
m_ncomflag=0;
if(!m_bnext)
{
m_numold=computer(m_numold,m_dnum,m_oper);
m_dnum=m_numold;
if(m_sys==dec)
{
m_strbnum.Format("%.12lf",m_dnum);//精度最高保留到小數點后6位
m_strbnum.TrimRight('0');//去除右邊起連續出現的0
m_strbnum.TrimRight('.');//去除右邊出現的.
UpdateData(FALSE);
m_bnext=TRUE;
}
else
{
if(m_sys==hex)
m_strbnum.Format("%X",long(m_dnum));
else
if(m_sys==oct)
m_strbnum.Format("%o",long(m_dnum));
if(m_sys==bin)
{
unsigned int num,bitMask=0x8000;//掩碼
int i;
num=(unsigned int)m_dnum;
m_strbnum="";
for (i=1;i<=16;i++)
{
m_strbnum=m_strbnum+(num&bitMask ? "1" : "0" );
bitMask >>=1;
}
}
UpdateData(FALSE);
}
}
}
void CMyDlg::OnC()
{
// TODO: Add your control notification handler code here
this->SetDlgItemText(IDC_EDITDIS,"0");
m_ncomflag=0;
m_dnum=0;
m_numold=0;
}
void CMyDlg::OnCe()
{
// TODO: Add your control notification handler code here
this->SetDlgItemText(IDC_EDITDIS,"0");
}
void CMyDlg::OnInt()
{
// TODO: Add your control notification handler code here
m_dnum=floor(m_dnum);
m_strbnum.Format("%.12lf",m_dnum);//精度最高保留到小數點后6位
m_strbnum.TrimRight('0');//去除右邊起連續出現的0
m_strbnum.TrimRight('.');//去除右邊出現的.
UpdateData(FALSE);
}
double ang2rad(double m_dnum)
{
m_dnum=pi/180*m_dnum;
return m_dnum;
}
double rad2ang(double m_dnum)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -