C++ Primer Plus 第八章答案 函数探幽

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

 复习题加编程练习

//8.7
//1
只有一行代码的小型,非递归函数适合作为内联函数
//2
void song(const char* name, int times = 1);
只有原型包含默认值的信息
void song(const char* name = "Oh,My Papa!", int times = 1);
//3
void iquote(int n)
{
  cout << '"' << n << '"';
}
void iquote(double n)
{
  cout << "\"" << n << "\"";
}
void iquote(string str) 
{
  cout << '"' << str << '"';
}
//4
void display(const box& box)
{
  cout << "maker = " << box.maker << endl;
}
void calculate(box& box)
{
  box.volume = box.height * box.width * box.length;
}
//5
先改原型
void fill(std::array<double, Seasons>& pa);
void show(std::array<double, Seasons>& da);
改调用
fill(expenses);
改fill函数
cin >> (*pa)[i]; 改成cin >> pa[i];
改show的函数头
void show(std::array<double, seasons>& da)
//6
默认参数和函数重载都可以完成
double mass(double density, double volume = 1);
double mass(double density, double volumn);
double mass(double density);
不能使用默认参数,因为必须从左到右指定参数, 可以用函数重载
void repeat(int times, const char* pstr);
void repeat(const char* pstr);
只能使用函数重载
int average(int a, int b);
double average(double x, double y);
不能这么做,因为两个版本特征标会相同             //没看懂题
//7
template <typename T>
T bigger(T a, T b)
{
  return(a > b ? a : b);
}
//8
template<>box bigger(const box& a, const box& b)
{
  return a.volume > b.volume ? a : b;
}
//9
float float& float& int double

image.gif

////practice 1
//#include<iostream>
//using namespace std;
//
//void Print(char* str, int n = 0);
//
//int main()
//{
//  char str[20];
//  cout << "输入一个字符串:";
//  cin >> str;
//  Print(str);
//  int n = 0;
//  cout << "再输入一个字符串和一个数字:";
//  cin >> str >> n;
//  Print(str, n);
//
//  return 0;
//}
//
//void Print(char* str, int n)
//{
//  if (n != 0)
//  {
//    Print(str, n - 1);
//  }
//  cout << "第" << n + 1 << "次调用Print: " << str << endl;
//}
////practice 2
//#include<iostream>
//#include<cstring>
//using namespace std;
//
//struct CandyBar
//{
//  char name[30];
//  double weight;
//  int calorie;
//};
//char name1[30] = "Millennium Munch";
//void setdata(CandyBar& candy, const char* name =name1,
//  const double weight = 2.85, const int calorie = 350);
//void display(const CandyBar& candy);
//
//int main()
//{
//  CandyBar candy1, candy2;
//  char name[30];
//  double weight = 0.0;
//  int calorie = 0;
//  cout << "输入第一个糖果的数据:";
//  cin.getline(name, 30);
//  cin >> weight >> calorie;
//  setdata(candy1, name, weight, calorie);
//  setdata(candy2);
//  cout << "两个糖果的信息显示:" << endl;
//  display(candy1);
//  display(candy2);
//  return 0;
//}
//
//void setdata(CandyBar& candy, const char* name, const double weight, const int calorie)
//{
//  strcpy_s(candy.name, name);     //candy.name = name1;地址不能赋给地址,用string或strcpy_s或strcpy)
//  candy.weight = weight;
//  candy.calorie = calorie;
//}
//
//void display(const CandyBar& candy)
//{
//  cout << "name=" << candy.name << endl;
//  cout << "weight=" << candy.weight << endl;
//  cout << "calorie=" << candy.calorie << endl;
//}
////practice 3
//#include<cctype>
//#include<iostream>
//#include<cstring>
//#include<string>
//
//using namespace std;
//
//string transform(string& str);
//
//int main()
//{
//  cout << "Enter a string (q to quit): ";
//  string str1;
//  while (getline(cin,str1))
//  {
//    if (str1[0] == 'q')
//    {
//      cout << "Bye!" << endl;
//      break;
//    }
//    else
//    {
//      str1 = transform(str1);
//      cout << str1 << endl;
//      cout << "Next string (q to quit): ";
//    }
//  }
//
//  return 0;
//}
//
//string transform(string& str)
//{
//  for (int i = 0; i < str.size(); i++)
//    str[i] = toupper(str[i]);
//  return str;
//}
/*
//practice 4
#pragma warning( disable : 4996)  
#include <iostream>
#include <cstring>
using namespace std;
struct stringy
{
  char* str;
  int ct;
};
void set(stringy& str, const char*);
void show(const stringy&, int n = 1);
void show(const char*, int n = 1);
int main()
{
  stringy beany;
  char testing[] = "Reality isn't what it used to be.";
  set(beany, testing);
  show(beany);
  show(beany, 2);
  testing[0] = 'D';
  testing[1] = 'u';
  show(testing);
  show(testing, 3);
  show("Done!");
  return 0;
}
void set(stringy& str, const char* t)
{
  char* copy_t= new char[strlen(t) + 1];      //strlen(str1)是计算字符串的长度,不包括字符串末尾的“\0”!!!
  //strcpy_s(str.str, t);                     //两个参数但如果:char *str=new char[7];会出错:提示不支持两个参数
  //链接https://blog.csdn.net/johnny710vip/article/details/6681160?ops_request_misc=&request_id=&biz_id=
  //102&utm_term=strcpy_s&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-8-.
  //pc_search_result_before_js&spm=1018.2226.3001.4187
  strcpy(copy_t, t);              //在include前加上#pragma warning( disable : 4996),否则会报错要求使用strcpy_s
  str.str = copy_t;
  str.ct = strlen(t) + 1;
}
void show(const stringy& str, int n)
{
  for (int i = 0; i < n; i++)
  {
    cout << str.str << endl;
  }
  cout << endl;
}
void show(const char* str, int n)
{
  for (int i = 0; i < n; i++)
  {
    cout << str << endl;
  }
  cout << endl;
}
*/
/*
//practice 5
#include<iostream>
using namespace std;
template <typename T>
T max(const T*);
int main()
{
  int one[5] = { 5,8,2,4,5 };
  double two[5] = { 2.5,8.9,99.2,5.1,5.5 };
  cout << max(one) << endl;
  cout << max(two) << endl;
  return 0;
}
template<typename T>
T max(const T* t)
{
  T temp = t[0];
  for (int i = 1; i < 5; i++)
    if (temp < t[i])
      temp = t[i];
  return temp;
}
*/
/*
//prcatice 6
#include<iostream>
#include<cstring>
using namespace std;
template<typename T>
T maxn(T*, int);
template <>char* maxn(char**, int);
int main()
{
  int test1[6] = { 1,3,8,6,4, };
  double test2[4] = { 1.1,2.2,5.5,9.9 };
  const char* test3[5] =
  {
    "yuetian",
    "yue tiann",
    "yeutianyues",
    "yue",
    "yuetianyues",
  };
  cout << "The max number of test1: " << maxn(test1, 6) << endl;
  cout << "The max number of test2: " << maxn(test2, 4) << endl;
  cout << "The longgest member's address: " <<(void*) maxn(test3, 4) << endl;
  //需要转换为void*指针,重载的操作符<<遇到地址会自动输出字符串
  cout << "The adderss is the pointer of: " << maxn(test3, 4) << endl;
  cout << "Done!" << endl;
  return 0;
}
template<typename T>
T maxn(T* array, int n)
{
  T temp = array[0];
  for (int i = 1; i < n; i++)
  {
    if (temp < array[i])
      temp = array[i];
  }
  return temp;
}
template <>char* maxn(char** str, int n)
{
  int i = 0;
  char* address=str[0];
  for (i = 1; i < n; i++)
  {
    if (strlen(address) < (strlen(str[i])))
      address = str[i];
  }
  return address;
}
*/
//practice 7
#include<iostream>
using namespace std;
template<typename T>
T SumArray(T arr[], int n);
template<typename T>
T SumArray(T* arr[], int n);
struct debts
{
  char name[50];
  double amount;
};
int main()
{
  int things[6] = { 13,31,103,310,130 };
  debts mr_E[3] =
  {
    {"Ima Wolfe",2400.0},
    {"Ura Foxe",1300.0},
    {"Iby Stout",1800.0}
  };
  double* pd[3];
  for (int i = 0; i < 3; i++)
    pd[i] = &mr_E[i].amount;
  cout << "The sum of things: " << SumArray(things, 6) << endl;
  cout << "The sum of amount of debt: " << SumArray(pd, 3) << endl;
  return 0;
}
template<typename T>
T SumArray(T arr[], int n)
{
  T temp = 0;
  for (int i = 0 ; i < n; i++)
    temp += arr[i];
  return temp;
}
template<typename T>
T SumArray(T* arr[], int n)
{
  T temp = 0;
  for (int i = 0; i < n; i++)
    temp += *arr[i];
  return temp;
}

