第七章 编程练习

编程练习1

#include <iostream>
using namespace std;

double average(double a, double b);
int main()
{
    double a,b;
    double c;
    cout<<"please enter two numbers: ";
    while((cin>>a>>b))
    {
        if (a==0 || b==0)
            break;
        c=average(a,b);
        cout<<"average of "<<a<<" and "<<b<<" is "<<c<<endl;
        cout<<"please enter two numbers: ";
    }
    system("pause");
    return 0;   
}
double average(double a, double b)
{
    return 2*a*b*(a+b)/2;
}

编程练习2

#include <iostream>
using namespace std;

const int Max = 10;
int fill_arr(double ar[], int limit);
void show_arr(const double ar[], int n);
double average(const double ar[], int n);

int main()
{
    int n_golf;
    double golf[Max];
    n_golf = fill_arr(golf, Max);
    show_arr(golf, n_golf);
    cout << average(golf,n_golf) << endl;

    system("pause");
    return 0;
}

int fill_arr(double ar[], int limit)
{
    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 inout; input process terminated"<<endl;
         break;
     }
     else if (temp<0)
         break;
     ar[i] = temp;
    }

    return i;       
}

void show_arr(const double ar[], int n)
{
    for (int i=0;i<n;i++)
    {
        cout <<"Golf #"<<(i+1)<<": ";
        cout << ar[i] <<endl;
    }       
}

double average(const double ar[], int n)
{
    double total=0;
    for (int i=0;i<n;i++)
        total = total+ar[i];
    return total/n;
}

编程练习3

#include <iostream>
using namespace std;

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

void show_box(box b);
void box_volume(box *b);

int main()
{
    box b={"box_maker",20.5,30.6,40.4,25.5};
    box_volume(&b);
    show_box(b);
    system("pause");
    return 0;
}
void box_volume(box *b)
{
    b->volume = b->height*b->width*b->length;
}
void show_box(box b)
{
    cout << "box maker is : " << b.maker << endl;
    cout << "box height is : " << b.height << endl;
    cout << "box width is : " << b.width << endl;
    cout << "box length is : " << b.length << endl;
    cout << "box volume is : " << b.volume << endl;
}

编程练习5

#include <iostream>
using namespace std;

long f(int i);
int main()
{
    int n;
    cin >> n;
    cout << f(n) << endl;

    system("pause");
    return 0;
}
long f(int i)
{
    long result = i;
    for(int n = i; n > 1; n--)
        result = result * f(n-1);
    return result;
}

编程练习6

#include <iostream>
using namespace std;

const int Max = 5;
double * fill_arr(double *start, int limit);
void show_arr(double *start, double *end);
void revalue(double *start, double *end);

int main()
{
    int n_properties;
    double properties[Max];
    double *properties_end;
    properties_end = fill_arr(properties, Max);
    show_arr(properties, properties_end);

    revalue(properties, properties_end);
    cout << "the array is reversed: " << endl;
    show_arr(properties, properties_end);

    system("pause");
    return 0;
}

double * fill_arr(double *start, int limit)
{
    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 inout; input process terminated"<<endl;
         break;
     }
     else if (temp < 0)
         break;
     *start = temp;
     start++;
    }
    return start;       
}

void show_arr(double *start, double *end)
{
    double *pt;
    int i=0;
    for (pt=start;pt!=end;pt++)
    {
        cout <<"Property #"<<(i+1)<<": ";
        cout << *pt <<endl;
        i++;
    }       
}

void revalue(double *start, double *end)
{
    double *p1 = start;
    double *p2 = end-1;
    double temp;
    int n=(end-start);

    for (int i=0;i<n/2;i++)
    {   
        temp=*p1;
        *p1 = *p2;
        *p2=temp;
        p1++;
        p2--;       
    }
}

编程练习7

#include <iostream>
using namespace std;

const int Max = 5;
double * fill_arr(double ar[], int limit);
void show_arr(double *start, double *end);
void revalue(double r, double *start, double *end);

