MFC之学生健康管理系统
发布时间
阅读量:
阅读量
为了实现学生健康管理系统的代码需求,在C++中开发了一个线性表类。随后计划将其转换为基于MFC的方式。目前系统功能尚不完整之处在于无法直接读取并展示文件内容。原本设想是将文件内容加载至列表框以便直观查看。然而在实际操作过程中遇到了一些问题。待进一步掌握MFC的文件操作技巧后将对现有功能进行完善。下面是现在的效果:
创建新表格的过程通常首先是在新建编辑框中输入初始人数,并在此基础上完成基本参数设置。
接着,在输入每位学生的具体信息后,“插入”按钮会被激活。
随后,在完成所有必要信息的录入后,“保存”操作将被触发。
值得注意的是,“其他功能”的操作通常也会在操作界面中有所提示。
下面是线性表类和dlg类的代码:
// TestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Stu_Healthy.h"
#include "TestDlg.h"
#include <fstream>
#include <string>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
m_birthday = _T("");
m_healthy = _T("");
m_name = _T("");
m_num = _T("");
m_pos = 0;
m_searchnum = _T("");
m_sex = _T("");
m_sum = 0;
m_once=FALSE;
m_new=-1;
m_array=0;
m_right=0;
//}}AFX_DATA_INIT
}
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
DDX_Text(pDX, IDC_EDIT_BIRTHDAY, m_birthday);
DDX_Text(pDX, IDC_EDIT_HEALTHY, m_healthy);
DDX_Text(pDX, IDC_EDIT_NAME, m_name);
DDX_Text(pDX, IDC_EDIT_NUM, m_num);
DDX_Text(pDX, IDC_EDIT_POS, m_pos);
DDX_Text(pDX, IDC_EDIT_S, m_searchnum);
DDX_Text(pDX, IDC_EDIT_SEX, m_sex);
DDX_Text(pDX, IDC_EDIT_sum, m_sum);
DDV_MinMaxInt(pDX, m_sum, 1, 50);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_BN_CLICKED(IDC_BUTTON_NEW, OnButtonNew)
ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
ON_BN_CLICKED(IDC_BUTTON_INSERT, OnButtonInsert)
ON_BN_CLICKED(IDC_BUTTON_PRINT, OnButtonPrint)
ON_BN_CLICKED(IDC_BUTTON_READ, OnButtonRead)
ON_BN_CLICKED(IDC_BUTTON_SEARCH, OnButtonSearch)
ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonUpdate)
ON_BN_CLICKED(IDC_BUTTON_WRITE, OnButtonWrite)
ON_BN_CLICKED(IDC_BUTTON_CLEAN, OnButtonClean)
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
student *stu,e;
Sqlist sq;
/
// CTestDlg message handlers
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
SetWindowText(_T("20102100227 kay"));
MessageBox("请在新建表之前先在右边的编辑框输入初始化人数(1~50)");
CRect rectLarge,rectsmall,rectseparator;
GetWindowRect(&rectLarge);
GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectseparator);
rectsmall.left=rectLarge.left;
rectsmall.top=rectLarge.top;
rectsmall.bottom=rectLarge.bottom;
rectsmall.right=rectseparator.right;
m_right=rectLarge.right-rectseparator.right;
SetWindowPos(NULL,0,0,rectsmall.Width(),rectsmall.Height(),SWP_NOMOVE);
m_brush.CreateSolidBrush(RGB(30,144,255));
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CTestDlg::OnButtonNew()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_sum==0)
{
MessageBox("请输入初始化人数,再新建表");
return;
}
if(m_once)
{
GetDlgItem(IDC_BUTTON_NEW)->EnableWindow(FALSE);
MessageBox("新建已经完成,本程序不允许重新新建,请选择其他操作");
}
else
{
m_once=true;
m_new=1;
MessageBox("在对应的信息框中输入每个学生的信息再按插入学生信息按钮完成一个个的新建");
stu=new student[m_sum];
}
}
void CTestDlg::OnButtonSearch()
{
// TODO: Add your control notification handler code here
UpdateData();
if(sq.Sq_GetLength()==0)
{
MessageBox("现在表中记录为空,请先新建表中数据再进行操作");
return ;
}
if(m_searchnum=="")
{
MessageBox("请在查询专用编辑框中输入要查询的学生的学号再按查询");
}
else
{
if(sq.Sq_Search(m_searchnum,e,m_pos))
{
MessageBox("查找成功,查找结果请看对话框的输出结果");
m_num=e.num;
m_name=e.name;
m_birthday=e.birthday;
m_sex=e.sex;
m_healthy=e.healthy;
UpdateData(FALSE);
}
else
{
MessageBox("查找的记录不存在");
}
}
}
void CTestDlg::OnButtonInsert()
{
// TODO: Add your control notification handler code here
UpdateData();
if(m_sum==0)
{
MessageBox("现在表中数据为空,请先新建表再进行操作");
return;
}
if(1==m_new)
{
if(m_array<m_sum)
{
if(m_name=="" || m_birthday=="" || m_sex=="" || m_healthy=="")
{
MessageBox("请将该学生的其他资料填完整");
}
else
{
stu[m_array].num=m_num;
stu[m_array].name=m_name;
stu[m_array].birthday=m_birthday;
stu[m_array].sex=m_sex;
stu[m_array].healthy=m_healthy;
m_array+=1;
char m[5];
itoa(m_array,m,10);
char str1[80]="第";
char str2[25]="个学生的基本信息新建完成";
strcat(str1,m);
strcat(str1,str2);
MessageBox(str1);
GetDlgItem(IDC_EDIT_NUM)->SetWindowText("");
GetDlgItem(IDC_EDIT_NAME)->SetWindowText("");
GetDlgItem(IDC_EDIT_BIRTHDAY)->SetWindowText("");
GetDlgItem(IDC_EDIT_SEX)->SetWindowText("");
GetDlgItem(IDC_EDIT_HEALTHY)->SetWindowText("");
if(m_array==m_sum)
{
m_new=0;
sq.Sq_listInit(stu,m_sum);
MessageBox("新建工作完成");
}
}
}
}
else if(0==m_new)
{
if(m_num=="")
{
MessageBox("请先输入要插入的新的学生的学号");
}
else
{
if(sq.Sq_Search(m_num,e,m_pos))
{
MessageBox("此记录在表中已经存在,不可以再重复插入");
m_num="";
}
else
{
MessageBox("请继续输入该学生的其他信息和在位置编辑框中输入要插入的位置,然后再次按下插入按键进行插入");
m_new=2;
}
}
}
else if(m_new==2)
{
e.num=m_num;
e.name=m_name;
e.birthday=m_birthday;
e.sex=m_sex;
e.healthy=m_healthy;
if(sq.Sq_Insert(m_pos,e))
MessageBox("插入成功");
m_new=0;
UpdateData(FALSE);
}
else
MessageBox("操作不合法");
}
void CTestDlg::OnButtonDel()
{
// TODO: Add your control notification handler code here
if(sq.Sq_GetLength()==0)
{
MessageBox("现在表中记录为空,请先新建表中数据再进行操作");
return ;
}
UpdateData();
if(m_pos==0)
MessageBox("请在位置对话框中输入要删除的记录为表中的第几个,再按删除按钮");
else
{
if(sq.Sq_Delete(m_pos,e))
{
MessageBox("删除成功,被删除的学生记录显示如下");
m_num=e.num;
m_name=e.name;
m_birthday=e.birthday;
m_healthy=e.healthy;
m_sex=e.sex;
UpdateData(FALSE);
}
}
}
void CTestDlg::OnButtonUpdate()
{
// TODO: Add your control notification handler code here
if(sq.Sq_GetLength()==0)
{
MessageBox("现在表中记录为空,请先新建表中数据再进行操作");
return ;
}
UpdateData();
e.num=m_num;
e.name=m_name;
e.birthday=m_birthday;
e.sex=m_sex;
e.healthy=m_healthy;
if(m_num=="")
MessageBox("请在学号编辑框中输入要更新的学生的学号,再按下更新按钮");
else
{
student temp;
if(sq.Sq_Search(m_num,temp,m_pos))
{
if(m_name=="" || m_birthday=="" || m_sex=="" || m_healthy=="")
{
MessageBox("请将该学生的其他资料填完整");
}
else
{
sq.Sq_Update(e,m_pos);
MessageBox("更新成功");
}
}
else
MessageBox("要更新的记录不存在");
}
}
void CTestDlg::OnButtonWrite()
{
// TODO: Add your control notification handler code here
if(sq.Sq_GetLength()==0)
{
MessageBox("现在表中记录为空,请先新建表中数据再进行操作");
return ;
}
if(sq.Write_into_file())
MessageBox("写入成功");
}
void CTestDlg::OnButtonRead()
{
// TODO: Add your control notification handler code here
char c[30];
int len;
CString str;
MessageBox("功能尚未完善");
/*
if(1)
{
MessageBox("读取成功,现在将显示文件中的内容");
if(GetDlgItem(IDC_BUTTON_PRINT)->GetWindowText(str),str=="输出全部信息")
{
GetDlgItem(IDC_BUTTON_PRINT)->SetWindowText("收起输出框");
}
else
{
SetDlgItemText(IDC_BUTTON_PRINT,"输出全部信息");
}
static CRect rectsmall,rectlarge;
if(str=="输出全部信息")
{
for(int i=0;i<10;i++)
{
file.get(c,30,'#');
((CListBox*)GetDlgItem(IDC_LIST))->AddString(c);
file.seekg((i+1)*(strlen(c)+2),ios::beg);
}
GetWindowRect(&rectsmall);
rectlarge.left=rectsmall.left;
rectlarge.top=rectsmall.top;
rectlarge.bottom=rectsmall.bottom;
rectlarge.right=rectsmall.right+m_right;
SetWindowPos(NULL,0,0,rectlarge.Width(),rectlarge.Height(),SWP_NOMOVE);
}
else
{
SetWindowPos(NULL,0,0,rectsmall.Width(),rectsmall.Height(),SWP_NOMOVE);
((CListBox*)GetDlgItem(IDC_LIST))->ResetContent();
}
}*/
}
void CTestDlg::OnButtonPrint()
{
// TODO: Add your control notification handler code here
if(sq.Sq_GetLength()==0)
{
MessageBox("现在表中记录为空,请先新建表中数据再进行操作");
return ;
}
CString str,temp;
if(GetDlgItem(IDC_BUTTON_PRINT)->GetWindowText(str),str=="输出全部信息")
{
GetDlgItem(IDC_BUTTON_PRINT)->SetWindowText("收起输出框");
}
else
{
SetDlgItemText(IDC_BUTTON_PRINT,"输出全部信息");
}
static CRect rectsmall,rectlarge;
if(str=="输出全部信息")
{
for(int i=0;i<sq.Sq_GetLength();i++)
{
temp=sq.Sq_Connect(i);
((CListBox*)GetDlgItem(IDC_LIST))->AddString(temp);
}
GetWindowRect(&rectsmall);
rectlarge.left=rectsmall.left;
rectlarge.top=rectsmall.top;
rectlarge.bottom=rectsmall.bottom;
rectlarge.right=rectsmall.right+m_right;
SetWindowPos(NULL,0,0,rectlarge.Width(),rectlarge.Height(),SWP_NOMOVE);
}
else
{
SetWindowPos(NULL,0,0,rectsmall.Width(),rectsmall.Height(),SWP_NOMOVE);
((CListBox*)GetDlgItem(IDC_LIST))->ResetContent();
}
}
void CTestDlg::OnButtonClean()
{
// TODO: Add your control notification handler code here
m_num="";
m_name="";
m_birthday="";
m_healthy="";
m_pos=0;
m_sex="";
m_searchnum="";
UpdateData(FALSE);
}
void CTestDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
/*
if(pWnd->GetDlgCtrlID()==IDC_EDIT_NUM || IDC_EDIT_NAME || IDC_EDIT_BIRTHDAY
|| IDC_EDIT_SEX || IDC_EDIT_HEALTHY || IDC_EDIT_S || IDC_EDIT_POS || IDC_EDIT_sum)
{
pDC->SetBkMode(TRANSPARENT);
//pDC->SetBkColor(RGB(255,55,55));
return m_brush;
}
*/
// TODO: Return a different brush if the default is not desired
return hbr;
}
/
// The Sqlist class
const int LIST_INIT_SPACE=50; //存储空间初始分配量
const int LIST_INC_SPACE=10; //分配增量
class Sqlist;
//学生信息类
class student
{
public:
CString num; //学号
CString name; //姓名
CString birthday; //出生日期
CString sex; //性别
CString healthy; //健康状况
student &operator = (student e)
{
this->num=e.num;
this->name=e.name;
this->birthday=e.birthday;
this->sex=e.sex;
this->healthy=e.healthy;
return *this;
}
friend class Sqlist;
};
class Sqlist
{
private:
student *elem; //指向学生信息数组的指针
int length; //表的当前长度
int maxlen; //表的最大长度
public:
Sqlist()
{
elem=new student[LIST_INIT_SPACE];
length=0;
maxlen=LIST_INIT_SPACE;
}
~Sqlist()
{
delete[]elem;
}
void Sq_listInit(student e[],int n) //用一个数组初始化线性表
{
if(n>maxlen)
{
loop: maxlen+=LIST_INC_SPACE;
if(n>maxlen)
goto loop; //增加长度一直到大于n为止
delete[]elem;
elem=new student[maxlen];
}
length=n;
for(int i=0;i<n;i++)
elem[i]=e[i];
}
int Sq_GetLength()
{
return length;
}
CString Sq_Connect(int i)
{
CString str;
str=elem[i].num+" "+elem[i].name+" "+elem[i].birthday+" "+elem[i].sex+" "+elem[i].healthy;
return str;
}
bool Sq_Insert(int pos,student e) //在表的第pos个元素后面插入
{
if(pos<1 || pos>length)
{
::MessageBox(NULL,"位置参数不合法","提示",MB_OK);
return false;
}
if(length==maxlen) //此时表已满
{
student *newspace=new student[LIST_INIT_SPACE+LIST_INC_SPACE]; //扩充原来的表
if(!newspace)
exit(1);
for(int i=0;i<length;i++) //将原来的元素放回新表中
newspace[i]=elem[i];
delete[]elem;
elem=newspace;
maxlen+=LIST_INC_SPACE;
}
for(int i=length-1;i>pos-1;i--)
elem[i+1]=elem[i];
elem[pos]=e;
length+=1;
return true;
}
bool Sq_Delete(int pos,student& e) //删除表中第pos个元素,值用e来装载
{
if(pos<1 || pos>length)
{
::MessageBox(NULL,"位置参数不合法","提示",MB_OK);
return false;
}
e=elem[pos-1];
for(int i=pos;i<length;i++)
elem[i-1]=elem[i];
length--;
return true;
}
bool Sq_Search(CString newnum,student& e,int& pos) //根据学号在表中查询学生信息,查询到的信息由e来装载,该节点在表中位置由pos表示
{
for(int i=0;i<length;i++)
{
if(strcmp(elem[i].num,newnum)==0)
{
e=elem[i];
pos=i+1;
return true;
}
}
return false;
}
bool Write_into_file() //向文件写入表信息
{
fstream file("data.txt",ios::out|ios::binary);
if(!file)
{
::MessageBox(NULL,"文件打开失败","提示",MB_OK);
return false;
}
for(int i=0;i<length;i++)
{
CString str;
str=elem[i].num+" "+elem[i].name+" "+elem[i].birthday+" "+elem[i].sex+" "+elem[i].healthy;
file.write(str,str.GetLength());
file.seekp(0,ios::end);
file.write("\r\n",2);
}
file.close();
return true;
}
bool Read_from_file(char c[20][100],int& len) //从文件中读取表信息
{
fstream file("data.txt",ios::in|ios::binary);
if(!file)
{
::MessageBox(NULL,"文件打开失败","提示",MB_OK);
return false;
}
char ch;
int i,j;
while(file.tellg()>0)
{
i=0;
j=0;
while(file.get(ch))
{
c[i][j]=ch;
j++;
}
i++;
}
len=i;
file.close();
return true;
}
void Sq_Update(student e,int pos) //更新指定学生的信息
{
elem[pos-1]=e;
}
};
全部评论 (0)
还没有任何评论哟~
