C++试题.docx

上传人:b****6 文档编号:7711250 上传时间:2023-01-25 格式:DOCX 页数:20 大小:33.39KB
下载 相关 举报
C++试题.docx_第1页
第1页 / 共20页
C++试题.docx_第2页
第2页 / 共20页
C++试题.docx_第3页
第3页 / 共20页
C++试题.docx_第4页
第4页 / 共20页
C++试题.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

C++试题.docx

《C++试题.docx》由会员分享,可在线阅读,更多相关《C++试题.docx(20页珍藏版)》请在冰豆网上搜索。

C++试题.docx

C++试题

C++程序设计基础复习题(06/07/2)

一、填空题

1.在每个C++程序中都必须包含有这样一个函数,该函数的函数名为__________。

2.对象的三大基本特性是多态性、_____________、封装性。

3.表达式-1<=3<=-5值是:

_________

4.C++中函数重载的条件是:

函数名相同,而函数参数的类型、____________、或___________三者中至少有一个不同。

5.有以下程序段:

intm=0,n=1,x;

x=(m++)&&(++n);

执行完以上语句后,n的值为___________。

6.inta=6;表达式a+==a-=a*a运行后,a的值为:

____________

7.有宏定义:

#defineX(a)a*a

执行完y=X(2+3);之后,y的值为____________。

8.二维数组a[3][3]中,第二行的指针为_______________。

9.结构化程序设计的三种基本结构为:

顺序结构、_________结构和________结构。

10.设intx[]={1,2,3,4,5},*p=x;p+=2,则*p++值为_______。

11.假定stu为一个类,则语句stu(stu&x);为该类_________函数的原型说明。

12.设a=3,b=4,c=5,表达式:

a||b+c&&b-c的值为___________________________。

13.已知:

inta=7;floatx=2.5,y=4.7,则表达式:

x+a%3*(int)(x+y)%2%4的值为_______。

14.数据在内存中以其二进制机器数____________形式存放的;若有十进制数-10存贮在两字节内存中,则其对应的十六进制机器码为____________。

15.C++语言中标识符由字母、__________和__________组成其中不能以_________开头。

16.若有定义inta=3,b=4,则表达式'a'

17.动态内存的分配和释放采用关键词_____________和______________来描述。

18.若有定义inta=3,b=4则表达式(a==b*3.3),a%8的值为_________,a的值为____________。

19.令x为任意数,则其能被3整除但不能被2整除的逻辑表达式可写成____________。

20.逻辑(bool)类型变量的值只能是____________或______________。

21.执行下列语句中,sum变量的值是:

___________

intsum=0;

for(inti=0;i<10;i++,sum+=i);

22.当调用函数时,实参是一个数组名,则向函数传送的是________。

23.若由定义inta[10],*p则欲使p指向数组a,可采用__________和___________两种方法。

24.循环结构的算法其对应的语法表述有三种:

分别是____________、____________和____________语句。

25.break语句可以用在_________________和_________________中。

二.程序填空

1.10的阶乘之和

#include"iostream.h"

_________________

voidmain()

{

doublesum=0,n;

for(n=1;n<=10;n++)

__________________

cout<<"Thesumis:

"<

}

doublefactorial(doublem)

{

if(m<=1)

return1;

else

return__________________;

}

2.求出100以内的所有素数:

#include"iostream.h"

_______________

intprime_number(intm);

voidmain()

