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

📄 kxdlg.cpp

📁 计算器 WINDOWS MFC的计算器 加减乘除都可以
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	m_operator=OpPow;
	m_operavail=FALSE;
run( );//	pushfh( );
}

void CKXdlg::OnPi() 
{
	// TODO: Add your control notification handler code here
CString str;
if(m_inv)
setoperand(3.14159265535897932384*2);
else	
setoperand(3.14159265535897932384);
str.Format(_T("%.20f"), m_operand);
CEdit* pedit=(CEdit *)GetDlgItem(IDC_EDITKX);
pedit->SetWindowText(str);
m_str=str;
}

void CKXdlg::OnDms() 
{
	// TODO: Add your control notification handler code here
	int degree,min,secc,m1,m2;
	double res,sec,secm;
	 degree=int(m_operand);
	 res=m_operand-degree;
     min=int(res*60.0);
	 res=res*60.0-min;
     sec=res*60.0;
	 m2=int(sec);
	 secm=sec-m2;
	 CString str,str1;
	str.Format("%f",secm);
	int i=str.GetLength( );
	while(str.GetAt(i-1)=='0')
	{
	   str.Delete(i-1,1);
		i-=1;
	}
	m1=str.GetLength();
	if((m1==2)&&(m2==0))
	{ if (min==0)
	    str1.Format("%d.",degree);
	  else
	   str1.Format("%d.%02d",degree,min);  
	}
	else
	{ if((m2!=0)&&(m1==2))
	      str1.Format("%d.%02d%02d",degree,min,m2);   
	else  
	{secc=int(secm*pow(10,m1-2));
	  str1.Format("%d.%02d%02d%d",degree,min,m2,secc);
	}	
	}
	  //res=m_operand-degree;
 		CEdit* pedit=(CEdit *)GetDlgItem(IDC_EDITKX);
		pedit->SetWindowText(str1);
 m_operavail=FALSE;   
}

void CKXdlg::OnMc1() 
{
	// TODO: Add your control notification handler code here
     kxmemory=0L;	
   	GetDlgItem(IDC_EDIT1)->SetWindowText(" ");
	
}

void CKXdlg::OnMr1() 
{
	// TODO: Add your control notification handler code here
			if (m_operator==OpMadd)
		if(m_operavail)
		{ if (kxmemory)  kxmemory+=m_operand;}
		else
		{if(kxmemory)	kxmemory+=m_accum;}
    m_accum=kxmemory;
    if(kxmemory==0)
		OnMc1( );
	m_operavail=FALSE;
	updisp( );


}

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

void CKXdlg::OnMadd1() 
{
	// TODO: Add your control notification handler code here
	    m_operator = OpMadd;
	if(m_operavail)
	  { if(m_operand+kxmemory==0)
	         OnMc1( );
        else  OnMs1( );
}
   else
	if(m_accum+kxmemory==0)
	  	 OnMc1( ); 
	 else  	OnMs1( );


}

void CKXdlg::OnBaspace() 
{
	// TODO: Add your control notification handler code here
if(m_jinzhi==1)
{
	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;
	}
}
if(m_jinzhi==0)
{		m_operand=(m_operand-(int)(m_operand)%16)/16;

}
if(m_jinzhi==2)
{		m_operand=(m_operand-(int)(m_operand)%8)/8;

}
if(m_jinzhi==3)
{	m_operand=(m_operand-(int)(m_operand)%2)/2;
}

updisp( );

}

void CKXdlg::OnCheck1() 
{
	// TODO: Add your control notification handler code here
	m_inv=TRUE;
	UpdateData( );
}

void CKXdlg::OnCheck2() 
{
	// TODO: Add your control notification handler code here
	m_hyp=TRUE;
	UpdateData( );
}

void CKXdlg::OnMENUcopy() 
{
	// TODO: Add your command handler code here
	CXYCALCApp::fuzhi=m_str;
	if(m_jinzhi==1)  flagjz=1;      //10
	 else if(m_jinzhi==0) flagjz=2;  //16
	  else  flagjz=3;    //2,8
}

