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

📄

📁 计算器源代码
💻
📖 第 1 页 / 共 4 页
字号:
// 计算器Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "计算器.h"
#include "计算器Dlg.h"
#include "stdio.h"
#include "math.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_CALCULATE);
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_BN_CLICKED(IDC_0, On0)
	ON_BN_CLICKED(IDC_1, On1)
	ON_BN_CLICKED(IDC_2, On2)
	ON_BN_CLICKED(IDC_3, On3)
	ON_BN_CLICKED(IDC_4, On4)
	ON_BN_CLICKED(IDC_5, On5)
	ON_BN_CLICKED(IDC_6, On6)
	ON_BN_CLICKED(IDC_7, On7)
	ON_BN_CLICKED(IDC_8, On8)
	ON_BN_CLICKED(IDC_9, On9)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_BACKSPACE, OnBackspace)
	ON_BN_CLICKED(IDC_CLEAN, OnClean)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_BN_CLICKED(IDC_DIV, OnDiv)
	ON_BN_CLICKED(IDC_EQUAL, OnEqual)
	ON_BN_CLICKED(IDC_MADD, OnMadd)
	ON_BN_CLICKED(IDC_MCLEAR, OnMclear)
	ON_BN_CLICKED(IDC_MREAD, OnMread)
	ON_BN_CLICKED(IDC_MSAVE, OnMsave)
	ON_BN_CLICKED(IDC_MUL, OnMul)
	ON_BN_CLICKED(IDC_NEG, OnNeg)
	ON_BN_CLICKED(IDC_PERCENT, OnPercent)
	ON_BN_CLICKED(IDC_POINT, OnPoint)
	ON_BN_CLICKED(IDC_RECIPROCAL, OnReciprocal)
	ON_BN_CLICKED(IDC_SQRT, OnSqrt)
	ON_BN_CLICKED(IDC_SUB, OnSub)
	ON_COMMAND(ID_COPY, OnCopy)
	ON_COMMAND(ID_PASTE, OnPaste)
	ON_WM_INITMENUPOPUP()
	ON_COMMAND(ID_STANDARD, OnStandard)
	ON_COMMAND(ID_EXTEND, OnExtend)
	ON_COMMAND(ID_HELP, OnHelp)
	ON_COMMAND(ID_ABOUT, OnAbout)
	ON_BN_CLICKED(IDC_A, OnA)
	ON_BN_CLICKED(IDC_B, OnB)
	ON_BN_CLICKED(IDC_C, OnC)
	ON_BN_CLICKED(IDC_D, OnD)
	ON_BN_CLICKED(IDC_E, OnE)
	ON_BN_CLICKED(IDC_F, OnF)
	ON_BN_CLICKED(IDC_PI, OnPi)
	ON_BN_CLICKED(IDC_AND, OnAnd)
	ON_BN_CLICKED(IDC_NOT, OnNot)
	ON_BN_CLICKED(IDC_OR, OnOr)
	ON_BN_CLICKED(IDC_XOR, OnXor)
	ON_BN_CLICKED(IDC_ARCCOS, OnArccos)
	ON_BN_CLICKED(IDC_ARCSIN, OnArcsin)
	ON_BN_CLICKED(IDC_ARCTAN, OnArctan)
	ON_BN_CLICKED(IDC_COS, OnCos)
	ON_BN_CLICKED(IDC_EXP, OnExp)
	ON_BN_CLICKED(IDC_FACTORIAL, OnFactorial)
	ON_BN_CLICKED(IDC_LG, OnLg)
	ON_BN_CLICKED(IDC_LN, OnLn)
	ON_BN_CLICKED(IDC_SIN, OnSin)
	ON_BN_CLICKED(IDC_TAN, OnTan)
	ON_BN_CLICKED(IDC_XSTEP2, OnXstep2)
	ON_BN_CLICKED(IDC_XSTEP3, OnXstep3)
	ON_BN_CLICKED(IDC_XSTEPY, OnXstepy)
	ON_BN_CLICKED(IDC_10NUM, On10num)
	ON_BN_CLICKED(IDC_16NUM, On16num)
	ON_BN_CLICKED(IDC_8NUM, On8num)
	ON_BN_CLICKED(IDC_2NUM, On2num)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

