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

📄 examdlg.cpp

📁 简单的考试管理系统程序的数据库是用的ODBC Acess驱动的数据源,使用前要在系统中添加4个系统数据源.下面以添加user数据源为例说明一下添加方法:控制面板-->管理工具-->数据源ODBC选择
💻 CPP
字号:
// ExamDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "DBLogin.h"
#include "ExamDlg.h"
#include "RegProFileDlg.h"
#include "Customer.h"
#include "DBLoginDlg.h"
#include "test.h"
#include "score.h"
#include "testTemp.h"
#include ".\examdlg.h"


// ExamDlg 对话框

IMPLEMENT_DYNAMIC(ExamDlg, CDialog)
ExamDlg::ExamDlg(CWnd* pParent/*=NULL*/)
	: CDialog(ExamDlg::IDD, pParent)
	, m_Question(_T(""))
	, m_Answer_A(_T(""))
	, m_Answer_B(_T(""))
	, m_Answer_C(_T(""))
	, m_Answer_D(_T(""))
	, m_Radio(0)
	, m_UserAnswer(_T(""))
	, m_Score(0)
{	
		pDB = new CDatabase();		
	try{
		if(!pDB->OpenEx("DSN=test"))
			AfxMessageBox("Error,Can't open database");
		pSet = new Ctest(pDB);
		pSet->Open(CRecordset::dynaset);
	}catch(CException *e){
		e->ReportError();
	}
	this->RandomGetTest();
	this->AddTempTest();
	SetTest();	
}
void ExamDlg::RandomGetTest()		//随机抽取试题
{
	int i,j;
	int maxTest;
	pSet->MoveLast();
	maxTest = pSet->m_ID;
	for(i=0;i<5;i++)
	{
		
loop:	srand((UINT)GetCurrentTime());
		this->TestNumber[i] = 1+rand()%maxTest;
			for(j=0;j<i;j++)
			{
				if(TestNumber[i]==TestNumber[j])
					goto loop;
			}
	}
}
void ExamDlg::WriteAnswer()
{
	UpdateData(true);
	A[pTSet->m_ID-1]=this->m_UserAnswer;
}

void ExamDlg::AddTempTest()
{
	int i(0);
	pTDB = new CDatabase();
	try{
		if(!pTDB->OpenEx("DSN=testTemp"))
			AfxMessageBox("Error,Can't open database");
		pTSet = new CtestTemp(pTDB);
		pTSet->Open(CRecordset::dynaset);
	}catch(CException *e){
		e->ReportError();
	} 
	for(pTSet->MoveFirst();!pTSet->IsEOF();pTSet->MoveNext())
	{
		if(pTSet->CanUpdate())
		{
			pSet->SetAbsolutePosition(this->TestNumber[i]);
			pTSet->Edit();
			pTSet->m_Question = pSet->m_Question;
			pTSet->m_A	=	pSet->m_A;
			pTSet->m_B	=	pSet->m_B;
			pTSet->m_C	=	pSet->m_C;
			pTSet->m_D	=	pSet->m_D;
			pTSet->m_Answer	= pSet->m_Answer;
			pTSet->m_Score  = pSet->m_Score;
			pTSet->Update();
			i++;
		}else
			AfxMessageBox("随机添加试题出错");
	}
	pTSet->MoveFirst();
}

void ExamDlg::SetTest()
{
	this->m_Question = pTSet->m_Question;
	this->m_Answer_A = pTSet->m_A;
	this->m_Answer_B = pTSet->m_B;
	this->m_Answer_C = pTSet->m_C;
	this->m_Answer_D = pTSet->m_D;
} 

ExamDlg::~ExamDlg()
{
	if(pDB->IsOpen())
		pDB->Close();
	if(pTDB->IsOpen())
		pTDB->Close();
	if(pDBs->IsOpen())
		pDBs->Close();
	if(pSet->IsOpen())
		pSet->Close();
	if(pTSet->IsOpen())
		pTSet->Close();
	if(pSets->IsOpen())
		pSets->Close();
	delete pDB;
	delete pTDB;
	delete pDBs;
	delete pSet;
	delete pTSet;
	delete pSets;
}

void ExamDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_Tester, m_Tester);
	DDX_Text(pDX, IDC_EDIT2, m_Question);
	DDX_Text(pDX, IDC_A, m_Answer_A);
	DDX_Text(pDX, IDC_B, m_Answer_B);
	DDX_Text(pDX, IDC_C, m_Answer_C);
	DDX_Text(pDX, IDC_D, m_Answer_D);
	DDX_Text(pDX, IDC_UserAnswer, m_UserAnswer);
	DDX_Text(pDX, IDC_Tester3, m_Score);
}


BEGIN_MESSAGE_MAP(ExamDlg, CDialog)
	//ON_BN_CLICKED(IDOK, OnBnClickedOk)	
	ON_BN_CLICKED(IDFIRST, OnBnClickedFirst)
	ON_BN_CLICKED(IDPREV, OnBnClickedPrev)
	ON_BN_CLICKED(IDNEXT, OnBnClickedNext)
	ON_BN_CLICKED(IDEND, OnBnClickedEnd)
	ON_BN_CLICKED(IDC_RadA, OnBnClickedRada)
	ON_BN_CLICKED(IDC_RadB, OnBnClickedRadb)
	ON_BN_CLICKED(IDC_RadC, OnBnClickedRadc)
	ON_BN_CLICKED(IDC_RadD, OnBnClickedRadd)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()


