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

📄 dot04_20view.cpp

📁 计算24点的C++源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			for (k=0;k<4;k++)
				for (l=0;l<4;l++)
					if (i!=j&&i!=k&&j!=k&&i!=l&&j!=l&&k!=l) 
						if (compu1(a[i],a[j],a[k],a[l],s)) 
							return s;
	s.Format("%d,%d,%d,%d 无解",i1,i2,i3,i4);
	return s;

}

float CDot04_20View::compu3(float i1, float i2, int i)
{switch(i)
	{
	case 0:
		return i1+i2;
	case 1:
		return i1-i2;
	case 2:
		return i1*i2;
	case 3:
		if (i2>0.0001||i2<-0.0001) return i1/i2;
		else return 9999.9;
	}
	return 9999.9;

}

int CDot04_20View::compu1(float i1, float i2, float i3, float i4, CString &s)
{char op[4][6]={"+","-","*","/"};
	int i,j,k;
	float f;
	for(i=0;i<4;i++)
		for (j=0;j<4;j++)
			for (k=0;k<4;k++)
			{
					f =compu3(compu3(compu3(i1,i2,i),i3,j),i4,k);
					if (f>23.9999&&f<24.0001)
					{
						s.Format("((%2d %s %2d) %s %2d) %s %2d=24",(int)i1,op[i],(int)i2,op[j],(int)i3,op[k],(int)i4); 
						return 1;
					}
					f =compu3(compu3(i1,i2,i),compu3(i3,i4,k),j);
					if (f>23.9999&&f<24.0001)
					{
						s.Format("(%2d %s %2d) %s (%2d %s %2d)=24",(int)i1,op[i],(int)i2,op[j],(int)i3,op[k],(int)i4); 
						return 1;
					}
					f =compu3(compu3(i1,compu3(i2,i3,j),i),i4,k);
					if (f>23.9999&&f<24.0001)
					{
						s.Format("(%2d %s (%2d %s %2d)) %s %2d=24",(int)i1,op[i],(int)i2,op[j],(int)i3,op[k],(int)i4); 
						return 1;
					}
					f =compu3(i1,compu3(compu3(i2,i3,j),i4,k),i);
					if (f>23.9999&&f<24.0001)
					{
						s.Format("%2d %s ((%2d %s %2d) %s %2d)=24",(int)i1,op[i],(int)i2,op[j],(int)i3,op[k],(int)i4); 
						return 1;
					}
					f =compu3(i1,compu3(i2,compu3(i3,i4,k),j),i);
					if (f>23.9999&&f<24.0001)
					{
						s.Format("%2d %s (%2d %s (%2d %s %2d))=24",(int)i1,op[i],(int)i2,op[j],(int)i3,op[k],(int)i4); 
						return 1;
					}
			}
			return 0;
}




/*HBRUSH CDot04_20View::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{  HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

   
}*/


