代码分析.docx
《代码分析.docx》由会员分享,可在线阅读,更多相关《代码分析.docx(50页珍藏版)》请在冰点文库上搜索。
代码分析
该对话框代码功能简介:
把输入的各章节及个章节的小知识点保存到指定数据表中。
下面是该对话框头文件的解释
//CStkDlg2对话框
classCStkDlg2:
publicCDialog
{
DECLARE_DYNAMIC(CStkDlg2)
public:
CStkDlg2(CWnd*pParent=NULL);//标准构造函数
virtual~CStkDlg2();//对话框析构函数
//对话框数据
enum{IDD=IDD_DIALOG2};//该对话框的ID标识符
public:
CComboBox*pB1,*pB2;//定义两个组合框指针变量
CEdit*pE1,*pE2;//定义两个文本编辑框变量
CListBox*pL1,*pL2;//定义两个列表框指针变量
_RecordsetPtrm_pZJ;//ADO动态数据表对象
_RecordsetPtrm_pZSD;
_variant_ttheValue;//适应读写数据表全部数据类型的对象
BOOLbSuccess1;//打开组卷结果数据表的标志变量
BOOLbSTZSD_A,bSTZSD_B,bSTZSD_C,
bSTZSD_D,bSTZSD_E,bSTZSD_F,
bSTZSD_G,bSTZSD_H,bSTZSD_I,
bSTZSD_J,bSTZSD_K,bSTZSD_L,
bSTZSD_M,bSTZSD_N,bSTZSD_O,
bSTZSD_P,bSTZSD_Q,bSTZSD_R,
bSTZSD_S,bSTZSD_T,bSTZSD_U;
//分解知识点字符串的函数
CStringZsdTempFz(constCString,char*);
//保存知识点的函数
intZsdSave(constCString,constCString);
//对话框初始化成员函数
virtualBOOLOnInitDialog();
protected:
//MFC中响应UpdateData的数据交换函数
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDV支持
//消息映射宏声明
DECLARE_MESSAGE_MAP()
public:
CComboBoxm_ZJ_COMB;//章节组合框控件变量
public:
CEditm_ZJ_EDIT;//章节文本框控件变量
public:
CStringm_ZJNR_EDIT;//保存章节内容的变量
public:
CListBoxm_ZJ_LIST;//章节列表框控件变量
public:
CStringm_ZJNR_LIST;//章节内容变量
public:
CStringm_ZJNR_COMB;//章节内容组合框变量
public:
CComboBoxm_ZSD_COMB;//知识点组合框变量
public:
CStringm_ZSDNR_COMB;//知识点内容组合框
public:
CStringm_ZSDNR_EDIT;//保存知识点内容的变量
public:
CListBoxm_ZSD_LIST;//知识点列表框变量
public:
CStringm_ZSDNR_LIST;//知识点内容变量
public:
CListBoxm_ZJZSD_LIST;//章节知识点列表框变量
public:
CStringm_ZJZSDNR_LIST;//章节知识点变量
public:
afx_msgvoidOnBnClickedButton1();
public:
//响应6个命令按钮的消息函数声明
afx_msgvoidOnBnClickedButton2();
public:
afx_msgvoidOnBnClickedButton3();
public:
afx_msgvoidOnBnClickedButton4();
public:
afx_msgvoidOnBnClickedButton6();
public:
afx_msgvoidOnBnClickedButton5();
};
//StkDlg2.cpp:
实现文件
//
#include"stdafx.h"
#include"TY_STK.h"
#include"StkDlg2.h"
//CStkDlg2对话框
//实现动态加载对话框宏
IMPLEMENT_DYNAMIC(CStkDlg2,CDialog)
//对话框构造函数定义
CStkDlg2:
:
CStkDlg2(CWnd*pParent/*=NULL*/)
:
CDialog(CStkDlg2:
:
IDD,pParent)
m_ZJNR_EDIT(_T(""))
m_ZJNR_LIST(_T(""))
m_ZJNR_COMB(_T(""))
m_ZSDNR_COMB(_T(""))
m_ZSDNR_EDIT(_T(""))
m_ZSDNR_LIST(_T(""))
m_ZJZSDNR_LIST(_T(""))
{
bSuccess1=FALSE;
bSTZSD_A=bSTZSD_B=bSTZSD_C=FALSE;
bSTZSD_D=bSTZSD_E=bSTZSD_F=FALSE;
bSTZSD_G=bSTZSD_H=bSTZSD_I=FALSE;
bSTZSD_J=bSTZSD_K=bSTZSD_L=FALSE;
bSTZSD_M=bSTZSD_N=bSTZSD_O=FALSE;
bSTZSD_P=bSTZSD_Q=bSTZSD_R=FALSE;
bSTZSD_S=bSTZSD_T=bSTZSD_U=FALSE;
m_pZJ=m_pZSD=FALSE;
pL1=pL2=NULL;
}
//对话框析构函数定义
CStkDlg2:
:
~CStkDlg2()
{
if(bSuccess1==TRUE)
{
m_pZJ->Close();//关闭章节数据表
}
if(m_pZSD)
{
m_pZSD->Close();//关闭知识点数据表
}
pB1=pB2=NULL;
pE1=pE2=NULL;
pL1=pL2=NULL;
}
voidCStkDlg2:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
DDX_Control(pDX,IDC_COMBO2,m_ZJ_COMB);
DDX_Control(pDX,IDC_EDIT1,m_ZJ_EDIT);
DDX_Text(pDX,IDC_EDIT1,m_ZJNR_EDIT);
DDX_Control(pDX,IDC_LIST1,m_ZJ_LIST);
DDX_LBString(pDX,IDC_LIST1,m_ZJNR_LIST);
DDX_CBString(pDX,IDC_COMBO2,m_ZJNR_COMB);
DDX_Control(pDX,IDC_COMBO1,m_ZSD_COMB);
DDX_CBString(pDX,IDC_COMBO1,m_ZSDNR_COMB);
DDX_Text(pDX,IDC_EDIT2,m_ZSDNR_EDIT);
DDX_Control(pDX,IDC_LIST2,m_ZSD_LIST);
DDX_LBString(pDX,IDC_LIST2,m_ZSDNR_LIST);
}
//对话框初始化代码
BOOLCStkDlg2:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//六个指针初始化
pB1=(CComboBox*)GetDlgItem(IDC_COMBO2);
pB2=(CComboBox*)GetDlgItem(IDC_COMBO1);
pE1=(CEdit*)GetDlgItem(IDC_EDIT1);
pE2=(CEdit*)GetDlgItem(IDC_EDIT2);
pL1=(CListBox*)GetDlgItem(IDC_LIST1);
pL2=(CListBox*)GetDlgItem(IDC_LIST2);
//初始化章节组合框,添加下列信息,任用户选用
pB1->AddString(_T("第一章"));
pB1->AddString(_T("第二章"));
pB1->AddString(_T("第三章"));
pB1->AddString(_T("第四章"));
pB1->AddString(_T("第五章"));
pB1->AddString(_T("第六章"));
pB1->AddString(_T("第七章"));
pB1->AddString(_T("第八章"));
pB1->AddString(_T("第九章"));
pB1->AddString(_T("第十章"));
pB1->AddString(_T("第十一章"));
pB1->AddString(_T("第十二章"));
pB1->AddString(_T("第十三章"));
pB1->AddString(_T("第十四章"));
pB1->AddString(_T("第十五章"));
pB1->AddString(_T("第十六章"));
/////////////////////////////////////////////
////////打开数据表STZJ
if(bSuccess1==FALSE)
{
try
{//打开试题库记录集
m_pZJ.CreateInstance("ADODB.Recordset");
m_pZJ->Open("SELECT*FROMSTZJ",theApp.m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
bSuccess1=TRUE;
}
catch(_com_errore)//处理异常
{
AfxMessageBox(_T("不能打开《试题章节》记录集"));
}
}
/////////////////////////////////
//打开知识点各个数据表...
/////////////////////////////////
returnTRUE;
}
BEGIN_MESSAGE_MAP(CStkDlg2,CDialog)
ON_BN_CLICKED(IDC_BUTTON1,&CStkDlg2:
:
OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2,&CStkDlg2:
:
OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3,&CStkDlg2:
:
OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4,&CStkDlg2:
:
OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON6,&CStkDlg2:
:
OnBnClickedButton6)
ON_BN_CLICKED(IDC_BUTTON5,&CStkDlg2:
:
OnBnClickedButton5)
END_MESSAGE_MAP()
//CStkDlg2消息处理程序
//单击“添加”按钮,把输入的章节内容添加到列表框中
voidCStkDlg2:
:
OnBnClickedButton1()
{
CStringtemp,t1,t2;
pB1->GetWindowTextW(t1);//获取章节
if(t1.IsEmpty())
{
AfxMessageBox(_T("咋没选章节号?
....."));
return;
}
pE1->GetWindowTextW(t2);//获取章节内容
temp=t1+_T("")+t2;
pL1->AddString(temp);//添加到列表框中
}
voidCStkDlg2:
:
OnBnClickedButton2()
{
intIndex=pL1->GetCurSel();
pL1->DeleteString(Index);//删除列表框选中的某项(输错了)
}
//确认章节输入完毕按钮
voidCStkDlg2:
:
OnBnClickedButton3()
{
inti,flag,Position,len;
CStringtemp;
flag=MessageBox(_T("确认否?
"),_T("录入章节数据存盘确认"),MB_YESNO);
if(flag!
=6)//如果不确认,到此结束...
{
AfxMessageBox(_T("输入参数不合适,重新输入..."));
return;
}
//下面的循环,把输入到列表框中内容全部添加到“所属章节”列表框中
for(i=0;iGetCount();i++)
{
pL1->GetText(i,temp);
pB2->AddString(temp);
}
//开始向数据表STZJ中存放章节数据....
intCount=pB2->GetCount();
m_pZJ->MoveFirst();
for(i=0;i{
pB2->GetLBText(i,temp);
len=temp.GetLength();
Position=temp.FindOneOf(_T(""));
temp=temp.Mid(Position+1,len-Position);
try
{
m_pZJ->PutCollect(_T("JCZJ"),(_bstr_t)temp);
}
catch(_com_errore)
{AfxMessageBox(e.ErrorMessage());}
m_pZJ->MoveNext();
}
m_pZJ->Update();//数据存盘
}
//各章节知识点“添加”按钮
voidCStkDlg2:
:
OnBnClickedButton4()
{
CStringtemp,t1,t2;
pB2->GetWindowTextW(t1);//选中哪一章
if(t1.IsEmpty())
{
AfxMessageBox(_T("咋没选章节?
....."));
return;
}
pE2->GetWindowTextW(t2);//获取某小节知识点输入内容
temp=t1+_T("")+t2;//链接在一起后,添加到下面列表框中
pL2->AddString(temp);
}
voidCStkDlg2:
:
OnBnClickedButton6()
{
//删除选中列表框的某项(输错了)
intIndex=pL2->GetCurSel();
pL2->DeleteString(Index);
}
voidCStkDlg2:
:
OnBnClickedButton5()
{
//本函数将知识点存入数据表,很重要欧...
intflag;
CStringSJBM;
CStringtemp;
charch;
flag=MessageBox(_T("确认否?
"),_T("录入知识点数据存盘确认"),MB_YESNO);
if(flag!
=6)//如果不组卷,到此结束...
{
AfxMessageBox(_T("输入参数不合适,重新输入..."));
return;
}
for(inti=0;iGetCount();i++)
{
pL2->GetText(i,temp);
temp=ZsdTempFz(temp,&ch);
SJBM.Format(_T("STZSD_%c"),ch);//合并的是存储知识点的数据表名..
ZsdSave(temp,SJBM);//知识点保存了..
}
}
//分解知识点字符串的函数,返回分解后的纯知识点
//第1个参数是该知识点所属章节
CStringCStkDlg2:
:
ZsdTempFz(CStringt,char*TempPosition)
{
intlen,Position1,Position2;
CStringtemp,tf;
len=t.GetLength();
Position1=t.FindOneOf(_T(""));
tf=t.Left(Position1);
temp=t.Mid(Position1+1,len-Position1);
Position2=temp.FindOneOf(_T(""));
temp=temp.Mid(Position2+1,len-Position2);
//分解出章节//如果是第一章,则返回’A’
if(tf.Find(_T("第一章"),0)!
=-1)
*TempPosition='A';
elseif(tf.Find(_T("第二章"),0)!
=-1)
*TempPosition='B';
elseif(tf.Find(_T("第三章"),0)!
=-1)
*TempPosition='C';
elseif(tf.Find(_T("第四章"),0)!
=-1)
*TempPosition='D';
elseif(tf.Find(_T("第五章"),0)!
=-1)
*TempPosition='E';
elseif(tf.Find(_T("第六章"),0)!
=-1)
*TempPosition='F';
elseif(tf.Find(_T("第七章"),0)!
=-1)
*TempPosition='G';
elseif(tf.Find(_T("第八章"),0)!
=-1)
*TempPosition='H';
elseif(tf.Find(_T("第九章"),0)!
=-1)
*TempPosition='I';
elseif(tf.Find(_T("第十章"),0)!
=-1)
*TempPosition='J';
elseif(tf.Find(_T("第十一章"),0)!
=-1)
*TempPosition='K';
elseif(tf.Find(_T("第十二章"),0)!
=-1)
*TempPosition='L';
elseif(tf.Find(_T("第十三章"),0)!
=-1)
*TempPosition='M';
elseif(tf.Find(_T("第十四章"),0)!
=-1)
*TempPosition='N';
elseif(tf.Find(_T("第十五章"),0)!
=-1)
*TempPosition='O';
elseif(tf.Find(_T("第十六章"),0)!
=-1)
*TempPosition='P';
elseif(tf.Find(_T("第十七章"),0)!
=-1)
*TempPosition='Q';
elseif(tf.Find(_T("第十八章"),0)!
=-1)
*TempPosition='R';
elseif(tf.Find(_T("第十九章"),0)!
=-1)
*TempPosition='S';
elseif(tf.Find(_T("第二十章"),0)!
=-1)
*TempPosition='T';
elseif(tf.Find(_T("第二十一章"),0)!
=-1)
*TempPosition='U';
elseif(tf.Find(_T("第二十二章"),0)!
=-1)
*TempPosition='V';
elseif(tf.Find(_T("第二十三章"),0)!
=-1)
*TempPosition='W';
elseif(tf.Find(_T("第二十四章"),0)!
=-1)
*TempPosition='X';
elseif(tf.Find(_T("第二十五章"),0)!
=-1)
*TempPosition='Y';
elseif(tf.Find(_T("第二十六章"),0)!
=-1)
*TempPosition='Z';
else
*TempPosition=-1;//这是溢出了,如何处理?
...
returntemp;
}
//该函数功能:
将各章知识点存入数据表STZSD_A.....STZSD_Z
//入口参数:
Zsd保存应存的知识点,TableName:
指Zsd应存到哪一个表
//出口参数:
return1指Zsd正确保存
intCStkDlg2:
:
ZsdSave(CStringZsd,CStringTableName)
{
if(TableName.Compare(_T("STZSD_A"))==0)
{
if(bSTZSD_A==FALSE)
{
try
{//打开试题库记录集
m_pZSD.CreateInstance("ADODB.Recordset");
m_pZSD->Open("SELECT*FROMSTZSD_A",theApp.m_pConnection.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdText);
bSTZSD_A=TRUE;
}
catch(_com_errore)//处理异常
{
AfxMessageBox(_T("不能打开《STZSD_A》记录集"));
}
}
}
elseif(TableName.Compare(_T("STZSD_B"))==0)
{
if(bSTZSD_B==FALSE)
{
try
{//打开试题库记录集
m_pZSD.CreateInstance("ADODB.Recordset");
m_pZSD-