C++ Primer第四章课后编程题

1、
代码
#include<iostream>
#include<string>
int main()
{
    using namespace std;
    string name;
    string lname;
    char grade;
    int age;
    cout << "What is your first name?";
    getline(cin, name);
    cout << "What is your last name?";
    cin >> lname;
    cout << "What letter grade do you deserve?";
    cin >> grade;
    grade = grade + 1;
    cout << "what is your age?";
    cin >> age;
    cout << "Name:" << lname << "," << name <<endl;
    cout << "Grade:" <<grade <<endl;
    cout << "Age" << age << endl;
    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";
    return 0;
}
运行结果

3、
代码
#include<iostream>
#include<cstring>
int main()
{
    using namespace std;
    const int SIZE = 20;
    char fname[SIZE];
    char lname[SIZE];
    char fullname[2*SIZE+1];
    cout << "Enter your first name:";
    cin >> fname;
    cout << "Enter your last name:";
    cin >> lname;
    strncpy(fullname,lname,SIZE);
    strcat(fullname,", ");
    strncat(fullname,fname,SIZE);
    cout << "Here's yhe information in a single string:" << fullname <<endl;
    return 0;
}
运行结果

4、
代码
#include<iostream>
#include<string>
int main()
{
    using namespace std;
    string fname;
    string lname;
    string fullname;
    cout << "Enter your first name:";
    cin >> fname;
    cout << "Enter your last name:";
    cin >> lname;
    fullname = lname + ", " + fname;
    cout << "Here's yhe information in a single string:" << fullname <<endl;
    return 0;
}
运行结果

5、
代码
#include<iostream>
struct CandyBar
{
    char brand[20];
    double weight;
    int energy;
};
int main()
{
    using namespace std;
    CandyBar snack =
    {
        "Mocha Munch",
        2.3,
        350
    };
    cout << "品牌名为:" << snack.brand <<endl;
    cout << "重量为:" << snack.weight <<endl;
    cout << "能量为:" << snack.energy <<endl;
    return 0;
}
运行结果

6、
代码
#include<iostream>
struct CandyBar
{
    char brand[20];
    double weight;
    int energy;
};
int main()
{
    using namespace std;
    CandyBar snack[3] =
    {
        {"Mocha Munch", 2.3,350},
        {"Coca-cola", 3.1, 500},
        {"Nestle", 3.8, 200}
    };
    cout << "品牌名为:" << snack[0].brand <<endl;
    cout << "重量为:" << snack[0].weight <<endl;
    cout << "能量为:" << snack[0].energy <<endl;
    cout << "品牌名为:" << snack[1].brand <<endl;
    cout << "重量为:" << snack[1].weight <<endl;
    cout << "能量为:" << snack[1].energy <<endl;
    cout << "品牌名为:" << snack[2].brand <<endl;
    cout << "重量为:" << snack[2].weight <<endl;
    cout << "能量为:" << snack[2].energy <<endl;
    return 0;
}
运行结果

7、
代码
#include<iostream>
const int LEN = 70;
struct inflatable
{
  char name[LEN];
  double diameter;
  double weight;
};

int main()
{
  using namespace std;
  inflatable pizza;
  cout << "请输入公司名称:";
  cin.getline(pizza.name, LEN);
  cout << "请输入比萨饼的直径:";
  cin >> pizza.diameter;
  cout << "请输入比萨的重量:";
  cin >> pizza.weight;
  cout << "公司:" << pizza.name <<endl;
  cout << "直径:" << pizza.diameter <<endl;
  cout << "重量:" << pizza.weight <<endl;
  return 0;
}
运行结果

8、
代码
#include<iostream>
const int LEN = 70;
struct inflatable
{
  char name[LEN];
  double diameter;
  double weight;
};
int main()
{
  using namespace std;
  inflatable *ps = new inflatable;
  cout << "请输入公司名称:";
  cin.getline(ps->name, LEN);
  cout << "请输入比萨饼的直径:";
  cin >> (*ps).diameter;
  cout << "请输入比萨的重量:";
  cin >> ps->weight;
  cout << "公司:" << (*ps).name <<endl;
  cout << "直径:" << ps->diameter <<endl;
  cout << "重量:" << ps->weight <<endl;
  delete ps;
  return 0;
}
运行结果


9、//暂时有问题,先发布
代码
#include<iostream>
const int LEN=60;
struct CandyBar
{
  char brand[LEN];
  double weight;
  int energy;
};
int main()
{
  using namespace std;
  CandyBar *snack = new CandyBar[3];
  cout << "输入品牌名称:";
  cin.getline(snack->brand, LEN);
  cout << "请输入重量:";
  cin >> snack->weight;
  cout << "请输入能力:";
  cin >> snack->energy;
  cout << "输入品牌名称:";
  cin.getline((snack+1)->brand, LEN);
  cout << "请输入重量:";
  cin >> (snack+1)->weight;
  cout << "请输入能力:";
  cin >> (snack+1)->energy;
  cout << "第一个数组数据:" << snack->brand << " " << snack->weight << " " <
< snack->energy <<endl;
  cout << "第二个数组数据:" << (snack+1)->brand << " " << (snack+1)->weight
<< " " << (snack+1)->energy <<endl;
  return 0;
}
运行结果



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值