image.gif


目录
相关文章
|
29天前
|
C++
C++ 数学函数、头文件及布尔类型详解
C++ 支持数学操作,如`max`和`min`函数找最大值和最小值,以及`&lt;cmath&gt;`库中的`sqrt`、`round`等数学函数。`bool`类型用于布尔逻辑,取值`true`(1)或`false`(0)。布尔表达式结合比较运算符常用于条件判断,例如在`if`语句中检查年龄是否达到投票年龄。在代码示例中,`isCodingFun`和`isFishTasty`变量分别输出1和0。
121 1
|
29天前
|
算法 C++ 容器
C++中模板函数以及类模板的示例(template)
C++中模板函数以及类模板的示例(template)
|
2月前
|
存储 设计模式 安全
【C++ 软件设计思路】多角度探索C++事件处理:以‘handlePowerEvent’函数为例
【C++ 软件设计思路】多角度探索C++事件处理:以‘handlePowerEvent’函数为例
85 2
|
2天前
|
编译器 C++
【C++进阶】引用 & 函数提高
【C++进阶】引用 & 函数提高
|
6天前
|
C++
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
|
6天前
|
存储 C++
C++从入门到精通:2.1.1函数和类
C++从入门到精通:2.1.1函数和类
|
14天前
|
机器学习/深度学习 定位技术 C++
c++中常用库函数
c++中常用库函数
38 0
|
15天前
|
算法 搜索推荐 C++
浅谈sort函数底层(一道c++面试的天坑题)
浅谈sort函数底层(一道c++面试的天坑题)
|
18天前
|
编译器 C++
C++ 解引用与函数基础:内存地址、调用方法及声明
C++ 中的解引用允许通过指针访问变量值。使用 `*` 运算符可解引用指针并修改原始变量。注意确保指针有效且不为空,以防止程序崩溃。函数是封装代码的单元,用于执行特定任务。理解函数的声明、定义、参数和返回值是关键。函数重载允许同一名称但不同参数列表的函数存在。关注公众号 `Let us Coding` 获取更多内容。
135 1
|
18天前
|
编译器 C语言 C++
【C++初阶(九)】C++模版(初阶)----函数模版与类模版
【C++初阶(九)】C++模版(初阶)----函数模版与类模版
20 0