Practise--《C++ primer plus》习题程序编写

本文详细介绍了使用C++编程实现学生信息输入、显示功能,并通过函数指针进行数学运算操作。通过实例展示了如何高效处理输入数据、结构体运用及函数指针的应用,旨在提升编程实践能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

《C++ primer plus》第七章8、9题,编程格式思路纯属个人喜好:

8. 

#include <iostream>
using namespace std;

const int SLEN = 30;
struct student {
    char fullname[SLEN];
    char hobby[SLEN];
    int opplevel;
};

int getinfo ( student pa[ ], int n );
void display1 ( student st );
void display2 ( const student * ps );
void display3 ( const student pa[ ], 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 pa[], int n )
{
    int count = 0;

    for (int i = 0; i < n; i++)
    {
        cout << "Please input student's name: ";
        cin.getline( pa[i].fullname, SLEN );
        if ( strlen(pa[i].fullname) != 0 )        //判断是否为空行
        {
            count += 1;
            cout << "Please input student's hobby: ";
            cin.getline( pa[i].hobby, SLEN );
            cout << "Please input student's ooplevel: ";
            cin >> pa[i].opplevel;
            while ( cin.get() != '\n' )
                continue;
        }
        else
            break;
        }
    cout << "In fact we have " << count << " students.\n" << endl;
    return count;
}

void display1( student st )
{
    cout << st.fullname << '\t' << st.hobby << '\t' << st.opplevel <<endl;
}

void display2( const student * ps )
{
    cout << ps->fullname << '\t' << ps->hobby << '\t' << ps->opplevel << endl;
}

void display3( const student pa[ ], int n )
{
    for (int i = 0; i < n; i++ )
    {
        cout << pa[i].fullname << '\t' << pa[i].hobby << '\t' << pa[i].opplevel << endl;
    }
}

9.

(1) 这里是让用户输入两个数计算或者按q退出:

#include <iostream>
using namespace std;

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

int main( )
{
    double x, y;
    cout << "Please input two numbers or end by q: ";
    while( (cin >> x >> y ) )
    {double result = calculate ( x, y, add );
        cout << "Result is " << result << endl;
        cout << "Please input two numbers or end by q: ";
    }
    return 0;
}

(2) 用了一个指向函数的指针数组:

#include <iostream>
using namespace std;

double add( double x, double y )
{
    return x + y;
}
double mlu( double x, double y )
{
    return x * y;
}
double calculate ( double x, double y, double (*f)( double , double )  )
{
    return f( x, y );
}

int main( )
{
    double x, y;
    double (*pf[2]) ( double, double ) = {add, mlu};
    char * cp[2] = {"add", "mlu"};
    cout << "Please input two numbers or end by q: ";
    while( (cin >> x >> y ) )
    {
        for (int i = 0; i < 2; i++ )
            cout << cp[i] << " = " << calculate( x, y, pf[i] ) << endl;
        cout << "Please input two numbers or end by q: ";
    }
    return 0;
}

转载于:https://www.cnblogs.com/heyuheitong/archive/2012/11/20/2779731.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值