河北工业大学-离散数学综合实验二

实验三通讯录管理程序设计与实现

一、实验目的与要求

实验目的:锻炼面向对象编程与文件操作能力

实验要求:

1、熟悉C++程序设计。

2、运用OO知识对实验题目进行分析和设计。

3、进行程序编写和调试工作。

二、实验内容

设计、实现一个具有通讯信息插入、修改、删除、显示、查询和统计功能的通讯录管理程序。程序设计功能及要求:

1、人数不定,数据使用文件存放。

2、记录每位同学的学号、姓名、性别、工作单位、电话号码和E-mail地址建立单独的条目,存入数据文件。

3、可对记录中的姓名和电话号码等进行修改。

4、可增加或删除记录。

5、可显示所有保存的记录。

6、可以统计男女同学或总人数。

7、通过姓名、学号或电话号码查询到同学的条目。

8、在开始画面加入简单的菜单便于选择功能。

9、储存数据文件格式:

学号姓名性别工作单位电话号码E-mail

1 李四….….….

10、选做:加入文件加密系统,对储存数据文件进行简单的加密,要求通过直接打开储存数据文件无法获得其中的数据。但同时不影响程序对储存数据文件的调用。

三、实验方法

1、通讯录的录入:从键盘接收输入的通讯录信息,将其存储到通讯录文件中。

2、通讯录信息的修改:从键盘接收输入的需要修改通讯录的姓名,在文件

中查找是否存在相应记录,如果存在,则从键盘输入修改后的信息,并保存在文件中。

3、通讯录信息的删除:从键盘接收输入的需要删除的通讯录的姓名,在通讯录文件中进行查找,如果存在这样的记录,则在文件中将记录删除。

4、通讯录信息的增加:从键盘接收要增加的通讯录信息,并与通讯录文件中已存在的信息进行比对,如果存在这样的记录,且前后信息不符,则更新该记录,如果不存在该条记录,则将新录入的通讯录信息保存到通讯录文件中。

5、显示所有保存的记录:能显示出所有通讯录文件的所有记录。

6、统计男女生人数或总人数。

7、通讯录的查询功能:从键盘接收要查询的姓名、学号或电话号码,对通讯录文件进行查询,并将查询到整条记录显示出来。

8、该通讯录可以采用字符界面,也可以在开始画面加入简单的菜单便于选择功能。

四、实验设计

该实验通过采用ADO封装对象进行数据库的访问和操作信息存储

数据文件存放在通讯录文件夹的根目录下建议从工作台直接进入进行测试

五,源代码

// UseAdoDlg.cpp : implementation file

//

#include "stdafx.h"

#include "UseAdo.h"

#include "UseAdoDlg.h"

#include "ADO.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()

/////////////////////////////////////////////////////////////////////////////

// CUseAdoDlg dialog

CUseAdoDlg::CUseAdoDlg(CWnd* pParent /*=NULL*/)

: CDialog(CUseAdoDlg::IDD, pParent)

{

//{{AFX_DATA_INIT(CUseAdoDlg)

m_ID = _T("");

m_Name = _T("");

m_Culture = _T("");

m_Sex = _T("");

m_Bianhao = _T("");

m_Addr = _T("");

//}}AFX_DATA_INIT

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

}

void CUseAdoDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CUseAdoDlg)

DDX_Control(pDX, IDC_LIST1, m_Grid);

DDX_Text(pDX, IDC_EDIT1, m_ID);

DDX_Text(pDX, IDC_EDIT2, m_Name);

DDX_Text(pDX, IDC_EDIT3, m_Culture);

DDX_Text(pDX, IDC_EDIT4, m_Sex);

DDX_Text(pDX, IDC_EDIT6, m_Bianhao);

DDX_Text(pDX, IDC_EDIT7, m_Addr);

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CUseAdoDlg, CDialog)

//{{AFX_MSG_MAP(CUseAdoDlg)

ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_BN_CLICKED(IDC_BUTADD, OnButadd)

ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)

