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

📄 calculatordlg.cpp

📁 EVC下计算器
💻 CPP
字号:
// calculatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "calculator.h"
#include "calculatorDlg.h"
#include "math.h"
#include "stdlib.h"
#include "afxwin.h"

bool setXiaoShu;
bool setNew;
int sjflag;
int xiaoshu;
int innum; 
int a;
double l;
double n;
double w;
char operate;

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

/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg dialog

CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCalculatorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCalculatorDlg)
	//}}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);
	DDX_Text(pDX, IDC_EDIT1, n);
	//{{AFX_DATA_MAP(CCalculatorDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
	//{{AFX_MSG_MAP(CCalculatorDlg)
	ON_BN_CLICKED(IDC_BUTTON4, OnZero)
	ON_BN_CLICKED(IDC_BUTTON9, OnOne)
	ON_BN_CLICKED(IDC_BUTTON12, OnTwo)
	ON_BN_CLICKED(IDC_BUTTON11, OnThree)
	ON_BN_CLICKED(IDC_BUTTON14, OnFour)
	ON_BN_CLICKED(IDC_BUTTON17, OnFive)
	ON_BN_CLICKED(IDC_BUTTON16, OnSix)
	ON_BN_CLICKED(IDC_BUTTON19, OnSeven)
	ON_BN_CLICKED(IDC_BUTTON22, OnEight)
	ON_BN_CLICKED(IDC_BUTTON21, OnNine)
	ON_BN_CLICKED(IDC_BUTTON5, OnAdd)
	ON_BN_CLICKED(IDC_BUTTON10, OnSub)
	ON_BN_CLICKED(IDC_BUTTON15, OnMul)
	ON_BN_CLICKED(IDC_BUTTON20, OnDiv)
	ON_BN_CLICKED(IDC_BUTTON28, OnXfangY)
	ON_BN_CLICKED(IDC_BUTTON13, OnDaoshu)
	ON_BN_CLICKED(IDC_BUTTON23, OnSqrt)
	ON_BN_CLICKED(IDC_BUTTON18, OnSin)
	ON_BN_CLICKED(IDC_BUTTON24, OnCos)
	ON_BN_CLICKED(IDC_BUTTON27, OnTan)
	ON_BN_CLICKED(IDC_BUTTON25, OnLn)
	ON_BN_CLICKED(IDC_BUTTON30, OnLog)
	ON_BN_CLICKED(IDC_BUTTON26, OnNjie)
	ON_BN_CLICKED(IDC_BUTTON8, OnDengyu)
	ON_BN_CLICKED(IDC_BUTTON3, OnClean)
	ON_BN_CLICKED(IDC_BUTTON7, OnZhengfu)
	ON_BN_CLICKED(IDC_BUTTON6, OnPoint)
	ON_BN_CLICKED(IDC_RADIO2, OnStandard)
	ON_BN_CLICKED(IDC_RADIO1, OnScience)
	ON_BN_CLICKED(IDC_BUTTON29, OnEXP)
	ON_BN_CLICKED(IDC_RADIO3, OnAngle)
	ON_BN_CLICKED(IDC_RADIO4, OnRadian)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg message handlers

BOOL CCalculatorDlg::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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	sjflag = 6;
	CCalculatorDlg::OnStandard();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCalculatorDlg::OnZero() 
{
	// TODO: Add your control notification handler code here
	if (setNew) CCalculatorDlg::OnClean(); else Invalidate(); 
	innum=0;
	CCalculatorDlg::inputNum(innum);
	UpdateData(false);
}

void CCalculatorDlg::OnOne() 
{
	// TODO: Add your control notification handler code here
	if (setNew) CCalculatorDlg::OnClean(); else Invalidate();
	innum=1;
	CCalculatorDlg::inputNum(innum);
	UpdateData(false);
}

void CCalculatorDlg::OnTwo() 
{
	// TODO: Add your control notification handler code here
	if (setNew) CCalculatorDlg::OnClean(); else Invalidate();
	innum=2;
	CCalculatorDlg::inputNum(innum);
	UpdateData(false);
}

void CCalculatorDlg::OnThree() 
{
	// TODO: Add your control notification handler code here
	if (setNew) CCalculatorDlg::OnClean(); else Invalidate();
	innum=3;
	CCalculatorDlg::inputNum(innum);
	UpdateData(false);
}

void CCalculatorDlg::OnFour() 
{
	// TODO: Add your control notification handler code here
	if (setNew) CCalculatorDlg::OnClean(); else Invalidate();
	innum=4;
	CCalculatorDlg::inputNum(innum);
	UpdateData(false);
}

void CCalculatorDlg::OnFive() 
{
	// TODO: Add your control notification handler code here
	if (setNew) CCalculatorDlg::OnClean(); else Invalidate();
	innum=5;
	CCalculatorDlg::inputNum(innum);
	UpdateData(false);
}

