当前位置:   article > 正文

C++ Primer Plus(第六版)中文版课后答案_c++primer plus第六版编程题答案

c++primer plus第六版编程题答案

第四章编程练习(1-5)

4.13.1.cpp

#include<iostream>
using namespace std;
struct infatable {
    char first_name[20];
    char last_name[20];
    int age;
    char grade;
};
int main() {
    infatable a;
    cout << "What's your first name? ";
    cin >> a.first_name;
    cout << "What's your last name? ";
    cin >> a.last_name;
    cout << "What letter grade do you deserve? ";
    cin >> a.grade;
    cout << "What's your age? ";
    cin >> a.age;
    cout << "Name: " << a.last_name << "," << a.first_name << endl
        << "Grade: " << ++(a.grade) << endl
        << "Age: " << a.age << endl;
    return 0;
}
 

4.13.2.cpp

#include<iostream>
#include<string>
using namespace std;
int main() {
    string name, dessert;
    cout << "Enter your name: ";
    getline(cin, name);
    cout << "Enter your favorite dessert: ";
    getline(cin, dessert);
    cout << "I have some delicious " << dessert << " for you,"
        << name << "." << endl;
    return 0;
}

4.13.3.cpp

#include<iostream>
#include<cstring>
using namespace std;
int main() {
    const int num = 20;
    char first_name[num];
    char last_name[num];
    char name[num * 2];
    cout << "Enter your first name: ";
    cin.getline(first_name,num);
    cout << "Enter your last name: ";
    cin.getline(last_name,num);
    strcpy_s(name, last_name);
    strcat_s(name, ",");
    strcat_s(name, first_name);
    cout << "Here's the information in a single string : ";
    cout << name << endl;
    return 0;
}

4.13.4.cpp

#include<iostream>
#include<string>
using namespace std;
string name(string a, string b) {
    string c;
    c = b + "," + a;
    return c;
}
int main() {
    string first_name;
    string last_name;
    cout << "Enter your first name: ";
    getline(cin,first_name);
    cout << "Enter your last name: ";
    getline(cin,last_name);
    cout << "Here's the information in a single string : ";
    cout << name(first_name, last_name);
    return 0;
}

4.13.5

#include<iostream>
using namespace std;
struct CandyBar {
    string brand;
    float weight;
    int cal;
};
int main() {
    CandyBar snack{
        "Mocha Munch",
        2.3,
        350
    };
    cout << "The brand of sugar cubes: " << snack.brand << endl;
    cout << "The weight of sugar cubes: " << snack.weight << endl;
    cout << "The calorie of sugar cubes: " << snack.cal << endl;
    return 0;
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/497695
推荐阅读
相关标签
  

闽ICP备14008679号