C++Primerplus编程练习 第四章

第四章

1

在这里插入图片描述

#include <iostream>                                                        
#include <string.h>
int main()
{
    using namespace std;
    string firstName;
    string lastName;
    char grade;
    int age;
    cout << "What is your first name? ";
    getline(cin,firstName);
    cout << "What is your lastName? ";
    getline(cin,lastName);
    cout << "What letter grade do you deserve? ";
    cin >> grade;
    cout << "What is yout age? ";
    cin >> age;
    cout << "Name: "<< lastName << ", "<<firstName<<endl;
    cout << "Grade: "<<char(grade+1)<<endl;
    cout << "Age: " << age << endl;
    return 0;
}

2

在这里插入图片描述

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

3

在这里插入图片描述

#include <iostream>
#include <string.h>
int main()
{
    using namespace std;
    char firstName[20];
    char lastName[20];
    cout << "Enter your first name: ";
    cin.getline(firstName, 20);
    cout << "Enter your last name: ";
    cin.getline(lastName, 20);                                             
    cout << "Here's the information in a single string: "<<lastName<<", "<<
    return 0;
}

4

在这里插入图片描述

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

5

在这里插入图片描述

#include <iostream>                                                        
#include <string.h>
struct CandyBar{
    std::string brand;
    float weight;
    int calorie;
};
int main()
{
    CandyBar snack{"Mocha Munch", 2.3, 350};
    std::cout << "snack's brand is "<<snack.brand;
    std::cout << " weight is "<< snack.weight;
    std::cout << " calorie is " << snack.calorie<< std::endl;
    return 0;
}

6

在这里插入图片描述

#include <iostream>                                                        
#include <string.h>
struct CandyBar{
    std::string brand;
    float weight;
    int calorie;
};
int main()
{
    using namespace std;
    CandyBar snack[3]{
        {"Mocha Munch", 2.3, 350},
        {"rabbit",1.3, 214},
        {"dragon",2.7,420}};

    cout << "snack's brand is "<<snack[0].brand
    << " weight is "<< snack[0].weight
    << " calorie is " << snack[0].calorie<< std::endl;
    cout << "rabbit's brand is "<<snack[1].brand
    << " weight is "<< snack[1].weight
    << " calorie is " << snack[1].calorie<< std::endl;
    cout << "dragon's brand is "<<snack[2].brand
    << " weight is "<< snack[2].weight
    << " calorie is " << snack[2].calorie<< std::endl;
    return 0;
}

7

在这里插入图片描述

#include <iostream>
#include <string.h>
using namespace std;
struct pizza{
    string company;
    float diameter;
    float weight;
};
int main()
{
    pizza piz;
    cout << "请输入公司名: ";
    getline(cin, piz.company);
    cout << "请输入披萨饼的直径: ";
    cin >> piz.diameter;
    cout << "请输入披萨饼的重量: ";
    cin >> piz.weight;

    cout << piz.company << " 生产的披萨饼直径为 "<< piz.diameter
        << " 重达 " << piz.weight<<endl;                                   
    return 0;
}

8

在这里插入图片描述

#include <iostream>
#include <string.h>
using namespace std;
struct pizza{
    string company;
    float diameter;
    float weight;
};
int main()
{
    pizza* piz = new pizza;
    cout << "请输入披萨饼的直径: ";
    cin >> piz->diameter;
    cin.get();                                                             
    cout << "请输入公司名: ";
    getline(cin, piz->company);
    cout << "请输入披萨饼的重量: ";
    cin >> piz->weight;

    cout << piz->company << " 生产的披萨饼直径为 "<< piz->diameter
        << " 重达 " << piz->weight<<endl;
    delete piz;
    return 0;
}

9

在这里插入图片描述

#include <iostream>
#include <string.h>
struct CandyBar{
    std::string brand;
    float weight;
    int calorie;
};
int main()
{
    using namespace std;
    CandyBar* snack = new CandyBar[3];
    snack[0]= {"Mocha Munch", 2.3, 350};
    snack[1]= {"rabbit",1.3, 214};
    snack[2]= {"dragon",2.7,420};

    cout << "snack's brand is "<<snack[0].brand
    << " weight is "<< snack[0].weight
    << " calorie is " << snack[0].calorie<< std::endl;
    cout << "rabbit's brand is "<<snack[1].brand
    << " weight is "<< snack[1].weight
    << " calorie is " << snack[1].calorie<< std::endl;
    cout << "dragon's brand is "<<snack[2].brand
    << " weight is "<< snack[2].weight
    << " calorie is " << snack[2].calorie<< std::endl;
    delete [] snack;                                                       
    return 0;
}

10

在这里插入图片描述

#include <iostream>
#include <array>
using namespace std;
int main()
{
    array<float,3> score;
    cout << "请输入你三次跑步的成绩(s)\n";
    cout << "请输入第一次的成绩: ";
    cin >> score[0];
    cout << "请输入第二次的成绩: ";
    cin >> score[1];
    cout << "请输入第三次的成绩: ";
    cin >> score[2];
    cout << "一共跑了三次,平均成绩为 " << (score[0]+score[1]+score[2])/3.<
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值