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

embedded/2024/11/15 4:01: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/embedded/115244.html

相关文章

【编程基础知识】MySQL中什么叫做聚簇索引、非聚簇索引、回表、覆盖索引

在MySQL中&#xff0c;聚簇索引&#xff08;Clustered Index&#xff09;和非聚簇索引&#xff08;Non-Clustered Index&#xff09;是两种不同类型的索引&#xff0c;它们在数据存储和查询性能上有不同的特点&#xff1a; 聚簇索引&#xff08;Clustered Index&#xff09; 聚…

【Linux】Linux进程的概念

一、冯诺依曼体系结构 我们常见的计算机&#xff0c;比如笔记本&#xff0c;我们不常见的计算机&#xff0c;比如服务器&#xff0c;大部分都遵循冯诺依曼体系结构。 截至目前&#xff0c;我们所认识的计算机&#xff0c;都是有一个一个独立的硬件组成&#xff1a; 输入单元&a…

Java安全(加密+HTTPS+WEB安全)

Java加密 单向加密 接收一段明文&#xff0c;然后以一种不可逆的方式将它转换成一段密文 ①、MD5&#xff0c;将无论多长的数据最后编码128位数据&#xff0c;常用文件校验、密码加密、散列数据 byte[] data ...;//明文数据 MessageDigest md5 MessageDigest.getInstance…

数据结构|二叉搜索树

&#x1f36c; mooridy-CSDN博客 &#x1f36c;数据结构专栏&#xff08;更新中&#xff09; 目录 1. ⼆叉搜索树的概念 2. ⼆叉搜索树的性能分析 3.⼆叉搜索树key和key/value key搜索场景 key/value搜索场景 4. 二叉搜索树的代码实现 4.1 ⼆叉搜索树的插⼊ 4.2 ⼆叉搜索…

GBase8a 开机自启动

1、/etc/rc.local 方式 echo su - gbase -c "gcware_services all restart;gcluster_services all restart" >> /etc/rc.local chmod x /etc/rc.local2、/etc/init.d 启动服务&#xff08;测试于centos7&#xff09;【对于953&#xff0c;可以拆分成两个服务G…

Spring 全家桶使用教程

Spring 全家桶(Spring Framework)是 Java 开发中非常流行的框架,它的模块化设计为开发者提供了灵活、可扩展的解决方案。Spring 全家桶包括 Spring Core、Spring MVC、Spring Boot、Spring Data、Spring Security 等多个子项目。本篇文章将介绍如何从零开始搭建一个基于 Spr…

内网渗透- 内网渗透的基本知识

攻击流程讲解 内网介绍 内网也指局域网&#xff0c;是指在某一区域内由多台计算机互联而成的计算机组&#xff0c;组网范围通常在数千米以内。在局域网中&#xff0c;可以实现文件管理、应用软件共享、打印机共享、工作组内的日程安排、电子邮件和传真通信服务等。内网是封闭的…

java Nio的应用

Java NIO&#xff08;New Input/Output&#xff09;是Java 1.4引入的一种非阻塞I/O模型&#xff0c;适用于高性能和高并发的应用程序。以下是NIO的一些主要应用场景和特点&#xff1a; 1. 非阻塞I/O NIO支持非阻塞模式&#xff0c;这意味着线程可以在I/O操作进行时继续执行其…