C++ Primer Plus6 第4单元程序清单.docx

上传人:b****6 文档编号:8189685 上传时间:2023-01-29 格式:DOCX 页数:17 大小:18.31KB
下载 相关 举报
C++ Primer Plus6 第4单元程序清单.docx_第1页
第1页 / 共17页
C++ Primer Plus6 第4单元程序清单.docx_第2页
第2页 / 共17页
C++ Primer Plus6 第4单元程序清单.docx_第3页
第3页 / 共17页
C++ Primer Plus6 第4单元程序清单.docx_第4页
第4页 / 共17页
C++ Primer Plus6 第4单元程序清单.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

C++ Primer Plus6 第4单元程序清单.docx

《C++ Primer Plus6 第4单元程序清单.docx》由会员分享,可在线阅读,更多相关《C++ Primer Plus6 第4单元程序清单.docx(17页珍藏版)》请在冰豆网上搜索。

C++ Primer Plus6 第4单元程序清单.docx

C++PrimerPlus6第4单元程序清单

4.1arrayone.cpp

#include

intmain()

{

usingnamespacestd;

intyams[3];

yams[0]=7;

yams[1]=8;

yams[2]=6;

intyamcosts[3]={20,30,5};

cout<<"Totalyams="<

cout<<"Thepackagewith"<

inttotal=yams[0]*yamcosts[0]+yams[1]*yamcosts[1]+yams[2]*yamcosts[2];

cout<<"Thetotalyamexpenseis"<

cout<

cout<<"Sizeofyamselement="<

return0;

}

4.2string.cpp

#include

#include

intmain()

{

usingnamespacestd;

constintSize=15;

charname1[Size];

charname2[Size]="C++owboy";

cout<<"Howdy!

I'm"<

whatisyourname?

\n";

cin>>name1;

cout<<"Well,"<

cout<<"inanarrayof"<

cout<<"Yourinitialis"<

name2[3]='\0';

cout<<"Herearethefirst3charactersofmyname:

"<

return0;

}

4.3instr1.cpp

#include

intmain()

{

usingnamespacestd;

constintArSize=20;

charname[ArSize];

chardessert[ArSize];

cout<<"Enteryourname:

\n";

cin>>name;

cout<<"Enteryourfavoritedessert:

\n";

cin>>dessert;

cout<<"Ihavesomedelicious"<

return0;

}

4.4instr2.cpp

#include

intmain()

{

usingnamespacestd;

constintArSize=20;

charname[ArSize];

chardessert[ArSize];

cout<<"Enteryourname:

\n";

cin.getline(name,ArSize);

cout<<"Enteryourfavoritedessert:

\n";

cin.getline(dessert,ArSize);

cout<<"Ihavesomedelicious"<

return0;

}

4.5instr3.cpp

#include

intmain()

{

usingnamespacestd;

constintArSize=20;

charname[ArSize];

chardessert[ArSize];

cout<<"Enteryourname:

\n";

cin.get(name,ArSize).get();

cout<<"Enteryourfavoritedessert:

\n";

cin.get(dessert,ArSize).get();

cout<<"Ihavesomedelicious"<

return0;

}

4.6numstr.cpp

#include

intmain()

{

usingnamespacestd;

cout<<"Whatyearwasyourhousebuilt?

\n";

intyear;

cin>>year;

cout<<"Whatisitsstreetaddress?

\n";

charaddress[80];

cin.getline(address,80);

cout<<"Yearbuilt:

"<

cout<<"Address:

"<

cout<<"Done!

"<

return0;

}

 

4.7strtype1.cpp

#include

#include

intmain()

{

usingnamespacestd;

charcharr1[20];

charcharr2[20]="jaguar";

stringstr1;

stringstr2="panther";

cout<<"Enterakindoffeline:

";

cin>>charr1;

cout<<"Enteranotherkindoffeline:

";

cin>>str1;

cout<<"Herearesomefelines:

\n"<

cout<<"Thethirdletterin"<

cout<<"Thethirdletterin"<

return0;

}

\

4.8strtype2.cpp

#include

#include

intmain()

{

usingnamespacestd;

strings1="penguin";

strings2,s3;

cout<<"Youcanassignonestringobjecttoanother:

s2=s1\n";

s2=s1;

cout<<"s1:

"<

"<

cout<<"YoucanassignaC-stylestringtoastringobject.\n";

cout<<"s2=\"buzzard\"\n";

s2="buzzard";

cout<<"s2:

"<

cout<<"Youcanconcatenatestring:

s3=s1+s2\n";

s3=s1+s2;

cout<<"s3:

"<

cout<<"Youcanappendstring.\n";

s1+=s2;

cout<<"s1+=s2yieldss1="<

s2+="foraday";

cout<<"s2+=\"foraday\"yieldss2="<

return0;

}

 

4.9strtype3.cpp

#include

#include

#include

intmain()

{

usingnamespacestd;

charcharr1[20];

charcharr2[20]="jaguar";

stringstr1;

stringstr2="panther";

str1=str2;

strcpy(charr1,charr2);

str1+="paste";

strcat(charr1,"juice");

intlen1=str1.size();

intlen2=strlen(charr1);

cout<<"Thestring"<

cout<<"Thestring"<

return0;

}

4.10strtype4.cpp

#include

#include

#include

intmain()

