C++ Primer Plus | chapter 7 课后编程练习

函数——C++的编程模块

#include<iostream>
using namespace std;
double Average(double, double);
int main()
{
    double x, y;
    cout << "Please enter x and y (0 to quit): ";
    cin >> x >> y;
    while(x && y)
    {
        cout << "The harmonic mean of x and y is: " << Average(x, y) << endl;
        cout << "Please enter x and y (0 to quit): ";
        cin >> x >> y;
    }
    return 0;
}

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

#include<iostream>
using namespace std;
int Input(double [], int);
void Show(double [], int);
double Mean(double [], int);
int main()
{
    double ar[10];
    int len = 0;
    len = Input(ar, 10);
    Show(ar, len);
    cout << "The mean of the scores is: " << Mean(ar, len);
}

int Input(double ar[], int n)
{
    int i = 0;
    cout << "Enter the score #" << i+1 << " (q to quit): ";
    while(i<n && cin >> ar[i])
    {
        i++;
        if(i == n)  break;
        else cout << "Enter the score #" << i+1 << " (q to quit): ";
    }
    return i;
}

void Show(double ar[], int len)
{
    for(int i=0; i<len; i++)
        cout << ar[i] << " ";
    cout << endl;
    return;
}

double Mean(double ar[], int len)
{
    double ans = 0;
    for(int i=0; i<len; i++)
        ans += ar[i];
    return ans/len;
}

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

int main()
{
    box x = {"FirstOne", 2, 2, 3};
    cal_V(&x);
    show_Member(x);
   return 0;
}

void show_Member(box x)
{
    cout << "Maker: " << x.maker << "\nheight: " << x.height << "\nwidth: " << x.width << "\nlength: " << x.length << "\nvolume: " << x.volume << endl;
    return;
}

void cal_V(box* x)
{
    x->volume = x->height * x->length * x->width;
    return;
}

#include<iostream>
using namespace std;
long double probability(unsigned numbers1, unsigned numbers2, unsigned picks1, unsigned picks2);
int main()
{
    unsigned total1, total2, choices1, choices2;
    cout << "Field number\tpicks\tSecond\tpicks" << endl;
    while((cin >> total1 >> choices1 >> total2 >> choices2) && choices1 <= total1 && choices2 <= total2)
    {
        cout << "You have one chance in ";
        cout << probability(total1, total2, choices1, choices2);
        cout << " of winning." << endl;
        cout << "Next 4 numbers (q to quit): ";
    }
    cout << "Bye" << endl;
    return 0;
}
long double probability(unsigned numbers1, unsigned numbers2, unsigned picks1, unsigned picks2)
{
    long double ans = 1.0;
    long double n;
    unsigned p;
    for(n = numbers1, p = picks1; p > 0; n--, p--)
        ans *= n/p;
    for(n = numbers2, p = picks2; p > 0; n--, p--)
        ans *= n/p;
    return ans;
}

#include<iostream>
using namespace std;
long long factorial(int n);
int main()
{
    int n;
    cout << "Please enter the number n (q to quit): ";
    while(cin >> n)
    {
        cout << n << "! = " << factorial(n) << endl;
        cout << "Please enter the number n (q to quit): ";
    }

    return 0;
}

long long factorial(int n)
{
    if(n == 0 || n == 1) return 1;
    return n*factorial(n-1);
}

#include<iostream>
using namespace std;
int Fill_array(double ar[], int n);
void Show_array(const double ar[], int len);
void Reverse_array(double ar[], int len);
int main()
{
    int n;
    cout << "Please enter the maximum number of the array: ";
    cin >> n;
    double *ar = new double [n];
    int len = Fill_array(ar, n);
    Show_array(ar, len);
    Reverse_array(ar, len);
    Show_array(ar, len);
    Reverse_array(ar, len); //先让数组变为原来的
    Reverse_array(ar+1, len-2); //妙啊
    Show_array(ar, len);
    delete [] ar;
    return 0;
}
int Fill_array(double ar[], int n)
{
    int len = 0;
    cout << "Enter number #" << len+1 << ": ";
    while(len < n && cin >> ar[len])
    {
        len++;
        if(len < n)    cout << "Enter number #" << len+1 << ": ";
    }
    return len;
}
void Show_array(const double ar[], int len)
{
    for(int i=0; i<len; i++)
        cout << ar[i] << " ";
    cout << endl;
    return;
}
void Reverse_array(double ar[], int len)
{
    for(int i=0; i<len/2; i++)
    {
        int tmp = ar[i];
        ar[i] = ar[len-i-1];
        ar[len-i-1] = tmp;
    }
    return;
}

