C++ Primer Plus第四章练习

4.13.1

#include <iostream>
using namespace std;
int main(void)
{
    cout << "What is your first name? ";
    char fname[20];
    cin.getline(fname, 19);
    cout << "What is your last name? ";
    char lname[20];
    cin.getline(lname, 19);
    cout << "What letter grade do you deserve? ";
    char grade;
    cin >> grade;
    grade += 1;
    cout << "What is your age? ";
    int year;
    cin >> year;
    cout << "Name: " << lname << ", " << fname << endl;
    cout << "Grade: " << grade << endl;
    cout << "Age: " << year << endl; 
    return 0;
}

4.13.2

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

4.13.3

#include <iostream>
#include <cstring>
using namespace std;
int main(void)
{
    char fname[20], lname[20], name[40];
    cout << "Enter your first name: ";
    cin.getline(fname, 19);
    cout << "Enter your last name: ";
    cin.getline(lname, 19);
    strcpy(name, lname);
    strcat(name, ", ");
    strcat(name, fname);
    cout << "Here's the information in a single string: " << name << endl;
    return 0;
}

4.13.4

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

4.13.5

#include <iostream>
using namespace std;
struct CandyBar
{
    char name[20];
    float weight;
    int calorie;
};
int main(void)
{
    CandyBar snack = {"Mocha Munch", 2.3, 350};
    cout << snack.name << endl
         << snack.weight << endl
         << snack.calorie << endl;
    return 0;
}

4.13.6

#include <iostream>
using namespace std;
struct CandyBar
{
    char name[20];
    float weight;
    int calorie;
};
int main(void)
{
    CandyBar snack[3] = 
    {
        {"Mocha Munch", 2.3, 350},
        {"xswl", 2.33, 213},
        {"wssb", 4.44, 250}
    };
    for(int i = 0; i < 3; i++)
        cout << snack[i].name << endl
             << snack[i].weight << endl
             << snack[i].calorie << endl;
    return 0;
}

4.13.7

#include <iostream>
using namespace std;
struct Pizza
{
    char name[20];
    int diameter;
    int weight;
};
int main(void)
{
    Pizza pa;
    cout << "请输入比萨名称: ";
    cin >> pa.name;
    cout << "请输入比萨直径: ";
    cin >> pa.diameter;
    cout << "请输入比萨重量: ";
    cin >> pa.weight;
    cout << pa.name << endl
         << pa.diameter << endl
         << pa.weight << endl;
    return 0;
}

4.13.8

#include <iostream>
using namespace std;
struct Pizza
{
    char name[20];
    int diameter;
    int weight;
};
int main(void)
{
    Pizza * pa = new Pizza;
    cout << "请输入比萨直径: ";
    cin >> pa->diameter;
    cout << "请输入比萨名称: ";
    cin >> pa->name;
    cout << "请输入比萨重量: ";
    cin >> pa->weight;
    cout << pa->name << endl
         << pa->diameter << endl
         << pa->weight << endl;
    delete pa;
    return 0;
}

4.13.9

#include <iostream>
using namespace std;
struct CandyBar
{
    char name[20];
    float weight;
    int calorie;
};
int main(void)
{
    CandyBar * snack = new CandyBar[3];
    snack[0]->name = "ss";
    snack[0]->weight = 2.3;
    snack[0]->calorie = 4;
    snack[1]->name = "aa";
    snack[1]->weight = 2.3;
    snack[1]->calorie = 5;
    snack[2]->name = "xx";
    snack[2]->weight = 2.3;
    snack[2]->calorie = 6;
    for(int i = 0; i < 3; i++)
        cout << snack[i]->name << endl
             << snack[i]->weight << endl
             << snack[i]->calorie << endl;
    return 0;
}

4.13.10

#include <iostream>
#include <array>
using namespace std;
int main(void)
{
    array<int, 3> grade;
    for(int i = 0; i < 3; i++)
    {
        cout << "请输入第" << i + 1 << "次40码跑记录: ";
        cin >> grade[i];
    }
    cout << "平均成绩为: " << (grade[0] + grade[1] + grade[2]) / 3 << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值