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

📄 计算器dlg.cpp

📁 加减乘除计算,正余弦,倒数,阶乘计算,进制转换等
💻 CPP
📖 第 1 页 / 共 3 页
字号:

void CMyDlg::OnNum14() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(OperatorPressed==true||Restarted==true)
		m_strResult="E";
	else
		m_strResult+="E";
	OperatorPressed=false;
	Restarted=false;
	h=1;
	UpdateData(FALSE);
}

void CMyDlg::OnNum15() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(OperatorPressed==true||Restarted==true)
		m_strResult="F";
	else
		m_strResult+="F";
	OperatorPressed=false;
	Restarted=false;
	h=1;
	UpdateData(FALSE);
}

void CMyDlg::OnDot()  //小数点
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(OperatorPressed==true||Restarted==true)
	 {if(i==0)
	   {m_strResult="0.";i=1;
	    OperatorPressed=false;
	    Restarted=false; 
	   }     
    else
	 { Error(0);OperatorPressed=true;i=0;}    //多个小数点时出错
	 }
	else
	 {if(i==0)
	  {m_strResult+=".";i=1;}
    else
	 { Error(0);OperatorPressed=true;i=0;}
	 }
	h=1;
	UpdateData(FALSE);
}

void CMyDlg::OnAc() //清空
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	Restarted=true;
	m_strResult="0";
	h=0;
	i=0;
	UpdateData(FALSE);
}

void CMyDlg::OnBack() //退一位
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	double n5;
	if(m_strResult!="0"&&m_strResult!="0.0"&&m_strResult.Left(m_strResult.GetLength()-1)!="")
	   {
		m_strResult=m_strResult.Left(m_strResult.GetLength()-1);
	    n5=atof(m_strResult);
	   if((n5>=1||n5<=-1)&&(n5==0||fmod(n5,floor(n5))==0)&&m_strResult.GetAt(strlen(m_strResult)-1)!='.')  i=0;
	   } 
	else
	 { m_strResult="0";i=0;}
	UpdateData(FALSE);
}

void CMyDlg::OnAdd() //加法
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
	if(k==2||k==8||k==16)
	 {
	  strcpy (str1,m_strResult);
      n1=qitazhuanshi(str1, k);
	 }
	else
    n1=atof(m_strResult);
	m_shi=m_strResult;
	s='+';i=0;
	UpdateData(FALSE);
	OperatorPressed=true;

}

void CMyDlg::OnEqual() //等号
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(k==2||k==8||k==16)
	 {
	  strcpy (str1,m_strResult);
      n2=qitazhuanshi(str1, k);
	 }
	else
	 n2=atof(m_strResult);
	 if(h==0)
	 Error(11);
	 else
	  if(h==1)
	   { h=2;
	  switch (s)
	  { 
	  case '+': n3=n1+n2;
	            m_shi+='+';
				m_shi+=m_strResult;
	            if(k==2||k==8||k==16)  
	             shizhuanqita(n3, k);
			    else
	             m_strResult.Format("%f",n3);
				m_strResult.TrimRight('0');
                m_strResult.TrimRight('.');
				break;
	   case '-': n3=n1-n2;
		         m_shi+='-';
				 m_shi+=m_strResult;
		         if(k==2||k==8||k==16)  
	             shizhuanqita(n3, k);
			    else
	             m_strResult.Format("%f",n3); 
				 m_strResult.TrimRight('0');
				 m_strResult.TrimRight('.');
				break;
	   case '*': n3=n1*n2; 
		         m_shi+='*';
			   	 m_shi+=m_strResult;
		         if(k==2||k==8||k==16)  
	             shizhuanqita(n3, k);
			    else
	             m_strResult.Format("%f",n3); 
				m_strResult.TrimRight('0');
				m_strResult.TrimRight('.');
				break;
	   case '/':  if (n2==0){ Error(1);return;}
		          else 
				  {n3=n1/n2; 
				   m_shi+='/';
				   m_shi+=m_strResult;
				   if(k==2||k==8||k==16)  
	              shizhuanqita(n3, k);
			     else
	              m_strResult.Format("%f",n3); 
				  }
				  m_strResult.TrimRight('0');
				  m_strResult.TrimRight('.');
				  break;				   
       case '^': m_shi+='^';
				 m_shi+=m_strResult;
		        if(k==2||k==8||k==16)  
	              shizhuanqita(pow(n1,n2), k);
			    else
	              m_strResult.Format("%f",pow(n1,n2)); 
				m_strResult.TrimRight('0');
				m_strResult.TrimRight('.');
				break;
       case '%':  m_shi+='%';
				  m_shi+=m_strResult; 
		          if (n2==0) {Error(4);return;}
				   else
					 if(k==2||k==8||k==16)  
	                 shizhuanqita(fmod(n1,n2), k);
			        else
					  m_strResult.Format("%f",fmod(n1,n2));  
					m_strResult.TrimRight('0');
					m_strResult.TrimRight('.');
				   break;
	   default : break; 
	   }
	  if(s!=NULL)
	   {
		m_shi+='=';
	    m_shi+=m_strResult;
	    if(t<16)
		 {strcpy(str3[t].str2,m_shi);t++;}
	    else
	     {t=1;strcpy(str3[t].str2,m_shi);t++;}
	   }
	    m_shi="";
	   }
	 else if(h==2)
		Error(12);
	UpdateData(FALSE);
	i=0;s=NULL;
    OperatorPressed=true;
}

