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


目录
相关文章
|
21天前
|
C++
C++ 语言异常处理实战:在编程潮流中坚守稳定,开启代码可靠之旅
【8月更文挑战第22天】C++的异常处理机制是确保程序稳定的关键特性。它允许程序在遇到错误时优雅地响应而非直接崩溃。通过`throw`抛出异常,并用`catch`捕获处理,可使程序控制流跳转至错误处理代码。例如,在进行除法运算或文件读取时,若发生除数为零或文件无法打开等错误,则可通过抛出异常并在调用处捕获来妥善处理这些情况。恰当使用异常处理能显著提升程序的健壮性和维护性。
38 2
|
21天前
|
算法 C语言 C++
C++语言学习指南:从新手到高手,一文带你领略系统编程的巅峰技艺!
【8月更文挑战第22天】C++由Bjarne Stroustrup于1985年创立,凭借卓越性能与灵活性,在系统编程、游戏开发等领域占据重要地位。它继承了C语言的高效性,并引入面向对象编程,使代码更模块化易管理。C++支持基本语法如变量声明与控制结构;通过`iostream`库实现输入输出;利用类与对象实现面向对象编程;提供模板增强代码复用性;具备异常处理机制确保程序健壮性;C++11引入现代化特性简化编程;标准模板库(STL)支持高效编程;多线程支持利用多核优势。虽然学习曲线陡峭,但掌握后可开启高性能编程大门。随着新标准如C++20的发展,C++持续演进,提供更多开发可能性。
41 0
|
12天前
|
Rust 安全 C++
系统编程的未来之战:Rust能否撼动C++的王座?
【8月更文挑战第31天】Rust与C++:现代系统编程的新选择。C++长期主导系统编程,但内存安全问题频发。Rust以安全性为核心,通过所有权和生命周期概念避免内存泄漏和野指针等问题。Rust在编译时确保内存安全,简化并发编程,其生态系统虽不及C++成熟,但发展迅速,为现代系统编程提供了新选择。未来有望看到更多Rust驱动的系统级应用。
35 1
|
15天前
|
编译器 C++ 容器
【C++】String常见函数用法
【C++】String常见函数用法
12 1
|
22天前
|
存储 C++
c++学习笔记05 函数
C++函数使用的详细学习笔记05,包括函数的基本格式、值传递、函数声明、以及如何在不同文件中组织函数代码的示例和技巧。
26 0
c++学习笔记05 函数
|
21天前
|
存储 编译器 C++
打破C++的神秘面纱:一步步带你走进面向未来的编程世界!
【8月更文挑战第22天】C++是一门功能强大但学习曲线陡峭的语言,提供高性能与底层控制。本文通过实例介绍C++基础语法,包括程序结构、数据类型、控制结构和函数。从简单的“Hello, C++!”程序开始,逐步探索变量声明、数据类型、循环与条件判断,以及函数定义与调用。这些核心概念为理解和编写C++程序打下坚实基础,引导你进入C++编程的世界。
31 0
|
22天前
|
存储 C++
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
29 0
|
29天前
|
C++ 容器
C++中自定义结构体或类作为关联容器的键
C++中自定义结构体或类作为关联容器的键
30 0
|
8天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
8天前
|
C++
C++(十六)类之间转化
在C++中,类之间的转换可以通过转换构造函数和操作符函数实现。转换构造函数是一种单参数构造函数,用于将其他类型转换为本类类型。为了防止不必要的隐式转换,可以使用`explicit`关键字来禁止这种自动转换。此外,还可以通过定义`operator`函数来进行类型转换,该函数无参数且无返回值。下面展示了如何使用这两种方式实现自定义类型的相互转换,并通过示例代码说明了`explicit`关键字的作用。