版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第二章:开始学习C+ 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 furlong un its to yard uin ts- 把浪单位换位码单位 #in clude double fur2yd(double); int mai n() using n amespace std; coute nter t
2、he dista nee measured by furlong un its:; double fur; cinfur; coutc onvert the furlong to yarde ndl; double yd; yd=fur2yd(fur); coutfur furlong is yd yarde ndl; return 0; double fur2yd(double t) return 220*t; ex2.3-每个函数都被调用两次 #in clude void mice(); void see(); using n amespace std; int mai n() mice(
3、); mice(); see(); see(); return 0; 25 / 28 void mice() coutthree blind miceendl; void see() coutsee how they runendl; /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 #i
4、nclude 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-c onvert the light years valve to astr ono mical un its- 把光年转换为天文单位 #in clude double convert(double);/ 函数原型
5、int mai n() using n amespace std; cout light_years; double astro_ un its; astro_ un its=c on vert(light_years); coutlight_years light_years = astro_u ni ts astr ono mical un its.e ndl; return 0; double con vert(double t) return 63240*t;/1 光年=63240 天文单位 /ex2.7-显示用户输入的小时数和分钟数 #in clude void show(); ma
6、in () using n amespace std; show(); return 0; void show() using n amespace std; int h,m; cout h; coute nter the nu mber of minu tes:; cinm; coutTime:h:me ndl; 第三章:处理数据 /ex3.1 将身高用英尺(feet)和英寸(inch)表示 #include const int inch_per_feet=12;/const 常量 -1feet=12inches-1 英尺 =12 英寸 int main() using namespace
7、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- 计算相应的 body mass index (体重指数) #include const int inch_per_feet=12; const double meter_per_inch=0.0254; const double pound_per_kilogram
8、=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_kilogram=wt_pound/pound_per_kilogram; coutendl; coutYour pensonal body
9、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( 体重指数 ) is BMIendl; return 0; /ex3.3 以度,分,秒输入,以度输出 #include const int minutes_per_degree=60; const int seconds_per_minute=
10、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/mi nutes_per_degree/seconds_per_minute; coutdegree degrees,minute minut
11、es,secondseconds =show_in_degree degreesn; return 0; /ex3.4 #include const 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_minute/minutes_per_hour/hours_per_day; Hou
12、r=seconds/seconds_per_minute/minutes_per_hour%hours_per_day; Minute=seconds/seconds_per_minute%minutes_per_hour; Second=seconds%seconds_per_minute; coutsecondsseconds = Day days,Hour hours,Minute minutes,Second secondsn; return 0; /ex3.5 #include int main() using namespace std; coutworld_population;
13、 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 欧洲风格 (L/100Km) #include int main() using namespace std; coutm_distance; coutm_gasoline; coutYour car
14、 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-耗油量-欧洲风格(L/100Km)转换成美国风格(mpg) #in cl
15、ude int mai n() using n amespace std; coutE nter the automobile gasoli ne con sumpti on figure inn Euro_style; coutC on verts to U.S. style(miles per gall on ):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 lit
16、ers. /Thus, 19 mpg is about 12.4 L/100Km, and 27 mpg is about 8.7 L/100Km. Enter the automobile gasoline consumption figure in Europea n style(liters per 100 kilometers):12.4 Con verts to U.S. style(miles per gall on): 12.4 L/100Km = 19.4187 mpg Press any key to con ti nue / ex3.7 automobile gasolin
17、e consumption-耗油量-美国风格(mpg)转换成欧洲风格(L/100Km) #in clude int mai n() using n amespace std; coutE nter the automobile gasoli ne con sumpti on figure inn US_style; coutC on verts to Europea n style(miles per gallo n):e ndl; coutUS_style mpg = 62.14*3.875/US_styleL/100Kmn; return 0; / Enter the automobile
18、 gasoline consumption figure in U.S. style(miles per gallo n) :19 Con verts to Europea n style(miles per gallo n): 19 mpg = 12.6733L/100Km Press any key to con ti nue 第四章复合类型 /ex4.1 display the information of student #include const int Asize=20; using namespace std; struct student/ 定义结构描述 char first
19、nameAsize; char lastnameAsize; char grade; int age; ; void display(student);/ 函数原型放在结构描述后 int main() coutwhat is your first name?endl; student lcg;/ 创建结构变量(结构数据对象) cin.getline(lcg.firstname,Asize); coutwhat is your last name?endl; cin.getline(lcg.lastname,Asize); coutwhat letter grade do you deserve
20、?lcg.grade; coutwhat is your age?lcg.age; display(lcg); return 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
21、name,dessert; coutEnter your name: n; getline(cin,name); coutEnter your favorite dessert: n; getline(cin,dessert); coutI have some delicious dessert; cout for you, namesbumpC(); II 修改后的 break; ex4.3 输入其名和姓,并组合显示 #inClude #inClude Const int Asize=20; int main() using namespaCe std; Char fnameAsize; C
22、har lnameAsize; Char fullname2*Asize+1; CoutEnter your first name:;II 输入名字,存储在 fname 数组中 Cin.getline(fname,Asize); CoutEnter your last name:;II 输入姓,存储在 lname 数组中 Cin.getline(lname,Asize); strnCpy(fullname,lname,Asize);II 把姓 lname 复制到 fullname 空数组中 strCat(fullname, );II 把“, ”附加到上述 fullname 尾部 strnCat
23、(fullname,fname,Asize);II 把 fname 名字附加到上述 fullname 尾部 fullname 2*Asize =0;II 为防止字符型数组溢出,在数组结尾添加结束符 CoutHeres the information in a single string:fullnameendl;II 显示组合结果 return 0; IIex4.4 使用 string 对象 存储、显示组合结果 #inClude #inClude int main() using namespaCe std; string fname,lname,attaCh,fullname; CoutEn
24、ter your first name:; getline(cin,str) getline(cin,fname);/note: 将一行输入读取到 string 类对象中使用的是 / 它没有使用句点表示法,所以不是类方法 coutEnter 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 initi
25、alize it 声明结果并创建一个变量 #include const int Asize=20; struct CandyBar char brandAsize; double weight; int calory; ; int main() using namespace std; CandyBar snack=Mocha Munch,2.3,350; coutHeres the information of snack:n; coutbrand:snack.brandendl; coutweight:snack.weightendl; coutcalory:snack.caloryend
26、l; return 0; /ex4.6 结构数组的声明及初始化 #include 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;i3;i+)/ 利用 for 循环来显示 snack 变量的内容 coutsnacki.brandendl snacki.wei
27、ghtendl snacki.caloryendlendl; return 0; /ex4.7 pizza 披萨饼 #include #include const int Size=20; struct pizza/ 声明结构 char companySize; double diameter; double weight; ; int main() using namespace std; pizza pie;/ 创建一个名为 pie 的结构变量 coutWhats the name of pizza company:; cin.getline(pany,Size); coutpie.dia
28、meter; coutpie.weight; coutcompany:panyendl; coutdiameter:pie.diameterinchesendl; coutweight:pie.weightounchesendl; return 0; /ex4.8 pizza pie 披萨饼 使用 new 创建动态结构 #include #include const int Size=20; struct pizza/ 声明结构 char companySize; double diameter; double weight; ; int main() using namespace std;
29、 pizza *pie=new pizza;/ 使用 new 创建动态结构 coutpie-diameter; cin.get();/ 读取下一个字符 coutcompany,Size); coutpie-weight; coutdiameter:diameter inchesendl; coutcompany:companyendl; coutweight:weight ounchesendl; delete pie;/delete 释放内存 return 0; /ex.4.9 使用 new 动态分配数组 方法 1 #include #include using namespace std;
30、 struct CandyBar string brand; double weight; int calory; ; int main() CandyBar *snack= new CandyBar3; snack0.brand=A;/ 单个初始化由 new 动态分配的内存 snack0.weight=1.1; snack0.calory=200; snack1.brand=B; snack1.weight=2.2; snack1.calory=400; snack2.brand=C; snack2.weight=4.4; snack2.calory=500; for(int i=0;i3;
31、i+) cout brand: snacki.brand endl; cout weight: snacki.weight endl; cout calorie: snacki.calory endlendl; delete snack; return 0; /ex.4.10 数组 方法 1 #include int main() using namespace std; const int Size = 3; int successSize; cout success0success1success2; coutsuccess1:success0endl; coutsuccess2:succ
32、ess1endl; coutsuccess3:success2endl; double average=(success0+success1+success2)/3; coutaverage:averageendl; return 0; /ex.4.10 array 方法 2 #include #include int main() using namespace std; arrayad=0; cout ad0ad1ad2; coutsuccess1:ad0endl; coutsuccess2:ad1e ndl; coutsuccess3:ad2e ndl; ad3=(ad0+ad1+ad2
33、)/ 3; coutaverage:ad3e ndl; return 0; 第五章循环和关系表达式 /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;+temp)/or temp+ sum+=temp; coutThe sum from n um1 to n um2 is sume ndl; return 0; /ex.5.2 #in clude #in clude int mai n() using n amespace std;
34、array ad=0; ad1=ad0=1L; for(int i=2;i101;i+) adi=i*adi-1; for(int i=0;i101;i+) couti! = adie ndl; return 0; /ex.5.3 #include int main() using namespace std; coutnum) coutSo far, the sum is sumendl; coutPlease enter an integer: ; return 0; /ex.5.4 #include int main() using namespace std; double sum1,
35、sum2; sum1=sum2=0.0; int year=0; while(sum2=sum1) +year; sum1+=10; sum2=(100+sum2)*0.05+sum2; cout经过year年后,Cleo的投资价值才能超过Daphne的投资价值。endl; cout此时,Cleo的投资价值为sum1,而Daphne的投资价值为sum2endl; return 0; /ex.5.5 #include const int MONTHS = 12; const monthsMONTHS=January,February,March,April,May,June,July,Augus
36、t,Sept ember,October,November,December; int main() using namespace std; int salesMONTHS,sum=0; for(int i=0;iMONTHS;i+) cout 请输入在 monthsisalesi; sum+=salesi; cout 这一年中的 C+ For Fools 的总销售量为: sumendl; return 0; /ex.5.6 #include const int MONTHS = 12; const monthsMONTHS=January,February,March,April,May,
37、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 的每个月销售量 :endl; for(int j=0;jMONTHS;j+) cout 请输入 monthsjsalesij; temp+=salesij; char* char* year_salei
38、=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 car string name; int year; ; int main() coutnum).get(); car* ps=new carnum; for(int i=0;inum;+i) coutCar #i+1:n; coutPlease enter the
39、make: ; getline(cin,); coutpsi.year).get(); coutHere is your collection:n; for(int i=0;inum;+i) coutpsi.year endl; delete ps; return 0; /ex.5.8 #include #include int main() using namespace std; char word20; int sum=0; coutword; while(strcmp(word,done) sum+; cinword; coutYou entered a
40、 total of sum words.n; return 0; /ex.5.9 #include #include int main() using namespace std; string word; int sum=0; coutword; while(word!=done) sum+; cinword; coutYou entered a total of sum words.n; return 0; /ex.5.10 #include int mai n() using n amespace std; coutE nter n umber of rows:; int num; ci
41、nn um; for(i nt i=0;i 1;j-) cout.; for(i nt k=O;k=i;+k) cout*; coute ndl; return 0; 第六章分支语句和逻辑运算符 /ex.6.1 #in clude #in clude int mai n() using n amespace std; char ch; cin .get(ch); while(ch!=) if(isdigit(ch) ci n.get(ch); else if(islower(ch) ch=toupper(ch); else ch=tolower(ch); coutch; ci n.get(ch
42、); return 0; /ex.6.2- 数组 #include #include int main() using namespace std; double sum=0,average=0; double num10; int i=0,total=0; double temp; while(cintemp sum+=numi; +i; if(i!=0) average=sum/i; for(int j=0;javerage) +total; n; cout 这些数字的平均值为 averageendl; cout 并且共有 total 个数字大于平均值。 return 0; /ex.6.2
43、-array #include #include #include int main() using namespace std; double sum=0,average=0; arrayad=0; int i=0,total=0; double temp; while(cintemp sum+=adi; +i; if(i!=0) average=sum/i; for(int j=0;javerage) +total; cout 这些数字的平均值为 averageendl; cout 并且共有 total 个数字大于平均值。 n; return 0; /ex.6.3 #include int
44、 main() using namespace std; coutPlease enter one of the following choices:n c)carnivore p)pianistn t)tree g)gamenfn; / 书上的这个 f 个人认为是打印错误 coutch; while(ch!=c switch(ch) case c: coutA maple is a carnivore.n; break; case p: coutA maple is a pianist.n; break; case t: coutA maple is a tree.n; break; cas
45、e g: coutA maple is a game.n; return 0; /ex.6.4 #include const int strsize=20; struct bop char fullnamestrsize; char titlestrsize; char bopnamestrsize; int preference; ; int main() using namespace std; coutBenevolent Order of Programmers Reportn a. display by nameb. display by titlen c. display by b
46、opname d. diplay by preferencen q. quitn; char ch; bop member5= Wimp Macho,English Teacher,DEMON,0, Raki Rhodes,Junior Programmer,BOOM,1, Celia Laiter,Super Star,MIPS,2, Hoppy Hipman,Analyst Trainee,WATEE,1, Pat Hand,Police,LOOPY,2 ; coutchi5;i+) coutmemberi.fullnameendl; break; case b: for(int i=0;
47、i5;i+) coutmemberi.titleendl; break; case c: for(int i=0;i5;i+) coutmemberi.bopnameendl; break; case d: for(int i=0;i5;i+) if(memberi.preference=0) coutmemberi.fullnameendl; else if(memberi.preference=1) coutmemberi.titleendl; else if(memberi.preference=2) coutmemberi.bopnameendl; break; coutNext ch
48、oice: ; coutBye!n; return 0; /ex.6.5 #include int main() using namespace std; double income,revenue; coutincome else if(income=15000) revenue=0.1*(income-5000); else if(income=35000) revenue=0.1*(15000-5000)+0.15*(income-15000); else revenue=0.1*(15000-5000)+0.15*(35000-15000)+0.2*(income-35000); cout你的所得税为 revenueendl; cout请输入你的收入: ; return 0; /ex.6.6 #include #include using namespace std; struct patron string name; double money; ; int main() int num,temp=0; coutnum; cin.get(); patron *ps=new patronnum; for(int i=0;inum;+i) cout 请
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年专业融资居间服务合同范本版B版
- 2024年外国人就业服务协议模板版B版
- 2024年公司部门间承包协议模板细则一
- 2024年全新劳务派遣与承包协议条款样本版
- 2024年度井筒安全评估合同:防止地质灾害与事故
- 2024年专业副食品供应商与零售商购销合作合同一
- 2024年因性格差异导致的离婚合同范本版
- 2024年商业购货协议附加协议样本版B版
- 2024工程招投标及合同
- 2024年度专业顾问咨询服务费用合同模板版
- 2024年新华社招聘122人历年(高频重点复习提升训练)共500题附带答案详解
- 成本核算与成本分析工作计划三篇
- 【小学语文中高年级单元整体设计的实践探究2000字(论文)】
- TBT3134-2023机车车辆驱动齿轮箱 技术要求
- 全国清华大学版信息技术七年级下册第2单元第4课《动物的力量-认识高效运算的函数》教学设计
- 国家开放大学《计算机网络》课程实验报告实验六-计算机网络综合性实-
- 售后服务方案及运维方案
- (2024)纪检监察业务知识测试题及答案
- 汽车厂质量提升方案
- (2024年)健康养生养生保健课件
- 《养老护理员》-课件:老年人安全防范及相关知识
评论
0/150
提交评论