void CMyDlg::OnDec() //减法
{
	// TODO: Add your control notification handler code here
	
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
    if(k==2||k==8||k==16)
	 {
	  strcpy (str1,m_strResult);
      n1=qitazhuanshi(str1, k);
	 }
	else
    n1=atof(m_strResult);
	m_shi=m_strResult;
	s='-';i=0;
	UpdateData(FALSE);
	OperatorPressed=true;
}

void CMyDlg::OnMut() //乘法
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
    if(k==2||k==8||k==16)
	 {
	  strcpy (str1,m_strResult);
      n1=qitazhuanshi(str1, k);
	 }
	else
    n1=atof(m_strResult);
	m_shi=m_strResult;
	s='*';i=0;
	UpdateData(FALSE);
	OperatorPressed=true;
}

void CMyDlg::OnDiv() //除法
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
    if(k==2||k==8||k==16)
	 {
	  strcpy (str1,m_strResult);
      n1=qitazhuanshi(str1, k);
	 }
	else
    n1=atof(m_strResult);
	m_shi=m_strResult;
	s='/';i=0;
	UpdateData(FALSE);
	OperatorPressed=true;
}

void CMyDlg::OnMo() //求模
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
    if(k==2||k==8||k==16)
	 {
	  strcpy (str1,m_strResult);
      n1=qitazhuanshi(str1, k);
	 }
	else
    n1=atof(m_strResult);
	m_shi=m_strResult;
	s='%';i=0;
	UpdateData(FALSE);
	OperatorPressed=true;
}

void CMyDlg::OnMutmut() //x的y次方
{
	// TODO: Add your control notification handler code here	
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
    if(k==2||k==8||k==16)
	 {
	  strcpy (str1,m_strResult);
      n1=qitazhuanshi(str1, k);
	 }
	else
    n1=atof(m_strResult);
	m_shi=m_strResult;
	s='^';i=0;
	UpdateData(FALSE);
	OperatorPressed=true;
}

void CMyDlg::OnSin() //正弦函数
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
	m_shi="Sin";
	m_shi+=m_strResult;
	n1=atof(m_strResult);
	if(j!=1)
    n1=n1/180*pi;
	m_strResult.Format("%f",sin(n1));
	m_strResult.TrimRight('0');
	m_strResult.TrimRight('.');
	m_shi+='=';
	m_shi+=m_strResult;
    if(t<16)
	 {strcpy(str3[t].str2,m_shi);t++;}
	else
	 {t=1;strcpy(str3[t].str2,m_shi);t++;}
    m_shi="";
	i=0;
	UpdateData(FALSE);
	OperatorPressed=true;
}

void CMyDlg::OnCos() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
	m_shi="COS";
	m_shi+=m_strResult;
	n1=atof(m_strResult);
	if(j!=1)
    n1=n1/180*pi;
	m_strResult.Format("%f",cos(n1));
	m_strResult.TrimRight('0');
	m_strResult.TrimRight('.');
	m_shi+='=';
	m_shi+=m_strResult;
    if(t<16)
	 {strcpy(str3[t].str2,m_shi);t++;}
	else
	 {t=1;strcpy(str3[t].str2,m_shi);t++;}
    m_shi="";
	i=0;
	UpdateData(FALSE);
	OperatorPressed=true;
}

void CMyDlg::OnTan() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
    m_shi="TAN";
	m_shi+=m_strResult;
	n1=atof(m_strResult);
	if(j!=1)
	 {if((n1!=0&&fmod(n1/90,3)==0)||n1==90) 
	    {Error(5);OperatorPressed=true;i=0;return;}
        n1=n1/180*pi;
	 }
	 m_strResult.Format("%f",tan(n1));
	 m_strResult.TrimRight('0');
	 m_strResult.TrimRight('.');
     m_shi+='=';
	m_shi+=m_strResult;
    if(t<16)
	 {strcpy(str3[t].str2,m_shi);t++;}
	else
	 {t=1;strcpy(str3[t].str2,m_shi);t++;}
    m_shi="";
   	i=0;
	UpdateData(FALSE);
	OperatorPressed=true;
}

