C++大作业报告(共85页)_第1页
C++大作业报告(共85页)_第2页
C++大作业报告(共85页)_第3页
C++大作业报告(共85页)_第4页
C++大作业报告(共85页)_第5页
已阅读5页,还剩80页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 C+大作业报告 班级 姓名 总学号 一题目:21内容:用三种循环语句完成求100以内的质数设计思路:1既不是质数也不是合数,所以直接从2考虑。找出来这些数字就是要保证这个数只能让1和其本身整除,所以让这个数先除以2,然后慢慢整除其小于除以2后的数,然后输出这些数。 程序代码:while 循环#includeusing namespace std;int main()int i=2;int j,n,m;while (i101)m=1;n=i/2;j=2;while (j=n)if(i%j=0)m=0;break;j+;if(m)coutiendl;i+;return 0;Do while 循环

2、#includeusing namespace std;void main()int i=2;int j,n,m;dom=1;n=i/2;j=2;doif(i%j=0)m=0;break;j+;while(j=n)if(m)coutiendl;i+while(i101);return 0;For 循环#includeusing namespace std;void main()int i,j,n,m;for (i=2;i101;i+)m=1;n=i/2;for (j=2;j=n;j+)if (i%j=0)m=0;break;if (m)coutiendl;return 0;运行结果:结论: 不

3、管for还是while还是do while,他们的循环体都是一样的,所以只要编出来一个就等于全编出来了,而且程序要设计尽量简单。题目:22内容:输入一个有符号的十进制数,转换成机内二进制数输出(要求用位操作运算)。设计思路:利用位运算将二进制的每一位取出存入数组,然后按要求输出。程序代码:#include using namespace std;void main()char a;int t8;int i;couta;for(i=0;i1;for(i=7;i=0;i-)coutti;coutendl;system(pause);结果: 结论:只有掌握位运算规则,才能编出来程序二内容: 书上P1

4、44,4-10 设计一个用于人事管理的“人员”类 .由于考虑到通用性,这里只抽象出所有人员都具有的属性:编号,性别,出生日期,身份证号.(“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数、复制构造函数、内联成员函数、带默认形参值的成员函数、类的组合。)设计思路:通过构造函数,实现人员的录入和输出。程序代码:#includeusing namespace std;class dateprivate: int year; int month; int day;public: date(int a=0,int b=0,int c=0)year=a;

