C-Primer-Plus-课后练习-第四章

38次阅读

共计 3762 个字符,预计需要花费 10 分钟才能阅读完成。

个人练习,仅供参考,欢迎大家提出意见,指出错误!喜欢的点个赞吧!

Practice 1

// 请求输入名字,成绩,年龄等,并输出 (需要向下调整成绩)

#include <iostream>

int main()
{
  using namespace std;

  char first_name[20];
  char last_name[20];
  char grade;
  int age = 0;
  cout << "What is your first name?" << endl;
  cin >> first_name;
  cout << "What is your last name?" << endl;
  cin >> last_name;
  cout << "What letter grade do you deserve?" << endl;
  cin >> grade;
  cout << "What is your age?" << endl;
  cin >> age;

  char next_grade = grade + 1;  
  //grade 自增 1 即可获得下个字母的 ASCII 码,将其赋给 char 即可获得相应字符
  // 若直接输出 grade+1 会输出该 ASCII 码

  cout << "Name:" << last_name << "," << first_name << endl;
  cout << "Grade:" << next_grade << endl;
  cout << "Age:" << age << endl;
  return 0;
  
}

Practice 2

// 使用 string 类完成练习 1

#include <iostream>
#include <string>

int main()
{
  using namespace std;

  string first_name, last_name;
  char grade;
  int age = 0;

  cout << "What is your first name?" << endl;
  getline(cin, first_name);
  cout << "What is your last name?" << endl;
  getline(cin, last_name);
  cout << "What letter grade do you deserve?" << endl;
  cin >> grade;
  cout << "What is your age?" << endl;
  cin >> age;

  grade ++; // 与上题不同,这里用了自增运算符达到同样的效果

  cout << "Name:" << last_name << "," << first_name << endl;
  cout << "Grade:" << grade << endl;
  cout << "Age:" << age << endl;
  return 0;
}

Practice 3

// 使用 char 数组和 cstring 头文件进行字符串拼接

#include <iostream>
#include <cstring>

int main()
{
  using namespace std;

  char first_name[20];
  char last_name[20];
  char full_name[45] = ""; 
  // 踩坑:一定要初始化,否则输出结果不正确
  const char link[3] = ",";

  cout << "Enter first name:" << endl;
  cin >> first_name;

  cout << "Enter last name:" << endl;
  cin >> last_name;

  strcat(full_name, first_name);
  strcat(full_name, link);
  strcat(full_name, last_name);

  cout << "Full name is" << full_name << endl;
  return 0;
}

Practice 4

// 使用 string 对象和 string 头文件实现上题

#include <iostream>
#include <string>

int main()
{
  using namespace std;

  string first_name = "";
  string last_name = "";
  string link = ",";
  cout << "Enter your first name:" << endl;
  getline(cin, first_name);
  cout << "Enter your last name:" << endl;
  getline(cin, last_name);

  string full_name = first_name + link + last_name;

  cout << "Your full name is:" << full_name << endl;
}

Practice 5

// 声明一个结构并初始化之

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

struct CandyBar
{
  string band;
  double weight;
  int cal;
};

int main()
{
  CandyBar snack = {
    "Mocha Munch",
    2.3,
    350
  };

  cout << snack.band << endl << snack.weight << endl << snack.cal << endl;
  return 0;
}

Practice 6

// 声明一个结构数组并初始化之

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

struct CandyBar
{
  string band;
  double weight;
  int cal;
};

int main()
{CandyBar snacks[3] = {
    {
      "Mocha Munch A",
      2.3,
      350
    },
    {
      "Mocha Munch",
      2.311,
      350
    },
    {
      "Mocha Munch",
      2.3,
      550
    },
  };

  cout << snacks[0].band << endl << snacks[1].weight << endl << snacks[2].cal << endl;
  return 0;
}

Practice 7

// 声明一个结构并初始化

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

struct CandyBar
{
  string band;
  double weight;
  int cal;
};

int main()
{
  CandyBar snack;

  cout << "Enter band:" << endl;
  getline(cin, snack.band);

  cout << "Enter weight:" << endl;
  cin >> snack.weight;

  cout << "Enter cal:" << endl;
  cin >> snack.cal;

  cout << snack.band << endl << snack.weight << endl << snack.cal << endl;
  return 0;
}

Practice 8

// 使用 new 生成一个结构数组并初始化

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

struct CandyBar
{
  string band;
  double weight;
  int cal;
};

int main()
{
  CandyBar* snack = new CandyBar;

  cout << "Enter weight:" << endl;
  (cin >> snack -> weight).get(); // 混合输入数字和面向行的字符串会导致问题, 需要再进行一次 get()

  cout << "Enter band:" << endl;
  getline(cin, snack -> band);


  cout << "Enter cal:" << endl;
  cin >> snack -> cal;

  cout << snack -> band << endl << snack -> weight << endl << snack -> cal << endl;
  return 0;
}

Practice 9

// 使用 new 生成一个结构数组并初始化之

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

struct CandyBar
{
  string band;
  double weight;
  int cal;
};

int main()
{CandyBar* snacks = new CandyBar [3];

  snacks[0] = {
      "Mocha Munch A",
      2.3,
      350
  };

  snacks[1] = {
      "Mocha Munch A",
      2.3111,
      350
  };

  snacks[2] = {
      "Mocha Munch A",
      2.3111,
      550
  };

  cout << snacks[0].band << endl << snacks[1].weight << endl << snacks[2].cal << endl;
  return 0;
}

Practice 10

// 使用 array 对象创建数组并存入数据

#include <iostream>
#include <array>

int main()
{
  using namespace std;
  array<double, 3> grades;
  cout << "Enter three grades:" << endl;

  double sum = 0;

  for(int i = 0; i < 3; i++){cin >> grades[i];
    sum += grades[i];
  }

  cout << "Times:" << 3 << endl << "Average:" << (sum / 3) << endl;
  return 0;

}

欢迎访问 Github 仓库地址

正文完
 0