C++ Primer Plus 第七章答案 函数——C++的编程模块

简介: 只有聪明人才能看见的摘要~( ̄▽ ̄~)~

 老样子

复习题+编程练习

//7.12
//1
  定义函数,提供原型,调用函数
//2
  void igor();
  float tofu(int);
  double mpg(double, double);
  long sumation(long[], int n);
  double doctor(const char[]);
  void ofcourse(boss);
  char* plot(map*);
//3
  void becint(int arr[], int n, int value)
  {
    for (int i = 0; i < n; i++)
      arr[i] = value;
  }
//4
  void becint(int* begin, int* end, int value)
  {
    int* pt;
    for (pt = begin; pt != end; pt++)
      *pt = value;
  }
//5
  double returnmax(const double arr[], int size)
  {
    if (size < 1)
    {
      std::cout << "Invalid size of array:" << size << endl;
      std::cout << "Returning a value of 0\n";
      return 0;
    }
    else
    {
      double max;
      max = arr[0];
      for (int i = 0; i < size; i++)
        if(arr[i] > max)
          max = arr[i];
      return max;
    }
  }
//6
  将const限定符用于指针,可以防止原始数据被修改。但是当程序传递基本类型时,将按值传递
  以便函数使用副本。这样原始数据将得到保护
//7
  字符串可以存储在char数组中
  可以用带双引号的字符串表示
  可以用指向字符串第一个字符的指针表示
//8
  int replace(char* str, char c1, char c2)
  {
    int num = 0;
    while (str != '\0')
    {
      if (str == c1)
      {
        str = c2;
        num++;
      }
      str++;
    }
    return num;
  }
//9
  字符串解释为第一个元素的地址,因此第一个表示p的地址,第二个表示c的地址
  换句话说,字符串常量和数组名的行为相同
//10
  按值传递直接传递结构名glitz,按地址传递要使用地址运算符&glitz
  按值传递自动保护原始数据,但是要以时间和内存为代价
  按地址传递可以节省时间和内存,但不能保护原始数据除非使用const限定符
  按值传递可以使用常规的结构成员表示法,按地址必须使用间接成员运算符
//11
  int judge(int (*pf)(const char*));
//12
  void fa(applicant a)
  {
    std::cout << a.name << endl;
    for (int i = 0; i < 3; i++)
      std::cout << a.credit_ratings[i] << endl;
  }
  void fb(const applicant* pa)
  {
    std::cout << pa->name << endl;
    for (int i = 0; i < 3; i++)
      std::cout << pa->credit_ratings[i] << endl;
  }
//13
  typedef void (*p_f1)(applicant* a);
  p_f1 p1 = f1;
  typedef const char* (*p_f2)(const applicant* a1, const applicant* a2);
  p_f2 p2 = f2;
  p_f1 ap[5];
  p_f2(*pa)[10];             //pa是指向(包含10个p_f2类型指针的数组)的指针
//end

image.gif

唉,实在太太太太长了,复制都麻烦

Shaift+Alt选取竖行,因为都要主函数,又不能一个题建一个项目太麻烦了,所以批量注释掉了,不过你们选取竖行就很好复制了,也可以删了注释线再复制