{

usingnamespacestd;

charcharr[20];

stringstr;

cout<<"Lengthofstringincharrbeforeinput:

"<

cout<<"Lengthofstringinstrbeforeinput:

"<

cout<<"Enteralineoftext:

\n";

cin.getline(charr,20);

cout<<"Youentered"<

cout<<"Enteranotherlineoftext:

\n";

getline(cin,str);

cout<<"Youentered:

"<

cout<<"Lengthofstringincharrafterinptu:

"<

cout<<"Lengthofstringinstrafterinput:

"<

return0;

}

4.11structur.cpp

#include

structinflatable

{

charname[20];

floatvolume;

doubleprice;

};

intmain()

{

usingnamespacestd;

inflatableguest={"GloriousGloria",1.88,29.99};

inflatablepal={"AudaciousArthur",3.12,32.99};

cout<<"Expandyourguestlistwith"<

cout<<"and"<

\n";

cout<<"Youcanhavebothfor$";

cout<

\n";

return0;

}

4.12assgn.cpp

#include

structinflatable

{

charname[20];

floatvolume;

doubleprice;

};

intmain()

{

usingnamespacestd;

inflatablebouquet={"sunflowers",0.20,12.49};

inflatablechoice;

cout<<"bouquet:

"<

choice=bouquet;

cout<<"choice:

"<

return0;

}

4.13arrstruc.cpp

#include

structinflatable

{

charname[20];

floatvolume;

doubleprice;

};

intmain()

{

usingnamespacestd;

inflatableguests[2]=

{

{"Bambi",0.5,21.99},

{"Godzilla",2000,565.99}

};

cout<<"Theguest"<

cout<<"haveacombinedvolumeof"<

return0;

}

 

4.14address.cpp

#include

intmain()

{

usingnamespacestd;

intdonuts=6;

doublecups=4.5;

cout<<"donutsvalue="<

cout<<"anddonutsaddress="<<&donuts<

cout<<"cupsvalue="<

cout<<"andcupsaddress="<<&cups<

return0;

}

4.15pointer.cpp

#include

intmain()

{

usingnamespacestd;

intupdates=6;

int*p_updates;

p_updates=&updates;

cout<<"Values:

updates="<

cout<<"Addresses:

&updates="<<&updates<<",p_updates="<

*p_updates=*p_updates+1;

cout<<"Nowupdates="<

return0;

}

 

4.16init_ptr.cpp

#include

intmain()

{

usingnamespacestd;

inthiggens=5;

int*pt=&higgens;

cout<<"Valueofhiggens="<

cout<<"Valueof*pt="<<*pt<<";Valueofpt="<

return0;

}

4.17use_new.cpp

#include

intmain()

{

usingnamespacestd;

intnights=1001;

int*pt=newint;

*pt=1001;

cout<<"nightsvalue="<

location"<<&nights<

cout<<"intvalue="<<*pt<<":

location"<

double*pd=newdouble;

*pd=10000001.0;

cout<<"doublevalue="<<*pd<<":

location:

"<

cout<<"locationofpionterpd:

"<<&pd<

cout<<"sizeofpt="<

sizeof*pt="<

cout<<"sizeofpd="<

sizeof*pd="<

return0;

}

4.18arraynew.cpp

#include

intmain()

{

usingnamespacestd;

double*p3=newdouble[3];

p3[0]=0.2;

p3[1]=0.5;

p3[2]=0.8;

cout<<"p3[1]is"<

p3=p3+1;

cout<<"Nowp3[0]is"<

p3=p3-1;

delete[]p3;

return0;

}

4.19addpnstr.cpp

#include

intmain()

{

usingnamespacestd;

doublewages[3]={10000.0,20000.0,30000.0};

shortstacks[3]={3,2,1};

double*pw=wages;

short*ps=&stacks[0];

cout<<"pw="<

pw=pw+1;

cout<<"add1tothepwpointer:

\n";

pw=pw+1;

cout<<"pw="<

cout<<"ps="<

ps=ps+1;

cout<<"add1tothepspointer:

\n";

cout<<"ps="<

cout<<"accesstwoelementwitharraynotation"<

cout<<"stacks[0]="<

cout<<"accesstwoelementwithpointernotation"<

cout<<"*stacks="<<*stacks<<",*(stacks+1)="<<*(stacks+1)<

cout<

cout<

return0;

}

4.20ptrstr.cpp

#include

#include

intmain()

{

usingnamespacestd;

charanimal[20]="bear";

constchar*bird="wren";

char*ps;

cout<

cout<<"Enterakindofanimal:

";

cin>>animal;

ps=animal;

cout<

"<

cout<<"Beforeusingstrcpy():

\n"<

cout<

ps=newchar[strlen(animal)+1];

strcpy(ps,animal);

cout<<"Afterusingstrcpy():

\n";

cout<

cout<

delete[]ps;

return0;

}

4.21newstrct.cpp

#include

structinflatable

{

charname[20];

floatvolume;

doubleprice;

};

intmain()

{

usingnamespacestd;

inflatable*ps=newinflatable;

cout<<"Enternameofinflatableitem:

";

cin.get(ps->name,20);

cout<<"Entervolumeincubicfeet:

";

cin>>(*ps).volume;

cout<<"Enterprice:

$";

cin>>ps->price;

cout<<"Name:

"<<(*ps).name<

cout<<"Volume:

"<volume<<"cubicfeet\n";

cout<<"Price;$"<price<

deleteps;

return0;

}

4.22delete.cpp

#include

#include

usingnamespacestd;

char*getname(void);

intmain()

{

char*name;

name=getname();

cout<

delete[]name;

name=getname();

cout<

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 工学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1