钱能C++教材习题答案:第2章---第21章
所有代码在VC++6.0下调试通过
第二章
2.1:
# include <math.h>
# include <iostream.h>
# include <iomanip.h>
# include <stdio.h>
void main(){
int a1=0;/*0*/
int b1='0';/*48*/
int c1='\0';/*0*/
char d1=0;/*空白*/
char e1='0';/*单个字符0*/
char f1='\0';/*空白*/
cout <<a1<<"\n"<<b1<<"\n"<<c1<<"\n"<<d1<<"\n"<<e1<<"\n"<<f1<<"\n";
cout<<"int:"<<sizeof(int)/*4*/<<"\n"<<"float:"<<sizeof(float)/*4*/<<"\n";
cout<<"double:"<<sizeof(double)/*8*/<<"\n";
enum COLOR{RED,BLUE=200,GREEN,WHITE};
COLOR paint=GREEN;
cout <<paint<<"\n";//201
const double pi=3.141596;/*3.1416*/
cout<<pi<<"\n";
cout<<sizeof(unsigned int)<<"\n";//4
cout<<sizeof(int)<<"\n";//4
int c=42486;
cout<<dec<<c<<"\n"//42486
<<oct<<c<<"\n"//122766
<<hex<<c<<"\n";//a5f6
unsigned int a=4294967295;
int b=-2;
//cout<<dec<<b<<"\n";
printf("a=%d,%u\n",a,a);//-1,4294967295
printf("b=%d,%u\n",b,b);//-2,4294967294
}
2.2:
# include <math.h>
# include <iostream.h>
# include <iomanip.h>
# include <stdio.h>
void main(){
const double pi=3.1415926;
long double a,b,area1,area2;
cout<<"float:"<<sizeof(float)<<"\n";//4
cout<<"double:"<<sizeof(double)<<"\n";//8
cout<<"long double:"<<sizeof(long double)<<"\n";//8
cin>>a>>b;
area1=pi*a*a;
area2=pi*b*b;
cout<<sizeof(area1)<<"\n";//8
cout<<sizeof(area2)<<"\n";//8
cout<<setw(13)<<pi<<setw(13)<<a<<setw(13)<<area1<<"\n";// 3.14159 1 3.14159
cout<<setw(13)<<pi<<setw(13)<<b<<setw(13)<<area2<<"\n";// 3.14159 2 12.5664
}
2.3:
# include <math.h>
# include <iostream.h>
# include <iomanip.h>
# include <stdio.h>
void main(){
const long double e=27182.81828;
cout<<setprecision(10)<<e<<"\n";//27182.81828
cout<<setprecision(8)<<setiosflags(ios::fixed)<<e<<"\n";//27182.81828000
cout<<setprecision(8)<<setiosflags(ios::scientific)<<e<<"\n";//27182.818
cout<<setprecision(9)<<setiosflags(ios::scientific)<<e<<"\n";//27182.8183
cout<<setprecision(10)<<setiosflags(ios::scientific)<<e<<"\n";//27182.81828
cout<<setprecision(11)<<setiosflags(ios::scientific)<<e<<"\n";//27182.81828
cout<<setprecision(6);
}
2.4:
# include <math.h>
# include <iostream.h>
# include <iomanip.h>
# include <stdio.h>
void main(){
const int a=500;
cout<<"\""<<"how many students here?"<<"\""<<"\n";
cout<<"\""<<a<<"\""<<"\n";
}
2.5:
# include <math.h>
# include <iostream.h>
# include <iomanip.h>
# include <stdio.h>
void main(){
cout<<setw(40)<<setiosflags(ios::left)<<"size of char"
<<setiosflags(ios::right)<<sizeof(char)<<" byte"<<"\n"; //1
cout<<setw(40)<<setiosflags(ios::left)<<"size of unsigned char"
<<setiosflags(ios::right)<<sizeof(unsigned char)<<" byte"<<"\n"; //1
cout<<setw(40)<<setiosflags(ios::left)<<"size of signed char"
<<setiosflags(ios::right)<<sizeof(signed char)<<" byte"<<"\n"; //1
cout<<setw(40)<<setiosflags(ios::left)<<"size of int"
<<setiosflags(ios::right)<<sizeof(int)<<" byte"<<"\n"; //4
cout<<setw(40)<<setiosflags(ios::left)<<"size of unsigned int"
<<setiosflags(ios::right)<<sizeof(unsigned int)<<" byte"<<"\n"; //4
cout<<setw(40)<<setiosflags(ios::left)<<"size of signed int"
<<setiosflags(ios::right)<<sizeof(signed int)<<" byte"<<"\n"; //4
cout<<setw(40)<<setiosflags(ios::left)<<"size of short int"
<<setiosflags(ios::right)<<sizeof(short int)<<" byte"<<"\n"; //2
cout<<setw(40)<<setiosflags(ios::left)<<"size of unsigned short int"
<<setiosflags(ios::right)<<sizeof(signed short int)<<" byte"<<"\n"; //2
cout<<setw(40)<<setiosflags(ios::left)<<"size of unsigned short int"
<<setiosflags(ios::right)<<sizeof(signed short int)<<" byte"<<"\n"; //2
cout<<setw(40)<<setiosflags(ios::left)<<"size of long int"
<<setiosflags(ios::right)<<sizeof(long int)<<" byte"<<"\n"; //4
cout<<setw(40)<<setiosflags(ios::left)<<"size of unsigned long int"
<<setiosflags(ios::right)<<sizeof(signed long int)<<" byte"<<"\n"; //4
cout<<setw(40)<<setiosflags(ios::left)<<"size of unsigned long int"
<<setiosflags(ios::right)<<sizeof(signed long int)<<" byte"<<"\n"; //4
cout<<setw(40)<<setiosflags(ios::left)<<"size of float"
<<setiosflags(ios::right)<<sizeof(float)<<" byte"<<"\n"; //4
cout<<setw(40)<<setiosflags(ios::left)<<"size of double"
<<setiosflags(ios::right)<<sizeof(double)<<" byte"<<"\n"; //8
cout<<setw(40)<<setiosflags(ios::left)<<"size of long double"
<<setiosflags(ios::right)<<sizeof(long double)<<" byte"<<"\n"; //8
}
2.6:
# include <math.h>//数学函数
# include <iostream.h>//cout\cin
# include <iomanip.h>//cout和cin的控制符
# include <stdio.h>//printf、scanf的库
void main(){
float a,b,c,s;
double area;//定义5个浮点数
printf("please input 3 side of one triangle:\n";
scanf("%f,%f,%f",&a,&b,&c);//输入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.2f\n",a,b,c);//6.00,6.00,8.00
printf("area of trangle is %10.5f",area);//17.88854
}
用cout和cin改写为:
# include <math.h>//数学函数
# include <iostream.h>//cout\cin
# include <iomanip.h>//cout和cin的控制符
# include <stdio.h>//printf、scanf的库
double darea(float,float,float);
void main(){
float a,b,c;
double area;
cout<<"please input 3 side of one triangle:\n";
cin>>a>>b>>c;
area=darea(a,b,c);
cout<<setw(7)<<setiosflags(ios::fixed)<<setprecision(2)<<"a="<<a;
cout<<setw(7)<<setiosflags(ios::fixed)<<setprecision(2)<<"b="<<b;
cout<<setw(7)<<setiosflags(ios::fixed)<<setprecision(2)<<"c="<<c<<"\n";
cout<<"area of trangle is"<<setw(10)<<setiosflags(ios::fixed)<<setprecision(5)<<area<<"\n";
}
double darea(float x,float y,float z)
{
float s;
double areas;
s=(x+y+z)/2;
areas=sqrt(s*(s-x)*(s-y)*(s-z));
return areas;
}
改写为:
# include <math.h>//数学函数
# include <iostream.h>//cout\cin
# include <iomanip.h>//cout和cin的控制符
# include <stdio.h>//printf、scanf的库
double darea(float,float,float);
void main(){
float a,b,c;
double area;
printf("please input 3 side of one triangle:\n";
scanf("%f,%f,%f",&a,&b,&c);
area=darea(a,b,c);
printf("a=%7.2f,b=%7.2f,c=%7.2f\n",a,b,c);
printf("area of trangle is %10.5f",area);
}
double darea(float x,float y,float z)
{
float s;
double areas;
s=(x+y+z)/2;
areas=sqrt(s*(s-x)*(s-y)*(s-z));
return areas;
}
2.7:
In main():
Enter two numbers:
_ _
Calling add():
In add(),received _ and _
and return _
Back in main():
c was set to _
Enter...
2.8:
# include <math.h>//数学函数
# include <iostream.h>//cout\cin
# include <iomanip.h>//cout和cin的控制符
# include <stdio.h>//printf、scanf的库
double vers(float,float);
const double pi=3.1415926;
void main(){
float a,b;
double ver;
cout<<"please input banjing and gao of one yuanzhu:\n";
cin>>a>>b;
ver=vers(a,b);
cout<<setw(20)<<setiosflags(ios::fixed)<<setprecision(8)<<"a="<<a;
cout<<setw(20)<<setiosflags(ios::fixed)<<setprecision(8)<<"b="<<b<<"\n";
cout<<"ver of yuanzhu is"<<setw(30)<<setiosflags(ios::fixed)<<setprecision(8)<<ver<<"\n";
}
double vers(float x,float y)
{
double ver;
ver=pi*x*x*y;
return ver;
}
3.1:
sqrt(pow(sin(x),2.5));
1/2*(a1*x+(a1+x)/4*a1);
pow(c1,pow(x,2))/sqrt(2*pi);
3.2:
13.70;
2.5;
9;
3.3:
(1)
a1=1 a2=1
(2)
1,1
(3)
2,0,0
(4)
20
3.4:
# include <math.h>
# include <iostream.h>
# include <iomanip.h>
# include <stdio.h>
void main(){
int x,y;
cout<<"please a int:"<<endl;
cin>>x;
if (x<=10 && x>2)/*注意括号的使用*/
y=x*(x+2);
if (x>-1 && x<=2)
y=2*x;
if (x<=-1)
y=x-1;
cout<<"x:"<<x<<"y:"<<y<<"\n";
}
3.5:
# include <math.h>
# include <iostream.h>
# include <iomanip.h>
# include <stdio.h>
void main(){
int x;
cout<<"please a int:"<<endl;
cin>>x;
if (x % 3==0) /*注意“==”和“=”的不同*/
if (x % 5==0)
if (x % 7==0)
cout<<"x may div by 3,5,7"<<endl;
else
cout<<"x may div by 3,5"<<endl;
else
if (x%7==0)
cout<<"x may div by 3,7"<<endl;
else
cout<<"x may div by 3"<<endl;
else
if (x%5==0)
if (x%7==0)
cout<<"x may div by 5,7"<<endl;
else
cout<<"x may div by 5"<<endl;
else
if (x%7==0)
cout<<"x may div by 7"<<endl;
else
cout<<"x can not div by 3,5,7"<<endl;
}
3.6:
# include <math.h>
# include <iostream.h>
# include <iomanip.h>
# include <stdio.h>
void main(){
int x;
cout<<"please a int:"<<endl;
cin>>x;
if (x>100 || x<0 )
cout<<"inputed x is wrong"<<endl;
if (x>=90 && x<=100)
cout<<"A"<<endl;
if (x<90 && x>=80)
cout<<"B"<<endl;
if (x<80 && x>=70)
cout<<"C"<<endl;
if (x<70 && x>=60)
cout<<"D"<<endl;
if (x<60 && x>0)
cout<<"E"<<endl;
}