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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值