// ExamDlg 消息处理程序



int ExamDlg::GetScore()
{	
	int Score(0);
	for(pTSet->MoveFirst();!pTSet->IsEOF();pTSet->MoveNext())
	{
		if(A[pTSet->m_ID-1]==pTSet->m_Answer)
				Score=Score + pTSet->m_Score;
	}
	return Score;
}

void ExamDlg::OnBnClickedFirst()
{
	// TODO: 在此添加控件通知处理程序代码
	//WriteAnswer();
	pTSet->MoveFirst();
	SetTest();
	this->m_UserAnswer = A[pTSet->m_ID-1];	
	UpdateData(false);	
}

void ExamDlg::OnBnClickedPrev()
{
	// TODO: 在此添加控件通知处理程序代码
	//WriteAnswer();
	pTSet->MovePrev();
	SetTest();
	this->m_UserAnswer = A[pTSet->m_ID-1];
	UpdateData(false);
	if(pTSet->IsBOF()){
     //  WriteAnswer();
	   pTSet->MoveFirst();		
	}

}

void ExamDlg::OnBnClickedNext()
{
	// TODO: 在此添加控件通知处理程序代码
	if(pTSet->IsEOF()){
		//WriteAnswer();
        pTSet->MoveLast();
		return;
	}else {
	//WriteAnswer();
	pTSet->MoveNext();
	SetTest();
	this->m_UserAnswer = A[pTSet->m_ID-1];
	UpdateData(false);
	}
}

void ExamDlg::OnBnClickedEnd()
{
	// TODO: 在此添加控件通知处理程序代码
	//WriteAnswer();
	pTSet->MoveLast();
	SetTest();
	this->m_UserAnswer = A[pTSet->m_ID-1];
	UpdateData(false);
}

void ExamDlg::OnBnClickedRada()
{
	// TODO: 在此添加控件通知处理程序代码
	this->m_Radio=1;
	this->m_UserAnswer ='A';
	UpdateData(false);
	WriteAnswer();
	
}

void ExamDlg::OnBnClickedRadb()
{
	// TODO: 在此添加控件通知处理程序代码
	this->m_Radio=2;
	this->m_UserAnswer = 'B';
	UpdateData(false);
	WriteAnswer();
}

void ExamDlg::OnBnClickedRadc()
{
	// TODO: 在此添加控件通知处理程序代码
	this->m_Radio=3;
	this->m_UserAnswer = 'C';
	UpdateData(false);
	WriteAnswer();
}

void ExamDlg::OnBnClickedRadd()
{
	// TODO: 在此添加控件通知处理程序代码
	this->m_Radio=4;
	this->m_UserAnswer = 'D';
	UpdateData(false);
	WriteAnswer();
}

void ExamDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	//OnOK();
	//pDB->Close();	
	this->m_Score=this->GetScore();
	this->WriteScore();
	UpdateData(false);
}
void ExamDlg::WriteScore()
{
	pDBs = new CDatabase();
	try{
		if(!pDBs->OpenEx("DSN=score"))
			AfxMessageBox("Error,Can't open database");
		pSets = new Cscore(pDBs);
		pSets->Open(CRecordset::dynaset);
	}catch(CException *e){
		e->ReportError();
	} 
	int Score;
	Score = this->GetScore();
	for(pSets->MoveFirst();!pSets->IsEOF();pSets->MoveNext())
	{
		if(pSets->m_ID == this->m_Tester)
		{
			if(pSets->CanUpdate())
			{
				pSets->Edit();
				pSets->m_ID = this->m_Tester;
				pSets->m_Question1=this->TestNumber[0];
				pSets->m_Question2=this->TestNumber[1];
				pSets->m_Question3=this->TestNumber[2];
				pSets->m_Question4=this->TestNumber[3];
				pSets->m_Question5=this->TestNumber[4];
				pSets->m_Answer1=this->A[0];   	
				pSets->m_Answer2=this->A[1];   	
				pSets->m_Answer3=this->A[2];   	
				pSets->m_Answer4=this->A[3];   	
				pSets->m_Answer5=this->A[4];   	
				pSets->m_Score=Score;
				pSets->m_Comment = "NULL";
				pSets->Update();
			}else
				AfxMessageBox("写成绩出错");
			return;
		}
	}
	if(pSets->CanAppend())
	{
		pSets->AddNew();
		pSets->m_ID = this->m_Tester;
		pSets->m_ID = this->m_Tester;
		pSets->m_Question1=this->TestNumber[0];
		pSets->m_Question2=this->TestNumber[1];
		pSets->m_Question3=this->TestNumber[2];
		pSets->m_Question4=this->TestNumber[3];
		pSets->m_Question5=this->TestNumber[4];
		pSets->m_Answer1=this->A[0];   	
		pSets->m_Answer2=this->A[1];   	
		pSets->m_Answer3=this->A[2];   	
		pSets->m_Answer4=this->A[3];   	
		pSets->m_Answer5=this->A[4]; 
		pSets->m_Score = Score;
		pSets->m_Comment = "NULL";
		pSets->Update();
	}else
		AfxMessageBox("写成绩出错");
}


	

⌨️ 快捷键说明

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