资源预览内容
第1页 / 共197页
第2页 / 共197页
第3页 / 共197页
第4页 / 共197页
第5页 / 共197页
第6页 / 共197页
第7页 / 共197页
第8页 / 共197页
第9页 / 共197页
第10页 / 共197页
亲,该文档总共197页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
第二章:开始学习 C+,/ex2.1-display your name andaddress #include int main(void) using namespace std; cout double fur2yd(double); int main() using namespace std; coutfur; cout void mice(); void see(); using namespace std; int main() mice(); mice(); see(); see(); return 0; ,1,2,void mice() cout 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 #include double C2F(double); int main() using namespace std; coutC; double F; F=C2F(C); coutC degrees Celsius is F degrees Fahrenheit.endl; return 0; double C2F(double t) return 1.8*t+32; ,/ex2.6-convert the light years valve to astronomical units-把光年转换为天文单位 #include double convert(double);/函数原型 int main() using namespace std; coutlight_years; double astro_units; astro_units=convert(light_years); cout void show(); main() using namespace std; show(); return 0; void show() using namespace std; int h,m; couth; coutm; coutTime:h:mendl;,第三章:处理数据 /ex3.1将身高用英尺(feet)和英寸(inch)表示,3,4,#include const 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;/取余 cout const int inch_per_feet=12; const double meter_per_inch=0.0254; const double pound_per_kilogram=2.2; int main() using namespace std; 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_kilogram=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);,5,cout const int minutes_per_degree=60; const int seconds_per_minute=60; int main() using namespace std; coutdegree; coutminute; coutsecond; double show_in_degree; show_in_degree=(double)degree+(double)minute/minutes_per_degree+(double)second/mi nutes_per_degree/seconds_per_minute;,minutes,secondseconds,coutdegreedegrees,minute =show_in_degree degreesn; return 0; ,/ex3.4 #include const int hours_per_day=24; const intminutes_per_hour=60; const int seconds_per_minute=60; int main() using namespace std; coutseconds; int Day,Hour,Minute,Second; Day=seconds/seconds_per_minute/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;,6,Second=seconds%seconds_per_minute;,=Daydays,Hour,hours,Minute,coutsecondsseconds minutes,Second secondsn; return 0; ,/ex3.5 #include int main() using namespace std; coutworld_population; coutUS_population; double percentage; percentage=(double)US_population/world_population*100; cout int main() using namespace std; coutm_distance; coutm_gasoline; 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-耗油量-欧洲风格(L/100Km)转换成美国风格(mpg) #include int main() using namespace std; coutEuro_style; cout int main() using namespace std; coutUS_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 in U.S. style(miles pergallon):19 Converts to European style(miles per gallon): 19 mpg = 12.6733L/100Km Press any key to continue 第四章 复合类型,7,8,display,theinformationof,/ex4.1 student,#include const int Asize=20; using namespace std; struct student/定义结构描述 char firstnameAsize; char lastnameAsize; char grade; int age; ; void display(student);/函数原型放在结构描述后 int main() coutlcg.grade; coutlcg.age; display(lcg); return 0; void display(student name) cout #include int main() using namespace std;,string name,dessert; coutsbumpc();/修改后的 break; ex4.3 输入其名和姓,并组合显示 #include #include const int Asize=20; int main() using namespace std; char fnameAsize; char lnameAsize; char fullname2*Asize+1; cout #include const int Asize = 20; int main(),9,10, using namespace std; char firstnameAsize; char lastname50; cout Enter your first name: ; cin.getline(firstname,Asize); cout Enter your last name: ; cin.getline(lastname,50); strcat(lastname, ); strncat(lastname,firstname,Asize); cout Heres the information in a single string: lastname endl; return 0; ,/ex4.4 使用 string 对象 存储、显示组合结果 #include #include int main() using namespace std; string fname,lname,attach,fullname; cout const int Asize=20; struct CandyBar char brandAsize; double weight; int calory;,11,; int main() using namespace std; CandyBar snack=Mocha Munch,2.3,350; cout const int Asize=20; struct CandyBar char brandAsize; double weight; int calory; ; int main() using namespace std; CandyBar snack3= Mocha Munch,2.3,350, XuFuJi,1.1,300, Alps,0.4,100 ; for(int i=0;i #include const int Size=
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号