版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、一1.cpp#include#includeusing namespace std;main() int m; srand(time(NULL);/产生随机数种子 m=rand()%1000+1; cout*猜数字游戏*endl; coutn; while(n1000|n1)/数据合法性判断 cout你输入的数据不合理,请重新输入n; while(m!=n) if(mn) cout)oendl; if(mn) cout)on; cout你好聪明呀,终于猜对啦()endl; system(pause); return 0; 一2.cpp#include#includeusing namespac
2、e std; class Point/点类 public: Point(float X,float Y) x=X; y=Y; float getX()return x; float getY()return y; float distance(Point A,Point B) return sqrt(A.getX()-B.getX()*(A.getX()-B.getX()+(A.getY()-B.getY()*(A.getY()-B.getY(); Point()/构造函数 coutPoint析构函数被调用endl; Point()/析构函数 coutpoint析构函数被调用endl; pri
3、vate: float x; float y;class square/矩形类 public: void putA(Point a)A=a; void putB(Point b)B=b; float S(Point A,Point B); square()/构造函数 coutsquare调用了构造函数endl; square()/析构函数 coutsquare调用了析构函数endl; private: Point A; Point B; float square:S(Point A,Point B)/计算矩形的面积 float L,W; L=B.getX()-A.getX();/ 长 W=B.
4、getY()-A.getY();/宽 if(L0|W0) return 0; return (L*W); int main() float x1,y1,x2,y2; square C; coutx1y1; Point A(x1,y1); coutx2y2; Point B(x2,y2); float square; cout两点间的距离是:; coutA.distance(A,B)endl; square=C.S(A,B); if(square=0) cout你输入的两个点是无效的-_-|endl; else cout矩形的面积是:; coutsquareendl; system(pause)
5、; return 0;一3.cpp#includeusing namespace std;class Car;class Boat friend double sum(Boat a,Car b);/友元函数 private: double weight; public: void set(double a)weight=a; ;class Car friend double sum(Boat a,Car b); /友元函数 private: double weight; public: void set(double a)weight=a; ;double sum(Boat a,Car b)
6、return a.weight+b.weight;int main() Boat a; Car b; double m,n; coutm; a.set(m); coutn; b.set(n); cout两者的质量之和:; coutsum(a,b)endl; system(pause); return 0; 二1.cpp#includeusing namespace std;void init(int m45)/矩阵的初始化函数 for(int i=0;i=3;i+) for(int j=0;jmij;void output(int m45) for(int i=0;i=3;i+) for(in
7、t j=0;j=4;j+) coutmij ; coutendl; void add(int m45,int n45,int p45)for(int i=0;i=3;i+) for(int j=0;j=4;j+) pij=mij+nij; output(p);void subtract(int m45,int n45,int p45)for(int i=0;i=3;i+) for(int j=0;j=4;j+) pij=mij-nij; output(p);main()int A145,A245,A345;cout初始化A1数组:endl; init(A1);cout初始化A2数组:endl;
8、init(A2);coutA1和A2的数组之和:endl;add(A1,A2,A3);coutA1和A2的数组之差:endl; subtract(A1,A2,A3);二2.cpp#include #include using namespace std; void init(int *p)/初始化 for(int i=0;i=3;i+) for(int j=0;jpij; void output(int *p) for(int i=0;i=3;i+) for(int j=0;j=4;j+) coutpij ; coutendl; void add(int *m,int *n,int *p)fo
9、r(int i=0;i=3;i+) for(int j=0;j=4;j+) pij=mij+nij; output(p); void subtract(int *m,int *n,int *p) for(int i=0;i=3;i+) for(int j=0;j=4;j+) pij=mij-nij; output(p); main() int *A1; int *A2; int *A3; A1=new int*4; for(int i=0;i=3;i+) A1i=new int5; A2=new int*4; for(int i=0;i=3;i+) A2i=new int5; A3=new i
10、nt*4; for(int i=0;i=3;i+) A3i=new int5; cout初始化A1数组:endl; init(A1);cout初始化A2数组:endl;init(A2);coutA1和A2的数组之和:endl;add(A1,A2,A3);coutA1和A2的数组之差:endl; subtract(A1,A2,A3); /释放内存部分 for(int i=0;i=3;i+) delete A15;delete A1;for(int i=0;i=3;i+) delete A25;delete A2;for(int i=0;i=3;i+) delete A35;delete A3;r
11、eturn 0; 二3.cpp#include#includeusing namespace std;class matrix private: int line,row; public: int *m;/二维数组指针matrix(int x,int y) /带参数的构造函数 line=x;row=y;m=new int *line;for(int i=0;iline;i+) mi=new introw; void init()/初始化函数 cout输入矩阵元素:endl; for(int i=0;iline;i+) for(int j=0;jmij; void output() for(in
12、t i=0;iline;i+) for(int j=0;jrow;j+) coutmij ; coutendl; void add(matrix &a,matrix &b) for(int i=0;iline;i+) for(int j=0;jrow;j+) mij=a.mij+b.mij; void subtract(matrix &a,matrix &b) for(int i=0;iline;i+) for(int j=0;jrow;j+) mij=a.mij-b.mij; matrix()/默认构造函数 coutlinerow; m=new int *line; for(int i=0;
13、iline;i+) mi=new introw; matrix(matrix & A)/拷贝构造函数 m=new int *A.line; for(int i=0;iA.line;i+) mi=new intA.row; line=A.line;row=A.row; matrix()/析构函数 cout调用了析构函数endl; for(int i=0;iline;i+) delete mi; delete m; main() matrix A1;/ 调用不带参数的构造函数 matrix A2=A1;/拷贝构造函数 matrix A3=A1; coutA1:; A1.init(); coutA2
14、:; A2.init(); A3.add(A1,A2); coutA1和A2的和是:endl; A3.output(); A3.subtract(A1,A2); coutA1和A2的差是:endl; A3.output(); int line ,row; coutlinerow; matrix* pA1=new matrix(line,row); matrix* pA2=new matrix(line,row); matrix* pA3=new matrix(line,row); coutinit(); coutinit(); pA3-add(*pA1,*pA2); coutA1和A2的和是:
15、output(); coutA1和A2的差是:subtract(*pA1,*pA2); pA3-output(); A1.matrix(); A2.matrix(); A3.matrix(); return 0;三1.cpp#include#define pi 3.1416using namespace std;class shapepublic: float area() return 1; shape()/构造函数 coutshape的构造函数被调用endl; shape()/析构函数 coutshape的析构函数被调用endl;system(pause); ;class rectangl
16、e: public shapeprivate: float length; float wideth; public: rectangle(float a,float b) length=a; wideth=b; coutrectangle带参数的构造函数endl; void set(float a,float b) length=a; wideth=b; float area() return length*wideth; rectangle() coutrectangle的析构函数endl; system(pause); ;class circle: public shapeprivate
17、: float r; public: circle(float m) r=m; coutcircle的构造函数endl; void set(float m) r=m; float area() return pi*r*r; circle() coutcircle的析构函数endl; system(pause); ;class square: public rectangleprivate: float q; public: square(float a):rectangle(a,a) q=a; coutsquare的构造函数endl; void set(float a) q=a; square
18、() coutsquare的析构函数endl; system(pause); ;int main()float a,b;cout1:计算长方形的面积:endl;coutab;rectangle p(a,b);cout长方形的面积是:p.area()endl;float c; cout2;计算正方形的面积:endl;coutc;square m(c);cout正方形的面积:m.area()endl;float d;cout3:计算圆的面积:endl;coutd; circle n(d);cout圆的面积:n.area()endl; return 0;三2.cpp#include#define p
19、i 3.1416using namespace std;class shapepublic: virtual float area()=0 shape()/构造函数 coutshape的构造函数被调用endl; shape()/析构函数 coutshape的析构函数被调用endl;system(pause); ;class rectangle: public shapeprivate: float length; float wideth; public: rectangle(float a,float b) length=a; wideth=b; coutrectangle带参数的构造函数e
20、ndl; void set(float a,float b) length=a; wideth=b; float area() return length*wideth; rectangle() coutrectangle的析构函数endl; system(pause); ;class circle: public shapeprivate: float r; public: circle(float m) r=m; coutcircle的构造函数endl; void set(float m) r=m; float area() return pi*r*r; circle() coutcirc
21、le的析构函数endl; system(pause); ;class square: public rectangleprivate: float q; public: square(float a):rectangle(a,a) q=a; coutsquare的构造函数endl; void set(float a) q=a; square() coutsquare的析构函数endl; system(pause); ;int main()float a,b;cout1:计算长方形的面积:endl;coutab;rectangle p(a,b);cout长方形的面积是:p.area()endl;
22、float c; cout2;计算正方形的面积:endl;coutc;square m(c);cout正方形的面积:m.area()endl;float d;cout3:计算圆的面积:endl;coutd; circle n(d);cout圆的面积:n.area()endl; return 0;三3.cpp#includeusing namespace std;class pointprivate: float x; float y; public: void set(float a,float b) x=a; y=b; float putX() return x; float putY()
23、return y; point() coutpoint的析构函数被调用endl; friend point operator+(point& c1)c1.x+;c1.y+;return c1;friend point operator+(point& c1,int) point temp=c1; +c1; return c1;friend point operator-(point& c1)c1.x-;c1.y-;return c1;friend point operator-(point& c1,int) point temp=c1; -c1; return c1;int main() fl
24、oat m,n; cout请输入坐标(x,y)mn; point p,b; p.set(m,n); cout1:p+的结果是 ; b=(p+); coutb.putX() b.putY()endl; cout2:+p的结果是endl; b=+p; coutb.putX() b.putY()endl; cout2:p-的结果是endl; b=(p-); coutb.putX() b.putY()endl; cout2:-p的结果是endl; b=-p; coutb.putX() b.putY()endl;四1.cpp#include #include#includeusing namespace
25、 std;int main() ofstream out; out.open(1.txt); int a=200; char b100=adadadad;int c=m;unsigned int d=7300;long e=1212121212;double f=121.121313;outsetw(15)b; outsetw(10)a;outsetw(10)d;outsetw(10)eendl;outsetprecision(12)f; setw(15); out十进制:setw(10)a; /默认以十进制形式输出a setw(15); out十六进制:hexsetw(10)a; /以十六进
26、制形式输出a out八进制:octsetw(10)a; /以八进制形式输出a out.close();return 0;四2.cpp#includeusing namespace std;int main()char name20;char temp20;coutname;ifstream read;ofstream out;out.open(测试.txt);read.open(name.txt); int n=1; while(read.getline(temp,20) outn:tempendl; n+; cout数据已被写入 测试.txtendl; out.close(); read.c
27、lose();四3.cpp#include#include#includeextern C/调用c的函数库#include#include using namespace std;/电话本结构体定义typedef struct TeleBookchar name20;long telenum;struct TeleBook *next;TeleBook,*LinkList;long num;char name20;/*电话本创建模块*/ LinkList CreatTelebook(LinkList L) LinkList s=L,p; cout请输入用户的姓名和号码,建立通讯录:(以#结束)
28、endl; coutname; coutnum; while(1) p=new TeleBook; p-telenum=num; strcpy(p-name,name); s-next=p; s=p; coutname; if(!(strcmp(name,#) break; coutnum; s-next=NULL; return L; /*在屏幕上输出电话本信息*/void printList(LinkList L) LinkList q=L-next; if(L=NULL|L-next=NULL)cout该通讯录为空endl; else cout*号码薄*endl; while(q!=NU
29、LL) cout姓名; ; cout.width(10); cout.fill(*); coutname; cout ; cout号码:; couttelenumnext; cout*name,name) if(n=0) cout你要查询的结果是:endl; cout姓名:name; coutsetw(10)号码:telenumnext;if(p=NULL&n=0) cout你要查找的用户不存在next!=NULL) m=m-next; int temp=0; LinkList q=L; coutname; coutnum; while(q!=NULL&temp=0) if(q-telenum
30、=num)temp=1; q=q-next; if(temp=0) p=new TeleBook; p-telenum=num; strcpy(p-name,name); m-next=p; m=p; if(temp=1) cout你所要录入的号码已被存入next=NULL; if(temp=0) cout添加信息成功!endl; return L;/*删除电话本联系人模块,提供按姓名和按号码删除,一次删除一条记录*/LinkList Delte(LinkList L)LinkList p=L,pre;coutn;if(p=NULL|p-next=NULL)cout该通讯录为空endl;ret
31、urn L; if(n=1) coutname; while(p!=NULL&(strcmp(p-name,name) pre=p; p=p-next; if(p=NULL&(strcmp(pre-name,name) cout电话本中不存在这个用户next=p-next; cout删除成功endl; if(n=2) coutnum; while(p-next!=NULL&p-telenum!=num) pre=p; p=p-next; if(p-next=NULL&p-telenum!=num) cout电话本中不存在这个用户next=p-next; cout删除成功next; ofstream mytext; mytext.open(mytext.txt); if(L=NULL|L-next=NULL)return; else while(q!=NULL)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026高校行政服务岗位招聘笔试模拟试题
- 胆囊健康科普指南
- 2026人工智能末日预言
- 卡路里健康宣教模板
- 小儿健康开场
- 2026岗位胜任力模型任职资格与晋升评审
- 市场拓展岗年度述职报告
- 沈阳二模试题及答案
- 关于2026年08月10日原材料采购调整的通知函4篇
- 向欺凌说不共筑阳光校园小学主题班会课件
- GA/T 1482-2026机动车驾驶人安全教育内容和方法
- 2026年北京市中考英语试卷真题(含答案及解析)
- 2026 年注塑车间主管上半年述职报告
- 安徽九华山旅游发展股份有限公司招聘笔试真题2025
- 2026年安徽省淮北市辅警考试试题解析及答案
- 【新教材】人教版(2024)八年级下册物理期末检测试卷 3套(含答案)
- 2026上海博物馆公开招聘12名工作人员备考题库及参考答案详解
- 2026年山东定期医师考核题库及答案
- 骨关节炎阶梯治疗专家共识(2026版)
- 小学6年级暑假每日学习打卡表(可直接打印使用)
- 2026年山东聊城市社区工作者考试试题解析及答案
评论
0/150
提交评论