////practice 1
//#include<iostream>
//using namespace std;
//
//double tiaohe(double a, double b);
//
//int main()
//{
//  double a, b;
//  cout << "Enter two numbers(quit by 0): ";
//  cin >> a >> b;
//  while (a != 0 && b != 0)
//  {
//    double th = tiaohe(a, b);
//    cout << "Their harmonic mean is: " << th << endl;
//    cout << "Enter two more numbers: ";
//    cin >> a >> b;
//  }
//  cout << "Done!";
//
//  return 0;
//}
//
//double tiaohe(double a, double b)
//{
//  return 2 * a * b / (a + b);
//}
////practice 2
//#include<iostream>
//using namespace std;
//
//const int SIZE = 10;
//
//int input(double*, int size);
//void display(double*, int size);
//void ave(double*, int size);
//
//int main()
//{
//  double arr[SIZE];
//  int size = input(arr, SIZE);
//  display(arr, size);
//  ave(arr, size);
//
//  return 0;
//}
//
//int input(double* arr, int size)
//{
//  int i = 0;
//  for (i; i < size; i++)
//  {
//    double score;
//    cout << "Enter the grade(quit by minus): ";
//    cin >> score;
//    if (!cin)                                             //if不是循环,break直接跳出整个for循环
//    {
//      cin.clear();
//      while (cin.get() != '\n')
//        continue;
//      cout << "Error!\n";
//      break;
//    }
//    else if (score < 0)
//      break;
//    arr[i] = score;
//  }
//  return i;
//}
//
//void display(double* arr, int size)
//{
//  for (int i = 0; i < size; i++)
//    cout << arr[i] << "  ";
//}
//
//void ave(double*arr, int size)
//{
//  double sum=0;
//  for (int i = 0; i < size; i++)
//    sum += arr[i];
//  cout << "The average is: " << sum / size;
//}
////practice 3
//#include<iostream>
//
//using namespace std;
//
//struct box
//{
//  char maker[40];
//  float height;
//  float width;
//  float length;
//  float volume;
//};
//
//void display(const box b)
//{
//  cout << b.maker << endl;
//  cout << b.height << endl;
//  cout << b.width << endl;
//  cout << b.length << endl;
//  cout << b.volume << endl;
//}
//
//void volume(box* pb)
//{
//  pb->volume = pb->height * pb->width * pb->length;
//}
//
//int main()
//{
//  box bbb = { "yuetian",10,10,5 };
//  display(bbb);
//  volume(&bbb);
//  cout << "The volume of yuetian's box is: " << bbb.volume;
// 
//  return 0;
//}
////practice 4
//#include<iostream>
//using namespace std;
//
//long double probability(unsigned numbers, unsigned picks);
//
//int main()
//{
//  unsigned total, choices;
//  cout << "Enter the total number of choices on the game card and\n"
//    "the number of picks allowed:\n";
//
//  int i = 0;
//  long double arr[2] = {};
//  while (i < 2 && (cin >> total >> choices) && choices < total)
//    //i<2的条件要放在前面,否则先判断输入,必须输入值才会继续判断,才会输出总概率
//  {
//    cout << "You have one change in ";
//    arr[i] = probability(total, choices);
//    cout << arr[i];
//    cout << " of winning.\n";
//    i++;
//  }
//  cout << "The final winning probability is: " << arr[0] * arr[1] << endl;
//  cout << "Bye!";
//  return 0;
//}
//
//long double probability(unsigned numbers, unsigned picks)
//{
//  long double result = 1;
//  long double n;
//  unsigned p;
//  for (n = numbers, p = picks; p > 0; n--, p--)
//    result = result * n / p;
//
//  return result;
//}
////practice 5
//#include<iostream>
//
//using namespace std;
//
//unsigned long f(unsigned n)
//{
//  long int factorial = 0;
//  if (n == 0 || n == 1)
//    factorial = 1;
//  else
//    factorial = n * f(n - 1);
//  return factorial;
//}
//
//int main()
//{
//  unsigned num = 0;
//  cout << "Enter a number to calculate its factorial(q to quit): ";
//  while (1)
//  {
//    if (!(cin >> num))
//    {
//      cin.clear();
//      while (cin.get() != '\n')
//        continue;
//      cout << "Error!";
//      break;
//    }
//    else
//      cout << "The factorial is: " << f(num) << endl;
//  }
//  return 0;
//}
////practice 6
//#include<iostream>
//
//using namespace std;
//
//int Fill_array(double* arr, int size)
//{
//  int i = 0;
//  for (i; i < size; i++)
//  {
//    cout << "Enter a double value(char to end early): ";
//    if (cin >> arr[i]);
//    else
//    {
//      cin.clear();
//      while (cin.get() != '\n')
//        continue;
//      cout << "Error!";
//      break;
//    }
//  }
//  return i;
//}
//
//void Show_array(double* arr, int size)
//{
//  for (int i = 0; i < size; i++)
//  {
//    cout << arr[i] << "  ";
//  }
//  cout << endl;
//}
//
//void Reverse_array(double* arr, int size)
//{
//  double substitute = 0;
//  for (int i = 0; i < size/2; i++)
//  {
//    substitute = arr[i];
//    arr[i] = arr[size - i - 1];
//    arr[size - i - 1] = substitute;
//  }
//}
//
//int main()
//{
//  double arr[10];
//  int size = Fill_array(arr, 10);
//  Show_array(arr, size);
//
//  Reverse_array(arr, size);
//  Show_array(arr, size);
//
//  Reverse_array(arr+1, size-2);
//  Show_array(arr, size);
//
//  return 0;
//}
////practice 7
//#include<iostream>
//
//const int MAX = 5;
//
//double* fill_array(double arr[], double arrend[]);
//void show_array(const double arr[], const double arrend[]);
//void revalue_array(double r, double arr[], double arrend[]);
//
//using namespace std;
//
//int main()
//{
//  double properties[MAX];
//
//  double* end = fill_array(properties, properties + MAX);
//  show_array(properties, end);
//  if ((end - properties) > 0)
//  {
//    cout << "Enter revaluation factor: ";
//    double factor;
//    while (!(cin >> factor))
//    {
//      cin.clear();
//      while (cin.get() != '\n')
//        continue;
//      cout << "Bad input;pleas enter a number: ";
//    }
//    revalue_array(factor, properties, end);
//    show_array(properties, end);
//  }
//  cout << "Done!\n";
////  cin.get();
////  cin.get();
//  return 0;
//}
//
//double* fill_array(double arr[], double arrend[])
//{
//  double temp;
//  int i;
//  for (i = 0; i < arrend - arr; i++)
//  {
//    cout << "Enter value #" << i + 1 << ": ";
//    cin >> temp;
//    if (!cin)
//    {
//      cin.clear();
//      while (cin.get() != '\n')
//        continue;
//      cout << "Bad input;input process terminated.\n";
//      break;
//    }
//    else if (temp < 0)
//      break;
//    arr[i] = temp;
//  }
//  return arr + i;
//}
//
//void show_array(const double arr[], const double arrend[])
//{
//  for (int i = 0; i < arrend - arr; i++)
//  {
//    cout << "Property#" << i + 1 << ": $";
//    cout << arr[i] << endl;
//  }
//}
//
//void revalue_array(double r, double arr[], double arrend[])
//{
//  for (int i = 0; i < arrend - arr; i++)
//    arr[i] *= r;
//}
////practice 8
//#include<iostream>
//using namespace std;
//
//const int Seasons = 4;
//const char* Snames[4] =
//{ "Spring","Summer","Fall","Winter" };
//
//void fill1(double* expenses, int size);
//void show1(double* expenses, int size);
//
//struct expenses
//{
//  double ex[Seasons];
//};
//
//void fill2(expenses* pex);
//void show2(expenses* pex);
//
////int main()
////{
////  double expenses[Seasons];
////  fill1(expenses, Seasons);
////  show1(expenses, Seasons);
////  return 0;
////}
//
//int main()
//{
//  expenses ex;
//  fill2(&ex);
//  show2(&ex);
//  return 0;
//}
//
//void fill1(double* expenses, int size)
//{
//  for (int i = 0; i < size; i++)
//  {
//    cout << "Enter " << Snames[i] << " expenses: ";
//    cin >> expenses[i];
//  }
//}
//
//void show1(double* expenses, int size)
//{
//  double total = 0;
//  cout << "EXPENSES\n";
//  for (int i = 0; i < size; i++)
//  {
//    cout << Snames[i] << " $: " << expenses[i] << endl;
//    total += expenses[i];
//  }
//  cout << "Total Expenses :$" << total << endl;
//}
//
//
//void fill2(expenses* pex)
//{
//  for (int i = 0; i < Seasons; i++)
//  {
//    cout << "Enter " << Snames[i] << " expenses: ";
//    cin >> pex->ex[i];
//  }
//}
//
//void show2(expenses* pex)
//{
//  double total = 0;
//  cout << "EXPENSES\n";
//  for (int i = 0; i < Seasons; i++)
//  {
//    cout << Snames[i] << " $: " << pex->ex[i] << endl;
//    total += pex->ex[i];
//  }
//  cout << "Total Expenses :$" << total << endl;
//}
////practice 9
//#include<iostream>
//using namespace std;
//const int Slen = 30;
//struct student
//{
//  char fullname[Slen];
//  char hobby[Slen];
//  int ooplevel;
//};
//
//int getinfo(student pa[], int n)
//{
//  int i;
//  cout << "Enter the data about student:\n";
//
//  for (i = 0; i < n; i++)
//  {
//    cout << "The fullname: ";
//    cin.getline(pa[i].fullname, Slen);
//    cout << "The hobby: ";
//    cin.getline(pa[i].hobby, Slen);
//    cout << "The ooplevel: ";
//    cin >> pa[i].ooplevel;
//    cin.get();
//  }
//  return i;
//}
//
//void display1(student st)
//{
//  cout << "The student's fullname: " << st.fullname << endl;
//  cout << "The hobby: " << st.hobby << endl;
//  cout << "The ooplevel: " << st.ooplevel << endl;
//}
//
//void display2(const student* ps)
//{
//  cout << "The student's fullname: " << ps->fullname << endl;
//  cout << "The hobby: " <<ps->hobby << endl;
//  cout << "The ooplevel: " << ps->ooplevel << endl;
//}
//
//void display3(const student pa[], int n)
//{
//  for (int i = 0; i < n; i++)
//  {
//    cout << "The "<<i+1<<" student's fullname: " << pa[i].fullname << endl;
//    cout << "The hobby: " << pa[i].hobby << endl;
//    cout << "The ooplevel: " << pa[i].ooplevel << endl;
//  }
//}
//
//int main()
//{
//  cout << "Enter class size: ";
//  int class_size;
//  cin >> class_size;
//  while (cin.get() != '\n')
//    continue;
//
//  student* ptr_stu = new student[class_size];
//  int entered = getinfo(ptr_stu, class_size);
//  for (int i = 0; i < entered; i++)
//  {
//    display1(ptr_stu[i]);
//    display2(&ptr_stu[i]);
//  }
//  display3(ptr_stu, entered);
//  delete[] ptr_stu;
//  cout << "Done!\n";
//  return 0;
//}
//practice 10
#include<iostream>
using namespace std;
double add(double a, double b)
{
  return a + b;
}
double substract(double a, double b)
{
  return a - b;
}
double multiply(double a, double b)
{
  return a * b;
}
double calculate(double a, double b, double(*pf)(double, double))
{
  return pf(a, b);
}
int main()
{
  while (1)
  {
    double a = 0, b = 0;
    cout << "Enter two number to chalculate(quit by char): ";
    cin >> a >> b;
    if (!cin)
    {
      cin.clear();
      while (cin.get() != '\n')
        continue;
      cout << "Quit!\n";
      break;
    }
    cout << "The result 1 is: " << calculate(a, b, add) << endl;
    cout << "The result 2 is: " << calculate(a, b, substract) << endl;
    cout << "The result 3 is: " << calculate(a, b, multiply) << endl;
  }
  return 0;
}

