C++ primer plus 编程练习25答案.docx
《C++ primer plus 编程练习25答案.docx》由会员分享,可在线阅读,更多相关《C++ primer plus 编程练习25答案.docx(26页珍藏版)》请在冰豆网上搜索。
C++primerplus编程练习25答案
C++primerplus
第二章到第五章编程练习答案
第二章
1:
#include
#definemax10
usingnamespacestd;
voidmain()
{
charname[max],dizhi[max];
cout<<"请输入姓名:
";
cin>>name;
cout<<"请输入地址:
";
cin>>dizhi;
cout<<"姓名--->"<"<}
2:
#include
usingnamespacestd;
voidmain()
{
longjuli;
cout<<"请输入距离long(1long为220码):
";
cin>>juli;
cout<<"按照您输入的距离是:
"<}
3:
#include
usingnamespacestd;
voidblind()
{
cout<<"Threeblindmice\n";
}
voidrun()
{
cout<<"Seehowtheyrun\n";
}
voidmain()
{
for(inti=0;i<2;i++)
blind();
for(intj=0;j<2;j++)
run();
}
4:
#include
usingnamespacestd;
voidmonth(intage)
{
cout<<"该年龄一共包含"<\n";
}
voidmain()
{
intage;
cout<<"请输入年龄:
";
cin>>age;
month(age);
}
5:
#include
usingnamespacestd;
doublefahrenheit(doublecelsius)
{
return1.8*celsius+32.0;
}
voidmain()
{
doublecelsius;
cout<<"pleaseenteracelsiusvalue:
";
cin>>celsius;
cout<}
6:
#include
usingnamespacestd;
doubleastronomical(doublelight)
{
return63240*light;
}
voidmain()
{
doublelight;
cout<<"Enterthenumberoflightyears:
";
cin>>light;
cout<}
7:
#include
usingnamespacestd;
voiddisplay(inthours,intminutes)
{
cout<<"Time:
"<"<}
voidmain()
{
inthour,minute;
cout<<"pleaseinputthetimeofhour:
";
cin>>hour;
cout<<"pleaseinputthetimeofminute:
";
cin>>minute;
display(hour,minute);
}
第三章
1:
#include
usingnamespacestd;
constfloatdanwei=0.0833333;
voidiswap(intcun)
{
cout<<"您的身高为:
"<"<}
voidmain()
{
intcun;
cout<<"请输入英寸单位的身高(整数):
_______\b\b\b\b\b\b";
cin>>cun;
iswap(cun);
}
2:
#include
usingnamespacestd;
constdoubleyingchi=12;
constdoublebang=2.2;
constdoublememter=0.0245;
voidcaculate(doublechi,doublecun,doubleweight)
{
doubleBMI;
doubleyingcun,mi,qianke;
yingcun=cun+chi*yingchi;
mi=yingcun*memter;
qianke=weight/bang;
BMI=qianke/(mi*mi);
cout<<"您的BMI值为:
"<}
voidmain()
{
doublechi,cun,weight;
cout<<"请输入身高(以几英尺几英寸方式输入):
";
cin>>chi>>cun;
cout<<"请输入体重(以磅为单位):
";
cin>>weight;
caculate(chi,cun,weight);
}
3:
#include
usingnamespacestd;
voidmain()
{
doubledegrees,minutes,seconds,sum;
cout<<"Enteralatitudeindegrees,minutes,andseconds:
"<cout<<"First,enterthedegrees:
";
cin>>degrees;
cout<<"Next,entertheminutesofarc:
";
cin>>minutes;
cout<<"Finally,enterthesecondsofarc:
";
cin>>seconds;
sum=degrees+minutes/60+seconds/3600;
cout<}
4:
#include
usingnamespacestd;
constlongm=60;
constlongh=60;
constlongd=24;
intsumday(longseconds)
{
longhour,minute;
minute=seconds/m;
hour=minute/h;
returnhour/d;
}
intsumhour(longseconds,intday)
{
longminute;
seconds=seconds-day*d*h*m;
minute=seconds/m;
returnminute/h;
}
intsumminute(longseconds,intday,inthour)
{
seconds=seconds-(day*d*h*m+hour*h*m);
returnseconds/m;
}
intsumsecond(longseconds,intday,inthour,intminute)
{
returnseconds=seconds-(day*d*h*m+hour*h*m+minute*m);
}
voidmain()
{
longseconds;
intday,hour,minute,second;
cout<<"Enterthenumberofseconds:
";
cin>>seconds;
day=sumday(seconds);
hour=sumhour(seconds,day);
minute=summinute(seconds,day,hour);
second=sumsecond(seconds,day,hour,minute);
cout<}
5:
#include
usingnamespacestd;
voidmain()
{
doubleworld,us;
cout<<"Entertheworld'spopulation:
";
cin>>world;
cout<<"Enterthepopulationoftheus:
";
cin>>us;
doublebilv;
bilv=us/world;
cout<<"Thepopulationoftheusis"<}
6:
#include
usingnamespacestd;
voidmain()
{
floatmemter,jialun;
cout<<"以美国风格还是欧洲风格显示耗油量?
m为美国,o为欧洲!
"<cout<<"请输入(m或o):
";
charc;
cin>>c;
if(c=='m')
{
cout<<"请输入驱车里程(英里):
";
cin>>memter;
cout<<"请输入使用汽油量(加仑):
";
cin>>jialun;
cout<<"汽车耗油量为:
"<}
else
{
cout<<"请输入驱车里程(公里):
";
cin>>memter;
cout<<"请输入使用汽油量(升):
";
cin>>jialun;
floatofg;
ofg=(100*jialun)/memter;
cout<<"汽车耗油量为:
"<}
}
7:
include
usingnamespacestd;
voidmain()
{
cout<<"请输入欧洲风格的汽车耗油量(每100公里消耗的汽油量(升)):
";
floatofg;
cin>>ofg;
floatjialun;
jialun=ofg/3.875;
floathaoyou;
haoyou=62.14/jialun;
cout<<"转换成美国风格的耗油量(一加仑的里程,mpg):
"<}
第四章
1:
#include
#include
constintnum=10;
usingnamespacestd;
intmain()
{
cout<<"What'syourfirstname?
";
charfirst[num];
cin.getline(first,num);
cout<<"whst'syourlastname?
";
charlast[num];
cin>>last;
cout<<"whatlettergradedoyoudeserve?
";
chargrade;
cin>>grade;
cout<<"what'syourage?
";
intage;
cin>>age;
cout<<"-------------------------------------"<cout<<"Name:
"<cout<<"Grade:
"<cout<<"Age:
"<return0;
}
2:
#include
#include
usingnamespacestd;
intmain()
{
stringname,dessert;
cout<<"Enteryourname:
\n";
getline(cin,name);
cin.get();
cout<<"Enteryourfavoritedessert:
\n";
getline(cin,dessert);
cout<<"Ihavesomedelicious"<<<"foryou,"<return0;
}
3:
#include
#include
usingnamespacestd;
intmain()
{
cout<<"Enter,yourfirstname:
";
charfirst[10];
cin>>first;
cout<<"Enteryourlastname:
";
charlast[10];
cin>>last;
strcat(last,",");
strcat(last,first);
cout<<"Here'stheinformationinasinglestring:
"<return0;
}
4:
include
#include
usingnamespacestd;
intmain()
{
cout<<"Enter,yourfirstname:
";
stringfirst;
cin>>first;
cout<<"Enteryourlastname:
";
stringlast;
cin>>last;
last=last+",";
last=last+first;
cout<<"Here'stheinformationinasinglestring:
"<return0;
}
5:
#include
#include
usingnamespacestd;
structCandyBar
{
charbrand[20];
doubleweight;
longcalories;
};
intmain()
{
CandyBarsnack={"MochaMunch",2.3,350};
cout<cout<cout<return0;
}
6:
#include
#include
usingnamespacestd;
structCandyBar
{
charbrand[20];
doubleweight;
longcalories;
};
intmain()
{
CandyBarsnack[3]={{"MochaMunch",2.3,350},{"caorui",3.6,456},{"denger",4.7,877}};
for(inti=0;i<3;i++)
{
cout<<"-----------------------"<cout<cout<cout<}
cout<<"-----------------------"<return0;
}
7:
#include
#include
#include
usingnamespacestd;
structpizza
{
stringcompany;
doublediameter;
doubleweight;
};
intmain()
{
pizzap;
cout<<"Pleaseinputthecompanyofmanufacturepizza:
";
getline(cin,pany);
cout<<"Pleaseinputthediameterofpizza:
";
cin>>p.diameter;
cout<<"Pleaseinputtheweightofpizza:
";
cin>>p.weight;
cout<<"Name:
"<cout<<"Diameter:
"<
cout<<"Weight:
"<
return0;
}
8:
#include
#include
#include
usingnamespacestd;
structpizza
{
stringcompany;
doublediameter;
doubleweight;
};
intmain()
{
pizza*p=newpizza;
cout<<"Pleaseinputthediameterofpizza:
";
cin>>p->diameter;
cin.get();
cout<<"Pleaseinputthecompanyofmanufacturepizza:
";
getline(cin,p->company);
cout<<"Pleaseinputtheweightofpizza:
";
cin>>p->weight;
cout<<"Name:
"<company<<",andthecompanynameisformof"<company.size()<<"words."<cout<<"Diameter:
"<diameter<cout<<"Weight:
"<weight<return0;
}
9:
#include
#include
usingnamespacestd;
structCandyBar
{
charbrand[20];
doubleweight;
longcalories;
};
intmain()
{
CandyBar*snack=newCandyBar[3];
strcpy(snack->brand,"shanghai");
snack->weight=1.2;
snack->calories=7;
strcpy((snack+1)->brand,"beijing");
(snack+1)->weight=2.3;
(snack+1)->calories=8;
strcpy((snack+2)->brand,"guangzhou");
(snack+2)->weight=3.4;
(snack+2)->calories=9;
for(inti=0;i<3;i++)
{
cout<<"-----------------------"<cout<cout<cout<}
cout<<"-----------------------"<return0;
}
10:
#include
#include
usingnamespacestd;
intmain()
{
doublegrade[3];
cout<<"Pleaseinputthreegradesofrunning."<for(inti=0;i<3;i++)
{
cout<<"The"<
";
cin>>grade[i];
}
cout<<"一共跑了3次,平均成绩为:
"<<(grade[0]+grade[1]+grade[2])/3<<"码."<return0;
}
第五章
1:
#include
usingnamespacestd;
intmain()
{
intx,y;
cout<<"pleaseinputtwonumbers."<cin>>x>>y;
doublesum=0;
for(inti=x;i<=y;i++)
sum+=i;
cout<"<system("pause");
return0;
}
2:
#include
usingnamespacestd;
intmain()
{
longdoublejiechen=1;
for(inti=2;i<=100;i++)
jiechen=i*jiechen;
cout<<"100!
="<system("pause");
return0;
}
3: