📄 studentdoc.cpp
字号:
// StudentDoc.cpp : implementation of the CStudentDoc class
//
#include "stdafx.h"
#include "Student.h"
#include "StudentDoc.h"
#include "DlgSelClass.h"
#include <fstream>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStudentDoc
IMPLEMENT_DYNCREATE(CStudentDoc, CDocument)
BEGIN_MESSAGE_MAP(CStudentDoc, CDocument)
//{{AFX_MSG_MAP(CStudentDoc)
ON_COMMAND(ID_SELECT_CLASS, OnSelectClass)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStudentDoc construction/destruction
CStudentDoc::CStudentDoc()
{
// TODO: add one-time construction code here
m_Class = "";
}
CStudentDoc::~CStudentDoc()
{
}
BOOL CStudentDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CStudentDoc serialization
void CStudentDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CStudentDoc diagnostics
#ifdef _DEBUG
void CStudentDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CStudentDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CStudentDoc commands
void CStudentDoc::OnSelectClass()
{
// TODO: Add your command handler code here
CDatabase database;
CRecordset* pRecordset;
CString strSQL, str;
CDBVariant dbv;
string id, name;
float exercise, report, midterm, terminal;
int i, n;
CDlgSelClass dlg;
if (!database.Open(_T("ODBC;DSN=MS Access Database")))
{
AfxMessageBox("不能打开数据源!");
return;
}
pRecordset = new CRecordset(&database);
strSQL = "SELECT 班级名称 FROM 班级;";
if (!pRecordset->Open(CRecordset::forwardOnly, strSQL, CRecordset::readOnly))
{
AfxMessageBox("不能打开记录集!");
delete pRecordset;
database.Close();
return;
}
dlg.m_lstClass.nodeNum = 0;
while (!pRecordset->IsEOF())
{
pRecordset->GetFieldValue("班级名称", str);
pRecordset->MoveNext();
dlg.m_lstClass.nodeNum ++;
}
pRecordset->Close();
dlg.m_lstClass.pStrArr = new CString[dlg.m_lstClass.nodeNum];
pRecordset->Open(CRecordset::forwardOnly, strSQL, CRecordset::readOnly);
for (i = 0; i < dlg.m_lstClass.nodeNum; i ++)
{
pRecordset->GetFieldValue("班级名称", str);
dlg.m_lstClass.pStrArr[i] = str;
pRecordset->MoveNext();
}
pRecordset->Close();
strSQL = "SELECT 课程名称 FROM 课程;";
pRecordset->Open(CRecordset::forwardOnly, strSQL, CRecordset::readOnly);
dlg.m_lstCourse.nodeNum = 0;
while (!pRecordset->IsEOF())
{
pRecordset->GetFieldValue("课程名称", str);
pRecordset->MoveNext();
dlg.m_lstCourse.nodeNum ++;
}
pRecordset->Close();
dlg.m_lstCourse.pStrArr = new CString[dlg.m_lstCourse.nodeNum];
pRecordset->Open(CRecordset::forwardOnly, strSQL, CRecordset::readOnly);
for (i = 0; i < dlg.m_lstCourse.nodeNum; i ++)
{
pRecordset->GetFieldValue("课程名称", str);
dlg.m_lstCourse.pStrArr[i] = str;
pRecordset->MoveNext();
}
pRecordset->Close();
if (dlg.DoModal() == IDOK && dlg.m_selClass >= 0 && dlg.m_selCourse >= 0)
{
m_Class = dlg.m_lstClass.pStrArr[dlg.m_selClass];
m_Course = dlg.m_lstCourse.pStrArr[dlg.m_selCourse];
strSQL = "SELECT 学生.学号, 学生.姓名, 成绩.平时作业, 成绩.实验报告, 成绩.期中考试, 成绩.期末考试 ";
strSQL += "FROM (班级 INNER JOIN 学生 ON 班级.班级ID = 学生.班级ID) INNER JOIN (课程 INNER JOIN 成绩 ON 课程.课程ID = 成绩.课程ID) ON 学生.学生ID = 成绩.学生ID ";
strSQL += "WHERE 班级.班级名称='";
strSQL += m_Class;
strSQL += "' AND 课程.课程名称='";
strSQL += m_Course;
strSQL += "'";
i = 0;
pRecordset->Open(CRecordset::forwardOnly, strSQL, CRecordset::readOnly);
while (!pRecordset->IsEOF())
{
pRecordset->MoveNext();
i ++;
}
pRecordset->Close();
n = i;
if (n > 0)
{
Student2* pStudent = new Student2[n];
pRecordset->Open(CRecordset::forwardOnly, strSQL, CRecordset::readOnly);
for (i = 0; i < n; i++)
{
pRecordset->GetFieldValue("学号", str);
id = str;
pRecordset->GetFieldValue("姓名", str);
name = str;
pRecordset->GetFieldValue("平时作业", dbv);
exercise = dbv.m_fltVal;
pRecordset->GetFieldValue("实验报告", dbv);
report = dbv.m_fltVal;
pRecordset->GetFieldValue("期中考试", dbv);
midterm = dbv.m_fltVal;
pRecordset->GetFieldValue("期末考试", dbv);
terminal = dbv.m_fltVal;
pStudent[i].SetStudent(id, name, exercise, report, midterm, terminal);
pStudent[i].CalcMark();
pRecordset->MoveNext();
}
pRecordset->Close();
m_studentlist.SetStudentList(pStudent, n);
delete[] pStudent;
}
else
{
Student2* pStudent = new Student2[1];
id = name = "";
exercise = report = midterm = terminal = 0;
pStudent[0].SetStudent(id, name, exercise, report, midterm, terminal);
pStudent[0].CalcMark();
m_studentlist.SetStudentList(pStudent, 1);
delete[] pStudent;
}
}
else
{
AfxMessageBox("查询成绩须同时选定一个班级和一门课程。");
}
delete pRecordset;
database.Close();
delete[] dlg.m_lstClass.pStrArr;
delete[] dlg.m_lstCourse.pStrArr;
UpdateAllViews(NULL, n);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -