钱能C++教材习题答案四

   /2005-05-08

 

//************************
//*        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;
}


相关话题/

  • 领限时大额优惠券,享本站正版考研考试资料!
    大额优惠券
    优惠券领取后72小时内有效,10万种最新考研考试考证类电子打印资料任你选。涵盖全国500余所院校考研专业课、200多种职业资格考试、1100多种经典教材,产品类型包含电子书、题库、全套资料以及视频,无论您是考研复习、考证刷题,还是考前冲刺等,不同类型的产品可满足您学习上的不同需求。 ...
    本站小编 Free壹佰分学习网 2022-09-19