版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、4.1 arrayone.cpp#include<iostream>int main()using namespace std;int yams3;yams0=7;yams1=8;yams2=6;int yamcosts3=20,30,5;cout<<"total yams="<<yams0+yams1+yams2<<endl;cout<<"the package with "<<yams1<<" yams costs "<<yamcost
2、s1<<" cents per yam."<<endl;int total=yams0*yamcosts0+yams1*yamcosts1+yams2*yamcosts2;cout<<"the total yam expense is "<<total<<" cents.n"cout<<endl<<"size of yams array= "<<sizeof yams<<" bytes.n"
3、;cout<<"size of yams element= "<<sizeof yams0<<" bytes.n"return 0;4.2 string.cpp#include<iostream>#include<cstring>int main()using namespace std;const int size=15;char name1size;char name2size="c+owboy"cout<<"howdy! i'm "
4、;<<name2<<"!what is your name?n"cin>>name1;cout<<"well, "<<name1<<", your name has "<<strlen(name1)<<" letters and is storedn"cout<<"in an array of "<<sizeof name1<<" bytes."
5、<<endl;cout<<"your initial is "<<name10<<" ."<<endl;name23='0'cout<<"here are the first 3 characters of my name: "<<name2<<endl;return 0;4.3 instr1.cpp#include<iostream>int main()using namespace std;const int
6、 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<<" for you, "<<name<<".n"
7、return 0;4.4 instr2.cpp#include<iostream>int main()using namespace std;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 h
8、ave some delicious "<<dessert<<" for you, "<<name<<".n"return 0;4.5 instr3.cpp#include<iostream>int main()using namespace std;const int arsize=20;char namearsize;char dessertarsize;cout<<"enter your name:n"cin.get(name,arsize).ge
9、t();cout<<"enter your favorite dessert:n"cin.get(dessert,arsize).get();cout<<"i have some delicious "<<dessert<<" for you, "<<name<<".n"return 0;4.6 numstr.cpp#include<iostream>int main()using namespace std;cout<<
10、;"what year was your house built?n"int year;cin>>year;cout<<"what is its street address?n"char address80;cin.getline(address,80);cout<<"year built: "<<year<<endl;cout<<"address: "<<address<<endl;cout<<"
11、;done!"<<endl;return 0;4.7 strtype1.cpp#include<iostream>#include<string>int main()using namespace std;char charr120;char charr220="jaguar"string str1;string str2="panther"cout<<"enter a kind of feline:"cin>>charr1;cout<<"en
12、ter another kind of feline:"cin>>str1;cout<<"here are some felines:n"<<charr1<<" "<<charr2<<" "<<str1<<" "<<str2<<endl;cout<<"the third letter in "<<charr2<<" is
13、"<<charr22<<endl;cout<<"the third letter in "<<str2<<" is "<<str22<<endl;return 0;4.8 strtype2.cpp#include<iostream>#include<string>int main()using namespace std;string s1="penguin"string s2,s3;cout<<"
14、;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=&qu
15、ot;buzzard"cout<<"s2: "<<s2<<endl;cout<<"you can concatenate string:s3=s1+s2n"s3=s1+s2;cout<<"s3: "<<s3<<endl;cout<<"you can append string.n"s1+=s2;cout<<"s1+=s2 yields s1= "<<s1<<
16、;endl;s2+="for a day"cout<<"s2+= "for a day" yields s2= "<<s2<<endl;return 0;4.9 strtype3.cpp#include<iostream>#include<string>#include<cstring>int main()using namespace std;char charr120;char charr220="jaguar"string str1;st
17、ring 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<<&q
18、uot;the string "<<charr1<<" contains "<<len2<<" characters."<<endl;return 0;4.10 strtype4.cpp#include<iostream>#include<string>#include<cstring>int main()using namespace std;char charr20;string str;cout<<"length of s
19、tring in charr before input: "<<strlen(charr)<<endl;cout<<"length of string in str before input:"<<str.size()<<endl<<endl;cout<<"enter a line of text:n"cin.getline(charr,20);cout<<"you entered "<<charr<<en
20、dl;cout<<"enter another line of text:n"getline(cin,str);cout<<"you entered: "<<str<<endl<<endl;cout<<"length of string in charr after inptu:"<<strlen(charr)<<endl;cout<<"length of string in str after input:"
21、;<<str.size()<<endl;return 0;4.11 structur.cpp#include<iostream>struct inflatablechar name20;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<<"expa
22、nd your guest list with "<<;cout<<" and "<<<<" !n"cout<<"you can have both for $"cout<<guest.price+pal.price<<" !n"return 0;4.12 assgn.cpp#include<iostream>struct inflatablechar name20; flo
23、at volume;double price;int main()using namespace std;inflatable bouquet="sunflowers",0.20,12.49;inflatable choice;cout<<"bouquet: "<<<<" for $"<<bouquet.price<<endl;choice=bouquet;cout<<"choice: "<<choice.n
24、ame<<" for $"<<choice.price<<endl;return 0;4.13 arrstruc.cpp#include<iostream>struct inflatablechar name20; float volume;double price;int main()using namespace std;inflatable guests2="bambi",0.5,21.99,"godzilla",2000,565.99;cout<<"the
25、guest "<<<<" and "<<<<endl;cout<<"have a combined volume of "<<guests0.volume+guests1.volume<<" cubic feetn"return 0;4.14 address.cpp#include<iostream>int main()using namespace std;int donuts=6
26、;double cups=4.5;cout<<"donuts value="<<donuts;cout<<" and donuts address ="<<&donuts<<endl; cout<<"cups value="<<cups;cout<<" and cups address ="<<&cups<<endl;return 0;4.15 pointer.cpp#includ
27、e<iostream>int main()using namespace std;int updates=6;int *p_updates;p_updates=&updates;cout<<"values:updates="<<updates<<",*p_updates="<<*p_updates<<endl;cout<<"addresses:&updates="<<&updates<<",p_
28、updates="<<p_updates<<endl;*p_updates=*p_updates+1;cout<<"now updates="<<updates<<endl;return 0;4.16 init_ptr.cpp#include<iostream>int main()using namespace std;int higgens=5;int *pt=&higgens;cout<<"value of higgens="<<higg
29、ens<<"address of higgens="<<&higgens<<endl;cout<<"value of *pt="<<*pt<<"value of pt="<<pt<<endl;return 0;4.17 use_new.cpp#include<iostream>int main()using namespace std;int nights=1001;int *pt=new int;*pt=1001; c
30、out<<"nights value="<<nights<<": location "<<&nights<<endl;cout<<"int value="<<*pt<<":location "<<pt<<endl;double *pd=new double;*pd=10000001.0;cout<<"double value="<<*pd<
31、<":location: "<<pd<<endl;cout<<"location of pionter pd:"<<&pd<<endl;cout<<"size of pt="<<sizeof(pt)<<": size of *pt="<<sizeof(*pt)<<endl;cout<<"size of pd="<<sizeof(pd)<
32、;<": size of *pd="<<sizeof(*pd)<<endl;return 0;4.18 arraynew.cpp#include<iostream>int main()using namespace std;double *p3=new double 3;p30=0.2;p31=0.5;p32=0.8;cout<<"p31 is "<<p31<<".n"p3=p3+1;cout<<"now p30 is "&l
33、t;<p30<<" and p31 is "<<p31<<".n"p3=p3-1;delete p3;return 0;4.19 addpnstr.cpp#include<iostream>int main()using namespace std;double wages3=10000.0,20000.0,30000.0;short stacks3=3,2,1;double *pw=wages;short *ps=&stacks0;cout<<"pw="<
34、;<pw<<",*pw="<<*pw<<endl;pw=pw+1;cout<<"add 1 to the pw pointer:n"pw=pw+1;cout<<"pw="<<pw<<",*pw="<<*pw<<endl<<endl;cout<<"ps="<<ps<<",*ps="<<*ps<<
35、endl;ps=ps+1;cout<<"add 1 to the ps pointer:n"cout<<"ps="<<ps<<",*ps="<<*ps<<endl<<endl;cout<<"access two element with array notation"<<endl;cout<<"stacks0="<<stacks0<<",sta
36、cks1="<<stacks1<<endl;cout<<"access two element with pointer notation"<<endl;cout<<"*stacks="<<*stacks<<",*(stacks+1)="<<*(stacks+1)<<endl;cout<<sizeof(wages)<<"=size of wages array"<<
37、;endl;cout<<sizeof(pw)<<"=size of pw pointer"<<endl;return 0;4.20 ptrstr.cpp#include<iostream>#include<cstring>int main()using namespace std;char animal20="bear"const char *bird="wren"char *ps;cout<<animal<<"and"<&l
38、t;bird<<endl;cout<<"enter a kind of animal:"cin>>animal; ps=animal;cout<<ps<<"!"<<endl;cout<<"before using strcpy():n"<<animal<<" at "<<(int *) animal<<endl;cout<<ps<<" at "
39、;<<(int *) ps<<endl;ps=new charstrlen(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;4.21 newstrc
40、t.cpp#include<iostream>struct inflatablechar name20;float volume;double price;int main()using namespace std;inflatable *ps=new inflatable;cout<<"enter name of inflatable item:"cin.get(ps->name,20);cout<<"enter volume in cubic feet:"cin>>(*ps).volume;cou
41、t<<"enter price:$"cin>>ps->price;cout<<"name:"<<(*ps).name<<endl;cout<<"volume:"<<ps->volume<<" cubic feetn"cout<<"price;$"<<ps->price<<endl;delete ps;return 0;4.22 delete.cpp
42、#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;4.23 mixtypes.cpp#include<iostream>struct antarctica_years_endint year;int main()using namespace std;antarctica_years_end s01,s
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 1632.1-2024塑料使用毛細(xì)管黏度計(jì)測(cè)定聚合物稀溶液黏度第1部分:通則
- 二零二五年度建筑工程質(zhì)量檢測(cè)與施工質(zhì)量控制規(guī)范合同3篇
- 2025年度跨境電商物流配送服務(wù)合同范本9篇
- 2024版影院室內(nèi)裝修設(shè)計(jì)合同書
- 二零二五年度綠色環(huán)保產(chǎn)業(yè)合作框架協(xié)議書范本3篇
- 2025年抵債合同房屋買賣協(xié)議3篇
- 2025年度智能停車場(chǎng)2噸不銹鋼帶打印功能電子地磅秤租賃合同6篇
- 2024版租賃商鋪合同書
- 2024標(biāo)準(zhǔn)型料場(chǎng)地租賃合同模板一
- 二零二五年房產(chǎn)租賃保證金繳納及退還協(xié)議6篇
- 林區(qū)防火專用道路技術(shù)規(guī)范
- 2023社會(huì)責(zé)任報(bào)告培訓(xùn)講稿
- 2023核電廠常規(guī)島及輔助配套設(shè)施建設(shè)施工技術(shù)規(guī)范 第8部分 保溫及油漆
- 2025年蛇年春聯(lián)帶橫批-蛇年對(duì)聯(lián)大全新春對(duì)聯(lián)集錦
- 表B. 0 .11工程款支付報(bào)審表
- 警務(wù)航空無人機(jī)考試題庫及答案
- 空氣自動(dòng)站儀器運(yùn)營維護(hù)項(xiàng)目操作說明以及簡(jiǎn)單故障處理
- 新生兒窒息復(fù)蘇正壓通氣課件
- 法律顧問投標(biāo)書
- 班主任培訓(xùn)簡(jiǎn)報(bào)4篇(一)
- 成都市數(shù)學(xué)八年級(jí)上冊(cè)期末試卷含答案
評(píng)論
0/150
提交評(píng)論