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

📄 编排dlg.cpp

📁 学生位置编排系统
💻 CPP
字号:
// 编排Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "编排.h"
#include "编排Dlg.h"
#include "Student.h"
#include <cstdlib>
#include "show.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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_strName = _T("");
	m_strSex = _T("");
	m_strStature = _T("");
	m_strGrade = _T("");

	n = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Text(pDX, IDC_EDIT1, m_strName);
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Control(pDX, IDC_COMBO1, m_gradectrl);
	DDX_Control(pDX, IDC_COMBO2, m_staturectrl);
	DDX_Control(pDX, IDC_COMBO3, m_sexCtrl);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_NOTIFY(NM_RCLICK, IDC_LIST1, OnRclickList1)
	ON_COMMAND(ID_DELETE, OnDelete)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::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_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	m_list.InsertColumn(0, "姓名", LVCFMT_LEFT, 100);
	m_list.InsertColumn(1, "身高", LVCFMT_LEFT, 100);
	m_list.InsertColumn(2, "性别", LVCFMT_LEFT, 100);
	m_list.InsertColumn(3, "成绩", LVCFMT_LEFT, 100);

	m_sexCtrl.InsertString(0, "男");
	m_sexCtrl.InsertString(1, "女");

	CString strStature;
	for(int i = 0; i < 70; i ++)
	{
		strStature.Format("%u cm", i+140);
		m_staturectrl.InsertString(i, strStature);
	}

	CString strgrad;
	for(i = 1; i <= 48; i ++)
	{
		strgrad.Format("%u", i);
		m_gradectrl.InsertString(i-1, strgrad);
	}

	SerializeStudentInfo(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::SerializeStudentInfo(BOOL bSave)
{
	FILE* pFile = fopen("StudentInfo.dat", "a+");
	if (pFile)
	{
		Student tem;
		
		unsigned int nNum = 0;
		char szRecv[100] = {0};
		while (!feof(pFile))
		{
			int nReadLen = fread(szRecv, sizeof(Student), 1, pFile);

			if (ferror(pFile) || nReadLen == 0)
			{
				break;
			}
			memcpy(&student[nNum], (Student*)szRecv, sizeof(Student));

			m_list.InsertItem(nNum, student[nNum].name);
			CString strTemp;
			strTemp.Format("%u cm", student[nNum].stature);
			m_list.SetItemText(nNum, 1, strTemp);
			student[nNum].sex==1 ? strTemp.Format("男"):strTemp.Format("女");
			m_list.SetItemText(nNum, 2, strTemp);
			strTemp.Format("%u", student[nNum].grade);
			m_list.SetItemText(nNum, 3, strTemp);
			nNum++;
		}

		n = nNum;
	}
}

void CMyDlg::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 CMyDlg::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();
	}
}

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

void CMyDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	//将输入的学生信息赋给student[]类
	if(n<48)
	{
		UpdateData(TRUE);
	    
	    strcpy(student[n].name,m_strName.GetBuffer(0));
	    m_strName.ReleaseBuffer();
	    
		int nSel = m_sexCtrl.GetCurSel();
		if (nSel == -1)
		{
			AfxMessageBox(_T("请选择性别!"));
			return;
		}
		student[n].sex = nSel;

		nSel = m_staturectrl.GetCurSel();
		if (nSel == -1)
		{
			AfxMessageBox(_T("请选择身高!"));
			return;
		}
		student[n].stature = nSel+140;

		nSel = m_gradectrl.GetCurSel();
		if (nSel == -1)
		{
			AfxMessageBox(_T("请选择名次!"));
			return;
		}
		student[n].grade = nSel+1;

		
		CString l;
		l.Format("%d",n);
		CString a = "已添加";
		CString b = "条记录!";
		CString r = a+l+b; 
		if (AfxMessageBox(r, MB_OKCANCEL) != IDOK)
		{
			return ;
		}

		FILE* pFile = fopen("StudentInfo.dat", "a+");
		if (pFile)
		{
			fseek(pFile, 0, SEEK_END);
			fwrite((void*)&student[n], sizeof(Student), 1, pFile);
		}
		fclose(pFile);

		m_list.InsertItem(n, student[n].name);
		CString strTemp;
		strTemp.Format("%u cm", student[n].stature);
		m_list.SetItemText(n, 1, strTemp);
		student[n].sex==0 ? strTemp.Format("男"):strTemp.Format("女");
		m_list.SetItemText(n, 2, strTemp);
		strTemp.Format("%u", student[n].grade);
		m_list.SetItemText(n, 3, strTemp);
		n++;

		// Clear
		GetDlgItem(IDC_EDIT1)->SetWindowText(_T(""));
		m_sexCtrl.SetCurSel(-1);
		m_gradectrl.SetCurSel(-1);
		m_staturectrl.SetCurSel(-1);
	}
	else
	{
		MessageBox("最多只能添加48条信息,请点击编排位置!","提示信息");
	}
}

void CMyDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	Student temp[60];
	memcpy(temp, student, sizeof(student));

//将学生信息按身高排序
	for(int i1 = 0; i1 < n; i1++){
		for(int j = i1; j < n; j++){
			if(temp[i1].stature > temp[j].stature){
				char na[20];
				strcpy(na,temp[i1].name);
				BOOL s = temp[i1].sex;
				int st = temp[i1].stature;
				int g = temp[i1].grade;
				strcpy(temp[i1].name,temp[j].name);
				temp[i1].sex = temp[j].sex;
				temp[i1].stature = temp[j].stature;
				temp[i1].grade = temp[j].grade;
				strcpy(temp[j].name,na);
				temp[j].sex = s;
				temp[j].stature = st;
				temp[j].grade = g;
			}
		}
	}
	//将实际身高转化为高矮次序1到48
	temp[0].stature = 1;
	for(int i2 = 1; i2 < n; i2++){
		if(temp[i2].stature == temp[i2-1].stature){
			temp[i2].stature = temp[i2-1].stature;
		}
		else{
			temp[i2].stature = temp[i2-1].stature + 1;
		}
	}

	//配对
	for(int i4 = 0; i4 < n; i4=i4+2)
	{
		int k = 0;
		int c = 0;
		//查找与学生i最适合的配对学生k
		if(temp[i4].sign == 0)
		{
			for(int j = i4 + 1; j < n; j++)
			{
				if((!temp[i4].sex&&temp[j].sex)||(temp[i4].sex&&!temp[j].sex))
				{
					int a = abs(temp[i4].grade - temp[j].grade);
					int b = abs(temp[i4].stature - temp[j].stature);
					
					if(a + 48 - b > c)
					{
						c = a + 48 - b;
						k = j;
					}
				}
			}
		}
		temp[i4].sign = true;
		//将学生k插到学生i与学生i+1之间,方便输出配好对的学生信息
		char na[20];
		strcpy(na,temp[k].name);
		BOOL s = temp[k].sex;
		int st = temp[k].stature;
		int g = temp[k].grade;
		for(int m = k - 1; m > i4; m--)
		{
            strcpy(temp[m+1].name,temp[m].name);
			temp[m+1].sex = temp[m].sex;
			temp[m+1].stature = temp[m].stature;
			temp[m+1].grade = temp[m].grade;
		}
		strcpy(temp[i4+1].name,na);
		temp[i4+1].sex = s;
		temp[i4+1].stature = st;
		temp[i4+1].grade = g;
		temp[i4+1].sign = true;
	}
	//显示座位信息窗口
	Show sh;
	sh.DisplayStudentInfo(temp, n);
	sh.DoModal();
}

void CMyDlg::OnRclickList1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	CMenu pMenu;
	pMenu.LoadMenu(IDR_MENU1);
	
	CMenu* pSubMenu = pMenu.GetSubMenu(0);
	CPoint ps;
	GetCursorPos(&ps);
	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN, ps.x, ps.y, this, 0);

	*pResult = 0;
}

void CMyDlg::OnDelete() 
{
	// TODO: Add your command handler code here
	int nSel = m_list.GetSelectionMark();
	CString strName = m_list.GetItemText(nSel, 0);
	CString strStature = m_list.GetItemText(nSel, 1);
	CString strSex = m_list.GetItemText(nSel, 2);
	CString strGrade = m_list.GetItemText(nSel, 3);

	BOOL bMaual ;
	strSex.Collate("男") == 0 ? bMaual = TRUE: bMaual = FALSE;
	int nstature = atoi(strStature.GetBuffer(0));
	int nGrade = atoi(strGrade.GetBuffer(0)) ;

	for(int j = 0; j < n; j++)
	{
		if( strcmp(strName.GetBuffer(0), student[j].name) == 0 && 
			nstature == student[j].stature &&
			bMaual == student[j].sex &&
			nGrade == student[j].grade)
		{
			break;
		}
	}

	if (j == n)
	{
		return;
	}

	for(int m = j - 1; m > 0; m--)
	{
		strcpy(student[m+1].name,student[m].name);
		student[m+1].sex = student[m].sex;
		student[m+1].stature = student[m].stature;
		student[m+1].grade = student[m].grade;
	}

	n--;

	m_list.DeleteItem(nSel);
	m_list.Invalidate();
}

⌨️ 快捷键说明

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