C++ Primer Plus | chapter 4 课后编程练习

第四章 复合类型

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

#include<iostream>
#include<string>
using namespace std;
int main()
{
    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";
    return 0;
}

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    char first_name[20];
    char last_name[20];
    cout << "Enter your first name: ";
    cin.getline(first_name, 20);
    cout << "Enter your last name: ";
    cin.getline(last_name, 20);
    char* result = strcat(strcat(last_name, ", "), first_name);
    cout << "Here's the information in a single string: " << result;
    return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string first_name, last_name;
    cout << "Enter your first name: ";
    getline(cin, first_name);
    cout << "Enter your last name: ";
    getline(cin, last_name);
    string result = last_name + ", " + first_name;
    cout << "Here's the information in a single string: " << result;
    return 0;
}
#include<iostream>
using namespace std;
struct CandyBar
{
    char brand[20];
    double weight;
    int calorie;
};
int main()
{
    CandyBar snack = {"Mocha Munch", 2.3, 350};
    cout << "Brand: " << snack.brand << endl;
    cout << "Weight: " << snack.weight << endl;
    cout << "Calorie: " << snack.calorie << endl;
    return 0;
}
#include<iostream>
#include<cstdio>
using namespace std;
struct CandyBar
{
    char brand[20];
    double weight;
    int calorie;
};

int main()
{
    CandyBar snack[3] = {{"aaaa", 2.5, 50}, {"bbbb", 3.6, 80}, {"cccc", 5.9, 90}};
//    snack[0] = {"aaaa", 2.5, 50}; 只能在初始化时赋值
//    snack[1] = {"bbbb", 3.6, 80};
//    snack[2] = {"cccc", 5.9, 90};
    for(int i=0; i<3; i++)  printf("%s %lf %d\n", snack[i].brand, snack[i].weight, snack[i].calorie);
    return 0;
}
#include<iostream>
using namespace std;
struct Pizza
{
    char company[20];
    double diameter, weight;
};
int main()
{
    Pizza x;
    cout << "Enter the company of the pizza: ";
    cin.getline(x.company, 20);
    cout << "Enter the diameter of the pizza: ";
    cin >> x.diameter;
    cout << "Enter the weight of the pizza: ";
    cin >> x.weight;
    cout << "The company: " << x.company << "\nThe diameter: " << x.diameter << "\nThe weight: " << x.weight;
    return 0;
}
#include<iostream>
using namespace std;
struct Pizza
{
    char company[20];
    double diameter, weight;
};
int main()
{
    Pizza* x = new Pizza;
    cout << "Enter the diameter of the pizza: ";
    (cin >> x->diameter).get(); //cin后使用getline,要吸收回车键
    cout << "Enter the company of the pizza: ";
    cin.getline(x->company, 20);
    cout << "Enter the weight of the pizza: ";
    cin >> x->weight;
    cout << "The company: " << x->company << "\nThe diameter: " << x->diameter << "\nThe weight: " << x->weight;
    delete x;
    return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct CandyBar
{
    char brand[20];
    double weight;
    int calorie;
};

int main()
{
    CandyBar* snack = new CandyBar [3];
    strcpy(snack->brand, "aaaa"); snack->weight = 2.3; snack->calorie = 4;  //char*类型不可直接赋值
    strcpy((snack+1)->brand, "bbbb"); (snack+1)->weight = 2.4; (snack+1)->calorie = 5;
    strcpy((snack+2)->brand, "cccc"); (snack+2)->weight = 2.4; (snack+2)->calorie = 5;
    for(int i=0; i<3; i++)  printf("%s %lf %d\n", (*(snack+i)).brand, (*(snack+i)).weight, (*(snack+i)).calorie);
    return 0;
}
#include<iostream>
#include<array>
using namespace std;
int main()
{
    array<double, 3> score;
    double ans = 0;
    for(int i=0; i<3; i++)
    {
        cout << "Please enter the " << i+1 << " score: ";
        cin >> score[i];
    }
    for(int i=0; i<3; i++) ans += score[i];
    ans /= 3;
    cout << "Average: " << ans << endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值