《C++程序设计教程(修订版)——设计思想与实现习题解答钱能(共80页).docx》由会员分享,可在线阅读,更多相关《C++程序设计教程(修订版)——设计思想与实现习题解答钱能(共80页).docx(80页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上二 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.; void main() double radius1, radius2; cout radius1 radius2; cout setw(10) pi setw(10) radius1 s
2、etw(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.; 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 m
3、any 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 sh
4、ort 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
5、(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; /输入时以空格作为数据间隔
6、 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
7、 #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
8、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
9、 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 a
10、2=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: c
11、out 只能被 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 =9
12、0) 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
13、)*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(i
14、nt 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(
15、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 #in
16、clude 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+
17、) 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 #inc
18、lude #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=
19、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=sqr
20、t(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) s
21、etw(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() mul
22、tab1(); 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+) c
23、out 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
24、=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.
25、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. 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 m
26、ain() 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); 六
27、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 doubl
28、e 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
29、) 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;
30、 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; f