C++Primer Plus(第六版)第七章编程练习

好几道题没做,返回指针的函数写之前的写过就没再写一次了

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

double harmonic_mean(double a,double b);
int input(double *a);*/
int Fill_array(double *a,int size);
void Show_array(double *a, int size);
void Reverse_array(double *a,int size);
/*void display(double *a,int size);
double ave(double *a,int size);
void display(box a);
void do_the_math(box *a);
int factorial(int a);*/
int main()
{
    /*cout << "No.1"<<endl;
    double i,j;
    while(1)
    {
        cout << "input two numbers not '0':"<<endl;
        cin >> i;
        cin >> j;
        if(i==0 || j==0)
        {
           cout << "end the loop";
           break;
        }

        cout << "The harmonic mean =" <<harmonic_mean(i,j)<<endl;
    }
    cout << "No.2"<<endl;
    double golf[10];
    int size;
    size = input(golf);
    display(golf,size);
    cout << "No.3"<<endl;
    box one =
    {
        "Bone",10,10,10,50
    };
    display(one);
    do_the_math(&one);
    display(one);
    cout << "No.5"<<endl;
    int i;
    while(1)
    {cin >> i;
    if(cin.fail())
    {
        cin.clear();
        cout << "invalid input"<<endl;
        break;
    }
    cout << "factorial "<<i<< "! = "<<factorial(i)<<endl;
    }
    cout << "No.6"<< endl;
    double *a;
    int size = 10;
    size = Fill_array(a,size);
    Reverse_array(a,size);
    Show_array(a,size);*/
    cout << "No.10" <<endl;

    return 0;
}
/*
int factorial(int a)
{
    if(a==0 || a == 1)
        return 1;
    else
    {
        return a*factorial(a-1);
    }
}
double harmonic_mean(double a,double b)
{
    double temp;
    temp = 2.0*a*b/(a+b);
    return temp;
}*/
/*int Fill_array(double *a,int size)
{

    for(int i = 0; i<size ; i++)
    {
        cin >> a[i];
        if(!cin)
        {
            cin.clear();
            cout << "input failed"<<endl;
            int s = i;
            return s;
        }
    }
    return size;
}
void Show_array(double *a,int size)
{
    for(int i = 0; i<size ; i++)
    {
        cout << a[i]<<endl;
    }
}
void Reverse_array(double *a,int size)
{
    double * b = new double[size];
    for(int i=0; i<size ;i++)
    {
        b[i] = a[size - i - 1];
    }
    for(int i = 0; i < size ; i++)
    {
        a[i] = b[i];
    }
    delete [] b;
}*/
/*int input(double *a)
{
    cout << "input score of golf match:"<< endl;
    for(int i = 0; i<10 ; i++)
    {
        cin >> a[i];
        if(!cin)
        {
            cin.clear();
            cout << "input failed"<<endl;
            int size = i;
            return size;
        }
    }
    return 10;
}*/
/*
void display(double *a,int size)
{
    for(int i = 0; i<size ; i++)
    {
        cout << a[i]<<endl;
    }
    cout << "average of score:" <<ave(a,size);
}
double ave(double *a,int size)
{
    double temp = 0;
    for(int i = 0 ; i<size ; i++)
    {
        temp += a[i];
    }
    temp /= size;
    return temp;
}

void display(box a)
{
    cout << a.maker<< endl;
    cout << a.height<< endl;
    cout << a.width << endl;
    cout << a.length<< endl;
    cout << a.volume<< endl;
}
void do_the_math(box *a)
{
    a->volume = a->height * a->length * a->width;
}
*/

猜你喜欢

转载自blog.csdn.net/baidu_29452653/article/details/88073719