当前位置:文档之家› c++primerplus中文版第六版源代码

c++primerplus中文版第六版源代码

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:__________\b\b\b\b\b\b\b\b";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 yams[3];yams[0]=7;yams[1]=8;yams[2]=6;int yamcosts[3]={20,30,5};cout<<"Total yams = ";cout<<yams[0]+yams[1]+yams[2]<<endl;cout<<"The package with "<<yams[1]<<" yams costs ";cout<<yamcosts[1]<<" cents per yam.\n";int total=yams[0]*yamcosts[0]+yams[1]*yamcosts[1];total=total+yams[2]*yamcosts[2];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 yams[0];cout<<" bytes.\n";}2:#include<iostream>using namespace std;void main(){const int size=15;char name1[size];char name2[size]="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 stored\n";cout<<" in an array of "<<sizeof name1<<" bytes.\n";cout<<"Your initial is "<<name1[0]<<".\n";name2[3]='\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 name[arsize];char dessert[arsize];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 name[arsize];char dessert[arsize];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 name[arsize];char dessert[arsize];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 charr1[20];char charr2[20]="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 "<<charr2[2]<<endl;cout<<"The third letter in "<<str2<<" is "<<str2[2]<<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=s1\n";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+s2\n";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 charr1[20];char charr2[20]="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 charr[20];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 inflatable{char name[20];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};cout<<"Expand your guest list with "<<;cout<<" and "<<<<"!\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 inflatable{char name[20];float volume;double price;};int main(){inflatable bouquet={"sunflowers",0.20,12.49};inflatable choice;cout<<"bouquet: "<<<<" for $";cout<<bouquet.price<<endl;choice=bouquet;cout<<"choice: "<<<<" for $";cout<<choice.price<<endl;return 0;}12:#include<iostream>using namespace std;struct inflatable{char name[20];float volume;double price;};int main(){inflatable guests[2]={{ "Bambi",0.5,21.99 },{ "Godzilla",2000,565.99 }};cout<<"The guests "<<guests[0].name<<" and "<<guests[1].name <<"\nhave a combined volume of "<<guests[0].volume+guests[1].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 temp[80];cout<<"Enter last name: ";cin>>temp;char *pn=new char[strlen(temp)+1];strcpy(pn,temp);return pn;}16:#include<iostream>using namespace std;int main(){double wages[3]={ 10000.0,20000.0,30000.0 };short stacks[3]={ 3,2,1 };double *pw=wages;short *ps=&stacks[0];cout<<"pw= "<<pw<<",*pw= "<<*pw<<endl;pw=pw+1;cout<<"add 1 to the pw pointer:\n";cout<<"pw= "<<pw<<",*pw= "<<*pw<<"\n\n";cout<<"ps= "<<ps<<",*ps= "<<*ps<<"\n\n";ps=ps+1;cout<<"add 1 to the ps pointer:\n";cout<<"ps= "<<ps<<",*ps= "<<*ps<<"\n\n";cout<<"access two elements with array notation\n";cout<<"stacks[0]= "<<stacks[0]<<",stacks[1]= "<<stacks[1]<<endl;cout<<"access two elements with pointer notation\n";cout<<"*stacks= "<<*stacks<<",*(stacks+1)= "<<*(stacks+1)<<endl;cout<<sizeof wages<<" =size of wages array\n";cout<<sizeof pw<<" =size of pw pointer\n";return 0;}17://指针和数组的真正区别#include<iostream>using namespace std;int main(){double *p3=new double[3];p3[0]=0.2;p3[1]=0.5;p3[2]=0.8;cout<<"p3[1] is "<<p3[1]<<".\n";p3=p3+1;cout<<"Now p3[0] is "<<p3[0]<<" and ";cout<<"p3[1] is "<<p3[1]<<".\n";p3=p3-1;cout<<"Now p3[0] is "<<p3[0]<<" and ";cout<<"p3[1] is "<<p3[1]<<".\n";delete [] p3;return 0;}18:#include<iostream>#include<cstring>using namespace std;struct inflatable{char name[20];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<<"Enter price: $";cin>>ps->price;cout<<"Name: "<<(*ps).name<<endl;cout<<"V olume: "<<ps->volume<<" cubic feet\n";cout<<"Price: $"<<ps->price<<endl;delete ps;return 0;}19:#include<iostream>#include<cstring>#include<vector>using namespace std;int main()double a1[4]={ 1.2,2.4,3.6,4.8 };vector<double> a2(4);a2[0]=1.0/3.0;a2[1]=1.0/5.0;a2[2]=1.0/7.0;a2[3]=1.0/9.0;cout<<"a1[2]: "<<a1[2]<<" at "<<&a1[2]<<endl;cout<<"a2[2]: "<<a2[2]<<" at "<<&a2[2]<<endl;a1[-2]=20.2;cout<<"a1[-2]: "<<a1[-2]<<" at "<<&a1[-2]<<endl;return 0;}20:#include<iostream>#include<cstring>using namespace std;int main(){char animal[20]="bear";const char *bird="wren";char *ps;cout<<animal<<" and ";cout<<bird<<"\n";cout<<"Enter a kind of animal: ";cin>>animal;ps=animal;cout<<ps<<"!\n";cout<<"Before using strcpy():\n";cout<<animal<<" at "<<(int *)animal<<endl;cout<<ps<<" at "<<(int *)ps<<endl;ps=new char[strlen(animal+1)];strcpy(ps,animal);cout<<"After using strcpy():\n";cout<<animal<<" at "<<(int *)animal<<endl;cout<<ps<<" at "<<(int *)ps<<endl;delete [] ps;return 0;}21:#include<iostream>using namespace std;int main(){int higgens=5;int *pt=&higgens;cout<<"Value of higgens = "<<higgens<<";Address of higgens = "<<&higgens<<endl;cout<<"Value of *pt = "<<*pt<<";Address of pt = "<<pt<<endl;return 0;}22:#include<iostream>using namespace std;int main(){int nights=1001;int *pt=new int;*pt=1001;cout<<"nights value = ";cout<<nights<<": location "<<&nights<<endl;cout<<"int ";cout<<"value = "<<*pt<<": location = "<<pt<<endl;double *pd=new double;*pd=10000001.0;cout<<"double ";cout<<"value = "<<*pd<<": location = "<<pd<<endl;cout<<"location of pointer pd: "<<&pd<<endl;cout<<"size of pt = "<<sizeof pt;cout<<":size of *pt = "<<sizeof *pt<<endl;cout<<"size of pd = "<<sizeof pd;cout<<":size of *pd = "<<sizeof *pd<<endl;return 0;}23:#include<iostream>#include<cstring>using namespace std;struct antarctica_years_end{int year;};int main(){antarctica_years_end s01,s02,s03;s01.year=1998;antarctica_years_end *pa=&s02;pa->year=1999;antarctica_years_end trio[3];trio[0].year=2003;cout<<trio->year<<endl;const antarctica_years_end *arp[3]={ &s01,&s02,&s03 };cout<<arp[1]->year<<endl;const antarctica_years_end **ppa=arp;//auto ppb=arp;cout<<(*ppa)->year<<endl;//cout<<(*(ppb+1))->year<<endl;return 0; }。

相关主题