//即时输入算式的删除
void CDot04_20View::OnChangeEdit() 
{UpdateData(true);
 
UpdateData(false);
	// send this notification unless you override the CFormView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}



HBRUSH CDot04_20View::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	switch (nCtlColor)
	{case CTLCOLOR_STATIC:
    pDC->SetBkMode(TRANSPARENT);//透明化编辑框
	case CTLCOLOR_DLG:
	 {CBrush* back_brush;
	 back_brush= new CBrush(color);
	 return (HBRUSH)(back_brush->m_hObject);}
	}
   return(CFormView::OnCtlColor(pDC,pWnd,nCtlColor));
//	HBRUSH hbr = CFormView::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 CDot04_20View::OnChangecolor() 
{CColorDialog a;               //调出调色框
if(a.DoModal()==IDOK)
   color=a.GetColor();
  Invalidate();
	// TODO: Add your command handler code here
	
}

int CDot04_20View::AnyLastPos(CString Str)
{
int SubPos=Str.ReverseFind('-')+1;
 int PluPos=Str.ReverseFind('+')+1;
 int MulPos=Str.ReverseFind('*')+1;
 int DivPos=Str.ReverseFind('/')+1;
 int Pos=SubPos;
 if(Pos<PluPos)
   Pos=PluPos;
 if(Pos<MulPos)
   Pos=MulPos;
 if(Pos<DivPos)
   Pos=DivPos;
 return Pos;
}

int CDot04_20View::AnyFirstPos(CString Str)
{
 int SubPos=Str.Find('-')+1;
 int PluPos=Str.Find('+')+1;
 int MulPos=Str.Find('*')+1;
 int DivPos=Str.Find('/')+1;
 int ForPos=Str.Find('^')+1;
 int Pos=200;

 if(SubPos==0) //如果没有-号
   SubPos=200; //将SubPos设置成一个不可能的值
 if(PluPos==0) //如果没有-号
   PluPos=200; //将PluPos设置成一个不可能的值
 if(MulPos==0) //如果没有-号
   MulPos=200; //将MulPos设置成一个不可能的值
 if(DivPos==0) //如果没有-号
   DivPos=200; //将DivPos设置成一个不可能的值
 if(ForPos==0) //如果没有-号
   ForPos=200; //将ForPos设置成一个不可能的值

 if(Pos>SubPos)
   Pos=SubPos;
 if(Pos>PluPos)
   Pos=PluPos;
 if(Pos>MulPos)
   Pos=MulPos;
 if(Pos>DivPos)
   Pos=DivPos;
 if(Pos>ForPos)
   Pos=ForPos;
 return Pos;
}

char CDot04_20View::AnyFirstF(CString Str)
{
int SubPos=Str.Find('-')+1;
 int PluPos=Str.Find('+')+1;
 int MulPos=Str.Find('*')+1;
 int DivPos=Str.Find('/')+1;

 if(SubPos==0)
   SubPos=200;
 if(PluPos==0)
   PluPos=200;
 if(MulPos==0)
   MulPos=200;
 if(DivPos==0)
   DivPos=200;

 char Result='-';
 int tempPos=SubPos;
 if(PluPos<tempPos)
   {
    Result='+';
    tempPos=PluPos;
   }
 if(MulPos<tempPos)
   {
    Result='*';
    tempPos=MulPos;
   }
 if(DivPos<tempPos)
   {
    Result='/';
    tempPos=DivPos;
   }

 return Result;
}

int CDot04_20View::SubCompute(CString Str)
{CString Middle="";
 CString Mul2="";
 CString Right="";

 //定位第一个^号位置 ,计算乘方
 int First=Str.Find("^")+1;
 if(First) //循环计算乘方
   {
     CString tempStr=Str.Mid(0,First-1);
    int        temp=AnyLastPos(tempStr);
    CString Left=Str.Mid(0,temp);
    CString Mul1 =Str.Mid(temp,First-temp-1);
    tempStr=Str.Mid(First,Str.GetLength()-First);
    temp=AnyFirstPos(tempStr);
    if(temp==200)
     {
      Mul2=tempStr;
      Right="";
     }
    else
     {
      Mul2 =tempStr.Mid(0,temp-1);
      Right=tempStr.Mid(temp-1,tempStr.GetLength()-temp+1);
     }
    Middle.Format("%d",(int)(pow(atof(Mul1),atof(Mul2))));
    Str=Left+Middle+Right;

    First=Str.Find("^")+1;
   }

 //定位第一个*号或/号的位置
 int MulPos=Str.Find('*')+1;
 int DivPos=Str.Find('/')+1;
 First=MulPos;
 if(MulPos>DivPos)
   First=DivPos;
 if(DivPos==0  && MulPos!=0)
   {
    First=MulPos;
    DivPos=2000; // 将除号所在位置设置成一个大于MulPos但又不可能的值
   }
 if(DivPos!=0  && MulPos==0)
   {
    First=DivPos; // 将乘号所在位置设置成一个大于DivPos但不可能的值
    MulPos=2000;
   }
 while(First) //循环计算乘、除
   {
    CString tempStr=Str.Mid(0,First-1);
    int        temp=AnyLastPos(tempStr);
    CString Left=Str.Mid(0,temp);
    CString Mul1 =Str.Mid(temp,First-temp-1);
    tempStr=Str.Mid(First,Str.GetLength()-First);
    temp=AnyFirstPos(tempStr);
    if(temp==200)
     {
      Mul2=tempStr;
      Right="";
     }
    else
     {
      Mul2 =tempStr.Mid(0,temp-1);
      Right=tempStr.Mid(temp-1,tempStr.GetLength()-temp+1);
     }
    if(MulPos>DivPos)
       Middle.Format("%d",(int)(strtod(Mul1.GetBuffer(Mul1.GetLength()),&stopstring)/strtod(Mul2.GetBuffer(Mul2.GetLength()),&stopstring)));
    else
	   Middle.Format("%d",(int)(strtod(Mul1.GetBuffer(Mul1.GetLength()),&stopstring)*strtod(Mul2.GetBuffer(Mul2.GetLength()),&stopstring)));

    Str=Left+Middle+Right;

    MulPos=Str.Find('*')+1;
    DivPos=Str.Find('/')+1;
    First=MulPos;
    if(MulPos>DivPos)
       First=DivPos;

    if(DivPos==0  && MulPos!=0)
      {
       First=MulPos;
       DivPos=2000; // 将除号所在位置设置成一个大于MulPos但又不可能的值
      }
    if(DivPos!=0  && MulPos==0)
      {
       First=DivPos; // 将乘号所在位置设置成一个大于DivPos但不可能的值
       MulPos=2000;
      }
   }

 //定位+、-号首先出现的位置
 First=AnyFirstPos(Str);
 if(First==200)//如果没有+、-号,则可以直接返回结果
   return (int)strtod(Str.GetBuffer(Str.GetLength()),&stopstring);
 char Fuhao=AnyFirstF(Str); //确定首先出现的符号是+号还是-号
 while(First)
   {//如果找到+号或-号
     CString tempStr=Str.Mid(0,First-1);
     int        temp=AnyLastPos(tempStr);
     CString Left=Str.Mid(0,temp);
     CString Mul1 =Str.Mid(temp,First-temp-1);
     tempStr=Str.Mid(First,Str.GetLength()-First);
     temp=AnyFirstPos(tempStr);
     if(temp==200)
       {
        Mul2=tempStr;
        Right="";
       }
     else
       {
        Mul2 =tempStr.Mid(0,temp-1);
        Right=tempStr.Mid(temp-1,tempStr.GetLength()-temp+1);
       }
     if(Fuhao=='+')
       Middle.Format("%d",(int)(strtod(Mul1.GetBuffer(Mul1.GetLength()),&stopstring)+strtod(Mul2.GetBuffer(Mul2.GetLength()),&stopstring)));
     else
       Middle.Format("%d",(int)(strtod(Mul1.GetBuffer(Mul2.GetLength()),&stopstring)-strtod(Mul2.GetBuffer(Mul2.GetLength()),&stopstring)));
     Str=Left+Middle+Right;
     First=AnyFirstPos(Str);
     if(First==200)
       break;
     Fuhao=AnyFirstF(Str);
   }

 return (int)strtod(Middle.GetBuffer(Middle.GetLength()),&stopstring);

}

int CDot04_20View::TotalCompute(CString Str)
{int First=Str.ReverseFind('(')+1; //定位最后一个(号位置
  while(First)
{
    CString SubStr =Str.Mid(First,(Str.GetLength()-First));
    int Last= SubStr.Find(')')+1;
    Last+=First;  //定位最后一个(号以后的最开始的)号位置
    CString LeftStr=Str.Mid(0,First-1); //(号左边的字符串
  CString Middle =Str.Mid(First,Last-First-1); //()号中间的字符串
  CString Right  =Str.Mid(Last,Str.GetLength()-Last); //)号右边的字符串
    int Result=SubCompute(Middle);  //进入下面的计算
  Middle.Format("%d",Result);

  Str=LeftStr+Middle+Right;
  First=Str.ReverseFind('(')+1;
  }

  int Result=SubCompute(Str);
  return Result;

}

void CDot04_20View::OnStart() 
{if(stop==0)
{	S=1;
	m_card[1]=(CButton*)GetDlgItem(IDC_CARD2);
	m_card[2]=(CButton*)GetDlgItem(IDC_CARD3);
	m_card[3]=(CButton*)GetDlgItem(IDC_CARD4);
	m_card[4]=(CButton*)GetDlgItem(IDC_CARD5);
    m_card[1]->SetBitmap(HBITMAP(m_Bitmap[a=rand()%52]));//指向随机给出牌的数组
    m_card[2]->SetBitmap(HBITMAP(m_Bitmap[x=rand()%52]));
    m_card[3]->SetBitmap(HBITMAP(m_Bitmap[y=rand()%52]));
	m_card[4]->SetBitmap(HBITMAP(m_Bitmap[z=rand()%52]));	// TODO: Add your command handler code here
	//记录牌面的大小
	if(a<13)
	t1=a+1;
	else
		t1=a%13+1;
	if(x<13)
	t2=x+1;
	else
		t2=x%13+1;
	if(y<13)
	t3=y+1;
	else
		t3=y%13+1;
	if(z<13)
	t4=z+1;
	else
		t4=z%13+1;

    SetTimer(1,1000,NULL);//按下牌记时器开始工作
	m_time=0;
    m_input = _T("");//对编辑框清空
 CListBox *p;
 p=(CListBox *)GetDlgItem(IDC_MTD);
 p->ResetContent();
	UpdateData(false);
	m_lun=0;
	m_ju=0;
	m_lun++;
	m_score=1000;}
}

void CDot04_20View::OnNone() 
{	UpdateData(true);
	m_none = true;
    noanswer=1;
   
	h++;
	if(h%2==0)
	{m_none = false;noanswer=0; } 
	else 	m_none = true;
R.Format("%d,%d,%d,%d 无解",t1,t2,t3,t4);
	UpdateData(false);
}

void CDot04_20View::OnChangecard() 
{
	changecard a;
//a.DoModal();
if(a.DoModal()==IDOK)
//MessageBox("有解!请看答案!");
{	m_card[0]=(CButton*)GetDlgItem(IDC_CARD1);
m_card[0]->SetBitmap(HBITMAP(bbjmap[a.N]));}
//UpdateData(false);	
}

⌨️ 快捷键说明

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