c + + p r i me r p l u s ( 第
六版)课后编程练习答
案
六版)课后编程练习答
案
第二章:开始学习C++
〃ex2.1--display your n ame and address #in clude<iostream>
int main(v oid)
{
using n amespace std;
cout<<"My n ame is liao chu ngua ng and I live in hunan che nzhou.\”
kilogram〃ex2.2--convert the furlong units to yard uints扌把浪单位换位码单位 #in clude<iostream>
double fur2yd(double);
int main()
{
using n amespace std;
cout<<"e nter the dista nee measured by furl ong un its:"; double fur;
cin»fur;
cout<<"c onvert the furlo ng to yard"<<e ndl;
double yd;
yd=fur2yd(fur);
coutvvfurvv" furlong is "<<yd<<" yard"<<e ndl;
return 0;
}
double fur2yd(double t)
{
return 220*t;
}
〃ex2.3-每个函数都被调用两次
#in clude<iostream>
void mice();
void see();
using n amespace std;
int main()
{
mice();
mice();
see();
see();
return 0;
}
void mice()
{
cout«"three bli nd mice"«e ndl;
}
void see()
{
cout<<"see how they run"<<en dl;
}
〃ex2.4
#in clude<iostream>
int mai n()
{
using n amespace std;
cout<<"E nter your age:";
int age;
cin> >age;
in t mon th;
mon th=age*12;
coutvvagevv" years is "<<mon th<<" mon ths"<<e ndl;
return 0;
}
〃ex2.5---convert the Celsius valve to Fahre nheit value
#in clude<iostream>
double C2F(double);
int main()
{
using n amespace std;
cout«"please en ter a Celsius value:";
double C;
cin> >C;
double F;
F=C2F(C);
coutvvCvv" degrees Celsius is "<<F<<" degrees Fahre nheit."«e ndl; return 0;
}
double C2F(double t)
{
return 1.8*t+32;
}
〃ex2.6---convert the light years valve to astronomical units-把光年转换为天文单位
#in clude<iostream>
double conv ert(double);//函数原型
int main()
{
using n amespace std;
cout<<"E nter the nu mber of light years:";
double light_years;
cin> >light_years;
double astro_ un its;
astro_ un its=co nv ert(light_years);
cout<<light_years<<" light_years = "<<astro_u ni ts<<" astr ono mical un its."v<e ndl; return 0;
}
double conv ert(double t)
{
return 63240*t;//1 光年=63240 天文单位
}
〃ex2.7--显示用户输入的小时数和分钟数
#in clude<iostream>
void show();
mai n()
{
using n amespace std;
show();
return 0;
}
void show()
{
using n amespace std;
int h,m;
cout<<"e nter the nu mber of hours:"; cin> >h;
cout<<"e nter the nu mber of minu tes:";
cin>>m; coutvv"Time:"v<hvv":"vvmvve ndl;
第三章:处理数据
〃ex3.1—将身高用英尺(feet)和英寸(inch)表示
#in clude<iostream> const int in ch_per_feet=12;〃 cons常量--1feet=12i nches--1 英尺=12 英寸
int main() {
using n amespace std;
cout<<"please en ter your height in inches: \b\b\b";〃 \b表示为退格字符 int ht_in ch;
cin> >ht_i nch;
int ht_feet=ht_i nch/i nch_per_feet;//取商 int rm_i nch=ht_i nch%i nch_per_feet;〃取余
cout<<"your height is "<<ht_feet<<" feet,a nd " <<rm_i nch<< "in ches\n";
return 0;
}
//ex3.2--计算相应的body mass index (体重指数)
#in clude<iostream>
const int in ch_per_feet=12;
const double meter_per_i nch=0.0254;
const double poun d_per_kilogram=2.2;
int main()
{
using n amespace std;
cout<<"Please en ter your height:"<<e ndl;
cout«"First,enter your height of feet part (输入你身高的英尺部分):_\b";
int ht_feet;
cin> >ht_feet;
cout«"Seco nd,e nter your height of inch part (输入你身高的英寸部
分):_\b";
int ht_in ch;
cin> >ht_i nch;
cout«"Now,please en ter your weight in pound: \b\b\b";
double wt_po und;
cin> >wt_po und;
int in ch;
in ch=ht_feet*i nch_per_feet+ht_i nch;
double ht_meter;
ht_meter=i nch*meter_per_i nch;
double wt_kilogram;
wt_kilogram=wt_po un d/po un d_per_kilogram; cout«e ndl;
cout<<"Your pensonal body in formatio n as follows:"<<e ndl;
cout«"身高:"<<inch<<"(英尺 inch)\n"<<"身高:"<<ht_meter<<"(米
meter)\n"
<<"体重:"<<wt_kilogram<<"(千克 kilogram八n";
double BMI;
BMI=wt_kilogram/(ht_meter*ht_meter);
cout<<"your Body Mass In dex(体重指数)is "<<BMI<<e ndl;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论