⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculatordlg.cpp

📁 vc计算器 有 二进制
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// CalculatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorDlg.h"
#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define FUNCNUM 16 //函数个数
#define CONSTNUM 11 //常数个数
#define E 2.7182818284590452353
#define PI 3.1415926535897932384

/////////////////////////////////////////////////////////////////////////////
// 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_strExp = _T("");
	m_strRes = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
	m_BKbitmap.LoadBitmap(IDB_BITMAP1);
	m_bDegree=1;
	//初始化函数和常数
	opt[0]="e^";
	opt[1]="ch";
	opt[2]="sqr";
	opt[3]="sh";
	opt[4]="log";
	opt[5]="cos";
	opt[6]="sin";
	opt[7]="tan";
	opt[8]="cot";
	opt[9]="ln";
	opt[10]="abs";
	opt[11]="at";
	opt[12]="as";
	opt[13]="ac";
	opt[14]="th";
	opt[15]="exp";
	opt2[0]='^';
	opt2[1]='/';
	opt2[2]='*';
	opt2[3]='%';
	opt2[4]='+';
	opt2[5]='-';
	m_strConValue[0]="3.1415926535897932384";	m_strConName[0]="PI";
	m_strConValue[1]="9.80665";					m_strConName[1]="GG";
	m_strConValue[2]="(8.988*10^9)";			m_strConName[2]="EF";
	m_strConValue[3]="0.02241383";				m_strConName[3]="VM";
	m_strConValue[4]="(1.380662*10^(-23))";		m_strConName[4]="BC";
	m_strConValue[5]="8.3144";					m_strConName[5]="MG";
	m_strConValue[6]="(6.6720*10^(-11))";		m_strConName[6]="GR";
	m_strConValue[7]="(6.022045*10^23)";		m_strConName[7]="NA";
	m_strConValue[8]="(2.99792458*10^8)";		m_strConName[8]="LS";
	m_strConValue[9]="(8.854187818*10^(-12))";	m_strConName[9]="DC";
	m_strConValue[10]="2.7182818284590452353";	m_strConName[10]="E";
}

