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

📄 clsballot.cpp

📁 利用MFC,实现投票系统
💻 CPP
字号:
// clsBallot.cpp : implementation file
//
#include "stdafx.h"
#include "ballot.h"
#include "clsBallot.h"
#include "ballotDlg.h"
#include "clsInput.h"
#include "clsMain.h"
#include "clsDlgEdit.h"
#include "clsSelect.h"
#include "clsSelectBallo.h"

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

/////////////////////////////////////////////////////////////////////////////
// clsBallot dialog


clsBallot::clsBallot(CWnd* pParent /*=NULL*/)
	: CDialog(clsBallot::IDD, pParent)
{
	//{{AFX_DATA_INIT(clsBallot)
	m_Value_Txt = _T("");
	//}}AFX_DATA_INIT
}


void clsBallot::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(clsBallot)
	DDX_Control(pDX, IDC_FIND_INPUT, m_Edit_Find);
	DDX_Control(pDX, IDC_LIST2, m_list);
	DDX_Text(pDX, IDC_FIND_INPUT, m_Value_Txt);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(clsBallot, CDialog)
	//{{AFX_MSG_MAP(clsBallot)
	ON_BN_CLICKED(IDC_BUTTON_INPUT, OnButtonInput)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit)
	ON_BN_CLICKED(IDBACK, OnBack)
	ON_BN_CLICKED(IDC_RADIO_NUM, OnRadioNum)
	ON_BN_CLICKED(IDC_RAD_NAME, OnRadName)
	ON_BN_CLICKED(IDC_BUTTON_FIND, OnButtonFind)
	ON_BN_CLICKED(IDC_BUTTON_INPUT5, OnShowSelect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// clsBallot message handlers

void clsBallot::OnCancel() 
{
//	CBallotDlg ballot;
	CDialog::EndDialog(0);
//	ballot.DoModal();
}

void clsBallot::OnButtonInput() 
{
	clsBallot::OnCancel();
	clsInput input;
	input.DoModal();
}

BOOL clsBallot::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	return CDialog::Create(IDD, pParentWnd);
}

