c++ primer plus 第七章习题

C++ primer plus 第七章习题

#include <iostream>
using namespace std;
float sum(int,int);
int main()
{
    int a,b;
    cout<<"Please enter 2 numbers: \n";
    cin>>a>>b;
    while(a!=0&&b!=0)
    {
        cout<<sum(a,b);
        cout<<"\nPlease enter 2 numbers: \n";
        cin>>a>>b;
        
    }
    return 0;
    
}
float sum(int x,int y)
{
    return 2.0*x*y/(x+y);
}
#include <iostream>
using namespace std;
int fill_arr(int *);
void show_arr(int *,int);
float cal_arr(int *,int);
const int N=10;
int main()
{
    int score[N];
    int n;
    n=fill_arr(score);
    show_arr(score,n);
    cout<<"The average score is "<<cal_arr(score,n)<<endl;
}
int fill_arr(int *a)
{
    int truesize=0;
    for(int i=0;i<N;i++)
    {
        cout<<"Please enter score #"<<(i+1)<<": ";
        if(!(cin>>a[i]))
            break;
        else truesize++;
    }
    return truesize;
}
void show_arr(int *a,int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<"Score #"<<(i+1)<<": "<<a[i]<<endl;
    }
}
float cal_arr(int *a,int n)
{
    float ave=0;
    for(int i=0;i<n;i++)
    {
        ave+=a[i];
    }
    return ave/n;
}
#include <iostream>
using namespace std;
struct box {
    char maker[40];
    float height;
    float width;
    float length;
    double volume;
};
void calvolume(box *);
void showbox(box);
int main()
{
    box box1;
    cout<<"Please enter the maker:\n";
    for(int i=0;i<40;i++)
    {
        cin.get(box1.maker[i]);
        if(box1.maker[i]=='\n') break;
    }
        
    
    
    cout<<"Please enter the height:\n";
    cin>>box1.height;
    cout<<"Please enter the width:\n";
    cin>>box1.width;
    cout<<"Please enter the length:\n";
    cin>>box1.length;
    calvolume(&box1);
    showbox(box1);
}
void calvolume(box *a)
{
    a->volume=a->height*a->length*a->width;
}
void showbox(box a)
{
    cout<<a.maker;
    cout<<"height: "<<a.height<<endl;
    cout<<"width: "<<a.width<<endl;
    cout<<"length: "<<a.length<<endl;
    cout<<"volume: "<<a.volume<<endl;
}
#include <iostream>
using namespace std;
float prfield(int m,int n);
int main()
{
    int min1,max1,m,min2,max2;
    cout<<"Please input minimum number of field numbers. \n";
    cin>>min1;
    cout<<"And the maximum number.\n";
    cin>>max1;
    cout<<"How many numbers do you expect to input?\n";
    cin>>m;
    float pr1=prfield(m,max1-min1+1);
    cout<<"Please input minimum number of special numbers. \n";
    cin>>min2;
    cout<<"And the maximum number.\n";
    cin>>max2;
    float pr2=1/float(max2-min2+1);
    cout.setf(ios_base::fixed);
    cout<<pr1*pr2;
}

float prfield(int m,int n)
{
    float a=1,b=1,c=1;
    for(int i=n;i>0;i--)
    {
        a*=i;
    }
    for(int i=m;i>0;i--)
    {
        b*=i;
    }
    for(int i=n-m;i>0;i--)
    {
        c*=i;
    }
    return b*c/a;
}

 #include <iostream>
using namespace std;
long long factorial(int n);
int main()
{
    int n;
    cout<<"Please enter number:\n";
    cin>>n;
    cout<<n<<"!= "<<factorial(n);
}
long long factorial(int n)
{
    if(n==1||n==0)
        return 1;
    else return n*factorial(n-1);
}
#include <iostream>
using namespace std;
const int N=10;
int fill_array(double [],int);
void show_array(double [],int);
void reverse_array(double [],int);
int main()
{
    double arr[N];
    int size=fill_array(arr, N);
    show_array(arr, size);
    reverse_array(arr, size);
    show_array(arr, size);
    int temp;
    temp=arr[size-1];
    arr[size-1]=arr[0];
    arr[0]=temp;
    show_array(arr, size);
}
int fill_array(double a[],int n)
{
    int count=0,temp;
    cout<<"Please enter values of numbers(no more than 10 numbers): \n";
    for(int i=0;(i<n)&&(cin>>temp);i++)
    {
        a[i]=temp;
        count++;
    }
    return count;
}
void show_array(double a[],int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
}
void reverse_array(double a[],int n)
{
    int temp;
    for(int i=0;i<n/2;i++)
    {
        temp=a[n-1-i];
        a[n-1-i]=a[i];
        a[i]=temp;
    }
}
#include <iostream>
using namespace std;
const int Max=5;
double * fill_array(double * ar, double * pend);
void show_array(const double * ar, double * pend);
void revalue(double r, double ar[], double * pend);
int main()
{
    double properties[Max];
    double * pend=fill_array(properties, properties+Max);
    show_array(properties, pend);
    if(pend-properties>0)
    {
        cout<<"Enter revaluation factor: \n";
        double factor;
        while(!(cin>>factor))
        {
            cin.clear();
            if(cin.get()!='\n')
                continue;
            cout<<"Bad input! Please enter a number: \n";
        }
        revalue(factor, properties, pend);
        show_array(properties, pend);
    }
    
}
double * fill_array(double * ar, double * pend)
{
    double *pt,temp;
    int i=0;
    for(pt=ar;pt<pend;pt++)
    {
        cout<<"Enter value #"<<(i+1)<<": \n";
        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;
        i++;
    }
    return pt;
}
void show_array(const double * ar, double * pend)
{
    const double *pt;
    int i=0;
    for(pt=ar;pt<pend;pt++)
    {
        cout<<"Property #"<<(i+1)<<": $"<<*pt<<endl;
    }
}
void revalue(double r, double ar[], double * pend)
{
    double *pt;
    for(pt=ar;pt<pend;pt++)
    {
        *pt*=r;
    }
}

