前面的一篇日志写过的计算器,由于那个存在一些问题所以这次经过优化和美化后,暂定为最终版本。
效果图片:
下面是源码 按钮的美化 是用了上一篇日志所写的方法,并没有重新写一个NEWButton类。
// CalculatorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorDlg.h"
#include <math.h>
#include <stack>
using namespace std;
#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()
/
// CCalculatorDlg dialog
CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCalculatorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCalculatorDlg)
m_edit1 = _T("");
m_edit2 = 0.0;
m_result=0.0;
m_Count_Style=-1;
m_temp1=0.0;
m_temp2=0.0;
m_Window_right=0.0;
m_Count_type=0;
m_OFF_Click=false;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCalculatorDlg)
DDX_Text(pDX, IDC_EDIT1, m_edit1);
DDX_Text(pDX, IDC_EDIT2, m_edit2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
//{{AFX_MSG_MAP(CCalculatorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_BUTTON_EQUAL, OnButtonEqual)
ON_BN_CLICKED(IDC_BUTTON_1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON_0, OnButton0)
ON_BN_CLICKED(IDC_BUTTON_10X, OnButton10x)
ON_BN_CLICKED(IDC_BUTTON_2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON_3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON_4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON_7, OnButton7)
ON_BN_CLICKED(IDC_BUTTON_5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON_6, OnButton6)
ON_BN_CLICKED(IDC_BUTTON_8, OnButton8)
ON_BN_CLICKED(IDC_BUTTON_9, OnButton9)
ON_BN_CLICKED(IDC_BUTTON_POINT, OnButtonPoint)
ON_BN_CLICKED(IDC_BUTTON_PLUS, OnButtonPlus)
ON_BN_CLICKED(IDC_BUTTON_SUB, OnButtonSub)
ON_BN_CLICKED(IDC_BUTTON_MUL, OnButtonMul)
ON_BN_CLICKED(IDC_BUTTON_DIV, OnButtonDiv)
ON_BN_CLICKED(IDC_BUTTON_DAOSHU, OnButtonDaoshu)
ON_BN_CLICKED(IDC_BUTTON_SQRT, OnButtonSqrt)
ON_BN_CLICKED(IDC_BACKSPACE, OnBackspace)
ON_BN_CLICKED(IDC_C, OnC)
ON_BN_CLICKED(IDC_BUTTON_QUYU, OnButtonQuyu)
ON_BN_CLICKED(IDC_BUTTON_ONOFF, OnButtonOnoff)
ON_BN_CLICKED(IDC_BUTTON_Science, OnBUTTONScience)
ON_BN_CLICKED(IDC_BUTTON_SIN, OnButtonSin)
ON_BN_CLICKED(IDC_BUTTON_COS, OnButtonCos)
ON_BN_CLICKED(IDC_BUTTON_TAN, OnButtonTan)
ON_BN_CLICKED(IDC_BUTTON_SINH, OnButtonSinh)
ON_BN_CLICKED(IDC_BUTTON_COSH, OnButtonCosh)
ON_BN_CLICKED(IDC_BUTTON_TANH, OnButtonTanh)
ON_BN_CLICKED(IDC_BUTTON_INT, OnButtonInt)
ON_BN_CLICKED(IDC_BUTTON_X2, OnButtonX2)
ON_BN_CLICKED(IDC_BUTTON_X3, OnButtonX3)
ON_BN_CLICKED(IDC_BUTTON_XY, OnButtonXy)
ON_BN_CLICKED(IDC_BUTTON_LN, OnButtonLn)
ON_BN_CLICKED(IDC_BUTTON_N, OnButtonN)
ON_BN_CLICKED(IDC_BUTTON_LOG, OnButtonLog)
ON_BN_CLICKED(IDC_BUTTON_LEFT, OnButtonLeft)
ON_BN_CLICKED(IDC_BUTTON_RIGHT, OnButtonRight)
ON_BN_CLICKED(IDC_BUTTON_PorN, OnBUTTONPorN)
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CCalculatorDlg message handlers
BOOL CCalculatorDlg::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
CRect rectLarge;
CRect rectSmall;
CRect rectSeprator;
GetWindowRect(&rectLarge);
GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeprator);
m_Window_right=rectLarge.right-rectSeprator.right;
rectSmall.left=rectLarge.left;
rectSmall.bottom=rectLarge.bottom;
rectSmall.top=rectLarge.top;
rectSmall.right=rectSeprator.right;
SetWindowPos(NULL,0,0,rectSmall.Width(),rectSmall.Height(),SWP_NOMOVE | SWP_NOZORDER);
SetWindowText(_T("kay & dan's calculator"));
m_brush.CreateSolidBrush(RGB(30,144,255));
m_font.CreatePointFont(90,"华文行楷");
//设置计算初始状态为normal模式
CButton *p;
p=(CButton*)GetDlgItem(IDC_RADIO2);
p->SetCheck(1);
m_Count_Style=2;
return TRUE; // return TRUE unless you set the focus to a control
}
void CCalculatorDlg::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 CCalculatorDlg::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();
/*
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BK);
BITMAP bitmap;
bmpBackground.GetBitmap(&bitmap);
CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground);
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitmap.bmWidth,bitmap.bmWidth,SRCCOPY);
*/
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCalculatorDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCalculatorDlg::OnRadio1()
{
// TODO: Add your control notification handler code here
//计算表达式
m_Count_Style=1;
}
void CCalculatorDlg::OnRadio2()
{
// TODO: Add your control notification handler code here
m_Count_Style=2;
}
void CCalculatorDlg::OnBUTTONScience()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
CString str;
if(GetDlgItemText(IDC_BUTTON_Science,str),str=="功能增强>>")
{
SetDlgItemText(IDC_BUTTON_Science,"基本运算<<");
}
else
{
SetDlgItemText(IDC_BUTTON_Science,"功能增强>>");
}
static CRect rectLarge;
static CRect rectSmall;
if(rectLarge.IsRectEmpty())
{
GetWindowRect(&rectSmall);
rectLarge.left=rectSmall.left;
rectLarge.top=rectSmall.top;
rectLarge.right=rectSmall.right+m_Window_right;
rectLarge.bottom=rectSmall.bottom;
}
if(str=="基本运算<<")
{
SetWindowPos(NULL,0,0,rectSmall.Width(),rectSmall.Height(),SWP_NOMOVE | SWP_NOZORDER);
}
else
{
SetWindowPos(NULL,0,0,rectLarge.Width(),rectLarge.Height(),SWP_NOMOVE | SWP_NOZORDER);
}
}
void CCalculatorDlg::OnBackspace()
{
// TODO: Add your control notification handler code here
UpdateData();
char *ch=(LPTSTR)(LPCTSTR)m_edit1;
int length=m_edit1.GetLength();
ch[length-1]='\0';
m_edit1=ch;
UpdateData(FALSE);
}
void CCalculatorDlg::OnC()
{
// TODO: Add your control notification handler code here
m_edit1="";
m_edit2=0.0;
m_result=0.0;
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonOnoff()
{
// TODO: Add your control notification handler code here
CString str;
if(GetDlgItemText(IDC_BUTTON_ONOFF,str),str=="OFF")
{
SetDlgItemText(IDC_BUTTON_ONOFF,"ON");
m_OFF_Click=TRUE;
}
else
{
SetDlgItemText(IDC_BUTTON_ONOFF,"OFF");
m_OFF_Click=FALSE;
}
if(str=="OFF")
{
GetDlgItem(IDC_EDIT1)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT2)->EnableWindow(FALSE);
m_edit1="";
m_edit2=0.0;
UpdateData(FALSE);
}
else
{
GetDlgItem(IDC_EDIT1)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT2)->EnableWindow(TRUE);
}
}
void CCalculatorDlg::OnButtonPoint()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
m_edit1+='.';
UpdateData(FALSE);
}
void CCalculatorDlg::OnBUTTONPorN()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
if(m_edit1=="")
MessageBox("操作不合法");
else if(m_Count_Style==1)
MessageBox("此功能暂不支持表达式模式下的操作");
else
{
if(m_edit1[0]=='-')
{
m_edit1.Remove('-');
}
else
m_edit1="-"+m_edit1;
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton0()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
m_edit1+='0';
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton1()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
m_edit1+='1';
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton2()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
m_edit1+='2';
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton3()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
m_edit1+='3';
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton4()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
m_edit1+='4';
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton5()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
m_edit1+='5';
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton6()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
m_edit1+='6';
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton7()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
m_edit1+='7';
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton8()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
m_edit1+='8';
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton9()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
m_edit1+='9';
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonPlus()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
if(m_Count_Style==1)
{
UpdateData();
m_edit1+='+';
UpdateData(FALSE);
}
if(m_Count_Style==2)
{
m_Count_type=1;
char m_char[10];
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char,10);
m_temp1=atof(m_char);
m_edit1="";
UpdateData(FALSE);
}
}
void CCalculatorDlg::OnButtonSub()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
if(m_Count_Style==1)
{
UpdateData();
m_edit1+='-';
UpdateData(FALSE);
}
if(m_Count_Style==2)
{
m_Count_type=2;
char m_char[10];
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char,10);
m_temp1=atof(m_char);
m_edit1="";
UpdateData(FALSE);
}
}
void CCalculatorDlg::OnButtonMul()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
if(m_Count_Style==1)
{
UpdateData();
m_edit1+='*';
UpdateData(FALSE);
}
if(m_Count_Style==2)
{
m_Count_type=3;
char m_char[10];
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char,10);
m_temp1=atof(m_char);
m_edit1="";
UpdateData(FALSE);
}
}
void CCalculatorDlg::OnButtonDiv()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
if(m_Count_Style==1)
{
UpdateData();
m_edit1+='/';
UpdateData(FALSE);
}
if(m_Count_Style==2)
{
m_Count_type=4;
char m_char[10];
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char,10);
m_temp1=atof(m_char);
m_edit1="";
UpdateData(FALSE);
}
}
void CCalculatorDlg::OnButtonQuyu()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
if(m_Count_Style==2)
{
m_Count_type=5;
char m_char[10];
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char,10);
m_temp1=atof(m_char);
m_edit1="";
UpdateData(FALSE);
}
}
void CCalculatorDlg::OnButtonXy()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
m_Count_type=6;
char m_char[10];
char m_char2[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char2,10);
int temp=atoi(m_char);
if(temp==0)
{
m_temp1=atof(m_char2);
m_edit1="";
}
else
{
m_temp1=temp;
m_edit1="";
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonDaoshu()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char1[10];
char m_char2[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char1,10);
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char2,10);
double temp=atof(m_char1);
double temp2=atof(m_char2);
CString str=m_char1;
if(temp==0 && temp2==0)
MessageBox("0不能做除数");
else if(temp!=0)
{
m_edit1="1/"+str;
m_edit2=1/temp;
}
else
{
str=m_char2;
m_edit1="1/"+str;
m_edit2=1/temp2;
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonSqrt()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
char m_char2[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char2,10);
CString str=m_char;
double temp=atof(m_char);
double temp2=atof(m_char2);
if(temp2==0)
{
m_edit2=sqrt(temp);
m_edit1="sqrt("+str+")";
}
else if(temp==0 && temp2!=0)
{
m_edit2=sqrt(temp2);
str=m_char2;
m_edit1="sqrt("+str+")";
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonSin()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
CString str=m_char;
double temp=atof(m_char);
m_edit1="sin("+str+")";
m_edit2=sin(temp);
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonCos()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
CString str=m_char;
double temp=atof(m_char);
m_edit1="cos("+str+")";
m_edit2=cos(temp);
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonTan()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
CString str=m_char;
double temp=atof(m_char);
m_edit1="tan("+str+")";
m_edit2=tan(temp);
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonSinh()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
CString str=m_char;
double temp=atof(m_char);
m_edit1="sinh("+str+")";
m_edit2=sinh(temp);
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonCosh()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
CString str=m_char;
double temp=atof(m_char);
m_edit1="cosh("+str+")";
m_edit2=cosh(temp);
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonTanh()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
CString str=m_char;
double temp=atof(m_char);
m_edit1="tanh("+str+")";
m_edit2=tanh(temp);
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonInt()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
char m_char2[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char2,10);
CString str=m_char;
int temp=atoi(m_char);
int temp2=atoi(m_char2);
if(temp2==0)
{
m_edit1="Int("+str+")";
m_edit2=temp;
}
else if(temp==0)
{
str=m_char2;
m_edit1="Int("+str+")";
m_edit2=temp2;
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonX2()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
char m_char2[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char2,10);
CString str=m_char;
double temp=atof(m_char);
double temp2=atof(m_char2);
if(temp!=0)
{
m_edit1=str+"*"+str;
m_edit2=temp*temp;
}
else if(temp==0)
{
str=m_char2;
m_edit1=str+"*"+str;
m_edit2=temp2*temp2;
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonX3()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
char m_char2[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char2,10);
CString str=m_char;
double temp=atof(m_char);
double temp2=atof(m_char2);
if(temp!=0)
{
m_edit1=str+"*"+str+"*"+str;
m_edit2=temp*temp*temp;
}
else if(temp==0)
{
str=m_char2;
m_edit1=str+"*"+str+"*"+str;
m_edit2=temp2*temp2*temp2;
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButton10x()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
char m_char2[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char2,10);
CString str=m_char;
int temp=atoi(m_char);
int temp2=atoi(m_char2);
if(temp2==0)
{
m_edit1="10^"+str;
m_edit2=pow(10,temp);
}
else if(temp==0)
{
str=m_char2;
m_edit1="10^"+str;
m_edit2=pow(10,temp2);
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonLn()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
char m_char2[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char2,10);
CString str=m_char;
double temp=atof(m_char);
double temp2=atof(m_char2);
if(temp2==0 && temp<=0)
MessageBox("参数不合法");
else if(temp>0)
{
m_edit1="ln("+str+")";
m_edit2=log(temp);
}
else if(temp==0 && temp2>0)
{
str=m_char2;
m_edit1="ln("+str+")";
m_edit2=log(temp2);
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonN()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
char m_char2[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char2,10);
CString str;
int temp=atoi(m_char);
int temp2=atoi(m_char2);
if(temp>0)
{
itoa(temp,m_char,10);
str=m_char;
m_edit1=str+"!";
m_edit2=temp;
for(int i=temp-1;i>=1;i--)
m_edit2*=i;
}
else
{
itoa(temp2,m_char2,10);
str=m_char2;
m_edit1=str+"!";
m_edit2=temp2;
for(int i=temp2-1;i>=1;i--)
m_edit2*=i;
}
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonLog()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
char m_char[10];
char m_char2[10];
GetDlgItem(IDC_EDIT2)->GetWindowText(m_char,10);
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char2,10);
CString str=m_char;
double temp=atof(m_char);
double temp2=atof(m_char2);
if(temp>0)
{
m_edit1="Log10("+str+")";
m_edit2=log10(temp);
}
else if(temp2>0 && temp==0)
{
str=m_char2;
m_edit1="Log10("+str+")";
m_edit2=log10(temp2);
}
else
MessageBox("参数不合法");
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonLeft()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
UpdateData();
if(m_Count_Style==1)
m_edit1+="(";
else
MessageBox("此操作仅在表达式模式下有用");
UpdateData(FALSE);
}
void CCalculatorDlg::OnButtonRight()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;;
}
UpdateData();
if(m_Count_Style==1)
m_edit1+=")";
else
MessageBox("此操作仅在表达式模式下有用");
UpdateData(FALSE);
}
void Count(stack<char> &s1,stack<double> &s2)
{
double v1,v2,v3;
switch(s1.top())
{
case '+':
v1=s2.top();
s2.pop();
v2=s2.top();
s2.pop();
v3=v1+v2;
s2.push(v3);
break;
case '-':
v1=s2.top();
s2.pop();
v2=s2.top();
s2.pop();
v3=v2-v1;
s2.push(v3);
break;
case '*':
v1=s2.top();
s2.pop();
v2=s2.top();
s2.pop();
v3=v1*v2;
s2.push(v3);
break;
case '/':
v1=s2.top();
s2.pop();
v2=s2.top();
s2.pop();
v3=v2/v1;
s2.push(v3);
break;
}
s1.pop();
}
void CCalculatorDlg::OnButtonEqual()
{
// TODO: Add your control notification handler code here
if(m_OFF_Click)
{
MessageBox("请按ON键开启计算器");
return;
}
if(m_Count_Style==1)
{
UpdateData();
if(m_edit1=="")
{
m_result=0;
goto lp;
}
int i=0;
double v,s,f;
char *ch=(LPTSTR)(LPCTSTR)m_edit1; //将CString类型强制转换为char *类型
stack<char> s1;
stack<double> s2;
while(ch[i]!='\0')
{
s=0;
f=0.1;
switch(ch[i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
while( (ch[i]>='0') && (ch[i]<='9') )
{
s=s*10+(ch[i]-'0');
i++;
}
if(ch[i]=='.')
{
i++;
while( (ch[i]>='0') && (ch[i]<='9'))
{
s=s+(ch[i]-'0')*f;
f=f*0.1;
i++;
}
}
s2.push(s);
i--;
break;
case '+':
case '-':
if( (s1.empty()) || (s1.top()=='(') )
s1.push(ch[i]);
else
{
while( (!s1.empty()) && (s1.top()!='(') )
{
Count(s1,s2);
}
s1.push(ch[i]);
}
break;
case '*':
case '/':
if( (s1.empty()) || (s1.top()=='(') || (s1.top()=='+') || (s1.top()=='-') )
s1.push(ch[i]);
else
{
while( (!s1.empty()) && (s1.top()!='(') && (s1.top()!='+') && (s1.top()!='-') )
{
Count(s1,s2);
}
s1.push(ch[i]);
}
break;
case '(':
s1.push(ch[i]);
break;
case ')':
while( (!s1.empty()) && (s1.top()!='(') )
{
Count(s1,s2);
}
if(!s1.empty())//pop掉左括号
s1.pop();
break;
}
i++;
}
while(!s1.empty())
{
Count(s1,s2);
}
v=s2.top();
s2.pop();
m_result=v;
}
else
{
CString str1,str2;
char m_c1[10],m_c2[10];
char m_char[10];
GetDlgItem(IDC_EDIT1)->GetWindowText(m_char,10);
m_temp2=atof(m_char);
m_edit1="";
UpdateData(FALSE);
switch(m_Count_type)
{
case 1:
m_temp1+=m_temp2;
if(m_result==0)
m_result=m_temp1;
else
m_result+=m_temp2;
break;
case 2:
m_temp1-=m_temp2;
if(m_result==0)
m_result=m_temp1;
else
m_result-=m_temp2;
break;
case 3:
m_temp1*=m_temp2;
if(m_result==0)
m_result=m_temp1;
else
m_result*=m_temp2;
break;
case 4:
m_temp1/=m_temp2;
if(m_result==0)
m_result=m_temp1;
else
m_result/=m_temp2;
break;
case 5:
m_temp1=(int)m_temp1%(int)m_temp2;
if(m_result==0)
m_result=m_temp1;
else
m_result=(int)m_result%(int)m_temp2;
break;
case 6:
itoa(m_temp1,m_c1,10);
itoa(m_temp2,m_c2,10);
str1=m_c1;
str2=m_c2;
m_edit1=str1+"^"+str2;
m_result=pow(m_temp1,m_temp2);
break;
default:
m_result=m_temp2;
}
}
lp: m_edit2=m_result;
UpdateData(FALSE);
}
HBRUSH CCalculatorDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
// TODO: Return a different brush if the default is not desired
if(pWnd->GetDlgCtrlID()==IDC_EDIT1 || IDC_EDIT2)
{
if(pWnd->GetDlgCtrlID()==IDC_TEXT_1 || IDC_TEXT_2)
{
pDC->SelectObject(&m_font);
}
pDC->SetTextColor(RGB(255,215,0));
pDC->SetBkMode(TRANSPARENT);
//pDC->SetBkColor(RGB(255,55,55));
return m_brush;
}
return hbr;
}