c++ primer plus 中文版 第六版源代码.doc

上传人:豆**** 文档编号:34304275 上传时间:2022-08-16 格式:DOC 页数:33 大小:76.50KB
返回 下载 相关 举报
c++ primer plus 中文版 第六版源代码.doc_第1页
第1页 / 共33页
c++ primer plus 中文版 第六版源代码.doc_第2页
第2页 / 共33页
点击查看更多>>
资源描述

《c++ primer plus 中文版 第六版源代码.doc》由会员分享,可在线阅读,更多相关《c++ primer plus 中文版 第六版源代码.doc(33页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、如有侵权,请联系网站删除,仅供学习与交流c+ primer plus 中文版 第六版源代码【精品文档】第 33 页c+ primer plus 中文版 第六版源代码c+ primer plus 中文版 第六版源代码C+ primer plus 中文版 第六版 源代码 第二章到第四章,后续继续更新 第二章1:#includevoid main()using namespace std;int carrots;carrots=25;coutI have ;coutcarrots;coutcarrots.; coutendl;carrots=carrots-1;coutCrunch,crunch.N

2、ow 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

3、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 n

4、amespace 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 th

5、e 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_i

6、ntendl;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.endl

7、Add $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 an

8、d 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:#include

9、using 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()coutaoperat

10、ion 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;coutT

11、he 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显示的

12、形式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

13、.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;co

14、uthats/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;cout

15、float 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:fixe

16、d,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

17、,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 ;

18、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 siz

19、e=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:#includeusi

20、ng 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

21、);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;ci

22、n.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 cha

23、rr2 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

24、=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=jagua

25、r;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()c

26、har 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查找输入,会自动调整大小coutY

27、ou 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.99inflatable pal=Audaciou

28、s Arthur,3.12,32.99coutExpand your guest list with guest.name;cout and pal.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=sunflo

29、wers,0.20,12.49inflatable choice;coutbouquet: bouquet.name for $;coutbouquet.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 , Godz

30、illa,2000,565.99 coutThe guests guests0.name and guests1.namenhave 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

31、 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;retu

32、rn 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

33、:#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=

34、*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

35、 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;cout

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com