版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、目 录 PART I1 需求分析12 算法基本原理13 类设计24 详细设计34.1 类的接口设计44.2 类的实现64.3 主函数设计115 运行结果与分析135.1 程序运行结果135.2 运行结果分析15PART II1 类设计162 详细设计.233 运行结果31参考文献31PART I1 需求分析(1)实现年级管理(4个年级)、班级管理及学生信息管理,可以实现班级的查询、添加、删除、修改,学生成绩的查询、添加、删除、修改等。程序使用类的包含方法实现。1)一个班最多30名学生,4门课程;2)班级信息、学生成绩存放在数据文件中;3)内存中数据组织建议采用STL容器。设计班级类、学生类等,
2、建立文件、输出文件内容、计算每个学生总分并进行总分排序、查找各科最低分和最高分。(2)设计一个Student类,包含学生的基本信息:姓名、学号、四门课程(大外、高数、模电、C+)的分数和总分数;StuList类用于实现学生成绩的查找、添加、删除、修改;Class类包含班级带的基本信息:年级、班级名称和所有学生;ClList类用于实现班级的查询、添加、删除、修改。(3)用list链表容器存放多个学生的信息和多个班级的信息。使用容器的sort()函数实现学生的总分排序。Max()和Min()分别实现查找各科的最高分和最低分。2 算法基本原理一个年级包含多个班级,用list链表容器存放一个年级的所有
3、班级,用Class类存放班级的所有学生,用ClList类的成员函数对班级进行查找、添加、删除、修改。 (2)一个班级有很多学生(不超过30个),用Student类存放学生信息,StuList类带的成员函数实现学生成绩的查找、添加、删除、修改,总分的排序,求各科的最高分和最低分。(3)学会STL标准模板库里德容器、迭代器、和算法的使用方法。本程序使用List链表容器存放学生信息并进行相应的操作,如用push_back()函数进行添加操作、用sort()函数进行排序,用erase()函数进行删除操作等。(4)用文件FILE读取和输出学生信息和班级信息。3 类设计从上面的算法分析可以看到,本设计面临
4、的计算问题的关键是学生信息和班级信息的处理。可以定义一个学生类Student显示学生的基本信息,然后定义一个学生链表类StuList实现学生成绩的相关操作;又定义一个班级类Class显示班级的基本信息,最后定义一个班级链表类ClList实现班级的一些相应操作等。从问题的需要来看,需要调用STL标准模板库里德一些函数如push_back()函数进行添加操作、用sort()函数进行排序,用erase()函数进行删除操作等。 学生类和班级类还有学生链表类和班级链表类之间的相互关系如图1所示。 Student- stuname20 : char - stunum12 : char- stuscore4
5、 : float- total : float+ Student()+ Student(char *name , char *num , float s1, float s2, float s3, float s4 ) + *Getnum() : char+ *Getstuscore() : float+operator<(const Student& stu) : bool StuList - list<Student> thestu + Getthestu() : list<Student> + Add(Student stu) : void + Se
6、ek(char *num) : void + Show() : void + SorttoFile(char *filename) : void + Max() : void + Min() : void学生类和学生链表类的UML图的表示 Class - clgrade10 : char- clclass10 : char- cl : list<Student> + Class()+ Class(char *grade, char *cclass, list<Student> l)+ *Getclclass() : char+ CPrint() : void ClLis
7、t- thecl :list<Class>+ CAdd(Class cl) : void+ CSeek(char *cclass) : void+ CModify(char *cclass, Class &cl) : void+ CDelete(char *cclass) : void+ CShow() : void班级类和班级链表类的UML图形表示学生链表类需要访问学生类的私有成员,但是私有成员是不允许外部函数访问的,故在学生类当中定义了返回私有成员(stunum和stuscore)的成员函数,同理在班级类中也定义了返回(clclass)的成员函数。4 详细设计整个程序一
8、个文档,kese1.h文件中包括学生类Student、学生链表类StuList、班级类Class、班级链表类ClList的声明,kese1.Cpp文件中包含; 这四个类类的成员函数实现文件;main.cpp文件包括程序的主函数,主函数中定义了一个类StuList的对象,通过这个对象存放所有的学生并进行相关操作;比定义了ClList类的对象,通过这个对象对班级进行相关的操作。4.1 类的接口设计/kese1.h 文件,实现类的声明#include <iostream>#include <iomanip>#include <list>#include <f
9、stream>#include <algorithm>#include <cstring>using namespace std;/student.h学生类class Student;class StuList;ostream& operator<<(ostream& os, const Student& stu); /输出流文件声明class Studentprivate:char stuname20; /学生姓名char stunum12; /学生学号float stuscore4; /四门课程的成绩float total;
10、 /总分数public:Student() /构造函数Student(char *name, char *num, float s1, float s2, float s3, float s4);/构造函数void print(int n = -1); /输出函数char *Getnum() /输出学号return stunum;float *Getstuscore() /输出分数return stuscore;friend ostream& operator<<(ostream& os, const Student& stu); /读出文件bool ope
11、rator<(const Student& stu) /重载<运算符return total > stu.total;/friend class StuList; /友元类;/StuList.h 学生信息的相关处理class StuListprivate:list<Student> thestu; /链表对象public:list<Student> Getthestu() /输出链表 return thestu;void Add(Student stu); /添加学生信息void Seek(char *num); /查询学生信息,按学号查询,返
12、回记录号,-1表示没有找到void Show(); /遍历列表显示void SorttoFile(char *filename); /按学生总成绩从高到低排序并写到文件中void Max(); /各科的最高分void Min(); /各科的最低分;/Class.h班级类class Classprivate:char clgrade10; /一共四个年级,其中的一个年级char clclass10; /班级名称list<Student> cl; /链表对象,一个班最多30个学生public:Class() /构造函数Class(char *grade, char *cclass, l
13、ist<Student> l) /构造函数strncpy(clgrade, grade, 10);strncpy(clclass , cclass, 10);cl = l;char *Getclclass() /输出学号return clclass;void CPrint(); /输出学生信息friend ostream& operator<<(ostream& os, const Class& cl); /读出文件;/ClList.h班级链表类class ClListprivate:list<Class> thecl;public:
14、void CAdd(Class cl); /添加班级信息void CSeek(char *cclass); /查询班级信息,按班级名称查询void CModify(char *cclass, Class &cl); /修改班级信息void CDelete(char *cclass); /删除班级信息void CShow(); /遍历列表显示;以上四个类已经能够显示学生信息,比不过可通过一些成员函数实现学生和班级的一些添加、修改、删除、查找等操作。4.2 类的实现#include "stdafx.h"#include <iostream>#include
15、<iomanip>#include <list>#include <iterator>#include <fstream>#include <algorithm>#include <cstring>using namespace std;/Student.cppStudent:Student(char *name, char *num, float s1, float s2, float s3, float s4)/构造函数strncpy(stuname, name, 20);strncpy(stunum , num, 1
16、2);stuscore0 = s1;stuscore1 = s2;stuscore2 = s3;stuscore3 = s4;total = stuscore0 + stuscore1 + stuscore2 + stuscore3;void Student:print(int n)/输出函数static bool stuHead = false;if(!stuHead)if(n > 0) cout<<setw(3)<<"序号"cout<<setw(10)<<"姓名"<<setw(20)&
17、lt;<"学号"<<setw(6)<<"大外"<<setw(6)<<"高数"<<setw(6)<<"模电"<<setw(10)<<"面向对象"<<setw(10)<<"总分"<<endl;stuHead = true;if(n > 0)cout<<setw(3)<<n;cout<<setw(10)&
18、lt;<stuname<<setw(20)<<stunum<<setw(6)<<stuscore0<<setw(6)<<stuscore1<<setw(6)<<stuscore2<<setw(10)<<stuscore3<<setw(10)<<total<<endl;ostream& operator<<(ostream& os, const Student& stu)/读出文件os.write(s
19、tu.stuname, 20);os.write(stu.stunum, 12);os.write(char*)stu.stuscore, sizeof(stu.stuscore);os.write(char*)&stu.total, sizeof(float);return os;/StuList.cppvoid StuList:Add(Student stu) /添加学生信息的函数thestu.push_back(stu);void StuList:Seek(char *num) /按学号查找int sign = -1, i = 0;list<Student>:iter
20、ator it = thestu.begin(); /定义迭代器指针Student stu;while(it != thestu.end()stu = *it;if(strcmp(stu.Getnum(), num) = 0)sign = i;break;it+;i+;if(sign >= 0)cout<<"找到的结果为"<<endl;stu.print(sign + 1);elsecout<<"没有找到!"<<endl;void StuList:Show() /遍历列表显示list<Stude
21、nt>:iterator it = thestu.begin(); /定义迭代器指针int i = 0;while(it != thestu.end()it->print(+i);it+;void StuList:SorttoFile(char * filename) /按学生总成绩进行排序thestu.sort();Show();/排序后的内容保存到文件中ofstream out(filename);copy(thestu.begin(), thestu.end(), ostream_iterator<Student>(out); void StuList:Max()
22、 /各科的最高分float max4 = 0, 0, 0, 0;for(int i=0; i<4; i+)list<Student>:iterator it = thestu.begin(); /定义迭代器指针Student stu;while(it != thestu.end()stu = *it;if(maxi < stu.stuscorei)maxi = stu.stuscorei;it+;cout<<"各科的最高分 :"<<"大外"<<max0<<"高数"
23、<<max1<<"模电"<<max2<<"面向对象"<<max3<<endl;void StuList:Min() /各科的最低分float min4 = 100, 100, 100, 100;for(int i=0; i<4; i+)list<Student>:iterator it = thestu.begin(); /定义迭代器指针Student stu;while(it != thestu.end()stu = *it;if(mini > stu.st
24、uscorei)mini = stu.stuscorei;it+;cout<<"各科的最低分 :"<<"大外"<<min0<<"高数"<<min1<<"模电"<<min2<<"面向对象"<<min3<<endl;/Class.cppvoid Class:CPrint() /输出班级信息cout<<setw(3)<<"年级"<&l
25、t;setw(10)<<"班级"<<endl;cout<<setw(3)<<clgrade<<setw(10)<<clclass<<endl;cout<<setw(3)<<"序号"cout<<setw(10)<<"姓名"<<setw(20)<<"学号"<<setw(6)<<"大外"<<setw(6)<
26、<"高数"<<setw(6)<<"模电"<<setw(10)<<"面向对象"<<setw(10)<<"总分"<<endl;list<Student>:iterator it = cl.begin();/定义迭代器指针int i = 0;while(it != cl.end()it->print(+i);it+;/ClList.cppvoid ClList:CAdd(Class cl) /增加班级信息thecl
27、.push_back(cl);void ClList:CSeek(char *cclass) /查询班级信息int sign = -1;list<Class>:iterator it = thecl.begin(); /定义迭代器指针Class cl;while(it != thecl.end()cl = *it;if(strcmp(cl.Getclclass(), cclass) = 0)sign = 1;break;it+;if(sign > 0)cout<<"查找的结果为"<<endl;cl.CPrint();elsecout
28、<<"没有查找到!"<<endl;void ClList:CModify(char *cclass, Class &cl) /修改班级信息list<Class>:iterator it = thecl.begin(); /定义迭代器指针while(it != thecl.end()if(strcmp(*it).Getclclass(), cclass) = 0)*it = cl;/sign = 1;break;it+;cout<<"修改的结果为"<<endl;cl.CPrint();vo
29、id ClList:CDelete(char *cclass) /班级的删除list<Class>:iterator it = thecl.begin();/定义迭代器指针while(it != thecl.end()if(strcmp(*it).Getclclass(), cclass) = 0)thecl.erase(it);break;it+;cout<<"删除成功"<<endl;void ClList:CShow() /年级中所有班级的显示list<Class>:iterator it = thecl.begin();
30、 /定义迭代器指针Class cl;while(it != thecl.end()cl = *it;cl.CPrint();it+;4.3 主函数设计/kese1.cppint main() /主函数StuList thestu; /存放所有学生Student stu1("ma", "0803070201", 88, 90, 69, 52.6f);Student stu2("li", "0803070202", 52, 96, 74, 56);Student stu3("wang", "
31、;0803070101", 85, 25, 95, 79);Student stu4("yang", "0803070102", 82, 78, 84, 95);Student stu5("ding", "0803070301", 76, 85.6f, 94, 83);thestu.Add(stu1);thestu.Add(stu2);thestu.Add(stu3);thestu.Add(stu4);thestu.Add(stu5);thestu.Show();thestu.Seek("080
32、3070201"); /查找学号为0803070201的同学cout<<"排序的结果"<<endl;thestu.SorttoFile("student.dat"); /按总分排序thestu.Max();thestu.Min();StuList s1;StuList s2;StuList s3;s1.Add(stu3);s1.Add(stu4);s3.Add(stu5);s2.Add(stu1);s2.Add(stu2);ClList thecl;Class cl1("二年级", "080
33、30701",s1.Getthestu();Class cl2("二年级", "08030702",s2.Getthestu();Class cl3("二年级", "08030703",s3.Getthestu();thecl.CAdd(cl1);thecl.CAdd(cl2);thecl.CShow(); thecl.CSeek("08030702"); /查找班级为08030702的班级thecl.CAdd(cl3);cout<<"将08030702修改为08
34、030703"<<endl;thecl.CModify("08030702", cl3); /修改班级cout<<"删除08030701班"<<endl; thecl.CDelete("08030701"); /删除08030701班return 0;5 运行结果与分析5.1 程序运行结果学生信息的程序运行结果如图所示此图是对学生信息的处理,先输入所有学生的信息,包括学生的姓名、学号、大外、高数、模电、面向对象的成绩,通过在Student类中的构造函数中计算总分Student:Studen
35、t(char *name, char *num, float s1, float s2, float s3, float s4)/构造函数strncpy(stuname, name, 20);strncpy(stunum , num, 12);stuscore0 = s1;stuscore1 = s2;stuscore2 = s3;stuscore3 = s4;total = stuscore0 + stuscore1 + stuscore2 + stuscore3; /计算总分 查找某个学生的成绩,按照学生的学号来查找,若查找到则输出查找的结果,如为查找到则输出”没有找到!”按学生的总成绩进
36、行排序,用StuList:SorttoFile函数void StuList:SorttoFile(char * filename) /按学生总成绩进行排序thestu.sort();Show();/排序后的内容保存到文件中ofstream out(filename);copy(thestu.begin(), thestu.end(), ostream_iterator<Student>(out); 可用函数Max()和Min()求出各科的最高成绩和最低成绩,其显示结果如上图。 班级信息的运行结果如图所示显示对班级的读入并显示出来如图有两个二年级的班级08030701和0803070
37、2。 查找班级,按班级的名称来查找08030702班,用函数ClList:CSeek(char *cclass)进行查找。void ClList:CSeek(char *cclass) /查询班级信息int sign = -1;list<Class>:iterator it = thecl.begin(); /定义迭代器指针Class cl;while(it != thecl.end()cl = *it;if(strcmp(cl.Getclclass(), cclass) = 0)sign = 1;break;it+;if(sign > 0)cout<<"
38、;查找的结果为"<<endl;cl.CPrint();elsecout<<"没有查找到!"<<endl;修改班级,将08030702班修改为08030703班。修改结果如上图。删除班级,主函数想删除08030701班,故因08030701班存在,所以删除成功。函数实现如下:void ClList:CDelete(char *cclass) /班级的删除list<Class>:iterator it = thecl.begin();/定义迭代器指针while(it != thecl.end()if(strcmp(*it
39、).Getclclass(), cclass) = 0)thecl.erase(it);break;it+;cout<<"删除成功"<<endl;5.2运行结果分析整个程序主要是利用链表容器存储学生和班级,并用标准模板库里的一些函数实现对学生和班级的操作。先把所有的学生放在了一个容器里,对他们进行查询、添加、修改和删除还有总成绩的排序和求每科的最高分和最低分;在吧每个班级作为一个容器,并把学生放进去,在对班级进行查询、添加、修改和删除的操作并显示结果。总的来说,主要还是利用标准模板库里的函数,还有理清班级和学生的关系及怎样处理好他们而不至于太乱。PA
40、RT 1 类设计/ Calculation.h: interface for the CCalculation class./#if !defined(AFX_CALCULATION_H_AA32EE12_B5F4_455F_AFB3_C02717C012B1_INCLUDED_)#define AFX_CALCULATION_H_AA32EE12_B5F4_455F_AFB3_C02717C012B1_INCLUDED_#if _MSC_VER > 1000#pragma once#endif / _MSC_VER > 1000class CCalculation public:
41、void Dec2Bin(CString *strExp);void Dec2Oct(CString *strExp);CCalculation();virtual CCalculation();bool m_bDegree;int m_nOutputFlag;/=0 十进制输出;=1 十六进制输出;=2 八进制输出;=3 二进制输出CString MainPro(CString strExp);/*主处理函数*void Dec2Hex(CString *strExp);private:void Calcu(CString *strExp,int pos);/*二元运算的预处理函数*void
42、Macro(CString *strExp);/*常数宏代换*void Oct2Dec(CString *strExp);/*处理8进制数*void Bin2Dec(CString *strExp);/*处理2进制数*void Hex2Dec(CString *strExp);/*处理16进制数*void MultiE(CString *strExp);/*多元运算*void MinusMinus(CString *strExp);/*处理负负得正*void DelBracket(CString *strExp);/*用计算结果替换表达式*bool SynRes(CString *strExp
43、);/*判断表达式是否合法*CString ModiResult(CString strRes);/*在格式上处理最后的计算结果*CString NtoS(double d);/*数字转字串*CString SingleE(CString op,double dx);/*一元运算*CString TwoE(CString strExp);/*二元运算*CString opt16;CString opt15;CString m_strConName15;CString m_strConValue15;CString m_strTmp;bool IsDigital(CString str);/*判
44、断表达式中是否有函数或运算符*int BraCheck(CString str);/*计算左右括号的差值*int LocateLBra(CString strExp);/*定位最后一个左括号*double StoN(CString str);/*字串转数字*char opt26;protected:;#endif / !defined(AFX_CALCULATION_H_AA32EE12_B5F4_455F_AFB3_C02717C012B1_INCLUDED_)/ Calculator.h : main header file for the CALCULATOR application/#
45、if !defined(AFX_XPSTYLE_H_7B24CC84_968D_4019_BF93_6C66B2CDE487_INCLUDED_)#define AFX_XPSTYLE_H_7B24CC84_968D_4019_BF93_6C66B2CDE487_INCLUDED_#if _MSC_VER > 1000#pragma once#endif / _MSC_VER > 1000#ifndef _AFXWIN_H_#error include 'stdafx.h' before including this file for PCH#endif#inclu
46、de "resource.h"/ main symbols/ CFUNC.h : header file/ CCFUNC dialogclass CCFUNC : public CDialog/ Constructionpublic:CCFUNC(CWnd* pParent = NULL); / standard constructorCString m_strN;/ Dialog Data/AFX_DATA(CCFUNC)enum IDD = IDD_DIALOG_FUNC ;/ NOTE: the ClassWizard will add data members he
47、re/AFX_DATA/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CCFUNC)public:protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:CMFECToolTip m_toolTip;UINT m_nCtrlID16;CString m_strCtrlName16;UINT m_nCtrlIDTmp;/ Gen
48、erated message map functions/AFX_MSG(CCFUNC)virtual BOOL OnInitDialog();virtual void OnCancel();afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);afx_msg void OnBntSin();afx_msg void OnBtnCos();afx_msg void OnBntTan();afx_msg void OnBntCot();afx_msg void OnBntLog();afx_msg void OnBnt
49、Arccos();afx_msg void OnBntArcsin();afx_msg void OnBntLn();afx_msg void OnBntSqr();afx_msg void OnBntAbs();afx_msg void OnBntSh();afx_msg void OnBntExp();afx_msg void OnBntCh();afx_msg void OnBntArctan();afx_msg void OnBntTh();afx_msg void OnBnt10exp();/AFX_MSGDECLARE_MESSAGE_MAP();/AFX_INSERT_LOCAT
50、ION/ Microsoft Visual C+ will insert additional declarations immediately before the previous line.#endif / !defined(AFX_CFUNC_H_53B33130_2390_4BA5_BA6D_009B6CC9B245_INCLUDED_)#if !defined(AFX_CNUM_H_1A2707AD_BCD9_41CE_8520_2BEBB5C6EF11_INCLUDED_)#define AFX_CNUM_H_1A2707AD_BCD9_41CE_8520_2BEBB5C6EF1
51、1_INCLUDED_#if _MSC_VER > 1000#pragma once#endif / _MSC_VER > 1000/ CNUM.h : header file/#include "MFECToolTip.h"/ CCOP dialogclass CCOP : public CDialog/ Constructionpublic:CString m_strN;CCOP(CWnd* pParent = NULL); / standard constructor/ Dialog Data/AFX_DATA(CCOP)enum IDD = IDD_DI
52、ALOG_OP ;/ NOTE: the ClassWizard will add data members here/AFX_DATA/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CCOP)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:CMFECToolTip m_toolTip;UINT m_nCtrlIDTmp;/ Generated message map functions/AFX_MSG(CCOP)virtual void OnCancel();afx_msg void OnBtnAdd();afx_msg void OnBtnMin();afx_msg void OnBtnMul();afx_msg void OnBtnDiv();afx_msg void OnBtnPow();afx_msg void OnBtnMod();afx_msg void OnBtnLbra();afx_msg void OnBtnRbra();virtual BOOL OnInitDialog()
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 常州机电职业技术学院档案分类方案
- 186红色喜庆国潮古建筑背景的中秋灯谜会模板 2
- 电气车间备品备件计划管理规定培训课件
- 临时用电施工焊接机械安全管理规范培训
- 20201112初一数学(人教版)解一元一次方程(二)(4)-3学习任务单
- 2026年广西国际商务职业技术学院单招综合素质考试题库及答案详解(网校专用)
- 2026年广东省佛山市单招职业倾向性考试题库附参考答案详解(模拟题)
- 2026年广东金融学院单招综合素质考试题库有答案详解
- 2026年平凉职业技术学院单招综合素质考试题库含答案详解(精练)
- 2026年广东茂名幼儿师范专科学校单招职业适应性考试题库附答案详解(轻巧夺冠)
- 农贸市场营销策划方案
- 【可行性报告】2023年高纯氮化铝粉体行业项目可行性分析报告
- 随机过程十四布朗运动
- 营养支持讲课最终课件
- 出口海运工厂集装箱货物绑扎加固指南
- 电动机检修作业指导书
- TS30测量机器人Geocom中文说明书
- 化工厂监控系统解决方案
- GB/T 3565.1-2022自行车安全要求第1部分:术语和定义
- GB/T 3452.4-2020液压气动用O形橡胶密封圈第4部分:抗挤压环(挡环)
- GB/T 15382-2021气瓶阀通用技术要求
评论
0/150
提交评论