已阅读5页,还剩11页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
面向对象程序设计期末综合练习五(功能与改错)指出程序或函数的功能 1. #include void main() int i,s=0; for(i=2;i=30;i+=2) s+=i*i; couts=sendl; 2. #include #include #include void main() int i=10,a; while(i0) a=rand()%100+10; int j, k=int(sqrt(a)+1e-5); /sqrt(x)为求x的平方根函数 for(j=2; jk) couta ; i-; 3. void trans(int x) char a10; int i=0,rem; do rem=x%16; x=x/16; if(rem0) couta-i; coutendl; 4. #include double f1(int n) double sign=1,s=1; for(int i=2;ia; coutf1(a)endl; 5. double f1(double a, double b, char op) switch(op) case +: return a+b; case -: return a-b; case *: return a*b; case /: if(b=0) coutdivided by 0!endl; exit(1); else return a/b; default: coutoperator error!endl; exit(1); 6. #include #include void main() int x,y; cinx; y=int(sqrt(x); /sqrt(x)为求x的算术平方根 for(int i=1;i=y;i+) if(x%i=0) cout”x=”i*x/iendl; 7. #include void main() int i,p=1,s=0; int N; coutN; for(i=1;i=N;i+) p*=i; s+=p; coutsendl; 8. #include #include #include const N=10; int ff(int x, int y) int z; coutx+yz; if(x+y=z) return 1; else return 0; void main() int a,b,c=0; srand(time(0); /初始化随机数序列 for(int i=0;iN;i+) a=rand()%20+1; /rand()函数产生0-32767之间的一个随机数 b=rand()%20+1; c+=ff(a,b); cout得分:c*10endl; 9. int s1(int n) int x; if(n=1) x=1; else x=s1(n-1)+n*n; return x; 10. void fun5(char* a, const char* b) while(*b) *a+=*b+; *a=0; 11. template bool fun8(T a, int n, T key) for(int i=0;in;i+) if(ai=key) return true; return false ; 12. void f2(double a, int n) int i; double sum=0; for(i=0;in;i+) sum+=ai; sum/=n; for(i=0;i=sum) coutai ; coutendl; 13. void f4(char aMN) int c1,c2,c3; c1=c2=c3=0; for(int i=0;iM;i+) if(strlen(ai)=5 & strlen(ai)15) c2+; else c3+; coutc1 c2 c3endl; 14. void fun3(int aN, int m, int& row, int& col) int x=a00; row=col=0; for(int i=0;im;i+) for(int j=0;jx) x=aij; row=i; col=j; 15. int fun6(int m, int n, int b=2) if(mb & nstr2i) return 1; else return -1; if(str1i=str2i) return 0; else if(str1istr2i) return 1; else return -1; 18. IntNode* FindMax(IntNode *f) if(!f) return NULL; IntNode *p=f; f=f-next; while(f) if(f-datap-data) p=f; f=f-next; return p; 假定IntNode的类型定义为: struct IntNode int data; /结点值域 IntNode* next; /结点指针域 ; 19. int Count(IntNode *f) if(!f) return 0; int c=0; while(f) c+; f=f-next; return c; 假定IntNode的类型定义为: struct IntNode int data; /结点值域 IntNode* next; /结点指针域 ; 20. void Output(IntNode *f) if(!f) return; while(f) coutdatanext; coutendl; 假定IntNode的类型定义为: struct IntNode int data; /结点值域 IntNode* next; /结点指针域 ; 21. void Input(IntNode*& f) int n; coutn; while(n0); if(n=0) f=NULL; return; f=new IntNode; IntNode* p=f; cout”从键盘输入”nnext=new IntNode; cinp-data; p-next =NULL; p=f; f=f-next; delete p; 假定IntNode的类型定义为: struct IntNode int data; /结点值域 IntNode* next; /结点指针域 ; 22. int f(const char *s) int i=0; while(*s+)i+; return i; ; 23. char *f(char *s) int n=strlen(s); char* r=new charn+1; for(int i=0; i=a & sidata=x; /3行 p-next=NULL; /4行 if(list=NULL) return p; /5行 NODE *p1=list; /6行 while(p1-next!=NULL) p1=p1-next; /7行 p1=p; /8行 return list; 错误行的行号为_和_。分别改正为_和_。 2. 在下面的定义中,NODE是链表结点的结构,addToList则是一函数,其功能是:将一个值为x的新结点添加到以plist为表头指针的链表的首部(即第一个结点的前面)并返回表头指针。函数中有两处错误,指出错误所在行的行号并提出改正意见。struct NODE int data; NODE *next; NODE* adndToList(NODE * plist, int x) /1行 NODE *p; /2行 *p=new NODE; /3行 p-data=x; /4行 p-next=NULL; /5行 plist=p; /6行 return p; /7行错误行的行号为_和_。分别改正为_和_。 3. 假定要求下面程序的输出结果为“11/15”,其主函数中存在着三行语句错误,请指出错误语句行的行号并改正错误行。 #include class Franction /定义分数类 int nume; /定义分子 int deno; /定义分母 public: /把*this化简为最简分数,具体定义在另外文件中实现void FranSimp(); /返回两个分数*this和x之和,具体定义在另外文件中实现Franction FranAdd(const Franction& x); /置分数的分子和分母分别0和1 void InitFranction() nume=0; deno=1; /置分数的分子和分母分别n和d void InitFranction(int n, int d) nume=n; deno=d; /输出一个分数void FranOutput() coutnume/denoendl; ;void main() /1行 /2行 Franction a,b,c; /3行 a.InitFranction(6,15); /4行 b.InitFranction(1); /5行 c.InitFranction(); /6行 c=FranAdd(a,b); /7行 coutc.nume/c.denoendl; /8行 /9行错误行的行号为_、_和_。分别改正为_、_和_。 4. 假定要求下面程序的输出结果为“23/20”,其主函数中存在着三条语句错误,请指出错误语句行的行号并改正。 #include class Franction /定义分数类 int nume; /定义分子 int deno; /定义分母 public: /把*this化简为最简分数,具体定义在另外文件中实现void FranSimp(); /返回两个分数*this和x之和,具体定义在另外文件中实现Franction FranAdd(const Franction& x); /置分数的分子和分母分别0和1 void InitFranction() nume=0; deno=1; /置分数的分子和分母分别n和d void InitFranction(int n, int d) nume=n; deno=d; /输出一个分数void FranOutput() coutnume/denoInitFranction(6,15); /5行 b.InitFranction(3,4); /6行 Franction c; /7行 c.InitFranction(); /8行 c=a.FranAdd(b); /9行 coutc.FranOutput()endl; /10行 /11行错误行的行号为_、_和_。分别改正为_、_和_。 5. 下面是一个类的定义,存在着3处语法错误,请指出错误行的行号并改正。class CE /1行 private: /2行 int a,b; /3行 int getmin() return (ab? a:b); /4行 public /5行 int c; /6行 void SetValue(int x1,int x2, int x3) /7行 a=x1; b=x2; c=x3; /8行 ; /9行 int GetMin(); /10行; /11行int GetMin() /12行int d=getmin(); /13行return (dc? d:c); /14行 /16行错误行的行号为_、_和_。分别改正为_、_和_。 6. 下面程序段第4-10行中存在着三条语句的语法错误,请指出错误语句的行号并改正。 class A /1行 int a,b; /2行 const int c; /3行 public /4行 A():c(0);a(0);b(0) /5行 A(int aa, int bb) c(aa+bb); a=aa; b=bb; /6行 ; /7行 A a,b(1,2); /8行 A *x=&a, &y=b; /9行 A *z=new A, w10; /10行错误行的行号为_、_和_。分别改正为_、_ 和_。 Public: A():c(0),a(0),b(0) A(int aa, int bb): c(aa+bb) a=aa; b=bb; 7. 下面程序段第4-9行中存在着三条语句错误,请指出错误语句的行号并说明原因。 class A /1行 int a,b; /2行 const int c; /3行 public: /4行 A() a=b=c=0; /5行 A(int aa, int bb):c(aa+bb) a=aa; b=bb; /6行 ; /7行 A a,b(1,2,3); /8行 A x(2,3), y(4); /9行错误行的行号为_、_和_。错误原因分别为_、_和_。 8. 下面程序段第10-17行中存在着三条语句错误,请指出错误语句的行号并说明原因。 class A /1行 int a; /2行 public: /3行 A(int aa=0):a(aa) /4行 ; /5行 class B /6行 int a,b; /7行 const int c; /8行 A d; /9行 public: /10行 B():c(0) a=b=0; /11行 B(int aa, int bb):d(aa+bb) /12行 a=aa; b=bb; c=aa-bb; /13行 /14行 /15行 B a,b(1,2); /16行 B x=a,y(b),z(1,2,3),; /17行 错误行的行号为_、_和_。错误原因分别为_、_ 和_。 9. 假定要求下面程序输出结果为“d=800,f=60”,在第4-23行中存在着三条语句错误,请指出错误语句的行号并改正。#includeclass A /1行 int a10; int n; /2行 public: /3行 A(int aa, int nn): n(nn) /4行 for(int i=0; in; i+) aai=ai; /5行 /6行 int Get(int i) return ai; /7行int SumA(int n); /8行; /9行int A:SumA(int n) /10行int s=0; /11行for(int j=0; jn; j+) s+=aj; /12行return s; /13行 /14行void main() /15行 int a=2,5,8,10,15,20; /16行 A x(a,6); /17行 int d=1; /18行for(int i=0; i4; i+) d*=x.ai; /19行 int f=SumA(6); /20行coutd=d,; /21行coutf=fendl; /22行 /23行错误行的行号为_、_和_。5 19 20分别改正为_、_和_。 10. 下面是分数类fract的定义及测试主程序,在类定义及其
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026二上数学第五单元核心素养课件
- 病理个人述职报告(3篇)
- 2026北师大二下买电器说课课件
- 2025-2026学年九年级物理(天津专用)上学期期中模拟试卷(含答案)
- 人教版六年级上册数学 第六单元总教案 教案
- 2026北师大二下小蝌蚪成长说课课件
- 葡萄糖-6-磷酸脱氢酶检测试剂注册审查指导原则(2026年第22号)
- 创伤病人失血量评估和成分输血
- Unit 4 The Earth Section 3 Expressing and communicating ideas Speaking 教学设计2026-2027学年沪教版英语七年级上册
- 2026四下数学观察物体二原创课件
- (正式版)DB61∕T 1624-2022 《公路护栏设置规范》
- 数字治理 课件全套 彭飞 第1-12章 数字治理导论 - 数字环境治理
- 肥胖与骨骼健康课件
- 2025-2030中国无人机行业应用场景与市场前景评估报告
- 环境现场采样培训
- 智能建造概论课程介绍
- 精神患者冲动护理
- DB23-T 2319-2019 红松人工林大径材定向培育技术规程
- 危重症患者镇静镇痛护理
- 工艺岗转正述职报告
- 乡镇消防安全知识培训课件
评论
0/150
提交评论