a


#include <iostream>
using namespace std;
const int seasons=4;
const char *snames[seasons] ={"spring","summer","autumn","winter"};
void fill(double *a);
void show(double *a);
int main()
{
    double expenses[seasons];
    fill(expenses);
    show(expenses);
    return 0;
}
void fill(double *a)
{
    for(int i=0;i<seasons;i++)
    {
        cout<<"Enter "<<snames[i]<<" expenses:\n";
        cin>>*(a+i);
    }
}
void show(double *a)
{
    cout<<"Expenses: \n";
    for(int i=0;i<seasons;i++)
    {
        cout<<snames[i]<<": "<<*(a+i)<<endl;
    }
}

b

#include <iostream>
using namespace std;
const int seasons=4;
const char *snames[seasons] ={"spring","summer","autumn","winter"};
struct season
{
    double expenses;
};
void fill(season *a);
void show(season *a);
int main()
{
    season _season[4];
    fill(_season);
    show(_season);
    return 0;
}
void fill(season *a)
{
    for(int i=0;i<seasons;i++)
    {
        cout<<"Enter "<<snames[i]<<" expenses:\n";
        cin>>a[i].expenses;
    }
}
void show(season *a)
{
    cout<<"Expenses: \n";
    for(int i=0;i<seasons;i++)
    {
        cout<<snames[i]<<": "<<a[i].expenses<<endl;
    }
}

#include <iostream>
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: \n";
    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<<"Please input student #"<<(i+1)<<
        " name, hobby and ooplevel respectively('\n' to separate): \n";
//        fullname input
        cin.getline(pa[i].fullname,SLEN);
//     hobby input
        cin.getline(pa[i].hobby,SLEN);
        
//        ooplevel input
        cin>>pa[i].ooplevel;
        getchar();
    }
    return i;
}
void display1(student st)
{
    cout<<st.fullname<<": "<<st.hobby<<", "<<st.ooplevel<<endl;
}
void display2(const student *ps)
{
    cout<<ps->fullname<<": "<<ps->hobby<<", "<<ps->ooplevel<<endl;
}
void display3(const student pa[], int n)
{
    cout<<"Student's number\tname\thobby\tooplevel\n";
    for(int i=0;i<n;i++)
    {
        cout<<"\t\t"<<(i+1)<<"\t\t\t"
        <<pa[i].fullname<<"\t\t"
        <<pa[i].hobby<<"\t\t"
        <<pa[i].ooplevel<<"\n";
    }
}
#include <iostream>
double calculate(double a, double b, double (*pfun)(double m,double n));
double add(double x, double y);
double _minus(double x, double y);
double multiply(double x, double y);
double division(double x, double y);
using namespace std;
int main()
{
//    funtion pointer arrays
    double (*pf[4])(double,double)=
    {
        add, _minus, multiply,division
    };
    cout<<"Please enter 2 numbers to do calculations: \n";
    int a,b;
    while((cin>>a)&&(cin>>b))
    {
        for(int i=0;i<4;i++)
        {
            cout<<calculate(a, b, pf[i])<<endl;
        }
    }
    return 0;
}
double add(double x, double y)
{
    return x+y;
}
double _minus(double x, double y)
{
    return x-y;
}
double multiply(double x, double y)
{
    return x*y;
}
double division(double x, double y)
{
    return x/y;
}
double calculate(double a, double b, double (*pfun)(double m,double n))
{
    return (*pfun)(a,b);
}

猜你喜欢

转载自blog.csdn.net/weixin_42365868/article/details/86611607