c程序设计教程钱能课后答案.doc
2023年大学_C++程序设计教程第一版(钱能著)课后习题答案下载
2023年C++程序设计教程第一版(钱能著)课后习题答案下载C++程序设计教程第一版(钱能著)课后答案下载作者:钱能著出版社:清华大学出版社出版时间: -9-1字数: 891000页数: 551纸张:胶版纸I S B N : 9787302114642定价:39.50C++程序设计教程第一版(钱能著):内容简介点击此处下载C++程序设计教程第一版(钱能著)课后答案C++程序设计教程第一版(钱能著):基本信息本书在介绍C++面向过程程序设计的语法基础上,重点讲解C++面向对象程序设计的语法内容,同时也讲解了C++系列泛型编程的语法支持,主要内容包括类与对象、继承、多态、容器类、模板、异常处理厦C++标准库。
本书内容比较全面,例题丰富,讲述简明清晰。
书中概念和语法都有丰富的示例讲解,易读易懂,实用性强。
书中全部源程序代码均在Dev-C++下调试通过。
为方便读者学习,我们编写了《C++程序设训教程实验指导及刊题解答》书,可与本书配套使用。
本书可作为高等院校、计算机水平考试培训、并类成人教育的C++程序设计课程教材或参考书,也可作为读者自学使用的参考书。
C++程序设计教程第一版(钱能著):推荐理由本书荣获教育部全国高校优秀教材奖!以C++标准为蓝本,从过程化编程的基本描述,到对象化编程的方法展开,乃至高级编程的实质揭示,形成一条自然流畅的主线,通俗易懂,形象风趣。
本书在内容结构上自成体系,并以独特的描述手法,辐射到计算机专业其他诸课程,体系严谨,结构独特。
本书特色:1、第二版全方位改版,代码全部以标准C++重写,风格独特,极具模仿价值;文字诙谐生动,通俗易懂。
2、自成体系,结构独特,整体关联,辐射计算机各门课程。
3、引领读者由欣赏书中的初级精彩到享受国外经典名著的`内在精彩。
4、观点鲜明,客观褒贬C++,对术语的见解独到。
5、C++内部特性和抽象编程并重,强化编程实践,以实际编程能力衡量计算机水平。
第一部分为程序设计基础,分四章,包括概述,基本编程语句,数据类型和计算表达。
c语言程序设计教程第二版课后习题答案
c语言程序设计教程第二版课后习题答案【篇一:c语言程序设计教程_李含光_郑关胜_清华大学出版社习题答案习题答案[完美打印版]】1.单项选择题(1)a (2)c(3)d (4)c (5)b 2.填空题(1)函数(2)主函数(main)(3)printf() , scanf()第2章习题参考答案1.单项选择题1-5 cbccc 6-10 cdcdc 11-13 dbb 2.填空题(1)1(2)26 (3)6 , 4 , 2 (4)10 , 6(5)3.000000 (6)双精度(double)(7)9 (8)字母,数字,下划线(9)13.700000 (10)11(11)((m/10)%10)*100+(m/100)*10+m%10(12)0 (13)10 ,9 ,11(15)(x0y0)||(x0z0)||(y0||z0)(16)double (17)x==0(18)sqrt(fabs(a-b))/(3*(a+b))(19)sqrt((x*x+y*y)/(a+b))第3章习题参考答案1.单项选择题1-5 cccdd 6-10 bcdbc11-15 bcbbb16 a 2.填空题(1)用;表示结束(2){ }(3)y=x0?1:x==0?0:-1(4)y%4==0y%100!=0||y%400==0(5)上面未配对(6)default 标号(7)while , do while , for(8)do while(9)本次(10)本层 3.阅读程序,指出结果(1)yes(2)*(3)abother(4)28 70(5)2,0(6)8(7)36 (8)1(9)3,1,-1,3,1,-1(10)a=12 ,y=12(11)i=6,k=4 (12)1,-2 4.程序填空(1)x:y , u:z(2)m=n , m!=0,m=m/10(3)teps , t*n/(2*n+1) , printf(“%lf\n”,2*s) (4)m%5==0 ,printf(“%d\n”,k) (5)cx=getchar() , cx!=front , cx(6)double s=0, 1.0/k , %lf (7)s=0 , sgmin, 5.编程题(1). #include stdio.h int main() {double x,y; scanf(%lf,x); if(x1) y=x;else if(x=1.0x10) y=2*x-11; elsey=3*x-11;printf(%lf\n,y); return 0; } (2).#include stdio.h int main() {double x,y,z,min;scanf(%lf%lf%lf,x,y,z); if(xy) min=y; else min=x; if(minz)min=z;printf(min=%lf\n,min); return 0; } (3).#include stdio.h int main() {int y,m,d,flag,s=0,w,i;scanf(%d%d%d,y,m,d);flag=(y%4==0y%100!=0||y%400==0);w=((y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400)%7;for(i=1;i=m;i++) {switch(i) {case 1:s=d;break; case 2:s=31+d;break; case 3:s=59+d;break; case 4:s=90+d;break; case 5:s=120+d;break; case6:s=151+d;break; case 7:s=181+d;break; case8:s=212+d;break; case 9:s=243+d;break; case10:s=273+d;break; case 11:s=304+d;break; case12:s=334+d;break;} }s=(w+s)%7; if(s==0)printf(星期日\n); elseprintf(星期%d\n,s); return 0; }(4).#include stdio.h int main() {float p,r;scanf(%f,p); if(p=10) r=p*0.1;else if(p10p=20) r=10*0.1+(p-10)*0.075; else if(p20p=40)r=10*0.1+10*0.075+(p-20)*0.05; else if(p40p=60)r=10*0.1+10*0.075+20*0.05+(p-40)*0.03;else if(p60p=100)r=10*0.1+10*0.075+20*0.05+20*0.03+(p-60)*0.015; else if(p100)r=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(p-100)*0.01; printf(%f\n,r); return 0; } (5).#include stdio.h int main() {char c;while((c=getchar())!=\n) {if(c=ac=z) c=c-32; putchar(c);}return 0; } (6).#includestdio.h int main() {int m,k=2;printf(输入一个正整数:\n); scanf(%d,m); while(km) if(m%k==0) {printf(%4d,k); m=m/k; } else k++;printf(%4d\n,m); return 0; } (7).#includestdio.h int main() {int a,n,s=0,p=0,i;scanf(%d %d,n,a); for(i=1;i=n;i++) {p=p*10+a; s=s+p; }printf(%d\n,s); return 0; } (8).#includestdio.h int main(){int i,j,k;for(i=1;i=9;i++) for(j=0;j=9;j++) for(k=0;k=9;k++)printf(%5d,100*i+10*j+k); return 0; }(9).#includestdio.h #includemath.h int main() {float a=-10,b=10,x,f1,f2,f; f1=(((2*a-4)*a+3)*a)-6; f2=(((2*b-4)*b+3)*b)-6; do {x=(a+b)/2;f=(((2*x-4)*x+3)*x)-6; if(f*f10) { b=x; f2=f; } else { a=x;f1=f; }}while(fabs(f)=1e-6); printf(%6.2f\n,x); return 0; }(10).#includestdio.h#includemath.h int main() {int n=2;double eps,t,s=0,x;scanf(%lf %lf,x,eps); t=x; s=t;while(fabs(t)=eps) {t=-t*(2*n-3)*x*x/(2*n-2); s=s+t/(2*n); n++; }printf(%d,%lf\n,n,s); return 0; }(11).#includestdio.h int main() {unsigned long s,t=0,p=1; scanf(%u,s); while(s!=0) {if((s%10)%2!=0) {t=t+(s%10)*p; p=p*10; }s=s/10; }printf(%u\n,t); return 0; }第4章习题参考答案1.单项选择题1-5 dddbd 6-10 badcd 11-14 bdab 2.填空题(1)2(2)嵌套,递归(3)全局变量,局部变量,静态变量,动态变量(4)auto , static , register , extern (5)外部变量(6)编译,运行 3.阅读程序,指出结果(1)15(2)5(3)5,4,3 (4)i=5 i=2 i=2 i=4 i=2(5)求水仙花数(6)-5*5*5(7)30 (8)0 10 1 11 2 124.程序填空(1)float fun(float , float) , x+y,x-y, z+y,z-y (2)x , x*x+1 (3)s=0 , a=a+b 5.编程题(1).while(s!=0) #includestdio.h { unsigned int fun(unsigned int);p=p+s%10; int main() s=s/10; { } unsigned int s; return p; scanf(%u,s); } printf(%u\n,fun(s)); (2). return 0;#includestdio.h } #includestdlib.h unsigned int fun(unsignedint s) #includemath.h { void f1(float,float,float,float); unsigned int p=0; void f2(float,float,float,float);【篇二:《c语言程序设计》课后习题答案(第四版)谭浩强】t>1.1什么是计算机程序11.2什么是计算机语言11.3c语言的发展及其特点31.4最简单的c语言程序51.4.1最简单的c语言程序举例61.4.2c语言程序的结构101.5运行c程序的步骤与方法121.6程序设计的任务141-5 #include stdio.hint main ( ){ printf (**************************\n\n);printf( very good!\n\n);printf (**************************\n);return 0;}1-6#include stdio.hint main(){int a,b,c,max;printf(please input a,b,c:\n);scanf(%d,%d,%d,a,b,c);max=a;if (maxb)max=b;if (maxc)max=c;printf(the largest number is %d\n,max);return 0;}第2章算法——程序的灵魂162.1什么是算法162.2简单的算法举例172.3算法的特性212.4怎样表示一个算法222.4.1用自然语言表示算法222.4.2用流程图表示算法222.4.3三种基本结构和改进的流程图262.4.4用n?s流程图表示算法282.4.5用伪代码表示算法312.4.6用计算机语言表示算法322.5结构化程序设计方法34习题36第章最简单的c程序设计——顺序程序设计37 3.1顺序程序设计举例373.2数据的表现形式及其运算393.2.1常量和变量393.2.2数据类型423.2.3整型数据443.2.4字符型数据473.2.5浮点型数据493.2.6怎样确定常量的类型513.2.7运算符和表达式523.3c语句573.3.1c语句的作用和分类573.3.2最基本的语句——赋值语句593.4数据的输入输出653.4.1输入输出举例653.4.2有关数据输入输出的概念673.4.3用printf函数输出数据683.4.4用scanf函数输入数据753.4.5字符数据的输入输出78习题823-1 #include stdio.h#include math.hint main(){float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf(p=%f\n,p);return 0;}3-2-1#include stdio.h#include math.hint main(){float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*((1+r5)*5);// 一次存5年期p2=p*(1+2*r2)*(1+3*r3); // 先存2年期,到期后将本息再存3年期 p3=p*(1+3*r3)*(1+2*r2); // 先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5); // 存1年期,到期后将本息存再存1年期,连续存5次 p5=p*pow(1+r0/4,4*5); // 存活期存款。
c语言程序设计教程第二版课后答案
c语言程序设计教程第二版课后答案【篇一:c语言程序设计(第2版)-- 课后题答案】p> 参考答案第1章进入c语言程序世界二、1.i love china!printf(we are students.\n)2.6项目实训题参考答案1.编写一个c程序,输出以下信息:* * * * * * * * * * * * * * * * * * * *i am a student!* * * * * * * * * * * * * * * * * * * *main(){ printf(********************\n);printf( i am a student!\n);printf(********************\n);}2.已知立方体的长、宽、高分别是10cm、20cm、15cm,编写程序,求立方体体积。
解:main(){int a,b,c,v;a=10;b=20;c=15;v=a*b*c;printf(v=%d,v);}本程序运行结果为:v=3000第2章编制c程序的基础知识一选择题c b a b a c c二操作题,2,-8,23.000000,2.500000,-8.0000002. abc defghwhy is21+35equal 523.34214. aaa项目实训题1.定义一个符号常量m为5和一个变量n值为2,把它们的乘积输出。
#define m 5main(){ int n,c;n=2; c=m*n;printf(%d\n,c);}2.编程求下面算术表达式的值。
(1)x+a%3*(int)(x+y)%2/4,设x=2.5,a=7,y=4.7;(2)(float)(a+b)/2+(int)x%(int)y,设a=2,b=3,x=3.5,y=2.5。
(1)main(){ int a=7;float x=2.5,y=4.7;printf(%f\n,x+a%3*(int)(x+y)%2/4);}(2)main(){ int a=2,b=3;float x=3.5,y=2.5;printf(%f\n,(float)(a+b)/2+(int)x%(int)y);}第三章顺序结构程序设计一选择题a c d c c二操作题1. x=3,a=2,b=32. z=12.7000002 13 3 2 bb cc abc n3. 1 2 1a2 1 2三.编程题编程题解:#include stdio.hmain(){float sj,gz,yfgz;printf(time,salary:);scanf(%f,%f,sj,gz);yfgz=sj*gz*0.9;printf(total salary:%f\n,yfgz);}本程序运行结果为:time,salary:4,3crtotal salary:10.8000002.编写一个程序求出任意一个输入字符的ascii码解:#include stdio.hmain(){char c;printf(input a string:);scanf(%c,c);printf(%c ascii is %d\n,c,c);}本程序运行结果为:input a string:acra ascii is 973、编写一个程序用于水果店售货员算帐:已知苹果每斤2.50元,鸭梨每斤1.80元,香蕉每斤2元,橘子每斤1.6元,要求输入各类水果的重量,打印出应付3解:main(){float p,y,x,j,ys,g,fk;printf(apple,pear,banana,orange(weight)=);scanf(%f,%f,%f,%f,p,y,x,j);ys=2.5*p+1.8*y+2*x+1.6*j;printf(fu kuan=);scanf(%f,g);fk=g-ys;printf(result:\n);printf(fukuan=%6.2fyuan\nshoukuan=%6.2fyuan\nzhaohui=%6. 2fyuan\n,g,ys,fk);}本程序运行结果为:apple,pear,banana,orange(weight)=1,2,3,4fu kuan=100result:fukuan=100.00yuanshoukuan= 18.50yuanzhaohui= 81.50yuan项目实训1.假设银行定期存款的年利率rate为2.25%,并已知存款期为n 年,存款本金为capital元,试编程计算n年后可得到本利之和deposit。
C程序设计教程修订版设计思想与实现习题解答钱能
二2.1#include <iostream.h>void main(){ //本题原考虑在 16 位机器上实验目前多为 32 位机器故已过时。
int a = 42486; cout <<oct <<a <<endl <<hex <<a <<endl; unsigned b = 42486;cout << dec <<(signed)b <<endl;}2.2#include<iostream.h>#include<iomanip.h>const double pi =3.1415926; voidmain(){double radius1, radius2;cout <<"please input twonumbers:\n";cin >>radius1 >>radius2;cout <<setw(10) <<pi <<setw(10)<<radius1 <<setw(10)<<(pi*radius1*radius1) <<endl<<setw(10) <<pi <<setw(10)<<radius2<<setw(10) <<(pi*radius2*radius2) <<endl; }2.3#include<iostream.h>#include<iomanip.h> constdouble e =2.718281828; voidmain(){cout <<setprecision(10) <<e <<endl<<setiosflags(ios::fixed) <<setprecision(8) <<e <<endl<<setiosflags(ios::scientific) <<e <<endl;}2.4#include <iostream.h>void main(){cout <<"\"How many students here?\"\n"<<"\"500\"\n";}2.5#include <iostream.h>void main(){cout <<"size of char " <<sizeof(char) <<"byte\n" <<"size of unsigned char "<<sizeof(unsigned char) <<" byte\n"<<"size of signed char " <<sizeof(signedchar) <<" byte\n" <<"size of int "<<sizeof(int) <<" byte\n"<<"size of unsigned " <<sizeof(unsigned)<<" byte\n" <<"size of signed "<<sizeof(signed) <<" byte\n" <<"size of short " <<sizeof(short) <<" byte\n"<<"size of unsigned short "<<sizeof(unsigned short) <<" byte\n"<<"size of long " <<sizeof(long) <<" byte\n"<<"size of signed long " <<sizeof(signedlong) <<" byte\n"<<"size of unsigned long "<<sizeof(unsigned long) <<" byte\n" <<"sizeof float " <<sizeof(float) <<" byte\n"<<"size of double " <<sizeof(double) <<"byte\n"<<"size of long double " <<sizeof(longdouble) <<" byte\n";}2.6 1) please input 3sides of one triangle:6,6,8a= 6.00,b= 6.00,c= 8.00area of triangle is 17.888542)该程序计算三角形的面积前后分为三部分输入处理输出。
《C语言程序设计教程》第三版课后习题参考答案
《C语言程序设计教程》第三版课后习题参考答案C语言程序设计教程第三版课后习题参考答案第一章:C语言概述1.1 C语言的特点答案:C语言是一种通用的、面向过程的程序设计语言,具有高效、简洁、灵活等特点。
它提供了丰富的程序设计元素和功能,适用于各种不同的应用领域。
1.2 C语言程序的基本结构答案:C语言程序由预处理指令、函数声明、函数定义、变量声明和语句组成。
其中,预处理指令用来引入头文件或定义宏,函数声明用来声明函数的名称和参数,函数定义用来实现函数的功能,变量声明用来声明变量的类型和名称,语句用来表达具体的计算过程。
1.3 C语言的数据类型答案:C语言提供了多种数据类型,包括基本类型(整型、浮点型、字符型等)和派生类型(数组、指针、结构体等)。
每种数据类型在内存中占据一定的存储空间,并具有特定的取值范围和操作规则。
1.4 C语言的运算符和表达式答案:C语言支持各种运算符和表达式,例如算术运算符(+、-、*、/等)、关系运算符(>、<、==等)、逻辑运算符(&&、||、!等)等。
通过运算符和表达式可以进行各种数值计算和逻辑判断。
第二章:基本数据类型与运算2.1 整型数据类型答案:C语言提供了不同长度的整型数据类型,包括有符号整型(int、long等)和无符号整型(unsigned int、unsigned long等)。
整型数据类型可以表示整数值,并具有不同的取值范围。
2.2 浮点型数据类型答案:C语言提供了浮点型数据类型(float、double等),用来表示带小数部分的实数值。
浮点型数据可以表示较大或较小的数值,并具有一定的精度。
2.3 字符型数据类型答案:C语言提供了字符型数据类型(char),用来表示单个字符。
字符型数据可以用于表示各种字符(包括字母、数字、符号等)。
2.4 布尔型数据类型答案:C语言不直接支持布尔型数据类型,但可以使用整型数据类型来表示布尔值(0表示假、非零表示真)。
c课后题答案 钱能第二版
学习资料收集于网络,仅供参考1-1#include<iostream>using namespace std;void main(){cout<<I'm a student<<endl;}1-2#include<iostream>using namespace std;int i,j;void main(){for(i=1;i<=4;i++){for(j=1;j<=4-i;j++){cout<< ;}for(j=1;j<=2*i-1;j++){cout<<*;}cout<<endl<<endl; }for(i=1;i<=3;i++){for(j=1;j<=i;j++){cout<< ;学习资料.学习资料收集于网络,仅供参考for(j=1;j<=(7-2*i);j++){cout<<*;}cout<<endl<<endl; }}2-13#include<iostream>using namespace std;int i,j;void main(){for(i=1;i<=9;i++)cout<<i<<\ ;for(i=1;i<=9;i++)cout<<-<<\ ;cout<<endl<<endl;for(i=1;i<=9;i++) // 下三角形式{for(j=1;j<=i;j++)//{cout<<i*j<<\ ;//}cout<<endl;}cout<<endl<<endl;学习资料.学习资料收集于网络,仅供参考for(i=1;i<=9;i++) //shang三角形式cout<<\ <<i;for(i=0;i<9;i++)cout<<\ <<-;cout<<endl<<endl;for (i=1;i<=9;i++){for(j=1;j<=i;j++)cout<<\ ;for(j=i;j<=9;j++)//{cout<<i*j<<\ ;//}cout<<endl; }}3-3#include<iostream>#include<vector>#include<cmath>using namespace std;void main(){int a[11]={6,3,7,1,4,8,2,9,11,5};vector<int> ve(a,a+10);int he=0;int sum=0;学习资料.学习资料收集于网络,仅供参考float average;float s;for(int i=0;i<ve.size();i++){sum+=ve[i];}average=sum/ve.size();for( i=0;i<ve.size();i++){he+=(ve[i]-average)*(ve[i]-average);}s=sqrt(he/ve.size());cout<<s=<<s<<endl;}4-6#include<iostream>#include<fstream>#include<sstream>using namespace std;void main(){int a=0;ifstream in(D:\\aaa.txt);for(string s;getline(in,s);)for(istringstream sin(s);sin>>a;){if((a%3==0)&&(a%5==0)&&(a%7==0))挠畯?愼?能同时被3 5 7整除<<endl;else if((a%3==0)&&(a%5)&&(a%7)) 学习资料.学习资料收集于网络,仅供参考挠畯?愼?能被3整除不能被5整除也不能被7整除<<endl; else if((a%3)&&(a%5==0)&&(a%7))挠畯?愼?能被5整除不能被3整除也不能被7整除<<endl;else if((a%3)&&(a%5)&&(a%7==0))?挠畯?愼?能被7整除不能被3整除也不能被5整除<<endl;else if((a%3)&&(a%5)&&(a%7))??潣瑵??尼不能被3 5 7 任意一个整除<<endl;}}4-8#include<iostream>using namespace std;void transform(int );int n;void main() //十进制数转换为二进制数{潣瑵?输入要转换的十进制整数:<<endl;int num;cin>>num;transform(num);}void transform(int n){if (n<2)cout<<n;else if(n>=2){学习资料.学习资料收集于网络,仅供参考transform(n/2);cout<<n%2;}5-5#include<iostream>#include<fstream>#include<sstream>#include<vector>#include<algorithm>using namespace std;typedef vector<vector<int> > vii;//---------------------------------------------------------------vector<int> read(){vector<int>a;ifstream in(D:\\aaa.txt);for(string s;getline(in,s);){int c;for(istringstream sin(s);sin>>c;)a.push_back (c);}if(a.empty())cout<<NO ELement!<<endl;else<<endl; 从磁盘中读取数据输出结果是:潣瑵?for(int i=0;i<a.size();i++) 学习资料.学习资料收集于网络,仅供参考cout<<a[i]<< ; //输出读取的数据cout<<endl<<endl;return a;}//---------------------------------------------------------------int bit_twice_sum(int a){int sum=0;for(int x=a;x;x/=10)sum+=(x_x0010_)*(x_x0010_);return sum;}//-----------------------------------------------------------------------bool compare(int sum1,int sum2){return bit_twice_sum(sum1)<bit_twice_sum(sum2);}//----------------------------------------------------------------------- void insert_cout(vector<int>ve){<<endl; 请输入你想插入的数:?潣瑵? int num;cin>>num;ve.push_back(num);sort(ve.begin(),ve.end(),compare);挠畯?攼摮?尼插入数之后,再按各位数字平方之和的大小输出:<<endl;for(int i=0;i<ve.size();i++)cout<<ve[i]<< ;学习资料.学习资料收集于网络,仅供参考cout<<endl;}//-----------------------------------------------------------------------void main(){vector<int>v0=read();sort(v0.begin(),v0.end(),compare);挠畯?攼摮?尼按各位数字平方之和的大小输出:<<endl;for(int i=0;i<v0.size();i++)cout<<v0[i]<< ;cout<<endl;insert_cout(v0);//----------------------------------------------------------------------- 8-1#include<iostream>#include<cmath>using namespace std;//------------------------------------class Point{protected:double x,y; //x y 轴分量public:void set(double ix,double iy)// 设置坐标{x=ix;y=iy;}学习资料.学习资料收集于网络,仅供参考//------------------------------------------double xoffset()//取y轴坐标分量{return x;}//------------------------------------------轴坐标分量double yoffset()//取x{return y;}//----------------------------------------@ 取点的极坐标double angle()//{return (180/3.14159)*atan2(y,x);}//--------------------------------------double radius()//取点的极坐标半径{return sqrt(x*x+y*y);void Point:: add(){x+=5;y+=6;潣瑵?硜尽??攼摮?尼??礼?湥汤※}};void main(){Point p;double x,y;学习资料.学习资料收集于网络,仅供参考cout<<Enter x and y:<<endl;cin>>x>>y;p.set(x,y);潣瑵?慜杮敬尽??湡汧??攼摮?尼慲楤獵尽??慲楤獵?湥汤?硜offset=<<p.xoffset()<<endl<<y offset=<<p.yoffset()<<endl; p.add();}8-3#include<iostream>#include<iomanip>using namespace std;class Date{private:int year,month,day;public:void set(int d,int m,int y);void add();void print();};void Date::set(int d,int m,int y){year=y;month=m;day=d;void Date::add(){day=day+1;<<day<<endl; ??潣瑵?加一天之后日期是:学习资料.学习资料收集于网络,仅供参考}void Date::print(){cout<<setfill('0');cout<<setw(2)<<day<<'/'<<setw(2)<<month<<'/'<<setw(4)<<year<<en dl;//cout<<setfill(' ');}void main(){Date d1,d2;d1.set(10,4,2012);d1.print();d2.set(10,4,2012);d2.add();d2.print();}8-4/**多文件结构*///Time.cpp#includeTime.hvoid Time::set(int h,int m,int s){hour=h;minite=m;学习资料.学习资料收集于网络,仅供参考second=s;}void Time::print(){setfill('0');cout<<setw(2)<<hour<<':'<<setw(2)<<minite<<':'<<setw(2)<<second<<endl; }#includeTime.h//主函数void main(){Time t;t.set(14,47,39);t.print();}9-1#include<iostream>using namespace std;class MyClass{int number;public:MyClass();MyClass(int);~MyClass();void Display();};//--------------------------------------------------MyClass::MyClass(){cout<<Constructing normally.\n;}学习资料.学习资料收集于网络,仅供参考//------------------------------------------------------MyClass::MyClass(int m):number(m){cout<<Constructing with a number:<<number<<endl;}//-----------------------------------------------------void MyClass::Display(){cout<<Display s number:<<number<<endl;}//-----------------------------------------------------------------MyClass ::~MyClass(){cout<<Destructing.\n;}//---------------------------------------------------void main(){MyClass obj1;MyClass obj2(20);obj1.Display();obj2.Display();}//--------------------------------------------------------------- 9-3#include<iostream>using namespace std;//----------------------------class CAT{int *itsAge;public:CAT():itsAge(new int(5)){}~CAT(){delete itsAge;}学习资料.学习资料收集于网络,仅供参考int GetAge() const {return *itsAge;}void SetAge(int age){*itsAge=age;}};//-------------------------------------------void main(){CAT friskey;cout<<\n; cout<<Setting friskey to 6……friskey.SetAge(6);cout<<Creating boots from friskey\n;CAT boot(friskey);cout<<cout<<oot's age: <<boot.GetAge()<<endl; \n; cout<<setting friskey to 7……friskey.SetAge(7);cout<<cout<<oot's age: <<boot.GetAge()<<endl; }学习资料.。
c++程序的设计钱能第二版习题解答(多解法)
//============================= //课本第23页1题。
//输出“I am a student."//============================= #include<iostream>using namespace std;//----------------------------- int main(){cout<<"I am a student.\n";return 0;}//============================//============================= //课本第23页2题。
//多行输出*//============================= #include<iostream>using namespace std;//----------------------------- int main(){cout<<" * \n";cout<<" *** \n";cout<<" ***** \n";cout<<"*******"<<endl;cout<<" ***** \n";cout<<" *** \n";cout<<" * \n";return 0;}//============================//============================= //练习二第01题。
//计算级数。
//============================= #include <iostream>#include <cmath>using namespace std;//----------------------------- int main(){double x;cin>>x;double sum = 1;double t = x;int i = 2;while (abs(t) > 1e-8){sum += t;t *= (-1)*x/i;i ++;}cout.precision(8);cout<<fixed<<"x="<<x<<", sum="<<sum<<"\n";return 0;}//============================//=============================//练习二第01题。
《C语言程序设计教程》(第三版)课后习题参考答案
C语言程序设计课后习题参考答案习题一一、单项选择题1、C2、B3、B4、C5、D6、A7、C8、A二、填空题1、判断条件2、面向过程编程3、结构化4、程序5、面向对象方法6、基本功能操作、控制结构7、有穷性8、直到型循环结构9、算法10、可读性11、模块化12、对问题的分解和模块的划分习题二一、单项选择题1、B2、D3、C4、B5、A6、A7、B8、C二、填空题1、主2、C编译系统3、结构化4、程序5、面向对象方法6、.OBJ7、库函数8、直到型循环结构习题三一、单项选择题1、D2、B3、A4、C5、A6、D7、B8、D9、B 10、C 11、A 12、D 13、C 14、B 15、C 16、A17、B 18、C 19、C 20、D 21、A 22、D 23、D 24、D、A25、D 26、A二、填空题1、补码2、10^-138~10^138、15~163、实4、单目运算符、自右向左5、函数调用6、65,89习题四一、单项选择题1、D2、C3、D4、A5、D6、B7、A8、C9、B 10、B二、填空题1、两, ;2、5.1690003、-200 2500、i=-200,j=2500回车、i=-200回车j=2500回车4、a=98,b=765.000000,c=4321.0000005、100 25.81 1.89234、100,25.81,1.89234、100回车25.81回车1.89234回车6、0,0,37、38、scanf(“%lf %lf %lf”,&a,&b,&c);9、13 13.000000 13.00000010、c=b-a;a=b+c;习题五一、单项选择题1、B2、D3、C4、B5、B6、D7、A8、B9、D二、填空题1、1 、02、k!=03、if(x>4||x<-4)printf(“%d”,x);else printf(“error!”);4、if(((x>=1&&x<=10)||(x>=200&&x<=210))&&(x%2!=0))Printf(“%d”,x);5、16、17、10!Right!8、a=09、2,110、0<a<b习题六一、单项选择题1、B2、C3、C4、B5、C6、B7、C8、A二、填空题1、无穷次2、83、204、115、3.66、*#*#*#$7、8 5 28、d=1.0 、k++、k<=n9、!(x<0)、x<amin习题七一、单项选择题1、B2、D3、C4、C5、A6、A7、D8、A二、填空题1、1 2 4 8 16 32 64 128 256 5122、a[age]++、i=18;i<263、break、i==84、a[i]>b[i]、i<3、j<55、b[j]=0、b[j]=a[j][k]习题八一、单项选择题1、B2、C3、C4、A5、A6、A7、C8、D9、D 10、B 11、A 12、C 13、A 14、C 15、B二、填空题1、return 1、return n+sum(n-1)2、return 1、n*facto(n-1)习题九一、单项选择题1、D2、C3、D4、A5、C6、C7、C8、C9、B 10、C 11、A 12、C 13、B 14、A 15、D 16、B17、C 18、A 19、B 20、C二、填空题1、int 、return z2、*p++3、’\0’、++4、p、max<*q、min>*q习题十一、单项选择题1、D2、D3、A4、5、B6、A7、C8、B9、D 10、11、C 12、D 13、D 14、C二、填空题1、34 122、ARRAY a[10],b[10],c[10];3、2 34、ab、cd5、(*b).day、b->day6、adabc defghi mnohi no7、(struct node *)、!=’\n’、p=top;8、p1=p1->next9、(struct list *)、(struct list *)、return (n)习题十一一、单项选择题1、A2、A3、B4、A5、B6、D7、B8、A9、A 10、B 11、B 12、B 13、C 14、C 15、D 16、A17、D二、填空题1、ASCII(文本)、二进制2、pf=fopen(“A:\\zk04\\data\\txfile.dat”,”w”);3、fputc()、fputs()、fscanf()、fread()4、(后两个空)文件结束符、非0值。
C++程序设计教程课后习题答案【钱能】清华版
1)please input 3 sides of one triangle:6,6,8a= 6.00,b= 6.00,c= 8.00area of triangle is 17.888542)该程序计算三角形的面积前后分为三部分:输入,处理,输出。
2.7In main():Enter two numbers:3 8Calling add():In add(),received 3 and 8and return 11Back in main():c was set to 11Exiting...3.1(1)sqrt(pow(sin(x),2.5))(2)(a*x+(a+x)/(4*a))/2(3)pow(c,x*x)/sqrt(2*M_PI) //M_PI为BC中math.h中的圆周率常数3.213.72.593.3(1)a1=1 a2=1(2)1.1(3)(4)205.4Main--x=5,y=1,n=1Func--x=6,y=21,n=11Main--x=5,y=1,n=11Func--x=8,y=31,n=215.6double poly(int n, double){if(n==0)return 1;if(n==0)return x;return ((2*n-1)*x*poly(n-1,x)-(n-1)*poly(n-2,x))/n;}6.1//6_1(1)//file1.cppint x=1;int func(){//...}//file2.cppextern int x;int func();void g(){x=func();}//file3.cppextern int x=2; //error: extern int变量若有赋值,则成定义int g(); //error: 函数声明与前面不一致void main(){x=g();}(2)//file1.cppint x=5;int y=8;extern int z;//file2.cppint x; //error: int x;重复定义extern double y; //error: y同一名字不同类型定义extern int z; //error: z只有声明,却无定义6.22510.4//10_4(1)见10_4_1.cpp(2)data? 3data? 5data? 7data? 6data? 4data? 8data? -3357648(3)见10_4_312.1Constructing normally.Constructing with a number: 20Display a number: 1Display a number: 20Destructing.12.2//12_2//见employee.h// employee.cpp 12.3//12_3.txt//见employ.h// employ.cpp//13_1.txt物质有机物生物动物哺乳动物人类猿类猫科类...卵生动物植物非生物无机物//13_2Ignore//14_1Multi[0] is:0Multi[1] is:1Multi[2] is:4Multi[3] is:9Multi[4] is:16 Multi[5] is:25 Multi[6] is:36 Multi[7] is:49 Multi[8] is:64 Multi[9] is:81 Destroying..9 Destroying..8Destroying..6Destroying..5Destroying..4Destroying..3Destroying..2Destroying..1Destroying..0//14_2//运行结果为:12345678910//增加拷贝构造函数后的运行结果为:149162536496481//14_3//运行结果为:create a X object with 1create a X object with 2 //创建临时对象call a func with para X objectcreate a X object from other X objecterase a object //析构临时对象create a X object from other X objectcall a func with para X objectcreate a X object from other X objecterase a objecterase a objecterase a objecterase a object17.1class A{public:int GetpA(){ return pA; }void SetpA(int a){ pA=a; }private:int pA;};class B :public A{public:void OnB(){}};class C :virtual public A, virtual public B{ public:Sc(){ /*use GetpA() and SetpA()... */ }};19.5Dill Arnson 8869533 139****1080 Welch Shammas 6695482 130****9001 Portel Braumbel 5937221 139****2991。
钱能--c++程序设计教程(第二版)第五章
可以在VS2010上运行,供正在学习C++的同学参考。
我也是新手,可能在编程风格和思路上有待提高,也有一些题目不知道该如何下手,路过的朋友知道的可以告诉我,我的邮箱是guosheng-14@,可以督促我继续写以后的答案。
vc++6.0可能在头文件上有所区别。
第五章第一部分//例5-2----寻找向量#include<iostream>#include<fstream>#include<sstream>#include<string>#include<vector>using namespace std;typedef vector<int> matrix1;typedef vector<matrix1> matrix2;void input(matrix2 &v2);void find(matrix2 &v2);void output(matrix1 &v1);int main(){matrix2 v2;input(v2);find(v2);}void input(matrix2 &v2){ifstream in("aa.txt");int n,t;in>>n;v2.resize(n);for(string s;getline(in,s)&&(n>0);n--)for(istringstream sin(s);sin>>t;v2[n].push_back(t));}void output(matrix1 &v1){for(int i=0;i<v1.size();i++) cout<<v1[i]<<" ";cout<<endl;}void find(matrix2 &v2){for(int i=0;i<v2.size();i++){for(int j=0;j<v2[i].size();j++)if(v2[i][j]==-1) output(v2[i]);}}//5.2.2—函数副作用#include<iostream>#include<vector>using namespace std;vector<int> add(vector<int> &a,vector<int> &b); void print(vector<int> &a);int main(){int aa[]={2,3,1,2,3,2,1},bb[]={5,3,1,1,6,2,2};vector<int> a(aa,aa+7);vector<int> b(bb,bb+7);vector<int> c;c=add(a,b);print(a);print(b);print(c);}void print(vector<int> &a){for(int i=0;i<a.size();i++)cout<<a[i]<<" ";cout<<endl;}vector<int> add(vector<int> &a,vector<int> &b) {for(int i=0;i<a.size();i++)a[i]+=b[i];return a;}//5.4.2—函数指针传递#include<iostream>#include<vector>#include<algorithm>using namespace std;int bitsum(int a);bool bitsumcomp(int a,int b) {return bitsum(a)<bitsum(b);} int main(){int a[]={33,41,12,19,14,71,78,59};vector<int> v(a,a+8);sort(v.begin(),v.end(),bitsumcomp);for(int i=0;i<v.size();i++) cout<<v[i]<<" ";cout<<endl;}int bitsum(int a){int x=0;x=a%10+a/10;return x;}//5.4.3—函数指针数组#include<iostream>using namespace std;typedef void (*Menufunction)();void f1() {cout<<"good";}void f2() {cout<<"better";}void f3() {cout<<"best";}int main(){Menufunction m[]={f1,f2,f3};cout<<"1---play good"<<endl<<"2---play better"<<endl<<"3---play best"<<endl<<"0---quite"<<endl;cout<<"your choise?"<<endl;for(int choise=1;choise;){cin>>choise;switch(choise){case 1:m[0]();break;case 2:m[1]();break;case 3:m[2]();break;}}}//5.7.1—函数重载示例#include<iostream>using namespace std;int myabs(int a) ///写成myabs,否则写成abs编译不通过{return (a>0)?a:-a;}double myabs(double a){return (a>0)?a:-a;}int main(){cout<<myabs(-10)<<endl;cout<<myabs(-12.23)<<endl;}//5.7.4—默认参数#include<iostream>using namespace std;void delay(int a=2);int main(){cout<<"delay 2s"<<endl;delay();cout<<"delay 5s"<<endl;delay(5);}void delay(int a){int sum=0;for(int i=1;i<=a;i++)for(int j=1;j<3500;++j)for(int k=1;k<100000;++k) sum++; }//5.7.7#include<iostream>#include<vector>using namespace std;vector<int> b(10,0);bool process(vector<int> &a);void print(vector<int> &a=b);int main(){vector<int> a(10,5);if(process(a)) print(a);else print();}bool process(vector<int> &a){int sum=0;for(int i=0;i<a.size();i++) sum+=a[i];if(sum<100) return 1;else return 0;}void print(vector<int> &a){for(int i=0;i<a.size();i++)cout<<a[i]<<" ";cout<<endl;}第二部分//1—递归#include<iostream>using namespace std;double poly(int ,double);int main(){cout<<"please input a double:"<<endl;double x;int n;cin>>x;cout<<"please input a int:"<<endl;cin>>n;cout<<poly(n,x)<<endl;}double poly(int n,double x){if(n==0) return 1;else if(n==1) return x;else if(n>1) return (((2*n-1)*x*poly(n-1,x)-(n-1)*poly(n-2,x))/n); }//2—改写#include<iostream>using namespace std;void input(int a[][5]);int total(int a[][5]);double ever(int a[][5],int);int main(){int a[4][5];input(a);cout<<"total: "<<total(a)<<endl;cout<<"ever: "<<ever(a,20)<<endl;}void input(int a[][5]){cout<<"please input 20 numbers:"<<endl;for(int i=0;i<4;i++)for(int j=0;j<5;j++)cin>>a[i][j];}int total(int a[][5]){int sum=0;for(int i=0;i<4;i++)for(int j=0;j<5;j++)sum+=a[i][j];return sum;}double ever(int a[][5],int x){return (total(a)/double(x));}//3—文本最大公约数#include<iostream>#include<fstream>#include<vector>#include<sstream>#include<string>#include<algorithm>using namespace std;int gongyue(int a,int b);void print(vector<int> v);int main(){ifstream in("abc.txt");vector<int> v;for(string s;getline(in,s);){int a,b;istringstream sin(s);sin>>a>>b;v.push_back(gongyue(a,b));}print(v);sort(v.begin(),v.end());print(v);}int gongyue(int a,int b){int item=((a>b)?b:a);for(int i=item;i>=1;i--){if(a%i==0&&b%i==0)return i;}}void print(vector<int> v){for(int i=0;i<v.size();i++)cout<<v[i]<<" ";cout<<endl;}4.又是这个题。
C程序设计教程——设计思想与实现习题解答钱能
二#include <>void main(){ .#include <> #include <> doubleCylinder(double r, double h); voidmain(){double radius, height;cout <<"请输入圆柱体的半径和高:\n";cin >>radius >>height;double volume = Cylinder(radius, height);cout <<"该圆柱体的体积为" <<volume <<endl;}double Cylinder(double r, double h){return r*r*M_PI*h;}三(1) sqrt(pow(sin(x),)(2) (a*x+(a+x)/(4*a))/2pow(c,x*x)/sqrt(2*M_PI) n"; break; case 1: cout <<"只能被 7 整除.\n"; break; case 2: cout <<"只能被 5 整除.\n"; break; case 3: cout <<"可以被 5,7 整除.\n";break; case 4: cout <<"只能被 3 整除.\n"; break; case 5: cout <<"可以被 3,7 整除.\n"; break; case 6: cout <<"可以被 3,5 整除.\n"; break;case 7: cout <<"可以被3,5,7整除.\n"; break; }}#include <>void main(){int grade; cout <<"please input a number:\n"; cin >>grade; if(grade>100||grade<0)cout <<"错误的成绩.\n";else if(grade>=90)cout <<"A.\n"; elseif(grade>=80) cout<<"B.\n"; elseif(grade>=70) cout<<"C.\n"; elseif(grade>=60) cout<<"D.\n"; elsecout <<"E.\n";}四1#include <> #include <>void main(){double sum=1, t=-1, x; int i=1; cout<<"please input a value:\n"; cin >>x;do{ t*=(-1)*x/i; sum+=t;i++; }while(fabs(t)>1e-8);cout <<"sum=" <<sum<<endl;}2#include <>#include <> void main(){double sum=1, t=-1, x; cout<<"please input a value:\n"; cin >>x;int i=1; while(fabs(t)>1e-8){ t*=(-1)*x/i; sum+=t; i++; } cout<<"sum=" <<sum<<endl;3#include <> #include <>void main(){double sum=1, t=-1, x; cout<<"please input a value:\n"; cin >>x;for(int i=1; fabs(t)>1e-8; i++){t*=(-1)*x/i; sum+=t; } cout<<"sum=" <<sum<<endl;}#include <>void main(){long sum=0, t=1; for(int i=1;i<=15; i++){ t*=i; sum+=t; }cout <<"sum=" <<sum <<endl;}#include <>void main(){for(int i=1; i<=9; i++) for(int j=0; j<=9; j++) for(int k=0; k<=9; k++) if(i*i*i+j*j*j+k*k*k == 100*i+10*j+k)cout <<(100*i+10*j+k) <<"是水仙花数.\n";}#include <>void main(){for(int i=1; i<1000; i++){ int sum=0; for(int j=1; j<=i/2; j++) if(i%j==0) sum+=j;if(sum==i) cout <<i<<"是完数.\n";}#include <>void main(){float s=100,h=100;for(int i=1; i<10;i++){ s+=h; h/=2;}cout <<"共经过" <<s <<"米第 10 次反弹" <<h <<"米高.\n"; }#include <> void main(){ int peachs=1;for(int i=1;i<10; i++)peachs=(peachs+1)*2;cout <<"第一天共摘下" <<peachs <<"个桃子.\n"; }#include <> #include <>void main(){double x, a; cout <<"please input avalue:\n"; cin >>a; x = a/2;while(fabs((x-a/x)/2)>1e-7)x=(x+a/x)/2;cout <<a <<"的平方根是" <<x <<endl;}1#include <>void main(){for(int i=1; i<=10;i++){ for(int j=1; j<=10-i; j++)cout <<" "; for(int j=1; j<=2*i-1; j++) cout <<"#"; cout<<endl;}}2#include <>void main(){ for(int i=1; i<=8; i++){ for(intj=1; j<=i; j++) cout <<" ";for(int j=1; j<=18-i; j++)cout <<"#"; cout <<endl;}}1#include <> #include <>void main(){cout <<" *"; for(inti=1; i<=9; i++) cout<<setw(4) <<i;cout <<"\n-----------------------------------------\n"; for(int i=1; i<=9; i++){ cout <<setw(3) <<i; for(int j=1; j<=9; j++) cout <<setw(4) <<i*j; cout <<endl;}}2#include <> #include <>void main(){cout <<" *"; for(inti=1; i<=9; i++) cout<<setw(4) <<i;cout <<"\n-----------------------------------------\n"; for(int i=1; i<=9; i++){ cout <<setw(3) <<i; for(int j=1; j<=i; j++) cout <<setw(4) <<i*j; cout <<endl;}}3#include <> #include <>void main(){ cout <<" *";for(int i=1;i<=9; i++)cout <<setw(4)<<i;cout <<"\n-----------------------------------------\n";for(int i=1; i<=9; i++){ cout <<setw(3) <<i;if(i!=1)cout <<setw(4*i-4) <<" ";for(int j=i; j<=9; j++) cout<<setw(4) <<i*j; cout <<endl;}}#include <>void main(){ int n; long a=1, b=1, c=1, temp;cout <<"please input a value:\n";cin >>n; for(int i=4; i<=n;i++){ temp=a+c; a=b; b=c; c=temp;} cout << c <<endl;}五#include <> #include <>#include <> boolisprime(long n);void main(){.}.}(2) <<a[i] <<" boy has won.\n"; <<a[i] <<" boy has won.\n"; n";cout <<"please select following operation:\n"; cout <<"\t\tstop at once! ----- 1\n"; cout<<"\t\tinput again. ------- 2\n"; cout <<"\t\tas default values -- 3\n\n"; int sele=0; cin >>sele; switch(sele){ case 1: return false; case 2: sign=false; break; case 3: num=10; interval=3; return true;}(3)#include <> const size=10; voidfindmax(int* a, int n, int& pk); voidmain(){ int a[size];int n=0;cout <<"please input " <<size <<"datas:\n";for(int i=0; i<size; i++) cin >>a[i]; findmax(a,size,n); cout <<"the maximum is "<<a[n] <<endl<<"It's index is " <<n <<endl;}void findmax(int* a,int n,int& pk){ for(int i=1; i<n; i++)if(a[i]>a[pk])pk=i;}9_2#include <> #include <>int& put(int n); .put(9)=30;cout <<get(0) <<endl; cout<<get(1) <<endl;cout <<get(9) <<endl; put(12)=1; .}int& put(int n){if(n>=10){ cerr <<"range error in putvalue!\n";exit(1); }return vals[n];}int get(int n){if(n>=10){ cerr <<"range error in getvalue!\n"; exit(1); } return vals[n];}#include <>void Swap(char*& str1, char*& str2);void main(){char* ap="hello"; char* bp="howare you?"; cout <<ap <<endl <<bp<<endl; Swap(ap, bp); cout <<"交换以后:\n"; cout <<ap <<endl<<bp <<endl;}void Swap(char*& str1, char*& str2){ char* temp=str1;str1=str2;str2=temp;}一○#include <> structStudent{ intmathmidterm;int mathfinal;};void main(){Student s;cout <<"please input midterm and final math\n" <<"grade of a student:\n"; cin >> >>;cout <<"the average grade of midterm and final math is " << + / <<endl;}10_2#include <> #include<> struct Node{ charstr[20]; Node* next;};void Insert(Node*& head); voidmain(){Node* p;Node* x=new Node;strncpy(x->str,"hello",20);x->next = NULL; p =x; x=newNode;strncpy(x->str,"jone",20);x->next = p; p=x;x=new Node;strncpy(x->str,"good",20);x->next = p; p=x;x=new Node;strncpy(x->str,"better",20); x->next =p; p=x; cout <<"\n 插入之前:\n";for(Node* pT=p; pT; pT=pT->next) cout <<pT->str <<"->"; cout <<"0\n"; Insert(p);cout <<"\n 插入之后:\n"; for(Node*pT=p; pT; pT=pT->next) cout <<pT->str <<"->";cout <<"0\n";}void Insert(Node*& head){Node* p=new Node;strncpy(p->str,"marit",20);head->str[19]='\0';if(!head){ head = p;p->next = NULL; return; }if(!strcmp(head->str,"jone")){ p->next = head; head= p; return; } Node* sp;for(sp=head; sp->next&& strcmp(sp->next->str,"jone"); sp=sp->next); p->next = sp->next;sp->next = p;}10_3#include <> struct Node{char c;Node* next;};Node* reverse(Node* head);void main(){Node x[4]; for(int i=0; i<4;i++){ x[i].c='A'+i; cout <<x[i].c <<"->";x[i].next = &x[i+1]; } cout <<"NULL"<<endl; x[3].next = NULL; Node* head =reverse(x); for(Node* pH=head; pH;pH=pH->next) cout <<pH->c <<"->"; cout<<"NULL" <<endl;}Node* reverse(Node* head){Node* newHead=NULL; for(Node*pT=head; pT;pT=head){ head=head->next;pT->next = newHead; newHead = pT; }return newHead;}10_4 (1) #include <>struct Lnode{double data; Lnode*next;};void ShowList(Lnode* head){ for(Lnode* p=head; p; p=p->next) cout <<p->data <<endl;}void AddToEnd(Lnode* pnew, Lnode*& head) {if(!head)head=pnew; else{Lnode* p; for(p=head; p->next;p=p->next); p->next = pnew;}pnew->next=NULL;}Lnode* GetNode(){Lnode* item = new Lnode;if(item){ item->next=NULL;item->data=; }else cout<<"Nothing allocated\n"; return item;}void main(){Lnode* head=NULL; Lnode* temp; double d; cout <<"data? "; cin >>d; while(d>0&&(temp=GetNode())){ temp->data=d; AddToEnd(temp, head); cout <<"data? "; cin >>d; }ShowList(head);}(2) data?3 data? 5data? 7data? 6data? 4data? 8data? -3357648(3)#include <>struct Lnode{double data; Lnode*next;};void ShowList(Lnode* head); void AddToEnd(Lnode* pnew, Lnode*& head); Lnode* GetNode(); void DeleteList(Lnode* head); void main(){Lnode* head=NULL; Lnode* temp;double d; cout <<"data? "; cin >>d;while(d>0&&(temp=GetNode())){ temp->data=d; AddToEnd(temp, head);cout <<"data? "; cin >>d; }ShowList(head);DeleteList(head);}void ShowList(Lnode* head){ if(head){cout<<head->data<<endl;if(head->next)ShowList(head->next);n";();}11_3#ifndef DATE#define DATE #include<> class Date{ public:void Display(); void AddOneDay(); void SetDay(inty,int m,int d); protected: boolLegal(int y, int m, int d); boolIsLeapYear(int y); int year; intmonth; int day;};void Date::Display(){cout <<day <<"/" <<month <<"/" <<year <<endl;}void Date::AddOneDay(){if(Legal(year,month,day+1)) day++;else if(Legal(year,month+1,1))month++,day=1;else if(Legal(year+1,1,1))day=1,month=1,year++;}void Date::SetDay(int y, int m, int d){if(Legal(y,m,d))day=d, month=m, year=y;}bool Date::Legal(int y, int m, int d){if(y>9999||y<1||d<1||m<1||m>12)return false; int dayLimit=31;switch(m) case 4: case 6: case 9: case 11: dayLimit--; if(m==2) dayLimit = IsLeapYear(y) ? 29 : 28;return (d>dayLimit)? false : true;}bool Date::IsLeapYear(int y){return !(y%4)&&(y%100)||!(y%400);}#endif11_4 #include ""void main(){Time a;(3,32,48);();}11_5#include <> #include ""void main(){Stack sta;(10); (12); (14); cout<<() <<endl; cout <<()<<endl;}n";}n"; exit(1); } int result = head->a; Node* p = head; head = head->next; delete p; return result;}Constructing with a number: 20Display a number: 1Display a number: 20Destructing. Destructing.12_2 ,"Revere","CA","12290"); ();("Richard Voss"); ();}物质一三13_1生物有机物动物哺乳人动物类猿类猫科类...卵生动物植物非生物无机物13_2Ignore一四14_1Multi[0] is:0Multi[1] is:1 Multi[2]is:4 Multi[3] is:9Multi[4] is:16 Multi[5]is:25 Multi[6] is:36Multi[7] is:49 Multi[8]is:64Multi[9] is:81 Destroying..9Destroying..8Destroying..7Destroying..6 Destroying..5 Destroying..4 Destroying..3 Destroying..2 Destroying..1 Destroying..014_2#include <> #include <> class Vector{ public: Vector(ints=100); Vector(const Vector& v); int& Elem(int ndx); void Display(); voidSet(); ~Vector(); protected:int size; int* buffer;};Vector::Vector(int s){buffer=new int[size=s]; for(inti=0; i<size; i++)buffer[i]=i*i;}Vector::Vector(const Vector& v):size {buffer = new int[size]; for(inti=0; i<size; i++)buffer[i] = [i];}int& Vector::Elem(int ndx){if(ndx<0||ndx>=size){ cout<<"error in index" <<endl;exit(1);} return buffer[ndx];}void Vector::Display(){ for(int j=0; j<size; j++)cout <<buffer[j] <<endl;}void Vector::Set(){ for(int j=0; j<size; j++)buffer[j]=j+1;}Vector::~Vector(){ delete[]buffer; }void main(){Vector a(10); Vectorb(a); ();();}14_2.\n"; (6);cout <<"Creating boots from frisky\n"; CATboots(frisky);cout <<"frisky's age:" <<() <<endl; cout<<"boots'age:" <<() <<endl; cout <<"setting frisky to 7...\n"; (7);cout <<"frisky's age:" <<() <<endl;cout <<"boots'age:" <<() <<endl;}一五15_1 (1)Return\n"<<"1. Savings business\n"<<"2. Checking business\n\n"<<"select : "; cin >>sele;if(sele==1||sele==2) DoAccess(sele);if(sele==0) break; } Return\n"<<"1. Fetching\n"<<"2. Saving\n\n"<<"select : "; cin >>sele;if(sele==1||sele==2)if(s==1) DoSavings(sele);elseDoChecking(sele);if(sele==0) break; }Return\n"<<"1. Savings business\n"<<"2. Checking business\n"<<"3. Credit business\n\n"<<"select : "; cin >>sele;if(sele>=1 && sele <=3) DoAccess(sele); }Account* p=Account::First(); Return\n"<<"1. Fetching\n"<<"2. Saving\n\n" <<"select : ";cin >>sele; if(sele!=1 && sele!=2)continue; switch(type){ case1: DoSavings(sele); break; case 2:DoChecking(sele); break;case 3: DoCredit(sele); break;}}}void DoSavings(int access){unsigned aN; float val; if(access==2){ n";cout <<"\nplease input a account number and amount for fetching(input 0 for end):\n"; cin >>aN >>val; } }}void DoChecking(int access){unsigned aN; floatval;if(access==2){ n";cout <<"\nplease input a account number and amount for fetching(input 0 for end):\n"; cin >>aN >>val; } }}void DoCredit(int access){unsigned aN; floatval;if(access==2){ n";cout <<"\nplease input a account number and amount for fetching(input 0 for end):\n"; cin >>aN >>val; } }}16_5#include <>#include "i:\c++答案\ch16\" #include "i:\c++答案\ch16\"#include "i:\c++答案\ch16\"#include "i:\c++答案\ch16\16_4\"#include ""void DoSavings(int access);void DoChecking(int access);void DoAccess(int type); voidDoCredit(int access);void DoFixed(int access);void main(){ int sele=1;while(sele){cout <<"\nbank management\n\n" <<"0. Return\n"<<"1. Savings business\n"<<"2. Checking business\n"<<"3. Credit business\n"<<"4. Fixed business\n\n"<<"select : ";cin >>sele;if(sele>=1 && sele <=4) DoAccess(sele);}Account* p=Account::First(); Return\n"<<"1. Fetching\n"<<"2. Saving\n\n"<<"select : "; cin >>sele;if(sele!=1 && sele!=2) continue;switch(type){case 1: DoSavings(sele); break; case 2: DoChecking(sele); break; case 3: DoCredit(sele); break; case 4: DoFixed(sele);}}}void DoSavings(int access){unsigned aN; float val; if(access==2){ n";cout <<"\nplease input a account number and amount for fetching(input 0 for end):\n"; cin >>aN >>val; } }}void DoChecking(int access){unsigned aN; floatval;if(access==2){ n";cout <<"\nplease input a account number and amount for fetching(input 0 for end):\n"; cin >>aN >>val; } }}void DoCredit(int access){unsigned aN; floatval;if(access==2){ n";cout <<"\nplease input a account number and amount for fetching(input 0 for end):\n"; cin >>aN >>val; } }}void DoFixed(int access){unsigned aN; floatval;if(access==2){ n";cout <<"\nplease input a account number and amount for fetching(input 0 for end):\n";cin >>aN >>val;}}}一七classA{ public:int GetpA(){ return pA; }void SetpA(int a){ pA=a; }private: int pA;};class B :public A{ public:void OnB(){}};class C :virtual public A, virtual public B{ public:Sc(){ /*use GetpA() and SetpA()... */ }};#include <>#include <>class Father{ public:Father(){ cout <<"father is created.\n"; }v oid DoWork(){ DriveACar(); } voidDoMannulWork(){ RepairTV(); }void DriveACar() { cout <<&typeid(*this).name()[6] <<" Drive a car.\n"; } protected:void RepairTV() { cout <<&typeid(*this).name()[6] <<" Repair a TV set.\n"; }};class Mother{ public:Mother(){ cout <<"mother is created.\n"; }void SingASong() { cout <<&typeid(*this).name()[6] <<" Sing a song.\n"; }void DoWork(){ SingASong(); } ame()[6] <<" Do mannul work.\n"; } n"; } void RepairTV(){ cout <<"boy "; Father::RepairTV(); } voidSingASong(){ cout <<"boy "; Mother::SingASong(); }void PlayPingPong() { cout <<&typeid(*this).name()[6] <<" Play pingpong.\n"; };};void main(){Father father;Mother mother;Boy boy;(); ();();(); ();();();();}n";return *this;}yuan-=( (jf< ?1:0); jf=(jf+%100;return *this;}RMB RMB::operator*(double d)const{double temp=(yuan+jf/*d; unsigned y= long(temp); unsigned j = ((temp-y)*1000+5)/10;return RMB(y,j);}RMB operator*(double d, const RMB& r){ return r * d; }void main(){RMB d1(1, 60);RMB d2(2, 50);RMB d3(0, 0); d3= d1 + d2; d1+= d3; d3 -= d2;();();}一九#include <> #include <>void main(){char str[128]; intcount=0; ifstreamfin(""); (str,sizeof(str));while(!()){ count++;(str,sizeof(str));}cout <<"the amount of lines of file is " <<count <<endl; }19_2#include <> #include<> class RMB{ public:RMB(double v =:yuan(v){ jf =(v-yuan)*100+; }operator double(){ return yuan+jf/; } voiddisplay(ostream& out){int n=1; .for(int x=yuan; x/10; x/=10) n++;if(n>5){ out <<"*******.**"; return; }double t=yuan+jf/;out <<setw(7-n) <<"" <<setiosflags(ios::fixed)<<setprecision(2) <<setw(n+3) <<t;}protected:unsigned int yuan;unsigned int jf;};ostream& operator <<(ostream& oo, RMB& d){(oo);return oo;}void main(){RMB rmb;cout <<"Initially rmb = " <<rmb <<"\n"; rmb=*rmb;cout <<"then rmb = " <<rmb <<"\n";}19_3#include <> #include <>void main(){ int n;char string[]="1 2 3 4 5 6 7 8 9"; istrstreams(string);while(!()){ s >>n;cout <<n<<" ";}cout <<endl;}19_4#include <> #include <>void main(){cout <<setiosflags(ios::uppercase); for(charch='A'; ch<='Z'; ch++){ cout <<ch <<" " <<hex<<(int)ch <<", "; if(ch=='I'||ch=='R') cout<<endl;} cout<<endl;}19_5#include <> #include <>#include <>#include <>class AddressBook{ public:AddressBook(char* n, char* p, char* h);virtual void Display(ostream& out);protected: char name[20]; char phone[10];char handp[15];};AddressBook::AddressBook(char* n, char* p, char* h) {strncpy(name, n, 19); name[19]=0; strncpy(phone, p,9); phone[9]=0;strncpy(handp, h, 14); handp[14]=0;}void AddressBook::Display(ostream& out){out <<setiosflags(ios::left) <<setw(20) <<name <<setw(10) <<phone <<setw(15) <<handp <<endl; }ostream& operator<<(ostream& out, AddressBook& a){(out);return out;}void main(){AddressBook a1("Dill Arnson", "8869533", ""); AddressBook a2("WelchShammas", "6695482", ""); AddressBook a3("Portel Braumbel", "5937221","");cout <<a1; cout<<a2; cout <<a3;}Dill Arnson 8869533Welch Shammas 6695482 Portel Braumbel 5937221二○20_1#include <> #include <>template<class T> Tmin(T& a, T& b){return a>b?b:a;}char* min(char* a, char* b){return (strcmp(a, b)>0 ? b: a);}void main(){cout <<min("Hello", "Good") <<endl; cout <<min(3,8) <<endl;}20_2 #include <>template<class T> classStack{ public: Stack();~Stack(){ delete[] stack; }void Push(T& n); T Pop(); private: static const int SIZE;T* stack; int tos;};template<class T>const int Stack<T>::SIZE = 100;template<class T> Stack<T>::Stack() :tos(0){stack = new T[SIZE];}template<class T> voidStack<T>::Push(T& n){if(tos==100) return;stack[tos++] = n;}template<class T> TStack<T>::Pop(){if(tos==0) return T(0);return stack[--tos];}void main(){Stack<int> ai; Stack<char>ac;Stack<float> af;int i[3]={3,5,7};(i[0]); (i[1]);(i[2]);char c[3]={'2','5','9'}; (c[0]);(c[1]); (c[2]);float f[2]={,}; (f[0]); (f[1]);cout <<() <<" "; cout <<()<<" "; cout <<() <<endl;cout <<() <<" "; cout <<()<<" "; cout <<() <<endl;cout <<() <<" ";cout <<() <<" " <<endl;}二一ch21_1 #include <>#include <>class String{ public: String(char*, int); i nt Length(){ return len; } class Range{ n";}n\n";}void main(){n";}#include <> classA{ public:class Error{};A(){n = new int;i nit();}private: int*n; void init(){. throw Error();}};void main(){try{ A a; } catch(A::Error&){ cout <<"error when initialize.\n";}}。
C++程序设计(第二版)钱能-第12章--多态
第12章 多态12.9 练习12(Exercises 12)1.使用 CHI2.6中的Account 类、Savings 类、Checking 类和AccountList 类,编写一个应用程序,它从文件account.txt 中读入一些账户号和对应的存款额,创建若干个Savings 和Checking 账户,直到遇到一个结束标志“x 0”,并输出所有账户号的存款数据。
aaxnmt.txt 的文件内容如下:ac c ou nt.t xt解答:2.使用 CH12.6中的 Account 类、Savings 类、Checking 类和AccountList 类,在第 1 题的基础上增加一个取款函数,它从文件withdrawal.txt 中读入一些账户号和取款额,查找并做取款操作,直到遇到一个结束标志“x 0”。
显然,取款操作是多态的。
Withdrawal.txt 文件内容如下:ac c ou nt.t xt解答:3.信用卡Credit 是一种储蓄Savings 账户,它的透支范围与savings 相同,但它有一个用户密码,取款时,必须验证密码。
试从Account 账户体系中派生一个信用卡类,写出其类定义和实现。
解答:4.定期储蓄是储蓄的一种,假设定期分一年期、三年期和五年期,利率分别为5%、8%和10%。
用户在办理定期存款账户时,必须确定其定期时段,中途不再在同一账号上办理存款业务。
取款是一次性完成的,若提前取款,则全部金额的利息按活期利率1%计算。
试将其银行业务设计成定期类。
解答:5.定义一个具有多态性的基类Shape,继承以下一些类:圆Circle类(坐标点和半径构成),长方形Rectangle类(两个不重合的坐标点构成),三角形Triangle 类(三个互不重合的坐标点构成)。
定义一些操作,特别是定义求面积操作。
编制应用程序,按文件内容创建各类对象,放在Shape指针向量中。
循环处理向量中的每个元素,求其面积。
c程序设计教程(修订版)钱能
c程序设计教程(修订版)钱能《C程序设计教程(修订版)》是一本面向计算机科学与技术专业学生以及自学者的C语言教材,由钱能教授编写。
这本书以其系统性、实用性和易读性著称,深受广大读者的喜爱。
以下是对这本书的详细介绍:书籍概述《C程序设计教程(修订版)》在原有的基础上进行了更新和修订,以适应现代计算机科学教育的需求。
这本书不仅涵盖了C语言的基础语法和编程技巧,还介绍了一些高级编程概念,如指针、结构体、文件操作等。
第一部分:C语言基础- 数据类型与变量:介绍了C语言中的基本数据类型,包括整型、浮点型、字符型等,以及如何声明和使用变量。
- 运算符与表达式:详细解释了C语言中的算术运算符、关系运算符、逻辑运算符等,以及它们的优先级和结合性。
- 控制语句:包括条件语句(if、switch)、循环语句(while、for、do-while)和跳转语句(break、continue、return)。
第二部分:函数与模块化编程- 函数的定义与调用:介绍了如何定义函数、参数传递、返回值以及函数的调用方法。
- 作用域与生命周期:讲解了变量的作用域和生命周期,以及如何使用静态存储类。
- 模块化编程:强调了模块化编程的重要性,以及如何通过函数实现代码的重用和模块化。
第三部分:数组与字符串- 数组:介绍了一维数组和多维数组的声明、初始化和使用。
- 字符串:详细讲解了C语言中字符串的表示、存储和操作,包括字符串的输入输出、连接、复制等操作。
第四部分:指针- 指针的基本概念:解释了指针是什么,以及如何声明和使用指针。
- 指针与数组:探讨了指针与数组的关系,以及如何使用指针操作数组元素。
- 指针与函数:介绍了如何通过指针传递函数参数,以及指针作为函数的返回值。
第五部分:结构体与联合体- 结构体:介绍了如何定义和使用结构体,以及结构体在数据组织中的作用。
- 联合体:讲解了联合体的概念和用法,以及它与结构体的区别。
第六部分:文件操作- 文件的打开与关闭:介绍了如何在C语言中打开和关闭文件。
c程序设计教程钱能课后答案
c程序设计教程钱能课后答案【篇一:c语言程序设计教程课后习题参考答案】t>课后习题参考答案习题11. (1)编译、链接.exe(2)函数主函数(或main函数)(3)编辑编译链接2.(1)-(5):ddbbc (6)-(10):abbbc3.(1)答:c语言简洁、紧凑,使用方便、灵活;c语言是高级语言,同时具备了低级语言的特征;c语言是结构化程序设计语言,具有结构化的程序控制语句;c语言有各种各样的数据类型;c语言可移植性好;生成目标代码质量高,程序执行效率高。
(2)编辑、编译、链接、执行(3)一个c程序由一或多个函数组成,一函数若干条语句构成,每条语句的末尾必须以分号结束。
(4)标识符,关键字,运算符,分隔符,常量,注释符等4. 从键盘输入一个双精度小数,打印出它的余弦值。
#include stdio.h#include math.hmain( ){double x;scanf(“%lf”, x);printf(“%lf\n”, cos(x) );}第2章1.(1)bde、acfg(2)d (3) c (4) c2.(1)错(2)错(3)错(4)对(5)错3.(1)a=3,b=-27(2)a=11,b=6,c=6(3)3(4)1 0 1 0 1 1 0(5)-9 9 8(6)1)20 2)8 3)70 4)0 5)0 6)0 4.(1)#include stdio.hmain( ){double r, h ,v;r = 2.5;h = 3.5;v = 3.14*r*r*h;printf(“v=%lf\n”, v);}(2)#include stdio.hmain( ){char ch;ch = getchar( );printf(“%c\n”, ch + 32);}(3)#include stdio.hmain( ){printf(“*\n”);printf(“ ***\n”);printf(“ *****\n”);printf(“*******\n”);}(4)#include stdio.hmain( ){double x;scanf(“%lf”, x);printf(“%d , %lf\n”, (int)x, x – (int)x );(5)#include stdio.hmain( ){double a=3, b=5;double result = (-2 * a + ( 4*a – b )/( 2*a + b ) )/( (a - 4*b)/(a + b) ); printf(“%lf\n”, result);}习题31.(1)d(2)ad(3)c(4)b(5)a(6)-(10):bdacb2.(1)3.141593,3.1416,3.142(2)c=k(3)| 123.46|,|123 |(4)x=1.23,y= 50.00(5)03.(1)scanf(%f”, c); 改为:scanf(“%f”, c);f = (9/5)*c+32; 改为:f = (9.0/5)*c + 32;printf(“摄氏温度%f度相当于华氏温度%f度”, c, f); 改为:printf(“摄氏温度%f度相当于华氏温度%f度”, c, f);(2)补充定义:int h;h = 500/60 改为: h = 500 / 60;m = 500% 60改为: m = 500%60;printf(“500分钟是%d小时%d分钟,”h, m);改为:printf(“500分钟是%d小时%d分钟” , h, m);4.(1)#includestdio.hmain( ){char x,y;scanf(“%c%c”, x, y);printf(“%d\n”, (x-?0?) + (y-?0?) );(2)#include stdio.hmain( ){char x, y;char tmp;printf(“input two characters:”);scanf(“%c%c”, x, y);printf(“before swap: x=%c, y=%c\n”, x, y);tmp = x;x = y;y = tmp; printf(“after swap: x=%c, y=%c\n”, x, y); }(3)#include stdio.hmain( ){char ch;ch = getchar( );printf(“%c\n”, ch - 32);}第4章1.(1)-(5):caaca2.(1)bbb(2)aaabbbccc(3)end(4)d=20(5)s=2,t=3(6)firstthird(7)y=0 y=5 y=10 y=53.(1)yz xz xy(2)ch=?a? ch=?z?ch=?a?ch=?z? ch = ch-32(3)x2x=10x-1x=2(4)t=x; x=y; y=t;4.(1)main( ){int x, y , z, t;scanf(“%d%d%d”, x, y, z); if ( xy ){ t=x; x=y; y=t; }if( x z )【篇二:钱能c++程序设计教程(第二版)第三章答案[1]】p> //3.3.2—浮点数表示#includeiostreamusing namespace std;int main(){}//3.4.2—字符操作函数相关 #includeiostreamusing namespace std;int main(){} char* s1=hello; char* s2=123; char a[20]; strcpy(a,s1);cout(strcmp(a,s1)==0?:not )equal.endl; coutstrcat(a,s2)endl; coutstrrev(a)endl; coutstrset(a,c)endl;cout(strstr(a,ell)?:not )find.endl; cout(strchr(a,c)?:not )findendl; float f=19.2f; coutfendl; unsigned int*p=reinterpret_castunsigned int*(f); for(int i=31;i=0;i--)cout(*pi1)(i==31||i==23?-:); coutendl;//3.4.3string函数相关#includeiostream#includestringusing namespace std;int main(){string a,s1=hello,s2=123; a=s1;} cout(a==s1?:not )equal.endl; couta+s2endl; reverse(a.begin (),a.end()); coutaendl; couta.replace (0,9,9,c)endl;cout(a.find(ell)?:not )findendl; cout(a.find(c)?:not )findendl;//3.4.5string流+文件流#includeiostream#includesstreamusing namespace std;int main(){} ifstream fin(original.txt); for(string s;getline(fin,s);) { } int a,sum=0; for(istringstream sin(s);sina;sum+=a); coutsumendl; //3.5.2--sizeof()#includeiostreamusing namespace std;int main(){} int a[]={1,15,8,3,8,48,2,64,5}; for(inti=0;isizeof(a)/sizeof(a[0]);i++) cout a[i]; coutendl;//3.5.3--初始化#includeiostreamusing namespace std;int array1[5]={1,2,3};int array2[5];int main(){}//3.5.4—二维数组#includeiostreamusing namespace std;int main(){int a[2][3]={1,2,3,4,5}; int b[2][3]={{1,2},4}; coutarray1:endl; for(int i=0;isizeof(a)/sizeof(a[0]);i++) { }cout=================================endl;coutarray2:endl; for(int i=0;isizeof(b)/sizeof(b[0]);i++) {cout ; for(int j=0;jsizeof(b[0])/sizeof(b[0][0]);j++) cout ; for(intj=0;jsizeof(a[0])/sizeof(a[0][0]);j++) couta[i][j] ; coutendl; int array3[5]={2}; int array4[5]; coutarray1:; for(inti=0;isizeof(array1)/sizeof(array1[0]);i++) coutarray1[i] ; coutendl; coutarray2:; for(inti=0;isizeof(array2)/sizeof(array2[0]);i++) coutarray2[i] ; coutendl; coutarray3:; for(inti=0;isizeof(array3)/sizeof(array3[0]);i++) coutarray3[i] ; coutendl; coutarray4:; for(inti=0;isizeof(array4)/sizeof(array4[0]);i++) coutarray4[i] ; coutendl;}} coutb[i][j] ; coutendl;cout=================================endl;//3.6.2—文件流+向量#includeiostream#includefstream#includevectorusing namespace std;int main(){} ifstream fin(original.txt); vectorint va; for(int a;fina;)va.push_back(a); int s=0; for(vectorint::iteratorp1=va.begin();p1va.end();p1++) { } coutsendl;for(vectorint::iterator p2=p1+1;p2va.end();p2++)if((*p1)==(*p2)) s++;//3.6.3二维向量#includeiostream#includesstream#includefstream#includevectorusing namespace std;typedef vectorvectorint matrix; matrix input();void sort(matrix a);void print(matrix a); int main() {}matrix input() {}void sort(matrix a) {}void print(matrix a) {} for(int i=0;ia.size();i++) { } for(int j=0;ja[i].size();j++)couta[i][j] ; coutendl; for(int i=0;ia.size();i++) {} for(intj=i;ja.size();j++) {} if(a[i].size()a[j].size()) a[i].swap(a[j]); matrix a; ifstream in(aa.txt); for(string s;getline(in,s);) { } return a; vectorint b; for(int t;sint;) b.push_back(t); a.push_back(b); istringstream sin(s); matrix a=input(); sort(a); print(a);【篇三:c程序设计教程与实验第8章习题参考答案】题 1. 22. 6 7 200c 3.p[5]4. 定义p1为指向整型数据的指针变量定义p2为指针数组,其数组元素均为指针定义p3为指向一维数组的指针,其数组元素均为整型数 5. 5,3 6. 207. r+b[k] *x8. secondlifirstwang 9. 2510. 12 12三程序分析题 1.72. 1 5 33 9 7 3.244. abcddfefdbd5. 53 四、编程题1.输入2个字符串,每个字符串的长度均不超过80字符,用自己实现的cmpstr()函数完成这两个字符串的大小比较,cmpstr()函数的功能和字符串比较函数strcmp()的功能相同 #include stdio.h#includestring.hint cmpstr(char *s1, char *s2) {char *p, *q; p=s1; q=s2;while(*p!=\0 *q!=\0) {if(*p==*q) {p++; q++; }else break; }return *p-*q;}void main() {char str1[81], str2[81]; int c;printf(请输入第1个字符串:); gets(str1);printf(请输入第2个字符串:); gets(str2);c=cmpstr(str1,str2); if(c0)printf(字符串%s 大于字符串%s\n,str1,str2); else if(c0)printf(字符串%s 小于字符串%s\n,str1,str2); elseprintf(字符串%s 等于字符串%s\n, str1, str2);}2.定义一个函数float reverse(int p[], int n),该函数有两个参数,第一个参数p为形参数组名,第二个参数n为该数组中的元素个数,要求使用reverse()函数将该数组中的所有元素逆序排列,并返回该数组中所有元素的平均值。
c++课后题答案--钱能第二版
1-1<>;(){<<"I'm a "<<; }1-2<>;;(){(1<=4){(1<=4){<<" ";}(1<=2*1){<<"*";}<<<<;}(1<=3){(1<){<<" ";}(1<=(7-2*i)){<<"*";}<<<<;}}2-13<>;;(){(1<=9)<<i<<"\t";(1<=9)<<"-"<<"\t";<<<<;(1<=9) 下三角形式{(1<){<<i*j<<"\t";}<<;}<<<<;(1<=9) 三角形式<<"\t"<<i;(0<9)<<"\t"<<"-";<<<<;(1<=9){(1<)<<"\t";(<=9){<<i*j<<"\t";}<<;}}3-3<><><>;(){a[11]={6,3,7,1,4,8,2,9,11,5};<> (10);0;0;;s;( 0<()){[i];}();( 0<()){([i])*([i]);}(());<<""<<s<<; }4-6<><><>;()0;("D:\\");( ();)( (s)>>a;){((30)(50)(70))<<a<<"能同时被3 5 7整除"<<;((30)(5)(7))<<a<<"能被3整除不能被5整除也不能被7整除"<<;((3)(50)(7))<<a<<"能被5整除不能被3整除也不能被7整除"<<;((3)(5)(70))<<a<<"能被7整除不能被3整除也不能被5整除"<<;((3)(5)(7))<<a<<"不能被3 5 7 任意一个整除"<<;}}4-8;( );n;() 十进制数转换为二进制数{<<"输入要转换的十进制整数:"<<;;>>;();}( n){(n<2)<<n;(n>=2){(2);<<2;}}<><><><><>;<<> > ;<> (){<>a;("D:\\");( ();){c;( (s)>>c;)(c);}(())<<" !"<<;<<"从磁盘中读取数据输出结果是:"<<;( 0<())<<a[i]<<" "; 输出读取的数据<<<<;a;}( a){0;( 10)(10)*(10);;}( 1 2){(1)<(2);}(<>){<<"请输入你想插入的数:"<<;;>>;();(()());<<<<"插入数之后,再按各位数字平方之和的大小输出:"<<;( 0<())<<[i]<<" ";<<;}(){<>v0();(v0()0());<<<<"按各位数字平方之和的大小输出:"<<;( 0<v0())<<v0[i]<<" ";<<;(v0);8-1<><>;{:; y 轴分量:( )设置坐标{;;}()取y轴坐标分量{x;}()取x轴坐标分量{y;}()取点的极坐标@{(180/3.14159)*2(); }()取点的极坐标半径{(x**y);}:: (){5;6;<<""<<x<<<<""<<y<<; }};(){p;;<<" x y:"<<;>>x>>y;();<<""<<()<<<<""<<<<<<"x "<<()<<<<"y "<<()<<;();}8-3<><>;{:;:( y);();();};( y){;;;}(){1;<<"加一天之后日期是:"<<<<;}(){<<('0');<<(2)<<<<'/'<<(2)<<<<'/'<<(4)<<<<;<<(' ');}(){d12;d1(10,4,2012);d1();d2(10,4,2012);d2();d2();}8-4/**多文件结构*/""( s){;;;}(){('0');<<(2)<<<<':'<<(2)<<<<':'<<(2)<<<<;}""主函数(){t;(14,47,39);();}9-1<>;{;:();();();();};(){<<" .\n";}( m)(m){<<" a :"<<<<; }(){<<" s :"<<<<;}(){<<".\n";}(){1;2(20);1();2();}9-3<>;{*;:()( (5)){}(){ ;}() { *;}( ){*;}}(){;<<"'s :"<<()<<;<<" 6……\n";(6);<<" \n";();<<"'s :"<<()<<;<<"'s : "<<()<<;<<" 7……\n";(7);<<"'s : "<<()<<;<<"'s : "<<()<<; }。
钱能著清华版visuallc++书本答案
y=x*x;
else if(x>=0&&x<1)
y=2.5*x-1;
else
y=3*x+1;
cout<<"x="<<x<<"\ty="<<y<<endl;
}
//2.设计一个程序,要求:对从键盘输入的一个不多于5位的正整数,能输出它的位数并输出它的各位数字之和。
第十四章
1答案
Base
Derived
2.答案:
2213
3.答案:
48//10
4.答案:
50,101
20,102
5. 答案:
A::disp n=6
第十五章
1答案
****123.45
123.45**
123.45
2.答案:
67890
abcdefghij
3.答案:
2 1
3 202
count=3
2.答案:
Result1=200
Result2=201Result3=125
3.答案:97
4.答案:1,2
第十二章
1.答案:
0 2 4 6 8
2.答案:
n=7
n=7
3.答案:
(1)2357 (2)10 10 (3)321 (4)first:3 3//second:5 15//third:18 15
(5)math.h fabs(term) (-1)*x*x/(2*n)/(2*n-1) (6)double p p=p*m i=1,f=0 return f
C++程序设计(第二版)钱能-第8章--类
第8章类8.9 练习8(Exercises 8)1.下面程序错在哪里?//=====================================//e0801.cpp//找错//=====================================#include <cmath>using namespace std;//-------------------------------------class Point{protected:double x; //x轴分量double y; //y轴分量public:void Set(double ix, double iy){ //设置坐标 x = ix; y = iy;}//--------------------------------double xOffset(){ //取x轴坐标分量return x;}//--------------------------------double yOffset(){ //取y轴坐标分量return y;}//--------------------------------double angle(){ //取点的极坐标0return (180/3.14159)*atan2(y,x);}//--------------------------------double radius(){ //取点的极坐标半径return sqrt(x*x+y*y);}};//-----------------------------------int main(){Point p;double x,y;cout << "Enter x and y:\n";cin >> x >> y;p.Set(x,y);p.x+=5;p.y+=6;cout << "angle=" << p.angle()<< ",radius=" << p.radius()<< ", x offset=" << p.xOffset()<< ", y offset=" << p.yOffset() << endl;}//=====================================解答:程序缺少头文件iostream,类Point定义结束时,最后应该加上半角分号表示语句的结束。
C语言程序设计教程(第2版)课后习题答案(完美版)
C语言程序设计教程(第二版)课后习题答案第一章C语言程序设计概述-习题答案1算法的描述有哪些基本方法?答1、自然语言2、专用工具2 C语言程序的基本结构是怎样的?举一个例子说明。
答1、C语言程序由函数构成;2、“/*”与“*/”之间的内容构成C语言程序的注释部分;3、用预处理命令#include、#define可以包含有关文件或预定义信息;4、大小写字母在C语言中是有区别的;5、除main()函数和标准库函数外,用户也可以自己编写函数,应用程序一般由多个函数组成,这些函数指定实际所需要做的工作。
3 C语言有什么特点?答1、具有结构语言的特点,程序之间很容易实现段的共享;2、主要结构成分为函数,函数可以在程序中被定义完成独立的任务,独立地编译代码,以实现程序的模块化;3、运算符丰富,包含的范围很广;4、数据类型丰富;5、允许直接访问物理地址,即可直接对硬件进行损伤,实现汇编语言的大部分功能;6、限制不太严格,程序设计自由度大,这样使C语言能够减少对程序员的束缚;7、生成的目标代码质量,程序执行效率高,同时C语言编写的程序的可移植性好。
4★指出合法与不合法的标识符命名。
答AB12--√leed_3-- a*b2--× 8stu--× D.K.Jon--× EF3_3--√ PAS--√ if--× XYZ43K2 --√ AVE#XY--× _762--√ #_DT5--× C.D--×5说明下列Turbo C热键的功能。
答F2:源文件存盘 F10:调用主菜单 F4:程序运行到光标所在行(用于调试程序) Ctrl+F9:编译并链接成可执行文件 Alt+F5:将窗口切换到 DOS 下,查看程序运行结果。
6说明下列Turbo C方式下输入并运行下列程序,记录下运行结果。
①main(){printf("********************\n");printf(" welcome you \n");printf(" very good \n);printf("********************\n");}②main()printf("please input three numbers;");scanf("%d,%d,%d",&a,&b,&c); /*教材S是错误的*/t=max(max(a,b),c);printf("max number is:%d\n",t);}int max(int x, int y){ int z;if(x>y)z=x;else z=y;return(z);}答运行结果:********************welcome youvery good********************运行结果:please input three numbers;3,1,4 /*左侧下划线内容为键盘输入*/max number is:47一个C程序是由若干个函数构成的,其中有且只能有一个___函数。
《C 程序设计教程》课后习题答案
第1章C++及其开发环境概述一、问答题1.结构化程序设计主要有哪些劣势?【答案】(1)程序的开发效率低下(2)程序的可重用性差(3)程序的维护成本高(4)无法适应信息的快速膨胀和多样化2.面向对象的程序设计有哪些优点?【答案】(1)程序的开发效率高(2)程序代码的可重用性高(3)程序易于维护3.C++的发展经历了哪些阶段?【答案】C++语言起源于C语言。
1980年初期,美国贝尔实验室的Bjarne Stroustrup博士及其同事在C语言的基础上,从Simula67中引入面向对象的特征,开发出一种过程性与对象性相结合的程序设计语言,最初称为“带类的C”,1983年正式取名为C++。
后来,Stroustrup和他的同事陆续将运算符重载、继承、虚函数等特性引入C++,1989年后推出AT&T C++ 2.0版,并经过多次改进和完善,发展成为现在的C++。
4.C++主要有哪些特点?【答案】(1)C++可以被看成C语言的一个超集,是对C的增强和扩展。
因此,许多C代码可以不经修改即可为C++所用。
自然,也可以利用C++开发环境来开发C程序。
(2)C++几乎支持所有面向对象的程序设计特征,因此,利用C++不仅可以进行传统的结构化程序设计,而且可以较好地进行面向对象的程序开发。
(3)C++很好地保持了C的简洁、高效、灵活和接近汇编语言的特点,因此,C++程序的执行效率也很高。
5.一个C++程序一般由哪几部分构成?【答案】①预处理命令。
C++提供了三种预处理命令,即宏定义命令、文件包含命令和条件编译命令。
②全局声明部分。
此部分内容通常会给出用户自定义的数据类型声明,以及程序中所涉及变量的定义,例如类的声明。
数据结构③函数。
一个程序中可包含一个或多个函数,它们是执行用户指定操作的主要部分。
6.使用Visual C++ 开发C++程序时一般要经过哪几个步骤?每个步骤的作用是什么?相应地产生哪些文件?【答案】(1)编写程序C++是一种高级语言,所以用其编写的程序称为“源程序”,以.cpp做为后缀。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
c 程序设计教程钱能课后答案【篇一:c 语言程序设计教程课后习题参考答案】t>课后习题参考答案习题11. (1)编译、链接.exe(2)函数主函数(或main 函数)(3)编辑编译链接2.(1)-(5):ddbbc (6)-(10):abbbc 3.(1)答:c 语言简洁、紧凑,使用方便、灵活; c 语言是高级语言,同时具备了低级语言的特征; c 语言是结构化程序设计语言,具有结构化的程序控制语句; c 语言有各种各样的数据类型; c 语言可移植性好;生成目标代码质量高,程序执行效率高。
(2)编辑、编译、链接、执行(3)一个 c 程序由一或多个函数组成,一函数若干条语句构成,每条语句的末尾必须以分号结束。
(4)标识符,关键字,运算符,分隔符,常量,注释符等4. 从键盘输入一个双精度小数,打印出它的余弦值。
#include stdio.h#include math.h main( ){double x;scanf( “%lf ”, x);printf( “%n l f”, cos(x) );}第2 章1.(1)bde 、acfg(2)d (3) c (4) c2.(1)错(2)错(3)错(4)对(5)错3.(1)a=3,b=-27(2)a=11,b=6,c=6(3)3(4)1 0 1 0 1 1 0(5)-9 9 8(6)1)20 2 )8 3 )70 4 )0 5 )0 6 )0 4. (1)#include stdio.h main( ){double r, h ,v;r = 2.5; h =3.5;v = 3.14*r*r*h;printf( “v=%nl f”, v); }(2)#include stdio.h main( ){char ch;ch = getchar( );printf( “%n c”, ch + 32); }(3)#include stdio.h main( ){printf( “n*”); printf( “*n**”);printf( “***n**”);printf( “******n*”);}(4)#include stdio.h main( ){double x;scanf( “%lf ”, x);printf( “%d , %l n f”, (int)x, x (int)–x );(5)#include stdio.h main( ){double a=3, b=5;double result = (-2 * a + ( 4*a –b )/( 2*a + b ) )/( (a - 4*b)/(a + b) ); printf( “%n l”f , result); }习题31.(1)d(2)ad(3)c(4)b(5)a (6)-(10):bdacb2.(1)3.141593,3.1416,3.142 (2)c=k(3)| 123.46|,|123 |(4)x=1.23,y= 50.00 (5)03.(1)scanf(%f ”, c);改为:scanf( “%f”, c);f = (9/5)*c+32; 改为:f = (9.0/5)*c + 32;printf( 摄“氏温度%f 度相当于华氏温度%f 度”, c, f);改为:printf( 摄“氏温度%f 度相当于华氏温度%f 度”, c, f);(2)补充定义:int h;h = 500/60 改为: h = 500 / 60;m = 500% 60 改为:m = 500%60;printf( “50分0钟是%d 小时%d 分钟, ”h, m)改; 为:printf( “50分0钟是%d 小时%d 分钟”, h, m);4.(1)#includestdio.hmain( ) {char x,y;scanf( “%c%”c, x, y);printf( “%n d”, (x-?0?) + (y-?0?) );(2)#include stdio.hmain( ){char x, y;char tmp;printf( “input two characters: ”);scanf( “%c%”c, x, y);printf( “before swap: x=%c, y=%c n”, x, y);tmp = x;x = y;y = tmp; printf( “after swap: x=%c, y =n%”c, x, y); } (3)#include stdio.h main( ){char ch;ch = getchar( );printf( “%n c”, ch- 32); }第4 章1.(1)-(5):caaca2. (1)bbb(2)aaabbbccc(3)end (4)d=20(5)s=2,t=3(6)first third(7)y=0 y=5 y=10 y=5 3.(1)yz xz xy (2)ch=?a?ch=?z?ch=?a?ch=?z? ch = ch-32(3)x2x=10x-1x=2(4)t=x; x=y; y=t;4.(1)main( ) {int x, y , z, t;scanf( “%d%d%”d, x, y, z); if ( xy ) { t=x; x=y;y=t; }if( x z )【篇二:钱能c++ 程序设计教程(第二版)第三章答案[1] 】p> //3.3.2 —浮点数表示#includeiostream usingnamespace std;int main() {}//3.4.2 —字符操作函数相关#includeiostream usingnamespace std;int main() {} char* s1=hello; char* s2=123; char a[20]; strcpy(a,s1);cout(strcmp(a,s1)==0?:not )equal.endl; coutstrcat(a,s2)endl;coutstrrev(a)endl; coutstrset(a,c)endl;cout(strstr(a,ell)?:not )find.endl; cout(strchr(a,c)?:not )findendl;float f=19.2f; coutfendl; unsigned int* p=reinterpret_castunsigned int*(f); for(int i=31;i=0;i--)cout(*pi1)(i==31||i==23?-:); coutendl;//3.4.3string 函数相关#includeiostream #includestringusing namespace std; int main(){string a,s1=hello,s2=123; a=s1;} cout(a==s1?:not )equal.endl; couta+s2endl; reverse(a.begin(),a.end()); coutaendl; couta.replace (0,9,9,c)endl;cout(a.find(ell)?:not )findendl; cout(a.find(c)?:not )findendl;//3.4.5string 流+文件流#includeiostream#includesstreamusing namespace std; int main(){} ifstream fin(original.txt); for(string s;getline(fin,s);) { } int a,sum=0; for(istringstream sin(s);sina;sum+=a); coutsumendl; //3.5.2--sizeof()#includeiostream usingnamespace std;int main() {} int a[]={1,15,8,3,8,48,2,64,5}; for(inti=0;isizeof(a)/sizeof(a[0]);i++) cout a[i]; coutendl;//3.5.3-- 初始化#includeiostream usingnamespace std;int array1[5]={1,2,3};int array2[5]; intmain(){}//3.5.4 —二维数组#includeiostreamusing namespace std; int main(){int a[2][3]={1,2,3,4,5}; int b[2][3]={{1,2},4}; coutarray1:endl; for(int i=0;isizeof(a)/sizeof(a[0]);i++) { }cout=================================endl; coutarray2:endl; for(int i=0;isizeof(b)/sizeof(b[0]);i++) {cout ; for(int j=0;jsizeof(b[0])/sizeof(b[0][0]);j++) cout ; for(intj=0;jsizeof(a[0])/sizeof(a[0][0]);j++) couta[i][j] ; coutendl; int array3[5]={2}; int array4[5]; coutarray1:; for(inti=0;isizeof(array1)/sizeof(array1[0]);i++) coutarray1[i] ; coutendl; coutarray2:; for(inti=0;isizeof(array2)/sizeof(array2[0]);i++) coutarray2[i] ; coutendl; coutarray3:; for(inti=0;isizeof(array3)/sizeof(array3[0]);i++) coutarray3[i] ; coutendl; coutarray4:; for(int。