收藏 分销(赏)

电大c形考册及答案.doc

上传人:天**** 文档编号:10555220 上传时间:2025-06-02 格式:DOC 页数:24 大小:102.04KB
下载 相关 举报
电大c形考册及答案.doc_第1页
第1页 / 共24页
电大c形考册及答案.doc_第2页
第2页 / 共24页
点击查看更多>>
资源描述
电大c++形考册及答案 教育部人才培养模式 改革和开放教育试点 C++语言程序设计形成性考核册 学校名称: 学生姓名: 学生学号: 班 级: 中央广播电视大学出版社 姓 名: 学 号: 得 分: 教师签名: C++语言程序设计作业1 一、填空题 1.C++语言中的每条基本语句以____;_______作为结束符,每条复合语句以____}______作为结束符。 2.用于输出表达式值的标准输出流对象是____cout_______,用于从键盘上为变量输入值的标准输入流对象是_______cin_____________。 3.当不需要函数返回任何值时,则应把该函数类型定义为________void_______。 4.执行”cout<<143<<‘+’<<18<<‘=‘<<143+18<<endl;”语句后得到的输出结果 为____143+18=161_________________。 5.执行”cout<<“nign”<<“chen”<<38<<endl;”语句后得到的输出结果为___nignchen38_____________。 6.在每个C++程序中都必须包含有这样一个函数,该函数的函数名为____main________。 7.C++源程序文件的缺省扩展名为___.cpp______, 由C++源程序文件编译而成的目标文件的缺省扩展名为____.obj___________,由C++目标文件连接而成的可执行文件的缺省扩展名为_.exe_________。 8.程序运行中需要从键盘上输入多于一个数据时,各数据之间应使用_空格___或__回车___符号作为分隔符。 9.十进制数25表示成符合C++语言规则的八进制和十六进制数分别为___031______ 和_____ox19______。 10.在C++语言中,用转义字符序列____\n_______或操纵符_endl________表示输出一个换行符。 11.执行”Cout<<char(‘b’+2)<<endl;”语句后得到的输出结果为____d________。 12.执行”cout<<char(‘K’-3)<<endl; “语句后得到的输出结果为____H________。 13. 已知’A’~’Z’的Ascll码为65~90,当执行”int x=‘H’+5;”语句后x的值为____77_____。 14.已知’A’~’Z’的Ascll码为65~90,当执行”char ch=16*5+2; cout<<ch<<endl;”语句序列后,得到的输出结果为_____R_____________。 15假定一个枚举类型的定义为”enum RA{xa,xb,xc,xd};”,则执行”cout<<“xc=”<<xc<<endl;”语句得到的输出结果为____xc=2___________。 16.假定一个枚举类型的定义为”enum RB{ab,ac=3,ad,ae } x=ad;”,则x的值为______4_____。 17.char、short和int类型的大小分别为___1B______、_2B____和____4B___。 18.float和double类型的大小分别为_____4B________和____8B__________。 19.十进制数128和-3.26的类型分别为____int_______和___flaot________。 20.若需要定义一个标识符常量,并且使C++能够进行类型检查,则应在定义语句的开始使用保留字_#define__。 21.使用const语句定义一个标识符常量时,则必须对它同时进行__初始化___。 22.执行”int x=45,y=16;cout<<x/y<<‘ ‘<<x%y<<endl;”语句序列后得到的输出结果为_____2 13____. 23.假定x=10,y=6,则表达式2+x++和++y*3的值分别为_____12______和_____21______。 24.算术表达式 对应的c++表达式为_(x*x+y*y)/(2-x*y)_。 25.算术表达式 对应的c++表达式为__x*y*y/3*a+4*b-1__。 26.表达式float (25)/4和int(14.6)%5的值分别为_6.25___和___4__。 27. 表达式a=a+b表示成复合赋值表达式为__a+=b___。 28.表达式a=a+l表示成增量表达式为__a++或++a_____。 29.增量表达式++y表示成赋值表达式为___y=y+1或y+=1___。 30.关系表达式(x==0)的等价表达式为_____x!=1__________。 31.关系表达式(x!=0)的等价表达式为__x=1__。 32.关系表达式x+y>z的相反表达式为_____x+y!>Z或_x+y<=Z____。 33.逻辑表达式x>5&&x<10的相反表达式为____x<=5 || x>=10___。 34.逻辑表达式a>b|| b==5的相反表达式为____a<=b &&b!=5______。 35.若x=15,y=40,则 x>y 和 x<=y的逻辑值分别为____0______和__1___。 36.假定x=5,则执行”a=(x? 10: 4*2);”语句后a的值为___10__。 37.假定a=5,则条件表达式”a==0 ? 10:20”的值为____20___。 38;执行”typedef int DataType;”语句后,在使用int定义整型变量的地方都可以使 用 DataType 来定义整型变量。 39.设x和y均为bool量,则x&&y为真的条件是_xture,yture____。 40.设x和y均为bool量,则x|| y为假的条件是_xfalse,yfalse___。 二、写出下列程序运行后的输出结果 1.# include <iostream.h> void SB(char ch){ switch(ch){ case ‘A’:case ‘a’: Cout<<“well!”;break; Case ‘B’: case ‘b’: Cout<<“good!” ; break; Case ‘C’:case ‘c’: cout<<“pass!”;break; default: cout<<“bad!”;break; good!Pass!Bad!Well! } } void main(){ char a1=‘b’,a2=‘C’,a3=‘f’; SB(a1);SB(a2);SB(a3);SB(‘A’); cout<<endl; } 2.#include <iostream.h> #include <stdlib.h> double SD (int a,int b,char op){ double x; switch(op) { case ‘+’: x=double(a)+b; break; case ‘-’: x=double(a)-b; break; 8 240 3.2 case ‘*’: x=doubel(a)*b; break; case ‘/’: if(b) x=double(a)/b; else exit(1); break; default:exit(1);} return x;} void main( ){ int x=20,y=12; cout<<SD(x,y,’-’)<<‘ ‘; cout<<SD(x,y,’*’)<<‘ ‘; cout<<SD(x+y,y-2,’/’)<<endl; } 3.#include <iostream.h> void main(){ int s=0; for(int i=l; i<6;i++) S+=i*i; cout<<“s=”<<s<<endl; s=55 } 4.# include <iostream.h> void main(){ int s=0; for(int i=1;i++){ if(s>50)break; if(i%3==0)s+=i; cout<<“i,s=”<<i<<“,”<<s<<endl; } i,s=19,63 5.# include <iostream.h> void main() { int s1=0,s2=0; for(int i=0; i<10;i++) if(i%2) s1+=i; else s2+=i; cout<<s1<<‘ ‘<<s2<<endl; } 25 20 6.# include <iostream.h> void main() { int n=10,y=1; while(n--) { y++; ++y;} cout<<“y*y=”<<y*y<<endl; } y*y=441 三、写出下列每个函数的功能 1.# include <iostream.h> int SA(int a,int b) { if(a>b) return 1; else if(a==b) return 0; 返回值 1 a>b else return -1; 0 a=b } -1 a<b 2. int SC(int a,int b,int c){ if(a>=b&&a>=c) return a; if(b>=a&&b>=c) return b; if(c>=a&&c>=b) return c; } 返回值int a,b,c中最大值 3. int SE(int n) { //n为大于等于1的整数 int x; cin>>x; if(n==1) return x; int m=x; 输入x当n=1返回地址x while(--n){ 否则求输入x值之和放到m中并 cin>>x; m+=x; 返回m处 } return m ; } 4. double SF(double x,ini n) { //n为大于等于0的整数 double p=1,s=1; for(int i=1;i<=n;i++){ p*=X; s+=p/(i+1); } return s; S=1+x/2+x*x/2+x*x*x/3+…xn/n+1 } 5. # include <math.h> bool SG(int x){ //x为大于等于2的整数 int a=int(sqrt(x));//取x的平方根 int i=2; while(i<=a){ if(x%i==0)break; 判断x是否为质数是返回1,否返回0 i++; } if(i<=a) return 0;else return 1; } 6. float FH() { float x, y=0, n=0; cin>>x; while(x!=-100){ 输入n个实数计算它的平均值以-100作结束 n++; y+=x; cin>>x; } if(n==0) return y; else return y/n; } 姓 名: 学 号: 得 分: 教师签名: C++语言程序设计作业2 一、填空题 1.假定一个一维数组的定义为”char *a[5];”,则该数组所含元素的个数为____5___ 所占存储空间的字节数为____5____. 2.假定一个二维数组的定义为”int a[4][6];”,则该数组所含元素的个数为___24_____, 所占存储空间的字节数为____96______ 。 3.假定一个二维数组的定义为”char a[8][8];”,则该数组所含元素的个数为_ 64_____,所占存储空间的字节数为__64_______。 4. 执行”typdef float ABC[10];”语句把ABC定义为具有10个单精度元素的__数组____。 5. 在函数外定义的变量称为___全局_ __变量,若没有被初始化则系统隐含对它赋初值0__。 6.函数中的形参变量的初值由 调用函数调用 该函数时获得。 7.函数调用时,实参向形参的传送分为_传值__ 和_传址___两种传送方式。 8. 变量分为全局和局部两种, 局部 _变量没有赋初值时,其值是不确定的。 二、给出下列程序运行后的输出结果 1.# include <iostream.h> void main() { int a[10]={12,39,26,41,55,63, 72,40,83,95}; int i0=0,i1=0,i2=0; for(int i=0;i<10; i++) switch (a[i]%3) { case 0:i0++; break; case 1: i1++; break; case 2:i2++; } cout<<iO<< ‘ ‘<<il<< ‘ ‘<<i2<<endl; } 4 2 4 2 . # include<iostream.h> # include<string.h> void main( ) { char a[5]={ “student”,”worker”,”cadre”,”apple”,”peasant” } ; char *pl,*p2; pl=p2=a[0]; for(int i=0;i<5;i++) { if(strcmp(a[i],pl)>0) pl=a[i]; if(strcmp(a[i],p2)<0) p2=a[i]; } cout<<pl<<‘ ‘ <<p2<<endl; work apple } 3 . # include <iostream.h> void main( ) { int a[8]={36,73,48,14,55,40,32,66}; int bl,b2; bl=b2=a[0]; for(int i=l; i<8; i++) if(a[i]>bl ) { if(bl>b2) b2=bl ; bl=a[i] ; } cout<<bl<<‘ ‘<<b2<<endl; 73 36 } 4 . # include<iostream.h> void main( ) { char a[]=“aabcdaabacabfgacd”; int il=0,i2=0,i=0; while (a[i]) { if(a[i]==‘a’) il++ ; if(a[i]==‘b’) i2++ ; i++; } 7 3 cout<<i1<<‘ ‘ <<i2<<endl; } 5 . # include <iostream.h> void nain( ) { char a[]=“abcdabcabdaeaf”; int b[5]={0} , i=0; while(a[i]) { switch(a[i]) { case ‘a’: b[0]++; break; case ‘b’: b[l]++; break; case ‘c’: b[2]++; break; case ‘d’: b[3]++; break; default: b[4]++; } i++; } for(i=0;i<5;i++) cout<<b[i]<<‘ ‘ cout<<endl ; } 5 3 2 2 2 6. # include<iostream.h> void main() { int a[10]={76,83,54,62,40,75,80,92,77,84}; int b[4]={60,70,90,101}; int c[4]={0}; for(int i=0;i<10;i++) { int j=0; while(a[i]>=b[j]) j++; c[j]++; } for(i=0; i<4; i++) cout<<c[i]<<’ ‘; cout<<endl; } 2 2 0 1 7. # include<iostream.h> void main( ) { int a[3][4]={{1,2,7,8},{5,6,11,12},{24,10,3,4}}; int m=a[0][0]; int ii=0,jj=0; for(int i=0;i<3;i++ ) for(int j=0;j<4;j++ ) if(a[i][j]>m) {m=a[i][j]; ii=i; jj=j; } cout<<ii<<‘ ‘ <<jj<<‘ ‘ <<a[ii][jj]<<endl; } 2 0 24 8 . # include<iostream.h> void main(){ int a=10,b=20; cout<<a<<‘‘<<b<<endl; { a*=4 int b=a+35; cout<<a<<‘‘<<b<<endl; 10 20 } 40 75 cout<<a<<‘‘<<b<<endl; 40 75 } 三、写出下列每个函数的功能 1.int WC(int a[],int n,int k){ int c=0; for(int i=0;i<n ;i++) if(a[i]>=k)c++; 统计数值a中元素大于等于k的个数 return c; } 2.# include<iostream.h> void WA(int a[],int n){ for(int i=0;i<n-1;i++){ int k=i ; for(int j=i+1;j<n;j++) 对数组a中n个元素从大到小排序 if(a[j]<a[k])k=j; int x=a[i]; a[i]=a[k]; a[k]=x; } } 3.# include<iostream.h> template<class TT> int WG(TT a,TT b){ if(a>b) return 1; else if(a==b)return 0; else return -1; 用函数模板比较a和b的大小 } 4.# include<iostream.h> template<class Type> Type WD(Type a[],int n){ Type m=a[0]; for(int i=0; i<n;i++) if(a[i]>m) m=a[i]; 用函数模板对数组中n个元素取最大值 return m; } 5.templatee<class Type> void WE(Type a[],Type b[],int n){ for(int i=0;i<n ;i++) b[i]=a[i]* 2; 用函数模板把数组a中n个元素x2,再逐一传到b数组中 } 姓 名: 学 号: 得 分: 教师签名: C++语言程序设计作业3 一、填空题 1.假定P所指对象的值为28,p+1所指对象的值为62,则*p++的值为__28___。 2.假定p所指对象的值为28,P+l所指对象的值为62,则*++p的值为__62__。 3.假定p所指对象值为25,p+l所指对象的值为50,则执行”(*p)++;”语句后,p所指对象的值为_25___。 4.假定p所指对象的值为25,p+1所指对象的值为50,则执行” *(p++);”语句后,p所指对象的值为_50__。 5.假定a是一个指针数组,则a+i所指对象的地址比a地址大_i*sizeof(a[0])_ 字节。 6.假定a是一个一维数组,则a[i]的指针访问方式为____*(a+i)_ _。 7.假定a是一个二维数组,则a[i][j]的指针访问方式为___*(a[i]+j)___。 8.假定a是一个一维数组,则a[i]对应的存储地址(以字节为单位)为 &a[0]+i*sizeof(a[0][0])_。 9.假定一个二维数组为a[M][N],则a[i][j]对应的存储地址(以字节为单位)为_&a+(i*4j)*sizeof(a[0][0])。 10.假定一个二维数组为a[M][N],则a[i]的地址值(以字节为单位)为&a[0][0]+(i*aj)*sizeof(a[0][0])。 11.假定p是一个指向float型数据的指针,则p+l所指数据的地址比p所指数据的地址大4_字节。 12.假定a为一个字符数组名,则元素a[8]的字节地址为_&a+8_。 13.假定a为一个整型数组名,则元素a[4]的字节地址为__&a+16___。 14.假定一个结构类型的定义为”struct A {int a,b;short c; A * d;};”,则该类型的大小为10_字节。(A * d含义:允许用A的指针类型。指针占4字节) 15.假定一个结构类型的定义为”struct B {int a[8];char *b;”,则该类型的大小为36节。 16.假定一个结构类型的定义为”struct D { int a ; union { int b;double c;} D*d[3];};” 则该类型的大小为__12 _字节。 17.假定要动态分配一个类型为Worker的具有n个元素的数组,并由r指向这个动态数组,则使用的语句为_worker*r=new(worker[n]_。 18.假定要访问一个结构x中的由a指针成员所指向的对象,则表示方法为_&a 对象。 19.假定要访问一个结构指针p所指对象中的b指针成员所指的对象,则表示方法为 _ p b 对象。 二、给出下列程序运行后的输出结果 1.# include<iostream.h> void main(){ int a[8]={7,9,ll,13,3,8,15,17}; int *p=a ; for(int i=0; i<8;i++){ cout<<setw(5)<<*p++; if(i+1)%4==0)cout<<endl; 7 9 11 13 } 3 8 15 17 } 2.# inelude<iostream.h> void main(){ int a[5]={3,6,15,7,20}; int *p=a; for(int i=0;i<5;i++) cout<<setw(5)<<*p++; cout<<endl; for(i=0;i<5;i++) cout<<setw(5)<<*--p; cout<<endl; 3 6 15 7 20 } 20 7 15 6 3 3.# include<ioatream.h> void main(){ int a[8]={4,8,12,16,20,24;28,32}; int *p=a ; do { cout<<*p<<‘‘; p+=3; } while(p<a+8); cout<<endl; 4 28 } 4.# include<iostream.h> void main(){ int x=20,y=40,*p ; p=&x; cout<<*p<<‘‘; *p=x+10; p=&y; cout<<*p<<endl; *p=y+20; cout<<x<<‘‘<<y<<endl; 20 40 } 30 60 5、# include<iostream.h> int LA(int * a,int n){ int s=0; for(int i=0; i<n ;i++) s+=a[i]; return s; } void main(){ int a[]={5,10,15,20,25,30}; int b=LA(a,5); int c=LA(a+3,2); cout<<b<<‘‘<<c<<‘‘<<b+2*c<<endl ; 75 45 165 } 6.# include<iostream.h> void LC(int a,int b){ int x=a; a=b;b=X; cout<<a<<‘‘<<b<<endl; } void main() { int x=15,y=36; LC(x,y);cout<<x<<‘‘<<y<<endl; 36 15 } 15 36 7.# include<iostream.h> void LF(int &x,int y){ X=X+y ; y=X+y; cout<<“x=“<<x<<“,y=“<<y<<endl; } void main(){ int x=5,y=8; cout< <“x= “<< x<<“,y= “<<y<< endl; x=5 y=8 LF(x,y) ; x=13 y=21 cout< <“x=“<<x<<“,y=“<<y<<endl; x=13 y=21 } 8 . # include< iomanip.h> void LG(int * & a,int & m) { a= new int[m] ; int * p=a; for(int i=0; i<m; i++) *p++=2*i+l; } void main( ) { int *p,h=5; LG(p, n) ; for(int i=0; i<n; i++) cout< <p[i]< < ‘ ‘ ; cout<<endl; “ delete[] P ; 1 3 5 7 9 } 9 . # include<iomanip.h> void LH(int *a,int n) { int * p=a+n-l; while(a<p) { int x= *a; *a=*p; *p=x; a++;p--; } } void main() { int * d=new int[5] ; int i; for(i=0; i<5; i++) { d[i]=2*i+3; cout<<setw(5)<<d[i]<<‘ ‘ ; } cout<< endl ; LH(d,5); for(i=0; i<5; i++) { cout<<setw(5)<<d[i]<< ‘ ‘ ; } cout<<endl; 3 5 7 9 11 delete [ ] d; 11 9 7 5 3 } 1O . # include<iostream.h> struct Worker { char name[15] ; int age; float pay; }; void main( ) { Worker x= {“weirong’, 55 , 640 }; weirong 55 640 Worker y, *p; weirong 60 630 y=x; p=&x; cout<<y.name<< ‘ ‘ <<y.age<< ‘ ‘<<y.pay<<endl; cout<<p->name<<‘ ‘ <<p->age+5<<‘ ‘<<p->pay-10<<endl; 11 . # include<iostream.h> # include<string.h> struct Worker { char name [15] ; int age; float pay; }; void main( ) { Worker x;
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 远程教育/电大

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服