long CMyDlg::LengthOfString(const char *str)
{
	long i=0;
	while(*str++) i++;
	return i;
}

long CMyDlg::GetCharPosFromLeft(const char *str1,char chr)
{
	long pos=0;
	while(*str1)
	{
		pos++;
		if(*str1++==chr) return pos;
	}
	return pos;
}

long CMyDlg::GetUnCharPosFromRight(const char *str1,char chr)
{
	long strlen=LengthOfString(str1),pos=strlen,i;
	str1+=strlen-1;
	for(i=0;i<strlen;i++)
	{
		if(*str1--!=chr) return pos;
		pos--;
	}
	return pos;
}

void CMyDlg::DoubleToString(double sum,char str[])
{
	int sumlen=1;
	double bk_sum=sum;
	if(!sum)
	{
		str[0]=' ';
		str[1]='0';
		str[2]='.';
		str[3]=0;
		return;
	}
	if(sum<0) sum=-sum;
	while(sum>=10)
	{
		sum/=10;
		sumlen++;
	}
	sum=bk_sum;
	if(sumlen>30)
	{
		sprintf(str,"%E",sum);
		return;
	}
	sprintf(str,"%f",sum);
	str[GetUnCharPosFromRight(str,'0')]=0;
}

void CMyDlg::Delay(unsigned long ti)
{
	unsigned long oldti=GetTickCount();
	while(GetTickCount()<oldti+ti);
}

void CMyDlg::Num10ToNum2(long num10,char *num2)
{
	char i=1,j;
	long _num10;
	if(!num10)
	{
		*num2++=' ';
		*num2++='0';
		*num2=0;
		return;
	}
	if(num10<0)
	{
		num10=-num10;
		*num2++='-';
	}
	else
		*num2++=' ';
	_num10=num10;
	while(num10/=2)
		i++;
	*(num2+i)=0;
	num10=_num10;
	for(j=i-1;j>=0;j--)
	{
		*(num2+j)=num10%2+48;
		num10/=2;
	}
	*(num2+30)='.';
	*(num2+31)=0;
}

void CMyDlg::Num10ToNum8(long num10,char *num8)
{
	char i=1,j;
	long _num10;
	if(!num10)
	{
		*num8++=' ';
		*num8++='0';
		*num8=0;
		return;
	}
	if(num10<0)
	{
		num10=-num10;
		*num8++='-';
	}
	else
		*num8++=' ';
	_num10=num10;
	while(num10/=8)
		i++;
	num10=_num10;
	*(num8+i)=0;
	for(j=i-1;j>=0;j--)
	{
		*(num8+j)=num10%8+48;
		num10/=8;
	}
	*(num8+30)='.';
	*(num8+31)=0;
}

