c++ primer plus chapter8 习题

在这里插入图片描述

#include<iostream>
#include<cstring>
using namespace std;

const int SIZE = 30;

struct CandyBar {
	char brand[SIZE];
	double weight;
	int heat;
};

void set_CandyBar(CandyBar& c, const char* str = "Millenniu Munch", const double w = 3.46, const int h = 340);
void show(const CandyBar& c);

int main() {
	CandyBar candy;
	set_CandyBar(candy, "xiaobaitu", 5.56, 180);
	show(candy);
	return 0;
}

void set_CandyBar(CandyBar& c, const char* str, const double w, const int h)
{
	strcpy_s(c.brand, str);
	c.weight = w;
	c.heat = h;
}

void show(const CandyBar& c)
{
	cout << "Brand:" << c.brand << endl;
	cout << "Weight:" << c.weight << endl;
	cout << "Heat:" << c.heat << endl;
}

在这里插入图片描述

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

void Toupper(string &str);

int main() {
	string str;
	cout << "Enter a string (q to quit):";
	getline(cin, str);
	while (str != "q") {
		Toupper(str);
		cout << str << endl;
		cout << "Next string(q to quit):";
		getline(cin, str);
	}
	cout << "Byebye!" << endl;
	return 0;
}

void Toupper(string& str)
{
	for (int i = 0; i < str.size(); i++) {
		str[i] = toupper(str[i]);
	}
}

在这里插入图片描述

#include<iostream>
#include<cstring>
#include<string>

using namespace std;

struct stringy {
	char* str;
	int ct;
};

void set(stringy& str, const char* ch);
void show(const stringy& str, int n = 1);
void show(const char* ch, int n = 1);


int main() {
	stringy beany;
	char testing[] = "Reality isn't what it used to be.";
	set(beany, testing);
	show(beany);
	show(beany, 2);
	testing[0] = 'D';
	testing[1] = 'u';
	show(testing);
	show(testing, 3);
	show("Done!");
	return 0;
}

void set(stringy& str, const char* ch) {
	int num = strlen(ch) + 1;
	str.str = new char[num];
	strcpy_s(str.str, num, ch);
	str.ct = strlen(str.str);
}

void show(const stringy& str, int n) {
	for (int i = 0; i < n; i++) {
		cout << str.str << " " << str.ct << endl;
	}
}

void show(const char* ch, int n) {
	for (int i = 0; i < n; i++)
		cout << ch << endl;
}

在这里插入图片描述

#include<iostream>
using namespace std;

template<typename T>
T max5(T num[]);


int main() {
	int num1[5] = { 5, 8, 4, 9, 6 };
	double num2[5] = { 5.3, 4.8, 3.9, 10.8, 1.2 };
	cout << max5(num1) << endl;
	cout << max5(num2) << endl;
	return 0;
}

template<typename T>
T max5(T num[]) {
	T max = num[0];
	for (int i = 1; i < 5; i++) {
		if (num[i] > max) {
			max = num[i];
		}
	}
	return max;
}

在这里插入图片描述

#include<iostream>
#include<cstring>
using namespace std;

template<typename T>
T max5(T num[], int n);

template<> 
char* max5(char* ch[], int n);

int main() {
	int num1[6] = { 5, 8, 4, 9, 6, 16 };
	double num2[4] = { 5.3, 4.8, 3.9, 10.8 };
	const char* ch[5] = { "Nice to meet you.", "I love you,Rick.", "Go away.", "Good grief.", "Good moring" };
	cout << max5(num1, 6) << endl;
	cout << max5(num2, 4) << endl;
	cout << max5(ch, 5) << endl;
	return 0;
}

template<typename T>
T max5(T num[], int n) {
	T max = num[0];
	for (int i = 1; i < n; i++) {
		if (num[i] > max) {
			max = num[i];
		}
	}
	return max;
}

template<> 
char* max5(char* ch[], int n) {
	int pos = 0;
	for (int i = 1; i < n; i++) {
		if (strlen(ch[i]) > strlen(ch[pos])) {
			pos = i;
		}
	}
	return ch[pos];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛定谔的喵~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值