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

📄 combineredballdlg.cpp

📁 一个关于怎样模拟双色球彩票的代码 非常好的哦~~ 直接在VC++6 可编译
💻 CPP
字号:
// CombineRedBallDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DoubleColorBall.h"
#include "CombineRedBallDlg.h"
#include  "time.h"  
#include "CommDef.h"
#include "CommonTool.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCombineRedBallDlg dialog
// 初始化变量
CCombineRedBallDlg::CCombineRedBallDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCombineRedBallDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCombineRedBallDlg)
	m_csAnySelect6 = _T(""); // 任意6个红球
	//}}AFX_DATA_INIT

	m_bIsFrom33Select6 = false;
    m_bIsAnySelect6 = false;
}


void CCombineRedBallDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCombineRedBallDlg)
	DDX_Text(pDX, IDC_EDIT_ANY_SELECT6, m_csAnySelect6);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCombineRedBallDlg, CDialog)
	//{{AFX_MSG_MAP(CCombineRedBallDlg)
	ON_BN_CLICKED(IDC_RADIO_FROM33_SELECT6, OnRadioFrom33Select6)
	ON_BN_CLICKED(IDC_RADIO_ANY_SELECT_6, OnRadioAnySelect6)
	ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnButtonClose)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCombineRedBallDlg message handlers
