第一范文网 - 专业文章范例文档资料分享平台

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

来源:用户分享 时间:2025/5/19 5:48:39 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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

using namespace std;

cout << \ cout << \ << \ char ch; cin >> ch;

while (ch != 'c' && ch != 'p' && ch != 't' && ch != 'g') {

cout << \ cin >> ch; }

switch (ch) {

case 'c' : cout << \ break;

case 'p' : cout << \ break;

case 't' : cout << \ break;

case 'g' : cout << \ break;

default : cout << \ }

return 0; }

// pe6-5.cpp

// Neutronia taxation #include

const double LEV1 = 5000; const double LEV2 = 15000; const double LEV3 = 35000; const double RATE1 = 0.10; const double RATE2 = 0.15; const double RATE3 = 0.20; int main( ) {

using namespace std; double income; double tax;

cout << \ cin >> income;

if (income <= LEV1) tax = 0;

else if (income <= LEV2)

tax = (income - LEV1) * RATE1; else if (income <= LEV3)

tax = RATE1 * (LEV2 - LEV1) + RATE2 * (income - LEV2); else

tax = RATE1 * (LEV2 - LEV1) + RATE2 * (LEV3 - LEV2) + RATE3 * (income - LEV3);

cout << \

SP 9 of 73 September 2, 2004

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

return 0; }

// pe6-7.cpp

#include #include int main() {

using namespace std; string word; char ch;

int vowel = 0;

int consonant = 0; int other = 0;

cout << \ cin >> word;

while ( word != \ {

ch = tolower(word[0]); if (isalpha(ch)) {

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') vowel++; else

consonant++; } else

other++; cin >> word; }

cout << vowel <<\

cout << consonant << \ cout << other << \

return 0; }

// pe6-8.cpp -- counting characters #include

#include // file I/O suppport #include // support for exit() const int SIZE = 60; int main() {

using namespace std; char filename[SIZE]; char ch;

ifstream inFile; // object for handling file input

cout << \ cin.getline(filename, SIZE);

inFile.open(filename); // associate inFile with a file if (!inFile.is_open()) // failed to open file {

SP 10 of 73 September 2, 2004

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

cout << \ cout << \ exit(EXIT_FAILURE); }

int count = 0; // number of items read

inFile >> ch; // get first value

while (inFile.good()) // while input good and not at EOF {

count++; // one more item read inFile >> ch; // get next value }

cout << count << \

inFile.close(); // finished with the file return 0; }

Chapter 7

//pe7-1.cpp -- harmonic mean

#include

double h_mean(double x, double y);

int main(void) {

using namespace std; double x,y;

cout << \ while (cin >> x >> y && x * y != 0)

cout << \ << y << \/* or do the reading and testing in two parts: while (cin >> x && x != 0) {

cin >> y; if (y == 0) break; ... */

cout << \ return 0; }

double h_mean(double x, double y) {

return 2.0 * x * y / (x + y); }

SP 11 of 73 September 2, 2004

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

// pe7-3.cpp

#include

struct box {

char maker[40]; float height; float width; float length; float volume; };

void showbox(box b); void setbox(box * pb);

int main(void) {

box carton = {\ setbox(&carton); showbox(carton); return 0; }

void showbox(box b) {

using namespace std;

cout << \ << \ << \ << \

<< \}

void setbox(box * pb) {

pb->volume = pb->height * pb->width * pb->length; }

// pe7-4.cpp -- probability of winning #include

long double probability(unsigned numbers, unsigned picks);

int main() {

using namespace std; double total, choices; double mtotal;

double probability1, probability2;

cout << \ \ while ((cin >> total >> choices) && choices <= total) {

cout << \ \ if (!(cin >> mtotal)) break;

SP 12 of 73 September 2, 2004

搜索更多关于: C++Primer - Plus(第五版)习题解答 的文档
C++Primer - Plus(第五版)习题解答.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c4ipnz2rizz3pebe0ilfj_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top