void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalculatorDlg)
	DDX_Control(pDX, IDC_RADIO_STANDARD, m_standard);
	DDX_Control(pDX, IDOK, m_ok);
	DDX_Control(pDX, IDC_EDIT_OUTPUT, m_output);
	DDX_Control(pDX, IDC_BUTTON_XO, m_xo);
	DDX_Control(pDX, IDC_BUTTON_XH, m_xh);
	DDX_Control(pDX, IDC_BUTTON_XB, m_xb);
	DDX_Control(pDX, IDC_BUTTON_TH, m_th);
	DDX_Control(pDX, IDC_BUTTON_TAN1, m_tan1);
	DDX_Control(pDX, IDC_BUTTON_TAN, m_tan);
	DDX_Control(pDX, IDC_BUTTON_SQRT, m_sqrt);
	DDX_Control(pDX, IDC_BUTTON_SQR, m_sqr);
	DDX_Control(pDX, IDC_BUTTON_SPACE, m_space);
	DDX_Control(pDX, IDC_BUTTON_SIN1, m_sin1);
	DDX_Control(pDX, IDC_BUTTON_SIN, m_sin);
	DDX_Control(pDX, IDC_BUTTON_SH, m_sh);
	DDX_Control(pDX, IDC_BUTTON_RPAREN, m_rparen);
	DDX_Control(pDX, IDC_BUTTON_PLUS, m_plus);
	DDX_Control(pDX, IDC_BUTTON_OPERATE, m_operator);
	DDX_Control(pDX, IDC_BUTTON_MUL, m_mul);
	DDX_Control(pDX, IDC_BUTTON_MOD, m_mod);
	DDX_Control(pDX, IDC_BUTTON_MINUS, m_minus);
	DDX_Control(pDX, IDC_BUTTON_LPAREN, m_lparen);
	DDX_Control(pDX, IDC_BUTTON_LOG, m_log);
	DDX_Control(pDX, IDC_BUTTON_LN, m_ln);
	DDX_Control(pDX, IDC_BUTTON_HISTORY, m_history);
	DDX_Control(pDX, IDC_BUTTON_F, m_hf);
	DDX_Control(pDX, IDC_BUTTON_EXP, m_exp);
	DDX_Control(pDX, IDC_BUTTON_ESQR, m_esqr);
	DDX_Control(pDX, IDC_BUTTON_E, m_he);
	DDX_Control(pDX, IDC_BUTTON_DOT, m_dot);
	DDX_Control(pDX, IDC_BUTTON_DIVID, m_divid);
	DDX_Control(pDX, IDC_BUTTON_D, m_hd);
	DDX_Control(pDX, IDC_BUTTON_COT, m_cot);
	DDX_Control(pDX, IDC_BUTTON_COS1, m_cos1);
	DDX_Control(pDX, IDC_BUTTON_COS, m_cos);
	DDX_Control(pDX, IDC_BUTTON_CONST, m_const);
	DDX_Control(pDX, IDC_BUTTON_CLEAR, m_clear);
	DDX_Control(pDX, IDC_BUTTON_CH, m_ch);
	DDX_Control(pDX, IDC_BUTTON_C, m_hc);
	DDX_Control(pDX, IDC_BUTTON_BACK, m_back);
	DDX_Control(pDX, IDC_BUTTON_B, m_hb);
	DDX_Control(pDX, IDC_BUTTON_ABS, m_abs);
	DDX_Control(pDX, IDC_BUTTON_A, m_ha);
	DDX_Control(pDX, IDC_BUTTON_9, m_nine);
	DDX_Control(pDX, IDC_BUTTON_8, m_eight);
	DDX_Control(pDX, IDC_BUTTON_7, m_seven);
	DDX_Control(pDX, IDC_BUTTON_6, m_six);
	DDX_Control(pDX, IDC_BUTTON_5, m_five);
	DDX_Control(pDX, IDC_BUTTON_4, m_four);
	DDX_Control(pDX, IDC_BUTTON_3, m_three);
	DDX_Control(pDX, IDC_BUTTON_1, m_one);
	DDX_Control(pDX, IDC_BUTTON_2, m_two);
	DDX_Control(pDX, IDC_RADIO_DEC, m_radDec);
	DDX_Control(pDX, IDC_RADIO_DEGREE, m_degree);
	DDX_Control(pDX, IDC_EDIT_INPUT, m_strExpc);
	DDX_Control(pDX, IDC_BUTTON_0, m_zero);
	DDX_Text(pDX, IDC_EDIT_INPUT, m_strExp);
	DDX_Text(pDX, IDC_EDIT_OUTPUT, m_strRes);
	//}}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_BUTTON_OPERATE, OnButtonOperate)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
	ON_BN_CLICKED(IDC_BUTTON_CONST, OnButtonConst)
	ON_BN_CLICKED(IDC_RADIO_DEC, OnRadioDec)
	ON_BN_CLICKED(IDC_RADIO_HEX, OnRadioHex)
	ON_BN_CLICKED(IDC_RADIO_OCT, OnRadioOct)
	ON_BN_CLICKED(IDC_RADIO_BIN, OnRadioBin)
	ON_BN_CLICKED(IDC_BUTTON_7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON_0, OnButton0)
	ON_BN_CLICKED(IDC_BUTTON_1, OnButton1)
	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_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_A, OnButtonA)
	ON_BN_CLICKED(IDC_BUTTON_ABS, OnButtonAbs)
	ON_BN_CLICKED(IDC_BUTTON_B, OnButtonB)
	ON_BN_CLICKED(IDC_BUTTON_BACK, OnButtonBack)
	ON_BN_CLICKED(IDC_BUTTON_C, OnButtonC)
	ON_BN_CLICKED(IDC_BUTTON_CH, OnButtonCh)
	ON_BN_CLICKED(IDC_BUTTON_COS, OnButtonCos)
	ON_BN_CLICKED(IDC_BUTTON_COS1, OnButtonCos1)
	ON_BN_CLICKED(IDC_BUTTON_COT, OnButtonCot)
	ON_BN_CLICKED(IDC_BUTTON_D, OnButtonD)
	ON_BN_CLICKED(IDC_BUTTON_DIVID, OnButtonDivid)
	ON_BN_CLICKED(IDC_BUTTON_DOT, OnButtonDot)
	ON_BN_CLICKED(IDC_BUTTON_E, OnButtonE)
	ON_BN_CLICKED(IDC_BUTTON_ESQR, OnButtonEsqr)
	ON_BN_CLICKED(IDC_BUTTON_EXP, OnButtonExp)
	ON_BN_CLICKED(IDC_BUTTON_F, OnButtonF)
	ON_BN_CLICKED(IDC_BUTTON_LN, OnButtonLn)
	ON_BN_CLICKED(IDC_BUTTON_LOG, OnButtonLog)
	ON_BN_CLICKED(IDC_BUTTON_LPAREN, OnButtonLparen)
	ON_BN_CLICKED(IDC_BUTTON_MINUS, OnButtonMinus)
	ON_BN_CLICKED(IDC_BUTTON_MOD, OnButtonMod)
	ON_BN_CLICKED(IDC_BUTTON_MUL, OnButtonMul)
	ON_BN_CLICKED(IDC_BUTTON_PLUS, OnButtonPlus)
	ON_BN_CLICKED(IDC_BUTTON_RPAREN, OnButtonRparen)
	ON_BN_CLICKED(IDC_BUTTON_SH, OnButtonSh)
	ON_BN_CLICKED(IDC_BUTTON_SIN, OnButtonSin)
	ON_BN_CLICKED(IDC_BUTTON_SIN1, OnButtonSin1)
	ON_BN_CLICKED(IDC_BUTTON_SPACE, OnButtonSpace)
	ON_BN_CLICKED(IDC_BUTTON_SQR, OnButtonSqr)
	ON_BN_CLICKED(IDC_BUTTON_SQRT, OnButtonSqrt)
	ON_BN_CLICKED(IDC_BUTTON_TAN, OnButtonTan)
	ON_BN_CLICKED(IDC_BUTTON_TAN1, OnButtonTan1)
	ON_BN_CLICKED(IDC_BUTTON_TH, OnButtonTh)
	ON_BN_CLICKED(IDC_BUTTON_XB, OnButtonXb)
	ON_BN_CLICKED(IDC_BUTTON_XH, OnButtonXh)
	ON_BN_CLICKED(IDC_BUTTON_XO, OnButtonXo)
	ON_BN_CLICKED(IDC_BUTTON_HISTORY, OnButtonHistory)
	ON_COMMAND(ID_MENU_C, OnMenuC)
	ON_COMMAND(ID_MENU_E, OnMenuE)
	ON_COMMAND(ID_MENU_E0, OnMenuE0)
	ON_COMMAND(ID_MENU_G, OnMenuG)
	ON_COMMAND(ID_MENU_K, OnMenuK)
	ON_COMMAND(ID_MENU_KE, OnMenuKe)
	ON_COMMAND(ID_MENU_NA, OnMenuNa)
	ON_COMMAND(ID_MENU_PI, OnMenuPi)
	ON_COMMAND(ID_MENU_R, OnMenuR)
	ON_COMMAND(ID_MENU_VM, OnMenuVm)
	ON_WM_SETCURSOR()
	ON_BN_CLICKED(IDC_RADIO_STANDARD, OnRadioStandard)
	ON_BN_CLICKED(IDC_RADIO_SCIENTFIC, OnRadioScientfic)
	//}}AFX_MSG_MAP
	ON_COMMAND_RANGE(IMENU_ITEM_ID,IMENU_ITEM_ID+100,OnMenuSelect)
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
	m_menu2.CreatePopupMenu();
	m_degree.SetCheck(1);
	m_radDec.SetCheck(1);
	m_standard.SetCheck(1);

	GetWindowRect(&largeCrect);    //取得科学型界面的大小
	GetDlgItem(IDC_STATIC4)->GetWindowRect(&separater);  //取得分界线的位置
	smallCrect.top=largeCrect.top;
	smallCrect.left=largeCrect.left;
	smallCrect.right=separater.right;
	smallCrect.bottom=largeCrect.bottom;  //设置标准型界面的大小
	SetWindowPos(NULL,0,0,smallCrect.Width(),smallCrect.Height(),
			SWP_NOMOVE | SWP_NOZORDER);   //把计算器界面设置为标准型的
	
	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() 
{
	CPaintDC dc(this);
	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
	{
		BITMAP bm;
		CRect rect;
		CDC dcMem;
		m_BKbitmap.GetBitmap (&bm);
		GetClientRect(&rect);
		dcMem.CreateCompatibleDC (&dc);
		CBitmap *oldbitmap=dcMem.SelectObject (&m_BKbitmap);
		dc.BitBlt (0,0,bm.bmWidth ,bm.bmHeight ,&dcMem,0,0,SRCCOPY);
		dcMem.SelectObject(oldbitmap);
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCalculatorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CCalculatorDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	
	return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

void CCalculatorDlg::Paster(CString strSign, BOOL bIsSingle)
{
	UpdateData(1);
	CString str;
	m_nlen=m_strExp.GetLength();
	m_strExpc.GetSel(m_nStart,m_nEnd);
	str=m_strExp.Mid(m_nEnd,m_nlen-m_nEnd);
	m_strExp=m_strExp.Left(m_nStart)+strSign;
	m_nlen=m_strExp.GetLength();
	if(bIsSingle) m_nlen-=2;
	m_strExp+=str;
	UpdateData(0);
	m_strExpc.SetFocus();
	m_strExpc.SetSel(m_nlen,m_nlen);
}

bool CCalculatorDlg::IsDigital(CString str)
{
	while(!str.IsEmpty() && str.GetAt(0)=='-')
		str.Delete(0);
	for(int i=0;i<FUNCNUM;i++)
		if(str.Find(opt[i])!=-1)
			return 0;
	for(i=0;i<6;i++)
		if(str.Find(opt2[i])!=-1)
			return 0;
	return 1;
}

CString CCalculatorDlg::NtoS(double d)
{
	int  decimal, sign;
	char *buffer;
	buffer = _ecvt( d, 16, &decimal, &sign );
	CString str=buffer;
	if(decimal>=0 && decimal<=16) str.Insert(decimal,".");
	else if(decimal>16)
	{
		for(int i=str.GetLength();i<decimal;i++) str+="0";
		str+=".0";
	}
	else 
	{
		for(int i=0;i<-decimal;i++)	str.Insert(0,"0");
		str.Insert(0,".");
	}
	if(sign==1) str.Insert(0,"-");
	return str;
}

double CCalculatorDlg::StoN(CString str)
{
	char   *stopstring;
	double x;
    x = strtod( str.GetBuffer(0), &stopstring );
	m_strTmp=stopstring;
	return x;
}

CString CCalculatorDlg::TwoE(CString strExp)
{
	double x2,y,x1=StoN(strExp);
	strExp=m_strTmp;
	char op=strExp.GetAt(0);
	strExp.Delete(0);
	x2=StoN(strExp);
	if(m_strTmp!="") return "ERROR_"+m_strTmp+"未知符号_";
	if(op=='+') {y=x1+x2;}
	else if(op=='*') {y=x1*x2;}
	else if(op=='-') {y=x1-x2;}
	else if(op=='^') {y=pow(x1,x2);}
	else if(op=='/') {y=x1/x2;}
	else if(op=='%') {y=fmod(x1,x2);}
	else return "ERROR_"+m_strTmp+"未知运算符_";
	return NtoS(y);
}

CString CCalculatorDlg::SingleE(CString op,double dx)
{
	if(op=="ln") 
	{
		return NtoS(log(dx));
	}
	else if(op=="log") 
	{
		return NtoS(log10(dx));
	}
	else if(op=="sqr") 
	{
		return NtoS(sqrt(dx));
	}
	else if(op=="e^") 
	{
		return NtoS(exp(dx));
	}
	else if(op=="abs") 
	{
		return NtoS(fabs(dx));
	}
	else if(op=="ac") 
	{
		if(m_bDegree)
			return NtoS(180*acos(dx)/PI);  //以角度返回
		else
			return NtoS(acos(dx));        //以度数返回
	}
	else if(op=="as") 
	{
		if(m_bDegree)
			return NtoS(180*asin(dx)/PI);
		else
			return NtoS(asin(dx));
	}
	else if(op=="at") 
	{
		if(m_bDegree)
			return NtoS(180*atan(dx)/PI);
		else
			return NtoS(atan(dx));
	}
	else if(op=="exp") 
	{
		return NtoS(pow(10,dx));
	}

	if(m_bDegree)
		dx=dx*PI/180;
	if(op=="tan") 
	{
		return NtoS(tan(dx));
	}
	else if(op=="sin") 
	{	
		return NtoS(sin(dx));			
	}
	else if(op=="cos") 
	{
		return NtoS(cos(dx));		
	}
	else if(op=="cot") 
	{
		return NtoS(1/tan(dx));		
	}
	else if(op=="sh") 
	{
		return NtoS(sinh(dx));				
	}	
	else if(op=="ch") 
	{
		return NtoS(cosh(dx));
	}
	else if(op=="th") 
	{
		return NtoS(sinh(dx)/cosh(dx));
	}
	return "ERROR"+op+"_未知函数_";
}

int CCalculatorDlg::LocateLBra(CString strExp)
{
	int len=strExp.GetLength();
	int pos=-1;
	for(int i=0;i<len;i++)
	{
		if(pos>=0 && strExp.GetAt(i)==')')
			break;
		if(strExp.GetAt(i)=='(')
		{
			pos=i;
		}
	}
	return pos;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -