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