C++Primer源代码.pdf
《C++Primer源代码.pdf》由会员分享,可在线阅读,更多相关《C++Primer源代码.pdf(76页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Sample Solutions for Programming Exercises in C+Primer Plus,3rd EditionChapter 22.Write a C+program that asks for a distance in furlongs and converts it to yards(one furlong is 220 yards)./pe2-2.cpp#include using namespace std;int main(void)(cout Enter a distance in furlongs:double furlongs;cin furl
2、ongs;double feet;feet=220*furlongs;cout furlongs furlongs=feet feetnH;return 0;)3.Write a C+program that uses three user-defined functions(counting mainQ as one)and produces the following output:Three blind miceThree blind miceSee how they runSee how they runOne function,called two times,should prod
3、uce the first two lines,and the remainingfunction,also called twice,should produce the remaining output./pe2-3.cpp#include using namespace std;void mice();void run();int main()(mice();mice();run();run();return 0;)void mice()(cout Three blind micen;void run()cout ”See how they runnH;4.Write a program
4、 that has main。call a user-defined function that takes a Celsiustemperature value as an argument and then returns the equivalent FahrenheitStephen Prata 1 July 22,1999 Sample Solutions for Programming Exercises in C+Primer Plus,3rd Editionvalue.The program should request the Celsius value as input f
5、rom the user anddisplay the result,as shown in the following code:Please enter a Celsius value:2020 degrees Celsius is 68 degrees Fahi*enheit.For reference,here is the formula for making the conversion:Fahrenheit=1.8 X Celsius+32.0/pe2-4.cpp#include using namespace std;double C_to_F(double);int main
6、()(cout nEnter a temperature in Celsius:double C;cin C;double F;F=Co_F(C);cout C ”degrees Celsius=F degrees FahrenheitnH;return 0;)double C_to_F(double temp)(return 1.8*temp+32.0;)Chapter 31.Write a short program that asks for your height in integer inches and then convertsyour height to feet and in
7、ches.Have the program use the underline character toindicate where to type the response.Also use a const type./pe3-l.cpp#include using namespace std;const int Inch_Per_Foot=12;int main(void)/Note:some environments dont support the backspace charactercout ”Please enter your height in inches:_/b/b/b”;
8、int ht_inch;cin ht_inch;int ht_feet=ht_inch/Inch_Per_Foot;int rm_inch=ht_inch%Inch_Per_Foot;cout Your height is ht_feet ”feet,cout rm_inch inch(es).nn;return 0;3.Write a program that asks how many miles you have driven and how many gallonsof gasoline you used and then reports the miles per gallon yo
9、ur car got.Or,Stephen Prata 2 July 22,1999 Sample Solutions for Programming Exercises in C+Primer Plus,3rd Editionif you prefer,the program can request distance in kilometers and petrol in liters andthen report the result European style,in liters per 100 kilometers.(Or,perhaps,youcan use litres per
10、100 kilometres.)/pe3-3.cpp#include using namespace std;int main(void)(cout ”How many miles have you driven your car?float miles;cin miles;cout How many gallons of gasoline did the car use?float gallons;cin gallons;cout ”Your car gotM miles/gallons;cout ”miles per gallon.nM;return 0;)Chapter 42.The C
11、andyBar structure contains three members.The first member holds the brandname of a candy bar.The second member holds the weight(which may have afractional part)of the candy bar,and the third member holds the number of calories(an integer value)in the candy bar.Write a program that declares such a st
12、ructure andcreates a CandyBar variable called snack,initializing its members to Mocha Munch,2.3,and 350 respectively.The initialization should be part of the declaration for snack.Finally,the program should display the contents of the snack variable./pe4-2.cpp/a candybar struct CandyBar char brand40
13、;double weight;intcalories;#include using namespace std;/introduces namespace stdint main()CandyBar snack=nMocha Munch,2.3,350;cout ”Brand name:snack.brand endl;cout Weight:snack.weight endl;cout ”Calories:snack.calories endl;return 0;Stephen Prata 3 July 22,1999 Sample Solutions for Programming Exe
14、rcises in C+Primer Plus,3rd Edition4.William Wingate runs a pizza-analysis service.For each pizza,he needs to recordthe following information:IbJ the name of the pizza company,which may consist of more than one wordIbJ the diameter of pizzalb the weight of the pizzaDevise a structure that can hold t
15、his information and write a program using a structurevariable of that type.The program should ask the user to enter each of the items ofinformation listed above,and then the program should display that information.Usecin(or its methods)and cout./pe4-4.ccp#include using namespace std;const int Slen=7
16、0;struct pizza char nameSlenJ;float diameter;float weight;int main(void)(pizza pie;cout ”What is the name of the pizza company?”;cin.getline(pie.name,Slen);cout What is the diameter of the pizza in inches?cin pie.diameter;cout ”How much does the pizza weigh in ounces?cin pie.weight;cout ”Company:pie
17、.name n;cout Diameter:pie.diameter inchesn;cout Weight:pie.weight ouncesnM;return 0;)Chapter 52.Write a program that asks you to type in numbers.After each entry,the numberreports the cumulative sum of the entries to date.The program terminates when youenter a zero./pe5-2.cpp#include using namespace
18、 std;int main(void)(double sum=0.0;double in;cout Enter a number(0 to terminate):;cin in;while(in!=0)sum+=in;cout Running total=sum n”;Stephen Prata 4 July 22,1999 Sample Solutions for Programming Exercises in C+Primer Plus,3rd Editioncout Enter next number(0 to terminate):cin in;)cout Bye!n;return
19、0;)4.You sell C+For Fools.Write a program that has you enter a years worth ofmonthly sales(in terms of number of books,not of money).The program should use aloop to prompt you by month,using an array of char*initialized to the month stringsand storing the input data in an array of int.Then,the progr
20、am should find the sum ofthe array contents and report the total sales for the year./pe5-4.cpp/book sales#include using namespace std;/introducesname space stdconst int MONTHS=12;const char*monthsMONTHS=January,February,March,April,“May”,“June”,“July”,“August,“September”,October,November,December;in
21、t main()int salesMONTHSJ;int month;cout MEnter the monthly sales for C+for Fools*,:nM;for(month=0;month MONTHS;month+)cout Sales for monthsmonth cin salesmonth;)double total=0.0;for(month=0;month MONTHS;month+)total+=salesmonth;cout Total sales:total endl;return 0;6.Design a structure called car tha
22、t holds the following information about anautomobile:its make(as a string in a character array)and the year it was built(as aninteger).Write a program that asks the user how many cars to catalog.The programshould then use new to create a dynamic array of that many car structures.Next,itshould prompt
23、 the user to input the make and year information for each structure.Note that this requires some care,for it alternates reading strings with numeric data.(See Chapter 4,Derived Types,n for more information.)Finally,it should display thecontents of each structure.A sample run should look something li
24、ke the following:How many cars do you wish to catalog?2Car#l:Please enter the make:Hudson HornetPlease enter the year made:1952Car#2:Stephen Prata 5 July 22,1999 Sample Solutions for Programming Exercises in C+Primer Plus,3rd EditionPlease enter the make:KaiserPlease enter the year made:1951Here is
25、your collection:1952 Hudson Hornet1951 Kaiser/pe5-6.cpp#include using namespace std;struct car char name20;int year;int main(void)(int n;cout ”How many cars do you wish to catalog?:;cin n;while(cin.get()!=n)/get rid of rest of linecar*pc=new car n;int i;for(i=0;i n;i+)(cout Car#(i+1)”:n;cout Please
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Primer 源代码
限制150内