C++程序设计教程(修订版)——设计思想与实现习题解答钱能.docx_第1页
C++程序设计教程(修订版)——设计思想与实现习题解答钱能.docx_第2页
C++程序设计教程(修订版)——设计思想与实现习题解答钱能.docx_第3页
C++程序设计教程(修订版)——设计思想与实现习题解答钱能.docx_第4页
C++程序设计教程(修订版)——设计思想与实现习题解答钱能.docx_第5页
已阅读5页,还剩75页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

二 2.1 #include 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 #include const double pi = 3.1415926; void main() double radius1, radius2; cout 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 #include const double e = 2.718281828; void main() cout setprecision(10) e endl setiosflags(ios:fixed) setprecision(8) e endl setiosflags(ios:scientific) e endl; 2.4 #include void main() cout How many students here?n 500n; 2.5 #include void main() cout size of char sizeof(char) byten size of unsigned char sizeof(unsigned char) byten size of signed char sizeof(signed char) byten size of int sizeof(int) byten size of unsigned sizeof(unsigned) byten size of signed sizeof(signed) byten size of short sizeof(short) byten size of unsigned short sizeof(unsigned short) byten size of long sizeof(long) byten size of signed long sizeof(signed long) byten size of unsigned long sizeof(unsigned long) byten size of float sizeof(float) byten size of double sizeof(double) byten size of long double sizeof(long double) byten; 2.6 1) please input 3 sides of one triangle: 6,6,8 a= 6.00,b= 6.00,c= 8.00 area of triangle is 17.88854 2) 该程序计算三角形的面积前后分为三部分输入处理输出。 3) /#include #include #include #include void main() float a,b,c,s,area; /printf(please input 3 sides of one triangle:n); cout a b c; /输入时以空格作为数据间隔 s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c); /printf(a=%7.2f,b=%7.2f,c=%7.2fn,a,b,c); cout setiosflags(ios:fixed) setprecision(2) a= setw(7) a ,b= setw(7) b ,c= setw(7) c endl; /printf(area of triangle is %10.5f,area); cout area of triangle is setw(10) setprecision(5) area endl; 4) #include #include #include float area(float a, float b, float c); /函数声明 void main() float a,b,c; cout a b c; /输入时以空格作为数据间隔 float result = area(a,b,c); /函数调用 cout setiosflags(ios:fixed) setprecision(2) a= setw(7) a ,b= setw(7) b ,c= setw(7) c endl; cout area of triangle is setw(10) setprecision(5) result endl; float area(float a, float b, float c) /函数定义 float s=(a+b+c)/2; return sqrt(s*(s-a)*(s-b)*(s-c); 2.7In main(): Enter two numbers: 3 8 Calling add(): In add(),received 3 and 8 and return 11 Back in main(): c was set to 11 Exiting. 2.8 #include #include double Cylinder(double r, double h); void main() double radius, height; cout radius height; double volume = Cylinder(radius, height); cout 该圆柱体的体积为 volume endl; double Cylinder(double r, double h) return r*r*M_PI*h; 三 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.2 13.7 2.5 9 3.3 (1) a1=1 a2=1 (2) 1.1 (3) 2,0.0 (4) 20 3.4 #include void main() int x; cout x; if(x=-1) cout (x-1) -1 & x=2) cout 2*x endl; if(2x & x=10) cout x*(x+2); 3.5 #include void main() int a; cout a; int c1 = a%3 =0; int c2 = a%5 =0; int c3 = a%7 =0; switch(c12)+(c21)+c3) case 0: cout 不能被 3,5,7 整除.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; 3.6 #include void main() int grade; cout grade; if(grade100|grade0) cout =90) cout =80) cout =70) cout =60) cout D.n; else cout E.n; 四 4.1 1 #include #include void main() double sum=1, t=-1, x; int i=1; cout x; do t*=(-1)*x/i; sum+=t; i+; while(fabs(t)1e-8); cout sum= sumendl; 2 #include #include void main() double sum=1, t=-1, x; cout x; int i=1; while(fabs(t)1e-8) t*=(-1)*x/i; sum+=t; i+; cout sum= sumendl; 3 #include #include void main() double sum=1, t=-1, x; cout x; for(int i=1; fabs(t)1e-8; i+) t*=(-1)*x/i; sum+=t; cout sum= sumendl; 4.2 #include void main() long sum=0, t=1; for(int i=1; i=15; i+) t*=i; sum+=t; cout sum= sum endl; 4.3 #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; 4.4 #include void main() for(int i=1; i1000; i+) int sum=0; for(int j=1; j=i/2; j+) if(i%j=0) sum+=j; if(sum=i) cout i是完数.n; 4.5 #include void main() float s=100,h=100; for(int i=1; i10; i+) s+=h; h/=2; cout 共经过 s 米第 10 次反弹 h 米高.n; 4.6 #include void main() int peachs=1; for(int i=1; i10; i+) peachs=(peachs+1)*2; cout 第一天共摘下 peachs 个桃子.n; 4.7 #include #include void main() double x, a; cout a; x = a/2; while(fabs(x-a/x)/2)1e-7) x=(x+a/x)/2; cout a 的平方根是 x endl; 4.8 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(int j=1; j=i; j+) cout ; for(int j=1; j=18-i; j+) cout #; cout endl; 4.9 1 #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; for(int j=1; j=9; j+) cout setw(4) i*j; cout endl; 2 #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; 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; 4.10 #include void main() int n; long a=1, b=1, c=1, temp; cout n; for(int i=4; i=n; i+) temp=a+c; a=b; b=c; c=temp; cout c endl; 五 5.1 #include #include #include bool isprime(long n); void main() /input long a,b,l=0; cout a b; cout primes from a to b is n; /process if(a%2=0) a+; for(long m=a; m=b; m+=2) if(isprime(m) /output if(l+%10=0) cout endl; cout setw(5) m; bool isprime(long n) int sqrtm=sqrt(n); for(int i=2; i=sqrtm; i+) /判明素数 if(n%i=0) return false; return true; 5.2 #include #include #include double f(double x); double integral(double a, double b); const double eps = 1e-8; void main() double a=0, b=1; cout the integral of f(x) from a to b is n setiosflags(ios:fixed) setprecision(8) setw(8) integral(a,b) =eps) tn = t2n; in = i2n; double sigma = 0.0; for(int k=0; kn; k+) double x = a+(k+0.5)*h; sigma += f(x); t2n = (tn+h*sigma)/2.0; /变步长梯形 i2n = (4*t2n-tn)/3.0; /辛普生公式 n *= 2; h /= 2; return i2n; 5.3 #include #include void multab1(); void multab2(); void multab3(); void main() multab1(); multab2(); multab3(); void multab1() 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; for(int j=1; j=9; j+) cout setw(4) i*j; cout endl; cout endl endl; void multab2() 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; for(int j=1; j=i; j+) cout setw(4) i*j; cout endl; cout endl endl; void multab3() 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; cout endl endl; 5.4 Main-x=5,y=1,n=1 Func-x=6,y=21,n=11 Main-x=5,y=1,n=11 Func-x=8,y=31,n=21 5.5#include void main() int n; long a=1, b=1, temp; cout n; for(int i=3; i=n; i+) temp=a+b; a=b; b=temp; cout b endl; 5.6 double 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; 5.7 #include #include void main() double x, y; x = 3.14159/4; do y = x; /x-=(cos(x)-x)/(sin(x)-1); x = cos(x); while(fabs(x-y)1e-6); cout x endl; /答案为: 0.739085 5.8 #include void display(double d) cout A double: d endl; void display(int i) cout A int: i endl; void display(char c) cout A char: c endl; void main() double a=100.0; float f=1.0; int n=120; char ch=c; short s=50; display(a); display(f); display(n); display(ch); display(s); 5.9 #include long cattle(int n); void main() int n; cout n; cout cattle(n) endl; long cattle(int n) if(n=0) return 0; if(n=3) return 1; return cattle(n-1)+cattle(n-3); 六 6.1(1) /file1.cpp int x=1; int func() /. /file2.cpp extern int x; int func(); void g() x=func(); /file3.cpp extern int x=2; /error: extern int 变量若有赋值则成定义 int g(); /error: 函数声明与前面不一致 void main() x=g(); /. (2) /file1.cpp int x=5; int y=8; extern int z; /file2.cpp int x; /error: int x;重复定义 extern double y; /error: y 同一名字不同类型定义 extern int z; /error: z 只有声明却无定义 6.2 25 6.3 #include multab.h void main() multab1(); multab2(); multab3(); /6_3_1 #include multab.h void multab1() 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; for(int j=1; j=9; j+) cout setw(4) i*j; cout endl; cout endl endl; /6_3_2 #include multab.h void multab2() 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; for(int j=1; j=i; j+) cout setw(4) i*j; cout endl; cout endl endl; /6_3_3 #include multab.h void multab3() 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; cout endl endl; /6_3.h #include #include void multab1(); void multab2(); void multab3(); 七 7.1 #include int findMinIndex(int a, int n); void main() int array=34,91,83,56,29,93,56,12,88,72; int size=sizeof(array)/sizeof(*array); int minIndex = findMinIndex(array, size); cout 最小数: arrayminIndex endl 相应的下标: minIndex endl; int findMinIndex(int a, int n) int index = 0; for(int i=1; iai) index = i; return index; 7.2 #include int insert(int a, int n, int value); void main() int array=12,29,34,56,72,83,88,91; int size=sizeof(array)/sizeof(*array); cout 插入前的数组:n; for(int i=0; isize; i+) cout arrayi ; int aValue; cout aValue; int max=insert(array,size,aValue); cout n 最大数: max endl; cout 插入后的数组:n; for(int i=0; isize; i+) cout arrayi ; int insert(int a, int n, int value) if(an-1=0&aivalue; i-) ai+1=ai; ai+1=value; return retValue; 7.3 #include const int num=17; void main() int interval=3; int anum; for(int i=0; inum; i+) cout (ai=i+1) ,; cout endl; int i=(interval-1)%num; for(int k=1; knum; k+) cout ai ,; ai=0; for(int j=1; !(ai&(j+=interval); i=(i+1)%num); /数数 cout nNo. ai boy has won.n; /输出胜利者 7.4 void Swap(int& a, int& b) int temp=a; a=b; b=temp; void Bsort(int a, int n) bool dontLoopAgain=false; while(dontLoopAgain=!dontLoopAgain) for(int i=0,pass=-n; iai+1) dontLoopAgain=false; Swap(ai,ai+1); 7.5 #include const int n=5; int sum(int a5, int size); void main() int arraynn=3,2,4,1,5, 8,7,2,5,6, 6,9,1,4,3, 5,5,3,6,2, 2,8,1,8,6; cout sum(array,n); int sum(int a5, int size) int s=0; for(int i=0; isize; i+) s+=aii+aisize-i-1; if(size%2=1) s-=asize/2size/2; return s; 7.6 #include void findMax(int a4, int row,int col); void findBad(int a4, int row,int col); void average(int a4, int row,int col); void main() int array54=88,67,48,91, 61,65,37,77, 92,81,73,60, 51,55,60,60, 77,63,70,80; findMax(array,5,4); findBad(array,5,4); average(array,5,4); void findMax(int a4, int row,int col) int r=0,c=0; for(int i=0; irow; i+) for(int j=0; jarc) r=i,c=j; cout 成绩最高的学生序号: (r+1) ; for(int i=0; icol; i+) cout ari ; cout endl; void findBad(int a4, int row,int col) for(int i=0; irow; i+) for(int j=0; jcol; j+) if(aij60) cout 有不及格课程的学生序号: (i+1) ; for(int k=0; kcol; k+) cout aik ; cout endl; break; void average(int a4, int row,int col) double sum=0; for(int i=0; irow; i+) for(int j=0; jcol; j+) sum+=aij; cout 所有课程平均分数为: sum/row/col endl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论