//************************
//* 5_7.chh *
//* 0.739085 *
//************************
# include <iostream.h>
# include <math.h>
double f();
void main()
{
double m;
m=f();
cout<<"f(1e-6)[cos(x)=x]:="<<m<<endl;
}
double f()
{
double f1=3.14159/4,f0=0;
const double exp=1e-6;
while (fabs(f1-f0)>exp)
{
f0=f1;
f1=f0+(cos(f0)-f0)/(sin(f0)+1);
}
return f1;
}
//************************
//* 5_8.chh *
//* *
//************************
# include <iostream.h>
# include <math.h>
void display(double);
void display(int);
void display(char);
void main()
{
double m=2.0;
display(m);
float n=2.0;
display(n);/*转换为double型*/
int o=2;
display(o);
char p='a';
display(p);
short q=2;
display(q);/*转换为int型*/
}
void display(double a)
{
cout<<"A double:"<<a<<endl;
}
void display(int b)
{
cout<<"A int:"<<b<<endl;
}
void display(char c)
{
cout<<"A char:"<<c<<endl;
}
//************************
//* 5_9.chh *
//* *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
int total(int a);
void main()
{
int n,m;
cout<<"please input years:"<<endl;
cin>>n;
m=total(n);
cout<<"the total of cows:"<<m<<endl;
}
int total(int a)
{
if (a<4)
return 1;
else
return 2*total(a-4);
}
第六章
6.1
(1)
files2.cpp中的int fun();应为extern int fun()
files3.cpp中的extern int x=2;应为extern int x;
files3.cpp中的int g();应为extern g();
(2)
files2.cpp中y的声明类型和files1.cpp中的不一致
files1.cpp和files2.cpp中的extern int z;必须有一个为定义,即为int z;
6.2
输出结果为:25
6.3
//myhead.h
//all.cpp
//up.cpp
//down.cpp
//main.cpp
//************************
//* myhead.h *
//* *
//************************
# include <iostream.h>
# include <math.h>
# include <iomanip.h>
void all();
void down();
void up();
//************************
//* all.cpp *
//* *
//************************
# include "myhead.h"
void all()
{
cout<<setw(6)<<setiosflags(ios::right)<<"*";
int i;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
}
cout<<endl;
for (i=1;i<11;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<".......";
}
cout<<endl;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
int j;
for (j=1;j<10;j++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i*j;
}
cout<<endl;
}
return;
}
//************************
//* down.cpp *
//* *
//************************
# include "myhead.h"
void down()
{
cout<<setw(6)<<setiosflags(ios::right)<<"*";
int i;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
}
cout<<endl;
for (i=1;i<11;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<".......";
}
cout<<endl;
for (i=1;i<10;i++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i;
int j;
for (j=1;j<i+1;j++)
{
cout<<setw(6)<<setiosflags(ios::right)<<i*j;
}
cout<<endl;
}
return;
}