1、计算机高级语言课程设计报告-学生成绩管理系统(MFC)班级:201131学号:20131000349姓名:马瑞琪日期:2013年1月17号一 课程设计题目与要求(1)增加记录:要求可以连续增加多条记录。(2)删除一个学生的记录:要求可以先查找,再删除.删除前,要求用户确认.(3)成绩修改:若输入错误可进行修改;要求可以先查找,再修改.(4)查找:可以根据姓名(或学号)查找某个学生的课程成绩,查找某门课程成绩处于指定分数段内的学生名单等等.(5)排序功能:要求按总分进行排序(从高到低),若总分相同,则按数学排序;若总分和数学相同,则按物理排序;若总分和各科成绩都相同,则按学号排序;(7)文件操作
2、:可以打开文件,显示班级的所有学生信息;可以将增加或修改后的成绩重新写入文件;可以将排序好的信息写入新的文件。 二 需求分析利用MFC来实现这一程序,需要定义一个数组,来贮存学生的信息,并且这个数组在整个程序里面都要能够用到。实现录入这个功能,需要添加一个列表控件,并且对列表进行初始化,设置列表的风格和大小.实现添加这个功能,需要增加几个列表的控件变量,来接受要添加的新的学生信息。实现修改这一功能,需要用添加的几个控件变量来接受要修改的信息,并且储存在数组中,然后将修改后的信息显示在列表上。实现排序这一功能,需要把数组中的数进行比较,然后将排完序的成绩显示在列表上。实现删除这一功能,需要把删除
3、的那个人用下一个人的信息把这个人的信息代替,然后显示在列表中。三 概要设计主界面退出排序删除修改查找录入该程序有六大模块:1. 录入模块;2. 查找模块;3. 修改模块;4. 删除模块;5. 排序模块;6. 退出模块; 四 详细设计1:界面设计源代码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) = ID
4、M_ABOUTBOX);ASSERT(IDM_ABOUTBOX 0xF000);CRect rect; m_programLangList.GetClientRect(rect); / 为列表视图控件添加全行选中和栅格风格 m_programLangList.SetExtendedStyle(m_programLangList.GetExtendedStyle() LVS_EX_FULLROWSELECT LVS_EX_GRIDLINES); m_programLangList。InsertColumn(0, _T(”学号), LVCFMT_CENTER, rect。Width()/6, 0)
5、; m_programLangList.InsertColumn(1, _T(姓名), LVCFMT_CENTER, rect。Width()/6, 1); m_programLangList.InsertColumn(2, _T(”数学), LVCFMT_CENTER, rect。Width()/6, 2); m_programLangList。InsertColumn(3, _T(英语”), LVCFMT_CENTER, rect。Width()/6, 3); m_programLangList。InsertColumn(4, _T(物理”), LVCFMT_CENTER, rect.Wi
6、dth()/6, 4); m_programLangList。InsertColumn(5, _T(总分”), LVCFMT_CENTER, rect。Width()/6, 5); CMenu pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL)CString strAboutMenu;strAboutMenu。LoadString(IDS_ABOUTBOX);if (!strAboutMenu。IsEmpty()pSysMenu-AppendMenu(MF_SEPARATOR);pSysMenuAppendMenu(MF_STRING,
7、IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the applications main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ TODO: Add extra initialization herereturn TRUE; / return TRUE unless you
8、set the focus to a control界面设计效果;2,.类的函数成员域数据成员struct studentstring name;string num;int mat;int phy;int eng;int total_score;;class CMyDlg : public CDialog/ Constructionpublic:CMyDlg(CWnd pParent = NULL);/ standard constructor/ Dialog Data/AFX_DATA(CMyDlg)enum IDD = IDD_MY_DIALOG ;CListCtrlm_programL
9、angList;CStringm_name;intm_phy;intm_total_score;intm_mat;intm_eng;CStringm_num;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CMyDlg)protected:virtual void DoDataExchange(CDataExchange* pDX);/ DDX/DDV support/AFX_VIRTUAL void show(); void sort(); void research();struct stude
10、nt a100;int n ;3.录入模块;void CMyDlg::OnButton1() show();/ TODO: Add your control notification handler code herevoid CMyDlg::show() ifstream ifile;ifile。open(成绩原数据.txt”);int i=0;while(1)ifileai。numai.nameai。matai。engai.phy;i+;if(ifile.eof()!=0) break; n=i;m_programLangList。DeleteAllItems();for(i=n2;i=0
11、;i)CString c1,c2,c3,c4;c1.Format(_T(%d),ai.mat);c2.Format(_T(%d),ai.eng);c3.Format(_T(”%d”),ai.phy);ai.total_score=ai。mat+ai。eng+ai。phy;c4.Format(_T(%d”),ai.total_score);m_programLangList.InsertItem(0, _T(ai。num。c_str()); m_programLangList.SetItemText(0, 1,_T(ai。name。c_str(); m_programLangList.SetIt
12、emText(0, 2, _T(c1);m_programLangList。SetItemText(0, 3, _T(c2); m_programLangList.SetItemText(0, 4, _T(c3);m_programLangList.SetItemText(0, 5, _T(c4));ifile。close();4.查找模块;void CREA::OnOK() / TODO: Add extra validation hereUpdateData(true); int i=0;while (i!=n)string ch = m_num;string c=m_name;if(ch
13、=pi.numc=pi。name)CString c1,c2,c3,c4;c1。Format(_T(”d),pi。eng);c2.Format(_T(”%d),pi。phy);c3.Format(_T(d),pi。mat);c4。Format(_T(”%d”),pi。total_score);m_crea。InsertItem(0, _T(pi。num。c_str(); m_crea.SetItemText(0, 1,_T(pi.name。c_str(); m_crea.SetItemText(0, 2, _T(c1);m_crea。SetItemText(0, 3, _T(c2); m_cr
14、ea。SetItemText(0, 4, _T(c3));m_crea.SetItemText(0, 5, _T(c4);break;+i;if(i=n1)m_crea。DeleteAllItems();UpdateData(true);MessageBox(没有这个学号!”);5。修改模块;void CMyDlg::Oncha() UpdateData(TRUE);int i=0;while (i!=n)string c=m_num;if(c=ai。num) ai.num=m_num; ai.name=m_name; ai。mat=m_mat; ai。eng=m_eng; ai。phy=m_
15、phy;ai。total_score=m_total_score;break;/ TODO: Add extra validation herei+;UpdateData(false); m_programLangList.DeleteAllItems();for(i=0;in1;i+)CString c1,c2,c3,c4;c1.Format(_T(d”),ai。mat);c2。Format(_T(d”),ai.eng);c3.Format(_T(d”),ai。phy);c4.Format(_T(d”),ai。total_score);m_programLangList.InsertItem
16、(0, _T(ai。num.c_str()); m_programLangList。SetItemText(0, 1,_T(ai。name。c_str(); m_programLangList。SetItemText(0, 2, _T(c1));m_programLangList。SetItemText(0, 3, _T(c2); m_programLangList。SetItemText(0, 4, _T(c3);m_programLangList。SetItemText(0, 5, _T(c4);ofstream ofile;ofile.open(修改后的成绩数据.txt”);int k;
17、for(k=0;k=n2;k+)ofile ”ak.num ak。name” ”ak。mat” ”ak。phy ak。engendl;7. 删除模块;void CMyDlg:Ondel() int i,j,m;for(m=0;m=1;m+)string c=m_num;for(i=0;i=n;i+)if(c=ai。num)n;for(j=i;j=n;j+) aj=aj+1; m_programLangList。DeleteAllItems();UpdateData(true); for(i=0;in1;i+)CString c1,c2,c3,c4;c1.Format(_T(%d”),ai.ma
18、t);c2。Format(_T(d”),ai。eng);c3.Format(_T(”%d”),ai。phy);c4.Format(_T(%d”),ai.total_score);m_programLangList。InsertItem(i, _T(ai。num.c_str()); m_programLangList.SetItemText(i, 1,_T(ai。name.c_str(); m_programLangList。SetItemText(i, 2, _T(c1);m_programLangList。SetItemText(i, 3, _T(c2); m_programLangList
19、。SetItemText(i, 4, _T(c3));m_programLangList.SetItemText(i, 5, _T(c4);8. 排序模块;void CMyDlg::sort()int i,k,m;student t;i=n+1;for(k=0;ki2;k+)for(m=k+1;mi2;m+)if(ak。total_scoream.total_score)t=ak;ak=am;am=t;else if(ak。total_score=am。total_score)if(ak。matam。mat)t=ak;ak=am;am=t;else if(ak。mat=am。mat)if(ak
20、。phyam。phy)t=ak;ak=am;am=t;else if(ak。phy=am。phy)if(ak。engam。eng)t=ak;ak=am;am=t; m_programLangList.DeleteAllItems(); for(i=n-2;i=0;i-)CString c1,c2,c3,c4;c1。Format(_T(”d”),ai。mat);c2。Format(_T(”d),ai。eng);c3。Format(_T(”%d”),ai。phy);c4。Format(_T(”%d),ai.total_score);m_programLangList。InsertItem(0, _
21、T(ai。num。c_str()); m_programLangList。SetItemText(0, 1,_T(ai。name.c_str())); m_programLangList。SetItemText(0, 2, _T(c1));m_programLangList.SetItemText(0, 3, _T(c2)); m_programLangList。SetItemText(0, 4, _T(c3);m_programLangList.SetItemText(0, 5, _T(c4)); ofstream ofile;ofile。open(”排序后的成绩数据。txt”);int l
22、;for(l=0;l=n-2;l+)ofile” ”al.num al。name ”al。mat al.phy” al。engendl;void CMyDlg::Onsort() sort();/ TODO: Add your control notification handler code here9. 退出模块;void CMyDlg::OnButton2() EndDialog(0); / TODO: Add your control notification handler code here 五 测试1 录入2。查找3.修改“马晴”4,。排序六 结论实现了MFC设计,可以一键删除,
23、修改,排序,录入。不足:没有实现模糊查找。七 附录源代码:/ 学生成绩管理系统Dlg.cpp : implementation fileinclude ”stdafx。h”includefstream#include 学生成绩管理系统.h”include ”学生成绩管理系统Dlg。h”include LoadIcon(IDR_MAINFRAME);void CMyDlg::DoDataExchange(CDataExchange pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CMyDlg)DDX_Control(pDX, IDC_LIST1, m_
24、programLangList);DDX_Text(pDX, IDC_name, m_name);DDX_Text(pDX, IDC_phy, m_phy);DDX_Text(pDX, IDC_plus, m_total_score);DDX_Text(pDX, IDC_mat, m_mat);DDX_Text(pDX, IDC_eng, m_eng);DDX_Text(pDX, IDC_Num, m_num);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CMyDlg, CDialog)/AFX_MSG_MAP(CMyDlg)ON_WM_SYSCOMMAND()ON_WM_P
25、AINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_add, Onadd)ON_BN_CLICKED(IDC_BUTTON1, OnButton1)ON_BN_CLICKED(IDC_sort, Onsort)ON_BN_CLICKED(IDC_search, Onsearch)ON_BN_CLICKED(IDC_del, Ondel)ON_BN_CLICKED(IDC_cha, Oncha)ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)ON_BN_CLICKED(IDC_BUTTON2, OnButton2)/A
26、FX_MSG_MAPEND_MESSAGE_MAP()/ CMyDlg message handlersBOOL 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);CRect rect; m_programLangList.GetC
27、lientRect(rect); / 为列表视图控件添加全行选中和栅格风格 m_programLangList。SetExtendedStyle(m_programLangList。GetExtendedStyle() LVS_EX_FULLROWSELECT LVS_EX_GRIDLINES); m_programLangList。InsertColumn(0, _T(学号”), LVCFMT_CENTER, rect.Width()/6, 0); m_programLangList。InsertColumn(1, _T(姓名”), LVCFMT_CENTER, rect。Width()/6
28、, 1); m_programLangList.InsertColumn(2, _T(数学), LVCFMT_CENTER, rect。Width()/6, 2); m_programLangList。InsertColumn(3, _T(英语”), LVCFMT_CENTER, rect.Width()/6, 3); m_programLangList。InsertColumn(4, _T(物理”), LVCFMT_CENTER, rect。Width()/6, 4); m_programLangList。InsertColumn(5, _T(”总分”), LVCFMT_CENTER, re
29、ct.Width()/6, 5); CMenu pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL)CString strAboutMenu;strAboutMenu。LoadString(IDS_ABOUTBOX);if (!strAboutMenu。IsEmpty()pSysMenu-AppendMenu(MF_SEPARATOR);pSysMenuAppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework
30、 does this automatically/ when the applications main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small icon/ TODO: Add extra initialization herereturn TRUE; / return TRUE unless you set the focus to a controlvoid CMyDlg::OnSysCommand(UINT nID, LPARAM lPar
31、am)if (nID 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog: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
32、framework.void CMyDlg:OnPaint() if (IsIconic())CPaintDC dc(this); / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(rect)
33、;int x = (rect。Width() cxIcon + 1) / 2;int y = (rect。Height() cyIcon + 1) / 2;/ Draw the icondc。DrawIcon(x, y, m_hIcon);elseCDialog: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;/D
34、EL void CMyDlg:Onadd() /DEL /DEL / TODO: Add your control notification handler code here/DEL /DEL void CMyDlg:Onadd() / TODO: Add your control notification handler code hereUpdateData(true);an1.name=m_name;an-1。num=m_num;an1。eng=m_eng;an1。mat=m_mat;an1。phy=m_phy;an-1。total_score=m_mat+m_eng+m_phy;n+
35、;CString c1,c2,c3,c4;c1。Format(_T(”d”),an2。mat);c2。Format(_T(d),an2.eng);c3。Format(_T(”d”),an2.phy);c4.Format(_T(”d”),an2。total_score);m_programLangList。InsertItem(n-2, _T(an-2.num。c_str()); m_programLangList.SetItemText(n2, 1,_T(an2.name.c_str(); m_programLangList。SetItemText(n-2, 2, _T(c1);m_programLangList。SetItemText(n2, 3, _T(c2); m_programLangList.SetItemText(n-2, 4, _T(c3);m_programLangList。SetItemText(n2, 5, _T(c4);ofstream ofile;ofile.open(”添加后的成绩数据。txt”);int k;for(k=0;k=n2;k+)ofile” ”ak.num ak.name” ak.mat ”ak。phy” ak。engendl;void CMyDlg:OnButton1