码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-10-25 14:36:16      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:c++ prime   c++初学   c++   

bubuko.com,布布扣

bubuko.com,布布扣

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;
}
运行结果

bubuko.com,布布扣

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;
}
运行结果

bubuko.com,布布扣

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;
}
运行结果

bubuko.com,布布扣

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;
}
运行结果

bubuko.com,布布扣

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;
}
运行结果

bubuko.com,布布扣

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;
}
运行结果

bubuko.com,布布扣

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;
}
运行结果

bubuko.com,布布扣

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;
}
运行结果

bubuko.com,布布扣


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;
}
运行结果

bubuko.com,布布扣

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

标签:c++ prime   c++初学   c++   

原文地址:http://blog.csdn.net/guugle2010/article/details/40452035

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!