c++-primer-plus-中文版-第六版源代码(共42页).doc
《c++-primer-plus-中文版-第六版源代码(共42页).doc》由会员分享,可在线阅读,更多相关《c++-primer-plus-中文版-第六版源代码(共42页).doc(42页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上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 s
2、tone;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
3、 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 na
4、mespace 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
5、_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;coutBit
6、s 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;co
7、ut 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
8、;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 st
9、riking 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
10、 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 ASCI
11、I 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 mill
12、ion 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;
13、/或者是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:fix
14、ed,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()con
15、st 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;cout
16、guess = 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 ;/cou
17、tstatiic_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*yamc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- primer plus 中文版 第六 源代码 42
限制150内