C++ Primer Plus第七章练习

本文展示了C++中多个函数的使用,包括计算调和平均数、数组处理、结构体操作、概率计算、递归、数组反转、季度支出记录、学生信息管理和算术运算封装。通过实例详细阐述了如何进行输入输出、数据处理和结构化数据的管理。
摘要由CSDN通过智能技术生成

7.13.1

#include <iostream>
using namespace std;
int Calculate(int, int);
int main(void)
{
    int num1, num2;
    while(cin >> num1 >> num2 && num1 > 0 && num2 > 0)
        cout << num1 << "与" << num2 << "的调和平均数为: " << Calculate(num1, num2) << endl;
    return 0;
}
int Calculate(int n1, int n2)
{
    return 2.0 * n1 * n2 / (n1 + n2);
}

7.13.2

#include <iostream>
using namespace std;
int inarray(int *, int);
void show(int *, int);
void calculate(int *, int);
int main(void)
{
    int ar[5];
    int c = inarray(ar, 5);
    show(ar, c);
    calculate(ar, c);
    return 0;
}
int inarray(int * ar, int n)
{
    int temp, i;
    for(i = 0; i < n; i++)
    {
        cin >> temp;
        if(temp < 0)
            break;
        ar[i] = temp;
    }
    return i;
}
void show(int * ar, int n)
{
    for(int i = 0; i < n; i++)
        cout << ar[i] << endl;
}
void calculate(int * ar, int n)
{
    int sum = 0;
    for(int i = 0; i < n; i++)
        sum += ar[i];
    cout << "平均数为" << sum / n << endl;
}

7.13.3

#include <iostream>
using namespace std;
struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};
void show(box);
void amend(box *);
int main(void)
{
    box c = 
    {
        "nmsl",
        9,
        6,
        5,
        2
    };
    show(c);
    amend(&c);
    show(c);
    return 0;
}
void show(box b)
{
    cout << b.maker << endl
         << b.height << endl
         << b.length << endl
         << b.width << endl
         << b.volume << endl;
}
void amend(box * b)
{
    b->volume = b->width * b->length * b->height;
}

7.13.4

#include <iostream>
using namespace std;
long double field(int, int);
int main(void)
{
    cout << "概率为: " << field(47, 5) * field(27, 1) << endl;
    return 0;
}
long double field(int sum, int num)
{
    long double result = 1.0;
    for(int i = 0, j = sum, l = num; i < num; i++, j--, l--)
        result = result * (j / l);
    return result;
}

7.13.5

#include <iostream>
using namespace std;
int factorial(int);
int main(void)
{
    cout << "请输入需要得到的递归数: ";
    int temp;
    cin >> temp;
    cout << temp << "的递归为: " << factorial(temp) << endl;
    return 0;
}
int factorial(int n)
{
    if(n == 1)
        return 1;
    else
        return factorial(n - 1) * n;
}

7.13.6

#include <iostream>
using namespace std;
int Fill_array(double *, int);
void Show_array(double *, int);
void Reverse_array(double *, int);
int main(void)
{
    double ar[5];
    int c = Fill_array(ar, 5);
    Show_array(ar, c);
    Reverse_array(ar, c);
    Show_array(ar, c);
    return 0;
}
int Fill_array(double * ar, int n)
{
    int i, temp;
    for(i = 0; i < n; i++)
    {
        cout << "请输入第" << i + 1 << "个数: ";
        if(cin >> temp && temp < 0)
            break;
        ar[i] = temp;
    }
    return i;
}
void Show_array(double * ar, int n)
{
    for(int i = 0; i < n; i++)
        cout << ar[i] << endl;
}
void Reverse_array(double * ar, int n)
{
    double temp;
    for(int i = 0, j = n - 1; i <= j; i++, j--)
    {
        temp = ar[i];
        ar[i] = ar[j];
        ar[j] = temp;
    }
}

7.13.7

#include <iostream>
using namespace std;
double * Fill_array(double *, double *);
void Show_array(double *, double *);
void Reverse_array(double *, double *, double);
int main(void)
{
    double ar[5];
    double * end = Fill_array(ar, &ar[5]);
    Show_array(ar, end);
    Reverse_array(ar, end, 6.5);
    Show_array(ar, end);
    return 0;
}
double * Fill_array(double * begin, double * end)
{
    double temp, * i;
    for(i = begin; i < end; i++)
    {
        cout << "请输入第" << (i - begin) + 1 << "个数: ";
        if(cin >> temp && temp < 0)
            return i;
        *i = temp;
    }
    return i;
}
void Show_array(double * begin, double * end)
{
    for(double * i = begin; i < end; i++)
        cout << *i << endl;
}
void Reverse_array(double * begin, double * end, double n)
{
    for(double * i = begin; i < end; i++)
        *i *= n;
}

7.13.8
a

#include <iostream>
using namespace std;
const char * quarter[4] = {"Spring", "Summer", "Fall", "Winter"};
void fill(double *, int);
void show(double *, int);
int main(void)
{
    
    double Seasons[4];
    fill(Seasons, 4);
    show(Seasons, 4);
    return 0;
}
void fill(double * Seasons, int n)
{
    for(int i = 0; i < n; i++)
    {
        cout << "Enter " << quarter[i] << " expenses: ";
        cin >> Seasons[i];
    }
}
void show(double * Seasons, int n)
{
    double total = 0.0;
    cout << "\bEXPENSES\n";
    for(int i = 0; i < n; i++)
    {
        cout << quarter[i] << ": $" << Seasons[i] << endl;
        total += Seasons[i];
    }
    cout << "Total Expenses: $" << total << endl;
}

b

#include <iostream>
using namespace std;
const char * quarter[4] = {"Spring", "Summer", "Fall", "Winter"};
struct Seasons
{
    double array[4];
};
void fill(Seasons *, int);
void show(Seasons *, int);
int main(void)
{
    Seasons x;
    fill(&x, 4);
    show(&x, 4);
    return 0;
}
void fill(Seasons * x, int n)
{
    for(int i = 0; i < n; i++)
    {
        cout << "Enter " << quarter[i] << " expenses: ";
        cin >> x->array[i];
    }
}
void show(Seasons * x, int n)
{
    double total = 0.0;
    cout << "\bEXPENSES\n";
    for(int i = 0; i < n; i++)
    {
        cout << quarter[i] << ": $" << x->array[i] << endl;
        total += x->array[i];
    }
    cout << "Total Expenses: $" << total << endl;
}

7.13.9

#include <iostream>
using namespace std;
const int SLEN = 30;
struct student
{
    char fullname[SLEN];
    char hobby[SLEN];
    int ooplevel;
};
int getinfo(student *, int);
void display1(student);
void display2(const student *);
void display3(const student *, int);
int main(void)
{
    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 * pa, int n)
{
    int i;
    for(i = 0; i < n; i++)
    {
        cout << "请输入第" << i + 1 << "个学生的姓名:";
        cin >> pa[i].fullname;
        cout << "请输入第" << i + 1 << "个学生的嗜好:";
        cin >> pa[i].hobby;
        cout << "请输入第" << i + 1 << "个学生的等级:";
        cin >> pa[i].ooplevel;
        if(!cin)
        {
            cin.clear();
            while(cin.get() != '\n');
            break;
        }
    }
    return i;
}
void display1(student st)
{
    cout << "姓名: " << st.fullname << endl;
    cout << "嗜好: " << st.hobby << endl;
    cout << "等级: " << st.ooplevel << endl;
}
void display2(const student * ps)
{
    cout << "姓名: " << ps->fullname << endl;
    cout << "嗜好: " << ps->hobby << endl;
    cout << "等级: " << ps->ooplevel << endl;
}
void display3(const student * pa, int n)
{
    for(int i = 0; i < n; i++)
    {
        cout << "姓名: " << pa[i].fullname << endl;
        cout << "嗜好: " << pa[i].hobby << endl;
        cout << "等级: " << pa[i].ooplevel << endl;
    }
}

7.13.10

#include <iostream>
using namespace std;
double calculate(double, double, double (*)(double, double));
double add(double, double);
double sub(double, double);
int main(void)
{
    double (*pf[2])(double, double) = {add, sub};
    double n1, n2;
    cout << "请输入2个数: ";
    while(cin >> n1 >> n2)
    {
        for(int i = 0; i < 2; i++)
        {
            switch (i)
            {
            case 0:
                cout << n1 << " + " << n2 << " = " << calculate(n1, n2, pf[0]) << endl;
                break;
            case 1:
                cout << n1 << " - " << n2 << " = " << calculate(n1, n2, pf[1]) << endl;
                break;
            }
        }
    }
    return 0;
}
double calculate(double n1, double n2, double (*pt)(double, double))
{
    return pt(n1, n2);
}
double add(double n1, double n2)
{
    return n1 + n2;
}
double sub(double n1, double n2)
{
    return n1 - n2;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值