c++ primer plus 中文版 第六版源代码.doc
如有侵权,请联系网站删除,仅供学习与交流c+ primer plus 中文版 第六版源代码【精品文档】第 33 页c+ primer plus 中文版 第六版源代码c+ primer plus 中文版 第六版源代码C+ primer plus 中文版 第六版 源代码 第二章到第四章,后续继续更新 第二章1:#include<iostream>void main()using namespace std;int carrots;carrots=25;cout<<"I have "cout<<carrots;cout<<"carrots." cout<<endl;carrots=carrots-1;cout<<"Crunch,crunch.Now I have "<<carrots<<" carrots"<<endl;2:#include<iostream>int stonetolb(int);int main()using namespace std;int stone;cout<<"Enter the weight in stone: "cin>>stone;int pounds=stonetolb(stone);cout<<stone<<" stone= "cout<<pounds<<" pounds."<<endl;return 0;int stonetolb(int sts)return 14*sts;3:#include<iostream>void main()using namespace std;int carrots;carrots=25;cout<<"How many carrots do you have?"<<endl;cin>>carrots;cout<<"Here are two more." carrots=carrots+2;cout<<"Now you have "<<carrots<<" carrots."<<endl; /下两行专门测试cin.get() cin.get();cin.get();4:#include<iostream>using namespace std;void main()cout<<"Come up and C+ me some time."cout<<endl;cout<<"You won't regret it!"<<endl;5#include<iostream>void simon(int);int main()using namespace std;simon(3);cout<<"Pick an integer: "int count;cin>>count;simon(count);cout<<"Done !"<<endl;return 0;void simon(int n)using namespace std;cout<<"Simon says touch your toes "<<n<<" times."<<endl;6:#include<iostream>#include<cmath>void main()using namespace std;double area;cout<<"Enter the floor arae,in square feet,of your home: "cin>>area;double side;side=sqrt(area);cout<<"That's the equivalent of a square "<<side<<" feet to the side."<<endl;cout<<"How fascinating!"<<endl; 第三章1:#include<iostream>#include<climits>using namespace std;int main()int n_int=INT_MAX;short n_short=SHRT_MAX;long n_long=LONG_MAX;cout<<"int is "<<sizeof(int)<<" bytes."<<endl;cout<<"short is"<<sizeof n_short<<" bytes."<<endl;cout<<"long is"<<sizeof n_long<<" bytes."<<endl<<endl;cout<<"Maximum values :"<<endl;cout<<"int :"<<n_int<<endl;cout<<"short :"<<n_short<<endl;cout<<"long :"<<n_long<<endl;cout<<"Minimum int value = "<<INT_MIN<<endl;cout<<"Bits per byts = "<<CHAR_BIT<<endl;return 0;2:#include<iostream>#include<climits>#define ZERO 0using namespace std;int main()short sam=SHRT_MAX;unsigned short sue=sam;cout<<"sam has "<<sam<<" dollars and sue has "<<sue;cout<<" dollars deposited."<<endl<<"Add $1 to each account."<<endl<<"Now "sam=sam+1;sue=sue+1;cout<<"Sam has "<<sam<<" dollars and sue has "<<sue;cout<<" dollars deposited.npoor sam!"<<endl;sam=ZERO;sue=ZERO;cout<<"sam has "<<sam<<" dollars and sue has "<<sue;cout<<" dollars deposited."<<endl;cout<<"Take $1 from each account."<<endl<<"Now "sam=sam-1;sue=sue-1;cout<<"sam has "<<sam<<" dolars and sue has "<<sue;cout<<" dollars deposited."<<endl<<"Lucky sue!"<<endl;return 0;3:#include<iostream>using namespace std;void main()int chest=42;int waist=0x42;int inseam=042;cout<<"Monsieur cuts a striking figure!n"cout<<"chest = "<<chest<<" (42 in decimal)n"cout<<"waist = "<<waist<<" (0x42 in hex)n"cout<<"inseam ="<<inseam<<" (042 in octal)n"4:#include<iostream>using namespace std;void main()int chest=42;int waist=42;int inseam=42; cout<<"Monsieur cuts a striking figure!n"cout<<"chest = "<<chest<<" (decimal for 42)"<<endl; cout<<hex;cout<<"waist = "<<waist<<" (hexadecimal for 42)"<<endl;cout<<oct;cout<<"inseam ="<<inseam<<" (octal for 42)n"<<endl;5:#include<iostream>using namespace std;void main()cout<<"aoperation "HyperHype" is now activated!n"cout<<"Enter your agent code:_bbbbbbbb"long code;cin>>code;cout<<"aYou entered "<<code<<" .n"cout<<"acode verified !proceed with plan z3!n"6:#include<iostream>using namespace std;void main()char ch;cout<<"Enter a character:"<<endl;cin>>ch;cout<<"Hola! "cout<<"Thank you for the "<<ch<<" character."<<endl;7:#include<iostream>using namespace std;void main()char ch='M'int i=ch;cout<<"The ASCII code for "<<ch<<" is "<<i<<endl;cout<<"Add one to the character code:"<<endl;ch=ch+1;i=ch;cout<<"The ASCII code for "<<ch<<" is "<<i<<endl;cout<<"Displaying char ch using cout.put(ch): "cout.put(ch);cout.put('!');cout<<endl<<"Done"<<endl;8:#include<iostream>using 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;cout<<"tub = "<<tub;cout<<", a million tubs = "<<million*tub;cout<<",nand ten million tubs = "cout<<10*million*tub<<endl;cout<<"mint = "<<mint<<" and a million mints = "cout<<million*mint<<endl;9:#include<iostream>using namespace std;void main()float a=2.34e+22f;float b=a+1.0f;cout<<"a= "<<a<<endl;cout<<"b-a= "<<b-a<<endl;10:#include<iostream>using namespace std;void main()double hats,heads;/或者是floatcout.setf(ios_base:fixed,ios_base:floatfield);cout<<"Enter a number: "cin>>hats;cout<<"Enter another number: "cin>>heads;cout<<"hats = "<<hats<<"heads = "<<heads<<endl;cout<<"hats+heads = "<<hats+heads<<endl;cout<<"hats-heads = "<<hats-heads<<endl;cout<<"hats*heads = "<<hats*heads<<endl;cout<<"hats/heads = "<<hats/heads<<endl;11:#include<iostream>using namespace std;void main()cout.setf(ios_base:fixed,ios_base:floatfield);cout<<"Integer division:9/5= "<<9/5<<endl;cout<<"Floating-point division: 9.0/5.0 = "cout<<9.0/5.0<<endl;cout<<"Mixed division: 9.0/5 = "<<9.0/5<<endl;cout<<"double constants:1.e7/9.0 = "cout<<1.e7/9.0<<endl;cout<<"float constants:1.e7f/9.0f = "cout<<1.e7f/9.0f<<endl;12:#include<iostream>using namespace std;void main()const int Lbs_per_stn=14;int lbs;cout<<"Enter your weight in pounds: "cin>>lbs;int stone=lbs/Lbs_per_stn;int pounds=lbs%Lbs_per_stn;cout<<lbs<<" pounds are "<<stone<<" stone, "<<pounds<<" pound(s).n"13:#include<iostream>using namespace std;void main()cout.setf(ios_base:fixed,ios_base:floatfield);float tree=3;int guess(3.9832);int debt=7.2E12;cout<<"tree = "<<tree<<endl;cout<<"guess = "<<guess<<endl;cout<<"debt = "<<debt<<endl;14:#include<iostream>using 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);cout<<"auks = "<<auks<<",bats = "<<bats;cout<<",coots = "<<coots<<endl;char ch='Z'cout<<"The code for"<<ch<<" is "cout<<int (ch)<<endl;cout<<"Yes,the code is "/cout<<statiic_cast<int>(ch)<<endl; 第四章1:#include<iostream>using namespace std;void main()int yams3;yams0=7;yams1=8;yams2=6;int yamcosts3=20,30,5;cout<<"Total yams = "cout<<yams0+yams1+yams2<<endl;cout<<"The package with "<<yams1<<" yams costs "cout<<yamcosts1<<" cents per yam.n"int total=yams0*yamcosts0+yams1*yamcosts1;total=total+yams2*yamcosts2;cout<<"The total yam expense is "<<total<<" cents.n"cout<<"nSize of yams array = "<<sizeof yams;cout<<" bytes.n"cout<<"Size of one element = "<<sizeof yams0;cout<<" bytes.n"2:#include<iostream>using namespace std;void main()const int size=15;char name1size;char name2size="C+owboy"cout<<"Howdy! I'm "<<name2;cout<<"! what's your name?n"cin>>name1;cout<<"Well, "<<name1<<",your name has "cout<<strlen(name1)<<" letters and is storedn"cout<<" in an array of "<<sizeof name1<<" bytes.n"cout<<"Your initial is "<<name10<<".n"name23='0'cout<<"Here are the first 3 charchters of my name: "cout<<name2<<endl;3:#include<iostream>using namespace std;void main()const int arsize=20;char namearsize;char dessertarsize;cout<<"Enter your name:n"cin>>name;cout<<"Enter your favorite dessert:n"cin>>dessert;cout<<"I have some delicious "<<dessert;cout<<" for you, "<<name<<".n"4:#include<iostream>using namespace std;void main()const int arsize=20;char namearsize;char dessertarsize;cout<<"Enter your name:n"cin.getline(name,arsize);cout<<"Enter your favorite dessert:n"cin.getline(dessert,arsize);cout<<"I have some delicious "<<dessert;cout<<" for you, "<<name<<".n"5:#include<iostream>using namespace std;void main()const int arsize=20;char namearsize;char dessertarsize;cout<<"Enter your name:n"cin.get(name,arsize).get();cout<<"Enter your favorite dessert:n"cin.get(dessert,arsize);cout<<"I have some delicious "<<dessert;cout<<" for you, "<<name<<".n"6:#include<iostream>#include<string>using namespace std;void main()char charr120;char charr220="jaguar"string str1;string str2="panther"cout<<"Enter a kind of feline: "cin>>charr1;cout<<"Enter another kind of feline: "cin>>str1;cout<<"Here are some felines:n"cout<<charr1<<" "<<charr2<<" "<<str1<<" "<<str2<<endl;cout<<"The third letter in "<<charr2<<" is "<<charr22<<endl;cout<<"The third letter in "<<str2<<" is "<<str22<<endl;7:#include<iostream>#include<string>using namespace std;void main()string s1="penguin"string s2,s3;cout<<"You can assign one string object to another:s2=s1n"s2=s1;cout<<"s1: "<<s1<<",s2: "<<s2<<endl;cout<<"You can assign a c-style string to a string object.n"cout<<"s2= "buzzard"n"s2="buzzard"cout<<"s2: "<<s2<<endl;cout<<"You can concatenate strings: s3=s1+s2n"s3=s1+s2;cout<<"s3: "<<s3<<endl;cout<<"You can append strings.n"s1+=s2;cout<<"s1+=s2 yields s1= "<<s1<<endl;s2+=" for a day"cout<<"s1+=" for a day" yields s2 = "<<s2<<endl; 8:#include<iostream>#include<string>#include<cstring>using 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);cout<<"The string "<<str1<<" contains "<<len1<<" characters.n"cout<<"The string "<<charr1<<" contains "<<len2<<" characters.n"9:#include<iostream>#include<string>#include<cstring>using namespace std;void main()char charr20;string str;cout<<"Length of string in charr before input: "<<strlen(charr)<<endl;cout<<"Length of string in str before input: "<<str.size()<<endl;cout<<"Enter a line of text:n"cin.getline(charr,20);cout<<"You entered: "<<charr<<endl; cout<<"Enter another line of text:n"getline(cin,str);/将cin用作参数,到str查找输入,会自动调整大小cout<<"You entered: "<<str<<endl;cout<<"Length of string in charr after input: "<<strlen(charr)<<endl;cout<<"Length of string in str before input: "<<str.size()<<endl;10:#include<iostream>struct 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.99cout<<"Expand your guest list with "<<guest.name;cout<<" and "<<pal.name<<"!n"cout<<"You can have both for $"cout<<guest.price+pal.price<<"!n"cout<<guest.volume+pal.volume<<endl;return 0;11:#include<iostream>using namespace std;struct inflatablechar name20;float volume;double price;int main()inflatable bouquet="sunflowers",0.20,12.49inflatable choice;cout<<"bouquet: "<<bouquet.name<<" for $"cout<<bouquet.price<<endl;choice=bouquet;cout<<"choice: "<<choice.name<<" for $"cout<<choice.price<<endl;return 0;12:#include<iostream>using namespace std;struct inflatablechar name20;float volume;double price;int main()inflatable guests2= "Bambi",0.5,21.99 , "Godzilla",2000,565.99 cout<<"The guests "<<guests0.name<<" and "<<guests1.name<<"nhave a combined volume of "<<guests0.volume+guests1.volume<<" cubic feet.n"return 0;13:#include<iostream>using namespace std;int main()int donuts=6;double cups=4.5;cout<<"donuts valus = "<<donuts;cout<<" and donuts address = "<<&donuts<<endl;cout<<"cups value = "<<cups;cout<<" and cups addres = "<<&cups<<endl;14:#include<iostream>using namespace std;int main()int updates=6;int *p_updates;p_updates=&updates;cout<<"Value:updates = "<<updates;cout<<",*p_updates = "<<*p_updates<<endl;cout<<"Address: &update = "<<&updates;cout<<",p_updates = "<<p_updates<<endl;*p_updates=*p_updates+1;cout<<"Now updates = "<<updates<<endl;return 0;15:#include<iostream>#include<cstring>using namespace std;char *getname(void);int main()char *name;name=getname();cout<<name<<" at "<<(int *)name<<endl;delete name;name=getname();cout<<name<<" at "<<(int *)name<<endl;delete name;return 0;char *getname()char temp80;cout<<"Enter last name: "cin>>temp;char *pn=new charstrlen(temp)+1;strcpy(pn,temp);return pn;16:#include<iostream>using namespace std;int main()double wages3= 10000.0,20000.0,30000.0 ;short stacks3= 3,2,1 ;double *pw=wages;short *ps=&stacks0;cout<<"pw= "<<pw<<",*pw= "<<*pw<<endl;pw=pw+1;cout<<"add 1 to the pw pointer:n"cout<<"pw= "<<pw<<",*pw= "<<*pw<<"nn" cout<<"ps= "<<ps<<",*ps= "<<*ps<<"nn"ps=ps+1; cout<<"add 1 to the ps pointer:n" cout<<"ps= "<<ps<<",*ps= "<<*ps<<"nn"cout<<"access two elements with array notationn"cout<<"stacks0= "<<stacks0<<",stacks1= "<<stacks1<<endl;cout<<"access two elements with pointer notationn"cout<<"*stacks= "<<*stacks<<",*(stacks+1)= "<<*(stacks+1)<<endl;cout<<sizeof wages<<" =size of wages arrayn"cout<<sizeof pw<<" =size of pw pointern"return 0;17:/指针和数组的真正区别#include<iostream>using namespace std;int main()double *p3=new double3;p30=0.2;p31=0.5;p32=0.8;cout<<"p31 is "<<p31<<".n"p3=p3+1;cout<<"Now p30 is "<<p30<<" and "cout<<"p31 is "<<p31<<".n"p3=p3-1; cout<<"Now p30 is "<<p30<<" and "cout<<"p31 is "<<p31<<".n"delete p3;return 0;18:#include<iostream>#include<cstring>using namespace std;struct inflatablechar name20;float volume;double price;int main()inflatable *ps=new inflatable;cout<<"Enter name of inflatable item: "cin.get(ps->name,20);cout<<"Enter volume of cubic feet: "cin>>(*ps).volume;cout