完整word版c++primerplus中文版第六版源代码.docx
《完整word版c++primerplus中文版第六版源代码.docx》由会员分享,可在线阅读,更多相关《完整word版c++primerplus中文版第六版源代码.docx(33页珍藏版)》请在冰豆网上搜索。
完整word版c++primerplus中文版第六版源代码
C++primerplus中文版第六版源代码
第二章到第四章,后续继续更新………
第二章
1:
#include
voidmain()
{
usingnamespacestd;
intcarrots;
carrots=25;
cout<<”Ihave";
cout〈〈carrots;
cout〈〈"carrots.”;
cout<〈endl;
carrots=carrots-1;
cout〈〈"Crunch,crunch。
NowIhave”<〈carrots<<”carrots"<〈endl;
}
2:
#include〈iostream>
intstonetolb(int);
intmain()
{
usingnamespacestd;
intstone;
cout<〈”Entertheweightinstone:
”;
cin>〉stone;
intpounds=stonetolb(stone);
cout〈〈stone<〈”stone=";
cout〈return0;
}
intstonetolb(intsts)
{
return14*sts;
}
3:
#include〈iostream>
voidmain()
{
usingnamespacestd;
intcarrots;
carrots=25;
cout<<"Howmanycarrotsdoyouhave?
”<cin>〉carrots;
cout<<”Herearetwomore。
”;
carrots=carrots+2;
cout<<”Nowyouhave"<"<//下两行专门测试cin.get()
cin.get();
cin.get();
}
4:
#includeusingnamespacestd;
voidmain()
{
cout<<"ComeupandC++mesometime.”;
cout〈〈endl;
cout<〈”Youwon'tregretit!
"<}
5#include〈iostream>
voidsimon(int);
intmain()
{
usingnamespacestd;
simon(3);
cout<〈"Pickaninteger:
”;
intcount;
cin〉>count;
simon(count);
cout<<”Done!
”〈return0;
}
voidsimon(intn)
{
usingnamespacestd;
cout<<”Simonsaystouchyourtoes"〈〈n<<”times。
"<〈endl;
}
6:
#include〈iostream〉
#includevoidmain()
{
usingnamespacestd;
doublearea;
cout<<"Enterthefloorarae,insquarefeet,ofyourhome:
";
cin>>area;
doubleside;
side=sqrt(area);
cout〈<"That'stheequivalentofasquare"〈〈〈”feettotheside."〈cout<〈"Howfascinating!
"〈}
第三章
1:
#include
#include
usingnamespacestd;
intmain()
{
intn_int=INT_MAX;
shortn_short=SHRT_MAX;
longn_long=LONG_MAX;
cout<〈”intis”<cout〈〈"shortis”〈〈sizeofn_short<<"bytes。
”<〈endl;
cout〈<"longis"<"〈〈endl〈cout<〈”Maximumvalues:
"<〈endl;
cout〈<”int:
"〈〈n_int〈cout<〈”short:
”<cout〈<”long:
"〈cout<<"Minimumintvalue=”〈cout<〈"Bitsperbyts=”<return0;
}
2:
#include〈iostream>
#include〈climits>
#defineZERO0
usingnamespacestd;
intmain()
{
shortsam=SHRT_MAX;
unsignedshortsue=sam;
cout<<”samhas”〈〈sam〈<”dollarsandsuehas"<cout<〈”dollarsdeposited."<〈endl
<<"Add$1toeachaccount.”<〈endl<〈"Now";
sam=sam+1;
sue=sue+1;
cout<<”Samhas"〈〈sam〈<"dollarsandsuehas”〈〈sue;
cout〈<"dollarsdeposited。
\npoorsam!
”〈sam=ZERO;
sue=ZERO;
cout〈<"samhas"<cout〈<”dollarsdeposited."<〈endl;
cout<<”Take$1fromeachaccount。
”〈〈endl<〈”Now”;
sam=sam-1;
sue=sue-1;
cout〈〈”samhas"<〈sam<〈"dolarsandsuehas"<cout<<"dollarsdeposited.”〈"〈return0;
}
3:
#includeusingnamespacestd;
voidmain()
{
intchest=42;
intwaist=0x42;
intinseam=042;
cout〈〈"Monsieurcutsastrikingfigure!
\n";
cout<〈"chest=”<cout〈〈"waist="<〈waist<〈”(0x42inhex)\n”;
cout<〈"inseam=”<}
4:
#include〈iostream〉
usingnamespacestd;
voidmain()
{
intchest=42;
intwaist=42;
intinseam=42;
cout<<”Monsieurcutsastrikingfigure!
\n";
cout<<”chest="〈cout〈cout〈<"waist=”〈〈waist〈〈"(hexadecimalfor42)”〈cout<〈oct;
cout〈<”inseam="〈}
5:
#include〈iostream>
usingnamespacestd;
voidmain()
{
cout〈〈”\aoperation\"HyperHype\”isnowactivated!
\n";
cout<<"Enteryouragentcode:
__________\b\b\b\b\b\b\b\b";
longcode;
cin〉>code;
cout<〈"\aYouentered"<〈code〈〈"。
.。
\n";
cout〈<"\acodeverified!
proceedwithplanz3!
\n";
}
6:
#include
usingnamespacestd;
voidmain()
{
charch;
cout〈<"Enteracharacter:
”<〈endl;
cin>>ch;
cout<<”Hola!
";
cout<<”Thankyouforthe"<"<}
7:
#include〈iostream>
usingnamespacestd;
voidmain()
{
charch=’M’;
inti=ch;
cout〈<"TheASCIIcodefor"<〈ch<〈”is”〈
cout<<"Addonetothecharactercode:
”<ch=ch+1;
i=ch;
cout<<”TheASCIIcodefor”〈〈ch〈〈”is”〈〈i〈cout〈<”Displayingcharchusingcout。
put(ch):
";
cout。
put(ch);
cout.put(’!
’);
cout<〈endl<〈"Done"〈}
8:
#includeusingnamespacestd;
voidmain()
{
cout.setf(ios_base:
:
fixed,ios_base:
:
floatfield);//控制cout显示的形式
floattub=10。
0/3.0;
doublemint=10。
0/3.0;
constfloatmillion=1.0e6;
cout〈〈"tub="<cout<<”,amilliontubs=”<cout<<",\nandtenmilliontubs=";
cout<〈10*million*tub<cout〈〈”mint="〈cout<〈million*mint〈〈endl;
}
9:
#include〈iostream〉
usingnamespacestd;
voidmain()
{
floata=2.34e+22f;
floatb=a+1。
0f;
cout<<"a=”〈〈a<cout<〈"b—a=”〈〈b-a〈〈endl;
}
10:
#include〈iostream〉
usingnamespacestd;
voidmain()
{
doublehats,heads;//或者是float
cout。
setf(ios_base:
:
fixed,ios_base:
:
floatfield);
cout〈<”Enteranumber:
";
cin〉>hats;
cout〈〈”Enteranothernumber:
";
cin〉〉heads;
cout〈<”hats="<〈hats〈〈";heads=”〈cout〈<"hats+heads="<cout<〈"hats—heads=”<〈hats—heads〈cout<<”hats*heads=”〈〈hats*heads<〈endl;
cout<<”hats/heads=”<}
11:
#include〈iostream〉
usingnamespacestd;
voidmain()
{
cout。
setf(ios_base:
:
fixed,ios_base:
:
floatfield);
cout〈〈"Integerdivision:
9/5="〈<9/5<cout<〈”Floating-pointdivision:
9.0/5。
0=”;
cout〈<9.0/5.0<〈endl;
cout<〈”Mixeddivision:
9.0/5="<<9。
0/5〈cout〈<"doubleconstants:
1。
e7/9。
0=”;
cout〈<1.e7/9.0〈cout〈〈”floatconstants:
1。
e7f/9。
0f=";
cout〈〈1。
e7f/9。
0f〈}
12:
#include
usingnamespacestd;
voidmain()
{
constintLbs_per_stn=14;
intlbs;
cout〈〈"Enteryourweightinpounds:
”;
cin〉〉lbs;
intstone=lbs/Lbs_per_stn;
intpounds=lbs%Lbs_per_stn;
cout<<<"stone,"〈〈pounds<<"pound(s)。
\n";
}
13:
#include〈iostream>
usingnamespacestd;
voidmain()
{
cout。
setf(ios_base:
:
fixed,ios_base:
:
floatfield);
floattree=3;
intguess(3。
9832);
intdebt=7。
2E12;
cout<〈"tree=”〈〈tree<〈endl;
cout〈<”guess=”<〈guess<cout<<”debt="<〈debt<〈endl;
}
14:
#includeusingnamespacestd;
voidmain()
{
intauks,bats,coots;
auks=19。
99+11.99;
bats=(int)19.99+(int)11.99;
coots=int(19。
99)+int(11.99);
cout〈<"auks="〈〈auks〈〈”,bats="<〈bats;
cout〈<”,coots=”〈〈coots<〈endl;
charch='Z’;
cout<<”Thecodefor”<cout〈cout<<”Yes,thecodeis”;
//cout〈(ch)〈}
第四章
1:
#include〈iostream>
usingnamespacestd;
voidmain()
{
intyams[3];
yams[0]=7;
yams[1]=8;
yams[2]=6;
intyamcosts[3]={20,30,5};
cout〈〈"Totalyams=”;
cout<cout〈<"Thepackagewith”<〈yams[1]〈<"yamscosts";
cout<inttotal=yams[0]*yamcosts[0]+yams[1]*yamcosts[1];
total=total+yams[2]*yamcosts[2];
cout<〈"Thetotalyamexpenseis”〈〈total〈<"cents.\n”;
cout〈<"\nSizeofyamsarray=”<〈sizeofyams;
cout〈〈”bytes。
\n”;
cout〈<”Sizeofoneelement="〈〈sizeofyams[0];
cout<<"bytes.\n";
}
2:
#includeusingnamespacestd;
voidmain()
{
constintsize=15;
charname1[size];
charname2[size]=”C++owboy";
cout〈<"Howdy!
I’m”<〈name2;
cout〈〈”!
what’syourname?
\n”;
cin〉>name1;
cout<〈"Well,"〈〈name1<<",yournamehas”;
cout〈cout<<"inanarrayof"〈〈sizeofname1<<"bytes。
\n”;
cout<<"Yourinitialis"〈\n”;
name2[3]='\0';
cout<〈”Herearethefirst3charchtersofmyname:
”;
cout<}
3:
#include
usingnamespacestd;
voidmain()
{
constintarsize=20;
charname[arsize];
chardessert[arsize];
cout〈<”Enteryourname:
\n";
cin>>name;
cout<〈"Enteryourfavoritedessert:
\n”;
cin〉>dessert;
cout〈〈”Ihavesomedelicious”<cout<<”foryou,”<〈name<〈”。
\n";
}
4:
#include
usingnamespacestd;
voidmain()
{
constintarsize=20;
charname[arsize];
chardessert[arsize];
cout〈<"Enteryourname:
\n”;
cin.getline(name,arsize);
cout〈〈"Enteryourfavoritedessert:
\n";
cin。
getline(dessert,arsize);
cout<<"Ihavesomedelicious"<〈dessert;
cout〈<"foryou,"〈〈name<<”。
\n”;
}
5:
#include〈iostream〉
usingnamespacestd;
voidmain()
{
constintarsize=20;
charname[arsize];
chardessert[arsize];
cout〈<”Enteryourname:
\n";
cin。
get(name,arsize)。
get();
cout〈〈"Enteryourfavoritedessert:
\n";
cin.get(dessert,arsize);
cout〈〈"Ihavesomedelicious"〈〈dessert;
cout〈<"foryou,"<}
6:
#include#includeusingnamespacestd;
voidmain()
{
charcharr1[20];
charcharr2[20]="jaguar”;
stringstr1;
stringstr2=”panther”;
cout<<"Enterakindoffeline:
";
cin>〉charr1;
cout〈〈”Enteranotherkindoffeline:
";
cin>〉str1;
cout<<”Herearesomefelines:
\n";
cout〈<〈str1<〈"”<〈str2
<〈endl;
cout<〈”Thethirdletterin”<<〈charr2[2]<〈endl;
cout<<"Thethirdletterin”<〈str2〈〈”is”
<〈str2[2]<}
7:
#include#include
usingnamespacestd;
voidmain()
{
strings1="penguin";
strings2,s3;
cout〈<"Youcanassignonestringobjecttoanother:
s2=s1\n”;
s2=s1;
cout〈<”s1:
”<”〈cout<〈”Youcanassignac-stylestringtoastringobject.\n";
cout〈〈”s2=\"buzzard\”\n”;
s2="buzzard”;
cout〈<"s2:
”〈cout<〈"Youcanconcatenatestrings:
s3=s1