C.Primer.Plus(第六版)第七章 编程练习

#include<iostream>
#include<string>
using namespace std;

double program_7_1(double x,double y);
int pro_7_2_set_arr(double [],int size);
void pro_7_2_show_arr(double *,int size);
double pro_7_2_average_arr(double arr[],int size);
long double probability(unsigned t_numbers,unsigned t_picks,unsigned s_numbers,unsigned s_picks);
struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};
void pro_7_3_a(box stru);
void pro_7_3_b(box* stru);
long double factorial(int n);
int pro_7_6_Fill_array(double arr[],int size);
void pro_7_6_Show_array(double arr[],int size);
void pro_7_6_Reverse_array(double arr[],int size);
double* pro_7_7_fill_array(double *arr,int limit);
void pro_7_7_show_array(const double* ps_beg,const double* ps_end);
void pro_7_7_revalue(double r,double* ps_beg,double* ps_end);
const char* season[4]={"Spring","Summer","Fail","Winter"};
void pro_7_8_a(double arr[],int size);
struct str_pro
{
    double str_dou[4];
};
void pro_7_8_b(str_pro xx,int size);
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);
double calculate(double d1,double d2,double (*pm)(double d3,double d4));
double add(double x,double y);
double mul(double x,double y);
double sub(double x,double y);
int main()
{
/*  //7_1 
    double x,y;
    int err_set=0;
    cout<<"Please enter two number(enter number 0 to quit):";
    while(cin>>x>>y)
    {   
        if(x!=0&&y!=0)
        {
            cout<<"The harmonic average is "<<program_7_1(x,y)<<endl;
            cout<<"Please input next two number:";
        }
        else
        {   
            cout<<"The input data contains 0,end.\n ";
            err_set++;//用于判断正常结束还是类型不匹配而结束程序。
            break;
        }
    }
    if(err_set==0)
        cout<<"The input data type dismatch.\n";
*/
/*  //7.2
    double golf_score[10];
    int size = pro_7_2_set_arr(golf_score,10);
    pro_7_2_show_arr(golf_score,size);
    double average = pro_7_2_average_arr(golf_score,size);
    cout<<"\n"<<average<<endl;
*/
/*  //7.3
    box pro_box={"baseball",2.1f,3,2.3f,32};
    pro_7_3_a(pro_box);
    pro_7_3_b(&pro_box);
*/
/*  //7.4
    double total,t_choices,specially,s_choices;
    cout<<"Enter the total number of choices on the game card and\nthe number of picks allowed:";
    if((cin>>total>>t_choices>>specially>>s_choices)&&total>=t_choices&&specially>=s_choices)
    {
        cout<<"Your have one chance in ";
        cout<<probability(total,t_choices,specially,s_choices);
        cout<<" of wining.\n";      
    }   
    cout<<"bye!\n";
*/
/* //7.5
//  long double x = factorial(-1); 函数将返回-1.#IND/nan,一般它们来自于任何未定义结果(非法)的浮点数运算。这里对一个负数求阶乘即非法。
    long double x=factorial(5);
    cout<<x<<endl;
*/
/*  //7.6
    double pro_7_6[10];
    int size=pro_7_6_Fill_array(pro_7_6,10);
    pro_7_6_Show_array(pro_7_6,size);
    pro_7_6_Reverse_array(pro_7_6,size);
    pro_7_6_Show_array(pro_7_6,size);
*/
/*  //7.7
    double pro_7_7[10];
    double* pro_end=pro_7_7_fill_array(pro_7_7,10);
    pro_7_7_show_array(pro_7_7,pro_end);
    pro_7_7_revalue(1.2,pro_7_7,pro_end);
    pro_7_7_show_array(pro_7_7,pro_end);
*/
/*  //7.8_a
    double pro_7_8[4];
    pro_7_8_a(pro_7_8,4);
*/
/*  //7.8_b
    str_pro str_test={2,3,4,5};
    pro_7_8_b(str_test,4);
*/
/*  // 7.9
    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";
*/
/*  //7.10_a
    double q = calculate(2.3,10.2,add);
    cout<<q<<endl;
*/
/*  //7.10_b
    double (*pf[3])(double ,double )={add,mul,sub};
    double x,y,q;
    while(cin>>x>>y)
    {
        for(int i=0;i<3;i++)
        {
            q = calculate(x,y,pf[i]);
            cout<<q<<endl;  
        }
    }
*/
    return 0;
}
/* //7.1
double program_7_1(double x,double y)
{
    return 2.0*x*y/(x+y);
}
*/
/* //7.2
int pro_7_2_set_arr(double arr[],int size)
{
    int true_size=0;
    for(int i=0;i<size;i++)
    {
        cout<<"Please input the score of arr["<<i<<"](input dismatch data to quit):";
        if(cin>>arr[i])
            true_size++;
        else
            break;
    }
    return true_size;//将数组的真实长度返回以便下面的函数显示和求平均值。
}
void pro_7_2_show_arr(double arr[],int size)
{
    for(int j=0;j<size;j++)
    {
        cout<<arr[j]<<",";
    }
}
double pro_7_2_average_arr(double arr[],int size)
{
    double sum=0;
    for(int k=0;k<size;k++)
    {
        sum+=arr[k];
    }
    return sum/size;
}
*/
/* //7.3
void pro_7_3_a(box stru)
{
    cout<<stru.maker<<"\t"<<stru.length<<"\t"<<stru.height<<"\t"<<stru.volume<<"\t"<<stru.width<<endl;
}
void pro_7_3_b(box* stru)
{
    stru->volume=stru->height*stru->length*stru->width;
    cout<<"box volume:"<<stru->volume<<endl;
}
*/
/* //7.4
long double probability(unsigned t_numbers,unsigned t_picks,unsigned s_numbers,unsigned s_picks)
{
    long double result = 1.0;
    long double tn,sn;
    unsigned tp,sp;
    for(tn=t_numbers,tp=t_picks;tp>0;tn--,tp--)
        result=result*tn/tp;
    for(sn=s_numbers,sp=s_picks;sp>0;sn--,sp--)
        result=result*sn/sp;
    return result;
}   
*/
/* //7.5 实现阶乘
long double factorial(int n)
{
    if(n>0)
    {
        return n*factorial(n-1);
    }
    else if(n==0)
        return 1;
    else
        cout<<"Bad input data.";
}
*/
/* //7.6
int pro_7_6_Fill_array(double arr[],int size)
{
    int true_size=0;
    for(int i=0;i<size;i++)
    {
        cout<<"Please input  double type data to arr["<<i<<"]:";
        if(cin>>arr[i])
            true_size++;
        else
            break;
    }
    return true_size;
}
void pro_7_6_Show_array(double arr[],int size)
{
    for(int j=0;j<size;j++)
        cout<<arr[j]<<"\t";
    cout<<endl;
}
void pro_7_6_Reverse_array(double arr[],int size)
{
    double temp=0;
    for(int begin=1,end=size-2;end>begin;end--,begin++)
    {
        temp=arr[begin];
        arr[begin]=arr[end];
        arr[end]=temp;
    }       
}
*/
/* //7.7
double* pro_7_7_fill_array(double *arr,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 input,input process terminated.\n";
            break;
        }
        else if(temp<0)
            break;
        *(arr+i)=temp;
    }
    return arr+i;
}
void pro_7_7_show_array(const double* ps_beg,const double* ps_end)
{
    for(;ps_beg<ps_end;ps_beg++)//不能取等号,ps_end指向数组末端下一个空间。
        cout<<*ps_beg<<"\t";
}
void pro_7_7_revalue(double r,double* ps_beg,double* ps_end)
{
    for(;ps_beg<ps_end;ps_beg++)
        *ps_beg*=r;
}
*/
/* //7.8_a
void pro_7_8_a(double arr[],int size)
{   
    int total=0;
    for(int i=0;i<size;i++)
    {
        cout<<"Enter "<<season[i]<<" expenses:";
        cin>>arr[i];
        total+=arr[i];
    }
    for(int j=0;j<size;j++)
    {
        cout<<season[j]<<": $"<<arr[j]<<endl;
    }   
}
*/
/*//7.8_b
void pro_7_8_b(str_pro xx,int size)
{
    int total=0;
    for(int i=0;i<size;i++)
    {
        cout<<"Enter "<<season[i]<<" expenses:";
        cin>>xx.str_dou[i];
        total+=xx.str_dou[i];
    }
    for(int j=0;j<size;j++)
    {
        cout<<season[j]<<": $"<<xx.str_dou[j]<<endl;
    }   
    cout<<total<<endl;
}
*/
/* // 7.9 分别使用传值传递,传址和指向数组的指针操作结构体。
int getinfo(student pa[],int n)
{
    int count;
    for(count=0;count<n;count++)
    {
        cout<<"Enter fullname:";
        cin.getline(pa[count].fullname,SLEN);
        cout<<"Enter hobby:";
        cin.getline(pa[count].hobby,SLEN);
        cout<<"Enter opplevel:";
        cin>>pa[count].ooplevel;
        cin.get();
    }
    return count;
}
void display1(student st)
{
    cout<<"fullname: "<<st.fullname<<endl;
    cout<<"hobby: "<<st.hobby<<endl;
    cout<<"ooplevel: "<<st.ooplevel<<endl;
}
void display2(const student* ps)
{
    cout<<"fullname: "<<ps->fullname<<endl;
    cout<<"hobby: "<<ps->hobby<<endl;
    cout<<"ooplevel: "<<ps->ooplevel<<endl;
}
void display3(const student pa[],int n)
{
    for(int i=0;i<n;i++)
    {
    cout<<"fullname: "<<pa[i].fullname<<endl;
    cout<<"hobby: "<<pa[i].hobby<<endl;
    cout<<"ooplevel: "<<pa[i].ooplevel<<endl;
    }
}
*/
/*//7.10_a
double add(double x,double y)
{
    return x+y;
}
double calculate(double d1,double d2,double (*pm)(double d3,double d4))
{
    return (*pm)(d1,d2); 
}
*/
/* //7.10_b
double calculate(double d1,double d2,double (*pm)(double d3,double d4))
{
    return (*pm)(d1,d2); 
}
double add(double x,double y)
{
    return x+y;
}
double mul(double x,double y)
{
    return x*y;
}
double sub(double x,double y)
{
    return x-y;
}
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值