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

📄 6and1dlg.cpp

📁 6+1彩票:已知中奖结果
💻 CPP
字号:
// 6and1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "6and1.h"
#include "6and1Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy6and1Dlg dialog

CMy6and1Dlg::CMy6and1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy6and1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy6and1Dlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy6and1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy6and1Dlg)
	DDX_Control(pDX, IDC_WINDOW, m_window);
	DDX_Control(pDX, IDC_RST_EXTRA, m_rst_extra);
	DDX_Control(pDX, IDC_RST_BASIC, m_rst_basic);
	DDX_Control(pDX, IDC_TOTAL, m_total);
	DDX_Control(pDX, IDC_TITLE, m_title);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy6and1Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy6and1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ACTION, OnAction)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy6and1Dlg message handlers

//分割字符串
int CMy6and1Dlg::SplitString(CString& str,TCHAR cTok,CStringArray& aryItem)   
{   
	TCHAR* p = str.GetBuffer(0);   
	TCHAR* e =  p;   
	TCHAR cEnd = *e;   
	int nCount = 0;   
	while(cEnd)   
	{   
		if(*e==_T('\0'))   
			cEnd=*e;   
		else if(*e==cTok)   
			*e= _T('\0');   

		if(*e)   
			e++;   
		else   
		{   
			if(*p!=_T('\0'))   
			{   
				aryItem.Add(p);   
				nCount++;   
			} 
			p=++e;   
		}
	}   
	return nCount;   
}

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//m_total.bkColor( RGB(236,5,4) );
	//m_total.textColor( RGB(255,255,255) );
    //m_total.setFont( -17 );

	m_charTemp=' ';	//数据分隔符
	m_success=false;
	m_start=false;
	m_save=false;

	//读取配置
	m_success=InitConfig();

	//主标题
	m_strTemp.Format("%s%d+%d兑奖",win_title,num_basic,num_extra);
	m_title
		.SetFontName("隶书")
		.SetFontSize(40)
		.SetTextColor(RGB(255,255,255))
		.SetFontUnderline(TRUE)
		.SetBkColor(RGB(254,0,0))
		.SetFontItalic(FALSE)
		.SetFontBold(TRUE)
		.SetBorder(FALSE)
		.SetSunken(FALSE)
		.SetText(m_strTemp);
	m_strTemp="";

	if(m_success)
	{
		//初始化数据
		lottery=new LOTTERY[max_total];
		m_success=InitData();

		//中奖结果显示
		for(int i=0;i<=num_basic+num_extra;i++)
		{
			if(i<num_basic)
			{
				m_strTemp=m_strTemp+" "+rst_basic[i];
			}
			else
			{
				if(i==num_basic)
				{
					m_rst_basic.SetWindowText(m_strTemp);
					m_strTemp="";
				}
				else if(i==num_basic+num_extra)
				{
					m_rst_extra.SetWindowText(m_strTemp);
				}
				m_strTemp=m_strTemp+" "+rst_extra[i-num_basic];
			}
		}
		m_strTemp="";

		//彩票投注数据显示
		for(int j=0;j<num_total;j++)
		{
			CString number="";
			for(int k=0;k<num_basic+num_extra;k++)
			{
				number+=" "+lottery[j].number[k];
			}
			m_strTemp.Format("第%04d注:%s",j+1,number);
			int nLength=m_window.SendMessage(WM_GETTEXTLENGTH);
			int nStart=nLength;
			if(nLength>=30000)
			{
				nStart=0;
			}
			m_window.SetSel(nLength, nLength);
			m_window.ReplaceSel(m_strTemp+"\n");
		}
		m_strTemp="";
		//总注数
		m_strTemp.Format("%d注",num_total);
		m_total.SetWindowText(m_strTemp);
		m_strTemp="";
	}

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMy6and1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CMy6and1Dlg::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
	{
		CPaintDC dc(this); // device context for painting

		//Draw the background
		CBitmap m_bitmap;
		CDC memDC;
		memDC.CreateCompatibleDC(&dc);
		m_bitmap.LoadBitmap(IDB_MAINBG);
		memDC.SelectObject(&m_bitmap);
		BITMAP bm;
		m_bitmap.GetObject(sizeof(BITMAP), &bm);
		dc.BitBlt(0,0,bm.bmWidth,bm.bmHeight,&memDC,0,0,SRCCOPY);
		m_bitmap.DeleteObject();
		memDC.DeleteDC();

		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMy6and1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

bool CMy6and1Dlg::InitConfig()
{
	//读取配置文件
	CString  FilePath = ".\\config.ini";
	CString strValue  = _T("");
	int  lenBuf=128;
	char inBuf[128];

	//设置
	GetPrivateProfileString ("General","title", NULL, inBuf, lenBuf, FilePath);	//主标题
	win_title=inBuf;
	max_total=GetPrivateProfileInt("General","max",512,FilePath);				//允许的最大数量
	num_basic=GetPrivateProfileInt("General","basic",0,FilePath);				//基本号数量
	num_extra=GetPrivateProfileInt("General","extra",0,FilePath);				//特别号数量
	
	//中奖结果
	GetPrivateProfileString ("General","result", NULL, inBuf, lenBuf, FilePath);
	CStringArray strList;
	strValue=inBuf;
	int size=SplitString(strValue,m_charTemp,strList);
	if(size!=num_basic+num_extra)
	{
		MessageBox("配置文件config.ini里基本号/特别号数量设置错误,或者开奖结果错误!");
		return false;
	}
	for(int i=0;i<size;i++)
	{
		//截取空格
		strList[i].TrimLeft();
		strList[i].TrimRight();
		//不足2位数字前面补0
		while(strList[i].GetLength()<2)
		{
			strList[i]="0"+strList[i];
		}
		if(i<num_basic)
		{
			rst_basic[i]=strList[i];
		}
		else
		{
			rst_extra[i-num_basic]=strList[i];
		}
	}

	return true;
}

bool CMy6and1Dlg::InitData()
{
	bool flag=true;
	//读取彩票数据///////////////////////////////////////////////////////////////////////////////
	FILE *fp=fopen("data.txt","r");
	if (fp==NULL)
	{
		return 0;
	}
	char buf[128];
	int line=0;
	while(!feof(fp))
	{
		//逐行读取数据存于buf中
		char *p=fgets(buf,128,fp);
		if (p!=NULL)
		{
			//允许的最大注数不足
			if(line>=max_total)
			{
				MessageBox("配置文件中max数值比实际数据值小,请重新配置!");
				flag=false;
				break;
			}
			
			CString number=buf;
			TRACE("第%d注:%s",number);
			number.TrimLeft();
			number.TrimRight();

			//处理彩票数据//////
			CStringArray strList;
			int size=SplitString(number,m_charTemp,strList);
			if(size!=num_basic+num_extra)
			{
				m_strTemp.Format("第%d注彩票[%s]无效!",line,number);
				//MessageBox(m_strTemp);
				flag=false;
				break;
			}
			for(int i=0;i<size;i++)
			{
				//截取空格
				strList[i].TrimLeft();
				strList[i].TrimRight();
				//不足2位数字前面补0
				while(strList[i].GetLength()<2)
				{
					strList[i]="0"+strList[i];
				}
				//取投注号
				lottery[line].number[i]=strList[i];
			}

			memset(buf,0,128);
			line++;
		}
	}
	fclose(fp);
	num_total=line;	//投注总数

	if(!flag)
	{
		MessageBox("读入彩票数据错误!");
	}
	return flag;
}

void CMy6and1Dlg::OnAction() 
{
	// TODO: Add your control notification handler code here
	if(!m_success)
	{
		MessageBox("程序配置或数据读取错误!");
		return;
	}

	m_window.SetWindowText("");
	for(int i=0;i<num_total;i++)
	{
		int nCount=0;
		//匹配基本号
		if(i==5)
			int test=0;
		for(int j=0;j<num_basic+num_extra;j++)
		{
			for(int k=0;k<num_basic;k++)
			{
				if(lottery[i].number[j]==rst_basic[k])	{ nCount++;break; }
			}
		}
		lottery[i].rst_basic=nCount;

		nCount=0;
		//匹配特别号
		for(int j=0;j<+num_basic+num_extra;j++)
		{
			for(int k=0;k<num_extra;k++)
			{
				if(lottery[i].number[j]==rst_extra[k])	{ nCount++;break; }
			}
		}
		lottery[i].rst_extra=nCount;

		//显示匹配结果
		CString number="";
		for(int k=0;k<num_basic+num_extra;k++)
		{
			number+=" "+lottery[i].number[k];
		}
		m_strTemp.Format("第%04d注:%s,结果:%d+%d",i+1,number,lottery[i].rst_basic,lottery[i].rst_extra);
		int nLength=m_window.SendMessage(WM_GETTEXTLENGTH);
		int nStart=nLength;
		if(nLength>=30000)
		{
			nStart=0;
		}
		m_window.SetSel(nStart, nLength);
		m_window.ReplaceSel(m_strTemp+"\n");
	}

	m_start=true;
}

void CMy6and1Dlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	if(!m_start)
	{
		MessageBox("请先点击开始按钮!");
		return;
	}

	FILE *fp=fopen("结果.xls","a");
	if((!feof(fp)))
	{
		CString writeLine;
		//输出标题
		for(int m=0;m<num_basic+num_extra;m++)
		{
			m_strTemp.Format("	投注号%d",m+1);
			writeLine+=m_strTemp;
		}
		writeLine+="	基本号中奖数	特别号中奖数\n";
		fputs(writeLine,fp);

		//输出结果
		for(int i=0;i<num_total;i++)
		{
			writeLine="";
			for(int j=0;j<num_basic+num_extra;j++)
			{
				writeLine+="	"+lottery[i].number[j];
			}
			m_strTemp.Format("	%d	%d\n",lottery[i].rst_basic,lottery[i].rst_extra);
			writeLine+=m_strTemp;
			fputs(writeLine,fp);
		}
	}
	fclose(fp);
}

BOOL CMy6and1Dlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	delete[] lottery;

	return CDialog::DestroyWindow();
}

⌨️ 快捷键说明

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