5、month=b;day=c; inline void setyear(int y)year=y; void setmonth(int m) month=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(n

6、umber,a); void setid(char*); void setsex(char* c) strcpy(sex,c); void setbirthday(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

7、(id,ids);int main() date birthday; cout录入信息endl; people p1; /people*p4=&p1,&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

8、(sexstr); cout输出信息endl; cout员工的出生日期; birthday.showdate (); cout编号为 p1.getnumber() 身份证号为 p1.getid() 性别为 p1.getsex() ; return 0;运行结果:结论: 要充分理解函数的概念,只有在理解的情况下才能编出程序。但是不能实现多个成员的录入和输出。内容: 书上P144,4-11定义并实现一个矩形类,有长、宽两个属性,由成员函数计算矩形的面积。设计思路:通过设计类,实现矩形的计算函数程序代码:#includeusing namespace std;class Rectanglepubli

9、c: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.show();return 0;运行结果:结论: 要理解类的含义,理解每个定义的作用!三内容:书上P1

10、86 5-7定义一个Cat类,拥有静态数据成员numOfCats,记录cat的个体数目,静态成员函数getNumOfCats(),读取numOfCats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法!设计思路:定义一个cat类,通过构造函数,并且声明静态数据成员。代码:#include #include using namespace std;class Catpublic: Cat()+numOfCats; Cat(const Cat& cat)+numOfCats; virtual Cat()-numOfCats; static int getNumOfCats()return

11、 numOfCats;private: static int numOfCats;int Cat:numOfCats=0;int main() Cat a; Cat b; coutnumOfCats:Cat:getNumOfCats()endl; Cat c(a); Cat* p=new Cat(); coutnumOfCats:Cat:getNumOfCats()endl; delete p; coutnumOfCats:Cat:getNumOfCats()endl; return 0;运行结果:结论:这部分与类密切联系,所以要掌握类,并且理解静态数据成员的使用.内容:书上P186 5-14

12、定义Boat类和Car两个类,二者都有weight属性,定义二者的友元函数getTotalWeight(),计算二者重量之和!设计思路:定义两个类,使其为友元函数,在其基础上进行所需的运算。代码:#includeusing namespace std;class Car;class Boatprivate: int Boatweight;public: Boat() Boatweight=450; friend int totalWeight(Boat &,Car &);class Carprivate: int Carweight;public: Car( ) Carweight=450;

13、friend int totalWeight(Boat &,Car &);int totalWeight(Boat &x,Car &y) return x.Boatweight+y.Carweight;int main() Boat a; Car b; cout这两者的总重量为totalWeight(a,b)endl; return 0;运行结果: 结论:在理解类的情况下,可以用友元函数。在编写程序时,可以有效的减少程序的冗长。四内容:书上P248 已知有一个数组名叫oneArray,用一条语句求出其元素的个数。 设计思路:利用sizeof函数代码:#include using namespa

14、ce std;int main() int array = 8, 4, 3, 4, 5,7,9,10; int i = sizeof(array)/sizeof(int); cout i endl; return 0;结果:结论:掌握基本函数的含义和用法内容:书上P249 6-20实现一个名为SimpleCircle的简单圆类。其数据成员int*itsRadius为一个指向其半径的指针,存放其半径值。设计数据成员的各种操作,给出这个类的完整实现并测试这个类。设计思路:利用类与友元函数代码:#include using namespace std;class SimpleCirclepublic

15、:SimpleCircle();SimpleCircle(int);SimpleCircle(const SimpleCircle &);SimpleCircle() void SetRadius(int);int GetRadius()const; private:int *itsRadius; SimpleCircle:SimpleCircle()itsRadius = new int(5); SimpleCircle:SimpleCircle(int radius)itsRadius = new int(radius); SimpleCircle:SimpleCircle(const S

16、impleCircle & rhs)int val = rhs.GetRadius();itsRadius = new int(val); int SimpleCircle:GetRadius() constreturn *itsRadius;int main()SimpleCircle CircleOne, CircleTwo(9);cout CircleOne: CircleOne.GetRadius() endl;cout CircleTwo: CircleTwo.GetRadius() endl;return 0;结果:结论:要充分理解类的含义与用法,理解友元的用法 五内容:书上P25

17、0 6-22编写函数void reverse(string &s),用递归算法是字符串倒置。算法设计:利用递归算法设计倒叙代码:#include#includeusing namespace std;void reverse (char *s)char t;static int i=0;t = *(s+strlen(s)-i-1);*(s+strlen(s)-i-1) = *(s+i);*(s+i) = t;i+;if(strlen(s)/2=i);else reverse (s);void main()char s100;cout请输入字符串:s;reverse(s);cout倒序后的字符串

18、:endl;coutsendl;结果: 结论:要熟练掌握递归方法内容:书上P250 6-27定义一个Employee类,其中包括姓名、地址、城市和邮编等属性,包括setname(),display(),等函数。Display()使用cout语句显示姓名、地址、城市和邮编等属性,函数setname()改变对象的姓名属性,实现并测试这个类。算法设计:利用类来设计这个程序代码:#include using namespace std;class Employee private: char *name,*address,*city,*postCode;public: Employee(char *_

19、name,char *_address,char *_city,char *_postCode) name = _name; address = _address; city = _city; postCode = _postCode; void setName(char *_name) name = _name; void display() coutname : nameendl; coutaddress : addressendl; coutcity : cityendl; coutpostcode : postCodedisplay(); e-setName(李四); e-displa

20、y(); 结果:结论:熟练掌握类的方法,用类来表示需要表示的东西。 六题目:定义一个基类sharp,在此基础上派生出rectangle和circle,两者都有getarea()函数计算面积。程序代码:#include #include #define pi 3.14using namespace std;class shape public: virtual float area()=0;class circle:public shape public: circle(float r1) r=r1; private: float r; float area() return (float)pi

21、*r*r; ;class rectangle:public shape public: rectangle(float w1,float h1) width=w1;height=h1; private: float width,height; float area() return width*height; ;class square : public rectangle public: square(float len):rectangle(len,len); square(); float area(float len) return len * len; ;int main() sha

22、pe* s2; int m,a,b; cout输入圆半径:m; s0=new circle(m); coutarea()endl; cout输入长和宽:ab; s1=new rectangle(a,b); coutarea()endl; s 2 = new square( a ); cout area() endl; return 0;:运行结果:结论:在派生类的基础上在派生,相当于把派生看作是基类!题目:定义一个Document类,有数据成员name,从document派生出book类,增加数据pagecount。程序代码:#include#includeusing namespace st