ON_BN_CLICKED(IDC_BUTMOD, OnButmod)

ON_BN_CLICKED(IDC_BUTDEL, OnButdel)

ON_BN_CLICKED(IDC_BUTCLEAR, OnButclear)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CUseAdoDlg message handlers

BOOL CUseAdoDlg::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_Grid.SetExtendedStyle(LVS_EX_FLATSB

|LVS_EX_FULLROWSELECT

|LVS_EX_HEADERDRAGDROP

|LVS_EX_ONECLICKACTIV ATE

|LVS_EX_GRIDLINES);

m_Grid.InsertColumn(0,"姓名",LVCFMT_LEFT,110,0);

m_Grid.InsertColumn(1,"电话",LVCFMT_LEFT,110,1);

m_Grid.InsertColumn(2,"QQ",LVCFMT_LEFT,110,2);

m_Grid.InsertColumn(3,"性别",LVCFMT_LEFT,110,3);

m_Grid.InsertColumn(4,"邮箱地址",LVCFMT_LEFT,110,4);

m_Grid.InsertColumn(5,"编号",LVCFMT_LEFT,110,5);

AddToGrid();

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

}

void CUseAdoDlg::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 CUseAdoDlg::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 CUseAdoDlg::OnQueryDragIcon()

{

return (HCURSOR) m_hIcon;

}

void CUseAdoDlg::AddToGrid()

{

ADO m_Ado;

m_Ado.OnInitADOConn();//连接数据库

CString SQL = "select * from employees order by 姓名desc"; //设置查询字符串

m_Ado.m_pRecordset = m_Ado.OpenRecordset(SQL);//打开记录集

while(!m_Ado.m_pRecordset->adoEOF)

{

m_Grid.InsertItem(0,"");

m_Grid.SetItemText(0,0,(char*)(_bstr_t)m_Ado.m_pRecordset->GetCollect("姓名"));

m_Grid.SetItemText(0,1,(char*)(_bstr_t)m_Ado.m_pRecordset->GetCollect("电话"));

m_Grid.SetItemText(0,2,(char*)(_bstr_t)m_Ado.m_pRecordset->GetCollect("QQ "));

m_Grid.SetItemText(0,3,(char*)(_bstr_t)m_Ado.m_pRecordset->GetCollect("性别"));

m_Grid.SetItemText(0,4,(char*)(_bstr_t)m_Ado.m_pRecordset->GetCollect("邮箱地址"));

m_Grid.SetItemText(0,5,(char*)(_bstr_t)m_Ado.m_pRecordset->GetCollect("编号"));

m_Ado.m_pRecordset->MoveNext();//将记录集指针移动到下一条记录}

m_Ado.CloseRecordset();

m_Ado.CloseConn();//断开数据库连接

}

void CUseAdoDlg::OnButadd()

{

// TODO: Add your control notification handler code here

UpdateData(TRUE);

if(m_ID.IsEmpty() || m_Name.IsEmpty() || m_Culture.IsEmpty())

{

MessageBox("基础信息不能为空!");

return;

}

ADO m_Ado;

m_Ado.OnInitADOConn();

CString sql = "select * from employees";

m_Ado.m_pRecordset = m_Ado.OpenRecordset(sql);

try

{

m_Ado.m_pRecordset->AddNew(); //添加新行

m_Ado.m_pRecordset->PutCollect("姓名",(_bstr_t)m_ID);

m_Ado.m_pRecordset->PutCollect("电话",(_bstr_t)m_Name);

m_Ado.m_pRecordset->PutCollect("QQ",(_bstr_t)m_Culture);

m_Ado.m_pRecordset->PutCollect("性别",(_bstr_t)m_Sex);

m_Ado.m_pRecordset->PutCollect("邮箱地址",(_bstr_t)m_Addr);

m_Ado.m_pRecordset->PutCollect("编号",(_bstr_t)m_Bianhao);

m_Ado.m_pRecordset->Update(); //更新数据表记录

m_Ado.CloseRecordset();

m_Ado.CloseConn();

}

catch(...)

{

MessageBox("操作失败");

return;

}

MessageBox("添加成功");

m_Grid.DeleteAllItems(); //删除列表控件

AddToGrid();

}

void CUseAdoDlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult) {

// TODO: Add your control notification handler code here

int pos = m_Grid.GetSelectionMark();

m_ID = m_Grid.GetItemText(pos,0);

m_Name = m_Grid.GetItemText(pos,1);

m_Culture = m_Grid.GetItemText(pos,2);

m_Sex = m_Grid.GetItemText(pos,3);

m_Addr = m_Grid.GetItemText(pos,4);

m_Bianhao = m_Grid.GetItemText(pos,5);

UpdateData(FALSE);

*pResult = 0;

}

