c++ primer plus 第四章 课后题答案

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string first_name;
    string last_name;
    char grade;
    int age;

    cout << "What is your first name? ";
    getline(cin,first_name);
    cout << endl << "What is your last name? ";
    getline(cin,last_name);
    cout << endl << "What letter grade do you deserve? ";
    cin >> grade;
    cout << endl << "What is your age? ";
    cin >> age;

    cout << "Name: " << last_name << ", " << first_name << endl;
    cout << "Grade: " << char(grade + 1) << endl;
    cout << "Age: " << age << endl;
    cin.get();
    cin.get();
    return 0;
}

#include <iostream>
#include<string>

int main()
{
    using namespace std;

    string name;
    string dessert;

    cout << "Enter your name:\n";
    getline(cin,name);
    cout << "Enter your favorite dessert:\n";
    getline(cin,dessert);
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << ".\n";
    cin.get();
    return 0;
}

#include<iostream>
#include<cstring>

using namespace std;

int main()
{
    char first_name[20];
    char last_name[20];
    char name[41];

    cout << "Enter your first name: ";
    cin >> first_name;
    cout << endl << "Enter your last name: ";
    cin >> last_name;

    strcpy_s(name, last_name);
    strcat_s(name, ",");
    strcat_s(name, first_name);
    cout << endl << "Here’s the information in a single string: " << name;

    //cout << endl << "Here’s the information in a single string: " << last_name << " , " << first_name;

    cin.get();
    cin.get();
    return 0;
}

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string first_name;
    string last_name;
    string name;

    cout << "Enter your first name: ";
    getline(cin,first_name);
    cout << "Enter your last name: ";
    getline(cin,last_name);

    name = last_name + "," + first_name;
    cout << "Here‘s the information in a single string: " << name << endl;
    cin.get();
    //system("pause");
    return 0;
}

#include<iostream>
using namespace std;

struct CandyBar
{
    char bard[20];
    double weight;
    int calories;
};

int main()
{
    CandyBar snack =
    {
        "Mocha Munch",
        2.3,
        350
    };

    cout << "The bard of this candy is: " << snack.bard << endl;
    cout << "The weight of this candy is: " << snack.weight << endl;
    cout << "The calories of this candy is: " << snack.calories << endl;

    cin.get();
    return 0;
}

#include<iostream>
using namespace std;

struct Candy {
    char name[20];
    double weight;
    int calories;
};

int main() {
    //Candy snack[3];
    //snack[0] = { "Mocha Munch1", 2.3, 350 };
    //snack[1] = { "Mocha Munch2", 2.5, 360 };
    //snack[2] = { "Mocha Munch3", 2.7, 390 };
    Candy snack[3] = { { "Mocha Munch1", 2.3, 350 } ,
                       { "Mocha Munch2", 2.5, 360 } ,
                       { "Mocha Munch3", 2.7, 390 } };
    cout << snack[0].name << "‘s weight is " << snack[0].weight <<
        ", and it includes " << snack[0].calories << " calories.\n";
    cout << snack[1].name << "‘s weight is " << snack[1].weight <<
        ", and it includes " << snack[1].calories << " calories.\n";
    cout << snack[2].name << "‘s weight is " << snack[2].weight <<
        ", and it includes " << snack[2].calories << " calories.\n";

    system("pause");
    return 0;
}

#include<iostream>
using namespace std;

struct Pizza
{
    char name[20];
    double diameter;
    double weight;
};

int main()
{
    Pizza x;
    cout << "Please enter the name of this pizza: ";
    cin >> x.name;
    cout << "Please enter the diameter of this pizza: ";
    cin >> x.diameter;
    cout << "Please enter the weight of this pizza: ";
    cin >> x.weight;

    cout << "The name of this pizza is " << x.name << endl;
    cout << "The diameter of this pizza is " << x.diameter << endl;
    cout << "The weight of this pizza is " << x.weight << endl;

    system("pause");
    return 0;
}

#include<iostream>
using namespace std;

struct Pizza {
    char name[20];
    double diameter;
    double weight;
};

int main() {
    Pizza *pizza = new Pizza;
    cout << "Please enter the name of this pizza: ";
    cin >> pizza->name;
    cout << "Please enter the diameter of this pizza: ";
    cin >> pizza->diameter;
    cout << "Please enter the weight of this pizza: ";
    cin >> pizza->weight;

    cout << "The name of this pizza is " << pizza->name << endl;
    cout << "The diameter of this pizza is " << pizza->diameter << endl;
    cout << "The weight of this pizza is " << pizza->weight << endl;

    delete pizza;

    system("pause");
    return 0;
}

#include<iostream>
using namespace std;

struct Candy {
    char name[20];
    double weight;
    int calories;
};

