C++程序设计教程(修订版)——设计思想与实现习题解答钱能(共80页).docx
精选优质文档-倾情为你奉上二 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.; void main() double radius1, radius2; cout <<"please input two numbers: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> 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 <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) <<" 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 <stdio.h> #include <iostream.h> #include <iomanip.h> #include <math.h> void main() float a,b,c,s,area; /printf("please input 3 sides of one triangle:n"); cout <<"please input 3 sides of one triangle:n" /scanf("%f,%f,%f",&a,&b,&c); /输入时以逗号作为数据间隔 cin >>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 <iostream.h> #include <iomanip.h> #include <math.h> float area(float a, float b, float c); /函数声明 void main() float a,b,c; cout <<"please input 3 sides of one triangle:n" cin >>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 <iostream.h> #include <math.h> double Cylinder(double r, double h); void main() 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; 三 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 <iostream.h> void main() int x; cout <<"please input x:n" cin >>x; if(x<=-1) cout <<(x-1) <<endl; if(x>-1 && x<=2) cout <<2*x <<endl; if(2<x && x<=10) cout <<x*(x+2); 3.5 #include <iostream.h> void main() int a; cout <<"please input a number:n" cin >>a; int c1 = a%3 =0; int c2 = a%5 =0; int c3 = a%7 =0; switch(c1<<2)+(c2<<1)+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 <iostream.h> 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" else if(grade>=80) cout <<"B.n" else if(grade>=70) cout <<"C.n" else if(grade>=60) cout <<"D.n" else cout <<"E.n" 四 4.1 1 #include <iostream.h> #include <math.h> 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 <iostream.h> #include <math.h> 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 <iostream.h> #include <math.h> 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; 4.2 #include <iostream.h> 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 <iostream.h> 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 <iostream.h> 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" 4.5 #include <iostream.h> void main() float s=100,h=100; for(int i=1; i<10; i+) s+=h; h/=2; cout <<"共经过" <<s <<"米第 10 次反弹" <<h <<"米高.n" 4.6 #include <iostream.h> void main() int peachs=1; for(int i=1; i<10; i+) peachs=(peachs+1)*2; cout <<"第一天共摘下" <<peachs <<"个桃子.n" 4.7 #include <iostream.h> #include <math.h> void main() double x, a; cout <<"please input a value:n" cin >>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 <iostream.h> 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 <iostream.h> 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 <iostream.h> #include <iomanip.h> 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 <iostream.h> #include <iomanip.h> 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 <iostream.h> #include <iomanip.h> 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 <iostream.h> 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; 五 5.1 #include <iostream.h> #include <iomanip.h> #include <math.h> bool isprime(long n); void main() /input long a,b,l=0; cout <<"please input two numbers:n" cin >>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 <iostream.h> #include <iomanip.h> #include <math.h> 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) <<endl; double f(double x) return exp(x)/(1+x*x); double integral(double a, double b) int n=1; double h,tn,t2n,i2n,in=0; h = b-a; t2n = i2n = h*(f(a)+f(b)/2; while(fabs(i2n-in)>=eps) tn = t2n; in = i2n; double sigma = 0.0; for(int k=0; k<n; 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 <iostream.h> #include <iomanip.h> 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 <iostream.h>void main() int n; long a=1, b=1, temp; cout <<"please input a number:n" cin >>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 <iostream.h> #include <math.h> 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 <iostream.h> 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 <iostream.h> long cattle(int n); void main() int n; cout <<"please input a number:n" cin >>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 <iostream.h> #include <iomanip.h> void multab1(); void multab2(); void multab3(); 七 7.1 #include <iostream.h> 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