栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Essential C++练习2.3

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Essential C++练习2.3

要求:

将练习2.2的Pentagonal数列求值函数拆分为两个函数,其中之一为inline,用来检验元素个数是否合理。如果合理而且尚未被计算,便执行第二个函数,执行实际求值工作。

#include
#include
using namespace std;

inline bool calc_elems(vector& vec, int pos);
void really_calc_elems(vector& vec, int pos);
void display_elems(vector& vec, const string& title);

int main()
{
	vector pent;
	const string title("Pentagonal Numeric Series");

	if (calc_elems(pent, 0))
	{
		display_elems(pent, title);
	}

	if (calc_elems(pent, 8))
	{
		display_elems(pent, title);
	}

	if (calc_elems(pent, 14))
	{
		display_elems(pent, title);
	}

	if (calc_elems(pent, 128))
	{
		display_elems(pent, title);
	}

	system("pause");
	return EXIT_SUCCESS;
}

inline bool calc_elems(vector& vec, int pos)
{
	if (pos<=0||pos>64)
	{
		cout << endl;
		cerr << "Sorry! Invalid position: " << pos << endl;
		return false;
	}
	if (vec.size()& vec, int pos)
{
	for (unsigned i = vec.size() + 1; i <= pos; i++)
	{
		vec.push_back(i * (3 * i - 1) / 2);
	}
}
void display_elems(vector& vec, const string& title)
{
	cout << endl << title << endl;
	for (unsigned i = 0; i < vec.size(); i++)
	{
		cout << vec[i] << " ";
	}
	cout << endl;
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/768512.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号