CPrimerPlus(V)第4单元程序清单.doc_第1页
CPrimerPlus(V)第4单元程序清单.doc_第2页
CPrimerPlus(V)第4单元程序清单.doc_第3页
CPrimerPlus(V)第4单元程序清单.doc_第4页
CPrimerPlus(V)第4单元程序清单.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

4.1 arrayone.cpp#includeint main()using namespace std;int yams3;yams0=7;yams1=8;yams2=6;int yamcosts3=20,30,5;coutTotal yams=yams0+yams1+yams2endl;coutThe package with yams1 yams costs yamcosts1 cents per yam.endl;int total=yams0*yamcosts0+yams1*yamcosts1+yams2*yamcosts2;coutThe total yam expense is total cents.n;coutendlSize of yams array= sizeof yams bytes.n;coutSize of yams element= sizeof yams0 bytes.n;return 0;4.2 string.cpp#include#includeint main()using namespace std;const int Size=15;char name1Size;char name2Size=C+owboy;coutHowdy! Im name2name1;coutWell, name1, your name has strlen(name1) letters and is storedn;coutin an array of sizeof name1 bytes.endl;coutYour initial is name10 .endl;name23=0;coutHere are the first 3 characters of my name: name2endl;return 0;4.3 instr1.cpp#includeint main()using namespace std;const int ArSize=20;char nameArSize;char dessertArSize;coutname;coutdessert;coutI have some delicious dessert for you, name.n;return 0;4.4 instr2.cpp#includeint main()using namespace std;const int ArSize=20;char nameArSize;char dessertArSize;coutEnter your name:n;cin.getline(name,ArSize);coutEnter your favorite dessert:n;cin.getline(dessert,ArSize);coutI have some delicious dessert for you, name.n;return 0;4.5 instr3.cpp#includeint main()using namespace std;const int ArSize=20;char nameArSize;char dessertArSize;coutEnter your name:n;cin.get(name,ArSize).get();coutEnter your favorite dessert:n;cin.get(dessert,ArSize).get();coutI have some delicious dessert for you, name.n;return 0;4.6 numstr.cpp#includeint main()using namespace std;coutyear;coutWhat is its street address?n;char address80;cin.getline(address,80);coutYear built: yearendl;coutAddress: addressendl;coutDone!endl;return 0;4.7 strtype1.cpp#include#includeint main()using namespace std;char charr120;char charr220=jaguar;string str1;string str2=panther;coutcharr1;coutstr1;coutHere are some felines:ncharr1 charr2 str1 str2endl;coutThe third letter in charr2 is charr22endl;coutThe third letter in str2 is str22endl;return 0;4.8 strtype2.cpp#include#includeint main()using namespace std;string s1=penguin;string s2,s3;coutYou can assign one string object to another:s2=s1n;s2=s1;couts1: s1 , s2: s2endl;coutYou can assign a C-style string to a string object.n;couts2=buzzardn;s2=buzzard;couts2: s2endl;coutYou can concatenate string:s3=s1+s2n;s3=s1+s2;couts3: s3endl;coutYou can append string.n;s1+=s2;couts1+=s2 yields s1= s1endl;s2+=for a day;couts2+= for a day yields s2= s2endl;return 0;4.9 strtype3.cpp#include#include#includeint main()using namespace std;char charr120;char charr220=jaguar;string str1;string str2=panther;str1=str2;strcpy(charr1,charr2);str1+= paste;strcat(charr1, juice);int len1=str1.size();int len2=strlen(charr1);coutThe string str1 contains len1 characters.n;coutThe string charr1 contains len2 characters.endl;return 0;4.10 strtype4.cpp#include#include#includeint main()using namespace std;char charr20;string str;coutLength of string in charr before input: strlen(charr)endl;coutLength of string in str before input:str.size()endlendl;coutEnter a line of text:n;cin.getline(charr,20);coutYou entered charrendl;coutEnter another line of text:n;getline(cin,str);coutYou entered: strendlendl;coutLength of string in charr after inptu:strlen(charr)endl;coutLength of string in str after input:str.size()endl;return 0;4.11 structur.cpp#includestruct inflatablechar name20;float volume;double price;int main()using namespace std;inflatable guest=Glorious Gloria,1.88,29.99;inflatable pal=Audacious Arthur,3.12,32.99;coutExpand your guest list with ;cout and !n;coutYou can have both for $;coutguest.price+pal.price !n;return 0;4.12 assgn.cpp#includestruct inflatablechar name20; float volume;double price;int main()using namespace std;inflatable bouquet=sunflowers,0.20,12.49;inflatable choice;coutbouquet: for $bouquet.priceendl;choice=bouquet;coutchoice: for $choice.priceendl;return 0;4.13 arrstruc.cpp#includestruct inflatablechar name20; float volume;double price;int main()using namespace std;inflatable guests2=Bambi,0.5,21.99,Godzilla,2000,565.99;coutThe guest and endl;couthave a combined volume of guests0.volume+guests1.volume cubic feetn;return 0;4.14 address.cpp#includeint main()using namespace std;int donuts=6;double cups=4.5;coutdonuts value=donuts;cout and donuts address =&donutsendl; coutcups value=cups;cout and cups address =&cupsendl;return 0;4.15 pointer.cpp#includeint main()using namespace std;int updates=6;int *p_updates;p_updates=&updates;coutValues:updates=updates,*p_updates=*p_updatesendl;coutAddresses:&updates=&updates,p_updates=p_updatesendl;*p_updates=*p_updates+1;coutNow updates=updatesendl;return 0;4.16 init_ptr.cpp#includeint main()using namespace std;int higgens=5;int *pt=&higgens;coutValue of higgens=higgens;Address of higgens=&higgensendl;coutValue of *pt=*pt;Value of pt=ptendl;return 0;4.17 use_new.cpp#includeint main()using namespace std;int nights=1001;int *pt=new int;*pt=1001; coutnights value=nights: location &nightsendl;coutint value=*pt:location ptendl;double *pd=new double;*pd=10000001.0;coutdouble value=*pd:location: pdendl;coutlocation of pionter pd:&pdendl;coutsize of pt=sizeof(pt): size of *pt=sizeof(*pt)endl;coutsize of pd=sizeof(pd): size of *pd=sizeof(*pd)endl;return 0;4.18 arraynew.cpp#includeint main()using namespace std;double *p3=new double 3;p30=0.2;p31=0.5;p32=0.8;coutp31 is p31.n;p3=p3+1;coutNow p30 is p30 and p31 is p31.n;p3=p3-1;delete p3;return 0;4.19 addpnstr.cpp#includeint main()using namespace std;double wages3=10000.0,20000.0,30000.0;short stacks3=3,2,1;double *pw=wages;short *ps=&stacks0;coutpw=pw,*pw=*pwendl;pw=pw+1;coutadd 1 to the pw pointer:n;pw=pw+1;coutpw=pw,*pw=*pwendlendl;coutps=ps,*ps=*psendl;ps=ps+1;coutadd 1 to the ps pointer:n;coutps=ps,*ps=*psendlendl;coutaccess two element with array notationendl;coutstacks0=stacks0,stacks1=stacks1endl;coutaccess two element with pointer notationendl;cout*stacks=*stacks,*(stacks+1)=*(stacks+1)endl;coutsizeof(wages)=size of wages arrayendl;coutsizeof(pw)=size of pw pointerendl;return 0;4.20 ptrstr.cpp#include#includeint main()using namespace std;char animal20=bear;const char *bird=wren;char *ps;coutanimalandbirdendl;coutanimal; ps=animal;coutps!endl;coutBefore using strcpy():nanimal at (int *) animalendl;coutps at (int *) psendl;ps=new charstrlen(animal)+1;strcpy(ps,animal);coutAfter using strcpy():n;coutanimal at (int *)animalendl;coutps at (int *) psendl;delete ps;return 0;4.21 newstrct.cpp#includestruct inflatablechar name20;float volume;double price;int main()using namespace std;inflatable *ps=new inflatable;coutname,20);cout(*ps).volume;coutps-price;coutName:(*ps).nameendl;coutVolume:volume cubic feetn;coutPrice;$priceendl;delete ps;return 0;4.22 delete.cpp#include#includeusing namespace std;char *getname(void);int main()char *name;name=getname();coutname at (int *)nameendl;delete name;name=getname();coutname at (int *)nameendl;delete name;return 0;char *getname()char temp80;couttemp;char *pn=new charstrlen(temp)+1;strcpy(pn,temp);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论