C++ - Primer - Plus(第五版)习题解答

2019-03-22 17:31

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

Chapter 2

// pe2-2.cpp

#include

int main(void) {

using namespace std;

cout << \ double furlongs; cin >> furlongs; double feet;

feet = 220 * furlongs;

cout << furlongs << \ << feet << \

return 0; }

// pe2-3.cpp

#include using namespace std;

void mice(); void run(); int main() {

mice(); mice(); run(); run();

return 0; }

void mice() {

cout << \}

void run() {

cout << \}

// pe2-4.cpp

#include

double C_to_F(double); int main() {

using namespace std;

cout << \ double C; cin >> C; double F;

SP 1 of 65 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

F = C_to_F(C);

cout << C << \ << F << \

return 0; }

double C_to_F(double temp) {

return 1.8 * temp + 32.0; }

Chapter 3

// pe3-1.cpp

#include

const int Inch_Per_Foot = 12;

int main(void) {

using namespace std;

// Note: some environments don't support the backspace character cout << \ int ht_inch; cin >> ht_inch;

int ht_feet = ht_inch / Inch_Per_Foot; int rm_inch = ht_inch % Inch_Per_Foot;

cout << \ cout << rm_inch << \ return 0; }

// pe3-3.cpp

#include

const double MINS_PER_DEG = 60.0; const double SECS_PER_MIN = 60.0; int main() {

using namespace std;

int degrees; int minutes; int seconds;

double latitude;

cout << \ cout << \ cin >> degrees;

cout << \ cin >> minutes;

cout << \ cin >> seconds;

latitude = degrees + (minutes + seconds / SECS_PER_MIN)/MINS_PER_DEG; cout << degrees << \ << seconds << \ return 0; }

SP 2 of 65 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

// pe3-5.cpp

#include

int main(void) {

using namespace std;

cout << \ float miles; cin >> miles;

cout << \ float gallons; cin >> gallons;

cout << \ cout << \ return 0; }

// pe3-6.cpp

#include

const double KM100_TO_MILES = 62.14; const double LITERS_PER_GALLON = 3.875;

int main ( void ) {

using namespace std; double euro_rating; double us_rating;

cout << \ cin >> euro_rating;

// divide by LITER_PER_GALLON to get gallons per 100-km // divide by KM100_TO_MILES to get gallons per mile // invert result to get miles per gallon

us_rating = (LITERS_PER_GALLON * KM100_TO_MILES) / euro_rating; cout << euro_rating << \ cout << us_rating << \

return 0; }

Chapter 4

// pe4-2.cpp -- storing strings in string objects #include #include int main() {

using namespace std; string name; string dessert;

cout << \

getline(cin, name); // reads through newline cout << \ getline(cin, dessert);

cout << \ cout << \ return 0; }

SP 3 of 65 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

// pe4-3.cpp -- storing strings in char arrays #include #include const int SIZE = 20; int main() {

using namespace std; char firstName[SIZE]; char lastName[SIZE];

char fullName[2*SIZE + 1];

cout << \ cin >> firstName;

cout << \ cin >> lastName;

strncpy(fullName,lastName,SIZE); strcat(fullName, \

strncat(fullName, firstName, SIZE); fullName[SIZE - 1] = '\\0';

cout << \ << fullName << endl; return 0; }

// pe4-5.cpp

// a candybar structure struct CandyBar { char brand[40]; double weight; int calories; };

#include

int main() {

using namespace std; //introduces namespace std CandyBar snack = { \

cout << \ cout << \

cout << \

return 0; }

// pe4-7.ccp

#include

const int Slen = 70;

struct pizza {

char name[Slen]; float diameter; float weight; };

int main(void)

SP 4 of 65 September 2, 2004

Solutions for Programming Exercises in C++ Primer Plus, 5th Edition

{

using namespace std; pizza pie;

cout << \ cin.getline(pie.name, Slen);

cout << \ cin >> pie.diameter;

cout << \ cin >> pie.weight;

cout << \

cout << \ cout << \ return 0; }

Chapter 5

// pe5-2.cpp

#include

int main(void) {

using namespace std; double sum = 0.0; double in;

cout << \ cin >> in;

while (in != 0) { sum += in;

cout << \

cout << \ cin >> in; }

cout << \ return 0; }

// pe5-4.cpp // book sales

#include

const int MONTHS = 12;

const char * months[MONTHS] = {\ \ \int main() {

using namespace std; //introduces namespace std int sales[MONTHS]; int month;

cout << \ for (month = 0; month < MONTHS; month++) {

cout << \ cin >> sales[month]; }

SP 5 of 65 September 2, 2004


C++ - Primer - Plus(第五版)习题解答.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2019-2024年中国洗衣机行业前景预测及投资咨询报告(目录) - 图

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: