c++primer 第八章函数编程答案

ops/2024/9/22 20:28:32/

题一

#include <iostream>
using namespace std;void print(char *str, int n = 0);int main()
{char str[20] = "leonardo liu";print(str);print(str, 5);print(str, 16);return 0;
}void print(char *str, int n)
{static int flag = 0; // 唯一初始化flag++;if (n == 0)cout << str << endl;else{for (int i = 0; i < flag; i++)cout << str << endl;}cout << endl;return;
}

 

题二

#include <iostream>
using namespace std;
struct CandyBar
{char *name;double weight;int calorie;
};
void Show(CandyBar &candy, char *p = "Millennium, Munch", double a = 2.85, int b = 350);
int main()
{CandyBar can1;Show(can1);cout << can1.calorie << endl;return 0;
}
void Show(CandyBar &candy, char *p, double a, int b)
{candy.name = p;candy.weight = a;candy.calorie = b;cout << candy.name << endl;cout << candy.weight << endl;cout << candy.calorie << endl;
}

题三

#include <iostream>
#include <cctype>
using namespace std;
void upper(string &str);
int main()
{string str1;cout << "Enter a string (q to quit):";while (str1 != "q") // string 类型可以直接比较{upper(str1);cout << "Next string (q to quit): ";getline(cin, str1);}cout << "Bye.";return 0;
}
void upper(string &str)
{for (int i = 0; str[i]; i++)str[i] = toupper(str[i]);cout << str << endl;
}

题四

#include <iostream>
using namespace std;
#include <cstring>
struct stringy
{char *str; // string pointerint ct;    // length of string (not counting '\0')
};
void set(stringy &rs, char a[]);
void show(stringy s, int a = 1);
void show(string str, int a = 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 &rs, char a[])
{strcpy(rs.str, a);rs.ct = strlen(a);
}
void show(stringy s, int a)
{for (int i = 0; i < a; i++){cout << "Str: " << s.str << endl;cout << "length: " << s.ct << endl;}
}
void show(string str, int a)
{for (int i = 0; i < a; i++){cout << "Str: " << str << endl;}
}

题五

#include <iostream>
using namespace std;
const int SIZE = 5;
template <class Any>
Any max(Any a[]);
int main()
{int int_arr[SIZE];double double_arr[SIZE];cout << "Enter five int numbers: ";for (int i = 0; i < SIZE; i++){cin >> int_arr[i];}cout << "Enter five double numbers: ";for (int i = 0; i < SIZE; i++){cin >> double_arr[i];}cout << "The maximum of int array is " << max(int_arr) << endl;cout << "The maximum of int array is " << max(double_arr) << endl;cout << "Done!";return 0;
}
template <class Any>
Any max(Any a[])
{Any temp = a[0];for (int i = 1; i < SIZE; i++){a[i] > temp ? temp = a[i] : temp = temp;}return temp;
}

题六

#include <iostream>
#include <cstring>
using namespace std;
template <class Any>
Any maxn(Any a[], int n);
template <>
const char *maxn<const char *>(const char *arr[], int n);
int main()
{int int_arr[6] = {1, 2, 3, 4, 5, 6};double double_arr[4] = {1.1, 2.2, 3.3, 1.5};const char *char_arr[5] = {"one", "two", "three", "four", "five"};cout << "The maximum of int array is " << maxn(int_arr, 6) << endl;cout << "The maximum of int array is " << maxn(double_arr, 4) << endl;cout << "The maximum of char array is " << maxn(char_arr, 5) << endl;cout << "The address of the longest string is: " << (void *)maxn(char_arr, 5) << endl; //return 0;
}
template <class Any>
Any maxn(Any a[], int n)
{Any temp = a[0];for (int i = 1; i < n; i++){a[i] > temp ? temp = a[i] : temp = temp;}return temp;
}
template <>
const char *maxn(const char *arr[], int n)
{const char *temp = arr[0];for (int i = 1; i < n; i++){strlen(arr[i]) > strlen(temp) ? temp = arr[i] : temp = temp;}return temp;
}

题七

