c++primerplus(第六版)課后編程練習(xí)答案(最新整理)_第1頁
c++primerplus(第六版)課后編程練習(xí)答案(最新整理)_第2頁
c++primerplus(第六版)課后編程練習(xí)答案(最新整理)_第3頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、第二章:開始學(xué)習(xí) c+/ex2.1-display your name and address #includeint main(void)using namespace std;coutmy name is liao chunguang and i live in hunan chenzhou.n”;/ex2.2-convert the furlong units to yard uints-把浪單位換位碼單位#includedouble fur2yd(double); int main()using namespace std;coutfur;coutconvert the furlong

2、 to yardendl; double yd;yd=fur2yd(fur);coutfur furlong is yd yardendl; return 0;double fur2yd(double t)return 220*t;/ex2.3-每個(gè)函數(shù)都被調(diào)用兩次#includevoid mice(); void see();using namespace std; int main()mice();mice();see();see(); return 0;void mice()coutthree blind miceendl;void see()coutsee how they runen

3、dl;/ex2.4 #include int main()using namespace std; coutage; int month;month=age*12;coutage years is month monthsendl; return 0;/ex2.5-convert the celsius valve to fahrenheit value #includedouble c2f(double); int main()using namespace std;coutc; double f; f=c2f(c);coutc degrees celsius is f degrees fa

4、hrenheit.endl; return 0;double c2f(double t)return 1.8*t+32;/ex2.6-convert the light years valve to astronomical units-把光年轉(zhuǎn)換為天文單位#includedouble convert(double);/函數(shù)原型int main()using namespace std;coutlight_years; double astro_units;astro_units=convert(light_years);coutlight_years light_years = astro_

5、units astronomical units.endl; return 0;doubleconvert(double t)return 63240*t;/1 光年=63240 天文單位/ex2.7-顯示用戶輸入的小時(shí)數(shù)和分鐘數(shù)#includevoid show(); main()using namespace std; show();return 0;void show()using namespace std; int h,m;couth;coutm; couttime:h:mendl;第三章:處理數(shù)據(jù)/ex3.1將身高用英尺(feet)和英寸(inch)表示#includeconst

6、int inch_per_feet=12;/const 常量-1feet=12inches-1 英尺=12 英寸int main()using namespace std;coutht_inch;int ht_feet=ht_inch/inch_per_feet;/取商int rm_inch=ht_inch%inch_per_feet;/取余coutyour height is ht_feet feet,and rm_inch inchesn; return 0;/ex3.2-計(jì)算相應(yīng)的 body mass index(體重指數(shù)) #includeconst int inch_per_feet

7、=12;const double meter_per_inch=0.0254; const double pound_per_kilogram=2.2; int main()using namespace std;coutplease enter your height:endl;coutht_feet;coutht_inch;coutwt_pound; int inch;inch=ht_feet*inch_per_feet+ht_inch; double ht_meter; ht_meter=inch*meter_per_inch; double wt_kilogram;wt_kilogra

8、m=wt_pound/pound_per_kilogram; coutendl;coutyour pensonal body information as follows:endl;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 以度,分,秒輸入,以度輸出#includeconst int

9、 minutes_per_degree=60; const int seconds_per_minute=60; int main()using namespace std;coutenter a latitude in degrees,minutes,and seconds:n; coutdegree;coutminute;coutsecond;double show_in_degree; show_in_degree=(double)degree+(double)minute/minutes_per_degree+(double)second/minutes_per_degree/seco

10、nds_per_minute;coutdegreedegrees,minuteminutes,secondseconds=show_in_degree degreesn; return 0;/ex3.4 #includeconst int hours_per_day=24; const int minutes_per_hour=60; const int seconds_per_minute=60; int main()using namespace std;coutseconds;int day,hour,minute,second; day=seconds/seconds_per_minu

11、te/minutes_per_hour/hours_per_day; hour=seconds/seconds_per_minute/minutes_per_hour%hours_per_day; minute=seconds/seconds_per_minute%minutes_per_hour;second=seconds%seconds_per_minute;coutsecondsseconds=daydays,hourhours,minute minutes,second secondsn;return 0;/ex3.5 #include int main()using namespa

12、ce std;coutworld_population;coutus_population;double percentage; percentage=(double)us_population/world_population*100;coutthe population of the us is percentage% of the world population.n; return 0;/ex3.6 汽車耗油量-美國(mpg)or 歐洲風(fēng)格(l/100km) #includeint main()using namespace std;coutm_distance;coutm_gasol

13、ine;coutyour car can run m_distance/m_gasoline miles per gallonn; coutcomputing by european style:n;coutk_distance;coutk_gasoline;coutin european style:your can used 100*k_gasoline/k_distance liters of petrol per 100 kilometersn;return 0;/ex3.7automobile gasoline consumption-耗油量-歐洲風(fēng)格(l/100km)轉(zhuǎn)換成美國風(fēng)格

