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

📄 xycalcdlg.cpp

📁 这是书上的代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:












void CXYCALCDlg::Onzhengfu() 
{
	// TODO: Add your control notification handler code here
if (m_operavail)
     m_operand=-m_operand;
 else 
	  m_accum=-m_accum;
  updatedisp(  );
}



void CXYCALCDlg::Onpoint() 
{
	// TODO: Add your control notification handler code here
	if (point==0)
		point=1;
	m_operavail=TRUE;
	 
}





void CXYCALCDlg::Onbackspaces() 
{
	// TODO: Add your control notification handler code here
	if (point==0)
		m_operand=(m_operand-((int)(m_operand))%10)/10;
	else
	{ m_operand=m_operand-(((int)(m_operand*pow(10,bitnum)))%10)/pow(10,bitnum);
	  bitnum--;
	  if (bitnum==0)
		  point=0;
	}
	updatedisp( );
}

void CXYCALCDlg::Onclear() 
{
	// TODO: Add your control notification handler code here
	m_operand = 0L;
    m_operator = OpNone;
	m_accum = 0L;
    point=0;
	bitnum=0;
	memoryvalue=0L;
    m_operavail=FALSE;
   	m_errorState = ErrNone;
updatedisp( );
}

void CXYCALCDlg::setoperand(double newvalue)
{
   m_operavail=TRUE;

    m_operand=newvalue;
}

	

void CXYCALCDlg::updatedisp()
{
  	CString str;
    double lVal = (m_operavail) ? m_operand : m_accum;
    str.Format(_T("%f"), lVal);
	int i=str.GetLength( );
	while(str.GetAt(i-1)=='0')
	{
		str.Delete(i-1,1);
		i-=1;
	}
	CEdit* pedit=(CEdit *)GetDlgItem(IDC_EDIT1);
	pedit->SetWindowText(str);
	m_string=str;
	UpdateData( );
}


void CXYCALCDlg::perform()
{
	if (m_operavail)
	{
		if (m_operator == OpNone)
			m_accum = m_operand;
		else if (m_operator == OpMultiply)
			m_accum *= m_operand;
		else if (m_operator == OpDivide)
		{
			if (m_operand == 0)
				m_errorState = ErrDivideByZero;
			else
				m_accum /= m_operand;
		}
		else if (m_operator == OpAdd)
			m_accum += m_operand;
		else if (m_operator == OpSubtract)
			m_accum -= m_operand;
		else if (m_operator==OpSqrt)
		         if ((m_operavail)&&(m_operand))
			        m_accum=sqrt(m_operand);
	}
      else
		   if (m_operator==OpSqrt)
			    m_accum=sqrt(m_accum);
	m_operavail = FALSE;
	updatedisp();

}

void CXYCALCDlg::OnMc() 
{
	// TODO: Add your control notification handler code here
   memoryvalue=0L;	
   	GetDlgItem(IDC_EDIT2)->SetWindowText("");
//		GetDlgItem(IDC_MC)->SetWindowText("HOW ARE YOU");

}

void CXYCALCDlg::OnMs() 
{
	// TODO: Add your control notification handler code here
	double lval = (m_operavail) ? m_operand : m_accum;
	memoryvalue=lval;
	if(memoryvalue!=0)
	GetDlgItem(IDC_EDIT2)->SetWindowText("M");
}

void CXYCALCDlg::OnMadd() 
{
	// TODO: Add your control notification handler code here
    m_operator = OpMadd;
	if(m_operavail)
	  { if(m_operand+memoryvalue==0)
	         OnMc( );
        else  OnMs( );
	}
   else
	if(m_accum+memoryvalue==0)
	  	 OnMc( ); 
	 else  	OnMs( );
	
}

void CXYCALCDlg::OnMr() 
{
	// TODO: Add your control notification handler code here
	if (m_operator==OpMadd)
		if(m_operavail)
		{ if (memoryvalue!=0)	
			   memoryvalue+=m_operand;
		}
		else
		{if (memoryvalue!=0)	memoryvalue+=m_accum;
		}
    m_accum=memoryvalue;
    if( memoryvalue==0)
		OnMc( );
	m_operavail=FALSE;
	updatedisp( );
}

void CXYCALCDlg::OnMenuitem32776() 
{
	// TODO: Add your command handler code here
	CAboutDlg dlg;
    dlg.DoModal( );

}



void CXYCALCDlg::OnMenuitem32774() 
{
	// TODO: Add your command handler code here
	
		CKXdlg dlg;
	dlg.DoModal( );
}

void CXYCALCDlg::ClickNumber(int lnum)
{
    if (!m_operavail)
	{m_operand = 0L;
	 point=0;
	 bitnum=0;
	}
	if (point==1)
	{	bitnum++;
	 setoperand(m_operand+lnum/pow(10,bitnum));
    }   
	 else		
	setoperand(m_operand*10+lnum);
	updatedisp();	
}
void CXYCALCDlg::OnClickedNumber(UINT nID)
{
	ASSERT(nID >= ID_0 && nID <= ID_9);
	ClickNumber(nID - ID_0);
}


BOOL CXYCALCDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
		if (m_hAccel != NULL && TranslateAccelerator(m_hWnd, m_hAccel, pMsg))
		return TRUE;

		
		if( pMsg->message ==WM_RBUTTONDOWN)
	{
		POINT pt = pMsg->pt;	
		ScreenToClient( &pt );
		m_toolTip.ShowToolTip( (CPoint)pt );
	}



	return CDialog::PreTranslateMessage(pMsg);

}

void CXYCALCDlg::OnMENUcopy() 
{
	// TODO: Add your command handler code here
	CXYCALCApp::fuzhi=m_string;	
}

void CXYCALCDlg::OnMENUpaste() 
{
	// TODO: Add your command handler code here
int i=0,j;
char a;
m_operand=0;
point=0;
m_string=CXYCALCApp::fuzhi;
bitnum=0;
UpdateData(FALSE);
j=m_string.GetLength( );
if(!j)   {m_string="0";	UpdateData(FALSE);}

while (i<j)
{	   a=m_string.GetAt(i);
if(a=='.') 
{if (point==0)
{point=1;   
 if(i==j-1) point=0;        //如果是整数,去掉小数点
 i++; continue;}
}
if (point==1)
	{bitnum++;
     setoperand(m_operand+(a-48)/pow(10,bitnum));
   }
  else		
     setoperand(m_operand*10+a-48);
 i++;
}
//setoperand(atof(m_string));
}

void CXYCALCDlg::OnChangeEdit1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
//
//	{m_operand = 0L;
//	 point=0;
//	 bitnum=0;
//	}
//if (!m_operavail)
//	CEdit* pedit=(CEdit*)GetDlgItem(IDC_EDIT1);
 //   pedit->GetWindowText(m_string);

//if(m_operavail)  
//	setoperand(atof(m_string));	
//	if(!m_operavail) m_string="0";
   // UpdateData(FALSE);
}


HBRUSH CXYCALCDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	
	// TODO: Change any attributes of the DC here
	
	// TODO: Return a different brush if the default is not desired
  	if (nCtlColor != CTLCOLOR_EDIT && nCtlColor != CTLCOLOR_LISTBOX) {
   		pDC->SetBkMode(TRANSPARENT);
     	HBRUSH B = CreateSolidBrush(LTGRAY); 
     	return (HBRUSH) B;
  	} 

	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	return hbr;
}


