c++primerpluschapter8习题-创新互联

在这里插入图片描述

成都服务器托管,创新互联建站提供包括服务器租用、光华机房服务器托管、带宽租用、云主机、机柜租用、主机租用托管、CDN网站加速、空间域名等业务的一体化完整服务。电话咨询:18982081108
#include#includeusing 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#include#includeusing 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#include#includeusing 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;
}

在这里插入图片描述

#includeusing namespace std;

templateT 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;
}

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

在这里插入图片描述

#include#includeusing namespace std;

templateT 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;
}

templateT 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];
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站题目:c++primerpluschapter8习题-创新互联
链接地址:http://myzitong.com/article/gicji.html