void CMyDlg::OnCot() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
    m_shi="COT";
	m_shi+=m_strResult;
	n1=atof(m_strResult);
	if(j!=1)
	 {if((n1!=0&&fmod(n1/90,2)==0)||n1==0) 
	   {Error(6);OperatorPressed=true;i=0;return;}
       n1=n1/180*pi;
	 }
	m_strResult.Format("%f",1/tan(n1));
	m_strResult.TrimRight('0');
	m_strResult.TrimRight('.');
	m_shi+='=';
	m_shi+=m_strResult;
    if(t<16)
	 {strcpy(str3[t].str2,m_shi);t++;}
	else
	 {t=1;strcpy(str3[t].str2,m_shi);t++;}
    m_shi="";
	i=0;
	UpdateData(FALSE);
	OperatorPressed=true;
}

void CMyDlg::OnLn() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
	m_shi="LN";
	m_shi+=m_strResult;
	if(k==2||k==8||k==16)
	 {strcpy (str1,m_strResult);
      n1=qitazhuanshi(str1, k);
	 }
	else
    n1=atof(m_strResult);
	if(n1<=0)
	 {Error(3);return;}
	else 
	 if(k==2||k==8||k==16)  
	   shizhuanqita(log(n1), k);
     else
   	  m_strResult.Format("%f",log(n1));
	m_strResult.TrimRight('0');
	m_strResult.TrimRight('.');
	m_shi+='=';
	m_shi+=m_strResult;
    if(t<16)
	 {strcpy(str3[t].str2,m_shi);t++;}
	else
	 {t=1;strcpy(str3[t].str2,m_shi);t++;}
    m_shi="";
	i=0;
	UpdateData(FALSE);
	OperatorPressed=true;
}

void CMyDlg::OnLog() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if(h==0)
	 { Error(11);return;}
	m_shi="LOG";
	m_shi+=m_strResult;
	if(k==2||k==8||k==16)
	 {strcpy (str1,m_strResult);
      n1=qitazhuanshi(str1, k);
	 }
	else
    n1=atof(m_strResult);
	if(n1<=0)
	 {Error(3);return;}
	else 
	 if(k==2||k==8||k==16)  
	   shizhuanqita(log10(n1), k);
	 else 
   	   m_strResult.Format("%f",log10(n1));
	m_strResult.TrimRight('0');
	m_strResult.TrimRight('.');
    m_shi+='=';
	m_shi+=m_strResult;
    if(t<16)
	 {strcpy(str3[t].str2,m_shi);t++;}
	else
	 {t=1;strcpy(str3[t].str2,m_shi);t++;}
    m_shi="";
	i=0;
	UpdateData(FALSE);
	OperatorPressed=true;
}

void CMyDlg::OnDecimalist() //十进制
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	strcpy (str1,m_strResult);
    n4=qitazhuanshi(str1, k);
	shizhuanqita(n4, 10);
	UpdateData(FALSE);
	OperatorPressed=true;
    k=10; i=0;  
	m_RadioDecimalist.SetCheck(1);
	m_Num2.EnableWindow(TRUE);
	m_Num3.EnableWindow(TRUE);
	m_Num4.EnableWindow(TRUE);
	m_Num5.EnableWindow(TRUE);
	m_Num6.EnableWindow(TRUE);
	m_Num7.EnableWindow(TRUE);
	m_Num8.EnableWindow(TRUE);
	m_Num9.EnableWindow(TRUE);
	m_OprDaoshu.EnableWindow(TRUE);
	m_OprSin.EnableWindow(TRUE);
	m_OprCos.EnableWindow(TRUE);
	m_OprCot.EnableWindow(TRUE);
	m_OprAcot.EnableWindow(TRUE);
	m_OprTan.EnableWindow(TRUE);
	m_OprAsin.EnableWindow(TRUE);
	m_OprAcos.EnableWindow(TRUE);
	m_OprAtan.EnableWindow(TRUE);
	m_OprDot.EnableWindow(TRUE);
	m_OprPi.EnableWindow(TRUE);
	m_NumA.EnableWindow(FALSE);
	m_NumB.EnableWindow(FALSE);
	m_NumC.EnableWindow(FALSE);
	m_NumD.EnableWindow(FALSE);
	m_NumE.EnableWindow(FALSE);
	m_NumF.EnableWindow(FALSE);
	m_RadioDegree.EnableWindow(TRUE);
	m_RadioRadian.EnableWindow(TRUE);
	m_RadioDegree.SetCheck(0);
	m_RadioRadian.SetCheck(1);
	j=2;
}

void CMyDlg::OnHex() //十六进制
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	strcpy (str1,m_strResult);
    n4=qitazhuanshi(str1, k);

⌨️ 快捷键说明

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