void CXYCALCDlg::buttoncolor()
{
	VERIFY(m_btn1.Attach(ID_1, this, LTGRAY, BLUE, DKGRAY)); 
	VERIFY(m_btn2.Attach(ID_2, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_btn3.Attach(ID_3, this, LTGRAY, BLUE, DKGRAY));
	VERIFY(m_btn4.Attach(ID_4, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_btn5.Attach(ID_5, this, LTGRAY, BLUE, DKGRAY)); 
	VERIFY(m_btn6.Attach(ID_6, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_btn7.Attach(ID_7, this, LTGRAY, BLUE, DKGRAY));
	VERIFY(m_btn8.Attach(ID_8, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_btn9.Attach(ID_9, this, LTGRAY, BLUE, DKGRAY));
	VERIFY(m_btn0.Attach(ID_0, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_btn10.Attach(ID_10, this, LTGRAY,BLUE, DKGRAY));
	VERIFY(m_btn11.Attach(ID_11, this,  LTGRAY, BLUE, DKGRAY));

	VERIFY(m_butn1.Attach(ID_BUTTON1, this, LTGRAY, RED, DKGRAY)); 
	VERIFY(m_butn2.Attach(ID_BUTTON2, this,  LTGRAY, RED, DKGRAY));
	VERIFY(m_butn3.Attach(ID_BUTTON3, this, LTGRAY, RED, DKGRAY));
	VERIFY(m_butn4.Attach(ID_BUTTON4, this,  LTGRAY, RED, DKGRAY));
	VERIFY(m_butn5.Attach(ID_BUTTON5, this, LTGRAY, BLUE, DKGRAY)); 
	VERIFY(m_butn6.Attach(ID_BUTTON6, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_butn7.Attach(ID_BUTTON7, this, LTGRAY, BLUE, DKGRAY));
	VERIFY(m_butn8.Attach(ID_BUTTON8, this,  LTGRAY, RED, DKGRAY));

	VERIFY(m_b1.Attach(IDC_MC, this, LTGRAY, RED, DKGRAY)); 
	VERIFY(m_b2.Attach(IDC_MR, this,  LTGRAY, RED, DKGRAY));
	VERIFY(m_b3.Attach(IDC_MS, this, LTGRAY, RED, DKGRAY));
	VERIFY(m_b4.Attach(IDC_MADD, this,  LTGRAY, RED, DKGRAY));
	VERIFY(m_b5.Attach(IDC_BUTTON1, this, LTGRAY, RED, DKGRAY)); 
	VERIFY(m_b6.Attach(IDC_BUTTON2, this,  LTGRAY, RED, DKGRAY));
	VERIFY(m_b7.Attach(IDCANCEL1, this, LTGRAY, RED, DKGRAY));



}

void CXYCALCDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnClose();
}


BOOL CAboutDlg::OnInitDialog()
{
	CDialog::OnInitDialog();	// CG:  This was added by System Info Component.

	// CG: Following block was added by System Info Component.
	{
		CString strFreeDiskSpace;
		CString strFreeMemory;
		CString strFmt;

		// Fill available memory
		MEMORYSTATUS MemStat;
		MemStat.dwLength = sizeof(MEMORYSTATUS);
		GlobalMemoryStatus(&MemStat);
		strFmt.LoadString(CG_IDS_PHYSICAL_MEM);
		strFreeMemory.Format(strFmt, MemStat.dwTotalPhys / 1024L);

		SetDlgItemText(IDC_MEMORY,strFreeMemory);
		//TODO: Add a static control to your About Box to receive the memory
		//      information.  Initialize the control with code like this:
		// SetDlgItemText(IDC_PHYSICAL_MEM, strFreeMemory);

		// Fill disk free information
		struct _diskfree_t diskfree;
		int nDrive = _getdrive(); // use current default drive
		if (_getdiskfree(nDrive, &diskfree) == 0)
		{
			strFmt.LoadString(CG_IDS_DISK_SPACE);
			strFreeDiskSpace.Format(strFmt,
				(DWORD)diskfree.avail_clusters *
				(DWORD)diskfree.sectors_per_cluster *
				(DWORD)diskfree.bytes_per_sector / (DWORD)1024L,
				nDrive-1 + _T('A'));
		}
		else
			strFreeDiskSpace.LoadString(CG_IDS_DISK_SPACE_UNAVAIL);
            SetDlgItemText(IDC_DISK,strFreeDiskSpace);
		//TODO: Add a static control to your About Box to receive the memory
		//      information.  Initialize the control with code like this:
		// SetDlgItemText(IDC_DISK_SPACE, strFreeDiskSpace);
	}

	return TRUE;	// CG:  This was added by System Info Component.

}

void CXYCALCDlg::OnHelpContents() 
{
	// TODO: Add your command handler code here
//	WinHelp(0,HELP_CONTENTS);
}

⌨️ 快捷键说明

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