void CCalculatorDlg::OnSix() 
{
	// TODO: Add your control notification handler code here
	if (setNew) CCalculatorDlg::OnClean(); else Invalidate();
	innum=6;
	CCalculatorDlg::inputNum(innum);
	UpdateData(false);
}

void CCalculatorDlg::OnSeven() 
{
	// TODO: Add your control notification handler code here
	if (setNew) CCalculatorDlg::OnClean(); else Invalidate();
	innum=7;
	CCalculatorDlg::inputNum(innum);
	UpdateData(false);
}

void CCalculatorDlg::OnEight() 
{
	// TODO: Add your control notification handler code here
	if (setNew) CCalculatorDlg::OnClean(); else Invalidate();
	innum=8;
	CCalculatorDlg::inputNum(innum);
	UpdateData(false);
}

void CCalculatorDlg::OnNine() 
{
	// TODO: Add your control notification handler code here
	if (setNew) CCalculatorDlg::OnClean(); else Invalidate();
	innum=9;
	CCalculatorDlg::inputNum(innum);
	UpdateData(false);
}
////////////////////////////////////////////////////////////////////////////

void CCalculatorDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	CCalculatorDlg::OnDengyu();     
	l=n;  
	CCalculatorDlg::setEmpty();  
	setNew=false;  
	operate='+';  
}

void CCalculatorDlg::OnSub() 
{
	// TODO: Add your control notification handler code here
	CCalculatorDlg::OnDengyu();
	l=n;
	CCalculatorDlg::setEmpty();
	setNew=false;
	operate='-';
}

void CCalculatorDlg::OnMul() 
{
	// TODO: Add your control notification handler code here
	CCalculatorDlg::OnDengyu();
	l=n;
	CCalculatorDlg::setEmpty();
	setNew=false;
	operate='*';
}

void CCalculatorDlg::OnDiv() 
{
	// TODO: Add your control notification handler code here
	CCalculatorDlg::OnDengyu();
	l=n;
	CCalculatorDlg::setEmpty();
	setNew=false;
	operate='/';
}

void CCalculatorDlg::OnXfangY() 
{
	// TODO: Add your control notification handler code here
	CCalculatorDlg::OnDengyu();
	l=n;
	CCalculatorDlg::setEmpty();
	operate='y';
	setNew=false;
}


void CCalculatorDlg::OnEXP() 
{
	// TODO: Add your control notification handler code here
	CCalculatorDlg::OnDengyu();
	l=n;
	CCalculatorDlg::setEmpty();
	operate='e';
	setNew=false;
}
/////////////////////////////////////////////////////////////////

void CCalculatorDlg::OnDaoshu() 
{
	// TODO: Add your control notification handler code here
	operate='x';
	CCalculatorDlg::operation(operate); 
	if(n != 0)
		UpdateData(false);
	operate='0';
	setNew=true; 
}

void CCalculatorDlg::OnSqrt() 
{
	// TODO: Add your control notification handler code here
	operate='q';
	CCalculatorDlg::operation(operate);
	if(n >= 0)
		UpdateData(false);
	operate='0';
	setNew=true;
}

void CCalculatorDlg::OnSin() 
{
	// TODO: Add your control notification handler code here
	operate='s';
	CCalculatorDlg::operation(operate);
	UpdateData(false);
	operate='0';
	setNew=true;
}

void CCalculatorDlg::OnCos() 
{
	// TODO: Add your control notification handler code here
	operate='c';
	CCalculatorDlg::operation(operate);
	UpdateData(false);
	operate='0';
	setNew=true;
}

void CCalculatorDlg::OnTan() 
{
	// TODO: Add your control notification handler code here
	operate='t';
	CCalculatorDlg::operation(operate);
	UpdateData(false);
	operate='0';
	setNew=true;
}

void CCalculatorDlg::OnLn() 
{
	// TODO: Add your control notification handler code here
	operate='l';
	CCalculatorDlg::operation(operate);
	if(n > 0)
		UpdateData(false);
	operate='0';
	setNew=true;
}

void CCalculatorDlg::OnLog() 
{
	// TODO: Add your control notification handler code here
	operate='g';
	CCalculatorDlg::operation(operate);
	if(n > 0)
		UpdateData(false);
	operate='0';
	setNew=true;
}

void CCalculatorDlg::OnNjie() 
{
	// TODO: Add your control notification handler code here
	operate='n';
	CCalculatorDlg::operation(operate);
	if(n>=0)
		UpdateData(false);
	operate='0';
	setNew=true;
}
//////////////////////////////////////////////////

void CCalculatorDlg::OnDengyu() 
{
	// TODO: Add your control notification handler code here
	CCalculatorDlg::operation(operate); 
	operate='0';  
	setNew=true;
	if(n!=0)
		UpdateData(false);
}

void CCalculatorDlg::OnClean() 
{
	// TODO: Add your control notification handler code here
	CCalculatorDlg::setEmpty();
	l=0;
	operate='0';
	UpdateData(false);
	Invalidate();
}

void CCalculatorDlg::OnZhengfu() 
{
	// TODO: Add your control notification handler code here
	n=-n;
	UpdateData(false);
	Invalidate();
}

