C++ Primer Plus 第6版 中文版 第7章编程练习

1、

#include <iostream>

using namespace std;

double Average(int x, int y);

int main()
{
    double result, one, two;

    cout << "Please enter two numbers:";

    while (cin >> one >> two)
    {
        if(!one || !two)
            break;
        result = Average(one, two);
        cout << "result = " << result << endl;
        cout << "Please enter two numbers:";
    }

    return 0;
}

double Average(int x, int y)
{
    return 2.0*x*y/(x+y);
}

2、

#include <iostream>

using namespace std;

int input(double *p, int size);
void show(double *p, int size);
double calculate(double *p, int size);

int main()
{
    double data[10] = {0.0};
    int num = 0;
    double ave = 0;

    num = input(data, 10);
    show(data, num);
    ave = calculate(data, num);

    cout << "Average = " << ave << endl;

    return 0;
}

int input(double *p, int size)
{
    int count = 0;

    cout << "Please enter max 10 numbers(q to quit):";

    while(cin >> p[count++])
    {
        if(count == size)
            return count;
    }

    return count-1;
}

void show(double *p, int size)
{
    for(int i=0; i<size; i++)
        cout << p[i] << " ";
}

double calculate(double *p, int size)
{
    double sum = 0;

    for(int i=0; i<size; i++)
    {
        sum += p[i];
    }

    return sum/size;
}

3、

#include <iostream>

using namespace std;
struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

void show(struct box tmp);
void show_point(struct box *p);

int main()
{
    struct box bb = {"BBB", 2.0, 3.0, 4.0, 10.0};
    show(bb);
    show_point(&bb);
    show(bb);

    return 0;
}

void show(struct box tmp)
{
    cout << "maker=" << tmp.maker
         << ", height=" << tmp.height
         << ", width=" << tmp.width
         << ", length=" << tmp.length
         << ", volume=" << tmp.volume
         << endl;
}

void show_point(struct box *p)
{
    p->volume = p->height*p->width*p->length;
}

4、

#include <iostream>

using namespace std;
long double probability(unsigned numbers, unsigned picks);