14、(mpg) #includeint main()using namespace std;coutenter the automobile gasoline consumption figure inneuro_style;coutconverts to u.s. style(miles per gallon):endl; 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./th

15、us, 19 mpg is about 12.4 l/100km, and 27 mpg is about 8.7 l/100km. enter the automobile gasoline consumption figure ineuropean style(liters per 100 kilometers):12.4 converts to u.s. style(miles per gallon):12.4 l/100km = 19.4187 mpgpress any key to continue/ ex3.7automobile gasoline consumption-耗油量-

16、美國風(fēng)格(mpg)轉(zhuǎn)換成歐洲風(fēng)格(l/100km) #includeint main()using namespace std;coutenter the automobile gasoline consumption figure innus_style;coutconverts to european style(miles per gallon):endl; coutus_style mpg = 62.14*3.875/us_stylel/100kmn; return 0;/ enter the automobile gasoline consumption figure inu.s.

17、style(miles per gallon):19converts to european style(miles per gallon): 19 mpg = 12.6733l/100kmpress any key to continue第四章 復(fù)合類型/ex4.1 display the information of student #includeconst int asize=20; using namespace std;struct student/定義結(jié)構(gòu)描述char firstnameasize; char lastnameasize; char grade;int age;v

18、oid display(student);/函數(shù)原型放在結(jié)構(gòu)描述后int main()coutwhat is your first name?endl; student lcg;/創(chuàng)建結(jié)構(gòu)變量(結(jié)構(gòu)數(shù)據(jù)對(duì)象) cin.getline(lcg.firstname,asize);coutwhat is your last name?endl; cin.getline(lcg.lastname,asize);coutwhat letter grade do you deserve?lcg.grade;coutwhat is your age?lcg.age;display(lcg); return

19、0;void display(student name)coutname: name.firstname,name.lastnameendl; coutgrade:char(name.grade+1)endl; coutage:name.ageendl;/ex4.2 use the string-class instead of char-array #include#include int main()using namespace std; string name,dessert;coutenter your name: n; getline(cin,name);coutenter you

20、r favorite dessert: n; getline(cin,dessert);couti have some delicious dessert; cout for you, namesbumpc();/修改后的break; ex4.3 輸入其名和姓,并組合顯示#include #includeconst int asize=20; int main()using namespace std; char fnameasize; char lnameasize;char fullname2*asize+1;coutenter your first name:;/輸入名字,存儲(chǔ)在 fna

21、me數(shù)組中cin.getline(fname,asize);coutenter your last name:;/輸入姓,存儲(chǔ)在 lname數(shù)組中cin.getline(lname,asize);strncpy(fullname,lname,asize);/把姓 lname 復(fù)制到 fullname 空數(shù)組中strcat(fullname, );/把“, ”附加到上述 fullname 尾部strncat(fullname,fname,asize);/把 fname 名字附加到上述 fullname 尾部fullname2*asize=0;/為防止字符型數(shù)組溢出,在數(shù)組結(jié)尾添加結(jié)束符couth

22、eres the information in a single string:fullnameendl;/顯示組合結(jié)果return 0;/ex4.4 使用 string 對(duì)象 存儲(chǔ)、顯示組合結(jié)果#include #include int main()using namespace std;string fname,lname,attach,fullname; coutenter your first name:;getline(cin,fname);/note:將一行輸入讀取到 string 類對(duì)象中使用的是 getline(cin,str)/它沒有使用句點(diǎn)表示法,所以不是類方法couten

23、ter your last name:;getline(cin,lname); attach=, ;fullname=lname+attach+fname;coutheres the information in a single string:fullnameendl; return 0;/ex4.5 declare a struct and initialize it 聲明結(jié)果并創(chuàng)建一個(gè)變量#include const int asize=20; struct candybarchar brandasize; double weight; int calory;int main()usin

24、g namespace std;candybar snack=mocha munch,2.3,350; coutheres the information of snack:n; coutbrand:snack.brandendl; coutweight:snack.weightendl; coutcalory:snack.caloryendl; return 0;/ex4.6 結(jié)構(gòu)數(shù)組的聲明及初始化#include const int asize=20; struct candybarchar brandasize; double weight; int calory;int main()u

25、sing namespace std;candybar snack3=mocha munch,2.3,350,xufuji,1.1,300,alps,0.4,100;for(int i=0;i3;i+)/利用 for 循環(huán)來顯示 snack 變量的內(nèi)容coutsnacki.brandendlsnacki.weightendlsnacki.caloryendlendl;return 0;/ex4.7 pizza 披薩餅#include #include const int size=20;struct pizza/聲明結(jié)構(gòu)char companysize; double diameter; do

