c++程序设计课后练习答案.docx
- 文档编号:11096013
- 上传时间:2023-02-25
- 格式:DOCX
- 页数:72
- 大小:32.60KB
c++程序设计课后练习答案.docx
《c++程序设计课后练习答案.docx》由会员分享,可在线阅读,更多相关《c++程序设计课后练习答案.docx(72页珍藏版)》请在冰豆网上搜索。
c++程序设计课后练习答案
第一章
一、选择题
1.B;(typedef,typeid,typename,都为保留字);
2.C;(标识符,应该以字母或,下划线开头);
3.C;(标识符中有的特殊符号,只能有下划线);
二、填空题
1.cin,cout
2.new,delete
3.inta(55);
三、改错题
1.没有定义变量num;
2.constint*p=&x;是声明指向常量的指针,*p不能当作“左值”,*p=65错误。
3.p为常量指针,不能吧p作为“左值”,p=&y,错误。
四、编程题
1.分别用字符和ASCII码形式输出整数值65和66.
#include
usingnamespacestd;
voidmain()
{
chara='A',b='B';
intascii_1=53,ascii_2=54;//ASCII码中的,5和6
cout<<"字符输出:
"<<(int)a<<","<<(int)b< cout<<"ASCII码输出: "<<(char)ascii_2<<(char)ascii_1<<","; cout<<(char)ascii_1<<(char)ascii_1< } 2.编写一个int型变量分配100个整形空间的程序。 #include usingnamespacestd; voidmain() { int*p; p=newint[100]; for(inti=0;i<100;i++) { *(p+i)=i; } for(i=0;i<100;i++) { cout<<*(p+i)<<","; } deletep; } 3.编写完整的程序,它读入15个float值,用指针把它们存放在一个存储快里,然后输出这些值和以及最小值。 #include #include usingnamespacestd; voidmain() { float*p; p=newfloat[15]; cout<<"输入15个float类型的值: "< for(inti=0;i<15;i++) { cin>>*(p+i); } for(i=0;i<15;i++) { cout<<*(p+i)<<","; } sort(p,p+15); cout<<"\n最小的是: "<<*(p)< deletep; } 4.声明如下数组: inta[]={1,2,3,4,5,6,7,8}; 先查找4的位置,讲数组a复制给数组b,然后将数组a的内容反转,再查找4的位置,最后分别输出数组a和b的内容。 #include #include #include usingnamespacestd; voidmain() { inta[]={1,2,3,4,5,6,7,8},b[8]; cout<<"数组a中‘4’的位置是: "< copy(a,a+8,b);//将数组a复制给数组b reverse_copy(b,b+8,a);//把数组b,逆向复制给a,完成a的逆转 cout<<"数组a反转后,‘4’的位置是: "< cout<<"数字a的内容: "< for(inti=0;i<8;i++) cout< cout<<"\n数组b中的内容: "< for(i=0;i<8;i++) cout< } 第二章 一、单项选择 1.D;2.D; 三、编程题 1.使用多种方法编写将两个字符串连接在一起的程序。 #include #include usingnamespacestd; voidmain() { //使用string类定义字符串,完成字符串连接 stringstr1("C++"),str2("程序设计"); stringstr3; str3=str1+str2;//连接方式1 cout< //使用char数组定义字符串,完成连接 charc1[]={"c++"},c2[]={"program"}; charc3[20]; inti=0,k=0; for(i=0;i<20;i++)//初始化c3 c3[i]='\0'; i=0; while(c1[i]! ='\0') { c3[k]=c1[i]; i++; k++; } i=0; while(c2[i]! ='\0') { c3[k]=c2[i]; i++; k++; } cout< } 2.已知一个string的对象str的内容为“Wearehere! ”,使用多种方法输出“h”。 #include #include #include #include usingnamespacestd; voidmain() { stringstr1("Wearehere! "); cout< stringstr2=str1.substr(7,1);//通过得到子字符串 cout< char*p=find(str1.begin(),str1.end(),'h');//通过find函数 if(p) cout<<*p< } 第三章 一、填空题 1.函数原型声明; 2.inline 3.对象、对象指针、引用 4.函数func返回引用 5.int*fun(char,int&); 二、单项选择题 1.A;2.C;3.C; 三、改错题 1.y=x*x-T;错误,T是类型,不是变量,不能参加运算; 2.y没有类型,并且x的类型和template中的不一样。 #include template Typemax(Typex,Typey) { return(x>y)? (x): (y); } 3.函数change的参数定义成了常量,只能使用参数,而无权修改他。 voidchange(string&s) { s=s+"pig! "; } 四、编程题 1.编写一个求方程ax2+bx+c=0的根的程序,用3个函数分别求当b2-4ac大于零、等于零、和小于零时的方程的根。 要求从主函数输入a,b,c的值并输出结果。 #include #include voidequation_1(inta,intb,intc) { doublex1,x2,temp; temp=b*b-4*a*c; x1=(-b+sqrt(temp))/(2*a*1.0); x2=(-b-sqrt(temp))/(2*a*1.0); cout<<"两个不相等的实根"< cout<<"x1="< } voidequation_2(inta,intb,intc) { doublex1,x2,temp; temp=b*b-4*a*c; x1=(-b+sqrt(temp))/(2*a*1.0); x2=x1; cout<<"两个相等的实根"< cout<<"x1="< } voidequation_3(inta,intb,intc) { doubletemp,real1,real2,image1,image2; temp=-(b*b-4*a*c); real1=-b/(2*a*1.0); real2=real1; image1=sqrt(temp); image2=-image1;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 程序设计 课后 练习 答案