编程练习答案第八章

8.1写输出字符串的函数,存在默认参数表示输出频率,莫感觉1.(原标题太扯了,的问题的细微变化的基础上,含义)

//8.1编写一个输出字符串的函数。有一个默认參数表示输出次数,默觉得1.(原题太扯啦,题意基础上小修改)
#include <iostream>
using namespace std; void show (const char* str, int time=1)
{
unsigned n=(time>0)? time:-time;
for (unsigned i = 0; i < n; ++i)
cout << str << endl;
cout << "-----------------" << endl;
} int main ()
{
const char* str = "hello word!";
show(str);
show(str,0);
show(str,1);
show(str,-1);
}

8.2CandyBar结构体有品牌,重量和热量3个成员,编写一个含有默认值的函数为成员赋值和一个显示内容函数

//8.2CandyBar结构体有品牌。重量和热量3个成员。编写一个含有默认值的函数为成员赋值和一个显示内容函数
//CandyBar& candbar, const char* strBrand = "Millennium Munch", double weight = 2.85, int calories = 350
#include <iostream>
#include <string>
using namespace std; struct CandyBar
{
string Brand;
double weight;
int calories;
}; void input (CandyBar& candbar, const char* Brand = "Millennium Munch", double weight = 2.85, int calories = 350)
{
candbar.Brand = Brand;
candbar.weight = weight;
candbar.calories = calories;
} void show (const CandyBar& candbar)
{
cout << candbar.Brand << '\t' << candbar.weight << '\t' << candbar.calories << endl;
} int main ()
{
CandyBar candbar1, candbar2;
input(candbar1);
show(candbar1);
input(candbar2, "world", 2,3 );
show(candbar2);
}

8.3编写一个函数。接受string引用。并把string内容变大写,之后利用循环測试

//8.3编写一个函数,接受string引用,并把string内容变大写,之后利用循环測试
#include <iostream>
#include <cctype>
using namespace std; string& upper (string& str)
{
for (char& e : str)
e = (char)toupper(e);
return (str);
} int main ()
{
while (true) {
cout << "Enter a string (q to quit): ";
string str;
getline(cin, str);
if (!cin || "q" == str || "Q" == str)
break;
cout << upper(str) << endl;
}
}

8.4按下面程序框架,完毕函数

//8.4按下面程序框架,完毕函数
#include <iostream>
#include <cstring>
using namespace std; struct stringy {
char * str;
int ct;
}; void set (stringy& stry, const char* szTxt)
{
stry.ct = (int)strlen(szTxt);
stry.str = new char [stry.ct + 1];
strcpy(stry.str, szTxt);
} void show (const char* szTxt, unsigned times = 1)
{
for (unsigned i = 0; i < times; ++i)
cout << szTxt << endl;
} void show (const stringy& stry, unsigned times = 1)
{
for (unsigned i = 0; i < times; ++i)
cout << stry.str << endl;
} void destroy (stringy& stry)
{
delete [] stry.str;
stry.str = NULL;
} int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set(beany, testing);
show(beany);
show(beany, 2);
destroy(beany);
testing[0] = 'D';
testing[1] = 'u';
show(testing);
show(testing, 3);
show("Done!");
}

8.5利用一个模板函数max5()。返回给予的5个数的最大值(应用c++11的array)

//8.5利用一个模板函数max5()。返回给予的5个数的最大值(应用c++11的array)
#include <iostream>
#include <array>
using namespace std; template<typename T>
const T& max5 (const array<T, 5>& arr)
{
unsigned max = 0;
for (unsigned i = 0; i < 5; ++i)
if (arr[i] > arr[max])
max = i;
return (arr[max]);
} int main ()
{
array<int, 5> iarray = {32, 20, 99, 256, 9};
for (const auto& e : iarray)
cout << e << ' ';
cout << " max: " << max5(iarray) << endl; array<double,5> darray = {1.2,2.3,4.5,32.3,4.3};
for (const auto& e : darray)
cout << e << ' ';
cout << "max: " << max5(darray) << endl;
}

