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

📄 24pointsview.cpp

📁 速算24点的C++源代码
💻 CPP
字号:
// 24PointsView.cpp : implementation of the CMy24PointsView class
//

#include "stdafx.h"
#include "24Points.h"
#include "OutPut.h"
#include "24PointsDoc.h"
#include "24PointsView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMy24PointsView

IMPLEMENT_DYNCREATE(CMy24PointsView, CView)

BEGIN_MESSAGE_MAP(CMy24PointsView, CView)
	//{{AFX_MSG_MAP(CMy24PointsView)
	ON_COMMAND(ID_VIEW_TAKEIN, OnViewTakein)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy24PointsView construction/destruction

CMy24PointsView::CMy24PointsView()
{
	// TODO: add construction code here
	expression="";
}

CMy24PointsView::~CMy24PointsView()
{
}

BOOL CMy24PointsView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy24PointsView drawing

void CMy24PointsView::OnDraw(CDC* pDC)
{
	CMy24PointsDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDC->TextOut(0,0,expression);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CMy24PointsView diagnostics

#ifdef _DEBUG
void CMy24PointsView::AssertValid() const
{
	CView::AssertValid();
}

void CMy24PointsView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMy24PointsDoc* CMy24PointsView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy24PointsDoc)));
	return (CMy24PointsDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMy24PointsView message handlers
void CMy24PointsView::F(float a, float b, float *result)
{
	*(result+0) = a*b;
	*(result+1) = a+b;
	if(b==0)
		*(result+2)=0;
	else
		*(result+2) = a/b;
	*(result+3) = a-b;
}

bool CMy24PointsView::F1(float a, float b, float c, float d)
{
	CString str1,str2,str3,str4;
	float rt1[4];
	float rt2[4];
	float rt3[4];
	int op1,op2,op3;
	op1=op2=op3=-1;
	F(a,b,&rt1[0]);
	F(c,d,&rt2[0]);
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
		{
			F(rt1[i], rt2[j],&rt3[0]);
			for(int k=0;k<4;k++)
			{
				if(rt3[k] == 24)
				{
					op1=i;op2=k;op3=j;
					str1.Format("%d",int(a));
					str2.Format("%d",int(b));
					str3.Format("%d",int(c));
					str4.Format("%d",int(d));
					if(op1==1 || op1==3){
						if(op2==0 || op2==3)
						{
							expression="("+str1+GetOperator(op1)+str2+")"+GetOperator(op2)+str3
							+GetOperator(op3)+str4;
						}
						if((op2==0 && op3!=0 && op3!=2)|| (op2==2 && op3!=0 && op3!=2) || (op2==3 && op3!=0 && op3!=2) )
							expression="("+str1+GetOperator(op1)+str2+")"+GetOperator(op2)+"("+str3
						+GetOperator(op3)+str4+")";
					}
					else if((op2==0 && op3!=0 && op3!=2)|| (op2==2 && op3!=0 && op3!=2) || (op2==3 && op3!=0 && op3!=2) )
						{
						expression=str1+GetOperator(op1)+str2+GetOperator(op2)+"("+str3
						+GetOperator(op3)+str4+")";
						}
					else
						expression=str1+GetOperator(op1)+str2+GetOperator(op2)+str3
						+GetOperator(op3)+str4;
					return true;
				}
			}
		}
	}
	return false;
}

bool CMy24PointsView::F2(float a, float b, float c, float d)
{
	CString str1,str2,str3,str4;
	float rt1[4];
	float rt2[4];
	float rt3[4];
	int op1,op2,op3;
	op1=op2=op3=-1;
	F(a,b,&rt1[0]);
	for(int i=0;i<4;i++)
	{
		F(rt1[i],c,&rt2[0]);
		for(int j=0;j<4;j++)
		{
			F(rt2[j],d,&rt3[0]);
			for(int k=0;k<4;k++)
			{
				if(rt3[k] == 24)
				{
					op1=i;op2=j;op3=k;
					str1.Format("%d",int(a));
					str2.Format("%d",int(b));
					str3.Format("%d",int(c));
					str4.Format("%d",int(d));
					
						expression=str1+GetOperator(op1)+str2+GetOperator(op2)+str3
						+GetOperator(op3)+str4;
					expression+="=24";
					return true;
				}
			}
		}
	}
	return false;
}

void CMy24PointsView::Perm(float list[],int k,int m,CDC *pDC)
{

	int i;
	if(k==m)
	{
	
		if(F1(list[0],list[1],list[2],list[3]))
		{
			expression+="=24";
			pDC->TextOut(0,0,expression);
		}
		if(F2(list[0],list[1],list[2],list[3]))
		{
			expression+="=24";
			pDC->TextOut(0,0,expression);
		}
		return;
	}
	else
		for(i=k;i<=m;i++)
		{
			Swap(list[k],list[i]);
			Perm(list,k+1,m,pDC);
			Swap(list[k],list[i]);
		}
	
}


void CMy24PointsView::Swap(float &a,float &b)
{
	float temp = a;
	a = b;
	b = temp;
}

CString CMy24PointsView::GetOperator(int op)
{
	CString str;
	switch(op)
	{
	case 0: str="*";
		break;
	case 1: str="+";
		break;
	case 2: str="/";
		break;
	case 3: str="-";
		break;
	default:str="?";
	}
	return str;
}

void CMy24PointsView::OnViewTakein() 
{
	// TODO: Add your command handler code here
	CClientDC dc(this);
	OutPut output;
	if(output.DoModal()==IDOK)
	{
	
		float list[4];
		list[0]=output.m_num1;
		list[1]=output.m_num2;
		list[2]=output.m_num3;
		list[3]=output.m_num4;
		Perm(list,0,3,&dc);
	}
	Invalidate();
}

⌨️ 快捷键说明

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