void CKXdlg::OnMENUpaste() 
{
	// TODO: Add your command handler code here
if(m_jinzhi==1)  //    如果是十进制
	{  int i=0,j;
    	char a;
    	m_operand=0;
    	bitnum=0;
    	point=0;
    	m_str=CXYCALCApp::fuzhi;
		j=m_str.GetLength( );
	 if(j)
	 {if(m_str.GetAt(0)=='-')	{	m_str.Delete(0,1);j--;}}
    	else {m_str="0";	UpdateData(FALSE);}
   
    if(flagjz==1||flagjz==0)             //复制时是十进制,则需考虑小数点
	{	 while (i<j)
		 {  a=m_str.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++;
		}
	UpdateData(FALSE);
	}
	else if((flagjz==2)||(flagjz==3))
	{  strtonum(10,57); 
	}

	}
	if(m_jinzhi==3)                 //二进制
	{   
       strtonum(2,49);

		}
	if(m_jinzhi==0)            //16进制
	{ 
		//strtonum(16,);
		if((flagjz==1)||(flagjz==3))
			strtonum(16,57);
		else if(flagjz==2)
		{
		    int length;int i=0;char a;double num;	m_operand=0;
			m_str=CXYCALCApp::fuzhi;
			length=m_str.GetLength( );
			if(length)  {if(m_str.GetAt(0)=='-')	{  m_str.Delete(0,1);length--;}}
			else  {m_str="0";	UpdateData(FALSE);}
     
			 CString str1=m_str;
        	while(i<length)	//turn to 16
			{    a=str1.GetAt(i);
	           if(a>='0'&&a<='9')
			      num=m_operand*16+a-48;
			   else if(a>='a'&&a<='f')
                   num=m_operand*16+a-87;
		       if(m_dushu==0)
			   {  if( num<=4294967295)
		         	setoperand(num);
			   else {str1.Delete(i,length-i);break;}}
		        if(m_dushu==1)
				{if(num<=65535)
				    setoperand(num);
				else  {str1.Delete(i,length-i);break;}}
			 	 if(m_dushu==2)
				{if(num<=255)
				    setoperand(num);
				 else  {str1.Delete(i,length-i);break;}}
		     i++;
		}
         	m_str=str1;
		UpdateData(FALSE);
		}
	}
	if(m_jinzhi==2)              //8进制
	{ strtonum(8,55);
	}
}

void CKXdlg::strtonum(int m,int n)
{
 CString str1;
 	m_operand=0;
m_str=CXYCALCApp::fuzhi;
int length=m_str.GetLength( );
if(!length) { m_str="0";	UpdateData(FALSE);}
str1=m_str;
int i=0;char a;
		while(i<length)
		{if((a=str1.GetAt(i))>n||(a=='.')||(a=='-')) {str1.Delete(i,1);i--;length--;}
		i++;}
		length=str1.GetLength( );
		i=0;   double num;
		if(length==0)  str1="0";
		while(i<length)	//各种进制化成十进制操作数
		  { 
	           num=m_operand*m+str1.GetAt(i)-48;
		       if(m_dushu==0)
			   {  if( num<=4294967295)
		         	setoperand(num);
			   else {str1.Delete(i,length-i);break;}}
		        if(m_dushu==1)
				{if(num<=65535)
				    setoperand(num);
				else  {str1.Delete(i,length-i);break;}}
			 	 if(m_dushu==2)
				{if(num<=255)
				    setoperand(num);
				 else  {str1.Delete(i,length-i);break;}}
		     i++;
		}
		m_str=str1;
		UpdateData(FALSE);
}

HBRUSH CKXdlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	
	// TODO: Return a different brush if the default is not desired
	return hbr;
}


void CKXdlg::OnSta() 
{
	// TODO: Add your control notification handler code here
	   	enablecount(TRUE);
if(!g_pmodless)
 g_pmodless=new CCountDlg(this);
}

void CKXdlg::OnDat() 
{
	// TODO: Add your control notification handler code here
	if(g_pmodless)
	{
		g_pmodless->m_list.InsertString(g_pmodless->m_num,m_str);
		g_pmodless->m_list.SetCurSel(g_pmodless->m_num);
		double lVal = (m_operavail) ?m_operand : m_accum;
		g_pmodless->num[g_pmodless->m_num]=lVal;
		g_pmodless->m_num+=1;
		g_pmodless->UpdateData(FALSE);
		m_accum=m_operand;
		m_operavail=FALSE;
	}
	else
enablecount(FALSE);

}

void CKXdlg::OnSum() 
{
	// TODO: Add your control notification handler code here
double sum=0;
int i;
if(g_pmodless)
{for(i=0;i<(g_pmodless->m_num);i++)
 sum+=(g_pmodless->num[i]);
m_accum=m_operand=sum;
	updisp( );
}
else
enablecount(FALSE);
}

void CKXdlg::OnAve() 
{
	// TODO: Add your control notification handler code here
double sum=0;
int i;
if(g_pmodless)
{
for(i=0;i<(g_pmodless->m_num);i++)
 sum+=(g_pmodless->num[i]);
m_accum=m_operand=sum/(g_pmodless->m_num);
	updisp( );
}
else
 enablecount(FALSE);

}