BOOL clsBallot::OnInitDialog() 
{
	CStdioFile readFile;
	CString    strText;
	CDialog::OnInitDialog();
    m_list.ResetContent();//清除列表框中的所有数据
    if(readFile.Open("candidate.dat",CStdioFile::modeRead | CStdioFile::typeText)==TRUE)
	{
		while(readFile.ReadString(strText))
		{
			m_list.AddString(strText);
		}
		readFile.Close();
	}	
	intCheckFlag=0;
	// TODO: Add extra initialization here
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void clsBallot::OnButtonDelete() 
{
	// TODO: Add your control notification handler code here
	CString Temp;
	int intCount=0;
	int b=0;
	CFile addFile;
	if(m_list.GetTextLen(0)==-1)
	{
		MessageBox("没有候选人信息记录,不能进行删除操作","电子投票系统",MB_OK);
	}
	else
	{
		if(MessageBox("你真的要删除该条信息吗?","电子投票系统",MB_YESNO|MB_ICONQUESTION)==IDYES)
			{
				//从列表框中移出该条记录
			   int b=m_list.GetCurSel();
			   if(b==-1)
			   {
					MessageBox("没有选择要删除的候选人信息记录,不能进行删除操作","电子投票系统",MB_OK);
			   }
			   else
			   {
				   m_list.DeleteString(m_list.GetCurSel());
				   addFile.Open("candidate.dat",CFile::modeCreate|CFile::modeWrite);
				   intCount=m_list.GetCount();
				   for(int i=0;i<intCount;i++)
				   {
					   //从列表框中重新写入文件
					   m_list.GetText(i,Temp);
					   addFile.Write((Temp+"\r\n"),strlen(Temp+"\r\n"));
				   }
				   addFile.Close();
				   UpdateData(TRUE);
				MessageBox("信息已经被成功删除","电子投票系统",MB_OK);
			   }
		}
	}
}

void clsBallot::OnBack() 
{
	CBallotDlg test;
	CDialog::EndDialog(0);
	test.DoModal();
}

void clsBallot::OnButtonEdit() 
{
    CStringArray temp;
	CString strText;
	clsDlgEdit show;
	if(m_list.GetTextLen(0)==-1)
	{
		MessageBox("没有候选人信息记录,不能进行修改操作","电子投票系统",MB_OK);
	}
	else
	{
		int b=m_list.GetCurSel();
	    if(b==-1)
		{
		   MessageBox("没有选择要修改的候选人信息记录,不能进行修改操作","电子投票系统",MB_OK);
		}
		else
		{
			m_list.GetText(m_list.GetCurSel(),strText);
		    decodeCString(strText+",",temp);
		    show.str_Num=(CString)temp[0];
		    show.str_Name=(CString)temp[1];
		    show.str_synopsis=(CString)temp[2];
		    show.str_enounce=(CString)temp[3];
		    show.int_List_Index=m_list.GetCurSel();
		    clsBallot::OnCancel();
	        show.DoModal();
		}
	}
}

/*参数说明:
source: 需要分割的源字符串;
dest: 存储分割后的各个字符串变量的目标变量;
division:分割字符*/
void clsBallot::decodeCString(CString source, CStringArray & dest)
{
   char division=',';
	dest.RemoveAll();
   for(int i=0;i<source.GetLength();i++)
   {
	   if(source.GetAt(i)== division)
	   {
		  dest.Add(source.Left(i)); //去掉右边
		  for(int j=0;j<(dest.GetSize()-1);j++)
		  {
			  dest[dest.GetSize()-1] = dest[dest.GetSize()-1].Right(dest[dest.GetSize()-1].GetLength()-dest[j].GetLength()-1); //去掉左边
		  }
	   }
   }
}

void clsBallot::OnRadioNum() 
{
	intCheckFlag=1;
	m_Edit_Find.SetFocus();
}

void clsBallot::OnRadName() 
{
   intCheckFlag=2;
   m_Edit_Find.SetFocus();
}

void clsBallot::OnButtonFind() 
{
    int intCount=0;
	CString Temp;
	CStdioFile readFile;
	CStringArray arryFind;
	CStringArray aryTemp;
	CString show_Txt;
    clsSelectBallo sel;
	UpdateData(TRUE);
	if(intCheckFlag==0)
	{
		MessageBox("请选择查询条件后,在进行查询操作","电子投票系统",MB_OK);
		m_Edit_Find.SetFocus();
	}
	else if(strlen(m_Value_Txt)==0)
	{
		MessageBox("查询信息请不要输入空","电子投票系统",MB_OK);
		m_Edit_Find.SetFocus();
	}
	else
	{
		if(readFile.Open("data.dat",CStdioFile::modeRead | CStdioFile::typeText)!=TRUE)
		{
			MessageBox("没有该候选人的投票记录,不能进行查询操作","电子投票系统",MB_OK);
			m_Edit_Find.SetFocus();
			m_Value_Txt="";
			UpdateData(FALSE);

		}
		else
		{
			CString temp;
			while(readFile.ReadString(temp))
			{
				if(strlen(temp)!=0)
				{
					aryTemp.Add(temp);
				}
			}
			
			if(intCheckFlag==1)
			{
				intCount=0;
				int c=aryTemp.GetSize();
				for(int i=0;i<aryTemp.GetSize();i++)
				{
					CString l=(CString)aryTemp[i]+",";
					decodeCString(l,arryFind);
					if(arryFind[0]==m_Value_Txt)
					{
						sel.intCount=i;
						sel.num=arryFind[0];
						sel.name=arryFind[1];
						sel.sum=arryFind[2];
						sel.ary.Copy(aryTemp);
						readFile.Close();
						intCount=-1;
						CDialog::OnCancel();
					    sel.DoModal();
					}	
				}
                if(intCount==0)
				{
					MessageBox("没有你要查询的候选人信息记录","电子投票系统",MB_OK);
					m_Edit_Find.SetFocus();
					m_Value_Txt="";
					UpdateData(FALSE);
				}

			}
			else if(intCheckFlag==2)
			{
				intCount=0;
				int c=aryTemp.GetSize();
				for(int i=0;i<aryTemp.GetSize();i++)
				{
					CString l=(CString)aryTemp[i]+",";
					decodeCString(l,arryFind);
					if(arryFind[1]==m_Value_Txt)
					{
						sel.intCount=i;
						sel.num=arryFind[0];
						sel.name=arryFind[1];
						sel.sum=arryFind[2];
						sel.ary.Copy(arryFind);
						readFile.Close();
						intCount=-1;
						CDialog::OnCancel();
						sel.DoModal();
					}	
				}
                if(intCount==0)
				{
					MessageBox("没有你要查询的候选人信息记录","电子投票系统",MB_OK);
					m_Edit_Find.SetFocus();
					m_Value_Txt="";
					UpdateData(FALSE);
				}
			
			}
		}
	}
}

void clsBallot::OnShowSelect() 
{
	clsSelect show;
	CDialog::OnCancel();
	show.DoModal();
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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