《C++primerplus》第8章练习题
1.(简单用一下引用变量,没有采用书中的题目)定义一个替身结构体,存储名字(char[])和力量值(int)。使用结构体引用作为形参写两个函数,一个不加const,使得能对定义的结构体做修改,另一个加上const不变动它的内容。第一个函数设置替身的名字和力量值,第二个函数输出结构的信息。
#include<iostream>
using namespace std; struct stand
{
int power;
char name[15];
}; void show_stand(const stand & temp_show);
void set_stand(stand & temp_set); int main()
{
stand new_stand; set_stand(new_stand);
show_stand(new_stand); system("pause");
} void show_stand(const stand & temp_show)
{
cout << "name:" << temp_show.name << endl;
cout << "power:" << temp_show.power << endl;
} void set_stand(stand & temp_set)
{
cout << "Enter stand's name:";
cin .get(temp_set.name,15);
cin.get();
cout << "Enter stand's power:";
cin >> temp_set.power;
cout << "\n";
}
2.编写一个函数,接受一个指向string对象的引用作为参数,并将该string对象的内容转换为大写,使用toupper函数。通过循环提示输入。
#include<iostream>
#include<cctype>
#include<string>
using namespace std; void upper_string(string & string_temp); int main()
{
string input_string; cout << "Enter a string (q to quit): "; getline(cin,input_string); while (input_string != "q")
{
upper_string(input_string);
cout << input_string;
cout << "\nNext string (q to quit): ";
getline(cin, input_string);
} cout << "Bye.\n";
system("pause");
} void upper_string(string & string_temp)
{
for (int i = 0; string_temp[i] != '\0'; i++)
{
string_temp[i] = toupper(string_temp[i]);
}
}
3.完成书上的例程,编写一个set函数,接受一个结构体引用和一个字符串,使用new为结构体内的字符指针动态分配空间以存储该字符串。利用函数重载编写两个show函数,都使用默认参数,一个使用上面定义的结构体,输出它存储的字符串,另一个直接输出传入的字符串。
#include<iostream>
#include<cstring>
using namespace std; struct stringy {
char * str;
int ct;
}; void set(stringy & st_ref, string str_set_temp);
void show(const stringy st_show_temp, int times = 1);
void show(const string str_show_temp, int times = 1); int main()
{
stringy beany;
char testing[] = "Reality isn't it used to be."; set(beany,testing);
show(beany);
show(beany, 2);
testing[0] = 'D';
testing[1] = 'u';
show(testing, 3);
show("Done!"); system("pause");
} void set(stringy & st_ref, string str_set_temp)
{
st_ref.ct = str_set_temp.length(); //获取字符串的长度
st_ref.str = new char[st_ref.ct]; //为结构体引用里面的字符数组分配空间
for (int i = 0; i < st_ref.ct; i++)
{
st_ref.str[i] = str_set_temp[i];
}
} void show(const stringy st_show_temp, int times = 1)
{
for (int t = 0; t < times; t++)
{
for (int i = 0; i < st_show_temp.ct; i++)
{
cout << st_show_temp.str[i];
}
cout << "\n";
}
} void show(const string str_show_temp, int times = 1)
{
for (int t = 0; t < times; t++)
{
for (int i = 0; i < str_show_temp.length(); i++)
{
cout << str_show_temp[i];
}
cout << "\n";
}
}
4.简单使用模板函数。编写模板函数,接受不同类型的数组和其大小,输出其中的最大值。先使用int数组测试,再使用double数组测试。
#include<iostream>
using namespace std; template <typename T>
T maxn(T * t_ptr, int n); template <typename T>
void input_num(T * temp, int n); int main()
{
int array_size; //先使用int数组测试
cout << "How many numbers:(int) ";
cin >> array_size; int * int_array = new int [array_size];
input_num(int_array,array_size);
cout << "Max number: " << maxn(int_array, array_size) << endl; //再使用double数组测试
cout << "How many numbers:(double) ";
cin >> array_size; double * double_array = new double[array_size];
input_num(double_array, array_size);
cout << "Max number: " << maxn(double_array, array_size) << endl; delete[]int_array;
delete[]double_array;
system("pause");
} template <typename T>
T maxn(T * t_ptr, int n)
{
T max = t_ptr[0];
for (int i = 0; i < n; i++)
{
if (t_ptr[i] > max)
max = t_ptr[i];
else {};
}
return max;
} template <typename T>
void input_num(T * temp, int n)
{
cout << "Enter the numbers:" << endl;
for (int i = 0; i < n; i++)
{
cin >> temp[i];
}
}
*需要注意模板函数每次函数原型和函数的实现之前,都要加上template<typename ...>
《C++primerplus》第8章练习题的更多相关文章
- 《C++ primerplus》第13章练习题
1.对CD类的派生练习.基类CD类存储作者和作品号等信息,派生类Classic额外增加一格"主要作品"的信息.主函数使用拷贝构造函数.按引用传递参数的函数和指针来测试基类和派生类的 ...
- 《C++primerplus》第12章练习题
做一下倒数两题,都是在队列模拟的程序基础上做点修改测试. 5.找出平均等候时间为1分钟时,每小时到达的客户数为多少(试验时间不少于100小时). 指定队伍最大长度10人,模拟100小时.粗略估计答案在 ...
- 《C++primerplus》第11章练习题
1.修改程序清单11.5(随机漫步),使之以特定的格式将结果写入文件中. //vector.h -- Vector Class #ifndef _VECTOR_H_ #define _VECTOR_H ...
- 《C++primerplus》第10章练习题
1.定义一个类表示银行账户.数据成员包括姓名,账号和存款.成员函数可以执行初始化数据.显示数据和取款存款的功能. //Bank.cpp #include<iostream> #includ ...
- 《C++primerplus》第9章练习题
1.(未使用原书例题)练习多文件组织.在一个头文件中定义一种学生的结构体,存储姓名和年龄,声明三个函数分别用于询问有多少个学生,输入学生的信息和展示学生的信息.在另一个源文件中给出所有函数的定义.在主 ...
- 《C++primerplus》第7章练习题
1.用户不断输入两个数,计算调和平均数,直到其中一个数为0. #include<iostream> using namespace std; double harm_mean(double ...
- 《C++primerplus》第6章练习题
本来前面五题都做完了,写博客时没保存好草稿= =,写了个整合版的程序,实现前五题的关键部分. 1.定义一个叫jojo的结构,存储姓名.替身和力量值,使用动态结构数组初始化二乔.承太郎和乔鲁诺乔巴纳等人 ...
- 《C++primerplus》第4章练习题
注:略过部分题目,修改了题设要求,实现差不多的功能 1.使用字符数组.要求用户输入姓名,等第和年龄,输出其姓名和年龄,等第降一级(即字母高一级). #include<iostream> u ...
- python第一章练习题
本章总节 练习题 1.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释 编译型:把源代码编译成机器语言的可执行文件,程序执行的时候执行可执行文件即可. 优点:程序执行不 ...
随机推荐
- 【Maven】Mac操作系统下安装配置maven环境变量
1.下载maven 下载地址:http://maven.apache.org/download.cgi 2.解压设置maven本地地址解压后 我放在 /Users/david/developer/ap ...
- C III
http://cossacksworld.ucoz.co.uk/load/c_iii_files/79 http://cossacksworld.ucoz.co.uk/load/c_iii_files ...
- html加C#上传文件
最近在学上传文件部分内容,包括创建文件夹,设置文件夹属性,上传文件并保存. 前台代码: <html xmlns="http://www.w3.org/1999/xhtml"& ...
- SpringSecurity中的Authentication信息与登录流程
目录 Authentication 登录流程 一.与认证相关的UsernamePasswordAuthenticationFilter 获取用户名和密码 构造UsernamePasswordAuthe ...
- shell小技巧(4)AIX和Linux计算天前日期
Linux计算天前日期: date -d "- day" +%Y%m%d AIX计算5天前日期: perl -e "use POSIX qw(strftime); pri ...
- leetcode刷题-88.合并两个有序数组
题目 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m ...
- 8.ffmpeg-基础常用知识
1.封装格式MPEG-4其中 MPEG-1 和 MPEG-2 是采用相同原理为基础的预测编码.变换编码. 熵编码及运动补偿等第一代数据压缩编码技术:MPEG-4(ISO/IEC 14496)则是基于第 ...
- pyhton:time模块和datetime模块
一.time模块 1.相关定义: time模块时间的表达有3种,时间戳,时间元祖,格式化时间 #时间戳: print(time.time())#获取当前时间戳.时间戳的计算是1970纪元后经过的浮点秒 ...
- DevOps-实践心得
基于最近几年从事与DevOps的相关实践,对这篇文章的观点深有体会,所以记录在这里.加粗部分是我比较深有体会的,但是对于最后作者对于"运维"有些悲观,我有点不敢苟同,反而对于运维的 ...
- 用ajax获取后端数据,显示在前端,实现了基本计算器功能
下午在看视频的时候,遇到一个问题:如何把后端 print_r或echo的数据显示在前端.百度了一下,说是用ajax,想着前一阵子学习了ajax,并且最近也想做一个计算器,于是就自己钻起来了. 计算器的 ...