void CUseAdoDlg::OnButmod()

{

// TODO: Add your control notification handler code here

UpdateData(TRUE);

if(m_ID.IsEmpty() || m_Name.IsEmpty() || m_Culture.IsEmpty())

{

MessageBox("基础信息不能为空!");

return;

}

int pos = m_Grid.GetSelectionMark();

ADO m_Ado;

m_Ado.OnInitADOConn();

CString sql = "select * from employees";

m_Ado.m_pRecordset = m_Ado.OpenRecordset(sql);

try

{

m_Ado.m_pRecordset->Move((long)pos,vtMissing);

m_Ado.m_pRecordset->PutCollect("姓名",(_bstr_t)m_ID);

m_Ado.m_pRecordset->PutCollect("电话",(_bstr_t)m_Name);

m_Ado.m_pRecordset->PutCollect("QQ",(_bstr_t)m_Culture);

m_Ado.m_pRecordset->PutCollect("性别",(_bstr_t)m_Sex);

m_Ado.m_pRecordset->PutCollect("邮箱地址",(_bstr_t)m_Addr);

m_Ado.m_pRecordset->PutCollect("编号",(_bstr_t)m_Bianhao);

m_Ado.m_pRecordset->Update();

m_Ado.CloseRecordset();

m_Ado.CloseConn();

}

catch(...)

{

MessageBox("操作失败");

return;

}

MessageBox("修改成功");

m_Grid.DeleteAllItems();

AddToGrid();

}

void CUseAdoDlg::OnButdel()

{

// TODO: Add your control notification handler code here

int pos = m_Grid.GetSelectionMark();

ADO m_Ado;

m_Ado.OnInitADOConn();

CString sql = "select * from employees";

m_Ado.m_pRecordset = m_Ado.OpenRecordset(sql);

try

{

m_Ado.m_pRecordset->Move(pos,vtMissing);

m_Ado.m_pRecordset->Delete(adAffectCurrent);

m_Ado.m_pRecordset->Update();

m_Ado.CloseRecordset();

m_Ado.CloseConn();

}

catch(...)

{

MessageBox("操作失败");

return;

}

MessageBox("删除成功");

OnButclear();

m_Grid.DeleteAllItems();

AddToGrid();

}

void CUseAdoDlg::OnButclear()

{

// TODO: Add your control notification handler code here m_ID = "";

m_Name = "";

m_Culture = "";

m_Sex = "";

m_Addr = "";

m_Bianhao = "";

UpdateData(FALSE);

}}

五、程序截图

七、实验总结

通过这次课程设计,使我对C++语言有了更进一步的认识和了解,了解到了mfc对数据库的基本操作知识的应用要想学好它要重在实践,要通过不断的上机操作才能更好地学习它,我也发现我的好多不足之处,对 C++语言的一些标准库函数不太了解,还有对函数调用的正确使用不够熟悉,还有对C++语言中经常出现的错误也不了解,通过实践的学习,我认识到学好计算机要重视实践操作,不仅仅是学习C++语言,还是其它的语言,以及其它的计算机方面的知识都要重在实践,所以后在学习过程中,我会更加注视实践操作,使自己便好地学好计算机。

相关文档
最新文档