Essential c++ 第二章课后练习

版权声明:瞎几把写 https://blog.csdn.net/weixin_43184615/article/details/82793395

练习2.1

#include <iostream>
#include <string>
using namespace  std;
bool fibon_elem(int pos, int &elem)
{
	if (pos<0 || pos>1024)
	{
		elem = 0;
		return false;
	}
	elem = 1;
	int n_2 = 1, n_1 = 1;

	for (int i = 3; i <= pos; ++i)
	{
		elem = n_2 + n_1;
		n_1 = n_2; n_2 = elem;
	}
	return true;
}
bool fibon_sequence(int pos)
{
	if (pos <= 0 || pos > 1024)
	{
		cout << "invalid position: " << pos << "-- cannot handle request!" << endl;
		return false;
	}
	cout << "The Fibonacci sequence for " << pos << " position:" << endl;
	if (pos == 1)
	{
		cout << "1"<<endl; return true;
	}
	else
	if (pos == 2)
	{
		cout << "1" << " 1" << endl; return true;
	}
	else
	{
		int elem;
		int n_2 = 1, n_1 = 1;
		cout << "1" << " 1" << " ";
		for (int i = 3; i <= pos; ++i)
		{
			elem = n_2 + n_1;
			n_1 = n_2; n_2 = elem;
			cout << elem << ' ';
		}
		cout << endl;
		return true;
	}
}
int main()
{
	while (true){   //用无限循环,告诉用户当输入0时,退出。
		int pos;
		cout << "please enter a position:";
		cin >> pos;
		if (pos == 0)
		{
			cout << "thank you~goodbye" << endl;
			break;
		}
		int elem;
		if (fibon_elem(pos, elem))
			cout << "element " << pos << " is " << elem << endl;
		else
			cout << "sorry,could not calculate element " << pos << endl;

		fibon_sequence(pos);
		cout << "if you want stop, just enter 0"<<endl;
	}
	system("pause");
}

练习2.2

#include <iostream>
#include <vector>
#include <string>
using namespace std;
void pentagonal(const int num,vector<int>&vec)
{
	if (num <= 0 || num > 1024)
	{
		cout << "Oops:requested num is not supported" << endl;
		return;
	}
	for (int i = 1; i <= num; ++i)
	{
		int sum = (i*(3 * i - 1)) / 2;
		vec.push_back(sum);

	}
}
void print(vector<int>&vec, const string &str)
{
	cout << "The Pentagonal sequence is: ";
	for (int i = 0; i < vec.size(); ++i)
	{
		cout << vec[i] << ' ';
	}
	cout << "The type of vector is: " << str << endl;
}

int main()
{
	int number;
	vector<int>vec;
	string str = "int";
	cout << "Please enter a number: ";
	cin >> number;
	
	pentagonal(number,vec);
	
	if (number<=1024 && number>0)    //如果输入的数不合理,就不让第二个打印函数输出任何内容了
	    print(vec, str);

	system("pause");
	return 0;
}

练习2.3

将pentagonal拆成两个,在检查函数前加inline就是内联函数咯。

inline bool CheckNum(const int num)
{
	if (num <= 0 || num > 1024)
	{
		cout << "Oops:requested num is not supported" << endl;
		return false;
	}
	return true;
}

void pentagonal(const int num,vector<int>&vec)
{
	if (!CheckNum)
	{
		return;
	}
	for (int i = 1; i <= num; ++i)
	{
		int sum = (i*(3 * i - 1)) / 2;
		vec.push_back(sum);

	}
}

练习2.4

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

const vector<int>*      //这步操作很关键。返回类型为const指针。
pentagonal(int num)
{
	static vector<int> vec1;
	if (num <= 0 || num > 1024)
	{
		cout << "Oops:requested num is not supported" << endl;
		return 0 ;
	}
	for (int i = 1; i <= num; ++i)
	{
		int sum = (i*(3 * i - 1)) / 2;
		vec1.push_back(sum);
	}
	return &vec1;
}

void BackNum(int num)
{
	const vector<int> *vec = pentagonal(num);        //将pentagonal函数返回的值赋予*vec  即 *vec=&vec1
	cout << "The value of this number in Pentagonal sequence is: ";
	cout << (*vec)[num-1] << endl;    //记得此时vec是指针,需要用 (*vec)。
	
}

int main()
{
	int number;
	cout << "Please enter a number: ";
	cin >> number;
	
	pentagonal(number);
	
	if (number<=1024 && number>0)
		BackNum(number);

	system("pause");
	return 0;
}

练习2.5

就是明白overload函数的定义方式就行了

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

void max(int, int)
{}
void max(float, float)
{}
void max(string &, string&)
{}
int main()
{
	int num1, num2;
	float num3, num4;
	string str1, str2;
	cin >> num1 >> num2 >> num3 >> num4 >> str1 >> str2;
	max(num1, num2);
	max(num3, num4);
	max(str1, str2);
	system("pause");
}

猜你喜欢

转载自blog.csdn.net/weixin_43184615/article/details/82793395