23、d;class Document public:Document(string Name) name=Name;void display() /coutname=nameendl;private:string name; ;class Book:public Document public:Book(string nam,int page):Document(nam) pageCount=page; void show() /显示Book类数据的函数coutpageCount=pageCountendl;private:int pageCount; /该类有数据成员pageCount;int

24、main() Book a(李四,50); a.display(); /显示数据name a.show(); /显示数据pageCountreturn 0;运行结果:结论:掌握类的含义,在类的基础上派生,直接输出的!可以转换为输入输出!七题目:7-10一、内容:定义一个object类,有数据成员weight及相应的操作函数,由此派生出box类,增加数据成员height和width及相应的操作函数,声明一个box类对象,观察构造函数和析构函数的调用顺序。二、设计思路:利用类的派生思想三、程序代码:#includeusing namespace std;class Objectpublic:Obj

25、ect()coutcall the Objects constructorendl;void set_weight(int neww=0,int newn=0)weight=neww;num=newn;void total_weight()coutthe total weight is weight*numendl;int getw()return weight;Object()coutcall the Objects desconstructorendl;private:int weight,num;class Box:public Objectpublic:Box() coutcall t

26、he Boxs constructorendl;void set_digital(int h=0,int w=0,int l=0)height=h;width=w;length=l;void showBox()coutBoxs height:heightendl;coutBoxs width:widthendl; coutBoxs length:lengthendl;coutboxs weightgetw()endl;Box()coutcall the Boxs desconstructorendl;private:int height,width,length;void main()Box

27、a;a.set_weight(4,5);a.total_weight();a.set_digital(1,2,3);a.showBox();四、运行结果:题目:711一、内容:定义一个基类baseclass,从它派生出类derivedclass。Baseclass有成员函数fn1,fn2,derivedclass也有成员函数fn1,fn2。在主函数中声明一个derivedclass的对象,分别用derivedclass的对象以及baseclass和derivedclass的指针来调用fn1,fn2,观察运行结果。 程序代码:#includeusing namespace std;class B

28、aseClasspublic:void fun1()cout1endl;void fun2()cout2endl;class DerivedClass:public BaseClasspublic:void fun1()cout3endl;void fun2()cout4endl;void main()DerivedClass a;cout通过DerivedClass的对象调用函数endl;a.BaseClass:fun1(); a.BaseClass:fun2(); a.fun1(); a.fun2(); BaseClass &b=a; cout通过BaseClass的指针调用函数endl;

29、 b.fun1(); b.fun2(); DerivedClass &t=a; cout通过DerivedClass的指针调用函数endl; t.BaseClass:fun1(); t.BaseClass:fun2(); t.fun1(); t.fun2();运行结果: 八一、内容:使用I/O流以文本方式建立一个文件test1.txt,写入字符“已成功写入文件!”用其他字处理程序(例如windows的记事本程序Nodepad)打开,看看是否正确写入。使用I/O流以文本方式打开11-3题建立的文本text1.txt读出其内容并显示出来,看看是否正确。二、设计思路:利用输出流对象ofstream和

30、输入流对象ifstream实现对文件的操作。代码:#include#include #include using namespace std;void main() ofstream ofile(test1.txt); string s=已成功写入文件;couts s.length(); ofile.write(char*)&s,s.length(); ofile.close(); ifstream tfile(test1.txt);coutndispaly filen;string s2; if(tfile) tfile.read(char*)&s2,s.length(); cout s2e

31、ndl; else cout ERROR: Cannot open file payroll. endl; tfile.close();结果: 题目11-6一、内容:定义一个dog类,包涵体重和年龄两个成员函数,声明一个实例dog1,体重为5,年龄为10,使用I/O流把dog1的状态写入磁盘文件,再声明另一个dog2,通过读文件把dog1的状态赋给dog2。使用二进制方式操作文件,看看结果有何不同;再看看磁盘文件的ASCII码有何不同。二、设计思路:三、程序代码:#include#include using namespace std;class dogpublic:dog(int weigh

32、t, long days):itsWeight(weight),itsNumberDaysAlive(days)dog()int GetWeight()const return itsWeight; void SetWeight(int weight) itsWeight = weight; long GetDaysAlive()const return itsNumberDaysAlive; void SetDaysAlive(long days) itsNumberDaysAlive = days; private:int itsWeight;long itsNumberDaysAlive

