




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、 ex2.1-display your n ame and address #in clude int main (void) using n amespace std; coutMy n ame is liao chun gua ng and I live in hunan che nzhou.n ex2.2-c onvert the furl ong un its to yard ui nts-把浪單位換位碼單位 #in clude double fur2yd(double); int main() using n amespace std; coute nter the dista ne
2、e measured by furl ong un its:; double fur; cinfur; coutc onvert the furl ong to yarde ndl; double yd; yd=fur2yd(fur); coutvvfurvv furl ong is yd yarde ndl; return 0; l i*士a1i double fur2yd(double t) return 220*t; ex2.3-每個(gè)函數(shù)都被調(diào)用兩次 #in clude .I - . void mice(); void see(); using n amespace std; int m
3、ain() mice(); mice(); see(); see(); return 0; void mice() coutthree bli nd micee ndl; void see() coutsee how they runen dl; ex2.4 #in clude int mai n() using n amespace std; cout age; in t mon th; mon th=age*12; coutage years is mon th mon thse ndl; return 0; ex2.5-convert the Celsius valve to Fahre
4、 nheit value #in clude double C2F(double); int main() using n amespace std; coutplease en ter a Celsius value:; double C; cin C; double F; F=C2F(C); coutvvCvv degrees Celsius is F degrees Fahre nheit.e ndl; return 0; i? double C2F(double t) return 1.8*t+32; ex2.6-c onvert the light years valve to as
5、tr ono mical un its-把光年轉(zhuǎn)換為天文單位 #in clude double con vert(double);/函數(shù)原型 int main() using n amespace std; cout light_years; double astro_ un its; astro_ un its=co nv ert(light_years); coutlight_years light_years = astro_u ni ts astro nomical un its.ve ndl; 僅供參考 return 0; double conv ert(double t) retu
6、rn 63240*t;/1 光年=63240 天文單位 ex2.7-顯示用戶輸入的小時(shí)數(shù)和分鐘數(shù) #in clude void show(); mai n() using n amespace std; show(); return 0; void show() using n amespace std; int h,m; cout h; coutm; coutvvTime:vhvv:vvmvve ndl; 第三章:處理數(shù)據(jù) /ex3.1 將身高用英尺(feet)和英寸(inch)表示 英尺=12英寸 表示為退格字符 #in clude const int in ch_per_feet=12;
7、 const常量-1feet=12 in ches-1 int main() using n amespace std; cout ht_i nch; int ht_feet=ht_i nch/i nch_per_feet;/取商 int rm_i nch=ht_i nch%i nch_per_feet;取余 coutyour height is ht_feet feet,a nd rm_i nch in chesn; return 0; /ex3.2- 計(jì)算相應(yīng)的body mass index (體重指數(shù)) #in clude const int in ch_per_feet=12; con
8、st double meter_per_i nch=0.0254; const double poun d_per_kilogram=2.2; int main() using n amespace std; coutPlease en ter your height:e ndl; coutFirst,e nter your height of feet part(輸入你身高的英尺部分):_b; int ht_feet; cin ht_feet; coutSeco nd,e nter your height of inch part(輸入你身高的英寸部分):_b; int ht_in ch;
9、cin ht_i nch; coutNow,please en ter your weight in pound:bbb; double wt_po und; cin wt_po und; int in ch; in ch=ht_feet*i nch_per_feet+ht_i nch; double ht_meter; ht_meter=i nch*meter_per_i nch; double wt_kilogram; wt_kilogram=wt_po un d/po un d_per_kilogram; coute ndl; coutYour pensonal body in form
10、atio n as follows:e ndl; cout身高:inch(英尺 inch)n 身高:ht_meter( 米 meter)n 體重:wt_kilogram(千克 kilogram)n; double BMI; BMI=wt_kilogram/(ht_meter*ht_meter); coutyour Body Mass Index( 體重指數(shù))is BMIendl; return 0; ex3.3以度,分,秒輸入,以度輸出 #in clude const int minu tes_per_degree=60; const int sec on ds_per_m inu te=60
11、; int ma in() using n amespace std; coutE nter a latitude in degrees, minu tes,a nd sec on ds:n; cout degree; coutNext,e nter the mi nu tes of arc:; int min ute; cinminu te; cout sec ond; double show_i n_degree; show_in_degree=(double)degree+(double)mi nute/mi nu tes_per_degree+(double)sec ond/mi nu
12、 tes_per_degree/seco nds_per_mi nu te; coutdegree degrees, minu te minu tes,sec on dsec onds =show_i n_degree degreesn; return 0; ex3.4 #in clude const int hours_per_day=24; const int minu tes_per_hour=60; const int sec on ds_per_m inu te=60; int main() using n amespace std; cout sec on ds; int Day,
13、Hour,M inu te,Sec ond; Day=sec on ds/sec on ds_per_mi nute/mi nu tes_per_hour/hours_per_day; Hour=sec on ds/seco nds_per_mi nute/mi nu tes_per_hour%hours_per_day; Min ute=seco nds/sec on ds_per_mi nute%mi nu tes_per_hour; Secon d=sec on ds%sec on ds_per_m inu te; coutsec on dssec onds = Day days,Hou
14、r hours, Minu te minu tes,Sec on d. i #in clude int mai n() using n amespace std; cout world_populati on; coutE nter the populati on of the US:; long long US_populati on; cinU S_populati on; double perce ntage; perce ntage=(double)US_populati on/world_populati on *100; coutThe population of the US i
15、s percentage% of the world population.n; return 0; /ex3.6? 汽車耗油量-美國(guó)(mpg)or歐洲風(fēng)格(L/100Km) #in clude int main() ?us ing n amespace std; ?coutE nter the miles of dista nee you have drive n:; ?double m_dista nee; ?cin m_dista nee; ?cout m_gasoli ne; ?coutvYour car can run m_dista nce/m_gasoli ne miles pe
16、r gallo nn; ?coutComputi ng by Europea n style:n; ?cout k_dista nce; ?cout k_gasoli ne; ?coutl n Europea n style:your can used 100*k_gasoli ne/k_dista nce liters of petrol per 100 kilometersn; ?return 0; 1 I -、 /ex3.7 automobile gasoline consumption-耗油量-歐洲風(fēng)格(L/100Km)轉(zhuǎn)換成美國(guó)風(fēng)格(mpg) #in clude int main()
17、 using n amespace std; coutEnter the automobile gasoline consumption figure inn Euro_style; coutC on verts to U.S. style(miles per gallo n):e ndl; coutEuro_style L/100Km = 62.14*3.875/Euro_style mpgn; return 0; / Note that 100 kilometers is 62.14 miles, and 1 gallon is 3.875 liters. /Thus, 19 mpg is
18、 about 12.4 L/100Km, and 27 mpg is about 8.7 L/100Km. Enter the automobile gasoline consumption figure in European style(liters per 100 kilometers):12.4 Conv erts to U.S. style(miles per gallo n): 12.4 L/100Km = 19.4187 mpg Press any key to con ti nue / ex3.7 automobile gasoline consumption-耗油量-美國(guó)風(fēng)格
19、(mpg)轉(zhuǎn)換成歐洲風(fēng)格(L/100Km) #in clude int main() using n amespace std; coutEnter the automobile gasoline consumption figure inn US_style; coutC on verts to Europea n style(miles per gall on ):e ndl; coutUS_style mpg = 62.14*3.875/US_styleL/100Kmn; return 0; / Enter the automobile gasoline consumption figu
20、re in U.S. style(miles per gallo n) :19 Conv erts to Europea n style(miles per gallo n): 19 mpg = 12.6733L/100Km Press any key to con ti nue 第四章復(fù)合類型 ex4.1 display the in formati on of stude nt #in clude const int Asize=20; using n amespace std; struct stude nt/定義結(jié)構(gòu)描述 char first nameAsize; char last
21、nameAsize; char grade; int age; ; void display(stude nt);函數(shù)原型放在結(jié)構(gòu)描述后 int main() coutwhat is your first n ame?e ndl; stude nt lcg;/創(chuàng)建結(jié)構(gòu)變量(結(jié)構(gòu)數(shù)據(jù)對(duì)象) cin .getli ne(lcg.first name,Asize); coutwhat is your last n ame?e ndl; cin .getli ne(lcg.last name,Asize); coutwhat letter grade do you deserve? lcg.grade
22、; coutwhat is your age? lcg.age; display(lcg); return 0; void display(stude nt n ame) coutName: n ame.first name, n ame .l ast namee ndl; coutvGrade:vchar( name.grade+1)e ndl; coutAge: n ame.agee ndl; /ex4.2 use the stri ng-class in stead of char-array #in clude #in clude int main() using n amespace
23、 std; stri ng n ame,dessert; coutE nter your n ame: n; getl in e(ci n,n ame); coutE nter your favorite dessert: n; getl in e(ci n, dessert); coutI have some delicious dessert; cout for you, n amesbumpc(); break; ex4.3輸入其名和姓,并組合顯示 #in clude #in clude const int Asize=20; int main() using n amespace st
24、d; char fnameAsize; char In ameAsize; char full name2*Asize+1; coutE nter your first n ame:;/ cin .getli ne(fname,Asize); coutE nter your last n ame:;/ cin .getli ne(l name,Asize); strncpy(full name,l name,Asize); strcat(full name,);/把“, strn cat(full name,fname,Asize); /修改后的 I; 輸入名字,存儲(chǔ)在fname數(shù)組中 輸入姓
25、,存儲(chǔ)在Iname數(shù)組中 把姓l(shuí)n ame復(fù)制到full name空數(shù)組中 ”附加到上述full name尾部 把fname名字附加到上述full name尾部 僅供參考 顯示組合結(jié)果 fullname 2*Asize =0;/為防止字符型數(shù)組溢出,在數(shù)組結(jié)尾添加結(jié)束符 coutHeres the information in a single string:fullnameendl;/ return 0; /ex4.4 使用string對(duì)象存儲(chǔ)、顯示組合結(jié)果 #in clude #in clude int main() using n amespace std; stri ng fname
26、,ln ame,attach,full name; coutE nter your first n ame:; getline(cin,fname);/note:將一行輸入讀取到 string 類對(duì)象中使用的是 getline(cin,str) /它沒(méi)有使用句點(diǎn)表示法,所以不是類方法 coutE nter your last n ame:; getli ne(ci n,ln ame); attach=,; full name=In ame+attach+f name; coutHeres the information in a single string:fullnameendl; retu
27、rn 0; ex4.5 declare a struct and in itialize it聲明結(jié)果并創(chuàng)建一個(gè)變量 #in clude const int Asize=20; struct Can dyBar char bran dAsize; double weight; int calory; ; int main() using n amespace std; Ca ndyBar sn ack=Mocha Mun ch,2.3,350; coutHeres the information of snack:n; coutbra nd:s nack.bra nde ndl; coutwe
28、ight:s nack.weighte ndl; coutcalory:s nack.caloryvve ndl; return 0; /ex4.6結(jié)構(gòu)數(shù)組的聲明及初始化 #in clude const int Asize=20; struct Can dyBar char bran dAsize; double weight; int calory; ; int main() using n amespace std; Can dyBar sn ack3= Mocha Mu nch,2.3,350, XuFuJi,1.1,300, Alps,0.4,100 ; for(int i=0;i3;
29、i+)利用for循環(huán)來(lái)顯示snack變量的內(nèi)容 couts nacki.bra nde ndl s nacki.weighte ndl s nacki.calorye ndle ndl; return 0; ex4.7 pizza披薩餅 #in clude #in clude const int Size=20; struct pizza/ 聲明結(jié)構(gòu) char compa ny Size; double diameter; double weight; ; int main() using n amespace std; pizza pie;/ 創(chuàng)建一個(gè)名為pie的結(jié)構(gòu)變量 coutWhats
30、 the n ame of pizza compa ny:; cin. getl in e(pa ny ,Size); cout pie.diameter; cout pie.weight; coutcompa ny:pa nyen dl; I coutvdiameter:vvpie.diametervi nchese ndl; coutvweight:vvpie.weightvo un chese ndl; return 0; /ex4.8 pizza pie 披薩餅 使用new創(chuàng)建動(dòng)態(tài)結(jié)構(gòu) #in clude #in clude const int Size=20; struct pizz
31、a/ 聲明結(jié)構(gòu) char compa ny Size; double diameter; double weight; ; int main() using n amespace std; pizza *pie=new pizza;/ 使用 new倉(cāng)U建動(dòng)態(tài)結(jié)構(gòu) coutWhats the diameter of pizza:; cin pie-diameter; cin. get();讀取下一個(gè)字符 coutcompa ny,Size); cout pie-weight; coutdiameter in chese ndl; coutcompa nyen dl; coutweightv ou
32、n chese ndl; delete pie;/delete釋放內(nèi)存 return 0; ex.4.9使用new動(dòng)態(tài)分配數(shù)組一方法1 #in clude #in clude using n amespace std; struct Can dyBar stri ng brand; double weight; int calory; ; int main() Can dyBar *sn ack= new Can dyBar3; snack0.brand=A;單個(gè)初始化由new動(dòng)態(tài)分配的內(nèi)存 sn ack0.weight=1.1; I sn ack0.calory=200; sn ack1.b
33、ra nd=B; sn ack1.weight=2.2; sn ack1.calory=400; sn ack2.bra nd=C; sn ack2.weight=4.4; sn ack2.calory=500; for(int i=0;i3;i+) cout bran d: sn acki.bra nd en dl; cout weight: sn acki.weight en dl; cout calorie: sn acki.calory en dle ndl; delete sn ack; return 0; 方法1 僅供參考 #i nclude int main() using n
34、amespace std; const int Size = 3; int successSize; cout success0success1success2; coutvsuccess1:vsuccess0vve ndl; coutvsuccess2:vvsuccess1ve ndl; coutvsuccess3:vsuccess2vve ndl; double average=(success0+success1+success2)/3; coutaverage:averagee ndl; return 0; 方法2 #i nclude #in clude int main() usin
35、g n amespace std; arrayad=0; cout ad0ad1ad2; coutvsuccess1:vad0vve ndl; coutvsuccess2:vvad1ve ndl; coutvsuccess3:vad2vve ndl; ad3=(ad0+ad1+ad2)/3; coutaverage:ad3e ndl; return 0; :J二第五章循環(huán)和關(guān)系表達(dá)式 /ex.5.1 #in clude int mai n() using n amespace std; cout n um2; int sum=0; for(i nt temp=n um1;temp=n um2;
36、+temp)/or temp+ sum+=temp; coutThe sum from n um1 to n um2 is sume ndl; return 0; 僅供參考 ex.5.2 #in elude #in clude int mai n() using n amespace std; array vlong double,101ad=0; ad1=ad0=1L; for(int i=2;i101;i+) adi=i*adi-1; for(int i=0;i101;i+) coutvvivv! = vadive ndl; return 0; ex.5.3 #in elude int m
37、ai n() using n amespace std; coutPlease en ter an in teger:; int sum=0,n um; while(ci nnum) coutSo far, the sum is vsumve ndl; coutPlease en ter an in teger:; return 0; i /ex.5.4 #i nclude int main() using n amespace std; double sum1,sum2; sum1=sum2=0.0; int year=0; while(sum2=sum1) +year; 僅供參考 sum1
38、+=10; sum2=(100+sum2)*0.05+sum2; cout經(jīng)過(guò)vvyearvv年后,Cleo的投資價(jià)值才能超過(guò) Daphne的投資價(jià)值。endl; cout此時(shí),Cleo的投資價(jià)值為sum1,而Daphne的投資價(jià)值為sum2endl; return 0; ex.5.5 #in clude const int MONTHS = 12; const char* mon thsMONTHS=Ja nuary,February,March,April,May,Ju ne,July,August,September,October,No vember,December; int mai
39、 n() using n amespace std; 、 int salesMONTHS,sum=O; for(int i=0;iMONTHS;i+) cout請(qǐng)輸入在monthsi salesi; sum+=salesi; cout這一年中的 C+ For Fools 的總銷售量為:vvsumvvendl; return 0; ex.5.6 #in clude const int MONTHS = 12;. - const char* mon thsMONTHS=Ja nuary,February,March,April,May,Ju ne,July,August,September,October,No vember,December; con st char* years3=第一年,
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度稻谷種植基地風(fēng)險(xiǎn)防范合同
- 2025至2030年中國(guó)精油柔和祛角質(zhì)露數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 二零二五年度水果種植與鄉(xiāng)村旅游融合發(fā)展協(xié)議
- 2025至2030年中國(guó)精密π尺數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025年度綠色環(huán)保街區(qū)店面租賃合同
- 知識(shí)產(chǎn)權(quán)保護(hù)與人才培養(yǎng)的國(guó)際化趨勢(shì)分析
- 知識(shí)產(chǎn)權(quán)與商業(yè)秘密的全球化挑戰(zhàn)
- Unit 2 教學(xué)設(shè)計(jì) 2024-2025學(xué)年人教版八年級(jí)英語(yǔ)上冊(cè)
- 科技企業(yè)利用聯(lián)通政企部資源進(jìn)行產(chǎn)品推廣的策略
- 社區(qū)零售行業(yè)模式探索以集市為例的盈利之道
- 高中轉(zhuǎn)學(xué)申請(qǐng)書(shū)
- 2025年中國(guó)建材集團(tuán)所屬中建材聯(lián)合投資有限公司招聘筆試參考題庫(kù)附帶答案詳解
- 2025年蘇州工業(yè)園區(qū)服務(wù)外包職業(yè)學(xué)院高職單招職業(yè)技能測(cè)試近5年??及鎱⒖碱}庫(kù)含答案解析
- 小學(xué)生拗九節(jié)課件
- 人教版PEP小學(xué)五年級(jí)英語(yǔ)下冊(cè)全冊(cè)教案(含計(jì)劃)
- 南方全站儀NTS-332R說(shuō)明書(shū)
- 2023湖南文藝出版社五年級(jí)音樂(lè)下冊(cè)全冊(cè)教案
- 中職數(shù)學(xué)單招一輪總復(fù)習(xí)《集合》復(fù)習(xí)課件
- DL-T 572-2021電力變壓器運(yùn)行規(guī)程-PDF解密
- 教科版四下科學(xué)《植物的生長(zhǎng)變化》單元解讀(新教材解讀)
- 公司員工離職申請(qǐng)表
評(píng)論
0/150
提交評(píng)論