c++ primer plus 第四章编程题

第四章

1 按要求输出对象属性

#include <iostream>
#include <string.h>
using namespace std;

struct man
{
    char first_name[20];
    char last_name[20];
    char grade[1];
    int age;
};

int main()
{
    man man1;
    cout << "What is your first name? : ";
    cin.getline(man1.first_name,20);
    cout << "What is yout last name? : ";
    cin.getline(man1.last_name,20);
    cout << "What letter grades do you deserve? : ";
    cin >> man1.grade;
    cout << "What is your age? : ";
    cin >> man1.age;
    cout << "Name: " << man1.last_name << ' ' << man1.first_name << endl;
    cout << "Grade: " <<  char(man1.grade[0]+1) << endl;
    cout << "Age: " << man1.age << endl;
    return 0;
}

2 使用string改写程序 4.4

#include <iostream>
#include <string.h>

int main()
{
    using namespace std;
    const int ArSize = 20;
    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 delicions " << dessert;
    cout << " for you, " << name << " \n";
    return 0;
}

3 输入姓和名,以逗号加空格合并为新的字符串(使用char)

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    int i = 0;

    cout << "Input your first name: ";
    char firstname[21];
    cin >> firstname;
    
    cout << "Input your last name: ";
    char lastname[21];
    cin >> lastname;
    char name[42];
    
    for(i = 0; i < strlen(lastname); i++)
    {
        name[i] = lastname[i];
    }
    name[i] = ','; i++;
    name[i] = ' '; i++;

    for(int j = 0; j < strlen(firstname); j++)
    {
        name[i] = firstname[j];
        i++;
    }
      
    cout << "Your name is: " << name << endl;
    return 0;
}

4 输入姓和名,以逗号加空格合并为新的字符串(使用string)

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    cout << "Input your first name: ";
    string firstname;
    getline(cin,firstname);
    cout << "Input your last name: ";
    string lastname;
    getline(cin,lastname);
    lastname += ", ";
    lastname += firstname;
    cout << "Your name is: " << lastname << endl;
    return 0;
}

5 使用结构完成糖块的信息储存:在声明时初始化,并输出信息

#include <iostream>
using namespace std;

struct CandyBar
{
    string brand;
    double weight;
    int calories;
};


int main()
{
    CandyBar snack{"Micha Munch",2.3,350};
    cout << "The " << snack.brand 
         << "\'s weight is " << snack.weight
         << " and it has " << snack.calories
         << " calories" << endl; 
    return 0;
}

6 上一问题的后续,创建包含3个元素的CandyBar数组,并将其初始化为选择的值,然后显示每个结构的内容

#include <iostream>
using namespace std;

struct CandyBar
{
    string brand;
    double weight;
    int calories;
};

int main()
{
    CandyBar snacks[3];
    for(int i = 0; i < 3; i++)
    {
        cout << "输入第" << i+1 << "个元素的信息" << endl;
        cout << "输入品牌:";
        cin >> snacks[i].brand;
        cout << "输入重量:";
        cin >> snacks[i].weight;
        cout << "输入热量:";
        cin >> snacks[i].calories;
    }

    for(int i = 0; i < 3; i++)
    {
        cout << "第" << i+1 << "个元素" << endl; 
        cout << "品牌: " << snacks[i].brand << endl;
        cout << "重量: " << snacks[i].weight << endl;
        cout << "热量: " << snacks[i].calories << endl;

    }
    return 0;
}

7 设计披萨公司信息存储,使用结构.

#include <iostream>
#include <string>
using namespace std;

struct Pizza
{
    char company[21] = "a";
    double diameter = 0;
    double weight = 0;
};

int main()
{
    Pizza pizzalst[3];
    for(int i = 0; i < 3; i++)
    {
        cout << "输入第" << i+1 << "个披萨的信息" << endl;
        cout << "输入公司:";
        cin.getline(pizzalst[i].company,20);
        cout << "输入直径:";
        cin >> pizzalst[i].diameter;
        cin.get();
        cout << "输入重量:";
        cin >> pizzalst[i].weight;
        cin.get();
    }

    for(int i = 0; i < 3; i++)
    {
        cout << "第" << i+1 << "个披萨" << endl; 
        cout << "公司: " << pizzalst[i].company << endl;
        cout << "直径: " << pizzalst[i].diameter << endl;
        cout << "重量: " << pizzalst[i].weight << endl;

    }
    return 0;
}

8 在7的基础上,更改程序为使用new来分配内存,另外,让程序在请求公司名称之前输入披萨的直径

#include <iostream>
#include <string>
using namespace std;

struct Pizza
{
    char company[21] = "a";
    double diameter = 0;
    double weight = 0;
};

int main()
{
    Pizza *pizzalist = new Pizza[3];
    for(int i = 0; i < 3; i++)
    {
        cout << "输入第" << i+1 << "个披萨的信息" << endl;
        cout << "输入直径:";
        cin >> pizzalist[i].diameter;
        cin.get();
        cout << "输入公司:";
        cin.getline(pizzalist[i].company,20);
        cout << "输入重量:";
        cin >> pizzalist[i].weight;
        cin.get();
    }

    for(int i = 0; i < 3; i++)
    {
        cout << "第" << i+1 << "个披萨" << endl; 
        cout << "公司: " << pizzalist[i].company << endl;
        cout << "直径: " << pizzalist[i].diameter << endl;
        cout << "重量: " << pizzalist[i].weight << endl;

    }
    return 0;
}

9 在6的基础上.使用new来动态分配数组

#include <iostream>
using namespace std;

struct CandyBar
{
    string brand;
    double weight;
    int calories;
};

int main()
{
    CandyBar *snacks = new CandyBar[3];
    for(int i = 0; i < 3; i++)
    {
        cout << "输入第" << i+1 << "个元素的信息" << endl;
        cout << "输入品牌:";
        getline(cin,snacks[i].brand);
        cout << "输入重量:";
        cin >> snacks[i].weight;
        cin.get();
        cout << "输入热量:";
        cin >> snacks[i].calories;
        cin.get();
    }

    for(int i = 0; i < 3; i++)
    {
        cout << "第" << i+1 << "个元素" << endl; 
        cout << "品牌: " << snacks[i].brand << endl;
        cout << "重量: " << snacks[i].weight << endl;
        cout << "热量: " << snacks[i].calories << endl;

    }
    return 0;
}

10 输入三次跑步成绩,显示次数和平均成绩,使用array存储

#include <iostream>
#include <array>
using namespace std;


int main()
{
    cout << "请输入三次跑步的成绩:" << endl;
    array<double , 3>  scores;
    for(int i = 0; i < 3; i++)
    {
        cin >> scores[i];
    }
    double average = 0;
    average = (scores[0] + scores[1] + scores[2]) / 3;
    cout << "The average score is: " << average << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39933320/article/details/89879836