21xrx.com
2024-09-19 09:55:35 Thursday
登录
文章检索 我的文章 写文章
C++ Primer第四版习题答案
2023-06-26 19:42:37 深夜i     --     --
C++ Primer Fourth Edition Exercises Answers Programming

C++ Primer是一本非常经典的C++编程语言教材,被广泛认为是学习C++编程的入门指南。其中,第四版的习题部分是该教材的重要内容之一,涵盖了很多C++编程的难点和重点。以下是C++ Primer第四版习题的答案概述。

第1章:开始学习C++

1.3:输出Hello, World!

#include

int main()

World!" << std::endl;

  return 0;

1.4:使用符号完成加法运算

#include

int main()

{

  std::cout << "Enter two numbers:" << std::endl; 

  int v1 = 0, v2 = 0;

  std::cin >> v1 >> v2; 

  std::cout << "The sum of " << v1 << " and " << v2

       << " is " << v1 + v2 << std::endl;

  return 0; 

}

1.5:使用符号完成加法和乘法运算

#include

int main()

{

  std::cout << "Enter two numbers:" << std::endl; 

  int v1 = 0, v2 = 0;

  std::cin >> v1 >> v2; 

  std::cout << "The sum of " << v1 << " and " << v2

       << " is " << v1 + v2 << std::endl;

  std::cout << "The product of " << v1 << " and " << v2

       << " is " << v1 * v2 << std::endl;

  return 0;

}

第2章:变量和基本类型

2.1.1:变量初始化

(a) int i = 0;

(b) int j = i;

(c) int k = 3.14; //错误,k是整型,不能被初始化为浮点型

(d) double salary = wage = 9999.99; //错误,wage未定义

2.1.2:区别int和unsigned类型的取值范围

int类型取值范围:-2^31~2^31-1

unsigned类型取值范围:0~2^32-1

2.2.1:字面值常量

(a) 'a', L'a', "a", L"a"

(b) 10, 10u, 10L, 10uL, 012, 0xC

(c) 3.14, 3.14f, 3.14L

(d) 10, 10u, 10., 10e-2

2.3.1:变量定义

(a) int i = 0;

(b) int a = 3, b = 4;

(c) int i = 3.14; //错误,i是整型,不能被初始化为浮点型

(d) double salary = wage = 9999.99; //错误,wage未定义

2.4.1:const修饰符

const int i = 42;

(i) 合法

(ii) 非法,不能修改i的值

(iii) 非法,不能修改i的值

(iv) 非法,不能修改i的值

第3章:字符串、向量和数组

3.2.1:标准库类型vector

#include

#include

using namespace std;

int main()

{

  vector ivec;

  int i;

  while (cin >> i) //输入

    ivec.push_back(i); //将输入添加到vector

  return 0;

}

3.2.2:迭代器

#include

#include

using namespace std;

int main()

{

  vector ivec;

  int i;

  while (cin >> i) //输入

    ivec.push_back(i); //将输入添加到vector

  for (auto it = ivec.begin(); it != ivec.end(); ++it)

    cout << *it << endl; //输出vector

  return 0;

}

3.3.2:范围for

#include

#include

using namespace std;

int main()

{

  vector ivec;

  int i;

  while (cin >> i) //输入

    ivec.push_back(i); //将输入添加到vector

  for (auto i : ivec) //遍历vector

    cout << i << endl; //输出vector

  return 0;

}

3.4.1:定义和初始化数组

(a) int ia[10];

(b) int ib[10] = 1;

(c) int ic[10]; for (int i = 0; i < 10; ++i) ic[i] = i;

(d) vector ivec(10);

3.4.2:字符数组和字符串数组

(a) char ca[] = {'C', '+', '+'};

(b) char sa[] = {"C++"};

第4章:表达式

4.3:定义和使用含参数的函数

#include

using namespace std;

int abs(int i)

{

  return i > 0 ? i : -i;

}

int main()

{

  int i;

  cin >> i;

  cout << abs(i) << endl;

  return 0;

}

4.4:使用迭代器访问数组元素和vector元素

#include

#include

using namespace std;

int main()

{

  int a[] = 4;

  vector v(a, a + 5);

  for (auto i = begin(v); i != end(v); ++i) //使用迭代器输出vector

    cout << *i << " ";

  cout << endl;

  for (auto i = begin(a); i != end(a); ++i) //使用迭代器输出数组

    cout << *i << " ";

  cout << endl;

  return 0;

}

第5章:语句

5.4.1:if语句

#include

using namespace std;

int main()

{

  int grade;

  cin >> grade;

  if (grade > 90)

    cout << "high pass" << endl;

  else if (grade > 75)

    cout << "pass" << endl;

  else if (grade > 60)

    cout << "low pass" << endl;

  else

    cout << "fail" << endl;

  return 0;

}

5.5.1:for语句

#include

using namespace std;

int main()