void CKXdlg::OnS() 
{
	// TODO: Add your control notification handler code here
	if(g_pmodless)
	{
    double		sum=0,ave;
	int i;
    for(i=0;i<(g_pmodless->m_num);i++)
     sum+=(g_pmodless->num[i]);
      ave=sum/(g_pmodless->m_num);
     sum=0;
	  for(i=0;i<(g_pmodless->m_num);i++)
         sum+=pow(((g_pmodless->num[i])-ave),2);
	  m_accum=m_operand=sqrt(sum/(g_pmodless->m_num-1));
	updisp( );

	}
	else
 enablecount(FALSE);

}

void CKXdlg::colorbutton()
{
	VERIFY(m_btn1.Attach(IDC_STA, this, LTGRAY, BLUE, DKGRAY)); 
	VERIFY(m_btn2.Attach(IDC_AVE, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_btn3.Attach(IDC_SUM, this, LTGRAY, BLUE, DKGRAY));
	VERIFY(m_btn4.Attach(IDC_S, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_btn5.Attach(IDC_DAT, this, LTGRAY,BLUE, DKGRAY)); 
	VERIFY(m_btn6.Attach(IDC_PI, this, LTGRAY,BLUE, DKGRAY)); 

	VERIFY(m_C1.Attach(IDC_BUTTON1, this, LTGRAY, RED, DKGRAY)); 
	VERIFY(m_C2.Attach(IDOK, this,  LTGRAY, RED, DKGRAY));
	VERIFY(m_C3.Attach(IDC_BASPACE, this, LTGRAY, RED, DKGRAY));
	VERIFY(m_d1.Attach(IDC_MC1, this, LTGRAY, RED, DKGRAY)); 
	VERIFY(m_d2.Attach(IDC_MR1, this,  LTGRAY, RED, DKGRAY));
	VERIFY(m_d3.Attach(IDC_MS1, this, LTGRAY, RED, DKGRAY));
	VERIFY(m_d4.Attach(IDC_MADD1, this, LTGRAY, RED, DKGRAY));

	VERIFY(m_b1.Attach(IDC_FE, this, LTGRAY, MAGENTA, DKGRAY)); 
	VERIFY(m_b2.Attach(IDC_DMS, this,  LTGRAY, MAGENTA, DKGRAY));
	VERIFY(m_b3.Attach(IDC_SIN, this, LTGRAY, MAGENTA, DKGRAY));
	VERIFY(m_b4.Attach(IDC_COS, this,  LTGRAY, MAGENTA, DKGRAY));
	VERIFY(m_b5.Attach(IDC_TAN, this, LTGRAY, MAGENTA, DKGRAY)); 
	VERIFY(m_b6.Attach(IDC_LEFT, this,  LTGRAY, MAGENTA, DKGRAY));
	VERIFY(m_b7.Attach(IDC_EXP, this, LTGRAY, MAGENTA, DKGRAY));
	VERIFY(m_b8.Attach(IDC_XY, this,  LTGRAY, MAGENTA, DKGRAY));
	VERIFY(m_b9.Attach(IDC_X3, this, LTGRAY, MAGENTA, DKGRAY));
	VERIFY(m_b10.Attach(IDC_X2, this, LTGRAY,MAGENTA, DKGRAY));
	VERIFY(m_b11.Attach(IDC_RIGHT, this,  LTGRAY, MAGENTA, DKGRAY));
	VERIFY(m_b12.Attach(IDC_IN, this,  LTGRAY, MAGENTA, DKGRAY));
	VERIFY(m_b13.Attach(IDC_LOG, this, LTGRAY, MAGENTA, DKGRAY));
	VERIFY(m_b14.Attach(IDC_FACTOR, this,  LTGRAY, MAGENTA, DKGRAY));
	VERIFY(m_b15.Attach(IDC_DAOSHUX, this, LTGRAY, MAGENTA, DKGRAY)); 

	VERIFY(m_1.Attach(IDC_BUTTONA0, this, LTGRAY, BLUE, DKGRAY)); 
	VERIFY(m_2.Attach(IDC_BUTTONA1, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_3.Attach(IDC_BUTTONA2, this, LTGRAY, BLUE, DKGRAY));
	VERIFY(m_4.Attach(IDC_BUTTONA3, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_5.Attach(IDC_BUTTONA4, this, LTGRAY,BLUE, DKGRAY)); 
	VERIFY(m_6.Attach(IDC_BUTTONA5, this, LTGRAY,BLUE, DKGRAY)); 
	VERIFY(m_7.Attach(IDC_BUTTONA6, this, LTGRAY, BLUE, DKGRAY)); 
	VERIFY(m_8.Attach(IDC_BUTTONA7, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_9.Attach(IDC_BUTTONA8, this, LTGRAY, BLUE, DKGRAY));
	VERIFY(m_10.Attach(IDC_BUTTONA9, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_11.Attach(IDC_BUTTONA, this, LTGRAY,BLUE, DKGRAY)); 
	VERIFY(m_12.Attach(IDC_BUTTONB, this, LTGRAY,BLUE, DKGRAY)); 
	VERIFY(m_13.Attach(IDC_BUTTONC, this, LTGRAY, BLUE, DKGRAY)); 
	VERIFY(m_14.Attach(IDC_BUTTOND, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_15.Attach(IDC_BUTTONE, this, LTGRAY, BLUE, DKGRAY));
	VERIFY(m_16.Attach(IDC_BUTTONF, this,  LTGRAY, BLUE, DKGRAY));
	VERIFY(m_17.Attach(IDC_BUTTONPOINT, this, LTGRAY,BLUE, DKGRAY)); 
	VERIFY(m_18.Attach(IDC_BUTTON18, this, LTGRAY,BLUE, DKGRAY)); 
	VERIFY(m_t5.Attach(IDC_BUTTON5, this, LTGRAY, RED, DKGRAY));
	VERIFY(m_t6.Attach(IDC_BUTTON6, this,  LTGRAY, RED, DKGRAY));
	VERIFY(m_t7.Attach(IDC_BUTTON7, this, LTGRAY,RED, DKGRAY)); 
	VERIFY(m_t8.Attach(IDC_BUTTON8, this, LTGRAY,RED, DKGRAY)); 

	VERIFY(m_m1.Attach(IDC_MOD, this, LTGRAY, RED, DKGRAY)); 
	VERIFY(m_m2.Attach(IDC_OR, this,  LTGRAY, RED, DKGRAY));
	VERIFY(m_m3.Attach(IDC_LSH, this, LTGRAY, RED, DKGRAY));
	VERIFY(m_m4.Attach(IDC_EQUAL, this,  LTGRAY, RED, DKGRAY));
	VERIFY(m_m5.Attach(IDC_AND, this, LTGRAY,RED, DKGRAY)); 
	VERIFY(m_m6.Attach(IDC_XOR, this, LTGRAY,RED, DKGRAY)); 
	VERIFY(m_m7.Attach(IDC_NOT, this, LTGRAY, RED, DKGRAY)); 
	VERIFY(m_m8.Attach(IDC_INT, this,  LTGRAY, RED, DKGRAY));
}

void CKXdlg::enters(double num)
{
s.push(num);
}

int CKXdlg::gettwo(double& op1, double& op2)
{
if(s.stackempty( ))
{ return 0;}
op1=s.pop( );
if(s.stackempty( ))
{return 0;}
op2=s.pop( );
return 1;
}

void CKXdlg::run()
{
	Operator play;
   if(p.stackempty( )||(m_operator==OpLeft)||m_operator>p.peek( ))
   {	if( m_operator==OpLeft)		
	       enterp(m_operator);
        else  {enterp(m_operator); enters(m_operand);}
   }
    else{  enters(m_operand);
		  while (!p.stackempty( )&&m_operator<=p.peek( ))
			 {  play=p.pop( ); 
		      computer(play); 
						   }
           enterp(m_operator);}
  if(!s.stackempty( ))
  {	m_operand=m_accum=s.peek( );
  updisp( );}
	if(m_operator==OpNone)
	{clearp( );clears( );}



}

void CKXdlg::clears()
{
s.clearstack( );
}

void CKXdlg::enterp(Operator opp)
{
p.push(opp);
}

void CKXdlg::clearp()
{
p.clearstack( );

}

void CKXdlg::computer(Operator opp)
{
double  op1,op2;
int result;
int h;
result=gettwo(op1,op2);
if(result)
{
      switch(opp)
	  { case OpAdd: enters(op1+op2);break;
	  case OpMultiply: enters(op1*op2);break;
		  
	  case OpSubtract: enters(op2-op1);break;
	  case OpDivide: enters(op2/op1);break;
	  case OpLsh: h=((int)op2)<<((int)op1);
		          enters(h);break;
	  case OpMod: enters((int)op2%(int)op1);break;
	  case OpOr:  h=((int)op2)|((int)op1);
		         enters(h);break;
	  case OpXor:  h=((int)op2)^((int)op1);
		          enters(h);break;
	  case OpAnd: h=((int)op2)&((int)op1);
		          enters(h);break;
	  case OpPow: double m;
		         if (m_inv)
					 {m=pow(op2,1.0/op1);
		              m_inv=FALSE;
			          UpdateData(FALSE);}
				   else  m=pow(op2,op1);
		           enters(m);break;
		         
	  }

}
else
{ clears( );clearp( );}

}

⌨️ 快捷键说明

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