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