void CMyDlg::Num10ToNum16(long num10,char *num16)
{
	char i=1,j;
	long _num10;
	if(!num10)
	{
		*num16++=' ';
		*num16++='0';
		*num16=0;
		return;
	}
	if(num10<0)
	{
		num10=-num10;
		*num16++='-';
	}
	else
		*num16++=' ';
	_num10=num10;
	while(num10/=16)
		i++;
	*(num16+i)=0;
	num10=_num10;
	for(j=i-1;j>=0;j--)
	{
		*(num16+j)=num10%16>9?num10%16-10+'A':num10%16+48;
		num10/=16;
	}
	*(num16+30)='.';
	*(num16+31)=0;
}

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	// 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
	Screen.SubclassDlgItem(IDC_SCREEN,this);
	Show.SubclassDlgItem(IDC_SHOW,this);
	Group.SubclassDlgItem(IDC_GROUP,this);
	Num16.SubclassDlgItem(IDC_16NUM,this);
	Num10.SubclassDlgItem(IDC_10NUM,this);
	Num8.SubclassDlgItem(IDC_8NUM,this);
	Num2.SubclassDlgItem(IDC_2NUM,this);
	btn0.InitButton(IDC_0,this,"0",0xff0000);
	btn1.InitButton(IDC_1,this,"1",0xff0000);
	btn2.InitButton(IDC_2,this,"2",0xff0000);
	btn3.InitButton(IDC_3,this,"3",0xff0000);
	btn4.InitButton(IDC_4,this,"4",0xff0000);
	btn5.InitButton(IDC_5,this,"5",0xff0000);
	btn6.InitButton(IDC_6,this,"6",0xff0000);
	btn7.InitButton(IDC_7,this,"7",0xff0000);
	btn8.InitButton(IDC_8,this,"8",0xff0000);
	btn9.InitButton(IDC_9,this,"9",0xff0000);
	btnNeg.InitButton(IDC_NEG,this,"+/-",0xff0000);
	btnPoint.InitButton(IDC_POINT,this,".",0xff0000);
	btnAdd.InitButton(IDC_ADD,this,"+",0x0000ff);
	btnSub.InitButton(IDC_SUB,this,"-",0x0000ff);
	btnMul.InitButton(IDC_MUL,this,"*",0x0000ff);
	btnDiv.InitButton(IDC_DIV,this,"/",0x0000ff);
	btnSqrt.InitButton(IDC_SQRT,this,"Sqrt",0xff0000);
	btnPercent.InitButton(IDC_PERCENT,this,"%",0xff0000);
	btnReciprocal.InitButton(IDC_RECIPROCAL,this,"1/x",0xff0000);
	btnEqual.InitButton(IDC_EQUAL,this,"=",0x0000ff);
	btnMadd.InitButton(IDC_MADD,this,"M+",0x0000ff);
	btnMsave.InitButton(IDC_MSAVE,this,"MS",0x0000ff);
	btnMread.InitButton(IDC_MREAD,this,"MR",0x0000ff);
	btnMclear.InitButton(IDC_MCLEAR,this,"MC",0x0000ff);
	btnBackspace.InitButton(IDC_BACKSPACE,this,"Backspace",0x0000ff);
	btnClean.InitButton(IDC_CLEAN,this,"CE",0x0000ff);
	btnClear.InitButton(IDC_CLEAR,this,"C",0x0000ff);
	btnA.InitButton(IDC_A,this,"A",0xff0000);
	btnB.InitButton(IDC_B,this,"B",0xff0000);
	btnC.InitButton(IDC_C,this,"C",0xff0000);
	btnD.InitButton(IDC_D,this,"D",0xff0000);
	btnE.InitButton(IDC_E,this,"E",0xff0000);
	btnF.InitButton(IDC_F,this,"F",0xff0000);
	btnNot.InitButton(IDC_NOT,this,"Not",0xff00ff);
	btnAnd.InitButton(IDC_AND,this,"And",0xff00ff);
	btnOr.InitButton(IDC_OR,this,"Or",0xff00ff);
	btnXor.InitButton(IDC_XOR,this,"Xor",0xff00ff);
	btnxStepy.InitButton(IDC_XSTEPY,this,"x^y",0xff00ff);
	btnxStep3.InitButton(IDC_XSTEP3,this,"x^3",0xff00ff);
	btnxStep2.InitButton(IDC_XSTEP2,this,"x^2",0xff00ff);
	btnFactorial.InitButton(IDC_FACTORIAL,this,"n!",0xff00ff);
	btnPi.InitButton(IDC_PI,this,"Pi",0xff0000);
	btnLn.InitButton(IDC_LN,this,"Ln",0xff00ff);
	btnLg.InitButton(IDC_LG,this,"Lg",0xff00ff);
	btnExp.InitButton(IDC_EXP,this,"Exp",0xff00ff);
	btnAsin.InitButton(IDC_ARCSIN,this,"ASin",0xff00ff);
	btnAcos.InitButton(IDC_ARCCOS,this,"ACos",0xff00ff);
	btnAtan.InitButton(IDC_ARCTAN,this,"ATan",0xff00ff);
	btnSin.InitButton(IDC_SIN,this,"Sin",0xff00ff);
	btnCos.InitButton(IDC_COS,this,"Cos",0xff00ff);
	btnTan.InitButton(IDC_TAN,this,"Tan",0xff00ff);
	OnStandard();
	ft.CreateFont(0,0,0,0,FW_NORMAL,0,0,0,ANSI_CHARSET,OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,FIXED_PITCH | FF_MODERN,"宋体");
	Screen.SetFont(&ft);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::On0() 
{
	// TODO: Add your control notification handler code here
	ScrLen=Screen.GetWindowTextLength();
	Screen.GetWindowText(ScrText,ScrLen+1);
	if(Flag)
	{
		Flag=0;
		ScrNum=0;
		ScrText[0]=' ';
		ScrText[1]='0';
		ScrText[2]='.';
		ScrText[3]=0;
	}
	else if(ScrLen<30)
	{
		if(Fpoint)
		{
			ScrNum+=0*pow(10,GetCharPosFromLeft(ScrText,'.')-ScrLen-1);
			ScrText[ScrLen]='0';
			ScrText[ScrLen+1]=0;
		}
		else 
		{
			if(ScrNum)
			{
				ScrText[ScrLen-1]='0';
				ScrText[ScrLen]='.';
				ScrText[ScrLen+1]=0;
			}
			else
			{
				ScrText[1]='0';
			}
			ScrNum=ScrNum*10+0;
		}
	}
	Screen.SetWindowText(ScrText);
}