#include<iostream>
using namespace std;
const int Max = 5;
double * fill_array(double *ar, int limit);
void show_array(double * ar, const double * last);
void revalue(double r, double *ar, double *last);
int main()
{
    using namespace std;
    double properties[Max];
    double *last = fill_array(properties, Max);
    show_array(properties, last);
    if(last - properties > 0)
    {
        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, last);
        show_array(properties, last);
    }
    cout << "Done." << endl;
    cin.get();
    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, const double * last)
{
    using namespace std;
    int i=0;
    for(double *p = ar; p < last; p++, i++)
    {
        cout << "Property #" << (i+1) << ": $";
        cout << *p << endl;
    }
}

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

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

//---------- b ----------
struct expense
{
    double ar[Seasons];
};
void fill_exp(expense * ptr);
void show_exp(expense * ptr);
int main()
{
    expense exp;
    fill_exp(&exp);
    show_exp(&exp);
    return 0;
}
void fill_exp(expense * ptr)
{
    for(int i=0; i<Seasons; i++)
    {
        cout << "Enter " << Snames[i] << " expenses: ";
        cin >> ptr->ar[i];
    }
}
void show_exp(expense * ptr)
{
    double total = 0.0;
    cout << "\nEXPENSES\n";
    for(int i=0; i<Seasons; i++)
    {
        cout << Snames[i] << ": $" << ptr->ar[i] << endl;
        total += ptr->ar[i];
    }
    cout << "Total Expenses: $" << total << endl;
}

#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()
{
    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 = 0;
    for(; i<n; i++)
    {
        cout << "Student #" << i+1 << ":" << endl;
        cout << "Full name: ";
        cin.getline(pa[i].fullname, SLEN);
        if(strlen(pa[i].fullname) == 0)
        {
            cout << "End." << endl;
            break;
        }
        cout << "Hobby: ";
        cin.getline(pa[i].hobby, SLEN);
        cout << "Ooplevel: ";
        cin >> pa[i].ooplevel;
        cin.get();
    }
    return i;
}
void display1(student st)
{
    cout << "\ndisplay1: " << endl;
    cout << "Full name: " << st.fullname << endl;
    cout << "Hobby: " << st.hobby << endl;
    cout << "Ooplevel: " << st.ooplevel << endl;
}
void display2(const student * ps)
{
    cout << "\ndisplay2: " << endl;
    cout << "Full name: " << ps->fullname << endl;
    cout << "Hobby: " << ps->hobby << endl;
    cout << "Ooplevel: " << ps->ooplevel << endl;
}
void display3(const student pa[], int n)
{
    cout << "\ndisplay3: " << endl;
    for(int i=0; i<n; i++)
    {
        cout << "Full name: " << pa[i].fullname << endl;
        cout << "Hobby: " << pa[i].hobby << endl;
        cout << "Ooplevel: " << pa[i].ooplevel << endl;
    }
}

#include<iostream>
using namespace std;
double add(double x, double y);
double multiply(double x, double y);
double calculate(double x, double y, double (*pf)(double, double));
int main()
{
    double x, y;
    cout << "x: ";
    cin >> x;
    cout << "y: ";
    cin >> y;
    double (*pa[2])(double x, double y) = {add, multiply};
    for(int i=0; i<2; i++)
    {
        cout << calculate(x, y, pa[i]) << endl;
    }
    return 0;
}
double add(double x, double y)
{
    return x + y;
}
double multiply(double x, double y)
{
    return x * y;
}
double calculate(double x, double y, double (*pf)(double, double))
{
    return (*pf)(x, y);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值