C++ Primer的课后规划问题的第八章
只要。假设提供了第二个参数(int种类),而这个参数不0,的次数的函数打印串数量为该功能将被称为(意,字符串的打印次数不等于第二个參数的值。而等于函数被调用的次数)。是的,这是一个很可笑的函数。但它让读者可以使用本章介绍的一些技术。
在一个简单的程序中使用该函数。以演示该函数是怎样工作的。
#include<iostream>
using namespace std;
void show(const char * a, int b=0);
void show(const char * a, int b)
{
static int uses = 0;
int lim = ++uses;
cout << "第" << uses << "次调用.\n";
if(b==0)
lim = 1;
for (int i=0; i<lim; i++)
cout <<a<<endl;
}
int main()
{
int i;
const char * p = "hello world\n";
cout <<"输入函数调用次数:";
cin >> i;
while(i>=0)
{
cout << "參数n为:" <<i;
show(p, i);
i--;
}
cout << "默认n为0:";
show(p);
return 0;
}
2、CandyBar结构饱含3个成员。第一个成员存储candy bar的品牌名称。第二个成员存储candy bar的重量(可能有小数)。第三个成员存储candy bar的热量(整数)。
请编写一个程序。它使用一个这种函数,即将CandyBar的引用、char指针、double和int作为參数。并用最后3个值设置对应的结构成员。最后3个參数的默认值分别为"Millennium
Munch"、2.85和350。另外,该程序还包括一个以CandyBar的引用为參数。并显示结构内容的函数。
请尽可能使用const.
#include<iostream>
#include<cstring>
using namespace std;
const int MAX=60;
struct CandyBar{
char brand[MAX];
double weight;
int energy;
};
void init(
CandyBar & cb,
const char *br = "Millennium Munch",
double we = 2.85,
int ca = 350
);
void display(const CandyBar & cb);
int main()
{
CandyBar candybar;
cout << "默认的candybar为:"<<endl;
init(candybar);
display(candybar);
char newBrand[MAX];
double newWeight;
int newEnergy;
cout << "请输入新的candybar数据:"<<endl;
cout << "brand: ";
cin.get(newBrand, MAX);
cout << "weight: ";
cin >> newWeight;
cout << "Energy: ";
cin >> newEnergy;
init(candybar, newBrand, newWeight, newEnergy);
display(candybar);
return 0;
}
void init(CandyBar &cb,const char *br, double we, int ca)
{
strcpy(cb.brand, br);
cb.weight = we;
cb.energy = ca;
}
void display(const CandyBar &cb)
{
cout << "新的candybar为:\n";
cout << "brand: " << cb.brand<<endl;
cout << "weight: "<<cb.weight<<endl;
cout << "energy: "<<cb.energy<<endl;
}
.然后编写一个程序,它通过使用一个循环让你可以用不同的输入来測试这个函数,该程序执行情况例如以下:
enter a string (q to quit) :go away
GO AWAY
next string (q to quit) : good grief !
GOOD GRIEF!
next string (q to quit) : q
bye.
#include<iostream>
#include<string>
using namespace std;
void wordupper(string &a);
int main()
{
string str;
cout << "Enter a string (1 to quit):";
getline(cin, str);
int i=0;
while (str != "q")
{
wordupper(str);
cout << str <<endl;
cout << "next string (q to quit):";
getline(cin, str);
}
cout << "bey!\n";
return 0;
}
void wordupper(string &a)
{
int i;
while (a[i])
{
a[i] = toupper(a[i]);
i++;
}
}
5、编写模板函数max5(),它将一个包括5个T类型元素的数组作为參数,并返回数组中最大的元素(因为长度固定,因此能够在循环中使用硬编码,而不必通过參数来传递)。在一个程序中使用该函数,将T替换为一个包括5个int值的数组和一个包括5个dowble值的数组,以測试该函数。
#include<iostream>
template<class T>
T max5(T ar[])
{
int n;
T max = ar[0];
for (n=1; n<5; n++)
if (ar[n] > max)
max = ar[n];
return max;
}
const int lim = 5;
int main()
{
using namespace std;
double ard[lim] = {-3.1, 8.1, -71.1, 34.4, 2.3};
int ari[lim] = {2, 3, 10, 9, 7};
double md;
int mi;
md = max5(ard);
mi = max5(ari);
cout << "Max double = " << md <<endl;
cout << "Max int = " << mi <<endl;
return 0;
}
6、编写模板函数maxn(),它将由一个T类型元素组成的数组和一个表示数组元素数目的整数作为參数。并返回数组中最大的元素。在程序对它进行測试,该程序使用一个包括6个int元素的数组和一个包括4个double元素的数组来调用该函数。程序还包括一个详细化,它将char指针数组和数组中的指针数量作为參数,并返回最长的字符串的地址。假设有多个这种字符串,则返回当中第一个字符串的地址。使用由5个字符串指针组成的数组来測试该详细化。
#include<iostream>
#include<cstring>
template<class T>
T maxn(T ar[], int n)
{
int i;
T max = ar[0];
for (i=1; i<n; i++)
if(ar[i] > max)
max = ar[i];
return max;
}
template<> const char * maxn(const char * arr[], int n)
{
const char * max = arr[0];
int i;
for (i=1; i<n; i++)
if(strlen(arr[i]) > strlen(max))
max = arr[i];
return max;
}
int main()
{
using namespace std;
int ari[6] = {8,4,2,7,10,22};
double ard[4] = {-3.1, 8.1, -71.1, 34.4};
const char * char_arr[5] = {
"hello world!",
"what your name?",
"I love you",
"you are wonderful",
"hi"
};
cout << "The max integer of array is:" << maxn(ari, 6) <<endl;
cout << "The max double of array is:" << maxn(ard, 4) <<endl;
cout << "The max string of array is:" << maxn(char_arr, 5) <<endl;
return 0;
}
7、改动程序清单8.14,使模板函数返回数组元素的总和,而不是显示数组的内容.程序thing的总和以及全部debt的总和.
#include <iostream>
template <typename T>
T ShowArray (T arr[], int n);
template <typename T>
T ShowArray (T * arr[], int n);
struct debts
{
char name[50];
double amount;
};
int main(void)
{
using namespace std;
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<<"Listing Mr. E's counts of things:\n";
cout << ShowArray(things,6) <<endl;
cout<<"Listing Mr. E's debts:\n";
cout << ShowArray(pd,3) <<endl;
return 0;
}
template <typename T>
T ShowArray (T arr[],int n)
{
using namespace std;
T total = 0;
cout<<"template A\n";
for (int i = 0; i < n; i++)
total +=arr[i];
return total;
}
template <typename T>
T ShowArray (T * arr[], int n)
{
using namespace std;
T total = 0;
cout<< "template B\n";
for (int i = 0; i < n; i++)
total += *arr[i];
return total;
}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
版权声明:本文博主原创文章。博客,未经同意不得转载。
C++ Primer的课后规划问题的第八章的更多相关文章
- C++ Primer Pluse_8_课后题
#include <iostream> #include <string> #include<cstring> using namespace std; void ...
- C++ Primer Pluse_7_课后题
#include <iostream> using namespace std; double Sum2(double x, double y) { double sum = 0; if ...
- C++ Primer Pluse_6_课后题
#include <iostream> #include <cctype> #include <array> #include <string> #in ...
- C++ Primer章课后编程问题
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- c primer plus(五版)编程练习-第八章编程练习
1.设计一个程序,统计从输入到文件结尾为止的字符数. #include<stdio.h> int main(void){ int ch; int i; i=; while((ch = ge ...
- <c++primer plus>学习笔记1之第八章函数探幽
1 c++内联函数 编译器将使用相应的函数代码替换函数调用,对于内联代码,函数无需跳到另一个位置执行代码再跳回来,所以内联函数运行速度比常规函数快. 但是代价是需要更多的内存. 使用场合: 执行函数代 ...
- C++ Primer Plus(第6版)中文版——课后练习程序代码
博客内容经历了一次整理,以前发的博文太散.没什么水准,搞的随笔分类越来越多orz,这次把CPP这本书的课后练习的程序代码放到一起方便查阅与修改..嗯 9.6.1 #ifndef _9..1_H_ #d ...
- C++ Primer第九章课后编程问题
1. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0J ...
- [C++ Primer Plus] 第10章、对象和类(二)课后习题
1. bank.h #include <string> using namespace std; class BankAccount { private: std::string m_na ...
随机推荐
- java——多线程——单例模式的static方法和非static方法是否是线程安全的?
单例模式的static方法和非static方法是否是线程安全的? 答案是:单例模式的static方法和非static方法是否是线程安全的,与单例模式无关.也就说,如果static方法或者非static ...
- node-webkit 使用nodejs第三方C/C++插件
node-webkit 在window环境下使用C/C++插件,需要使用nw-gyp先编译.本文以编译node-expat演示操作过程: 1.安装nodejs: 最好将nodejs的执行路径添加进系统 ...
- Servlet基础知识(四)——Servlet过滤器Filter
一.什么是过滤器: 政府大楼的安检保安,它既能对进入政府大楼的人员进行检查,只允许检查符合要求的进入:同时他也负责对出大楼的人进行检查,看他带出的东西是否符合要求. 同样的,Servlet中的过滤器既 ...
- 最近比较迷flash professional cc 做PPT,做一个flash做动态打字效果的教程
想做一个flash打字效果.网上的方法要不是太繁琐,要不然就是各种遗漏.在这边做一个行之有效的flash做打字效果教程. 首先我用的是最新版本的flash professional cc .但是应该和 ...
- MapReuce 编程总结-多MapReduce执行
学习hadoop,必不可少的就是写MapReduce程序,当然,对于简单的分析程序,我们只需一个MapReduce就能搞定,这里就不提单MapReuce的情况了,网上例子很多,大家可以百度Google ...
- 简单使用 PHP Phar 打包php代码 笔记
Phar简介:Phar 归档的概念来自 Java™ 技术的 JAR 归档,它允许使用单个文件打包应用程序,这个文件中包含运行应用程序所需的所有东西.该文件不同于单个可执行文件,后者通常由编程语言生成, ...
- break的使用例一
/* Name:break的使用例一 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月21日 02:28:24 Description:本程序代码无如何含 ...
- QT显示如何减轻闪屏(双缓冲和NoErase)
很多同志在些QT 程序后会遇见闪屏的问题, 有时速度非常快,但毕竟影响了显示效果,如何做到减轻屏幕抖动或闪屏呢?我曾试过如下的办法:1.使用双缓冲. 比如我们在一个Widget里面绘多个图的话, 先创 ...
- 【转载】Android Studio jar、so、library项目依赖,原文链接http://zhengxiaopeng.com/2014/12/13/Android-Studio-jar、so、library项目依赖/
前言 Android Studio(以下简称AS)在13年I/O大会后放出预览版到现在放出的正式版1.0(PS.今天又更新到1.0.1了)历时一年多了,虽然Google官方推出的Android开发者的 ...
- Nhibernate初入门基本配置(二)
转载地址http://www.cnblogs.com/kissdodog/p/3306428.html 使用NHibernate最重要的一步就是配置,如果连NHibernate都还没有跑的起来,谈何学 ...