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与面向对象程 ...
随机推荐
- Java keytool 使用总结
Keytool 是一个Java 数据证书的管理工具 ,Keytool 将密钥(key)和证书(certificates)存在一个称为keystore的文件中. 在keystore里,包含两种数据: ( ...
- CF# 334 Alternative Thinking
A. Alternative Thinking time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Document 按照xml格式输出
private void GetXMLDocument(Document doc) { OutputFormat format1 = new OutputFormat(" ", t ...
- Boom.TV完成350万美元融资,目标直指VR电竞直播
3D在线电竞直播平台Boom.tv刚刚宣布已经完成350万美元的融资,该平台旨在让观众在任何设备以任意视角观看电竞比赛,并将支持VR版本. 这家位于美国加州红木城的初创公司成立于2015年,由Gupt ...
- textarea{resize:none}
resize:none设置了不可以调整文本域
- 洛谷 P1074 靶形数独 Label:search 不会
题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教, Z 博士拿出了他最近发明的 ...
- ASP.NET状态保持方案若干
客户端方案: 1.ViewState 2.隐藏域 3.cookie 大小4KB限制,不消耗服务器资源,可配置到期时间,但安全性不高,还被客户端禁用. 4.QueryString 方法简单,但不安全,有 ...
- docker 练习
echo 'DOCKER_OPTS="-b=bridge0"' >> /etc/default/docker sudo docker run -i -t centos ...
- GO语言练习:实现最简单的http helloword 服务器
用Go语言实现一个最简单的http服务器端,主要用到了package io, log, net/http 这个3个库. 用到的函数包括: http.Handle() http.HandlerFunc( ...
- MVVM Command Binding: InvokeCommandAction v.s. EventToCommand
This gives you the ability to create a trigger on an event and bind it to an ICommand on the view mo ...