C程序设计基础第二版高等教育出版社习题答案.doc

上传人:s****u 文档编号:12811927 上传时间:2020-05-26 格式:DOC 页数:43 大小:223KB
返回 下载 相关 举报
C程序设计基础第二版高等教育出版社习题答案.doc_第1页
第1页 / 共43页
C程序设计基础第二版高等教育出版社习题答案.doc_第2页
第2页 / 共43页
C程序设计基础第二版高等教育出版社习题答案.doc_第3页
第3页 / 共43页
点击查看更多>>
资源描述
well, with the effectiveness of services to defend the interests of the masses. Third, we should strive to do well. To achieve good practical results, the key is to know the law, to grasp the laws and using laws. Office of economic development, social progress, there are rules to follow. Only act according to the law, to overcome blindness and strengthening initiative, creative. Working in the Office, we should be good at analyzing the essence of things, to find regular thing, change from passive to active, to seek one. Investee 2. establishment of evaluation system. Evaluation system is essentially an incentive mechanism, the evaluation was objective and fair, reasonable, and can stimulate a persons energy, mobilizing peoples enthusiasm or be misleading, dampened the enthusiasm of people. At present, the concept of people-oriented people, but to establish and perfect evaluation system is still lagging behind. Work in this area should be seriously caught up. Three emphasis on to take advantage of. Is a focus on standards and scientific. A fundamental point of the evaluation criteria, is to keep contact, development, comprehensive eye evaluation of cadres. Office work, both record and potential performance; both subjective efforts, take another look at the objective condition; both pragmatic enough, take another look at the retreat level. Second, focus on participating the breadth of the subject. Evaluation of cadres must give full play to democracy, cadres and the masses handing over evaluation. Office work services leadership and service base, serve the people, then nominated the main object will contain a variety of services, so that assessment was accurate and to avoid one-sidedness. Third, pay attention to the seriousness of the conclusion. The use of evaluation findings is mainly embodied in his direction. Permitted to choose one, it would be tantamount to establishing a banner. Evaluation cant take care of the balance, otherwise it would undermine the offset the positive significance of evaluation. Office of the party committees at all levels should take the findings as on the cadres bonus-penalty, an important basis for future movements, incentive and restraint effect of making evaluations. 3. Prevention of understanding infidelity. People-centered development, concrete is dialectical,. In practice in the process, to prevent one-sided and still look, from actual people. One is to use dialectical point of view to understand. Humanist is the core of the scientific Outlook on development, remains the development of the scientific Outlook on development emphasizes, is still taking economic construction as the Center. Any departure from the development of thought and action, is a misinterpretation and misunderstanding of people-oriented. Adhere to people-oriented both to overcome one-sidedness of the old, but also to prevent one-sidedness, not accelerating the development of people and the opposition. Must be very clear, people oriented and speeding up development is not opposed, but consistent, we should never bestrengthened password centralized management, enhanced password communication network and the construction of electronic Government Affairs network, strengthen the translation of code cables work, strengthen the management of important files, make sure that the password is absolutely safe, ensure that Cryptography is absolutely clear. History of work to write a history of the original, compiled a book called changde Yearbook, out of a journal of the history of the publication, production of a song of the past after reading forums, changde, a story of peoples hard work and exhibition. Archives to go the path of managing archives according to law, strengthen the construction of all types of archives, with the focus on key project archives, private enterprises, credit and Community Archives Archives, making full use of electronic means of technology and network, and promote the standardization and legalization of archives and information. Investee 4. insist on the logistics, will be around strong strong reforms, cadres of service management. Office workers is to be for this, deepen reform, strengthen management, and continuously meet the cadres of the organs function properly and the need for material and cultural life. One is to enhance the logistical support capability. Adapt to the new situation of the market economy, making full use of social power, flexibility in the use of market mechanisms and accelerate the process of marketization and socialization of logistic services. Adhere to the institutionalization of the logistics management, establish and implement a financial management system, trolley management systems, health management systems, security systems, such as the logistics management system, strengthen the management of human, financial, material, and logistical services standardized and institutionalized track. Secondly, we should strengthen the management of construction. Increasing investment, improving the working conditions of organs; overall civilization create an activity, and constantly improve the level of civilization; to promote greening, lighting, purification, flowers, landscaping, Office workers create a good environment for work and life. Third, to improve staff welfare. To in policy allows of range within, trying to for Office cadres workers solution housing, and couples separation, and children employment, aspects of actual difficult provides help, has conditions of place and units, also should appropriate to increased cadres workers of welfare, real do career left people, feelings left people, appropriate treatment left people, let General Office staff in pay hard efforts of while, feel organization of warm, get due of affordable. Investee 5. insist on the team, will focus on stimulating vitality who care and respect for people. To Office cadres for the cut . Learning style, strengthen the theoretical knowledge, optimize the structure of knowledge, and continuously improve the quality of cadres and workers. Attention to their physical health, and actively carry out regular wholesome recreational习题一 参考答案第1章 C+语言概述一、选择题:1. D 2. A 3. D 4. A 5. A 6. D 7. C 8. B 9. B二、改后如下:#include /预处理指令,原程序缺少“#”号int main() /定义主函数,原程序缺少返回值类型“int”(也可为“void”) coutHello World! n; /语句必须以“;”结尾。原程序缺少“;”号return 0; /原程序缺少返回值语句运行后输出: Hello World!三、简答题1、解答:在较大或较复杂的C+程序中,加上相关的注释信息,解释相应的程序,可提高程序的可读性。2、C+语言有两种注释: (1)C+型注释。从“/”开始,该行中所有字符都被作为注释处理。 (2)C型注释。“/*”表示注释的开始,“*/”表示注释的结束,在“/*”、 “*/”之间的字符均被视作注释。3、C型注释主要用于大块注释,而且C+型注释可以嵌在C型注释中。四、程序设计题1、编写一个程序,在屏幕上输出“I can use C+”。解:#includeint main() coutI can use C+ n;return 0;运行后输出: I can use C+2、参照例题, 设计一个C+程序,输入两门课程的成绩,求总成绩。解:# include main() int s1,s2,sum;couts1s2;sum=s1+s2;coutsum=sumn;3、参照例题,设计一个C+程序,输入a、b两个整数,输出其中的最小数。解:#include void main(void) int min(int x,int y);int a,b,m;coutab;m=min(a,b);coutmin=mendl;int min(int x,int y)int z;if (xy) z=x;else z=y;return z;习题二 参考答案第2章 数据类型和表达式一、 选择题1. C 2. B 3. B 4.B 5.A 6 B 7.D 8.C 9. D 10. C11.D 12.A 13.D 14.A 15.B 16.B 17.B 18.C 19.A二、 填空题1、基本类型 导出类型 用户定义类型2、63、iostream #include 4、一个int型数据占用的字节数5、06、77、1008、?:9、false false true10、(50 x )& (x x ) | | ( x 100 ) )11、(m%10)*100 + (m/10)%10)*10+m/10012、非零13、114、字母 数字 下划线15、4.5三、 问答题1. C+语言中用数值“1”表示“真”,数值“0”表示“假”。2. 答:字符常量与字符串常量的主要区别在于:(1)定界符不同。字符常量使用单引号,而字符串常量使用双引号。(2)长度不同。字符常量的长度固定为1,而字符串常量的长度,可以是0,也可以是某个整数。(3)存储要求不同。字符常量存储的是字符的ASCII码值,而字符串常量除了要存储有效的字符外,还要存储一个结束标志0。3. (1) x+a%3*(int)(x+y)%2/4 x=2.5,a=7,y=4.7 答:上述表达式=2.5+1*(int)7.2%2/4 = 2.5+1*7%2/4 = 2.5(2) (float)(a+b)/2+(int)x%(int)y a=2,b=3,x=3.5,y=2.5答:上述表达式=(float)5/2+3%2 = 2.5+1 = 3.54答:C+语言中的数据类型有:整型(短整型,整型,长整型);字符型;逻辑型;枚举型;实型(单精度,双精度,长双精度);引用型(指针,引用);复合型(结构,联合);空类型。5. (1)1/3 int (2) 48L long(3) 5 % 3 int (4) x*3/y double (5) x+20 int (6) (int )y int(7) y != 10 bool (8) x10 bool(9) x10 ? x : sqrt (x) int或float (10) x & y bool(11) true bool (12) (char) (x+10) char(13) 1.0 /3 float (14) sizeof (float) int(15) x,y,x+y, double说明:(9)题,如果x10成立,表达式值的类型为int,如果x10不成立,表达式值的类型为float。(15)题,为逗号表达式,x+y的值即为整个表达式的值,因而值的类型为double。bool型在C+用int代替,true值为1,false值为0。6. 数值常量有:25,18,-3.67, 12.8字符常量有:4,32字符串有:“4”,“x2”, “1949.10”, “sy1.cpp”, “n”保留字:cin ,int,endl,if运算符:+标点:?非法数据:“float x ,y “ ; , 3ab变量:x1,K_name7.设程序中有说明语句: int a=20; double x=4.7; char r= a; 试求出下列每个表达式的值(假定各表达式互不影响)。 答案 答案(1)a+ 20(2) +r 98(3) 1+a/3%4 3(4) 10*sizeof( int ) 40(5) char (a+20) (6)a= =r | a= =floor(x) 0(7) a/9.0 2.(8)x=pow(3,5) 273.(9) x=(a+,r+) 97.(10) ceil(x-0.5) 5.(11) a=abs(a)%8-10 -6(12)x=fabs(-27.2)+6 33.(13)floor(x+0.5) 5.(14) ceil(x-=0.5)-0.5) 4.(15)floor(x*1000+0.5)/1000 4.(16)floor(x+0.5) 5.(17)floor(x*1000)/1000 4.(18)a+rand()%40 某一个随机整数说明:函数ceil(x),返回大于等于x的最小整数;floor(x),返回小于等于x的最大整数;rand(),返回032767之间的整数。8.设程序中有说明语句int x=20; float y=4.7; char ch = k; 试求出下列每个表达式的值(假定各表达式互不影响) 答案 答案(1) x=0 0(2) x!=y 1(3) x+ 21 0(4) a = A 0(5) ch != 0 1(6) x0 & y0 1(7) x0 & 1 1(8) y0 & 0 0(9) !(x|y)=!x&!y 0(10) x+ !=20 | y 1(11) ! ch=ch 0(12) !(x&y)=!x|!y 1说明:C+语言提供了三种逻辑运算符:!,&,|,其中!逻辑非的优先级最高,&逻辑与次之,|逻辑或最低。!逻辑非是右结合性,&逻辑与和|逻辑或是左结合性。四、编程题1:编写一个程序,从键盘输入半径和高,输出圆柱体的底面积和体积。#includeusing namespace std;void main() double r, h, area, volume; coutr; couth; area=3.14*r*r; volume=area*h; cout”底面积是: ”areaendl; cout”体积是: ”volumeendl;2. 设计一个程序,输入一个华氏温度值,要求输出其对应的摄氏温度值。温度转换公式为:c=(f-32)*5/9。解:# include using namespace std;void main() float c,f;coutf;c=(f-32)*5/9;coutc=c=65 & ch=Z 设y为int变量,判断y是否为奇数。 y%2 =1 设x、y、z都为int型变量,描述“x、y和 z中有两个为负数”。 (x0 | y0 | z0)#include using namespace std;void main() char ch;cinch;if(ch=A &ch=Z)coutUppercase Letterendl;elsecoutnot a uppercase lettert;if(t%2=1)coutt是奇数endl;elsecoutt不是奇数xyz;if(x0|y0|z 0)coutx、y和 z中有两个为负数endl;4. 编写一个程序,从键盘输入两个整型的数,在屏幕上分别输出这两个数的和、差和积。#include using namespace std;void main() int x,y;cinxy;coutx+y = (x+y)endl;coutx-y = (x-y)endl;coutx*y = (x*y)endl;5. 编写一个程序,从键盘输入两个数,分别求出这两个数的平方和和立方和,在屏幕上予以输出。#include using namespace std;void main() int x,y;cinxy;coutx*x+y*y = (x*x+y*y)endl;coutx*x*x + y*y*y = (x*x*x + y*y*y)endl;6. 编写程序,从键盘输入某一字母的ASCII码,如:97(字母a),98(字母b),65(字母A)等,在屏幕上输出给字母。 #include using namespace std;void main() int x;cout请输入一个6590之间,或者97122之间的一个整数:x;char ch = (char)x;coutchendl;习题三 参考答案第3章 结构化程序设计一、选择题1A 2C 3C 4B 5B 6D 7B 8D 9A 10. C11. C12. B 13. C 14. B 15. B 16. A 17. C 18. D 19. D 二、填空题1、122、141,64,4523、524、210 5、顺序结构 选择结构 循环结构6、23习题四 参考答案第4章 数组、指针一、选择题1.B,D 2.B 3.A 4.C 5.C 6.D 7.B 8.B 9.A 10.C11.A 12.C 13. C 14.D 15.B 16.D 17.C 18.C 19.A 20.C 21.B 22.B 23.A二、填空题1. 0 , 0 , 编译出错 。2 符号常量 3值 ,地址4. 215.1 0 0 0 0 00 1 0 0 0 00 0 1 0 0 00 0 0 1 0 00 0 0 0 1 00 0 0 0 0 16. fedcba7. x=1.5 y=2.5z=48. 20 50 50 419. 101,101 10. 30011. tuesday sunday 12. k=p 13. f 三、问答题1. 什么是数组?答:数组是若干个同类数据元素的集合。 2. 一维数组与二维数组在内存中是如何存储的?答:一维数组an定义后,系统分配n*k个连续存储单元,用于存放数组元素值,其中k=元素占用字节数。二维数组amn定义后,系统分配m*n*k个连续存储单元,用于存放数组元素值,其中k=元素占用字节数。存放方式为按行存放,即先存第一行元素,再存第二行元素,依次把各行元素存入一串连续的存储单元中。3 有如下数组定义:int a20;指出该数组的数组名、数组元素类型、数组元素个数、第一个数组元素的下标值和最后一个数组元素的下标值。答:数组名为a,数组元素类型为整型,数组元素个数为20,第一个数组元素的下标值为0和最后一个数组元素的下标值为19。4. 什么叫指针?有哪些类型的指针?答:所谓指针就是变量、数组、函数等的内存地址。指针变量按定义格式大致可分为五种:指针变量、指针数组、指向一维数组的指针变量、返回指针值函数、函数指针变量。5. 指针变量按定义格式分为哪5类?每类指针变量如何定义?答:指针变量按定义格式大致可分为五种:指针变量、指针数组、指向一维数组的指针变量、返回指针值的函数、函数指针变量。(1)指针变量的定义格式如下:存储类型 * ;如: int b, a10, * p;(2)指针数组的定义格式如下:存储类型 *;例如: char c3 4= ABC,DEF,GHI;char *pc3 =c0,c1,c2;(3)指向一维数组的指针变量可用于处理二维数组的运算问题,其定义格式为: (*); 如:int (*p)3;(4)函数可返回一个指针值,该指针指向一个已定义好的任一类型的数据。定义返回指针值的函数格式为: *() 如:float * f ( float x) 定义了一个返回实型指针的函数f (),该函数的形参为实型的x。(5)函数指针变量是用于存放函数指针的变量,也即存放函数入口地址的变量。函数指针变量的定义格式为: (*) ();6. 定义一个整型指针变量pi,用什么方法才能使pi:(1)指向整型变量i,(2)指向整型一维数组a的首地址,(3)指向整型二维数组b的首地址。答:(1)pi=&i;(2)pi=a;(或者pi=&a0;)(3)pi=&a00;(或者pi=a0;)7. 叙述二维数组a的行地址、行首地址、元素地址的概念、作用及表示方法,写出元素aij值的表示方法。答:行首地址是第i行第0列的地址,第i行首地址表示方式有:*(a+i)、ai、&ai0,用于指向数组元素的指针变量。行地址是第i行的地址,行地址表示方式有:a+i、&ai,用于指向一维数组的指针变量。注意,二维数组名a是表示第0行的行地址&a0,而不是第0行第0列的元素地址&a00。第i行第j列元素aij地址的表示方式有:ai+j 、*(a+i)+j 、&ai0+j、&aij。第i行第j列元素值的表示方式有:*(ai+j) 、 *(*(a+i)+j) 、 *(&ai0+j)、aij。8. 引用类型变量与其相关变量有何关系?引用类型变量主要用于什么场合?答:引用类型变量与其相关的变量使用相同的内存空间。在C+中引入引用类型的主要目的是为了函数参数传送提供方便。由于引用类型变量与相关变量使用相同的内存空间,所以用引用类型变量作为函数形参时,形参与实参变量使用相同的内存,在函数内对形参的运算就是对实参的运算,因此可通过参数返回函数运算结果。9. 对字符串指针变量,能否用cin、cout对其输入/输出字符串?能否对字符串指针变量进行赋值运算?字符串指针变量能否作为字符串处理函数的参数?答:不能将字符串用cin直接输入指针变量。但可将指针变量所指字符串用直接输出。可以对字符串指针变量进行赋值运算。字符串指针变量能作为字符串处理函数的参数。四、程序设计题1:编程对用户输入一个含有10个整数的一维数组,分别计算数组中所有正数和所有负数的和。程序代码如下:#include using namespace std;void main( ) int a10,I,sum0=0,sum1=0; cout”please input 10 number:”endl; for(I=0;IaI; for(I=0;I=0) sum0+=aI; else sum1+=aI; cout”正数和:”sum0”负数和:”sum1endl; 2:通过循环按行顺序为一个5*5的二维数组赋125的自然数,然后计算该数组的左下半三角元素之和并输出左下半三角元素。程序代码如下:#include using namespace std;void main( ) int a55,I,j,n=1,sum=0; for(I=0;I5;I+) for(j=0;j5;j+)aIj=n+; for(I=0;I5;I+) for(j=0;jI;j+)sum+=aIj;coutaIjt;coutn;cout”该数组左下半三角的元素之和为:”sum;3.有一个数组,内放10个整数,要求找出最小的数和它的下标,然后把它和数组中最前面的元素对换。程序代码如下:#include using namespace std;void main()int a10;int i,min,j,temp;cout请输入10个数n;for(i=0;iai;min=a0;j=0;for(i=1;iai)min=ai;j=i;if(j!=0)temp=a0;a0=aj;aj=temp;cout交换后数组n;for(i=0;i10;i+)coutait;coutendl;4.有n个数,已按由小到大顺序排列好,要求输入一个数,把它插入到原有数列中,而且仍然保持有序,同时输出新的数列。程序代码如下:#include using namespace std;void main() int i,n,j;int a11=10,20,30,40,50,60,70,80,90,100;for(i=0;i10;i+) coutait;coutendl;coutn;j=9;while(j=0&naj)aj+1=aj;j-; aj+1=n; cout插入后的数组n; for(i=0;i11;i+) coutait;coutendl;5.水仙花数。如果一个正整数等于组成该数的各个数字的立方和,则称该数为水仙花数,亦称阿姆斯特朗数。如407=43+03+73,407是一个水仙花数。试编写求100999之间的所有水仙花数的程序。程序代码如下:#include#include using namespace std;void main()int i,j,k,t; for(i=1;i=9;i+)for(j=0;j=9;j+) for(k=0;k=9;k+) t=i*100+j*10+k; if(i*i*i+j*j*j+k*k*k=t) couttend;6. 输入一个nn矩阵各元素的值,求出两条对角线元素值之和。程序代码如下:#include using namespace std;#define N 5void main()int aNN; int i,j,sum=0; cout请输入N*N数n; for(i=0;iN;i+) for(j=0;jaij;cout二维数组为:n; for(i=0;iN;i+) for(j=0;jN;j+) coutaijt; coutendl; for(i=0;iN;i+)sum=sum+aii+aiN-i-1 ; if(i=N-i-1) sum=sum-aiN-i-1;cout对角线元素和为sumendl;7.编写一程序,将两个字符串连接起来,不用strcat函数。程序代码如下:#include using namespace std;void main()char st180,st220; int i,k; coutst1st2; i=0; while(st1i!=0) i+; k=0; while(st2k!=0) st1i=st2k;k+;i+; st1i=0; coutst1s2,输出一个正数;s1=s2,输出0;s1s2,输出一个负数。不用strcmp函数。两个字符串用gets函数读入。程序代码如下:#include #includeusing namespace std;void main()int i,resu;char s1100,s2200;coutinput string1:endl;gets(s1);coutinput string2:endl;gets(s2);i=0;while(s1i=s2i)&(s1i!=0) i+;if(s1i=0&s2i=0) resu=0;elseresu=s1i-s2i;coutresult:resuendl;9编写一程序,将字符数组s2中的全部字符拷贝到字符数组s1中。不用strcpy函数。拷贝时,0也要拷贝过去。0后面的字符不拷贝。程序代码如下:#include#include using namespace std;void main()char s180,s280;int i;coutInput s2:s2;for(i=0;i=strlen(s2);i+) s1i=s2i;couts1:s1endl;10编写程序,在堆内存中申请一个float型数组,把10个float型数据0.1、0.2、0.3、1.0赋予该数组,然后使用float型指针输出该数组的各元素值并求出其累加和。程序代码如下:#includeusing namespace std;void main() float *p= new float10; /在堆内申请一个float型数组 int i; float sum=0.0; for(i=0;i10;i+) *(p+i)=(i+1)*0.1; /给数组赋值0.1,0.2,1.0 cout*(p+i)t; /输出数组值sum+=*(p+i); /求和 coutn sum= sumendl; 11编写一个函数f,将传入此函数的直角坐标值转换为极坐标值,并返回主调函数中。求极坐标的公式是:c= q=arctan(y/x)程序代码如下:(1) 两值均以指针形参带回。void fun(double x,double y,double *c,double *q) coutx,yendl;*c=sqrt(x*x+y*y);cout*cendl; *q=atan(y/x);(2) 两值均以引用形参返回。void fun(double x,double y,double &a,double &b)a=sqrt(x*x+y*y); b=atan(y/x);12编写一个程序,实现在命令行中以参数的形式接收两个整数,输出这两个整数的和。程序代码如下:#include#includeusing namespace std;void main(int argc,char *argv) int a,b; a=atoi(argv1); b=atoi(argv2); couta+b=a+bendl;习题五 参考答案第5章 函数一、选择题1:A 2:C 3:B 4:B 5. B 6. B 7. D 8. C 9. C 10. C11. B 12. B 13. B 14. A二、问答题1. 什么是函数?函数是如何分类的?答:函数是实现特定功能的相对独立的程序段 .从不同的角度可以将函数分成 : 库函数和用户自定义函数、主调函数和被调函数、无参函数和有参函数。 2. 自定义函数如何定义?有哪三种调用方式?答:自定义函数定义一般格式为: 类型 ( ) 函数调用的方式通常有三种,即:函数调用语句、函数表达式、函数参数。 3. 在函数的调用过程中,实参传送给形参有哪两种方式?这两种传送方式有什么区别?答:在函数的调用过程中,实参传送给形参有“值传递”和“传地址”两种方式。所谓的“值传递“,调用函数时,系统给形参分配存储单元,并将实参的值传递给对应的形参;在函数执行时,都是形参参与运算;函数返回时,形参值的改变不影响实参,实参保持原来的值。所谓的“传地址”,当函数的形参为数组时,其实参只能是相同类型的数组。调用函数时,系统将实参数组的起始地址传递给形参数组,两个数组占用相同的内存空间单元。这时,在执行函数时,形参数组中各元素值的变化会使实参数组元素的值同时发生变化。 4. 什么情况下必须使用函数的原型说明?在函数的原型说明的形参表中,形参名是否是必须的?为什么?答:在 C+ 程序中,当函数调用在前、函数定义在后时,则应在主调函数中,在调用前增加对被调函数的原型说明。在函数原型说明中,可只写形参的数据类型,而省略形参名。这是因为:函数原型说明的目的是告诉编译程序,该函数返回值的类型、参数的个数
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!