int main()
{
    Candy *snack = new Candy[3];
    snack[0] = { "Mocha Munch1", 2.3, 350 };
    snack[1] = { "Mocha Munch2", 2.5, 360 };
    snack[2] = { "Mocha Munch3", 2.7, 390 };

    cout << snack[0].name << "‘s weight is " << snack[0].weight <<
        ", and it includes " << snack[0].calories << " calories.\n";
    cout << snack[1].name << "‘s weight is " << snack[1].weight <<
        ", and it includes " << snack[1].calories << " calories.\n";
    cout << snack[2].name << "‘s weight is " << snack[2].weight <<
        ", and it includes " << snack[2].calories << " calories.\n";

    delete [] snack;

    system("pause");
    return 0;
}

#include<iostream>
#include<array>

using namespace std;
const int Times = 3;

int main()
{
    array<double, Times> grade;
    int i;
    double avg_grade=0.0;

    cout << "Please enter your grade: " << endl;

    for (i = 0; i <= 2; i++)
    {
        cout << endl << ("%d", i+1) << " time: ";
        cin >> grade[i];
        avg_grade += grade[i];
    }

    avg_grade = avg_grade / Times;
    cout << endl << "The grade of average is: " << avg_grade << endl;

    system("pause");
    return 0;
} 

原文地址:https://www.cnblogs.com/CJT-blog/p/10230387.html

时间: 2024-10-08 10:10:14

c++ primer plus 第四章 课后题答案的相关文章

c++ primer plus 第三章 课后题答案

#include<iostream> using namespace std; int main() { const int unit=12; int shen_gao; cout <<"Please enter your leight in inches:____\b\b\b"; cin >> shen_gao; cout << "It is contains: " << shen_gao / unit

C++ Primer 第五版 部分课后题答案

当时刚学C++的时候买了这本书,一开始前面看的一知半解,索性就先缓缓,等学完学校的C++课程(中途自己也写了不少c++的代码),一段时间之后又拿起这本书去看,感觉还是挺有滋味的,这本书对我印象中的C++做了很大的扩展,个人认为这本书不太适合刚学C++就去看,而是写了一定的代码,对C++有一个大体的了解之后再去看会很有味道.在看书的过程中自己也写了上面的课后练习题,现在整理一下,也跟大家分享一下,下面是9~12 15~16章的课后题编程题的答案 (第八章之前的都没保存/(ㄒoㄒ)/~~): 当时保

第四章课后题

1.结对项目的案例和论文学术界.工业界对结对编程已经有不少研究,请阅读至少两篇相关论文或论文,结合自己的切身体会总结一下.(1)提高效率 结对编程的形式使得代码处于不断地审查过程,每一段代码都由一个人编写,另一个人检查,最大程度上减少了出现bug的可能:两人互相交流,商讨实现方式,遇到问题时,能够做到互补.(2)互相学习 结对编程也是一个互相学习的过程.在结对编程过程中,两人会不断对实现方法.代码风格或命名方法等进行讨论,两个人的思路能够进行互补,在编写过程中能够学到对方解决问题的思路和方法,对

python核心编程第4章课后题答案(第二版75页)

4-1Python objects All Python objects have three attributes:type,ID,and value. All are readonly with a possible expection of the value(which can be changed only if the object is mutable). 4-5str()and repr() repr() is a built-in function while str() wa

python核心编程第2章课后题答案(第二版36页)

2-5 Loops and Numbers a) i = 0    while i <11:     print i    i += 1 b) for i in range(0,11): print i 2-6 Conditionals n =int( raw_input('enter a number:')) if n < 0: print 'negative' elif n > 0: print 'positive' else: print 'zero' 2-7 Loops and

C++ primer (第五版)课后题答案(八)

8.10 #include <sstream> #include <fstream> #include <iostream> #include <string> #include <vector> using namespace std; void main() { vector <string> ch; string word, line; ifstream input("test.txt"); if (!inp

python核心编程第5章课后题答案

5-8Geometry import math def sqcube(): s = float(raw_input('enter length of one side: ')) print 'the area is:', s ** 2., '(units squared)' print 'the volume is:', s ** 3., '(cubic units)'def cirsph(): r = float(raw_input('enter length of radius: ')) p

c++面向对象程序设计 课后题 答案 谭浩强 第四章

c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} double get_real(); double get_imag(); void display(); private: double real; double imag;

C Primer Plus (第四章总结)

1.定义字符串可以直接在头文件下定义,如: #include <stdio.h> #define hello  "hello world!" 2.sizeof() 和 strlen() sizeof运算符是以字节为单位给出数据的大小,strlen()是以字符为单位给出长度. <string.h>包含许多与字符串相关的函数的原型,包括strlen() sizeof运算符提供的数据比肉眼直观的要大多一位,因为他把用来标志字符串的不可见的空字符也计算在内. 定义常量最