void CMyDlg::On1() 
{
	// TODO: Add your control notification handler code here
	ScrLen=Screen.GetWindowTextLength();
	Screen.GetWindowText(ScrText,ScrLen+1);
	if(Flag)
	{
		Flag=0;
		ScrNum=1;
		ScrText[0]=' ';
		ScrText[1]='1';
		ScrText[2]='.';
		ScrText[3]=0;
	}
	else if(ScrLen<30)
	{
		if(Fpoint)
		{
			ScrNum+=1*pow(10,GetCharPosFromLeft(ScrText,'.')-ScrLen-1);
			ScrText[ScrLen]='1';
			ScrText[ScrLen+1]=0;
		}
		else 
		{
			if(ScrNum)
			{
				ScrText[ScrLen-1]='1';
				ScrText[ScrLen]='.';
				ScrText[ScrLen+1]=0;
			}
			else
			{
				ScrText[1]='1';
			}
			switch(FNum)
			{
			case FNUM16:
				ScrNum=ScrNum*16+1;
				break;
			case FNUM10:
				ScrNum=ScrNum*10+1;
				break;
			case FNUM8:
				ScrNum=ScrNum*8+1;
				break;
			case FNUM2:
				ScrNum=ScrNum*2+1;
			}
		}
	}
	Screen.SetWindowText(ScrText);
}

void CMyDlg::On2() 
{
	// TODO: Add your control notification handler code here
	ScrLen=Screen.GetWindowTextLength();
	Screen.GetWindowText(ScrText,ScrLen+1);
	if(Flag)
	{
		Flag=0;
		ScrNum=2;
		ScrText[0]=' ';
		ScrText[1]='2';
		ScrText[2]='.';
		ScrText[3]=0;
	}
	else if(ScrLen<30)
	{
		if(Fpoint)
		{
			ScrNum+=2*pow(10,GetCharPosFromLeft(ScrText,'.')-ScrLen-1);
			ScrText[ScrLen]='2';
			ScrText[ScrLen+1]=0;
		}
		else
		{
			if(ScrNum)
			{
				ScrText[ScrLen-1]='2';
				ScrText[ScrLen]='.';
				ScrText[ScrLen+1]=0;
			}
			else
			{
				ScrText[1]='2';
			}
			switch(FNum)
			{
			case FNUM16:
				ScrNum=ScrNum*16+2;
				break;
			case FNUM10:
				ScrNum=ScrNum*10+2;
				break;
			case FNUM8:
				ScrNum=ScrNum*8+2;
			}
		}
	}
	Screen.SetWindowText(ScrText);
}