void CCalculatorDlg::OnPoint() 
{
	// TODO: Add your control notification handler code here
	setXiaoShu=true; 
	Invalidate();
}

void CCalculatorDlg::setEmpty(void) 
{
	xiaoshu=0; 
	setXiaoShu=false;  
	n=0;  
}

void CCalculatorDlg::inputNum(int i)    
{
	if (n==fabs(n))  
	{
		if (!setXiaoShu) 
			n=10*n+i;
		else 
		{
			xiaoshu=xiaoshu+1;
			n=n+pow(10.0,-xiaoshu)*i; 
		}
	}
	else   
	{		
		if (!setXiaoShu)  
		{
			n=-10*n+i; 
			n=-n;  
		}
		else  
		{
			xiaoshu=xiaoshu+1;
			n=-n+pow(10.0,-xiaoshu)*i;
			n=-n;
		}
	}
	setNew=false;
}

CCalculatorDlg::fact(double n)
{
	int a = int(n);										
	int answer;
    if (a==1||a==0)
		return (1);
	answer=fact(a-1) * a;
    return(answer);
}

double CCalculatorDlg::divd(double m,double n)
{
	double an;
	if(m!=0&&n!=0)
		an = m/n;
	else 
		an = 0;
	return (an);
}

double CCalculatorDlg::power(double m,double n)
{
	int b = int(n);
	double ans;
	if (b%2 == 0)
		ans = pow(-l,n);
	else ans = -pow(-l,n);
	return (ans);
}

void CCalculatorDlg::operation(char ope)  
{
	switch (ope)
	{
	case '+':{n=l+n;}break;
	case '-':{n=l-n;}break;
	case '*':{n=l*n;}break;
	case '/':
			if(n == 0)
			{
				this->dozero();
			}else
			{n=divd(l,n);}break;
	case 'e':{n=l*pow(10,n);}break;
	case 'x':if (n != 0)
			{
				n=1/n;
			}
			else {this->dodaoshu();}break;
	case 'q':if (n < 0)
			{
				this->doSqrt();
			}
			else {if (n>0) n=sqrt(n); else n=0;}break;
	case 's':if(sjflag != 6)
			 {
				 if (n != 0) {n=sin(double (n*3.1415926535897932384626433832795/180));}
				 else n = sin(0);}
			 else n=sin(n); break;
	case 'c':if(sjflag != 6)
			 {
				 if (n != 0) {n=cos(double (n*3.1415926535897932384626433832795/180));}
				 else n = cos(0);}
			 else n=cos(n); break;
	case 't':if(sjflag != 6)
			 {
				 if (n != 0) {n=tan(double (n*3.1415926535897932384626433832795/180));}
				 else n = tan(0);}
			 else n=tan(n); break;
	case 'l':if(n <= 0)
			 {
				 this->doLog();
			 }
			 else {n=log(n);}break;
	case 'g':if(n <= 0)
			 {
				 this->doLog();
			 }
			 else {n=log(n)/log(10.0);}break;
	case 'y':if (l >= 0)
				{
					n=pow(l,n);
				}
			 else {n=power(l,n);}break;
	case 'n':if(n<0)
			 {
				 this->dojiecheng();
			 }	
			 else {n=fact(n);}break;
	default:; 
	}
	Invalidate();
}


void CCalculatorDlg::OnStandard() 
{
	// Get the size of the window .	
	//Make the window twice as wide and twice as tall .
	SetWindowPos (NULL , 0 , 0 , 340 , 400 , SWP_NOMOVE|SWP_NOZORDER );
}

void CCalculatorDlg::OnScience() 
{
	// Get the size of the window .	
	//Make the window twice as wide and twice as tall .
	SetWindowPos (NULL , 0 , 0 , 475 , 400 , SWP_NOMOVE|SWP_NOZORDER );
}

void CCalculatorDlg::OnAngle() 
{
	// TODO: Add your control notification handler code here
	sjflag = 5;
}

void CCalculatorDlg::OnRadian() 
{
	// TODO: Add your control notification handler code here
	sjflag = 6;
}

void CCalculatorDlg::dozero()
{
	this->GetDlgItem(IDC_EDIT1)->SetWindowText(_T("除数不能为0"));
}

void CCalculatorDlg::dojiecheng()
{
	this->GetDlgItem(IDC_EDIT1)->SetWindowText(_T("阶乘不能为负数"));
}

void CCalculatorDlg::dodaoshu()
{
	this->GetDlgItem(IDC_EDIT1)->SetWindowText(_T("倒数不能为0"));
}

void CCalculatorDlg::doSqrt()
{
	this->GetDlgItem(IDC_EDIT1)->SetWindowText(_T("实数范围内只可求正数的开平方"));
}

void CCalculatorDlg::doLog()
{
	this->GetDlgItem(IDC_EDIT1)->SetWindowText(_T("真数需大于0"));
}

⌨️ 快捷键说明

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