江南大学《面向对象的程序设计》大作业报告.doc
《江南大学《面向对象的程序设计》大作业报告.doc》由会员分享,可在线阅读,更多相关《江南大学《面向对象的程序设计》大作业报告.doc(39页珍藏版)》请在三一办公上搜索。
1、面向对象的程序设计大作业报告 班级 姓名 总学号 题目2-1题目要求:用穷举法找出1100间的质数,显示出来。分别使用while, do-while,for循环语句实现。设计思路:通过判断整除比自己小的数的余数是否为零来判别质数 由于非平方数的因素必有一个小于其平方根值 故可以只验算那些比根值小的数来更快获得结果程序代码:使用while:void main()int i,j,k,flag; i=2; while(i=100) flag=1; k=sqrt(i); j=2; while(j=k) if(i%j=0) flag=0; breake; j+; if(flag) couti是质数end
2、l; i+; 使用do while:void main()int i,j,k,flag; i=2; do flag=1; k=sqrt(i);j=2; do if(i%j=0) flag=0; breake; j+; while(j=k); if(flag) couti是质数endl; i+; while(i=100);使用for循环:void main() int i,j,k,flag; for(i=2;i=100;i+) flag=1; k=sqrt(i); for(j=2;j=k;j+) If(i%j=0) flag=0; breake; if(flag) couti是质数endl; 运
3、行结果:心得体会:举一反三题目:2-2题目内容:输入一个有符号的十进制数,转换成机内二进制数输出(求 用位操作运算)。屏蔽低4位,再以2进制显示。设计思路:进行进制转换 按位进行右移程序代码:void main()char a;int t8;int i;couta;for(i=0;i1;for(i=7;i=0;i-)coutti;coutendl;system(pause);运行结果:心得体会:熟悉进制 的意义即可 题目3-1 题目:观察下面程序的运行输出,与你设想的有何不同?仔细体会引用的用法。源程序:#include int main() int intOne; int &rSomeRef
4、 = intOne; intOne = 5; cout intOne:tt intOne endl; cout rSomeRef:t rSomeRef endl; int intTwo = 8; rSomeRef = intTwo; / not what you think! cout nintOne:tt intOne endl; cout intTwo:tt intTwo endl; cout rSomeRef:t rSomeRef endl; return 0;程序运行输出:心得体会:与设想一样 题目3-2题目要求:用递归的方法编写函数求n阶勒让德多项式的值,在主程序中实现输入、输出;设
5、计思路:程序代码:int main() int x,n; float polya(int ,int ); coutxn; coutpolya(x,n)endl; return 0;float polya(int x,int n) if(n=0) return 1; else if(1=n) return x; else return ( (2*n-1)*x*polya(n-1,x) - (n-1)*polya(n-2,x) )/n;输出结果:心得体会:搞懂递归的意义即可 题目3-3题目要求:求任意2(3或n)个数之和,要求分别用函数:缺省参数,重载和模板函数完成设计思路:定义不同的函数让两个数
6、相加,然后让其根据数据类型相加。 设计相加的函数,然后用主函数调用函数,运行结果。程序代码:函数重载:int sum(int m,int n)return m+n;double sum(double m,double n)return m+n;int main()int m,n;coutmn;cout他们的和为:sum(m,n)endl; double x,y; coutxy; cout他们的和为:sum(x,y)endl;return 0;缺省参数:int Sum(int a,int b=0,int c=0) return a+b+c;int main()int const x=3,y=2,
7、 z=1;coutSum(x,y,z)endl;coutSum(x,y)endl;coutSum(x)endl;return 0;函数模版:T sum(T x,T y)return x+y;int main()int a=3,b=2;double c=3.2,d=2.3;cout他们的和:sum(a,b)endl;cout他们的和:sum(c,d)endl;return 0;输出结果:心得体会:函数重载比较简洁题目4-1题目要求 定义一个Dog 类,包含的age、weight等属性,以及对这些属性操作的方法。实现并测试这个类。设计思路:程序代码:class dog private: int i
8、tsAge; int itsWeight; public: dog():itsAge(0),itsWeight(0) void setAge(int i)itsAge=i; void setWeight(int i)itsWeight=i; int age()return itsAage; int weight()return itsWeight;int main() dog d; coutd.age() d.weight()endl; return 0;运行结果:心得体会: 题目4-2题目要求:定义一个矩形类,有长、宽两个属性,有成员函数计算矩形的面积 。设计思路:通过设计类来实现程序代码:
9、#includeusing namespace std;class Rectanglepublic:Rectangle(); float area(); void show();private:float a;float b;Rectangle:Rectangle() do coutplease input two numbers :ab;while(a= 0 | b= 0); float Rectangle:area()return a*b;void Rectangle:show()couta=a,b=b,area=area()endl;int main()Rectangle c;c.sho
10、w();return 0;运行结果:心得体会:理解类的含义题目4-3题目要求:设计一个人事管理的“人员”类,包含编号,性别,出生日期,身份证号等,实现对人员信息的录入和显示。设计思路:通过构造函数实现程序代码:#includeusing namespace std;class dateprivate: int year; int month; int day;public: date(int a=0,int b=0,int c=0)year=a;month=b;day=c; inline void setyear(int y)year=y; void setmonth(int m) month
11、=m; void setday(int d) day=d; void showdate() coutyear month day endl; ;class peopleprivate: char number100; char id100; char sex2; date birthday; public: people(); people(people&p); people(); void setnumber(char* a) strcpy(number,a); void setid(char*); void setsex(char* c) strcpy(sex,c); void setbi
12、rthday(date d) birthday=d; char *getnumber() return number; char *getsex() return sex; char *getid() return id; date getbirthday() return birthday; ;date d;char m;people:people():birthday(d)void people:setid (char*ids) strcpy(id,ids);int main() date birthday; cout录入信息endl; people p1; /people*p4=&p1,
13、&p2,&p3,&p4; cout输入员工的出生日期endl; couta; birthday.setyear (a); coutb; birthday.setmonth (b); coutc; birthday.setday (c); cout输入编号numberstr; p1.setnumber (numberstr); cout输入身份证号idstr; p1.setid (idstr); cout输入性别sexstr; p1.setsex (sexstr); cout输出信息endl; cout员工的出生日期; birthday.showdate (); cout编号为 p1.getnu
14、mber() 身份证号为 p1.getid() 性别为 p1.getsex() ; return 0;运行结果:心得体会:只能单员工输入 题目5-1题目: 下面的程序的运行结果是什么,实际运行一下,看看与你的设想有何不同。#include void myFunction(); int x = 5, y = 7; int main() cout x from main: x n; cout y from main: y nn; myFunction(); cout Back from myFunction!nn; cout x from main: x n; cout y from main:
15、y n; return 0;void myFunction() int y = 10; cout x from myFunction: x n; cout y from myFunction: y nn;解:预计程序运行输出:x from main: 5y from main: 7x from myFunction: 5y from myFunction: 10Back from myFunction!x from main: 5y from main: 7实际输出: 心得体会:输出结果和预估一致,感觉只要搞清楚各同名变量在函数中的值,参数传递的过程就可以。题目5-2题目要求: 定义一个Cat
16、类,拥有静态数据成员HowManyCats,记录Cat的个体数目;静态成员函数GetHowMany(),存取HowManyCats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法。设计思路:定义一个cat类,通过构造函数,并且声明静态数据成员程序代码:#include #include using namespace std;class Catpublic: Cat()+HowManyCats; Cat(const Cat& cat)+HowManyCats; virtual Cat()-HowManyCats; static int GetHowMany()return HowMa
17、nyCats;private: static int HowManyCats;int Cat:HowManyCats=0;int main() Cat a; Cat b; coutHowManyCats:Cat:GetHowMany()endl; Cat c(a); Cat* p=new Cat(); coutHowManyCats:Cat:GetHowMany()endl; delete p; coutHowManyCats:Cat:GetHowMany()endl; return 0;运行结果:心得体会:理解静态数据成员的用法题目5-3题目要求:定义Boat与Car两个类,二者都有weig
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象的程序设计 江南 大学 面向 对象 程序设计 作业 报告

链接地址:https://www.31ppt.com/p-2386073.html