C++PrimerPlus学习——第八章编程练习

8-1

#include <iostream>
#include <string>

void show_string(std::string str, int i = 0);

int main()
{
	using namespace std;
	string str;
	int n = 1;
	cout << "Enter a string: ";
	cin >> str;
	show_string(str);
	cout << "Enter a number: ";
	cin >> n;
	show_string(str, n-1);
	return 0;
}

void show_string(std::string str, int i)
{
	while (i > -1)
	{
		std::cout << str << std::endl;
		i--;
	}
}

8-2

#include <iostream>

struct CandyBar
{
	char* brand;
	double weight;
	int cal;
};

void set_candy(CandyBar& candytype, char* brand, double weight, int cal);
void show_candy(CandyBar& candytype);

int main()
{
	using namespace std;
	CandyBar candy;
	char brand[20];
	double weight;
	int cal;
	cout << "Enter candy brand: ";
	cin.getline(brand, 20);
	cout << "\nEnter candy weight: ";
	cin >> weight;
	cout << "\nEnter candy cal: ";
	cin >> cal;
	set_candy(candy, brand, weight, cal);
	show_candy(candy);
	return 0;
}

void set_candy(CandyBar& candytype, char* brand, double weight, int cal)
{
	candytype.brand = brand;
	candytype.weight = weight;
	candytype.cal = cal;
}
void show_candy(CandyBar& candytype)
{
	using namespace std;
	cout << candytype.brand << endl;
	cout << candytype.weight << endl;
	cout << candytype.cal << endl;
}

8-3

#include <iostream>
#include <string>

void upword(std::string& str);

int main()
{
	using namespace std;
	string str;
	cout << "Enter a string (q to quit): ";
	while (getline(cin, str))
	{
		if (str == "q")
		{
			cout << "Bye.";
			break;
		}
		else
		{
			upword(str);
			cout << str << endl;
			cout << "Next string (q to quit): ";
		}
	}
	return 0;
}
void upword(std::string& str)
{
	int size = str.size();
	for (int i = 0; i < size; i++)
	{
		if (isalpha(str[i]))
		{
			str[i] = toupper(str[i]);
		}
	}
}

8-4
show函数里面可以加上const

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

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

void set(stringy& beany, char* testing);
void show(const char* testing, int n = 1);
void show(const stringy& beany, 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& beany, char* testing)
{
	beany.ct = strlen(testing);
	beany.str = testing;
}

void show(const char* testing, int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << testing << endl;
	}
}
void show(const stringy& beany, int n)
{
	for (int i = 0; i < n; i++)
		cout << beany.str << endl;
}

8-5

#include <iostream>

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

int main()
{	 
	int a[5] = { 3, 2, 1, 5, 9 };
	double b[5] = { 2.8,3.5,1.5,1.7,3.3 };
	std::cout << "max int: " << max5(a) << std::endl;
	std::cout << "max double: " << max5(b) << std::endl;
	return 0;
}

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

8-6

#include <iostream>
#include<string>

template <typename T>
T max(T arr[], int n);
template <> const char* max(const char* arr[], int n);
	
int main()
{	 
	int a[6] = { 3, 2, 1, 5, 9, 8 };
	double b[4] = { 2.8,3.5,1.5,1.7};
	const char* c[6] = { "asdf", "asdfd", "sqweq", "wqf", "sdfa", "sdfaqqq" };
	std::cout << "max int: " << max(a, 6) << std::endl;
	std::cout << "max double: " << max(b, 4) << std::endl;
	std::cout << "max double: " << max(c, 6) << std::endl;
	return 0;
}

template <typename T>
T max(T arr[], int n)
{
	T temp = arr[0];
	for (int i = 1; i < 5; i++)
	{
		if (arr[i] > temp)
			temp = arr[i];
	}
	return temp;
}
template <> const char* max(const char* arr[], int n)
{
	const char* m = arr[0];
	for (int i = 1; i < n; ++i)
	{
		if (strlen(arr[i]) > strlen(m))
			m = arr[i];
	}
	return m;
}

8-7

#include <iostream>

template <typename T>
T SumArray(T arr[], int n);

template <typename T>
T SumArray(T* arr[], int n);

struct debts
{
	char name[50];
	double amount;
};

int main()
{
	using namespace std;
	int things[6] = { 13,31,103,301,310,130 };
	struct debts mr_E[3] =
	{
		{"Ima Wolfe", 2400.0},
		{"Ura Foxe", 1300.0},
		{"Iby Stout", 1800.0}
	};
	double* pd[3];

	for (int i = 0; i < 3; i++)
		pd[i] = &mr_E[i].amount;

	cout << "Listing Mr. E's counts of things: " << SumArray(things, 6) << "\n";
	cout << "Listing Mr E's debts: " << SumArray(pd, 3) << "\n";
	return 0;
}
template <typename T>
T SumArray(T arr[], int n)
{
	using namespace std;
	T temp = arr[0];
	for (int i = 1; i < n; i++)
		temp += arr[i];
	return temp;
}
template <typename T>
T SumArray(T* arr[], int n)
{
	using namespace std;
	T temp = *arr[0];
	for (int i = 1; i < n; i++)
		temp += *arr[i];
	return temp;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值