// DLT_ZJCXDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DLT_ZJCX.h"
#include "DLT_ZJCXDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDLT_ZJCXDlg dialog

CDLT_ZJCXDlg::CDLT_ZJCXDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDLT_ZJCXDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDLT_ZJCXDlg)
	m_zjh = _T("01 02 03 04 05 + 06 07");
	m_qqd = _T("11 19 24 30");
	m_qqt = _T("03 07 09 14 18 20 29 31 33 34 35");
	m_hqd = _T("06");
	m_hqt = _T("04 08 09");
	m_jg = _T("小于10的号码前面加0 比如 08\r\n"\
		"号码之间空格分开 比如 01 02 03\r\n\r\n"\
		"前胆只能输入4个，后胆只能输入1个\r\n\r\n"\
		"不是胆拖的普通复试\r\n号码输入在【拖】输入框中\r\n\r\n"\
		"123等奖金，不进行合计\r\n\r\n"\
		"       预大家早日中大奖!");
	m_zj = FALSE;
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDLT_ZJCXDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDLT_ZJCXDlg)
	DDX_Text(pDX, IDC_EDIT_ZJH, m_zjh);
	DDX_Text(pDX, IDC_EDIT_QQD, m_qqd);
	DDX_Text(pDX, IDC_EDIT_QQT, m_qqt);
	DDX_Text(pDX, IDC_EDIT_HQD, m_hqd);
	DDX_Text(pDX, IDC_EDIT_HQT, m_hqt);
	DDX_Text(pDX, IDC_EDIT_JG, m_jg);
	DDX_Check(pDX, IDC_CHECK_ZJ, m_zj);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDLT_ZJCXDlg, CDialog)
	//{{AFX_MSG_MAP(CDLT_ZJCXDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_JS, OnButtonJs)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDLT_ZJCXDlg message handlers

BOOL CDLT_ZJCXDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	UpdateData(true);
	char str[256]={0,};
	GetPrivateProfileString("DLT","ZJH","",str,255,"./ini/DLT_ZJCX.ini");
	m_zjh=str;
	GetPrivateProfileString("DLT","QQD","",str,255,"./ini/DLT_ZJCX.ini");
	m_qqd=str;
	GetPrivateProfileString("DLT","QQT","",str,255,"./ini/DLT_ZJCX.ini");
	m_qqt=str;
	GetPrivateProfileString("DLT","HQD","",str,255,"./ini/DLT_ZJCX.ini");
	m_hqd=str;
	GetPrivateProfileString("DLT","HQT","",str,255,"./ini/DLT_ZJCX.ini");
	m_hqt=str;
	GetPrivateProfileString("DLT","ZJ","",str,255,"./ini/DLT_ZJCX.ini");
	m_zj=atoi(str);
	UpdateData(false);	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDLT_ZJCXDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CDLT_ZJCXDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDLT_ZJCXDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	//CDialog::OnCancel();
}

void CDLT_ZJCXDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

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

CStringArray saqq;
CStringArray sahq;

template <class Elem>
void combine(Elem a[], int n, int m, int s, int t, Elem b[],int iMode) 
{
	CString strResult = "";
	if (t > n) return; 
	if (s == m) 
	{
		for (int i = 0; i < m; i++)
		{
			char buf[10];
			sprintf(buf,"%02d ",b[i]);
			strResult += buf;
		}
		if(iMode==0)
			saqq.Add(strResult);
		if(iMode==1)
			sahq.Add(strResult);
		strResult = "";
	} 
	else 
	{
		combine(a, n, m, s, t+1, b,iMode);
		b[s] = a[t];
		combine(a, n, m, s+1, t+1, b,iMode);
	}
}

