C++ Primer Plus6 第4单元程序清单Word文件下载.docx

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

C++ Primer Plus6 第4单元程序清单Word文件下载.docx

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

C++ Primer Plus6 第4单元程序清单Word文件下载.docx

cstring>

constintSize=15;

charname1[Size];

charname2[Size]="

C++owboy"

Howdy!

I'

m"

name2<

!

whatisyourname?

\n"

cin>

>

name1;

Well,"

name1<

yournamehas"

strlen(name1)<

lettersandisstored\n"

inanarrayof"

sizeofname1<

bytes."

Yourinitialis"

name1[0]<

."

name2[3]='

\0'

Herearethefirst3charactersofmyname:

"

4.3instr1.cpp

constintArSize=20;

charname[ArSize];

chardessert[ArSize];

Enteryourname:

name;

Enteryourfavoritedessert:

dessert;

Ihavesomedelicious"

dessert<

foryou,"

name<

.\n"

4.4instr2.cpp

cin.getline(name,ArSize);

cin.getline(dessert,ArSize);

4.5instr3.cpp

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

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

4.6numstr.cpp

Whatyearwasyourhousebuilt?

intyear;

year;

Whatisitsstreetaddress?

charaddress[80];

cin.getline(address,80);

Yearbuilt:

year<

Address:

address<

Done!

 

4.7strtype1.cpp

string>

charcharr1[20];

charcharr2[20]="

jaguar"

stringstr1;

stringstr2="

panther"

Enterakindoffeline:

charr1;

Enteranotherkindoffeline:

str1;

Herearesomefelines:

charr1<

charr2<

str1<

str2<

Thethirdletterin"

is"

charr2[2]<

str2[2]<

\

4.8strtype2.cpp

strings1="

penguin"

strings2,s3;

Youcanassignonestringobjecttoanother:

s2=s1\n"

s2=s1;

s1:

s1<

"

s2:

s2<

YoucanassignaC-stylestringtoastringobject.\n"

s2=\"

buzzard\"

s2="

buzzard"

Youcanconcatenatestring:

s3=s1+s2\n"

s3=s1+s2;

s3:

s3<

Youcanappendstring.\n"

s1+=s2;

s1+=s2yieldss1="

s2+="

foraday"

s2+=\"

foraday\"

yieldss2="

4.9strtype3.cpp

str1=str2;

strcpy(charr1,charr2);

str1+="

paste"

strcat(charr1,"

juice"

);

intlen1=str1.size();

intlen2=strlen(charr1);

Thestring"

contains"

len1<

characters.\n"

len2<

characters."

4.10strtype4.cpp

charcharr[20];

stringstr;

Lengthofstringincharrbeforeinput:

strlen(charr)<

Lengthofstringinstrbeforeinput:

str.size()<

Enteralineoftext:

cin.getline(charr,20);

Youentered"

charr<

Enteranotherlineoftext:

getline(cin,str);

Youentered:

str<

Lengthofstringincharrafterinptu:

Lengthofstringinstrafterinput:

4.11structur.cpp

structinflatable

charname[20];

floatvolume;

doubleprice;

};

inflatableguest={"

GloriousGloria"

1.88,29.99};

inflatablepal={"

AudaciousArthur"

3.12,32.99};

Expandyourguestlistwith"

guest.name;

and"

pal.name<

!

Youcanhavebothfor$"

guest.price+pal.price<

4.12assgn.cpp

inflatablebouquet={"

sunflowers"

0.20,12.49};

inflatablechoice;

bouquet:

bouquet.name<

for$"

bouquet.price<

choice=bouquet;

choice:

choice.name<

choice.price<

4.13arrstruc.cpp

inflatableguests[2]=

{

{"

Bambi"

0.5,21.99},

Godzilla"

2000,565.99}

};

Theguest"

guests[0].name<

guests[1].name<

haveacombinedvolumeof"

guests[0].volume+guests[1].volume<

cubicfeet\n"

4.14address.cpp

intdonuts=6;

doublecups=4.5;

donutsvalue="

donuts;

anddonutsaddress="

&

donuts<

cupsvalue="

cups;

andcupsaddress="

cups<

4.15pointer.cpp

intupdates=6;

int*p_updates;

p_updates=&

updates;

Values:

updates="

updates<

*p_updates="

*p_updates<

Addresses:

p_updates="

p_updates<

*p_updates=*p_updates+1;

Nowupdates="

4.16init_ptr.cpp

inthiggens=5;

int*pt=&

higgens;

Valueofhiggens="

higgens<

Addressofhiggens="

Valueof*pt="

*pt<

Valueofpt="

pt<

4.17use_new.cpp

intnights=1001;

int*pt=newint;

*pt=1001;

nightsvalue="

nights<

:

location"

intvalue="

location"

double*pd=newdouble;

*pd=10000001.0;

doublevalue="

*pd<

location:

pd<

locationofpionterpd:

sizeofpt="

sizeof(pt)<

sizeof*pt="

sizeof(*pt)<

sizeofpd="

sizeof(pd)<

sizeof*pd="

sizeof(*pd)<

4.18arraynew.cpp

double*p3=newdouble[3];

p3[0]=0.2;

p3[1]=0.5;

p3[2]=0.8;

p3[1]is"

p3[1]<

p3=p3+1;

Nowp3[0]is"

p3[0]<

andp3[1]is"

p3=p3-1;

delete[]p3;

4.19addpnstr.cpp

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

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

double*pw=wages;

short*ps=&

stacks[0];

pw="

pw<

*pw="

*pw<

pw=pw+1;

add1tothepwpointer:

ps="

ps<

*ps="

*ps<

ps=ps+1;

add1tothepspointer:

accesstwoelementwitharraynotation"

stacks[0]="

stacks[0]<

stacks[1]="

stacks[1]<

accesstwoelementwithpointernotation"

*stacks="

*stacks<

*(stacks+1)="

*(stacks+1)<

sizeof(wages)<

=sizeofwagesarray"

sizeof(pw)<

=sizeofpwpointer"

4.20ptrstr.cpp

charanimal[20]="

bear"

constchar*bird="

wren"

char*ps;

animal<

and"

bird<

Enterakindofanimal:

animal;

ps=animal;

Beforeusingstrcpy():

at"

(int*)animal<

(int*)ps<

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

strcpy(ps,animal);

Afterusingstrcpy():

(int*)animal<

delete[]ps;

4.21newstrct.cpp

inflatable*ps=newinflatable;

Enternameofinflatableitem:

cin.get(ps->

name,20);

Entervolumeincubicfeet:

(*ps).volume;

Enterprice:

$"

ps->

price;

Name:

(*ps).name<

Volume:

volume<

Price;

price<

deleteps;

4.22delete.cpp

usingnamespacestd;

char*getname(void);

char*name;

name=getname();

(int*)name<

delete[]name;

(int*

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

当前位置:首页 > 农林牧渔 > 水产渔业

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

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