1、南昌大学C+大作业软件学院大作业任务书 课程名称: C+程序设计 题 目: C+综合编程(面向对象) 专 业: 计算机网络技术 班 级: 131 学 号: 8001613 学生姓名: 完成人数: 起讫日期: 2014.6.3 任课教师: 李荣鑫 职称: 教师 部分管主任: 完成时间: 2014.6.9 说 明1.本任务书由任课教师填写后,下达到学生。2.任务完成后,任课教师需填写小结表。3.任务书、学生成绩与学生完成后的大作业(纸质和电子两种)一并报送各教学研究部审核后转教务办。4.另附一份全班大作业总结大作业的要求和内容:(包括题目选择范围、技术要求、递交时间、考核方法等)1.要求独立完成下
2、面的每一道题目。(题目附后面)2.每一道题均要求写出完整的程序代码,并要求调试运行通过。程序要有输入和输出。并将程序运行结果的窗口截屏(用Alt+Prt键),粘贴(用Ctrl+V键)到程序代码的后面。3.不允许有程序代码雷同和抄袭的情况,若代码雷同率超过30%以上的,双方成绩均按不及格处理。4.要求递交电子文档和打印纸质文档,由班长统一收取。5.递交时间:2014年6月12日之前。教师小结:成绩: 教 师 签 名: 教研部负责人: 学生姓名: 康涛 C+程序设计编程试题1.设计一个圆柱体类,定义构造函数初始化圆柱体,定义两个成员函数分别计算它的底面积和体积。2.定义一个点类Point,有横坐标
3、x和纵坐标y,定义构造函数初始化点,定义display()函数显示点的坐标。再定义一个线段类Line,由两个点作为对象成员,定义不同的构造函数初始化线段,定义成员函数计算线段的距离。3.编写一个复数类。实现各种常用构造函数,实现整数,实数向复数的转换函数,实现整数、实数和复数以及复数和复数之间的加、减、乘法运算。4.设计一个矩阵类,要求在矩阵类中重载加(+)、减(-)、乘(*)、复值(=)运算,在主函数中定义两个对象,并分别调用重载的运算符。5.设计一个基类Shapes,包括成员函数display(),声明为纯虚函数。由该类派生出两个类:矩形类Rectangle和圆类Circle,再由长方形类
4、派生出立方体类Box,由圆类派生出圆柱体类Cylinder。分别定义display()函数显示图形的主要几何元素(如长宽高半径等),用GetS()计算图形的面积,用GetV计算图形的体积。在主函数中定义相关的对象并显示相应的内容。6.定义一个日期类Cdate.,该类有year,month,day三个私有数据成员。实现一下功能:1)定义成员函数display()显示日期;2)定义成员函数setdate()设置日期;3)定义默认构造函数设置系统默认日期;4)重载+运算符。7.用类实现一个单链表的操作。(包括链表的建立,链表的输出,链表的插入和删除操作)大作业的要求和内容:(包括题目选择范围、技术要
5、求、递交时间、考核方法等)1.要求独立完成下面的每一道题目。(题目附后面)2.每一道题均要求写出完整的程序代码,并要求调试运行通过。程序要有输入和输出。并将程序运行结果的窗口截屏(用Alt+Prt键),粘贴(用Ctrl+V键)到程序代码的后面。3.不允许有程序代码雷同和抄袭的情况,若代码雷同率超过30%以上的,双方成绩均按不及格处理。4.要求递交电子文档和打印纸质文档,由班长统一收取。递交时间:2014年6月12日之前。1.设计一个圆柱体类,定义构造函数初始化圆柱体,定义两个成员函数分别计算它的底面积和体积。#include#define PI 3.14using namespace std;
6、class Cylinderprivate :int radius; int height;public: Cylinder(int r,int h) radius=r; height=h; ; double area()return (PI*radius*radius); double volume()return PI*radius*radius*height; void show() coutthe area is:area()nthe volume is:volume()endl;void main() int r,h; cout姓名:康涛endl; cout学好:8001613018
7、endl; cout输入r和hrh; Cylinder Cyli1(r,h); Cyli1.show();2.定义一个点类Point,有横坐标x和纵坐标y,定义构造函数初始化点,定义display()函数显示点的坐标。再定义一个线段类Line,由两个点作为对象成员,定义不同的构造函数初始化线段,定义成员函数计算线段的距离。#include #include using namespace std;class Point private: int x; int y; public: Point(int a,int b)x=a;y=b; void display(); ; void Point:d
8、isplay() cout(x,y)endl;class Line public: Line(int a,int b,int c,int d):x1(a),y1(b),x2(c),y2(d) float getdist()return sqrt(pow(x1-x2),2)+pow(y1-y2),2); private: int x1,y1; int x2,y2; int main() cout姓名:康涛endl; cout学好:8001613018endl; Line l(3,4,6,2); Point p1(3,4); Point p2(6,2); p1.display(); p2.disp
9、lay(); coutthe distance of tow points is :endl; coutl.getdist()endl;return 0;3.编写一个复数类。实现各种常用构造函数,实现整数,实数向复数的转换函数,实现整数、实数和复数以及复数和复数之间的加、减、乘法运算#includeclass Complex public: Complex()real=0;imag=0; Complex(double r)real=r;imag=0; /实数转换为复数的转换构造函数 Complex(double r,double i)real=r;imag=i; Complex operato
10、r+(Complex &c2); /重载+号 Complex operator+(int &i); /复数与实数相加 friend Complex operator+(int&,Complex &); /实数与复数相加 Complex operator-(Complex &c2); /重载号 Complex operator*(Complex &c2); /重载*号 void display(); void display_1(); private: double real; double imag; ; Complex Complex:operator+(Complex &c) return
11、 Complex(real+c.real,imag+c.imag); Complex Complex:operator+(int &i) return Complex(real+i,imag); void Complex:display() cout(real,imagi)endl; Complex operator+(int &i,Complex &c) return Complex(i+c.real,c.imag); Complex Complex:operator -(Complex &C2) Complex C; C.real=real-C2.real; C.imag=imag-C2.
12、imag; return C; Complex Complex:operator *(Complex &C2) Complex C; C.real=real*C2.real-imag*C2.imag; C.imag=imag*C2.real+real*C2.imag; return C; void main() cout姓名:康涛endl; cout学好:8001613018endl; Complex c1(2,3),c2(8,-6),c3; int i=5; c3=c1+c2; coutc1=;c1.display(); coutc2=;c2.display(); coutc1+c2=; c
13、3.display(); c3=i+c1; couti+c1=; c3.display(); c3=c1+i; coutc1+i=; c3.display(); c3=c1-c2; coutc1-c2=; c3.display(); c3=c1*c2; coutc1*c2=; c3.display(); cout将一个实数转换为复数:endl; c3=Complex(3.8); c3.display();4.设计一个矩阵类,要求在矩阵类中重载加(+)、减(-)、乘(*)、复值(=)运算,在主函数中定义两个对象,并分别调用重载的运算符。#include class Matrix friend o
14、stream& operator(ostream& out,Matrix& M); /重载(istream& in,Matrix& M); /重载运算符 public: Matrix(int r,int c); Matrix operator+(Matrix &M); Matrix operator-(Matrix &M); Matrix operator*(Matrix &M); int GetCounts() return rows*cols; private: int rows,cols; /定义矩阵的行数rows,列数cols double *m; /矩阵中实际(物理)按行存放的元素;
15、 Matrix:Matrix(int r,int c) rows=r,cols=c; m=new doublerows*cols; istream& operator(istream& in,Matrix& M) /输入流重载 int i; for(i=0;i*(M.m+i); return in; ostream& operator(ostream& out,Matrix& M) /输出流重载 int i,j=0; for(i=0; iM.rows*M.cols; i+) if(j=M.cols) j=0,outendl; out *(M.m+i); j+; return out; Matr
16、ix Matrix:operator+(Matrix &M) int i; Matrix tempM(rows,cols); for(i=0; irows*cols; i+) *(tempM.m+i)=*(m+i)+*(M.m+i); return tempM; Matrix Matrix:operator-(Matrix &M) int i; Matrix tempM(rows,cols); for(i=0;irows*cols;i+) *(tempM.m+i)=*(m+i)-*(M.m+i); return tempM; Matrix Matrix:operator*(Matrix &M)
17、 int i,j,t,k=0; Matrix tempM(rows,cols); for(i=0; irows*cols; i+=cols) for(j=0; jcols; j+) for(t=0,*(tempM.m+k)=0; tcols; t+) *(tempM.m+k)=*(tempM.m+k)+*(m+i+t)*(M.m+j+t*rows); k+; return tempM; void main() cout姓名:康涛endl; cout学好:8001613018endl; Matrix A(3,3); Matrix B(3,3); Matrix C(3,3); cout输入第一个矩
18、阵的A.GetCounts()个数:A; cout输入第二个矩阵的B.GetCounts()个数:B; C=A+B; coutendl矩阵之和:endl; coutCendl; coutendl矩阵之差:endl; C=A-B; coutCendl; C=A*B; coutendl矩阵之积:endl; coutCendlendl; 5.设计一个基类Shapes,包括成员函数display(),声明为纯虚函数。由该类派生出两个类:矩形类Rectangle和圆类Circle,再由长方形类派生出立方体类Box,由圆类派生出圆柱体类Cylinder。分别定义display()函数显示图形的主要几何元素
19、(如长宽高半径等),用GetS()计算图形的面积,用GetV计算图形的体积。在主函数中定义相关的对象并显示相应的内容。#include #define PI 3.14using namespace std;class Shapes public: virtual void display()=0;class Rectangle:virtual public Shapes/长方形类protected: int x,y;public: Rectangle(int a,int b)x=a;y=b; float GetS_Rectangle()return (x*y); virtual void di
20、splay() cout宽:x长:yendl; cout长方形的面积是:GetS_Rectangle()endl; ;class Circle:virtual public Shapes/圆形类protected: int r;public: Circle(int radius)r=radius; float GetS_Circle()return (PI*r*r); virtual void display() cout圆形半径是:rendl; cout圆形的面积是:GetS_Circle()endl; ;class Box: public Rectangle/长方体类protected:
21、int g;public: Box(int a,int b,int gao):Rectangle(a,b),g(gao) float GetV_Box()return Rectangle:GetS_Rectangle()*g; virtual void display() cout长方体的高是:gendl; cout长方体的体积是:Rectangle:GetS_Rectangle()*gendl; ;class Cylinder: public Circle/圆柱体类protected: int h;public: Cylinder(int radious,int height):Circle
22、(radious),h(height) float GetV_Cylinder()return Circle:GetS_Circle()*h; virtual void display() cout圆柱体的高是:hendl; cout圆柱体的体积是:Circle:GetS_Circle()*hendl; ;void main() cout姓名:康涛endl; cout学好:8001613018endl; Rectangle R1(2,3); R1.display(); Circle C1(2); C1.display(); Box B1(2,3,2); B1.display(); Cylind
23、er Cy1(2,3); Cy1.display();6.定义一个日期类Cdate.,该类有year,month,day三个私有数据成员。实现一下功能:1)定义成员函数display()显示日期;2)定义成员函数setdate()设置日期;3)定义默认构造函数设置系统默认日期;4)重载+运算符。#include#include using namespace std; class Cdatepublic: Cdate()year=0;month=0;day=0; Cdate(int y,int m,int d):year(y),month(m),day(d) setdate(int,int,i
24、nt);/设置一个时间 Cdate operator+(); void display()coutyear年month月day日endl;private: int year; int month; int day;Cdate:setdate(int y,int m,int d) year=y; month=m; day=d;Cdate Cdate:operator+() +day; +month; +year; return *this;void main() cout姓名:康涛endl; cout学好:8001613018endl; Cdate setdate(2013,9,15); cou
25、tsetdate()设置的时间是:; setdate.display();/输出设置时间 cout重载setdate()设置的time is:; Cdate Cdat1(2013,9,15); for(int i=0;i1;i+) +Cdat1; Cdat1.display(); tm *year; /设置当前的系统时间 tm *month; tm *day; time_t t; t=time(0); year = localtime(&t); month = localtime(&t); day = localtime(&t); cout当前您的电脑系统时间是:; couttm_year+1
26、900)年 tm_mon+1)月 tm_mday日endl;/输出当前系统时间7.用类实现一个单链表的操作。(包括链表的建立,链表的输出,链表的插入和删除操作)#includeusing namespace std;typedef int elemtype; /数据类型模版class Lnode /结点 public: elemtype data; Lnode *next;/建表void creat_Link(Lnode *&head) Lnode *p,*q; int n; p=new Lnode; head=p; cout输入链表长度:n; cout输入数据:p-data; q=p; for(int i=1;ip-data; q-next=p; q=p; q-next=NULL; /表的输出void output_Link(Lnode *&head) if(head=NULL) cout空链表!endl; return; Lno
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1