void CDLT_ZJCXDlg::OnButtonJs() 
{
	// TODO: Add your control notification handler code here
	int i=0,j=0;
	
	int izjh[7];

	int iqqd[35];
	int iqqt[35];

	int ihqd[12];
	int ihqt[12];

	int itmp[7];
	// str to array
	UpdateData(1);
	m_jg = "";
	CString strerr = "";
	if( str2array(izjh,m_zjh,7) != 7 )
		strerr="中奖号码输入错误!\r\n";
	int iqqd_gs = str2array(iqqd,m_qqd,4);
	int iqqt_gs = str2array(iqqt,m_qqt,35);
	int ihqd_gs = str2array(ihqd,m_hqd,1);
	int ihqt_gs = str2array(ihqt,m_hqt,12);
	
	if( iqqd_gs <=0 && iqqt_gs <=0 )
	{
		strerr += "前区没有输入号码!\r\n";
	}

	if(iqqd_gs + iqqt_gs < 5 && iqqd_gs + iqqt_gs>=1)
	{
		strerr += "前区没有输入号码不够5个!\r\n";
	}

	if( ihqd_gs <=0 && ihqt_gs <=0 )
	{
		strerr += "后区没有输入号码!\r\n";
	}

	if( ihqd_gs + ihqt_gs == 1 )
	{
		strerr += "后区没有输入号码不够2个!\r\n";
	}

	if(strerr.GetLength()>0)
	{
		m_jg=strerr;
		UpdateData(0);
		return;
	}
	

	//jin xing zu he
	//01 02 03 04 05 06
	//C 6 5
	//01 02 03 04 05
	//01 02 03 04 06
	//01 03 04 05 06
	saqq.RemoveAll();
	sahq.RemoveAll();
	//qian qu
	combine(iqqt,iqqt_gs,5-iqqd_gs,0,0,itmp,0);
	CString strc = "";
	CString str;
	CString strm = "";
	if(iqqd_gs>0)
	{
		for(i=0;i<iqqd_gs;i++)
		{
			str.Format("%02d ",iqqd[i]);
			strc += str;
		}
		for(i=0;i<saqq.GetSize();i++)
		{
			strm = strc;
			CString str1 = saqq.GetAt(i);
			strm += str1;
			saqq.SetAt(i,strm);
		}
	}
	//hou qu
	combine(ihqt,ihqt_gs,2-ihqd_gs,0,0,itmp,1);
	if(ihqd_gs>0)
	{
		strc = "";
		for(i=0;i<ihqd_gs;i++)
		{
			str.Format("%02d ",ihqd[i]);
			strc += str;
		}
		for(i=0;i<sahq.GetSize();i++)
		{
			strm = strc;
			CString str1 = sahq.GetAt(i);
			strm += str1;
			strm.TrimRight();
			sahq.SetAt(i,strm);
		}
	}
	
	int iz = 0;
	double zjj=0.0;
	strc = "";
	for(j =sahq.GetSize()-1;j>=0;j--)
	for(i =saqq.GetSize()-1;i>=0;i--)
	{
		CString strtmp;
		CString strqq = saqq.GetAt(i);
		strtmp = sortStr(strqq);
		strc += strtmp;
		strc += "+ ";
		CString strhq = sahq.GetAt(j);
		
		strtmp = sortStr(strhq);
		strtmp.TrimRight();
		strc += strtmp;
		int iqq = 0;
		int ihq = 0;
		for(int k=0;k<5;k++)
		{
			strm.Format("%02d",izjh[k]);
			if(strqq.Find(strm,0)!= -1)
			{
				iqq++;
			}
		}

		for(k=0;k<2;k++)
		{
			strm.Format("%02d",izjh[k+5]);
			if(strhq.Find(strm,0)!= -1)
			{
				ihq++;
			}
		}

		char jj[13][4][10]=
		{
			"5+2","【1等】","500万","800万",//0
			"5+1","【2等】","20%",  "20%+",//1
			"5+0","【3等】","5%",   "5%+",//2
			"4+2","4等","3000", "4500",//3
			"4+1","5等","600",  "900",//4
			"4+0","6等","100",  "150",//5
			"3+2","6等","100",  "150",//6
			"3+1","7等","10",   "15",//7
			"2+2","7等","10",   "15",//8
			"3+0","8等","5",    "5",//9
			"2+1","8等","5",    "5",//10
			"1+2","8等","5",    "5",//11
			"0+2","8等","5",    "5",//12
		};

		
		strm.Format("%d+%d",iqq,ihq);
		for(int mm = 12 ; mm>=0;mm--)
		{
			if(strm == jj[mm][0])
			{
				strm+= " ";
				strm+= jj[mm][1];
				strm+= " ";
				if(m_zj==false)
					strm+=jj[mm][2];
				else
					strm+=jj[mm][3];
				if(atoi(jj[mm][1])>3)
				{
					if(m_zj==false)
						zjj+= atoi(jj[mm][2]);
					else
						zjj+= atoi(jj[mm][3]);

				}
				strc += " " + strm;
				break;
			}
		}
	
		strc += "\r\n";

		iz++;
	}

	CString strZuHe = "";

	if(m_qqd.GetLength()>0) {strZuHe = "【";strZuHe += m_qqd;strZuHe += "】";}
	if(m_qqt.GetLength()>0) {strZuHe += m_qqt;strZuHe +=" + ";}

	if(m_hqd.GetLength()>0) {strZuHe += "【";strZuHe += m_hqd;strZuHe += "】";}
	if(m_hqt.GetLength()>0) {strZuHe += m_hqt;strZuHe +="\r\n\r\n";}

	int iBenJin = m_zj?iz*3:iz*2;
	strm.Format("共%d注 %d元          "
		        "奖金为 %.0f元\r\n\r\n",
				//"[纯利润为 %.0f元]\r\n\r\n",
				iz,iBenJin,zjj);//,zjj-iBenJin<=0?0:zjj-iBenJin);
	m_jg = strm+strZuHe+strc;
	UpdateData(0);
}