#include <iostream>
template <typename T>
T SumArray(T arr[], int n);template <typename T>
T SumArray(T *arr[], int n);
using namespace std;
struct debts
{char name[50];double amount;
};
int main(void)
{int things[6] = {13, 31, 103, 301, 310, 130};struct debts mr_R[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_R[i].amount;cout << "Listing Mr. R's counts of things: \n"<< SumArray(things, 6) << endl;cout << "Listing Mr R's debts: \n"<< SumArray(pd, 3) << endl;return 0;
}
template <typename T>
T SumArray(T arr[], int n)
{using namespace std;T temp;// cout << "template A\n";for (int i = 0; i < n; i++)temp += arr[i];return temp;
}template <typename T>
T SumArray(T *arr[], int n)
{// cout << "template B\n";T temp;for (int i = 0; i < n; i++)temp += *arr[i];return temp;
}


http://www.ppmy.cn/ops/114408.html

相关文章

HTTPS是如何保证安全传输的

我们都知道https是保证安全传输的&#xff0c;那么究竟是如何保证的呢&#xff1f; 答&#xff1a;通过使⽤对称加密、⾮对称加密、数字证书等⽅式来保证数据的安全传输。 下面&#xff0c;就让我们来详细了解一下&#xff0c;具体是如何做的&#xff1a; 客户端向服务端发送数…

HTTP代理PAC脚本:自动化代理设置的利器

在现代互联网的海洋里&#xff0c;代理IP就像一艘神奇的船&#xff0c;带领我们穿越层层网络波涛。今天&#xff0c;我们要聊聊HTTP代理和PAC脚本这两位“船长”&#xff0c;看他们如何在网络世界里大显身手。 什么是HTTP代理&#xff1f; HTTP代理是一个中间服务器&#xff0…

LeetCode 滑动窗口 滑动子数组的美丽值

滑动子数组的美丽值 给你一个长度为 n 的整数数组 nums &#xff0c;请你求出每个长度为 k 的子数组的 美丽值 。 一个子数组的 美丽值 定义为&#xff1a;如果子数组中第 x 小整数 是 负数 &#xff0c;那么美丽值为第 x 小的数&#xff0c;否则美丽值为 0 。 请你返回一个包含…

Python 二级考试

易错点 电脑基础知识 定义学生关系模式如下&#xff1a;Student &#xff08;S#&#xff0c; Sn&#xff0c; Ssex&#xff0c;class&#xff0c;monitorS#&#xff09;&#xff08;其属性分别为学号、学生名、性别、班级和班长学号&#xff09; 在关系模式中&#xff0c;如果…

Vue3中shallowRef和ref区别

在 Vue 3 中&#xff0c;ref 和 shallowRef 都是用来创建响应式引用的工具&#xff0c;但它们之间存在一些关键的区别&#xff0c;主要在于它们如何处理对象的响应式特性。 ref ref 创建的是一个完整的响应式引用&#xff0c;它不仅使基础值成为响应式的&#xff0c;而且当值…

大学生涯规划

记得大学刚开学老师布置的作业就是写自己的职业生涯规划&#xff0c;但是直到大学毕业我也不知道有什么规划&#xff0c;很迷茫&#xff0c;虽然经常去图书馆看书&#xff0c;前端、后端、C#、JAVA等东一棍西一棒&#xff0c;但是实际上实战经验很少&#xff0c;知识基本也没运…

食品检测与分类系统源码分享

食品检测与分类检测系统源码分享 [一条龙教学YOLOV8标注好的数据集一键训练_70全套改进创新点发刊_Web前端展示] 1.研究背景与意义 项目参考AAAI Association for the Advancement of Artificial Intelligence 项目来源AACV Association for the Advancement of Computer V…

在Python中快速获取HTML中<span>标签的内容

在Python中&#xff0c;要获取HTML中<span>标签的内容&#xff0c;通常我们会使用像BeautifulSoup这样的库&#xff0c;它提供了方便的方法来解析HTML和XML文件&#xff0c;并从中提取数据。下面是一个使用BeautifulSoup来获取<span>标签内容的简单示例。 首先&am…