程序设计课程设计报告目录一、课程设计题目及内容二、程序中使用的数据及主要符号说明三、带有详细注释的自己编写的源程序四、程序运行时的效果图五、实验结果分析,实验收获和体会。
1、实验结果分析:2、实验收获和体会:一、实验内容实验1:(1)、设计一个学生类Student,包括数据成员:姓名、学号、二门课程(面向对象程序设计、高等数学)的成绩。
(2)、创建一个管理学生的类Management,包括实现学生的数据的增加、删除、修改、按课程成绩排序、保存学生数据到文件及加载文件中的数据等功能。
(3)、创建一个基于对话框的MFC应用程序,程序窗口的标题上有你姓名、学号和应用程序名称。
使用(1)和(2)中的类,实现对学生信息和成绩的输入和管理。
(4)、创建一个单文档的MFC应用程序,读取(3)中保存的文件中的学生成绩,分别用直方图和折线方式显示所有学生某课程的成绩分布图。
二、程序中使用的数据及主要符号说明unsigned int mID;//学号CString mName;//姓名unsigned int mAge;//年龄CString mAdd;//地址float mCpp;//c++成绩float mMath;//数学成绩CListBox m_list;//列表名afx_msg void OnClickedButtonAdd();//添加按钮afx_msg void OnClickedButtonDel();//删除按钮afx_msg void OnClickedButtonChange();//修改afx_msg void OnClickedButtonOk();//确定afx_msg void OnClickedButtonCancle();//取消afx_msg void OnSelchangeList1();//列表控件virtual BOOL OnInitDialog();//初始化对话框afx_msg void OnDestroy();//防止内存泄漏afx_msg void OnClickedButton6();int m_count;//记录人数int mSex;//性别三、带有详细注释的自己编写的源程序(1)、设计一个学生类Student//Student.h#pragma once#include<string>//using namespace std;#include<iostream>enum Sex { male, female };class Student{public:Student();//构造函数~Student();//析构函数unsigned int GetID()const{return m_num;}void SetID(unsigned int ID){ m_num = ID;};std::string GetName()const{return m_name;}void SetName(const std::string& name){ m_name = name;};Sex GetSex()const{return m_sex;}void SetSex(Sex sex){ m_sex = sex;};unsigned int GetAge()const{return m_age;}void SetAge(unsigned int age){ m_age = age;};std::string GetAdd()const{return m_add;}void SetAdd(std::string add){ m_add = add;};float GetCpp()const{return m_cpp;}void SetCpp(float cpp){ m_cpp = cpp;};float GetMath()const{return m_math;}void SetMath(float math){ m_math = math;};friend std::ostream&operator<<(std::ostream& os,const Student& st);friend std::istream&operator>>(std::istream& is, Student& st);private:unsigned int m_num;//学号std::string m_name;//姓名Sex m_sex;//性别unsigned int m_age;//年龄std::string m_add;//地址float m_cpp;//c++成绩float m_math;//数学成绩};//Student.cpp///////////////////////////////////////////////#include "pch.h"#include "Student.h"Student::Student()//初始化:m_num(0), m_name(""), m_sex(male), m_age(20), m_add(""), m_cpp(0.0f), m_math(0.0f){}Student::~Student(){}std::ostream&operator<<(std::ostream& os,const Student& st)//重载函数{os <<""<< st.m_num;os <<""<< st.m_name;os <<""<<(int)st.m_sex;os <<""<< st.m_age;os <<""<< st.m_add;os <<""<< st.m_cpp;os <<""<< st.m_math;return os;}std::istream&operator>>(std::istream& is, Student& st){is >> st.m_num;is >> st.m_name;int sex;is >> sex;st.m_sex = sex ==0? male : female;is >> st.m_age;is >> st.m_add;is >> st.m_cpp;is >> st.m_math;return is;}(2)、添加一个CStudentinfo类,包括实现学生的数据的增加、删除、修改、按课程成绩排序、保存学生数据到文件及加载文件中的数据等功能。
//Dlg.h#pragma once// CStudentinfo 对话框class CStudentinfo :public CDialogEx{private:unsigned int mID;//学号CString mName;//姓名unsigned int mAge;//年龄CString mAdd;//地址float mCpp;//c++float mMath;//数学CListBox m_list;//列表名称public:afx_msg void OnClickedButtonAdd();afx_msg void OnClickedButtonDel();afx_msg void OnClickedButtonChange();afx_msg void OnClickedButtonOk();afx_msg void OnClickedButtonCancle();public:afx_msg void OnSelchangeList1();virtual BOOL OnInitDialog();afx_msg void OnDestroy();afx_msg void OnClickedButton6();int m_count;人数private:int mSex;//性别};// CStudentinfo.cpp: 实现文件//#include "pch.h"#include "EX03.h"#include "afxdialogex.h"#include "Student.h"#include "CStudentinfo.h"#include <ctime>#include <fstream>// CStudentinfo 对话框IMPLEMENT_DYNAMIC(CStudentinfo, CDialogEx)//对话框初始化CStudentinfo::CStudentinfo(CWnd* pParent /*=nullptr*/) : CDialogEx(DIALOG_STUDENT, pParent), mID(0), mName(_T("")), mAge(0), mAdd(_T("")), mCpp(0), mMath(0), mSex(0), m_count(0){}// CStudentinfo 消息处理程序////////////////////////////////////////////////////////////////////添加void CStudentinfo::OnClickedButtonAdd(){// TODO: 在此添加控件通知处理程序代码UpdateData(TRUE);if(mID >0&&!mName.IsEmpty()){Student* pSt =new Student();// pSt = nullptr;pSt->SetID(mID);pSt->SetName(std::string(mName));pSt->SetSex(mSex ==0? male : female);pSt->SetAge(mAge);pSt->SetAdd(std::string(mAdd));pSt->SetMath(mMath);pSt->SetCpp(mCpp);CString temp;temp.Format(_T("%d-%s"), mID, mName);m_list.AddString(temp);m_list.SetItemDataPtr(m_list.GetCount()-1, pSt);mName ="";mAdd ="";mID =0;mAge =0;mMath =0;mCpp =0;UpdateData(false);}}//删除void CStudentinfo::OnClickedButtonDel(){// TODO: 在此添加控件通知处理程序代码int sel = m_list.GetCurSel();//获取当前对象if(sel == LB_ERR)return;auto pSt =(Student*)m_list.GetItemDataPtr(sel);delete pSt;m_list.DeleteString(sel);}//修改void CStudentinfo::OnClickedButtonChange(){// TODO: 在此添加控件通知处理程序代码int sel = m_list.GetCurSel();//获取当前对象if(sel == LB_ERR)return;auto pSt =(Student*)m_list.GetItemDataPtr(sel);UpdateData(TRUE);//更新数据pSt->SetID(mID);pSt->SetName(std::string(mName));pSt->SetSex(mSex ==0? male : female);pSt->SetAge(mAge);pSt->SetAdd(std::string(mAdd));pSt->SetCpp(mCpp);pSt->SetMath(mMath);CString temp;temp.Format(_T("%d-%s"), mID, mName);m_list.DeleteString(sel);m_list.InsertString(sel, temp);//m_list.AddString(temp);//m_list.SetItemDataPtr(m_list.GetCount() - 1, pSt);m_list.SetItemDataPtr(sel, pSt);mName ="";mAdd ="";UpdateData(false);}//确定,保存void CStudentinfo::OnClickedButtonOk(){// TODO: 在此添加控件通知处理程序代码TCHAR fileName[MAX_PATH];GetModuleFileName(nullptr, fileName, MAX_PATH);CString file = fileName;int pos = file.ReverseFind(_T('.'));file = file.Left(pos +1)+ _T("txt");std::ofstream ofile(file, std::ios::out);if(ofile){int n = m_list.GetCount();ofile << n << std::endl;for(int k =0; k < n;++k){auto pSt =(Student*)m_list.GetItemDataPtr(k);ofile <<(*pSt);}}ofile.close();CDialogEx::OnOK();}//取消void CStudentinfo::OnClickedButtonCancle(){// TODO: 在此添加控件通知处理程序代码CDialog::OnOK();}//列表事件void CStudentinfo::OnSelchangeList1(){// TODO: 在此添加控件通知处理程序代码int sel = m_list.GetCurSel();//获取当前对象if(sel == LB_ERR)return;auto pSt =(Student*)m_list.GetItemDataPtr(sel); //数据读取mID = pSt->GetID();mName = pSt->GetName().c_str();mSex = pSt->GetSex()== male ?0:1;mAge = pSt->GetAge();mAdd = pSt->GetAdd().c_str();mMath = pSt->GetMath();mCpp = pSt->GetCpp();UpdateData(false);}//添加额外初始化BOOL CStudentinfo::OnInitDialog(){CDialogEx::OnInitDialog();// TODO: 在此添加额外的初始化//读取信息TCHAR fileName[MAX_PATH];GetModuleFileName(nullptr, fileName, MAX_PATH);CString file = fileName;int pos = file.ReverseFind(_T('.'));file = file.Left(pos +1)+ _T("txt");std::ifstream ifile(file, std::ios::in);if(ifile){int n =0;ifile >> n;for(int k =0; k < n;++k){Student* pSt =new Student();ifile >>(*pSt);CString temp;temp.Format(_T("%d-%s"),pSt->GetID(), pSt->GetName().c_str());m_list.AddString(temp);m_list.SetItemDataPtr(m_list.GetCount()-1, pSt);}}return TRUE;// return TRUE unless you set the focus to a control// 异常: OCX 属性页应返回 FALSE}//防止内存泄漏void CStudentinfo::OnDestroy(){int n = m_list.GetCount();for(int k =0; k < n;++k)delete(Student*)m_list.GetItemDataPtr(k);CDialogEx::OnDestroy();// TODO: 在此处添加消息处理程序代码}//随机生成void CStudentinfo::OnClickedButton6(){// TODO: 在此添加控件通知处理程序代码UpdateData(true);for(int k =0; k < m_count;++k){auto pSt =new Student();pSt->SetID(190620000+ rand()%10000);TCHAR name[9];for(int i =0; i <8;++i){name[i]=65+ rand()%26;}name[8]= _T('\0');pSt->SetName(std::string(name));pSt->SetSex(rand()%2==0? male : female);pSt->SetAge(17+ rand()%4);pSt->SetAdd(std::string(name));pSt->SetMath(40.0f+ rand()%51);pSt->SetCpp(40.0f+ rand()%54);CString temp;temp.Format(_T("%d: %s"), pSt->GetID(), pSt->GetName().c_str());m_list.AddString(temp);m_list.SetItemDataPtr(m_list.GetCount()-1, pSt);}}(3)、创建一个基于对话框的MFC应用程序(4)、创建一个单文档的MFC应用程序,读取(3)中保存的文件中的学生成绩,分别用直方图和折线方式显示所有学生某课程的成绩分布图。