C++ Primer plus第六版编程答案第四章

本文展示了C++编程中基础的输入输出操作,使用了getline函数、string类型以及结构体如CandyBar和Pizza,涉及动态内存分配和数组的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.cpp

2.cpp

3.cpp

4.cpp

5.cpp

6.cpp

7.cpp

8.cpp

9.cpp

10.cpp


1.cpp

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

2.cpp

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

3.cpp

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

4.cpp

#include<iostream>
#include<string>
using namespace std;
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:" << last_name + ", "+first_name<<endl;

    return 0;
}

5.cpp

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

6.cpp

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
    string brand;
    double weight;
    int calorie;
    /* data */
};

int main()
{
    const int ArSize = 3;
    CandyBar gift[ArSize] = 
    {
        {"brand1",2.3,100},
        {"brand2",2.4,200},
        {"brand3",2.5,300}
    };
    cout << "brand:" << gift[0].brand << " weight:" << gift[0].weight << " calorie:" << gift[0].calorie <<endl;
    cout << "brand:" << gift[1].brand << " weight:" << gift[1].weight << " calorie:" << gift[1].calorie <<endl;
    cout << "brand:" << gift[2].brand << " weight:" << gift[2].weight << " calorie:" << gift[2].calorie <<endl;
    return 0;
}

7.cpp

#include<iostream>
#include<string>
using namespace std;
struct pizza
{
    string name;
    double diameter;
    double weight;
    /* data */
};

int main()
{
    pizza brand;
    cout << "请输入披萨公司的名称:";
    getline(cin,brand.name);
    cout << "请输入披萨的直径:";
    cin >> brand.diameter;
    cout << "请输入披萨的重量:";
    cin >> brand.weight;
    cout << "披萨的品牌:" << brand.name << " 直径:" << brand.diameter << " 重量:" << brand.weight <<endl;
    return 0;
}

8.cpp

#include<iostream>
#include<string>
using namespace std;
struct pizza
{
    string name;
    double diameter;
    double weight;
    /* data */
};
int main()
{
    pizza* brand = new pizza;
    cout << "请输入披萨的直径:";
    cin >> brand->diameter;
    cout << "请输入披萨公司的名称:";
    cin.get();
    getline(cin,brand->name);
    cout << "请输入披萨的重量:";
    cin >> brand->weight;
    cout << "name:" << brand->name << " diameter:" << brand->diameter << " weight:" << brand->weight <<endl;
    delete brand;
    return 0;
}

9.cpp

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
    string brand;
    double weight;
    int calorie;
    /* data */
};

int main()
{
    CandyBar* gift = new CandyBar [3]
    {
        {"brand1",2.3,100},
        {"brand2",2.4,200},
        {"brand3",2.5,300}
    };
    cout << "brand:" << gift[0].brand << " weight:" << gift[0].weight << " calorie:" << gift[0].calorie <<endl;
    cout << "brand:" << gift[1].brand << " weight:" << gift[1].weight << " calorie:" << gift[1].calorie <<endl;
    cout << "brand:" << gift[2].brand << " weight:" << gift[2].weight << " calorie:" << gift[2].calorie <<endl;
    return 0;
}

10.cpp

#include<iostream>
#include<array>
using namespace std;
int main()
{
    array<double,3> grade;
    cout << "grade of three times :";
    cin >> grade[0];
    cin >> grade[1];
    cin >> grade[2];
    cout << "times:" << 3 << " average grade:" << (grade[0]+grade[1]+grade[2])/3 <<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值