8.6编写模板函数maxn(),參数为一个数组和元素数目。返回数组最大值,然后详细化一个字符串为元素的字符串,返回最长字符串

//8.6编写模板函数maxn(),參数为一个数组和元素数目,返回数组最大值,
//然后详细化一个字符串为元素的字符串,返回最长字符串
#include <iostream>
#include <cstring>
using namespace std; template <typename T>
const T& maxn (const T arr[], unsigned n)
{
unsigned max = 0;
for (unsigned i = 0; i < n; ++i)
if (arr[i] > arr[max])
max = i;
return (arr[max]);
} char* maxn ( char* arr[], unsigned n)
{
unsigned max = 0;
for (unsigned i = 0; i < n; ++i)
if (strlen(arr[i]) > strlen(arr[max]))
max = i;
return (arr[max]);
} int main ()
{
int iArray[] = {32, -1, 99, 0, 256, 9};
for (const auto& e : iArray)
cout << e << ' ';
cout << " ----max: " << maxn(iArray, sizeof(iArray)/sizeof(iArray[0])) << endl;
double dArray[] = {-3.2, 221.22, 9.9, 0, 1};
for (const auto& e : dArray)
cout << e << ' ';
cout << " ----max: " << maxn(dArray, sizeof(dArray)/sizeof(dArray[0])) << endl;
const char* szArray[] = {"aaaa","bbbbbb","cc","fffffffffff","kkkk"};
for (const auto& e : szArray)
cout << '\"' << e << '\"' << ' ';
cout << " ----max: " << '\"' << maxn(szArray, sizeof(szArray)/sizeof(szArray[0])) << '\"' << endl;
}

8.7对程序清单8.14,使用两个SumArray()模板返回数组元素总和。

程序返回thing以及debt的总和