void CMyDlg::On3() 
{
	// TODO: Add your control notification handler code here
	ScrLen=Screen.GetWindowTextLength();
	Screen.GetWindowText(ScrText,ScrLen+1);
	if(Flag)
	{
		Flag=0;
		ScrNum=3;
		ScrText[0]=' ';
		ScrText[1]='3';
		ScrText[2]='.';
		ScrText[3]=0;
	}
	else if(ScrLen<30)
	{
		if(Fpoint)
		{
			ScrNum+=3*pow(10,GetCharPosFromLeft(ScrText,'.')-ScrLen-1);
			ScrText[ScrLen]='3';
			ScrText[ScrLen+1]=0;
		}
		else
		{
			if(ScrNum)
			{
				ScrText[ScrLen-1]='3';
				ScrText[ScrLen]='.';
				ScrText[ScrLen+1]=0;
			}
			else
			{
				ScrText[1]='3';
			}
			switch(FNum)
			{
			case FNUM16:
				ScrNum=ScrNum*16+3;
				break;
			case FNUM10:
				ScrNum=ScrNum*10+3;
				break;
			case FNUM8:
				ScrNum=ScrNum*8+3;
			}
		}
	}
	Screen.SetWindowText(ScrText);
}

void CMyDlg::On4() 
{
	// TODO: Add your control notification handler code here
	ScrLen=Screen.GetWindowTextLength();
	Screen.GetWindowText(ScrText,ScrLen+1);
	if(Flag)
	{
		Flag=0;
		ScrNum=4;
		ScrText[0]=' ';
		ScrText[1]='4';
		ScrText[2]='.';
		ScrText[3]=0;
	}
	else if(ScrLen<30)
	{
		if(Fpoint)
		{
			ScrNum+=4*pow(10,GetCharPosFromLeft(ScrText,'.')-ScrLen-1);
			ScrText[ScrLen]='4';
			ScrText[ScrLen+1]=0;
		}
		else
		{
			if(ScrNum)
			{
				ScrText[ScrLen-1]='4';
				ScrText[ScrLen]='.';
				ScrText[ScrLen+1]=0;
			}
			else
			{
				ScrText[1]='4';
			}
			switch(FNum)
			{
			case FNUM16:
				ScrNum=ScrNum*16+4;
				break;
			case FNUM10:
				ScrNum=ScrNum*10+4;
				break;
			case FNUM8:
				ScrNum=ScrNum*8+4;
			}
		}
	}
	Screen.SetWindowText(ScrText);
}

void CMyDlg::On5() 
{
	// TODO: Add your control notification handler code here
	ScrLen=Screen.GetWindowTextLength();
	Screen.GetWindowText(ScrText,ScrLen+1);
	if(Flag)
	{
		Flag=0;
		ScrNum=5;
		ScrText[0]=' ';
		ScrText[1]='5';
		ScrText[2]='.';
		ScrText[3]=0;
	}
	else if(ScrLen<30)
	{
		if(Fpoint)
		{
			ScrNum+=5*pow(10,GetCharPosFromLeft(ScrText,'.')-ScrLen-1);
			ScrText[ScrLen]='5';
			ScrText[ScrLen+1]=0;
		}
		else
		{
			if(ScrNum)
			{
				ScrText[ScrLen-1]='5';
				ScrText[ScrLen]='.';
				ScrText[ScrLen+1]=0;
			}
			else
			{
				ScrText[1]='5';
			}
			switch(FNum)
			{
			case FNUM16:
				ScrNum=ScrNum*16+5;
				break;
			case FNUM10:
				ScrNum=ScrNum*10+5;
				break;
			case FNUM8:
				ScrNum=ScrNum*8+5;
			}
		}
	}
	Screen.SetWindowText(ScrText);
}

void CMyDlg::On6() 
{
	// TODO: Add your control notification handler code here
	ScrLen=Screen.GetWindowTextLength();
	Screen.GetWindowText(ScrText,ScrLen+1);
	if(Flag)
	{
		Flag=0;
		ScrNum=6;
		ScrText[0]=' ';
		ScrText[1]='6';
		ScrText[2]='.';
		ScrText[3]=0;
	}
	else if(ScrLen<30)
	{
		if(Fpoint)
		{
			ScrNum+=6*pow(10,GetCharPosFromLeft(ScrText,'.')-ScrLen-1);
			ScrText[ScrLen]='6';
			ScrText[ScrLen+1]=0;
		}
		else
		{
			if(ScrNum)
			{
				ScrText[ScrLen-1]='6';

⌨️ 快捷键说明

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