C++学习-Day-14

C++学习-Day-14

一、编程练习

  1. 输入两个数,调用一个函数计算其调和平均数并返回给main函数,若这两数中出现0则重新输入。
#include<iostream>
using namespace std;
int main()
{
    int a,b;
    double harmn(int a,int b);
    cout<<"Please enter two digits\n";
    while(cin>>a>>b)
    {
        if( a*b!=0)
        cout<<"The harmonic mean of two digits is : "<<harmn(a,b);
        else
        {
          cout<<"two difits have at least one zero,please input again\n";
        }
    }
}
double harmn(int a,int b)
{
    return (2.0*a*b)/(a+b);
}

  1. 编写一个程序,请用户输入最多10次比赛成绩,分别编写3个数组函数进行输入、显示和计算平均值。
#include<iostream>
const int Size=10;
using namespace std;
int nu;
int main()
{
    double * input(double score[Size]);
    void display(double *p);
    double average(double *p);
    double score[Size];
    double * p=input(score);
    cout<<"Now display scores you have input\n";
    display(p);
    cout<<"The average score is : "<<average(p);
}
double * input(double score[Size])
{
    int i;
    cout<<"Please enter scores\n";
    for(i=0;i<Size;i++)
    {
        if(cin>>score[i])
        ;
        else
            break;
    }
    nu=i;
    return score;
}
void display(double *p)
{
    for(int i=0;i<nu;i++)
    cout<<p[i]<<endl;
}
double average(double *p)
{
    double total=0;
    for(int i=0;i<nu;i++)
        total+=p[i];
    return total/nu;
}

  1. 下面是一个结构声明:
struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};
编写一个函数用来按值传递一个box结构,并显示输出;
编写一个函数按地址传递一个box结构,并计算出volume;
程序中要调用以上两个程序。
#include<iostream>
using namespace std;
struct box
{
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};
int main()
{
    void volume(box * vv);
    void display(box vd);
    box mybox;
    cout<<"Enter the maker of your box : ";
    cin>>mybox.maker;
    cout<<"Enter the height of your box : ";
    cin>>mybox.height;
    cout<<"Enter the width of your box : ";
    cin>>mybox.width;
    cout<<"Enter the length of your box : ";
    cin>>mybox.length;
    box * p=&mybox;
    volume(p);
    display(mybox);
}
void volume(box * vv)
{
    vv->volume=vv->height*vv->width*vv->length;
}
void display(box vd)
{
    cout<<vd.maker<<endl
        <<vd.height<<endl
        <<vd.width<<endl
        <<vd.length<<endl
        <<vd.volume;
}

  1. 写一个彩票机制的小程序,号码分两段区选取,首先是一段给定号码域和选中号码个数,接着再选取第二段的特殊号码,只需给定号码域,只有在一段和二段号码全中才能获得一等奖,最终输出获得一等奖的概率。
#include<iostream>
using namespace std;
int main()
{
    long double probability(int number1,int choice,int number2);
    cout<<"Please input number1 and choice and number2:\n";
    int num,chi,nu;
    while(cin>>num>>chi>>nu && chi<=num)
    {
        cout<<"You have one chance "<<probability(num,chi,nu)<<"\nprobability of winning\n";
        cout<<"Next to input or inputting q to quit\n";
    }
}
long double probability(int number1,int choice,int number2)
{
    long double pr=1;
    for(int i=0;i<choice;i++)
        pr=pr*(i+1)/(number1-i);
    return pr/number2;
}


  1. 编写一个小程序,对于一个double型的数组,编写三个函数,接受数组名和数组长度,分别对数组进行填充、显示和顺序反转,其中填充函数返回输入有效数字的个数,反转函数反转数组中除第一个个最后一个的所有数字。
#include<iostream>
using namespace std;
const int Size=10;
int main()
{
    int Fill_array(double ar[],int siz);
    void Show_array(const double br[],int siz);
    void Reverse_array(double cr[],int siz);
    int n;
    double arr[Size];
    n=Fill_array(arr,Size);
    cout<<"Now display the array\n";
    Show_array(arr,n);
    Reverse_array(arr+1,n-2);
    cout<<"Now display the array reversed\n";
    Show_array(arr,n);
}
int Fill_array(double ar[],int siz)
{
    cout<<"Please enter "<<siz<<" number\n";
    int n=0;
    while(cin>>ar[n] && n<siz)
    {
        if(++n==siz)
            break;
    }
    cout<<"You have input "<<n<<" digits\n";
    return n;
}
void Show_array(const double br[],int siz)
{
    for(int i=0;i<siz;i++)
        cout<<br[i]<<endl;
}
void Reverse_array(double cr[],int siz)
{
    int a,mark=siz;
    while(mark)
    {
        for(int i=0;i<mark-1;i++)
        {
            a=cr[i+1];
            cr[i+1]=cr[i];
            cr[i]=a;
        }
        mark--;
    }
}

  1. 采用数组和结构两种形式完成程序清单7.15的工作。
#include<iostream>
using namespace std;
const int Season=4;
const char *sname[Season]={"Spring","Summer","Fall","Winter"};
struct exp
{
    double ar[Season];
};
int main()
{
    void Fill_array(double ex[Season]);
    void Display_array(double ex[Season]);
    void Fill_array1(exp *st);
    void Display_array1(exp *st);
    double expense[Season];
    cout<<"**********using array version**********\n";
    Fill_array(expense);
    Display_array(expense);
    cout<<"**********using structure version**********\n";
    exp expp;
    Fill_array1(&expp);
    Display_array1(&expp);
}
void Fill_array(double ex[Season])
{
    cout<<"input the expense of every season\n";
    for(int i=0;i<Season;i++)
        cin>>ex[i];
}
void Display_array(double ex[Season])
{
    for(int i=0;i<Season;i++)
        cout<<sname[i]<<": "<<ex[i]<<endl;
}
void Fill_array1(exp *st)
{
    cout<<"input the expense of every season\n";
    for(int i=0;i<Season;i++)
        cin>>st->ar[i];
}
void Display_array1(exp *st)
{
    for(int i=0;i<Season;i++)
        cout<<sname[i]<<": "<<st->ar[i]<<endl;
}


  1. 给出一个程序框架,请写出描述函数(p7-9)。
#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(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 enter = 0;
    for (int i = 0; i < n; i++)
    {
        cout << "Enter the infomation of student #" << i+1 << endl;
        cout << "Enter full name (blank line to quit): ";
        cin.getline(pa[i].fullname, SLEN);
        cout << "Enter hobby: ";
        cin.getline(pa[i].hobby, SLEN);
        cout << "Enter ooplevel: ";
        cin >> pa[i].ooplevel;
        while (cin.get() != '\n')
            continue;
        enter++;
    }
    return enter;
}
void display1(student st)
{
    cout<<"fullname:\t"<<st.fullname<<endl
        <<"hobby:\t\t"<<st.hobby<<endl
        <<"ooplevel:\t"<<st.ooplevel<<endl;
}
void display2(const student * ps)
{
    cout<<"fullname:\t"<<ps->fullname<<endl
        <<"hobby:\t\t"<<ps->hobby<<endl
        <<"ooplevel:\t"<<ps->ooplevel<<endl;
}
void display3(student pa[],int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<"fullname:\t"<<pa[i].fullname<<endl
            <<"hobby:\t\t"<<pa[i].hobby<<endl
            <<"ooplevel:\t"<<pa[i].ooplevel<<endl;
    }
}

  1. 设计一个名为calculate的函数,接受两个double值和一个指向add函数的函数指针,而被指的函数接受两个double值并返回一个double值,其中返回值为接受两个double值的和。程序使用循环每次接受两个数字,并使用calculate函数通过函数指针调用add函数。
#include<iostream>
using namespace std;
double add(double x,double y);
double calculate(double a,double b,double (*pf)(double x,double y));
int main()
{
    cout<<"Please input two digits\n";
    int x,y;
    double (*pf)(double x,double y)=add;
    while(cin>>x>>y)
    {
        cout<<x<<"+"<<y<<"="<<calculate(x,y,pf)<<endl;
        cout<<"Please input two digits(quit to 'q')\n";
    }
}
double add(double x,double y)
{
    return x+y;
}
double calculate(double a,double b,double (*pf)(double x,double y))
{
    return pf(a,b);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值