【C++】学习笔记十四——C++ primer plus第六版第4章编程练习

  1. 1.
//4.13编程练习 1
#include<iostream>
#include<string>
int main()
{
    using namespace std;
    enum grade {A, B, C, D};
    cout << "What is your first name? ";
    string first_name;
    getline(cin, first_name);
    cout << "What is your last name? ";
    string last_name;
    cin >> last_name;
    cout << "What letter grade do you deserve? ";
    char grade;
    cin >> grade;
    cout << "What is your age? ";
    int age;
    cin >> age;
    cout << "Name: " << last_name << ", " << first_name << endl;
    cout << "Grade: " << char(grade + 1) << endl;
    cout << "Age: " << age;
    cin.get();
    cin.get();
    return 0;
}

这里写图片描述

2.

#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();
    cin.get();
    return 0;
}

这里写图片描述

3.

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

这里写图片描述

4.

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

这里写图片描述

5.

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
    string brand;
    double weight;
    int calorie;
};
int main()
{
    CandyBar snack
    {
        "Mocha Munch",
        2.3,
        350
    };
    cout << "snack: " << endl;
    cout << "brand: " << snack.brand << endl;
    cout << "weight: " << snack.weight << endl;
    cout << "calorie: " << snack.calorie << endl;
    cin.get();
    return 0;
}

这里写图片描述

6.

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
    string brand;
    double weight;
    int calorie;
};
int main()
{
    CandyBar snack[3]
    {
        {"Mocha Munch", 2.3, 350}, {"Dabaitu", 5.6, 520}, {"Wowo", 3.6, 450}
    };
    int i;
    for (i = 0; i < 3; i++)
    {
        cout << "Candy " << i << endl;
        cout << "\tbrand: " << snack[i].brand << endl;
        cout << "\tweight: " << snack[i].weight << endl;
        cout << "\tcalorie: " << snack[i].calorie << endl;
    }
    cin.get();
    return 0;
}

这里写图片描述

7.

#include<iostream>
#include<string>
using namespace std;
struct William
{
    string company_name;
    double pizza_diameter;
    double pizza_weight;
};
int main()
{
    William pizza;
    cout << "Enter the name of the company: ";
    getline(cin, pizza.company_name);
    cout << "Enter the diameter of the pizza: ";
    cin >> pizza.pizza_diameter;
    cout << "Enter the weight of the pizza:";
    cin >> pizza.pizza_weight;
    cout << "pizza: ";
    cout << "\tCompany: " << pizza.company_name << endl
        << "\tdiameter: " << pizza.pizza_diameter << endl
        << "\tweight: " << pizza.pizza_weight << endl;
    cin.get();
    cin.get();
    return 0;
}

这里写图片描述

8.

#include<iostream>
#include<string>
using namespace std;
struct William
{
    string company_name;
    double pizza_diameter;
    double pizza_weight;
};
int main()
{
    William * pizza = new William;
    cout << "Enter the diameter of the pizza: ";
    cin >> pizza->pizza_diameter;
    cout << "Enter the name of the company: ";
    cin.get();                                  //输入直径后,输入队列中还有换行符
    getline(cin, pizza->company_name);
    cout << "Enter the weight of the pizza:";
    cin >> pizza->pizza_weight;
    cout << "pizza: ";                                             
    cout << "\tCompany: " << pizza->company_name << endl
        << "\tdiameter: " << pizza->pizza_diameter << endl
        << "\tweight: " << pizza->pizza_weight << endl;
    delete pizza;
    cin.get();
    cin.get();
    return 0;
}

这里写图片描述

9.

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
    string brand;
    double weight;
    int calorie;
};
int main()
{
    CandyBar * snack = new CandyBar[3];
    snack[0] = { "Mocha Munch", 2.3, 350 };
    snack[1] = { "Dabaitu", 5.6, 520 };
    snack[2] = { "Wowo", 3.6, 450 };
    int i;
    for (i = 0; i < 3; i++)
    {
        cout << "Candy " << i << endl;
        cout << "\tbrand: " << snack[i].brand << endl;
        cout << "\tweight: " << snack[i].weight << endl;
        cout << "\tcalorie: " << snack[i].calorie << endl;
    }
    delete [] snack;        //不要忘记delete,不要忘记[]
    cin.get();
    return 0;
}

这里写图片描述

10.

#include<iostream>
#include<array>
int main()
{
    using namespace std;
    array<double, 3> grade;
    cout << "Enter your first grade: ";
    cin >> grade[0];
    cout << "Enter your second grade: ";
    cin >> grade[1];
    cout << "Enter your third grade: ";
    cin >> grade[2];
    double grade_avr = (grade[0] + grade[1] + grade[2]) / 3;
    cout << "The average grade of your 3 times grades is " << grade_avr;
    cin.get();
    cin.get();
    return 0;
}

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值