《C++ Primer Plus》编程练习第七章 解析与答案

#include <iostream>
using namespace std;
double average(double x,double y);
int main() 
{
    cout << "Please Input 2 num; 0 terminated\n";
    double x = 0,y = 0;
    double result = 0;
    while (cin>>x>>y)
    {
        if(x*y == 0)
            cout<<"Done\n";
            break;
        result = average(x,y);
        cout << "result:"<<result<<endl;
        
    }
    return 0;
}
double average(double x,double y)
{
    return 2.0*x*y/(x+y);
}
#include <iostream>
using namespace std;
int  Input(double * ar,int Max);
void Show(const double * ar,int Size);
void Averge(const double * ar,int Size);
const int Max = 10;
int main() 
{
    double ar[Max];
    int Size =  Input(ar,Max);
    Show(ar,Size);
    Averge(ar,Size);
    return 0;
}

int Input(double * ar,int Max)
{
    int i ;
    for (i = 0; i < Max; i++)
    {
        cout << "Please input:"<<(i+1)<<endl;
        cin >> *(ar+i);
        if(!cin)
            break;
    }
    cout<<"i = "<<i<<endl;
    return i;
}
void Show(const double * ar,int Size)
{
    for (int i = 0; i < Size;i++)
    {
        cout << i << ":"<< *(ar+i)<<" ";
    }
    cout<<endl;
}
void Averge(const double * ar,int Size)
{
    double sum = 0;
    for (int i = 0; i < Size;i++)
    {
        sum += *(ar+i);   
    }
    cout<<"Averge:"<<sum/Size<<endl;
}

3

#include <iostream>
using namespace std;
struct  box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

void show(box a);
void revolume(box * a);
int main() 
{
    box a = {"Talk is cheap,Show me the Code",20,40,60,0};
    box *b = &a;
    show(a);
    revolume(b);
    show(a);
    return 0;
}

void show(box a)
{
    cout<<"box.marker:"<<a.maker<<endl;
    cout<<"box.height:"<<a.height<<endl;
    cout<<"box.width:"<<a.width<<endl;
    cout<<"box.length:"<<a.length<<endl;
    cout<<"box.volume:"<<a.volume<<endl;
}
void revolume(box * a)
{
    double volume = (a->height)*(a->length)*(a->width);
    a->volume = volume;
}

4

#include <iostream>
using namespace std;
long double probability(unsigned numbers, unsigned picks);
int main() 
{
    long double pro = 0;
    pro = probability(47,5)*probability(27,1);
    cout << pro<<endl;
    return 0;
}

long double probability(unsigned numbers, unsigned picks)
{
    long double results = 1.0;
    long double n;
    unsigned p;
    for (n = numbers, p = picks; p > 0; n--, p--)
        results = results * n/p;
    return results;

}

5

#include <iostream>
using namespace std;

int function(int num);
int main() 
{
    int b;
    b = function(3);
    cout << b<<endl;
    return 0;
}
int function(int num)
{
    if (num > 0)
        {
            return num*function(--num);
        }
    else 
        return 1;
}

6

#include <iostream>
using namespace std;
int Fill_array(double ar[], int Limit);
void Show_array(double ar[], int Size);
void Reverse_array(double ar[], int Size);
const int Max = 10;
int main() 
{
    double ar[Max];
    int Size = Fill_array(ar,Max);
    Show_array(ar,Size);
    Reverse_array(ar,Size);
    Show_array(ar,Size);
    return 0;
}
int Fill_array(double ar[], int Limit)
{
    cout << "Input double"<<endl;
    int i;
    for (i = 0; i < Limit; i++)
    {
        if(!(cin>>ar[i]))
        {
            cout<<"Wrong!\n";
            break;
        }
    }
    return i;
}
void Show_array(double ar[], int Size)
{
    for (int i = 0; i < Size; i++)
    {
        cout << i<<":"<<ar[i]<<" ";
    }
    cout <<endl;
}
void Reverse_array(double ar[], int Size)
{
    double temp;
    int i,j;
    for (i = 0,j = Size -1; i < Size,i < j; i++,j--)
    {
        temp = ar[j];
        ar[j] = ar[i];
        ar[i] = temp;
    }
}

7

#include <iostream>
using namespace std;
double* Fill_array(double * begin, double * end);
void Show_array(double * begin, double * end);
void revalue(double r,double * begin, double * end);
const int Max = 5;
int main() 
{
    double properties[Max];
    double *end = Fill_array(properties,properties+Max);
    Show_array(properties,end);
    if (end-properties > 0)
    {
        cout << "Enter r:";
        double factor;
        while ( !(cin>>factor))
        {
           cin.clear();
           while (cin.get()!='\n')
               continue;
            cout << "Bad input; Please enter a number:";
        }
        revalue(factor,properties,end);
        Show_array(properties,end);   
    }
    cout << "Done\n";
    cin.get();

    return 0;

}
double* Fill_array(double * begin, double * end)
{
    double temp;
    double *pt;
    for (pt = begin;pt !=end;pt++)
    {
        cout << "Enter value #"<< (pt - begin + 1)<<":";
        cin >> temp;
        if (!cin)
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Bad input; input process terminated!";
            break;      
        }
        else if (temp < 0)
            break;
        *pt = temp;
    }
    return pt;
}
void Show_array(double * begin, double * end)
{
     for (double * pt = begin;pt != end;pt++)
    {
        cout << pt - begin<<":"<<*pt<<" ";
    }
    cout <<endl;
}
void revalue(double r,double * begin, double * end)
{
 for (double * pt = begin;pt != end;pt++)
    {
       *pt =(*pt) * r;
    }
}
#include <iostream>
using namespace std;

typedef double (*fp)(double x, double y);
double add(double x, double y);
double sub(double x, double y);
double calculate(double x, double y,fp fun);
int main() 
{
    double x=0,y=0;
    double (*pf[2])(double,double) = {add,sub};
    while (cin>>x>>y)
    {
        cout<<"add:"<<calculate(x,y,pf[0])<<endl;
        cout<<"sub:"<<calculate(x,y,pf[1])<<endl;
    }
    return 0;
}
double add(double x, double y)
{
    return x + y;
}
double sub(double x, double y)
{
    return x-y;
}
double calculate(double x, double y,fp fun)
{
    return (*fun)(x,y);
}

因为最近事情较多且该书内容浅显,所以以后只会选择一些代表性的习题发布,不好意思。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值