C++ Primer Pluse_8_课后题
#include <iostream>
#include <string>
#include<cstring> using namespace std; void showStr(const char * str, int & n)
{
cout << str << endl; if (n > 0)
{
showStr(str, --n);
}
}
int test8_1()
{
char str[] = "hello cat";
int n = 2;
showStr(str, n);
return 0;
} int test8_2()
{ return 0;
} void Up(string & str)
{
int i = 0;
while (str[i])
{
str[i] = toupper(str[i]);
i++;
} cout << str << endl; } int test8_3()
{
string str; cout << "Enter a string (q to quit): ";
getline(cin, str); while (str != "q")
{
Up(str); cout << "Enter next string:(q to quit):";
getline(cin, str);
}
cout << "bye.\n"; return 0;
} struct stringy{
char *str;
int ct;
}; void set(stringy & beany, const char *test)
{
beany.ct = strlen(test);
beany.str = new char[beany.ct +1];
int i = 0;
for (i = 0; test[i]; i++)
{
beany.str[i] = test[i];
}
beany.str[i] = 0;
} void show(const stringy beany, int n = 1)
{
for (int i = 1; i <= n; i++)
{
cout << beany.str << endl;
cout << beany.ct << endl;
}
} void show(const char *testing, int n = 1)
{
for (int i = 1; i <= n; i++)
{
cout << testing << endl;
}
}
int test8_4()
{
stringy beany;
char testing[] = "Hello cat."; set(beany, testing);
show(beany);
cout << endl;
show(beany, 2);
testing[0] = 'D';
testing[1] = 'u';
show(testing); show(testing, 3);
show("Done!");
return 0;
} template <typename T>
T Max5(T arr[])
{
int i = 0;
T max = arr[0];
for (i = 1; i < 5; i++)
{
if (max < arr[i])
{
max = arr[i];
}
} return max; }
int test8_5()
{
int a[5] = { 2, 3, 1, 5, 4 }; cout << Max5(a); double b[5] = { 6, 8, 5, 9, 6 }; cout << endl << Max5(b);
return 0;
} template <typename T>
T maxn(T arr[], int n)
{
int i = 0;
T max = arr[0];
for (i = 1; i < n; i++)
{
if (max < arr[i])
{
max = arr[i];
}
}
return max;
} template <> char * maxn<char *>(char * arr[], int n)
{
int i = 0;
int max = strlen(arr[0]);
int maxk = 0; for (i = 1; i < n; i++)
{
if (max < strlen(arr[i]))
{
max = strlen(arr[i]);
maxk = i;
} } return arr[maxk];
}
int test8_6()
{
int a[6] = { 2, 3, 1, 5, 6, 4 };
double b[4] = { 9, 6, 7, 4 }; char * strarr[3] = { "Hello cat!", "boat", "miao wu" }; cout << maxn(a, 6) << endl;
cout << maxn(b, 4) << endl;
cout << maxn(strarr, 3) << endl;
return 0;
} template <typename T>
void ShowArray(T arr[], int n); template <typename T>
void ShowArray(T * arr[], int n); struct debts
{
char name[50];
double amount;
}; template <typename T>
void ShowArray(T arr[], int n)
{
cout << "template A\n";
for (int i = 0; i < n; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
} template <typename T>
void ShowArray(T *arr[], int n)
{
cout << "using template B\n";
for (int i = 0; i < n; i++)
{
cout << *arr[i] << ' ';
} cout << endl;
} template <typename T>
T SumArray(T arr[], int n)
{
T sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
return sum;
} template <typename T>
T SumArray(T *arr[], int n)
{
T sum = 0;
for (int i = 0; i < n;i++)
{
sum += *arr[i];
}
return sum;
}
int test8_7()
{
int things[6] = { 1, 2, 3, 4, 5, 6 }; struct debts mr_e[3] =
{
"Cat mioa", 10.1,
"Dog Boat", 20.1,
"Fish ni", 30.1
};
double *pd[3];
for (int i = 0; i < 3; i++)
{
pd[i] = &mr_e[i].amount;
} ShowArray(things, 6); ShowArray(pd, 3); cout << SumArray(things, 6) << endl;
cout <<SumArray(pd, 3) << endl;
return 0;
} int main()
{
test8_7(); system("pause");
return 0;
}
C++ Primer Pluse_8_课后题的更多相关文章
- 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 ...
- 玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
前文 的最后给出了玉伯的一道课后题,今天我们来讲讲这题的思路. 题目是这样的: Number.MAX_VALUE + 1 == Number.MAX_VALUE; Number.MAX_VALUE + ...
- 算法(JAVA)----两道小小课后题
LZ最近翻了翻JAVA版的数据结构与算法,无聊之下将书中的课后题一一给做了一遍,在此给出书中课后题的答案(非标准答案,是LZ的答案,猿友们可以贡献出自己更快的算法). 1.编写一个程序解决选择问题.令 ...
- 课后题2.87&2.86
课后题2.86&2.87 单纯就是想加点分第十章的题目都被做过了就做下第二章的,正好复习一下前面学的知识,第二章给我剩下的题目也不多了,我就挑了这个题目. 2.86 考虑一个基于IEEE浮点格 ...
- c++面向对象程序设计 课后题 答案 谭浩强 第四章
c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...
- 学习参考《零基础入门学习Python》电子书PDF+笔记+课后题及答案
国内编写的关于python入门的书,初学者可以看看. 参考: <零基础入门学习Python>电子书PDF+笔记+课后题及答案 Python3入门必备; 小甲鱼手把手教授Python; 包含 ...
- 学习《零基础入门学习Python》电子书PDF+笔记+课后题及答案
初学python入门建议学习<零基础入门学习Python>.适合新手入门,很简单很易懂.前一半将语法,后一半讲了实际的应用. Python3入门必备,小甲鱼手把手教授Python,包含电子 ...
- Java程序设计(2021春)——第一章课后题(选择题+编程题)答案与详解
Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 第一章选择题 1.1 Java与面向对象程 ...
随机推荐
- BZOJ3934 : [CQOI2015]标识设计
轮廓线插头DP. 设$f[i][j][a][b][c][d][e]$表示考虑到了$(i,j)$,轮廓线上3个下插头的位置分别为$a,b,c$,是否有右插头,已经放了$e$个$L$的方案数. 然后直接D ...
- 消除ListView, gridview中的选项单击是的默认黄色底色
要消除其默认的单击底色,只需要在**View定义时为其添加 android:cacheColorHint="#00000000" android:listSelector=&quo ...
- 显式Intent和隐式Intent
http://blog.csdn.net/qs_csu/article/details/7995966 对于明确指出了目标组件名称的Intent,我们称之为“显式Intent”. 对于没有明确指出目标 ...
- 【HDU】1850 Being a Good Boy in Spring Festival
http://acm.hdu.edu.cn/showproblem.php?pid=1850 题意:同nim...顺便求方案数... #include <cstdio> #include ...
- 【JAVA】Spring 数据源配置整理
在Spring中,不但可以通过JNDI获取应用服务器的数据源,也可以直接在Spring容器中配置数据源,此外,还可以通过代码的方式创建一个数据源,以便进行无依赖的单元测试. 配置数据源 ...
- 51nod算法马拉松12
A 第K大区间 不妨考虑二分答案x,则问题转化成计算有多少个区间满足众数出现的次数>=x. 那么这个问题我们使用滑动窗口,枚举右端点,则左端点肯定单调递增,然后维护一个简单的数组就能资瓷添加元素 ...
- Spring MVC过滤器-字符集过滤器(CharacterEncodingFilter)
spring的字符集过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter,Charact ...
- JSch - Java实现的SFTP(文件下载详解篇)
上一篇讲述了使用JSch实现文件上传的功能,这一篇主要讲述一下JSch实现文件下载的功能.并介绍一些SFTP的辅助方法,如cd,ls等. 同样,JSch的文件下载也支持三种传输模式:OVERWRITE ...
- [转]AS3的垃圾回收
GC和内存泄露无关 垃圾回收,这次是一个被无数人讨论过的传统话题. Action Script使用的是和Java相似的内存管理机制,并不会即时回收废弃对象的内存,而是在特定时间统一执行一次GC(Gab ...
- Pixar Shorts 皮克斯动画短片全集
[原创短片](Theatrical Shorts)16部 <安德鲁和威利冒险记><顽皮跳跳灯><红色的梦><锡铁小兵><小雪人大行动>< ...