image.gif


相关文章
|
5月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
144 0
|
9月前
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
8月前
|
安全 C++
【c++】继承(继承的定义格式、赋值兼容转换、多继承、派生类默认成员函数规则、继承与友元、继承与静态成员)
本文深入探讨了C++中的继承机制,作为面向对象编程(OOP)的核心特性之一。继承通过允许派生类扩展基类的属性和方法,极大促进了代码复用,增强了代码的可维护性和可扩展性。文章详细介绍了继承的基本概念、定义格式、继承方式(public、protected、private)、赋值兼容转换、作用域问题、默认成员函数规则、继承与友元、静态成员、多继承及菱形继承问题,并对比了继承与组合的优缺点。最后总结指出,虽然继承提高了代码灵活性和复用率,但也带来了耦合度高的问题,建议在“has-a”和“is-a”关系同时存在时优先使用组合。
445 6
|
9月前
|
存储 机器学习/深度学习 编译器
【C++终极篇】C++11:编程新纪元的神秘力量揭秘
【C++终极篇】C++11:编程新纪元的神秘力量揭秘
|
9月前
|
存储 算法 C++
深入浅出 C++ STL:解锁高效编程的秘密武器
C++ 标准模板库(STL)是现代 C++ 的核心部分之一,为开发者提供了丰富的预定义数据结构和算法,极大地提升了编程效率和代码的可读性。理解和掌握 STL 对于 C++ 开发者来说至关重要。以下是对 STL 的详细介绍,涵盖其基础知识、发展历史、核心组件、重要性和学习方法。
|
9月前
|
存储 安全 算法
深入理解C++模板编程:从基础到进阶
在C++编程中,模板是实现泛型编程的关键工具。模板使得代码能够适用于不同的数据类型,极大地提升了代码复用性、灵活性和可维护性。本文将深入探讨模板编程的基础知识,包括函数模板和类模板的定义、使用、以及它们的实例化和匹配规则。
|
消息中间件 存储 安全
|
9月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
5月前
|
存储 编译器 程序员
c++的类(附含explicit关键字,友元,内部类)
本文介绍了C++中类的核心概念与用法,涵盖封装、继承、多态三大特性。重点讲解了类的定义(`class`与`struct`)、访问限定符(`private`、`public`、`protected`)、类的作用域及成员函数的声明与定义分离。同时深入探讨了类的大小计算、`this`指针、默认成员函数(构造函数、析构函数、拷贝构造、赋值重载)以及运算符重载等内容。 文章还详细分析了`explicit`关键字的作用、静态成员(变量与函数)、友元(友元函数与友元类)的概念及其使用场景,并简要介绍了内部类的特性。
226 0
|
7月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
274 12