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++】类的默认成员函数
|
15天前
|
编译器 C++ 容器
【C++】String常见函数用法
【C++】String常见函数用法
12 1
|
22天前
|
C++
c++常见函数及技巧
C++编程中的一些常见函数和技巧,包括生成随机数的方法、制表技巧、获取数字的个位、十位、百位数的方法、字符串命名技巧、避免代码修改错误的技巧、暂停和等待用户信号的技巧、清屏命令、以及避免编译错误和逻辑错误的建议。
17 6
|
22天前
|
存储 C++
c++学习笔记05 函数
C++函数使用的详细学习笔记05,包括函数的基本格式、值传递、函数声明、以及如何在不同文件中组织函数代码的示例和技巧。
26 0
c++学习笔记05 函数
|
2月前
|
C++ 运维
开发与运维函数问题之析构函数在C++类中起什么作用如何解决
开发与运维函数问题之析构函数在C++类中起什么作用如何解决
34 11
|
22天前
|
存储 C++
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
【C/C++学习笔记】string 类型的输入操作符和 getline 函数分别如何处理空白字符
29 0
|
27天前
|
Dart 编译器 API
Dart ffi 使用问题之在C++线程中无法直接调用Dart函数的问题如何解决
Dart ffi 使用问题之在C++线程中无法直接调用Dart函数的问题如何解决
|
1月前
|
JavaScript C++
【C++ visual studio】解决VS报错:error C2447: “{”: 缺少函数标题(是否是老式的形式表?)【亲测有效,无效捶我】
【C++ visual studio】解决VS报错:error C2447: “{”: 缺少函数标题(是否是老式的形式表?)【亲测有效,无效捶我】
|
2月前
|
Rust 编译器 测试技术
Rust与C++的区别及使用问题之Rust中函数参数传递的问题如何解决
Rust与C++的区别及使用问题之Rust中函数参数传递的问题如何解决
|
2月前
|
Java C++ 运维
开发与运维函数问题之C++中有哪些继承方式如何解决
开发与运维函数问题之C++中有哪些继承方式如何解决
21 0