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
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
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
{ 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 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 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 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 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 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 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 using namespace std; int main(void ) { int i, k, n ; k=0 cout< 2.求从键盘输入的10个整数中正数之积和负数之和以及平均值,并分别输出这三个值。#include { int i,a,mul=1,sum1=0,sum=0; cout<<\请输入十个数:\ for(i=0;i<10;i++) { cin>>a; sum1+=a; 20 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库C++期末复习题(4)在线全文阅读。
相关推荐: