《c++ primer plus 中文版 第六版源代码(31页).doc》由会员分享,可在线阅读,更多相关《c++ primer plus 中文版 第六版源代码(31页).doc(31页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、-c+ primer plus 中文版 第六版源代码-第 31 页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
2、 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;cout
3、Now 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
4、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
5、 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 =
6、 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 dollar
7、s 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:#includ
8、eusing 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; co
9、utMonsieur 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;coutaco
10、de 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
11、+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;couttu
12、b = 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
13、()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()co
14、ut.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 namespac
15、e 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;cout
16、tree = 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;coutY
17、es,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;to
18、tal=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;c
19、outWell, 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 desse
20、rtarsize;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
21、 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
22、.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#in
23、cludeusing 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+s
24、2;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+= p
25、aste;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(
26、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(char
27、r)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.99inflatable pal=Audacious Arthur,3.12,32.99coutExpand your guest list with guest.name;cout and pal
28、.name!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.49inflatable choice;coutbouquet: bouquet.name for $;coutbouqu
29、et.priceendl;choice=bouquet;coutchoice: choice.name 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 guests0.name and guests1.namenhave a combi
30、ned 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
31、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()
32、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,3000
33、0.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,st
34、acks1= 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;co
35、utp31 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;coutEnter price: $;