




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
. .第二章:开始学习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 to yardendl;double yd;yd=fur2yd(fur);coutfur furlong is yd yardendl;return 0;double fur2yd(double t)return 220*t;/ex2.3-每个函数都被调用两次#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 runendl;/ex2.4#includeint 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 Fahrenheit.endl;return 0;double C2F(double t)return 1.8*t+32; /ex2.6-convert the light years valve to astronomical units-把光年转换为天文单位#includedouble convert(double);/函数原型int main()using namespace std;coutlight_years;double astro_units;astro_units=convert(light_years);coutlight_years light_years = astro_units astronomical units.endl;return 0;double convert(double t)return 63240*t;/1 光年=63240 天文单位 /ex2.7-显示用户输入的小时数和分钟数#includevoid 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)表示#includeconst 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-计算相应的body mass index(体重指数)#includeconst int inch_per_feet=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_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);coutyour Body Mass Index(体重指数) is BMIendl;return 0;/ex3.3 以度,分,秒输入,以度输出#includeconst int 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/seconds_per_minute;coutdegree degrees,minute minutes,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_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;Second=seconds%seconds_per_minute;coutsecondsseconds = Day days,Hour hours,Minute minutes,Second secondsn;return 0;/ex3.5#includeint main() using namespace 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欧洲风格(L/100Km)#includeint main()using namespace std;coutm_distance;coutm_gasoline;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.7 automobile gasoline consumption-耗油量-欧洲风格(L/100Km)转换成美国风格(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. /Thus, 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.4Converts to U.S. style(miles per gallon):12.4 L/100Km = 19.4187 mpgPress any key to continue/ ex3.7 automobile gasoline consumption-耗油量-美国风格(mpg)转换成欧洲风格(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. style(miles per gallon):19Converts to European style(miles per gallon):19 mpg = 12.6733L/100KmPress any key to continue第四章 复合类型/ex4.1 display the information of student#includeconst int Asize=20;using namespace std;struct student/定义结构描述char firstnameAsize;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?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#includeint main()using namespace std;string 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();/修改后的 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:;/输入名字,存储在fname数组中cin.getline(fname,Asize);coutEnter your last name:;/输入姓,存储在lname数组中cin.getline(lname,Asize);strncpy(fullname,lname,Asize);/把姓lname复制到fullname空数组中strcat(fullname, );/把“, ”附加到上述fullname尾部strncat(fullname,fname,Asize);/把fname名字附加到上述fullname尾部fullname2*Asize=0;/为防止字符型数组溢出,在数组结尾添加结束符 coutHeres the information in a single string:fullnameendl;/显示组合结果return 0;/ex4.4 使用string对象 存储、显示组合结果#include#includeint main()using namespace std;string fname,lname,attach,fullname;coutEnter your first name:;getline(cin,fname);/note:将一行输入读取到string类对象中使用的是getline(cin,str) /它没有使用句点表示法,所以不是类方法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 initialize it 声明结果并创建一个变量#includeconst int Asize=20;struct CandyBarchar 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.caloryendl;return 0;/ex4.6 结构数组的声明及初始化#includeconst int Asize=20;struct CandyBarchar 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.brandendlsnacki.weightendlsnacki.caloryendlendl;return 0;/ex4.7 pizza披萨饼#include#includeconst 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.diameter;coutpie.weight;coutcompany:panyendl;coutdiameter:pie.diameterinchesendl;coutweight:pie.weightounchesendl;return 0;/ex4.8 pizza pie 披萨饼 使用new创建动态结构#include#includeconst int Size=20;struct pizza/声明结构char companySize;double diameter;double weight;int main()using namespace std;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#includeusing namespace std;struct CandyBarstring 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;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: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;arrayad=0; cout ad0ad1ad2; coutsuccess1:ad0endl;coutsuccess2:ad1endl;coutsuccess3:ad2endl;ad3=(ad0+ad1+ad2)/3;coutaverage:ad3endl; return 0; 第五章 循环和关系表达式/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 num2 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 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经过year年后,Cleo的投资价值才能超过Daphne的投资价值。endl; cout此时,Cleo的投资价值为sum1,而Daphne的投资价值为sum2endl; return 0; /ex.5.5#include const int MONTHS = 12;const char* monthsMONTHS=January,February,March,April,May,June,July,August,September,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 char* monthsMONTHS=January,February,March,April,May,June,July,August,September,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; year_salei=temp; sum+=year_salei;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026届广西贺州市平桂管理区平桂高级中学高一化学第一学期期末经典试题含解析
- 2026届贵州省凯里一中高三化学第一学期期末综合测试模拟试题含解析
- 2026届安徽省部分高中化学高一第一学期期中联考模拟试题含解析
- 重庆市主城区七校联考2026届化学高一上期末达标检测试题含解析
- 2025年注册建筑师考试押题卷:建筑设计与施工规范
- 2025年春季小学数学毕业升学考试重点题型冲刺押题试卷
- 2025年注册测绘工程师考试冲刺押题试卷 测绘技术应用专项强化
- 2025年公务员考试申论押题试卷 案例分析专项训练
- 2025年初级经济师职业资格考试 经济基础知识高分冲刺试卷
- 2026届湖北省武汉市新洲一中阳逻校区化学高一第一学期期中质量跟踪监视试题含解析
- 市场营销(第2版)课件全套 王永贵 第1-17章-市场与市场营销概述及发展-顾客营销学
- 2023年6月英语六级考试真题及答案解析(全3套)
- 高中数学 人教A版 必修一 《集合与常用逻辑用语》 1.1集合的概念
- 深圳某电厂锅炉维修改造施工组织设计-new(常用版)
- GB/T 4950-2021锌合金牺牲阳极
- 中药调剂技术-课件
- 证券从业考试基础模拟卷二(题目+解析)
- 水轮发电机讲义课件
- 信息系统运维服务方案
- 化工试生产总结报告
- 导数与原函数的对称性 微专题课件-2023届高三数学一轮复习
评论
0/150
提交评论