33、;int main() / returns 1 on errorchar fileName80;cout fileName;ofstream fout(fileName);/ ofstream fout(fileName,ios:binary);if (!fout)cout Unable to open fileName for writing.n;return(1);dog Dog1(5,10);fout.write(char*) &Dog1,sizeof Dog1);fout.close();ifstream fin(fileName);/ ifstream fin(fileName,io

34、s:binary);if (!fin)cout Unable to open fileName for reading.n;return(1);dog Dog2(2,2);cout Dog2 weight: Dog2.GetWeight() endl;cout Dog2 days: Dog2.GetDaysAlive() endl;fin.read(char*) &Dog2, sizeof Dog2);cout Dog2 weight: Dog2.GetWeight() endl;cout Dog2 days: Dog2.GetDaysAlive() endl;fin.close();retu

35、rn 0;四、运行结果:九内容: 分别用函数重载,函数的缺省参数及模板函数完成两个或三个(n个)数相加。设计思路:定义不同的函数让两个数相加,然后让其根据数据类型相加。 设计相加的函数,然后用主函数调用函数,运行结果。程序代码:函数重载:#includeusing namespace std;int sum(int m,int n)return m+n;double sum(double m,double n)return m+n;int main()int m,n;coutmn;coutTheir sum:sum(m,n)endl;double x,y;coutxy;coutTheir su

36、m:sum(x,y)endl;return 0;缺省参数:#includeusing namespace std;int Sum(int a,int b=5,int c=2) return a+b+c;int main()const x=8,y=1,z=9;coutSum(x,y,z)endl;coutSum(x,y)endl;coutSum(x)endl;return 0;函数模板:#includeusing namespace std;int Sum(int a,int b/*=4*/,int c/*=8*/)return a+b+c;templateT sum(T x,T y)retur

37、n x+y;int main()int a=4,b=9;double c=3.4,d=2.6;coutTheir sum:sum(a,b)endl;coutTheir sum:sum(c,d)endl;return 0;运行结果:结果一:结果二:结果三:结论:个人觉得使用函数重载比较好,因为其易明白, 容易操作。 十内容:用类完成一个时钟,要求做到使用构造函数 使用析构函数 十二时制,二十四时制 显示时间 输入走过的时间,在输出走过之后显示的时间设计思路:设置时钟,构造函数,通过数字大小判定是否是AM还是PM,看分,秒是否大于60,注意加一。代码:#includeusing namespace

38、 std;class Clockpublic:Clock(int newH,int newM,int newS);Clock(Clock &a);void setTime(int newH,int newM,int newS);void showTime();void run(int newH,int newM,int newS,int k);private:int hour,minute,second;Clock:Clock(int newH,int newM,int newS)hour=newH;minute=newM;second=newS;void Clock:setTime(int

39、newH,int newM,int newS)hour=newH;minute=newM;second=newS;inline void Clock:showTime()if(hour=12)&(hour=23)coutPMhour-12:minute:second=0)|(hour12)coutAMhour:minute:second60) second=newS-60;newM=newM+1;else second=newS;if(newM60) minute=newM-60;newH=newH+1;else minute=newM;int main() int h,m,s,k;coutp

40、lease enter time:endl;couth m shms;coutkk;Clock a(0,0,0);a.showTime(); a.setTime(h,m,s);a.showTime();a.run(h,m,s,k);a.showTime();return 0;运行结果:结论:时钟函数需要自己掌握构造,调用函数等,要认真,不放弃,就会编出自己的程序十一内容: 求解一元二次方程设计思路:通过类来设计程序。 程序代码: #include#includeusing namespace std;class funtionpublic: void make();void display()

41、; void answer();private: float a,b,c;float x1,x2; float r,i,f;void funtion:make () cout输入 a b c 的值a; cinb; cinc;void funtion:display ()f=b*b-4*a*c; if(f0) x1=-b/(2*a)+sqrt(f)/(2*a); x2=-b/(2*a)-sqrt(f)/(2*a); else if(f=0) x1=-b/(2*a); x2=-b/(2*a); else r=-b/(2*a); i=sqrt(-f)/(2*a); void funtion:answer() if(f0) coutx1=x1 x2=x2endl; else if(f=0

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论