c++ primer plus 中文版 第六版源代码.doc_第1页
c++ primer plus 中文版 第六版源代码.doc_第2页
c++ primer plus 中文版 第六版源代码.doc_第3页
c++ primer plus 中文版 第六版源代码.doc_第4页
c++ primer plus 中文版 第六版源代码.doc_第5页
已阅读5页,还剩37页未读 继续免费阅读

下载本文档

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

文档简介

C+ primer plus 中文版 第六版 源代码 第二章到第四章,后续继续更新 第二章1:#includevoid main()using namespace std;int carrots;carrots=25;coutI have ;coutcarrots;coutcarrots.; coutendl;carrots=carrots-1;coutCrunch,crunch.Now I have carrots carrotsendl;2:#includeint stonetolb(int);int main()using namespace std;int stone;coutstone;int pounds=stonetolb(stone);coutstone stone= ;coutpounds pounds.endl;return 0;int stonetolb(int sts)return 14*sts;3:#includevoid main()using namespace std;int carrots;carrots=25;coutHow many carrots do you have?carrots;coutHere are two more.; carrots=carrots+2;coutNow you have carrots carrots.endl; /下两行专门测试cin.get() cin.get();cin.get();4:#includeusing namespace std;void main()coutCome up and C+ me some time.;coutendl;coutYou wont regret it!endl;5#includevoid simon(int);int main()using namespace std;simon(3);coutcount;simon(count);coutDone !endl;return 0;void simon(int n)using namespace std;coutSimon says touch your toes n times.endl;6:#include#includevoid main()using namespace std;double area;coutarea;double side;side=sqrt(area);coutThats the equivalent of a square side feet to the side.endl;coutHow fascinating!endl; 第三章1:#include#includeusing namespace std;int main()int n_int=INT_MAX;short n_short=SHRT_MAX;long n_long=LONG_MAX;coutint is sizeof(int) bytes.endl;coutshort issizeof n_short bytes.endl;coutlong issizeof n_long bytes.endlendl;coutMaximum values :endl;coutint :n_intendl;coutshort :n_shortendl;coutlong :n_longendl;coutMinimum int value = INT_MINendl;coutBits per byts = CHAR_BITendl;return 0;2:#include#include#define ZERO 0using namespace std;int main()short sam=SHRT_MAX;unsigned short sue=sam;coutsam has sam dollars and sue has sue;cout dollars deposited.endlAdd $1 to each account.endlNow ;sam=sam+1;sue=sue+1;coutSam has sam dollars and sue has sue;cout dollars deposited.npoor sam!endl;sam=ZERO;sue=ZERO;coutsam has sam dollars and sue has sue;cout dollars deposited.endl;coutTake $1 from each account.endlNow ;sam=sam-1;sue=sue-1;coutsam has sam dolars and sue has sue;cout dollars deposited.endlLucky sue!endl;return 0;3:#includeusing namespace std;void main()int chest=42;int waist=0x42;int inseam=042;coutMonsieur cuts a striking figure!n;coutchest = chest (42 in decimal)n;coutwaist = waist (0x42 in hex)n;coutinseam =inseam (042 in octal)n;4:#includeusing namespace std;void main()int chest=42;int waist=42;int inseam=42; coutMonsieur cuts a striking figure!n;coutchest = chest (decimal for 42)endl; couthex;coutwaist = waist (hexadecimal for 42)endl;coutoct;coutinseam =inseam (octal for 42)nendl;5:#includeusing namespace std;void main()coutaoperation HyperHype is now activated!n;coutcode;coutaYou entered code .n;coutacode verified !proceed with plan z3!n;6:#includeusing namespace std;void main()char ch;coutEnter a character:ch;coutHola! ;coutThank you for the ch character.endl;7:#includeusing namespace std;void main()char ch=M;int i=ch;coutThe ASCII code for ch is iendl;coutAdd one to the character code:endl;ch=ch+1;i=ch;coutThe ASCII code for ch is iendl;coutDisplaying char ch using cout.put(ch): ;cout.put(ch);cout.put(!);coutendlDoneendl;8:#includeusing namespace std;void main()cout.setf(ios_base:fixed,ios_base:floatfield);/控制cout显示的形式float tub=10.0/3.0;double mint=10.0/3.0;const float million=1.0e6;couttub = tub;cout, a million tubs = million*tub;cout,nand ten million tubs = ;cout10*million*tubendl;coutmint = mint and a million mints = ;coutmillion*mintendl;9:#includeusing namespace std;void main()float a=2.34e+22f;float b=a+1.0f;couta= aendl;coutb-a= b-aendl;10:#includeusing namespace std;void main()double hats,heads;/或者是floatcout.setf(ios_base:fixed,ios_base:floatfield);couthats;coutheads;couthats = hats;heads = headsendl;couthats+heads = hats+headsendl;couthats-heads = hats-headsendl;couthats*heads = hats*headsendl;couthats/heads = hats/headsendl;11:#includeusing namespace std;void main()cout.setf(ios_base:fixed,ios_base:floatfield);coutInteger division:9/5= 9/5endl;coutFloating-point division: 9.0/5.0 = ;cout9.0/5.0endl;coutMixed division: 9.0/5 = 9.0/5endl;coutdouble constants:1.e7/9.0 = ;cout1.e7/9.0endl;coutfloat constants:1.e7f/9.0f = ;cout1.e7f/9.0fendl;12:#includeusing namespace std;void main()const int Lbs_per_stn=14;int lbs;coutlbs;int stone=lbs/Lbs_per_stn;int pounds=lbs%Lbs_per_stn;coutlbs pounds are stone stone, pounds pound(s).n;13:#includeusing namespace std;void main()cout.setf(ios_base:fixed,ios_base:floatfield);float tree=3;int guess(3.9832);int debt=7.2E12;couttree = treeendl;coutguess = guessendl;coutdebt = debtendl;14:#includeusing namespace std;void main()int auks,bats,coots;auks=19.99+11.99;bats=(int)19.99+(int)11.99;coots=int (19.99)+int (11.99);coutauks = auks,bats = bats;cout,coots = cootsendl;char ch=Z;coutThe code forch is ;coutint (ch)endl;coutYes,the code is ;/coutstatiic_cast(ch)endl; 第四章1:#includeusing namespace std;void main()int yams3;yams0=7;yams1=8;yams2=6;int yamcosts3=20,30,5;coutTotal yams = ;coutyams0+yams1+yams2endl;coutThe package with yams1 yams costs ;coutyamcosts1 cents per yam.n;int total=yams0*yamcosts0+yams1*yamcosts1;total=total+yams2*yamcosts2;coutThe total yam expense is total cents.n;coutnSize of yams array = sizeof yams;cout bytes.n;coutSize of one element = sizeof yams0;cout bytes.n;2:#includeusing namespace std;void main()const int size=15;char name1size;char name2size=C+owboy;coutHowdy! Im name2;coutname1;coutWell, name1,your name has ;coutstrlen(name1) letters and is storedn;cout in an array of sizeof name1 bytes.n;coutYour initial is name10.n;name23=0;coutHere are the first 3 charchters of my name: ;coutname2endl;3:#includeusing namespace std;void main()const int arsize=20;char namearsize;char dessertarsize;coutname;coutdessert;coutI have some delicious dessert;cout for you, name.n;4:#includeusing namespace std;void main()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;cout for you, name.n;5:#includeusing namespace std;void main()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);coutI have some delicious dessert;cout for you, name.n;6:#include#includeusing namespace std;void main()char charr120;char charr220=jaguar;string str1;string str2=panther;coutcharr1;coutstr1;coutHere are some felines:n;coutcharr1 charr2 str1 str2endl;coutThe third letter in charr2 is charr22endl;coutThe third letter in str2 is str22endl;7:#include#includeusing namespace std;void main()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 strings: s3=s1+s2n;s3=s1+s2;couts3: s3endl;coutYou can append strings.n;s1+=s2;couts1+=s2 yields s1= s1endl;s2+= for a day;couts1+= for a day yields s2 = s2endl; 8:#include#include#includeusing namespace std;void main()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.n;9:#include#include#includeusing namespace std;void main()char charr20;string str;coutLength of string in charr before input: strlen(charr)endl;coutLength of string in str before input: str.size()endl;coutEnter a line of text:n;cin.getline(charr,20);coutYou entered: charrendl; coutEnter another line of text:n;getline(cin,str);/将cin用作参数,到str查找输入,会自动调整大小coutYou entered: strendl;coutLength of string in charr after input: strlen(charr)endl;coutLength of string in str before input: str.size()endl;10:#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;coutguest.volume+pal.volumeendl;return 0;11:#includeusing namespace std;struct inflatablechar name20;float volume;double price;int main()inflatable bouquet=sunflowers,0.20,12.49;inflatable choice;coutbouquet: for $;coutbouquet.priceendl;choice=bouquet;coutchoice: for $;coutchoice.priceendl;return 0;12:#includeusing namespace std;struct inflatablechar name20;float volume;double price;int main()inflatable guests2= Bambi,0.5,21.99 , Godzilla,2000,565.99 ;coutThe guests and nhave a combined volume of guests0.volume+guests1.volume cubic feet.n;return 0;13:#includeusing namespace std;int main()int donuts=6;double cups=4.5;coutdonuts valus = donuts;cout and donuts address = &donutsendl;coutcups value = cups;cout and cups addres = &cupsendl;14:#includeusing namespace std;int main()int updates=6;int *p_updates;p_updates=&updates;coutValue:updates = updates;cout,*p_updates = *p_updatesendl;coutAddress: &update = &updates;cout,p_updates = p_updatesendl;*p_updates=*p_updates+1;coutNow updates = updatesendl;return 0;15:#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);return pn;16:#includeusing namespace std;int main()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;coutpw= pw,*pw= *pwnn; coutps= ps,*ps= *psnn;ps=ps+1; coutadd 1 to the ps pointer:n; coutps= ps,*ps= *psnn;coutaccess two elements with array notationn;coutstacks0= stacks0,stacks1= stacks1endl;coutaccess two elements with pointer notationn;cout*stacks= *stacks,*(stacks+1)= *(stacks+1)endl;coutsizeof wages =size of wages arrayn;coutsizeof pw =size of pw pointern;return 0;17:/指针和数组的真正区别#includeusing namespace std;int main()double *p3=new double3;p30=0.2;p31=0.5;p32=0.8;coutp31 is p31.n;p3=p3+1;coutNow p30 is p30 and ;coutp31 is p31.n;p3=p3-1; coutNow p30 is p30 and ;coutp31 is p31.n;delete p3;return 0;18:#include#includeusing namespace std;struct inflatablechar name20;float volume;double price;int main()inflatable *ps=new inflatable;coutname,20);cout(*ps).volume;coutps-price;coutName: (*ps).nameendl;coutVolume: volume cubic feetn;coutPrice: $priceendl;delete ps;return 0;19:#include#include#includeusing namespace std;int main()double a14= 1.2,2.4,3.6,4.8 ;vector a2(4);a20=1.0/3.0;a21=1.0/5.0;a22=1.0/7.0;a23=1.0/9.0;couta12: a12 at &a12endl;couta22: a22 at &a22endl;a1-2=20.2;couta1-2: a1-2 at &a1-2endl;return 0;20:#include#includeusing namespace std;int main()char animal20=bear;const char *bird=wren;char *ps;coutanimal and ;coutbirdn;coutanimal;ps=animal;coutps!n;coutBefore usin

温馨提示

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

评论

0/150

提交评论