{

  int sum = 0;

  for (int i = 1; i <= 10; ++i)

    sum += i;

  cout << "Sum of 1 to 10 inclusive is "

     << sum << endl;

  return 0;

}

5.6.1:while语句

#include

using namespace std;

int main()

{

  int i;

  while (cin >> i)

    cout << i << endl;

  return 0;

}

第6章:函数

6.1:函数基础

(a) 合法

(b) 非法,函数定义缺少大括号

(c) 合法

(d) 非法,缺少函数名

6.3:参数传递

(a) 形参不会被改变

(b) 形参还是不会被改变

(c) 形参会被改变

(d) 形参不会被改变

6.4.1:局部对象

控制传参和返回值

6.5:递归

#include

using namespace std;

int fact(int i)

{

  if (i == 1)

    return 1;

  else

    return i * fact(i - 1);

}

int main()

{

  int i;

  cin >> i;

  cout << "The factorial of " << i << " is " << fact(i) << endl;

  return 0;

}

第7章:类

7.1:定义抽象数据类型

#include

#include

using namespace std;

struct Sales_data

  string bookNo;

  unsigned units_sold = 0;

  double revenue = 0.0;

;

int main()

{

  Sales_data total;

  if (cin >> total.bookNo >> total.units_sold >> total.revenue) {

    Sales_data trans;

    while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue) {

      if (total.bookNo == trans.bookNo) {

        total.units_sold += trans.units_sold;

        total.revenue += trans.revenue;

      }

      else

        cout << total.bookNo << " " << total.units_sold << " "

           << total.revenue << endl;

        total = trans;

    }

    cout << total.bookNo << " " << total.units_sold << " "

       << total.revenue << endl;

  }

  else {

    cerr << "No data?!" << endl;

    return -1;

  }

  return 0;

}

7.2.1:定义类Data

#include

#include

using namespace std;

struct Date month;

int main()

  Date d;

  cin >> d.year >> d.month >> d.day;

  cout << d.year << '/' << d.month << '/' << d.day << endl;

  return 0;

第8章:IO库

8.1:IO类

(a) istream类

(b) ostream类

(c) iostream类

8.2:文件输入和输出

#include

#include

#include

#include

using namespace std;

int main()

{

  vector lines;

  ifstream input("input.txt");

  string line;

  while (getline(input, line))

    lines.push_back(line);

  input.close();

  ofstream output("output.txt");

  for (const auto &s : lines)

    output << s << endl;

  output.close();

  return 0;

}

第9章:顺序容器

9.1.1:vector练习

#include

#include

using namespace std;

int main()

{

  vector v1; //空vector

  vector v2(10); //10个元素,每个元素都为0

  vector v3(10,42); //10个元素,每个元素都为42

  vector v4{ 10 }; //一个元素,值为10

  vector v5 10; //两个元素,值分别为10和42

  vector v6{ 10 }; //10个元素,每个元素都为空串

  vector v7"hi" ; //10个元素,每个元素都为字符串"hi"

  return 0;

}

9.2.1:list练习

(a) 使用front()和back()函数操作

(b) 使用begin()和end()函数操作

9.3.1:迭代器

auto iter = vi.begin();

while (iter != vi.end()) {

  if (*iter % 2)

    iter = vi.insert(iter, *iter);

  ++iter;

}

第10章:泛型算法

10.1:泛型算法

(a) 由于使用了相等比较运算符,所以只能用于相同类型的容器

(b) 该函数复制每个元素到目标容器中

10.3.1:使用sort排序vector

#include

#include

#include

using namespace std;

int main()

{

  vector v{ 1,6,3,8,2,4,9 };

  sort(v.begin(), v.end());

  for (const auto &i : v)

    cout << i << " ";

  cout << endl;

  return 0;

}

10.4:unique重排容器

#include

#include

#include

using namespace std;

void print(const vector &v)

{

  for (const auto &i : v)

    cout << i << " ";

  cout << endl;

}

int main()

{

  vector v{ 1,2,2,3,3,3,4,4,4,4,5,5,5,5,5 };

  print(v);

  auto end_unique = unique(v.begin(), v.end());

  print(v);

  v.erase(end_unique, v.end());

  print(v);

  return 0;

}

第11章:关联容器

11.1:使用map

#include

#include

#include

using namespace std;

int main()

{

  map m;

  string word;

  while (cin >> word)

    ++m[word];

  for (const auto &w : m)

    cout << w.first << " occurs " << w.second

       << ((w.second > 1) ? " times" : " time") << endl;

  return 0;

}

11.2:使用set

#include

#include

#include

#include

using namespace std;

void print(const vector &v)

{

  for (const auto &i : v)

    cout << i << " ";

  cout << endl;

}

int main()

{

  vector v{ 1,2,3,4,5,5,5,6,7,7,8,9 };

  sort(v.begin(), v.end());

  auto end_unique = unique(v.begin(), v.end());

  v.erase(end_unique, v.end());

  print(v);

  set s(v.begin(), v.end());

  cout << s.size() << endl;

  return 0;

}

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复