




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上 C+大作业报告 班级 姓名 总学号 一题目:21内容:用三种循环语句完成求100以内的质数设计思路:1既不是质数也不是合数,所以直接从2考虑。找出来这些数字就是要保证这个数只能让1和其本身整除,所以让这个数先除以2,然后慢慢整除其小于除以2后的数,然后输出这些数。 程序代码:while 循环#include<iostream>using namespace std;int main()int i=2;int j,n,m;while (i<101)m=1;n=i/2;j=2;while (j<=n)if(i%j=0)m=0;break;j+;i
2、f(m)cout<<i<<endl;i+;return 0;Do while 循环#include<iostream>using 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)cout<<i<<endl;i+while(i<101);return 0;For 循环#include<iostream>using namespace std;void main()i
3、nt i,j,n,m;for (i=2;i<101;i+)m=1;n=i/2;for (j=2;j<=n;j+)if (i%j=0)m=0;break;if (m)cout<<i<<endl;return 0;运行结果:结论: 不管for还是while还是do while,他们的循环体都是一样的,所以只要编出来一个就等于全编出来了,而且程序要设计尽量简单。题目:22内容:输入一个有符号的十进制数,转换成机内二进制数输出(要求用位操作运算)。设计思路:利用位运算将二进制的每一位取出存入数组,然后按要求输出。程序代码:#include <iostream&
4、gt;using namespace std;void main()char a;int t8;int i;cout<<"请输入一个数y:n"cin>>a;for(i=0;i<8;i+)ti=a&0x01;a=a>>1;for(i=7;i>=0;i-)cout<<ti;cout<<endl;system("pause");结果: 结论:只有掌握位运算规则,才能编出来程序二内容: 书上P144,4-10 设计一个用于人事管理的“人员”类 .由于考虑到通用性,这里只抽象出所有人员
5、都具有的属性:编号,性别,出生日期,身份证号.(“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数、复制构造函数、内联成员函数、带默认形参值的成员函数、类的组合。)设计思路:通过构造函数,实现人员的录入和输出。程序代码:#include<iostream>using 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 se
6、tyear(int y)year=y; void setmonth(int m) month=m; void setday(int d) day=d; void showdate() cout<<year<<" "<<month<<" "<<day<<" "<<endl; ;class peopleprivate: char number100; char id100; char sex2; date birthday; public: people(
7、); people(people&p); people(); void setnumber(char* a) strcpy(number,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;ch
8、ar m;people:people():birthday(d)void people:setid (char*ids) strcpy(id,ids);int main() date birthday; cout<<"录入信息"<<endl; people p1; /people*p4=&p1,&p2,&p3,&p4; cout<<"输入员工的出生日期"<<endl; cout<<"年" int a; cin>>a; birthda
9、y.setyear (a); cout<<"月" int b; cin>>b; birthday.setmonth (b); cout<<"日" int c; cin>>c; birthday.setday (c); cout<<"输入编号"<<endl; char numberstr20; cin>>numberstr; p1.setnumber (numberstr); cout<<"输入身份证号"<<e
10、ndl; char idstr20; cin>>idstr; p1.setid (idstr); cout<<"输入性别"<<endl; char sexstr30; cin>>sexstr; p1.setsex (sexstr); cout<<"输出信息"<<endl; cout<<"员工的出生日期" birthday.showdate (); cout<<"编号为 "<<p1.getnumber() &l
11、t;<" 身份证号为 "<<p1.getid() <<"性别为 "<<p1.getsex() ; return 0;运行结果:结论: 要充分理解函数的概念,只有在理解的情况下才能编出程序。但是不能实现多个成员的录入和输出。内容: 书上P144,4-11定义并实现一个矩形类,有长、宽两个属性,由成员函数计算矩形的面积。设计思路:通过设计类,实现矩形的计算函数程序代码:#include<iostream>using namespace std;class Rectanglepublic:Rectangle
12、(); float area(); void show();private:float a;float b;Rectangle:Rectangle() do cout<<"please input two numbers :"<<endl; cin>>a>>b;while(a<= 0 | b<= 0); float Rectangle:area()return a*b;void Rectangle:show()cout<<"a="<<a<<",b=&
13、quot;<<b<<",area="<<area()<<endl;int main()Rectangle c;c.show();return 0;运行结果:结论: 要理解类的含义,理解每个定义的作用!三内容:书上P186 5-7定义一个Cat类,拥有静态数据成员numOfCats,记录cat的个体数目,静态成员函数getNumOfCats(),读取numOfCats。设计程序测试这个类,体会静态数据成员和静态成员函数的用法!设计思路:定义一个cat类,通过构造函数,并且声明静态数据成员。代码:#include <iost
14、ream>#include <string>using namespace std;class Catpublic: Cat()+numOfCats; Cat(const Cat& cat)+numOfCats; virtual Cat()-numOfCats; static int getNumOfCats()return numOfCats;private: static int numOfCats;int Cat:numOfCats=0;int main() Cat a; Cat b; cout<<"numOfCats:"<
15、<Cat:getNumOfCats()<<endl; Cat c(a); Cat* p=new Cat(); cout<<"numOfCats:"<<Cat:getNumOfCats()<<endl; delete p; cout<<"numOfCats:"<<Cat:getNumOfCats()<<endl; return 0;运行结果:结论:这部分与类密切联系,所以要掌握类,并且理解静态数据成员的使用.内容:书上P186 5-14定义Boat类和Car两个类,二
16、者都有weight属性,定义二者的友元函数getTotalWeight(),计算二者重量之和!设计思路:定义两个类,使其为友元函数,在其基础上进行所需的运算。代码:#include<iostream>using namespace std;class Car;class Boatprivate: int Boatweight;public: Boat() Boatweight=450; friend int totalWeight(Boat &,Car &);class Carprivate: int Carweight;public: Car( ) Carweig
17、ht=450; 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 已知有一个数组名叫oneA
18、rray,用一条语句求出其元素的个数。 设计思路:利用sizeof函数代码:#include <iostream>using namespace 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为一个指向其半径的指针,存放其半径值。设计数据
19、成员的各种操作,给出这个类的完整实现并测试这个类。设计思路:利用类与友元函数代码:#include <iostream> using namespace std;class SimpleCirclepublic:SimpleCircle();SimpleCircle(int);SimpleCircle(const SimpleCircle &);SimpleCircle() void SetRadius(int);int GetRadius()const; private:int *itsRadius; SimpleCircle:SimpleCircle()itsRadiu
20、s = new int(5); SimpleCircle:SimpleCircle(int radius)itsRadius = new int(radius); SimpleCircle:SimpleCircle(const SimpleCircle & rhs)int val = rhs.GetRadius();itsRadius = new int(val); int SimpleCircle:GetRadius() constreturn *itsRadius;int main()SimpleCircle CircleOne, CircleTwo(9);cout <<
21、; "CircleOne: " << CircleOne.GetRadius() << endl;cout << "CircleTwo: " << CircleTwo.GetRadius() << endl;return 0;结果:结论:要充分理解类的含义与用法,理解友元的用法 五内容:书上P250 6-22编写函数void reverse(string &s),用递归算法是字符串倒置。算法设计:利用递归算法设计倒叙代码:#include<iostream>#include&
22、lt;string>using 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<<"请输入字符串:"<<endl;cin>>s;reverse(s);cout<<"倒序后的字符串:"&l
23、t;<endl;cout<<s<<endl;结果: 结论:要熟练掌握递归方法内容:书上P250 6-27定义一个Employee类,其中包括姓名、地址、城市和邮编等属性,包括setname(),display(),等函数。Display()使用cout语句显示姓名、地址、城市和邮编等属性,函数setname()改变对象的姓名属性,实现并测试这个类。算法设计:利用类来设计这个程序代码:#include <iostream>using namespace std;class Employee private: char *name,*address,*ci
24、ty,*postCode;public: Employee(char *_name,char *_address,char *_city,char *_postCode) name = _name; address = _address; city = _city; postCode = _postCode; void setName(char *_name) name = _name; void display() cout<<"name : "<<name<<endl; cout<<"address : &quo
25、t;<<address<<endl; cout<<"city : "<<city<<endl; cout<<"postcode : "<<postCode<<endl; ;int main(int argc,char *argv) Employee *e =new Employee("张三","天堂","宇宙",""); e->display(); e->setName
26、("李四"); e->display(); 结果:结论:熟练掌握类的方法,用类来表示需要表示的东西。 六题目:定义一个基类sharp,在此基础上派生出rectangle和circle,两者都有getarea()函数计算面积。程序代码:#include <iostream>#include <cmath>#define pi 3.14using namespace std;class shape public: virtual float area()=0;class circle:public shape public: circle(floa
27、t r1) r=r1; private: float r; float area() return (float)pi*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
28、(); float area(float len) return len * len; ;int main() shape* s2; int m,a,b; cout<<"输入圆半径:"<<endl; cin>>m; s0=new circle(m); cout<<s0->area()<<endl; cout<<"输入长和宽:"<<endl; cin>>a>>b; s1=new rectangle(a,b); cout<<s1-&g
29、t;area()<<endl; s 2 = new square( a ); cout << s2->area() << endl; return 0;:运行结果:结论:在派生类的基础上在派生,相当于把派生看作是基类!题目:定义一个Document类,有数据成员name,从document派生出book类,增加数据pagecount。程序代码:#include<iostream>#include<string>using namespace std;class Document public:Document(string Na
30、me) name=Name;void display() /cout<<"name="<<name<<endl;private:string name; ;class Book:public Document public:Book(string nam,int page):Document(nam) pageCount=page; void show() /显示Book类数据的函数cout<<"pageCount="<<pageCount<<endl;private:int pag
31、eCount; /该类有数据成员pageCount;int main() Book a("李四",50); a.display(); /显示数据name a.show(); /显示数据pageCountreturn 0;运行结果:结论:掌握类的含义,在类的基础上派生,直接输出的!可以转换为输入输出!七题目:7-10一、内容:定义一个object类,有数据成员weight及相应的操作函数,由此派生出box类,增加数据成员height和width及相应的操作函数,声明一个box类对象,观察构造函数和析构函数的调用顺序。二、设计思路:利用类的派生思想三、程序代码:#include
32、<iostream>using namespace std;class Objectpublic:Object()cout<<"call the Object's constructor"<<endl;void set_weight(int neww=0,int newn=0)weight=neww;num=newn;void total_weight()cout<<"the total weight is"<<" "<<weight*num<<
33、;endl;int getw()return weight;Object()cout<<"call the Object's desconstructor"<<endl;private:int weight,num;class Box:public Objectpublic:Box() cout<<"call the Box's constructor"<<endl;void set_digital(int h=0,int w=0,int l=0)height=h;width=w;lengt
34、h=l;void showBox()cout<<"Box's height:"<<height<<endl;cout<<"Box's width:"<<width<<endl; cout<<"Box's length:"<<length<<endl;cout<<"box's weight"<<getw()<<endl;Box()cout<
35、;<"call the Box's desconstructor"<<endl;private:int height,width,length;void main()Box 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。在主函数中声明一个derivedcla
36、ss的对象,分别用derivedclass的对象以及baseclass和derivedclass的指针来调用fn1,fn2,观察运行结果。 程序代码:#include<iostream>using namespace std;class BaseClasspublic:void fun1()cout<<"1"<<endl;void fun2()cout<<"2"<<endl;class DerivedClass:public BaseClasspublic:void fun1()cout<
37、<"3"<<endl;void fun2()cout<<"4"<<endl;void main()DerivedClass a;cout<<"通过DerivedClass的对象调用函数"<<endl;a.BaseClass:fun1(); a.BaseClass:fun2(); a.fun1(); a.fun2(); BaseClass &b=a; cout<<"通过BaseClass的指针调用函数"<<endl; b
38、.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读出其内容并显示出来,
39、看看是否正确。二、设计思路:利用输出流对象ofstream和输入流对象ifstream实现对文件的操作。代码:#include<iostream>#include <fstream>#include <string>using namespace std;void main() ofstream ofile("test1.txt"); string s="已成功写入文件"cout<<s<<" "<<s.length(); ofile.write(char*)&am
40、p;s,s.length(); ofile.close(); ifstream tfile("test1.txt");cout<<"ndispaly filen"string s2; if(tfile) tfile.read(char*)&s2,s.length(); cout << s2<<endl; else cout << "ERROR: Cannot open file 'payroll'." << endl; tfile.close();结果
41、: 题目11-6一、内容:定义一个dog类,包涵体重和年龄两个成员函数,声明一个实例dog1,体重为5,年龄为10,使用I/O流把dog1的状态写入磁盘文件,再声明另一个dog2,通过读文件把dog1的状态赋给dog2。使用二进制方式操作文件,看看结果有何不同;再看看磁盘文件的ASCII码有何不同。二、设计思路:三、程序代码:#include<iostream>#include <fstream>using namespace std;class dogpublic:dog(int weight, long days):itsWeight(weight),itsNumb
42、erDaysAlive(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;int main() / returns 1 on errorchar fi
43、leName80;cout << "Please enter the file name: "cin >> 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*) &D
44、og1,sizeof Dog1);fout.close();ifstream fin(fileName);/ ifstream fin(fileName,ios:binary);if (!fin)cout << "Unable to open " << fileName << " for reading.n"return(1);dog Dog2(2,2);cout << "Dog2 weight: " << Dog2.GetWeight() << endl;cou
45、t << "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();return 0;
46、四、运行结果:九内容: 分别用函数重载,函数的缺省参数及模板函数完成两个或三个(n个)数相加。设计思路:定义不同的函数让两个数相加,然后让其根据数据类型相加。 设计相加的函数,然后用主函数调用函数,运行结果。程序代码:函数重载:#include<iostream>using 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;cout<<"Enter two numbers:"cin>>
47、m>>n;cout<<"Their sum:"<<sum(m,n)<<endl;double x,y;cout<<"Enter two other numbers:"cin>>x>>y;cout<<"Their sum:"<<sum(x,y)<<endl;return 0;缺省参数:#include<iostream>using namespace std;int Sum(int a,int b=5,in
48、t c=2) return a+b+c;int main()const x=8,y=1,z=9;cout<<Sum(x,y,z)<<endl;cout<<Sum(x,y)<<endl;cout<<Sum(x)<<endl;return 0;函数模板:#include<iostream>using namespace std;int Sum(int a,int b/*=4*/,int c/*=8*/)return a+b+c;template<class T>T sum(T x,T y)return
49、x+y;int main()int a=4,b=9;double c=3.4,d=2.6;cout<<"Their sum:"<<sum(a,b)<<endl;cout<<"Their sum:"<<sum(c,d)<<endl;return 0;运行结果:结果一:结果二:结果三:结论:个人觉得使用函数重载比较好,因为其易明白, 容易操作。 十内容:用类完成一个时钟,要求做到使用构造函数 使用析构函数 十二时制,二十四时制 显示时间 输入走过的时间,在输出走过之后显示的时间设计思路:
50、设置时钟,构造函数,通过数字大小判定是否是AM还是PM,看分,秒是否大于60,注意加一。代码:#include<iostream>using namespace 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:
51、Clock(int newH,int newM,int newS)hour=newH;minute=newM;second=newS;void Clock:setTime(int newH,int newM,int newS)hour=newH;minute=newM;second=newS;inline void Clock:showTime()if(hour>=12)&&(hour<=23)cout<<"PM"<<hour-12<<":"<<minute<<&qu
52、ot;:"<<second<<endl; else if(hour>=0)|(hour<12)cout<<"AM"<<hour<<":"<<minute<<":"<<second<<endl;void Clock:run(int newH,int newM,int newS,int k) newS=newS+k;if(newS>60) second=newS-60;newM=newM+1;else s
53、econd=newS;if(newM>60) minute=newM-60;newH=newH+1;else minute=newM;int main() int h,m,s,k;cout<<"please enter time:"<<endl;cout<<"h"<<" ""m"<<" ""s"<<endl;cin>>h>>m>>s;cout<<&qu
54、ot;k"<<endl;cin>>k;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<iostream>#include<math.h>using namespace std;class funtionpublic: vo
55、id make();void display(); void answer();private: float a,b,c;float x1,x2; float r,i,f;void funtion:make () cout<<"输入 a b c 的值"<<endl; cin>>a; cin>>b; cin>>c;void funtion:display ()f=b*b-4*a*c; if(f>0) x1=-b/(2*a)+sqrt(f)/(2*a); x2=-b/(2*a)-sqrt(f)/(2*a); el
56、se 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(f>0) cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl; else if(f=0) cout<<"x1="<<x1<<" "<<"x2=&q
57、uot;<<x2<<endl; else cout<<"x1="<<r<<'+'<<i<<"i"<<" "<<"x2="<<r<<'+'<<i<<"i"<<endl;int main() funtion c; c.make (); c.display (); c.answer (); return 0;运行结果:结论:类需要灵活使用,明白面对对象和面对过程的区别! 十二内容:书上P222修改算法设计:通过书上的描述,加上一些自己的理解代码:#include<iostream>#include<
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 创新驱动探索新型的医疗-社区-保险合作模式
- 是个再学习的过程工作总结模版
- 区块链技术助力供应链金融的智能化升级
- 2025年小学数学听课评课个人学习总结模版
- 区块链和大数据在办公自动化中的融合应用
- 医疗器械生产中的物料管理与质量控制
- 区块链技术助力实现肿瘤患者信息共享的透明化
- 上海模特经纪合同范例
- 医疗信息化与医院品牌形象的建设关系
- 2024年文教体育用品项目资金筹措计划书代可行性研究报告
- 民间非营利组织会计培训
- 不良资产项目律师法律尽调报告(模板)
- 产品借用申请表
- 医院院内紧急意外事件应急预案(整理)
- 人教部编版六年级下册语文【选择题】专项复习训练真题100题(附答案解析)
- 外国画家作品介绍赏析
- 岩土工程勘察报告
- 分布式光伏发电项目投标技术方案(纯方案)
- 哈弗H5汽车说明书
- 音乐鉴赏(西安交通大学)知到章节答案智慧树2023年
- 2023年成都市新都区九年级二诊英语试题(含答案和音频)
评论
0/150
提交评论