//8.7对程序清单8.14,使用两个SumArray()模板返回数组元素总和。程序返回thing以及debt的总和
#include <iostream>
using namespace std; struct debts
{
char name[50];
double amount;
}; template <typename T>
double SumArray(T arr[], int n)
{
cout << "template A\n";
double sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return (sum);
} template <typename T>
double SumArray(T * arr[], int n)
{
cout << "template B\n";
double sum = 0;
for (int i = 0; i < n; i++)
sum += *arr[i];
return (sum);
} int main()
{
int things[6] = {13, 31, 103, 301, 310, 130};
struct 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 total number of Mr. E's things: " << SumArray(things, 6) << endl;
cout << "the sum of Mr. E's all debts: " << SumArray(pd, 3) << endl;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

c++ primer plus(文章6版本)中国版 编程练习答案第八章的更多相关文章

  1. 孔雀翎----《Programming C# 》中国版 文章4版

    孔雀翎----<Programming C# >中国版 文章4版 页:http://blog.csdn.net/21aspnet/           时间:2007.8.7 电子工业出版 ...

  2. 瘟疫公司中国版(Android)手动破解内购

    前言 洒家近日下载了个瘟疫公司中国版(安卓版)(com.easymobi.plagueinc.mi ,版本 1.1.2(5)(.mi 小米版)),发现游戏需要内购而且价格不菲. 需求 root权限 文 ...

  3. CSDN CODE平台,中国版Github简要使用说明

    CSDN CODE平台,中国版Github简要使用说明!(多图慎入)   楼主说 以前一直看到别人在用github发布自己的代码,各种牛逼,各种羡慕嫉妒恨.最后终于受不了了,也去注册了一个,注册到没什 ...

  4. 一起学微软Power BI系列-使用技巧(4)Power BI中国版企业环境搭建和帐号问题

    千呼万唤的Power BI中国版终于落地了,相信12月初的微软技术大会之后已经铺天盖地的新闻出现了,不错,Power BI中国版真的来了,但还有些遗憾,国际版的一些重量级服务如power bi emb ...

  5. 简体中国版文档的Markdown语法

    Markdown文件 注意︰这是简体中国版文档的Markdown语法.如果你正在寻找英语版文档.请参阅Markdown︰ Markdown: Syntax. Markdown: Syntax 概述 哲 ...

  6. Bluemix中国版体验(一)

    很高兴终于拿到了中国版Bluemix的账号!中国版的Bluemix是由世纪互联运营的,这也是世纪互联继Microsoft Azure,Office 365之后运营的又一个国际一线大品牌的云服务. 中国 ...

  7. 准备使用 Office 365 中国版--安装

    温故而知新,先附上一个链接:Office 365常见问题 Office 365中Office套件的安装介质和传统Office套件的安装介质是有些区别的,虽然功能都一样.因此,如果购买了带Office套 ...

  8. 准备使用 Office 365 中国版--购买

    Office 365中国版支持两种购买方式,Web Direct(在线购买)和CSP(代理商购买).如果客户的企业规模不大(几十个用户,小于100用户)或者是个人/家庭购买,可以直接选择在线购买方式. ...

  9. 真相:中国版BBB用USB连电脑没有盘符的根本原因分析

    很多网友在问:为什么中国版的装完驱动插上板子没有显示端口号和69M的盘符??楼主发现,在开机启动的时候,加载g_multi模块时出现错误提示 invalid argument.         Emb ...

随机推荐

  1. Java的Log系统介绍和切换(转)

    Java的log系统比较繁杂.在这里梳理一下.本文只涉及log系统介绍和处理log系统之间的切换.不涉及如何配置和使用. 具体的log系统 Log4j:准确的说是log4j 1.x版.是之前使用最广泛 ...

  2. [Cacti] cacti监控mongodb性能实战

    .生成监控图 在界面.选择"Device".选择mongodb服务器连接"3.X2_mongodb".再选择右上角的"Create Graphs fo ...

  3. jenkins配置.net mvc网站

    jenkins配置.net mvc网站 上一篇使用jenkins配置.net mvc网站进行持续集成一只是简单介绍了jenkins构建站点到本地服务器,这一篇,就来讲解如何部署站点到指定的服务器上面. ...

  4. svn 使用(一个)

    一个. 安装svn  server(操作系统centos) yum install subversion 通过 subversion -v 如果成功安装命令来查看 温馨提示不承担任何subversio ...

  5. ICMP协议Ping命令的应用

    ICMP的全称是 Internet Control Message Protocol ,它是TCP/IP协议族的一个子协议,属于网络层协议,用于在IP主机.路由器之间传递控制消息.从技术角度来讲,就是 ...

  6. ASP.Net 重写IHttpModule 来拦截 HttpApplication 实现HTML资源压缩和空白过滤

    务实直接上代码: 1. 重写FilterModule.cs using System; using System.Collections.Generic; using System.Linq; usi ...

  7. 如何设置一个activity透明

    1.在AndroidManifest.xml文件中设置: android:theme="@android:style/Theme.Translucent 此代码固定为全背景透明. 2.在Ac ...

  8. uva 11396Claw Decomposotion(二分图判定)

     题目大意:给出一个简单无向图,每一个点的度为3.推断是否能将此图分解成若干爪的形式.使得每条边都仅仅出如今唯一的爪中. (点能够多次出如今爪中) 这道题实质上就是问这个图是否为二分图,dfs判定 ...

  9. $(&#39;#checkbox&#39;).attr(&#39;checked&#39;); 回报checked或undefined该解决方案

    $('#checkbox').attr('checked'); 返回的是checked或者是undefined,不是原来的true和false了,有关此问题的解决方法例如以下 在JQ1.6之前的版本号 ...

  10. top 查看资源使用

    top:动态观察程序的变化 ? [root@linux ~]# top [-d] | top [-bnp] 参数: -d :后面可以接秒数,就是整个程序画面更新的秒数.预设是 5 秒: -b :以批次 ...