26、uble weight;int main()using namespace std;pizza pie;/創(chuàng)建一個(gè)名為 pie 的結(jié)構(gòu)變量coutwhats thename of pizza company:; cin.getline(pany,size);coutpie.diameter;coutpie.weight; coutcompany:panyendl; coutdiameter:pie.diameterinchesendl; coutweight:pie.weightounchesendl; return 0;/ex4.8 pizza pie 披薩餅 使

27、用 new 創(chuàng)建動(dòng)態(tài)結(jié)構(gòu)#include#includeconst int size=20; struct pizza/聲明結(jié)構(gòu)char companysize; double diameter; double weight;int main()using namespace std;pizza *pie=new pizza;/使用 new 創(chuàng)建動(dòng)態(tài)結(jié)構(gòu)coutpie-diameter;cin.get();/讀取下一個(gè)字符coutcompany,size);coutpie-weight;coutdiameter:diameter inchesendl; coutcompany:companye

28、ndl; coutweight:weight ounchesendl; delete pie;/delete 釋放內(nèi)存return 0;/ex.4.9 使用 new 動(dòng)態(tài)分配數(shù)組方法 1 #include#include using namespace std; struct candybarstring brand; doubleweight; int calory;int main()candybar *snack= new candybar3;snack0.brand=a;/單個(gè)初始化由 new 動(dòng)態(tài)分配的內(nèi)存snack0.weight=1.1;snack0.calory=200; sn

29、ack1.brand=b; snack1.weight=2.2;snack1.calory=400; snack2.brand=c; snack2.weight=4.4; snack2.calory=500;for(int i=0;i3;i+)cout brand: snacki.brand endl; cout weight: snacki.weight endl;cout calorie: snacki.calory endlendl;delete snack; return 0;/ex.4.10 數(shù)組方法 1 #include int main()using namespace std;

30、 const int size = 3;int successsize;cout success0success1success2; coutsuccess1:success0endl; coutsuccess2:success1endl; coutsuccess3:success2endl;double average=(success0+success1+success2)/3; coutaverage:averageendl;return 0;/ex.4.10 array方法 2 #include #include int main()using namespace std; array

31、ad=0;cout ad0ad1ad2;coutsuccess1:ad0endl;coutsuccess2:ad1endl; coutsuccess3:ad2endl; ad3=(ad0+ad1+ad2)/3; coutaverage:ad3endl; return 0;第五章 循環(huán)和關(guān)系表達(dá)式/ex.5.1#include int main()using namespace std;coutnum1num2; int sum=0;for(int temp=num1;temp=num2;+temp)/or temp+ sum+=temp;coutthe sum from num1 to num

32、2 is sumendl; return 0;/ex.5.2#include #includeint main()using namespace std; arrayad=0; ad1=ad0=1l;for(int i=2;i101;i+) adi=i*adi-1;for(int i=0;i101;i+) couti! = adiendl;return 0;/ex.5.3#include int main()using namespace std;coutnum)&num!=0)sum+=num;coutso far, the sum is sumendl; coutplease enter

33、an integer: ;return 0;/ex.5.4#include int main()using namespace std; double sum1,sum2; sum1=sum2=0.0;int year=0; while(sum2=sum1)+year; sum1+=10;sum2=(100+sum2)*0.05+sum2;cout經(jīng)過year年后,cleo 的投資價(jià)值才能超過 daphne 的投資價(jià)值。endl; cout此時(shí),cleo 的投資價(jià)值為sum1,而 daphne 的投資價(jià)值為sum2endl; return 0;/ex.5.5#include const int

34、 months = 12;constchar*monthsmonths=january,february,march,april,may,june,july,august,sept ember,october,november,december;int main()using namespace std;int salesmonths,sum=0; for(int i=0;imonths;i+)cout請(qǐng)輸入在monthsisalesi;sum+=salesi;cout這一年中的c+ for fools的總銷售量為:sumendl; return 0;/ex.5.6#include const

35、 int months = 12;constchar*monthsmonths=january,february,march,april,may,june,july,august,sept ember,october,november,december;const char* years3=第一年,第二年,第三年; int main()using namespace std;int year_sale3,sum=0,sales3months; for(int i=0;i3;i+)int temp=0;coutyearsi的每個(gè)月銷售量:endl; for(int j=0;jmonths;j+)

36、cout請(qǐng)輸入monthsjsalesij;temp+=salesij;year_salei=temp;sum+=year_salei;for(int i=0;i3;i+)coutyearsi的銷售量為:year_saleiendl; cout這三年的總銷售量為:sumendl;return 0;/ex.5.7#include #include using namespace std; struct carstring name; int year;int main()coutnum).get(); car* ps=new carnum;for(int i=0;inum;+i)coutcar #i+1:n; coutplease enter the make: ; getline(cin,); coutpsi.year).get();couthere is your collection:n; for(int i=0;inum;+i) cout

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論