int main()
{
    double properties[Max];
    double *properties_end;
    properties_end = fill_arr(properties, Max);
    show_arr(properties, properties_end);

    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, properties_end);
    cout << "the array is revalued: " << endl;
    show_arr(properties, properties_end);

    system("pause");
    return 0;
}

double * fill_arr(double *start, int limit)
{
    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 inout; input process terminated"<<endl;
         break;
     }
     else if (temp < 0)
         break;
     *start = temp;
     start++;
    }
    return start;       
}

void show_arr(double *start, double *end)
{
    double *pt;
    int i=0;
    for (pt=start;pt!=end;pt++)
    {
        cout <<"Property #"<<(i+1)<<": ";
        cout << *pt <<endl;
        i++;
    }       
}

void revalue(double r, double *start, double *end)
{
    double *pa;
    for(pa = start; pa!=end; pa++)
        *pa = r;
} 

编程练习8.a

#include <iostream>
using namespace std;

const int Season = 4;
const char* Snames[Season] = {"Spring", "Summer","Fall","Winter"};

void fill(double ex[]);
void show(double ex[]);

int main()
{
    double expenses[4];
    fill(expenses);
    show(expenses);

    system("pause");
    return 0;
}

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

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

编程练习8.b

#include <iostream>
using namespace std;

const int Season = 4;
const char* Snames[Season] = {"Spring", "Summer","Fall","Winter"};

struct expenses {
    double ex[];
};
void fill(expenses);
void show(expenses);

int main()
{
    expenses Sexpense;
    fill(Sexpense);
    show(Sexpense);

    system("pause");
    return 0;
}

void fill(expenses e)
{
    for (int i = 0; i < Season; i++)
    {
        cout << "Enter " << Snames[i] << " expenses: ";
        cin >> e.ex[i];
    }
}

void show(expenses e)
{
    double total = 0;
    cout << "\nEXPENSES\n";
    for (int i = 0; i < Season; i++)
    {
        cout << Snames[i] << ": $" << e.ex[i] << endl;
        total +=e.ex[i];
    }
    cout << "Total Expenses: $" << total << endl;
}

编程练习9

#include <iostream>
using namespace std;

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

int getinfor(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)
     {
         cin.clear();
         while(cin.get() != '\n')
             continue;
         cout << "bad inout"<<endl;
         cout << "Enter class size: ";
         cin >> class_size;
     }

    student *ptr_stu = new student[class_size];
    int entered = getinfor(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"<<endl;

    system("pause");
    return 0;
}

int getinfor(student pa[], int n)
{   

    int count=0;
    for (int i=0; i<n;i++)
    {
        cin.ignore(1024,'\n');
         //while(cin.get() != '\n')
            // continue;
        cout << "Enter the full name of student #" << i+1 << " : ";
        cin >> pa[i].fullname;
        if(cin.get()=='\n')
            break;  
        cout << "Enter the hobby of student #" << i+1<<" : ";
        cin >> pa[i].hobby;
        cout << "Enter the ooplevel of student #" << i+1<<" : ";
        cin >> pa[i].ooplevel;  
        count++;
    }
    return count;
}

void display1(student st)
{
    cout << "the full name of this student is " << st.fullname<<endl;
    cout << "the hobby of this student is " << st.hobby<<endl;
    cout << "the ooplevel of this student is " << st.ooplevel<<endl;
}
void display2(const student *ps)
{
    cout << "the full name of this student is " << ps->fullname<<endl;
    cout << "the hobby of this student is " << ps->hobby<<endl;
    cout << "the ooplevel of this student is " << ps->ooplevel<<endl;
}
void display3(const student pa[], int n)
{
    for(int i=0;i<n;i++)
    {
        cout << "the full name of # "<<i+1<<" student is " << pa[i].fullname<<endl;
        cout << "the hobby of # "<<i+1<<" student is " << pa[i].hobby<<endl;
        cout << "the ooplevel of # "<<i+1<<" student is " << pa[i].ooplevel<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值