{

intn;

for(n=3;n

if(_______________)cout<

}

intprime_number(intm)

{

inti,flag=1;

for(i=2;i

if(m%i==0)

{

_______________;

break;

}

returnflag;

}

3.冒泡法排序

#include"iostream.h"

#defineN10

voidchange(_______________)

{

intt;

t=m;m=n;n=t;

}

voidarrange(_______________)

{

inti,j;

for(j=1;j

for(i=0;i

if(*(p+i)>*(p+i+1))

_______________;

}

voidmain()

{

inta[N],i;

for(i=0;i

cin>>a[i];

_______________;

for(i=0;i

cout<

}

4.输入字串,输出其中的字母:

#include"iostream.h"

#include"stdio.h"

voidfind(char*str1,char*str2)

{

inti,j;

for(i=0,j=0;*(str1+i)!

='\0';i++)

if(*(str1+i)>='a'&&*(str1+i)<='z'||*(str1+i)>='A'&&*(str1+i)<='Z')

{

*(str2+j)=*(str1+i);

_______________;

}

_______________;

}

voidmain()

{

char*a,*b;

_______________;

_______________;

gets(a);

find(a,b);

puts(b);

}

5.输出一个点

#include"iostream.h"

classpoint

{

public:

point(intx,inty);

~point()

{

cout<

}

private:

intX;

intY;

};

point:

:

point(intx,inty)

{

__________________;

__________________;

}

voidmain()

{

pointp1(10,10);

}

6.求两个正整数的最大公约数

#include"iostream.h"

voidchange(__________________)

{

intt;

if(a>b)

{

t=a;a=b;b=t;

}

}

voidmain()

{

intm,n,i,min_common_divisor;

cin>>m>>n;

change(m,n);

for(i=n;i>=1;i--)

if__________________)

{

min_common_divisor=i;

__________________;

}

cout<

}

7.计算两门功课的平均成绩

#include"iostream.h"

classstu

{

public:

voidfun_put();

voidfun_sum();

staticfloatfun_out_sum(intn);

private:

charname[10];

floatscore[2];

staticfloatsum[2];

};

floatstu:

:

sum[2]={__________________};

voidstu:

:

fun_put()

{

cout<<"请输入该生姓名"<

cin>>name;

cout<<"请输入该生的两门功课的成绩"<

cin>>score[0]>>score[1];

}

voidstu:

:

fun_sum()

{

sum[0]+=score[0];

sum[1]+=score[1];

}

floatstu:

:

fun_out_sum(intn)

{

return__________________;

}

voidmain()

{

stust[3];

inti;

floatscore_sum[2];

for(i=0;i<3;i++)

st[i].fun_put();

for(i=0;i<3;i++)

__________________;

for(i=0;i<2;i++)

{

score_sum[i]=stu:

:

__________________;

cout<

}

}

8.友元应用

#include"iostream.h"

classstudent

{

public:

intNo;

char*Name;

student(intn,char*str);

~student();

__________________

voidf_age(intag,student__________________);

private:

intage;

};

student:

:

student(intn,char*str)

{

No=n;

Name=str;

age=18;

}

student:

:

~student()

{

cout<<"学号"<

cout<<"姓名"<

cout<<"年龄"<

}

voidf_age(intag,student__________________)

{

p.age=ag;

cout<<"友元年龄"<

}

voidmain()

{

studentwang(5074401,"wang");

f_age(30,wang);

}

9.求3个数的最大值

#include

voidmain()

{

floata,b,c,t;

_______________;

if(a

if(a

cout<<”max=”<<________;

}

10.将10个数逆序存放

#include"iostream.h"

__________________

voidmain()

{inta[N],*p1,*p2;

for(p1=a;p1

cin<<*p;

p1=a;p2=a+N-1;

for(;p1

{t=*p1;

*p1=*p2;

*p2=t;

}

for(p1=a;p1

cout<<””<<*p;

cout<

}

11.求最高分且指出学号

#include"iostream.h"

classstudent

{

public:

intscore;

_______________;

voidput_number();

private:

charm_number[20];

};

voidstudent:

:

get_number(charnumber[])

{

inti;

for(i=0;number[i]!

='\0';i++)

m_number[i]=number[i];

_______________;

}

voidstudent:

:

put_number()

{

cout<

}

voidmain()

{

studentst[3];

inti,p;

char*number=_______________;

for(i=0;i<3;i++)

{

cout<<"请输入第"<

"<

cin>>number;

st[i].get_number(number);

cin>>st[i].score;

}

for(i=0,p=0;i<3;i++)

if(st[p].score<=st[i].score)

_______________;

cout<<"最高成绩为"<

st[p].put_number();

cout<<"同学的:

"<

}

三、阅读程序写出运行结果

1.

#include

classA

{public:

A(inth,intw):

he(h),we(w){}

~A(){cout<

private:

inthe,we;

};

classB:

publicA

{public:

B(inth,intw,inth1,intw1,intl):

A(h,w),obj1(h1,w1){len=l;}

~B(){cout<

private:

Aobj1;

intlen;

};

voidmain()

{

Bobj(1,2,3,4,5);

}

2.#include"iostream.h"

#include"stdio.h"

voidfind(charstr1[],charstr2[])

{

inti,j;

for(i=0,j=0;str1[i]!

='\0';i++)

if(str1[i]>='a'&&str1[i]<='z'||str1[i]>='A'&&str1[i]<='Z')

{

str2[j]=str1[i];

j++;

}

str2[j]=’\0’;

}

voidmain()

{

char*a,*b;

a=newchar[];

b=newchar[];

gets(a);

find(a,b);

puts(b);

}

输入字串如下:

aAtg234Hj\n

3.

main()

{

inti,j;

for(i=1;i<=5;i++)

for(j=1;j<=i;j++)

{

printf("%d*%d=%d",i,j,i*j);

if(i==j)printf("\n");

}

}

4.

#include

main()

{ intc;

  while((c=getchar())!

='\n')

  { switch(c-'2')

{  case0:

         case1:

putchar(c+2);

         case2:

putchar(c+4);break;

         case3:

putchar(c+3);

case4:

putchar(c+2);break;

}

 printf("\n");  

}

程序运行时,从第一列开始输入以下数据,代表一个回车符。

2345

5.

#include

voidmain()

{intx=1,y=0,a=0,b=0;

switch(x)

{case1:

switch(y)

{case0:

a++;break;

case1:

b++;break;

}

case2:

a++;b++;break;

case3:

a++;b++;

}

cout<<“a=”<

}

6.

voidm(intb[],intn);

main()

{

inta[10]={0,1,3,2,56,100,32,4,7,10};

inti;

m(a,10);

for(i=0;i<=9;i++)

printf("%8d",a[i]);

}

voidm(intb[],intn)

{

inti,j,p,t;

for(j=1;j<=n-1;j++)

{

p=0;

for(i=0;i<=n-j;i++)

if(b[i]>b[p])

p=i;

t=b[p];b[p]=b[n-j];b[n-j]=t;

}

}

7.

#include

voidswap(pa,pb)

int*pa,*pb;

{

int*t;

t=pa;pa=pb;pb=t;

}

voidmain()

{

inta,b,*p1=&a,*p2=&b;

cin>>a>>b;

cout<<"A=”<

if(a

swap(p1,p2);

cout<<"A=”<

}

当输入35<回车>

输出结果是:

8.

voidmain()

{

intj,k;

for(j=1;j<=5;j++)

{

for(k=1;k<=j;k++)

printf(“%d*%d=%-5d”,j,k,j*k);

printf(“\n”);

}

}

9.

voidmain()

{

inta=23;

floatx=2.6;

charc=’a’;

printf(“%d,%f,%c\n”,a,x,c);

printf(“%4d%10.3f%d,&c\n”,a,x,c,c);

}

10.

#include"iostream.h"

classstudent

{

public:

intage;

student(intn);

student(student&p);

~student();

private:

staticintNo;

};

voidmain()

{

studentp1(20);

studentp2(p1);

studentp3(p1);

}

intstudent:

:

No=1;

student:

:

student(intn)

{

age=n;

}

student:

:

student(student&p)

{

age=(p.age)++;

}

student:

:

~student()

{

cout<

"<

}

四、分析以下程序并回答问题:

classAclassB:

publicA

{public:

{public:

voidf1();voidf3();

inti;protected:

protected:

intm;

voidf2();private:

intj;intn;

private:

};

intk;

};

classC:

publicBvoidmain()

{public:

{Aa1;

voidf4();Bb1;

private:

Cc1;

intp;}

};

(1)在main函数中能否用b1.i,b1.j,b1.k引用类B对象b1中基类A的成员?

为什么?

(2)能否在main函数中用c1.f1(),c1.f2(),c1.f3()和c1.f4()调用f1,f2,f3,f4成员函数?

为什么?

(3)类C的成员函数f4能否调用基类A中的成员函数f1,f2和派生类中的成员函数f3?

为什么?

(4)如果把类B从类A的继承方式改为protected,回答问题

(1)。

五、编程

1.已知下列分段函数:

编写程序输入x的值,求y的值,并画出N-S流程图。

2.输入任意x,求y的值,画出N-S流程图。

3.求数列:

前20项的和,画出N-S流程图。

4.求

前20项的累加和。

5.有数列:

f1=1,

f2=1,

fn=fn-1+fn-2,求该数列第20项。

6.已知数列:

编一程序,求其前十项的和。

(画出流程图)

7.编写函数求n个数的最大值(n≥10),且指出其在整个数列中的位置。

8.统计一字串中字母、数字和其他字符的个数,并把字母存入另一个字串中。

9.编写一函数,将输入的n(n≥10)个整数中最小的数与第一个数对换,最大的数与最后一个数对换。

10.下面这个程序的功能是找出一个整型数组中元素的最大值。

程序给出了类的定义和主函数,请把它补充完整。

#include

classArray_max//声明类

{public:

voidset_value();//对数组元素设置值

voidmax_value();//找出数组中的最大元素

voidshow_value();//输出最大元素

private:

intarray[10];//整型数组

intmax;//max用来存放最大值

};

voidmain()//主函数

{Array_maxarry;

arry.set_value();

arry.max_value();

arry.show_value();

}

11.编写一函数将字符串中的第m个字符开始的字母字符复制成另一个字符串,(主函数如下)。

#include

#include

voidcopystr(m);

main()

{

intm;

charstr1[80],str2[80];

printf("Pleaseinputastring:

\n");

gets(str2);

printf("Inputm:

\n");

scanf("%d",&m);

if(strlen(str2)

\n");

else

{

copystr(str1,str2,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1