int main()
{
    long double field = probability(47, 5);
    long double special = probability(27, 1);

    cout << "probability = " << field*special << 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>

using namespace std;
long long jiecheng(long n);

int main()
{
    long data;

    while(cin >> data)
    {
        cout << data << "! = " << jiecheng(data) << endl;
    }

    return 0;
}

long long jiecheng(long n)
{
    if(n == 0)
        return 1;

    return n*jiecheng(n-1);
}

6、

#include <iostream>

using namespace std;

int Fill_array(double *arr, int size);
void Show_array(double *arr, int size);
void Reverse_array(double *arr, int size);

int main()
{
    double arr[10] = {0.0};

    int num = Fill_array(arr, 10);
    Show_array(arr, num);
    Reverse_array(arr, num);
    Show_array(arr, num);

    return 0;
}

int Fill_array(double *arr, int size)
{
    int count = 0;
    cout << "请输入double数(最多10个):";
    while(cin >> arr[count])
    {
        count++;
        if(count == size)
            break;
    }
    return count;
}

void Show_array(double *arr, int size)
{
    cout << "arr is: ";
    for(int i=0; i<size; i++)
    {
        cout << arr[i] << " ";
        if(i == size-1)
            cout << endl;
    }
}

void Reverse_array(double *arr, int size)
{
    double * first = &arr[0];
    double * last = &arr[size-1];
    double tmp = 0;

    while(first != last)
    {
        tmp = *first;
        *first = *last;
        *last = tmp;
        first++;
        last--;
    }
}

7、

#include <iostream>

using namespace std;
const int Max = 5;
double * fill_array(double *begin, double *end);
void show_array(const double *begin, const double *end);
void revalue(double r, double *begin, double *end);

int main()
{
    double properties[Max];

    double *point = fill_array(properties, properties+Max);
    show_array(properties, point);
    if(point != properties)
    {
        cout << "Enter revaluation factor: ";
        double factor;
        while(!(cin >> factor))
        {
            cin.clear();
            while(cin.get() != '\n')
                continue;
            cout << "Bad input; Please enter a number: ";
        }
        revalue(factor, properties, point);
        show_array(properties, point);
    }
    cout << "Done.\n";
    cin.get();
    cin.get();

    return 0;
}

double *fill_array(double *begin, double *end)
{
    double temp, *p;
    int i=0;
    p = begin;

    while(p != end)
    {
        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;
        *p = temp;
        p++;
        i++;
    }
    return p;
}

void show_array(const double *begin, const double *end)
{
    const double *p = begin;
    int i = 0;
    while(p!=end)
    {
        cout << "Property #" << (i+1) << ": $";
        cout << *p << endl;
        p++;
        i++;
    }
}

void revalue(double r, double *begin, double *end)
{
    double *p = begin;
    while(p!=end)
    {
        *p *= r;
        p++;
    }
}

8、

#include <iostream>

using namespace std;
struct expense
{
    double a[4];
};
const int Seasons = 4;
const char * Snames[4] = {"Spring", "Summer", "Fall", "Winter"};

void fill(double *arr);
void show(double *arr);
void fill_struct(struct expense *tmp);
void show_struct(struct expense *tmp);

int main()
{
    double data[Seasons] = {0.0};
    struct expense e;

    fill(data);
    show(data);
    fill_struct(&e);
    show_struct(&e);

    return 0;
}

void fill(double *arr)
{
    for(int i=0; i<Seasons; i++)
    {
        cout << "Enter " << Snames[i] << " expenses: ";
        cin >> arr[i];
    }
}

void show(double *arr)
{
    double total = 0.0;
    cout << "\nEXPENSES\n";
    for(int i=0; i<Seasons; i++)
    {
        cout << Snames[i] << ": $" << arr[i] << endl;
        total += arr[i];
    }
    cout << "Total Expenses: $" << total << endl;
}

void fill_struct(expense *tmp)
{
    for(int i=0; i<Seasons; i++)
    {
        cout << "Enter " << Snames[i] << " expenses: ";
        cin >> tmp->a[i];
    }
}

void show_struct(expense *tmp)
{
    double total = 0.0;
    cout << "\nEXPENSES\n";
    for(int i=0; i<Seasons; i++)
    {
        cout << Snames[i] << ": $" << tmp->a[i] << endl;
        total += tmp->a[i];
    }
    cout << "Total Expenses: $" << total << endl;
}

9、

#include <iostream>
#include <cstring>

using namespace std;
const int SLEN = 30;
struct student
{
    char fullname[SLEN];
    char hobby[SLEN];
    int ooplevel;
};

int getinfo(student pa[], int n);
void display1(student st);
void display2(const student * ps);
void display3(const student pa[], int n);

int main()
{
    student arr[3];
    int size = getinfo(arr, 3);
    display1(arr[1]);
    display2(&arr[2]);
    display3(arr, size);

    return 0;
}

int getinfo(student pa[], int n)
{
    int i=0;
    while(i<n)
    {
        cout << "Enter #" << (i+1) << " fullname: ";
        cin.getline(pa[i].fullname, SLEN);
        if(pa[i].fullname[0] == '\0')
            break;
        cout << "Enter #" << (i+1) << " hobby: ";
        cin.getline(pa[i].hobby, SLEN);
        cout << "Enter #" << (i+1) << " ooplevel: ";
        cin >> pa[i].ooplevel;
        cin.get();
        i++;
    }
    return i;
}

void display1(student st)
{
    cout << "Student info: ("
         << st.fullname << ","
         << st.hobby << ","
         << st.ooplevel << ")" << endl;
}

void display2(const student *ps)
{
    cout << "Student info: ("
         << ps->fullname << ","
         << ps->hobby << ","
         << ps->ooplevel << ")" << endl;
}

void display3(const student pa[], int n)
{
    for(int i=0; i<n; i++)
    {
        cout << "student " << (i+1) << " fullname: " << pa[i].fullname << endl;
        cout << "student " << (i+1) << " hobby: " << pa[i].hobby << endl;
        cout << "student " << (i+1) << " ooplevel: " << pa[i].ooplevel << endl;
    }
}

10、

#include <iostream>

using namespace std;
double add(double x, double y);
double minu(double x, double y);
double multi(double x, double y);
double divide(double x, double y);
double calculate(double x, double y, double (*ptr)(double, double));

int main()
{
    double d1, d2;

    double (*pf[3])(double, double) = {add, minu, divide};

    cout << "Please enter two double numbers: " ;
    while(cin >> d1 >> d2)
    {
        double q = calculate(d1, d2, add);
        cout << "Add result is " << q << endl;
        double q2 = calculate(d1, d2, minu);
        cout << "Minus result is " << q2 << endl;
        for(int i=0; i<3; i++)
        {
            double res = (*pf[i])(d1, d2);
            cout << "Res is " << res << endl;
        }
        cout << "Please enter two double numbers: " ;
    }

    return 0;
}

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

double minu(double x, double y)
{
    return x-y;
}

double multi(double x, double y)
{
    return x*y;
}

double divide(double x, double y)
{
    if(y==0)
    {
        cout << "y cannot be 0!" << endl;
        return -1;
    }
    return x/y;
}

double calculate(double x, double y, double (*ptr)(double, double))
{
    return (*ptr)(x, y);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《C++ Primer Plus》第6中文版是一本经典的C++编程入门教材,由Stephen Prata编写。本书内容全面,涵盖了C++语言的基础知识、面向对象编程、模板、STL等方面的内容,适合初学者和有一定编程基础的读者阅读。本书注重实践,每章都有大量的练习题和编程实践,可以帮助读者巩固所学知识,提高编程能力。总之,这是一本非常好的C++编程入门教材,值得推荐。 ### 回答2: "c primer plus"第6是一本经典的C++语言入门教程,由美国程序员Stephen Prata所著。这本书的中文版经过翻译者熟练的工作,已经被广泛应用在我国C++编程学习领域,成为了国内学习C++语言必须的参考书之一。 本书分为18个章节,每个章节都按照步骤讲解从基本的语法到高级的应用。首先,作者从介绍C++编程基础知识开始,逐步深入到C++编程更高级的概念和应用,如指针、类、继承、多态和异常处理等。 在学习过程中,作者各章节都非常注重实践。每个章节中都有很多关于C++代码编写、程序调试、错误处理等方面的实例和演示代码,有助于读者加深对C++语言的理解和实践能力。 除了基础知识以外,本书还讲解了一些实际应用的知识,如数据库编程和多线程编程等。对于想进一步学习C++开发的读者来说,这样的内容是非常宝贵的。 总之,“c primer plus”第6中文版是一本非常优秀的C++编程语言教程,对于初学者来说是一本很好的选择。它不仅注重基本知识和实践,而且具备实际应用能力,有助于提高读者的编程水平并掌握C++领域的前沿技术。 ### 回答3: 《C Primer Plus》第6是一本经典的编程入门教程,旨在帮助读者掌握C语言的核心概念和编程技巧。本书的作者是Stephen Prata,他以清晰、易懂的语言和实例,引导读者逐步理解和应用C语言的基础知识。 本书的内容涵盖了C语言的大部分核心概念、语法和程序设计方法。它分为18个章节,依次介绍了变量、表达式和语句、字符串和格式化输入/输出、控制语句、函数、数组和指针、结构体、文件输入/输出、位运算、算法和数据结构等多个主题。其中,每个章节都深入浅出地讲解了概念、语法和实践练习。此外,本书还附带了一些实用的工具和技巧,如调试、数据类型转换、内存管理、预处理器等,以及一些常见问题的解决方案。 与其他C语言教程相比,本书的优点在于对基础知识的详细讲解和实例演示。作者Stephen Prata精心挑选了各种有趣的编程问题和练习,使读者能够更好地了解C语言的核心概念和程序设计方法。此外,本书还包含了一些高级主题,如指针、结构体、算法和数据结构等,可以帮助读者深入掌握C语言的精髓。 总之,《C Primer Plus》第6是一本优秀的C语言入门教程。无论是初学者还是有经验的程序员,都可以通过本书提升自己的编程技能和思考能力。如果你正在寻找一本系统和全面的C语言教程,那么这本书绝对是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值