int CDLT_ZJCXDlg::str2array(int *array,CString str,int imax)
{
	char buf[3]={0,};
	int iLen = str.GetLength();
	if(iLen<=0) return 0;
	int k=0;
	int i=0;
	for(i=0;i<iLen-1;i++)
	{
		char ch1 = str.GetAt(i);
		char ch2 = str.GetAt(i+1);
		if( (ch1>='0' && ch1<='9') && (ch2>='0' && ch2<='9') )
		{
			buf[0]= ch1;buf[1]=ch2;buf[2]=0;
			int m = atoi(buf);
			if(m>=1 && m<=35)
			{
				array[k]=m;
				k++;
				if(k>=imax) break;
			}
		}
	}
	return k;
}

CString CDLT_ZJCXDlg::sortStr(CString str)
{
	char buf[3]={0,};
	int it[5];
	CString strRet = "";
	int ic=0;
	for(int i=0;i<str.GetLength()-1;i++)
	{
		char ch1=str.GetAt(i);
		char ch2=str.GetAt(i+1);
		if( (ch1>='0' && ch1<='9') && (ch2>='0' && ch2<='9') )
		{
			buf[0]=ch1;
			buf[1]=ch2;
			buf[2]=0;
			it[ic]=atoi(buf);
			ic++;
		}
	}
	if (ic>5) ic=5;
	
	int k,m;
	for(k=0;k<ic-1;k++) 
	for(m=k+1;m<ic;m++)
	if( it[k] > it[m] )
	{
		int ii=it[k];
		it[k]=it[m];
		it[m]=ii;
	}

	CString tmp;
	for(i=0;i<ic;i++)
	{
		tmp.Format("%02d ",it[i]);
		strRet += tmp;
	}
	return strRet;
}

void CDLT_ZJCXDlg::OnDestroy()
{
	CDialog::OnDestroy();
	UpdateData(true);
	WritePrivateProfileString("DLT","ZJH",m_zjh,"./ini/DLT_ZJCX.ini");
	WritePrivateProfileString("DLT","QQD",m_qqd,"./ini/DLT_ZJCX.ini");
	WritePrivateProfileString("DLT","QQT",m_qqt,"./ini/DLT_ZJCX.ini");
	WritePrivateProfileString("DLT","HQD",m_hqd,"./ini/DLT_ZJCX.ini");
	WritePrivateProfileString("DLT","HQT",m_hqt,"./ini/DLT_ZJCX.ini");
	CString str;
	str.Format("%d",m_zj);
	WritePrivateProfileString("DLT","ZJ",str,"./ini/DLT_ZJCX.ini");
	UpdateData(false);
}