// 初始化对话框
BOOL CCombineRedBallDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	// 使编辑框无效.
    GetDlgItem( IDC_EDIT_ANY_SELECT6 )->EnableWindow(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// 处理“33选6”单选按钮
void CCombineRedBallDlg::OnRadioFrom33Select6() 
{
	// TODO: Add your control notification handler code here
	m_bIsFrom33Select6 = true;	
    m_bIsAnySelect6 = false;
}

// 处理“从指定的红球中任选6个”单选按钮
void CCombineRedBallDlg::OnRadioAnySelect6() 
{
	// TODO: Add your control notification handler code here
	m_bIsAnySelect6 = true;
    m_bIsFrom33Select6 = false;

    // 使编辑框有效
    GetDlgItem( IDC_EDIT_ANY_SELECT6 )->EnableWindow( TRUE );
    GetDlgItem( IDC_EDIT_ANY_SELECT6 )->SetFocus();
}

// 开始组合红球
void CCombineRedBallDlg::OnButtonStart() 
{
	// TODO: Add your control notification handler code here
	int nRet = 0;
    CString csRet = _T("");
    
    if ( m_bIsFrom33Select6 ) // 33 选 6
    {
        int arr[RED_BALL_CNT_33];

        for ( int i = 0; i < RED_BALL_CNT_33; i++ )
        {
            arr[i] = i + 1;
        }
        
        nRet = 	CombineRed( arr, RED_BALL_CNT_33, RED_BALL_CNT_6 );        
        csRet.Format( _T("%d"), nRet );
    }

    if ( m_bIsAnySelect6 ) // 从指定的红球中任意选6
    {
        m_arnSpecifiedRedBallInfo.RemoveAll();
        m_arcsSpecifiedRedBallInfo.RemoveAll();
        
        UpdateData();

        // 检查用户输入的红球的有效性
        if ( m_csAnySelect6.IsEmpty() )
        {
            AfxMessageBox( _T("你没有指定红球,请指定!") );
            GetDlgItem( IDC_EDIT_ANY_SELECT6 )->SetFocus();
            return;
        }

        // 检查用户输入的红球的个数
        if ( m_csAnySelect6.GetLength() < 11 )
        {
            AfxMessageBox( _T("你指定的这组红球个数必须超过6个!") );
            m_csAnySelect6 = _T("");
            UpdateData( FALSE );
            GetDlgItem( IDC_EDIT_ANY_SELECT6 )->SetFocus();
            return;
        }

        // 检查用户否输入了非法字符
        bool bIsDigit = CCommonTool::CheckDigit( m_csAnySelect6, m_csAnySelect6.GetLength() );
        if ( !bIsDigit )
        {
            AfxMessageBox( _T("你指定的这组红球含有非法字符,请重新指定!") );
            m_csAnySelect6 = _T("");
            UpdateData( FALSE );
            GetDlgItem( IDC_EDIT_ANY_SELECT6 )->SetFocus();
            return;
        }

        int nResult = CCommonTool::SplitString( m_csAnySelect6, 
			                       _T(","), 
								   m_arcsSpecifiedRedBallInfo );

        // 检查用户否输入了重复的数字
        bool bIsSameNumber = CheckSameNumber( m_arcsSpecifiedRedBallInfo );
        if ( !bIsSameNumber )
        {
            AfxMessageBox( _T("你输入的红球不合法,不应该有相同的!") );            
            GetDlgItem( IDC_EDIT_ANY_SELECT6 )->SetFocus();
            return;
        }

        // 检查用户否输入了小于1或大于33的数字
        bool bIsValidRange = CheckNumberRange( m_arcsSpecifiedRedBallInfo );
        if ( !bIsValidRange )
        {
            AfxMessageBox( _T("你输入的红球不合法,应该是1~33之间的数字,包括1和33!") );            
            GetDlgItem( IDC_EDIT_ANY_SELECT6 )->SetFocus();
            return;
        }
                        
        int nRedBallValue = 0;
        CString csRedBallValue = _T("");
        for ( int j = 0; j < m_arcsSpecifiedRedBallInfo.GetSize(); j++ )
        {
            csRedBallValue = m_arcsSpecifiedRedBallInfo.GetAt( j );
            nRedBallValue = atoi( csRedBallValue );
            m_arnSpecifiedRedBallInfo.Add( nRedBallValue );
        }

        int nSize = m_arnSpecifiedRedBallInfo.GetSize();
        
        nRet = 	RedCombine( m_arnSpecifiedRedBallInfo, nSize, RED_BALL_CNT_6 );        
        csRet.Format( _T("%d"), nRet );
    }    

    CString csEndTime = _T("结束时间: ");
    CTime   oTime   = CTime::GetCurrentTime();
    CString csSystemTime = oTime.Format(_T("%Y年%m月%d日-%H时:%M分:%S秒"));
    csEndTime += csSystemTime;
    csEndTime += _T("\r\n\r\n");
    myFile.Write( csEndTime, csEndTime.GetLength() );

    CString csCombineCount = _T("    总共生成了:");
    csCombineCount += csRet;
    csCombineCount +=  _T("注");

    CString csEmptyLine = _T("\r\n");
    myFile.Write( csEmptyLine, csEmptyLine.GetLength() );
    myFile.Write( csCombineCount, csCombineCount.GetLength() );

    myFile.Close();	
	
	// 生成组合结果文件
    WinExec( _T("notepad.exe c:\\CombineResult.txt"), SW_MAXIMIZE );
}

// 组合红球
int CCombineRedBallDlg::CombineRed(int a[], int n, int m)
{
    m = m > n ? n : m;

    int* order = new int[m+1];    
    for ( int i = 0; i <= m; i++ )
    {
        order[i] = i - 1; // 注意这里order[0]=-1用来作为循环判断标识
    }
    

    int count = 0;                                
    int k = m;
    bool flag = true; // 标志找到一个有效组合
    
    // 打开文件               	            
	if( !myFile.Open( _T("c:\\CombineResult.txt"),CFile::modeReadWrite | CFile::modeCreate | CFile::modeNoTruncate) )
	{
		AfxMessageBox( _T("文件打开失败!") );
		return -1;
	}

    // 定位到文件头
	myFile.SeekToBegin();	
	
	// 清空文件内容
	myFile.SetLength( 0 );
    
    CString csStartTime = _T("开始时间: ");
    CTime   oTime   = CTime::GetCurrentTime();
    CString csSystemTime = oTime.Format(_T("%Y年%m月%d日 - %H时:%M分:%S秒"));
    csStartTime += csSystemTime;
    csStartTime += _T("\r\n");
    myFile.Write( csStartTime, csStartTime.GetLength() );

    while ( order[0] == -1 )
    {
        if ( flag ) // 输出符合要求的组合
        {   
            for ( i = 1; i <= m; i++ ) 
            {              
	            // 写文件 
                int nResult = a[order[i]];
                CString csCombineResult = _T("");
                csCombineResult.Format( _T("%d"), nResult );

                if ( i < RED_BALL_CNT_6 )
                {
                    csCombineResult += _T(",");
                }

                if ( RED_BALL_CNT_6 == i )
                {
                    csCombineResult += _T("\r\n");
                }                	            
                                                
                myFile.Write( csCombineResult, csCombineResult.GetLength() );	            
            }
            count++;
            flag = false;
        }

        order[k]++; // 在当前位置选择新的数字
        if ( order[k] == n ) // 当前位置已无数字可选,回溯
        {
            order[k--] = 0;
            continue;
        }     

        if ( k < m ) // 更新当前位置的下一位置的数字          
        {
            order[++k] = order[k-1];
            continue;
        }

        if ( k == m )
        {
            flag = true;
        }
    }
    
    delete[] order;
    return count;
}

// 红球组合
int CCombineRedBallDlg::RedCombine(CArray<int, int> &arnSpecifiedRedBall, 
								   int n, 
								   int m)
{
    m = m > n ? n : m;

    int* order = new int[m+1];    
    for ( int i = 0; i <= m; i++ )
    {
        order[i] = i - 1; // 注意这里order[0]=-1用来作为循环判断标识
    }
    

    int nCount = 0;                                
    int k = m;
    bool flag = true; // 标志找到一个有效组合
    
    // 打开文件               	            
	if( !myFile.Open( _T("c:\\CombineResult.txt"),
		              CFile::modeReadWrite | 
					  CFile::modeCreate | 
					  CFile::modeNoTruncate) )
	{
		AfxMessageBox( _T("文件打开失败!") );
		return -1;
	}

    // 定位到文件头
	myFile.SeekToBegin();	
	
	// 清空文件内容
	myFile.SetLength( 0 );
    
    CString csStartTime = _T("开始时间: ");
    CTime   oTime   = CTime::GetCurrentTime();
    CString csSystemTime = oTime.Format(_T("%Y年%m月%d日 - %H时:%M分:%S秒"));
    csStartTime += csSystemTime;
    csStartTime += _T("\r\n");
    myFile.Write( csStartTime, csStartTime.GetLength() );

    while ( order[0] == -1 )
    {
        if ( flag ) // 输出符合要求的组合
        {   
            for ( i = 1; i <= m; i++ ) 
            {
	            // 写文件
                int nResult = arnSpecifiedRedBall[order[i]];
                CString csCombineResult = _T("");
                csCombineResult.Format( _T("%d"), nResult );

                if ( i < RED_BALL_CNT_6 )
                {
                    csCombineResult += _T(",");
                }

                if ( RED_BALL_CNT_6 == i )
                {
                    csCombineResult += _T("\r\n");
                }                	            
                                                
                myFile.Write( csCombineResult, csCombineResult.GetLength() );	            
            }
            nCount++;
            flag = false;
        }

        order[k]++; // 在当前位置选择新的数字
        if ( order[k] == n ) // 当前位置已无数字可选,回溯
        {
            order[k--] = 0;
            continue;
        }     

        if ( k < m ) // 更新当前位置的下一位置的数字          
        {
            order[++k] = order[k-1];
            continue;
        }

        if ( k == m )
        {
            flag = true;
        }
    }
    
    delete[] order;
    
    return nCount;
}

// 检查输入的数字是否有相同的
bool CCombineRedBallDlg::CheckSameNumber(CStringArray &m_arcsSpecifiedRedBallInfo)
{
    if ( m_arcsSpecifiedRedBallInfo.GetSize() == 0 )
    {
        AfxMessageBox( _T("空数组") );
        return false;
    }

    for ( int i = 0; i < m_arcsSpecifiedRedBallInfo.GetSize(); i++ )
    {
        CString csRedBall1 = m_arcsSpecifiedRedBallInfo.GetAt( i );
        int nRedBall1 = atoi( csRedBall1 );

        CString csRedBall2 = _T("");
        int nRedBall2 = 0;

        if ( i < m_arcsSpecifiedRedBallInfo.GetSize() - 1 )
        {
            csRedBall2 = m_arcsSpecifiedRedBallInfo.GetAt( i + 1 );
            nRedBall2 = atoi( csRedBall2 );
        }
        
        // 表示输入的两个数字相同
        if ( nRedBall1 == nRedBall2 ) 
        {            
            return false;            
        }
    } 
    
    return true;
}

// 检查输入数字的范围
bool CCombineRedBallDlg::CheckNumberRange(CStringArray &m_arcsSpecifiedRedBallInfo)
{
    if ( m_arcsSpecifiedRedBallInfo.GetSize() == 0 )
    {
        AfxMessageBox( _T("空数组") );
        return false;
    }

    for ( int i = 0; i < m_arcsSpecifiedRedBallInfo.GetSize(); i++ )
    {
        CString csRedBall = m_arcsSpecifiedRedBallInfo.GetAt( i );
        int nRedBall = atoi( csRedBall );
        
        // 有效数字是从1到33
		if ( nRedBall < 1 || nRedBall > 33 ) 
        {            
            return false;            
        }
    }

    return true;
}

// 关闭对话框
void CCombineRedBallDlg::OnButtonClose() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnCancel();
}


⌨️ 快捷键说明

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