77范文网 - 专业文章范例文档资料分享平台

C++期末复习题(4)

来源:网络收集 时间:2019-08-30 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

class Derived:public Base { int b; public:

Derived (int i):Base(i+1),b(i){ } void show(){ cout<

void main()

{ Base b(5),*pb; Derived d(1); pb=&d; pb->show();

}

39. 下列程序的输出结果第一行是Base::display1(),第二行是 Derived::display2()。 #include class Base{ public:

void display1(){ cout<<\ }

virtual void display2(){ cout<<\ } };

class Derived:public Base{ public:

void display1(){ cout<<\ } void display2(){ cout<<\ } };

void main()

{ Base *pbase; Derived d;

pbase=&d; pbase->display1(); p base->display2(); }

40. 下列程序的输出结果第一行是 Derived::display1() ,第二行是 Base::display2()。 #include class Base{ public:

virtual void display1(){ cout<<\ } void display2(){ cout<<\ } };

class Derived:public Base{ public:

void display1(){ cout<<\ } void display2(){ cout<<\ } };

void main()

{ Base *pbase; Derived d;

pbase=&d; pbase->display1(); pbase->display2(); }

●完善程序题

41. 以下程序的功能:斐波那契数列的第1和第2个数分别为0和1 ,从第三个数开始,每个数等于

其前两个数之和。求斐波那契数列中的前20个数,要求每行输出5个数。请完善程序。

16

请完善程序。

#include #include using namespace std; int main()

{ int f,f1,f2,i;

cout<<\斐波那契数列:\\n\ f1=0; f2=1;

cout<

f=___ f1+f2____;

cout<

if (___i%5==0____) cout<

f2=___ f ____;

}

cout<

}

42. 以下程序实现对输入的一个整数,求出各位数字之和。请完善程序。

#include using namespace std; int main() { int n,s=0;

cout<<\输入一个整数:\ cin>>n;

while( n ){ s+=n; n/=10或 n=n/10 ; }

cout<

43. 以下程序的功能是:采用插入排序的方法将数组s1中的数据升序排序,并删除相同的数据。函数insert()将整数x插入到已排序的数组p中(并仍保持升序),参数c为数组p中元素的个数。函数sort()将数组s中的数据按升序排序。先将s[0]放到临时数组tem[0]中,然后,依次从s中取一个元素,若该元素不在数组tem中,则调用函数insert()将该元素插入到数组tem中。最后将数组tem拷贝到数组s中。请完善程序。

#include using namespace std; void insert(int p[],int c,int x) { for(int i=0;ix) break; if(i==c) p[i]=x ;

17

else{ for(int j=c;j>i;j--)p[j]=p[j-1]; p[i]=x ; } }

int sort(int s[],int n) { int tem[200], len,j,k; tem[0]=s[0]; len=1; // len记录数组tem中的元素个数 for( j=1;j=len) { insert( tem,len,s[j] ); len=len+1; } } for(j=0;j

int main(void)

{ int s1[200]={34,22,11,55,66,30,22,100,66},n; n=sort(s1,9); for(int i =0;i

44. 下面的程序功能是:输入一行字符串,分别统计出该行字符串中大写字母、空格、数字及其他字符的个数。在函数count中,要求用引用数据类型做参数,带回该函数统计的结果。例如:当输入字符串 I’m 18 years old. 时,程序的运行结果为:

Caps : 1 // 大写字母1个 Space : 3 // 空格3个

Num : 2 // 数字字符2个 Other : 11 // 其他字符11个

请完善程序。 #include using namespace std;

void count( char[], int &, int &, int &, int & ) ; int main(void)

{ int caps=0, space=0, num=0, other=0; char str[80]; cout<<\:\

cin.getline(str,80); // 输入一行带空格的英文字符串 cout<

count(str, caps, space, num, other ); cout<<\

18

cout<<\ Caps : \ // 输出大写字母的个数 cout<<\ // 输出空格符的个数 cout<<\ Num : \ // 输出数字字符的个数 cout<<\ // 输出其他字符的个数 return 0; }

void count( char p[ ], int &caps1, int &space1, int &num1, int &other1 ) { for(int i=0; p[i] ; i++) { if (('A'<=p[i])&&( p[i]<='Z')) caps1++ ;

else if ((p[i]<='9') && (p[i]>='0')) num1++ ;

else if ( p[i]==' ' ) space1++ ; else other1++ ; } }

45. 下列程序通过重载运算符\,实现直接比较两个一维数组是否相等。请完善程序。#include class Arr{ int x[10]; public:

Arr() { for(int i=0;i<10;i++) x[i]=0; }

Arr(int *p) { for(int i=0;i<10;i++) x[i]= *p++ ; } int operator==( Arr a )

{ for(int i=0; i<10&&x[i]==a.x[i] ;i++);

return (i<10 ?0:1); } };

void main()

{ int a[10]={1,2,3,4,5,6,7,8,9,10},b[10]={1,2,3,4,5,6,7,8,9,10}; Arr arr(a),brr(b);

cout<<(arr==brr)<

46. 下列程序通过重载运算符\,实现两个一维数组的对应元素相加。请完善程序。 #include class Arr{ int x[10]; public:

Arr() { for(int i=0;i<10;i++) x[i]=0; }

Arr(int *p) { for(int i=0;i<10;i++) x[i]= *p++ ; } Arr operator+( Arr a ) { Arr temp;

for(int i=0; i<10;i++) temp.x[i]=x[i]+a.x[i] ;

19

return temp ; }

void show(){ for(int i=0; i<10;i++) cout<

void main()

{ int a[10]={1,2,3,4,5,6,7,8,9,10},b[10]={1,2,3,4,5,6,7,8,9,10}; Arr arr(a),brr(b),crr; crr=arr+brr; crr.show(); }

三、编写程序

1. 求2~300之间的素数,并以每行5个素数输出。

#include #include #include

using namespace std; int main(void ) { int i, k, n ; k=0 cout<=j+1) { cout<

2.求从键盘输入的10个整数中正数之积和负数之和以及平均值,并分别输出这三个值。#include using namespace std; int main( )

{ int i,a,mul=1,sum1=0,sum=0; cout<<\请输入十个数:\ for(i=0;i<10;i++) { cin>>a; sum1+=a;

20

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库C++期末复习题(4)在线全文阅读。

C++期末复习题(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/jiaoyu/704481.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: