C++ Primer Plus 第三四章习题解答

1.

#include<iostream>

 

usingnamespacestd;

constintinch_feet=12;

 

intmain()

{

 

    cout<<"Pleaseinputyourheightininches:___\b\b\b";

    intffeet;

    cin>>ffeet;

    intfinch;

    finch=ffeet/inch_feet;

    ffeet=ffeet%inch_feet;

    cout<<"yourheightis"<<finch<<"inchesand"<<ffeet<<"feets."<<endl;

    return0;

}

 

2.

#include<iostream>
 
usingnamespacestd;
 
constintinch_to_feet=12;
constfloatfeet_to_m=0.0254;
constfloatkg_to_pounds=2.2;
 
intmain()
{
    intfinch,ffeet;
    doublefpound;
    cout<<"ThisisaprogramtogiveyourBMI"<<endl;
    cout<<"pleaseinputyourheightininchesandfeet:"<<endl;
    cout<<"inches:__\b\b";
    cin>>finch;
    cout<<"feet:__\b\b";
    cin>>ffeet;
    cout<<"Nowpleaseinputyourweighthinpounds:__\b\b";
    cin>>fpound;
 
    doublefh_m;
    doublefkg;
    fh_m=(finch*inch_to_feet+ffeet)*feet_to_m;
    fkg=fpound/2.2;
 
    doublebmi;
    bmi=fkg/(fh_m*fh_m);
    cout<<endl;
    cout<<"yourBMIis"<<bmi<<endl;
    cout<<"Thanksforyouruse!"<<endl;
 
 
    return0;
}
 

其他的类似

 

第四章

         1.

#include<iostream>

 

usingnamespacestd;

structPersoninfo

{

    charfirstname[20];

    charlastname[20];

    chargrade;

    intage;

};

voiddisplay(constPersoninfo&per);

intmain()

{

    Personinfoone;

 

    cout<<"Whatisyourfirstname?";

    cin.getline(one.firstname,20);

    cout<<endl<<"Whatisyourlastname?";

    cin.getline(one.lastname,20);

    cout<<endl<<"Whatyourgrade?";

    cin>>one.grade;

    cout<<endl<<"Whatisyourage?";

    cin>>one.age;

 

    display(one);

 

    return0;

}

voiddisplay(constPersoninfo&per)

{

    cout<<endl<<"Name:"<<per.lastname<<","<<per.firstname;

    cout<<endl<<"Grade:"<<(char)(per.grade+1)\;

    cout<<endl<<"Age:"<<per.age;

 

}

 

 

2.

#include<iostream>

 

usingnamespacestd;

structPersoninfo

{

    stringfirstname;

    stringlastname;

    chargrade;

    intage;

};

voiddisplay(constPersoninfo&per);

intmain()

{

    Personinfoone;

 

    cout<<"Whatisyourfirstname?";

    getline(cin,one.firstname);

    cout<<endl<<"Whatisyourlastname?";

    getline(cin,one.lastname);

    cout<<endl<<"Whatyourgrade?";

    cin>>one.grade;

    cout<<endl<<"Whatisyourage?";

    cin>>one.age;

 

    display(one);

 

    return0;

}

voiddisplay(constPersoninfo&per)

{

    cout<<endl<<"Name:"<<per.lastname<<","<<per.firstname;

    cout<<endl<<"Grade:"<<(char)(per.grade+1);

    cout<<endl<<"Age:"<<per.age;

 

}

 

 

3.

#include<iostream>

#include<cstring>

usingnamespacestd;

 

 

intmain()

{

    charfirstname[20];

    charlastname[20];

    charfullname[42];

    cout<<"Enteryourfirstname:";

    cin.getline(firstname,20);

    cout<<endl<<"Enteryourlastname:";

    cin.getline(lastname,20);

    strcpy(fullname,lastname);

    //strcpy(fullname,",");  //copy擦除原来的字符串

    strcat(fullname,",");

    strcat(fullname,firstname);

 

    cout<<endl<<"Hereistheinformationinasinglestring:"<<fullname;

 

 

    return0;

}

 

 

4.

#include<iostream>

 

usingnamespacestd;

 

 

intmain()

{

    stringfirstname;

    stringlastname;

    stringfullname;

    cout<<"Enteryourfirstname:";

    getline(cin,firstname);

    cout<<endl<<"Enteryourlastname:";

    getline(cin,lastname);

    fullname=lastname+","+firstname;

 

    cout<<endl<<"Hereistheinformationinasinglestring:"<<fullname;

 

 

    return0;

}

 

 

5.

#include<iostream>

 

usingnamespacestd;

 

structCandyBar

{

    stringName;

    doubleweighth;

    intKluli;

};

voiddisplay(constCandyBar&);

intmain()

{

    CandyBarsnack={"MochaMunch",2.3,350};

 

    display(snack);

 

    return0;

}

 

voiddisplay(constCandyBar&candy)

{

    cout<<endl<<"TheBrandis:"<<candy.Name;

    cout<<endl<<"Theweightis:"<<candy.weighth;

    cout<<endl<<"Thekululiis:"<<candy.Kluli;

}

 

 

 

6.

#include<iostream>

 

usingnamespacestd;

 

structCandyBar

{

    stringName;

    doubleweighth;

    intKluli;

};

voiddisplay(constCandyBar&);

intmain()

{

    CandyBarsnack[3]={{"MochaMunch",2.3,350},{"Fanlei",3.3,450},{"Blingjie",4.3,550}};

 

    display(snack[0]);

    display(snack[1]);

    display(snack[2]);

 

    return0;

}

 

voiddisplay(constCandyBar&candy)

{

    cout<<endl<<"TheBrandis:"<<candy.Name;

    cout<<endl<<"Theweightis:"<<candy.weighth;

    cout<<endl<<"Thekululiis:"<<candy.Kluli;

}

 

 

 

9.

#include<iostream>

 

usingnamespacestd;

 

structCandyBar

{

    stringName;

    doubleweighth;

    intKluli;

};

voiddisplay(constCandyBar&);

intmain()

{

 

    CandyBar*p=newCandyBar[3];

    *p={"MochaMunch",2.3,350};

    *(p+1)={"Fanlei",3.3,450};//c++11可以这样列表初始化,若不允许,则各个元素分别赋值

    *(p+2)={"Blingjie",4.3,550};

    display(p[0]);

    display(p[1]);

    display(p[2]);

 

    delete[]p;

    return0;

}

 

voiddisplay(constCandyBar&candy)

{

    cout<<endl<<"TheBrandis:"<<candy.Name;

    cout<<endl<<"Theweightis:"<<candy.weighth;

    cout<<endl<<"Thekululiis:"<<candy.Kluli;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值