C++ Primer Plus6 第4单元程序清单Word文件下载.docx
《C++ Primer Plus6 第4单元程序清单Word文件下载.docx》由会员分享,可在线阅读,更多相关《C++ Primer Plus6 第4单元程序清单Word文件下载.docx(17页珍藏版)》请在冰豆网上搜索。
cstring>
constintSize=15;
charname1[Size];
charname2[Size]="
C++owboy"
Howdy!
I'
m"
name2<
!
whatisyourname?
\n"
cin>
>
name1;
Well,"
name1<
yournamehas"
strlen(name1)<
lettersandisstored\n"
inanarrayof"
sizeofname1<
bytes."
Yourinitialis"
name1[0]<
."
name2[3]='
\0'
Herearethefirst3charactersofmyname:
"
4.3instr1.cpp
constintArSize=20;
charname[ArSize];
chardessert[ArSize];
Enteryourname:
name;
Enteryourfavoritedessert:
dessert;
Ihavesomedelicious"
dessert<
foryou,"
name<
.\n"
4.4instr2.cpp
cin.getline(name,ArSize);
cin.getline(dessert,ArSize);
4.5instr3.cpp
cin.get(name,ArSize).get();
cin.get(dessert,ArSize).get();
4.6numstr.cpp
Whatyearwasyourhousebuilt?
intyear;
year;
Whatisitsstreetaddress?
charaddress[80];
cin.getline(address,80);
Yearbuilt:
year<
Address:
address<
Done!
4.7strtype1.cpp
string>
charcharr1[20];
charcharr2[20]="
jaguar"
stringstr1;
stringstr2="
panther"
Enterakindoffeline:
charr1;
Enteranotherkindoffeline:
str1;
Herearesomefelines:
charr1<
charr2<
str1<
str2<
Thethirdletterin"
is"
charr2[2]<
str2[2]<
\
4.8strtype2.cpp
strings1="
penguin"
strings2,s3;
Youcanassignonestringobjecttoanother:
s2=s1\n"
s2=s1;
s1:
s1<
"
s2:
s2<
YoucanassignaC-stylestringtoastringobject.\n"
s2=\"
buzzard\"
s2="
buzzard"
Youcanconcatenatestring:
s3=s1+s2\n"
s3=s1+s2;
s3:
s3<
Youcanappendstring.\n"
s1+=s2;
s1+=s2yieldss1="
s2+="
foraday"
s2+=\"
foraday\"
yieldss2="
4.9strtype3.cpp
str1=str2;
strcpy(charr1,charr2);
str1+="
paste"
strcat(charr1,"
juice"
);
intlen1=str1.size();
intlen2=strlen(charr1);
Thestring"
contains"
len1<
characters.\n"
len2<
characters."
4.10strtype4.cpp
charcharr[20];
stringstr;
Lengthofstringincharrbeforeinput:
strlen(charr)<
Lengthofstringinstrbeforeinput:
str.size()<
Enteralineoftext:
cin.getline(charr,20);
Youentered"
charr<
Enteranotherlineoftext:
getline(cin,str);
Youentered:
str<
Lengthofstringincharrafterinptu:
Lengthofstringinstrafterinput:
4.11structur.cpp
structinflatable
charname[20];
floatvolume;
doubleprice;
};
inflatableguest={"
GloriousGloria"
1.88,29.99};
inflatablepal={"
AudaciousArthur"
3.12,32.99};
Expandyourguestlistwith"
guest.name;
and"
pal.name<
!
Youcanhavebothfor$"
guest.price+pal.price<
4.12assgn.cpp
inflatablebouquet={"
sunflowers"
0.20,12.49};
inflatablechoice;
bouquet:
bouquet.name<
for$"
bouquet.price<
choice=bouquet;
choice:
choice.name<
choice.price<
4.13arrstruc.cpp
inflatableguests[2]=
{
{"
Bambi"
0.5,21.99},
Godzilla"
2000,565.99}
};
Theguest"
guests[0].name<
guests[1].name<
haveacombinedvolumeof"
guests[0].volume+guests[1].volume<
cubicfeet\n"
4.14address.cpp
intdonuts=6;
doublecups=4.5;
donutsvalue="
donuts;
anddonutsaddress="
&
donuts<
cupsvalue="
cups;
andcupsaddress="
cups<
4.15pointer.cpp
intupdates=6;
int*p_updates;
p_updates=&
updates;
Values:
updates="
updates<
*p_updates="
*p_updates<
Addresses:
p_updates="
p_updates<
*p_updates=*p_updates+1;
Nowupdates="
4.16init_ptr.cpp
inthiggens=5;
int*pt=&
higgens;
Valueofhiggens="
higgens<
Addressofhiggens="
Valueof*pt="
*pt<
Valueofpt="
pt<
4.17use_new.cpp
intnights=1001;
int*pt=newint;
*pt=1001;
nightsvalue="
nights<
:
location"
intvalue="
location"
double*pd=newdouble;
*pd=10000001.0;
doublevalue="
*pd<
location:
pd<
locationofpionterpd:
sizeofpt="
sizeof(pt)<
sizeof*pt="
sizeof(*pt)<
sizeofpd="
sizeof(pd)<
sizeof*pd="
sizeof(*pd)<
4.18arraynew.cpp
double*p3=newdouble[3];
p3[0]=0.2;
p3[1]=0.5;
p3[2]=0.8;
p3[1]is"
p3[1]<
p3=p3+1;
Nowp3[0]is"
p3[0]<
andp3[1]is"
p3=p3-1;
delete[]p3;
4.19addpnstr.cpp
doublewages[3]={10000.0,20000.0,30000.0};
shortstacks[3]={3,2,1};
double*pw=wages;
short*ps=&
stacks[0];
pw="
pw<
*pw="
*pw<
pw=pw+1;
add1tothepwpointer:
ps="
ps<
*ps="
*ps<
ps=ps+1;
add1tothepspointer:
accesstwoelementwitharraynotation"
stacks[0]="
stacks[0]<
stacks[1]="
stacks[1]<
accesstwoelementwithpointernotation"
*stacks="
*stacks<
*(stacks+1)="
*(stacks+1)<
sizeof(wages)<
=sizeofwagesarray"
sizeof(pw)<
=sizeofpwpointer"
4.20ptrstr.cpp
charanimal[20]="
bear"
constchar*bird="
wren"
char*ps;
animal<
and"
bird<
Enterakindofanimal:
animal;
ps=animal;
Beforeusingstrcpy():
at"
(int*)animal<
(int*)ps<
ps=newchar[strlen(animal)+1];
strcpy(ps,animal);
Afterusingstrcpy():
(int*)animal<
delete[]ps;
4.21newstrct.cpp
inflatable*ps=newinflatable;
Enternameofinflatableitem:
cin.get(ps->
name,20);
Entervolumeincubicfeet:
(*ps).volume;
Enterprice:
$"
ps->
price;
Name:
(*ps).name<
Volume:
volume<
Price;
price<
deleteps;
4.22delete.cpp
usingnamespacestd;
char*getname(void);
char*name;
name=getname();
(int*)name<
delete[]name;
(int*