第七章 函数编程题

1

#include <iostream>
double average(double, double);
int main() {
    using namespace std;
    double n, m;
    cout << "Enter two numer: ";
    cin >> n >> m;
    while (n != 0 && m != 0)
    {
        double avg = average(n, m);
        cout << "调和平均数为: " << avg << endl;
        cout << "Enter two numer: ";
        cin >> n >> m;
    }
    return 0;
}
double average(double x, double y)
{
    return 2.0 * x * y /(x + y);
}

2

#include <iostream>
struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};
void show_num(box box1);
void set_num(box *box2);
int main()
{
    using namespace std;
    box b = {"apple", 1.0, 2.0, 3.0};
    set_num(&b);
    show_num(b);


    return 0;
}
void show_num(box box1)
{
    using namespace std;
    cout << "maker: " << box1.maker << endl;
    cout << "height: " << box1.height << endl;
    cout << "width: " << box1.width << endl;
    cout << "length: " << box1.length << endl;
    cout << "volume: " << box1.volume << endl;
}
void set_num(box *box2)
{
    using namespace std;
    box2->volume = box2->height * box2->length * box2->width;
}




3

#include <iostream>
struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};
void show_num(box box1);
void set_num(box *box2);
int main()
{
    using namespace std;
    box b = {"apple", 1.0, 2.0, 3.0};
    set_num(&b);
    show_num(b);


    return 0;
}
void show_num(box box1)
{
    using namespace std;
    cout << "maker: " << box1.maker << endl;
    cout << "height: " << box1.height << endl;
    cout << "width: " << box1.width << endl;
    cout << "length: " << box1.length << endl;
    cout << "volume: " << box1.volume << endl;
}
void set_num(box *box2)
{
    using namespace std;
    box2->volume = box2->height * box2->length * box2->width;
}

4

#include <iostream>

long double probability(unsigned numbers, unsigned picks);

int main() {
    using namespace std;
    cout << "中彩票头奖的几率为: " << probability(47, 5) * probability(27, 1) << endl;
    return 0;
}

long double probability(unsigned numbers, unsigned picks)
{
    long double result = 1.0;
    long double n;
    unsigned p;

    for (n = numbers, p = picks; p > 0; n--, p--)
        result = result * n /p;
    return result;
}

5

#include <iostream>
int f(int);

int main()
{
    using namespace std;
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cout << n << "的阶乘为" << f(n) << endl;
    return 0;
}
int f(int n)
{
    if (n == 0)
        return 1;
    else
        return n * f(n-1);
}

6

#include <iostream>
int Fill_array(double *, int);
void Show_array(double *, int);
void Reverse_array(double *, int);

int main()
{
    using namespace std;
    double n[10];
    int len = Fill_array(n, 10);
    Show_array(n, len);
    Reverse_array(n, len);
    Show_array(n, len);
    Reverse_array(n+1, len-2);
    Show_array(n, len);

    return 0;
}

int Fill_array(double *n, int len)
{
    using namespace std;
    double temp;
    int i = 0;
    cout << "Enter numbers: ";
    while (cin >> temp)
    {
        if (i == len)
            break;
        n[i] = temp;
        i++;
    }
    return i;
}

void Show_array(double *n, int len)
{
    using namespace std;
    for (int i = 0; i < len; ++i)
        cout << n[i] << " ";
    cout << endl;
}

void Reverse_array(double *n, int len)
{
    double temp;
    for (int i = 0, j = len-1; i <= j; i++, j--)
    {
        temp = n[i];
        n[i] = n[j];
        n[j] = temp;
    }
}

7

#include <iostream>
const int Max = 5;
double *fill_array (double ar[], int limit);
void show_array (double ar[], double *);
void revalue (double r, double ar[], double *);

int main() {
    using namespace std;
    double properties[Max];

    double *p = fill_array(properties, Max);
    show_array(properties, p);
    cout << "Enter revalueation factor: ";
    double factor;
    cin >> factor;
    revalue(factor, properties, p);
    show_array(properties, p);
    cout << "Done.\n";
    return 0;
}

double *fill_array(double ar[], int limit)
{
    using namespace std;
    double temp;
    int i;
    for (i = 0; i < limit; i++)
    {
        cout << "Enter value #" << (i+1) << ": ";
        cin >> temp;
        if(!cin)
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Bad input; input process terminated.\n";
            break;
        }
        else if (temp < 0)
            break;
        ar[i] = temp;
    }
    return ar+i;
}

void show_array(double ar[], double *p)
{
    using namespace std;
    int j=0;
    for (double *i = ar; i < p; i++)
    {
        cout << "Property #" << ++j << ": $";
        cout << *i << endl;
    }
}

void revalue(double r, double ar[], double *p)
{
    for (double *i = ar; i <= p; i++)
    {
        *i *= r;
    }
}

8

#include <iostream>
using namespace std;
const int SLEN = 30;
struct student
{
    char fullname[SLEN];
    char hobby[SLEN];
    int ooplevel;
};
void display1(student);
void display2( const student *);
void display3(const student *, int);

int getinfo(student *stu, int n);

int main()
{
    cout << "Enter class size: ";
    int class_size;
    cin >> class_size;
    while (cin.get()!='\n')
    {
        continue;
    }
    student *ptr_stu = new student[class_size];
    int entered = getinfo(ptr_stu, class_size);
    for (int i = 0; i < entered; ++i)
    {
        display1(ptr_stu[i]);
        display2(&ptr_stu[i]);
    }
    display3(ptr_stu, entered);
    delete [] ptr_stu;
    cout << "Done!\n";
    return 0;
}

int getinfo(student *stu, int n)
{
    int i;
    for (i = 0; i < n; ++i)
    {
        cout << "Enter fullname: ";
        cin >> stu->fullname;
        if(stu->fullname[0] == 'q')
            break;
        cout << "Enter hobby: ";
        cin >> stu->hobby;
        cout << "Enter ooplevel: ";
        cin >> stu->ooplevel;
    }
    return i;
}

void display1(student stu)
{
    cout << "Fullname: " << stu.fullname << endl;
    cout << "Hobby: " << stu.hobby << endl;
    cout << "Ooplevel: " << stu.ooplevel << endl;
}

void display2( const student * stu)
{
    cout << "Fullname: " << stu->fullname << endl;
    cout << "Hobby: " << stu->hobby << endl;
    cout << "Ooplevel: " << stu->ooplevel << endl;
}

void display3(student * stu, int n)
{
    for (int i = 0; i < n; ++i)
    {
        cout << "Fullname: " << stu[i].fullname << endl;
        cout << "Hobby: " << stu[i].hobby << endl;
        cout << "Ooplevel: " << stu[i].ooplevel << endl;
    }
}

9

#include <iostream>
double add(double x, double y);
double calculate(double x, double y, double (*pf)(double, double));

int main()
{
    using namespace std;
    double x, y;
    cout << "Enter two numbers: ";
    cin >> x >> y;
    cout << calculate(x, y, add);
    return 0;
}

double add(double x, double y)
{
    return x+y;
}

double calculate(double x, double y, double (*pf)(double, double))
{
    return add(x,y);

}
发布了42 篇原创文章 · 获赞 1 · 访问量 1595

猜你喜欢

转载自blog.csdn.net/qq_32631105/article/details/104199236