cpp: Narcissistic in C++11

 

// NarcissisticList.h : 
//练习案例:水仙花数 100 - 1000  NarcissisticList
//案例描述:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身
//例如:1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
//请利用do...while语句,求出所有3位数中的水仙花数// 2023年4月5日 涂聚文 Geovin Du edit. c++ 11
//
// 
//

#pragma once
#ifndef NARCISSISTICLIST_H 
#define NARCISSISTICLIST_H 

#include <iostream> 
#include <string> 
#include <vector> 
#include <memory> 
#include <iostream> 
#include <string> 
#include <list>

#include "NarcissisticInfo.h"


namespace geovindu
{

	/**
	 * @brief  水仙花数集合类.
	 * 水仙花数集合类.
	 */
	class NarcissisticList
	{

	private:

		list<NarcissisticInfo> NarcissisticInfos;



	public:

		/**
		 * @brief.
		 * 
		 * \param narcissisticInfos
		 */
		void setNarcissisticList(list<NarcissisticInfo> narcissisticInfos)
		{
			this->NarcissisticInfos = narcissisticInfos;
		}
		/**
		 * @brief.
		 * 
		 * \return 
		 */
		list<NarcissisticInfo> getNarcissisticInfo()
		{
			return NarcissisticInfos;
		}
		/**
		 * @brief.
		 * 
		 * \param narcissisticInfos
		 */
		//NarcissisticList(list<NarcissisticInfo> narcissisticInfos)
		//{
			//this->NarcissisticInfos = narcissisticInfos;
		//}
		/**
		 * @brief.
		 * 
		 */
		void ShowInfo();
	};

}


#endif

  

// NarcissisticInfo.h : 
//练习案例:水仙花数 100 - 1000  NarcissisticNumber
//案例描述:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身
//例如:1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
//请利用do...while语句,求出所有3位数中的水仙花数
// 2023年4月5日 涂聚文 Geovin Du edit.
// https://mathworld.wolfram.com/NarcissisticNumber.html
//

#pragma once
#ifndef NARCISSISTICINFO_H
#define NARCISSISTICINFO_H

#include <iostream>
#include<string.h>
#include<math.h>

using namespace std;


/**
 * @brief 
 * \author geovindu.
 * \date 20230-4-10
 */
namespace geovindu
{

	/**
	 * @brief 水仙花数 实体类.
	 */
	class NarcissisticInfo
	{

	private:

		string NarcissisticName;

		int NarcissisticDigit;

		

	public:




		NarcissisticInfo(string name,int digit)
		{
			this->NarcissisticName = name;
			this->NarcissisticDigit = digit;
		}

		NarcissisticInfo(const NarcissisticInfo& child) {
			this->NarcissisticName = child.NarcissisticName;
			this->NarcissisticDigit = child.NarcissisticDigit;
		}
       /*
		NarcissisticInfo& operator=(const NarcissisticInfo& child) {//第一个&表明返回值是计算结果的引用,第二个&表明使用c的引用,const表明不会改变c
			this->NarcissisticName = child.NarcissisticName;
			this->NarcissisticDigit = child.NarcissisticDigit;
			return *this;//返回对象的引用(如果是前面不是NarcissisticInfo &而是NarcissisticInfo,那么返回的就是对象的拷贝)
		}
		*/
	



		void setNarcissisticName(string narcissisticName)
		{
			this->NarcissisticName = narcissisticName;
		}


		void setNarcissisticDigit(int narcissisticDigit)
		{
			this->NarcissisticDigit = narcissisticDigit;
		}


		string getNarcissisticName()
		{
			return NarcissisticName;
		}

		int getNarcissisticDigit()
		{
			return NarcissisticDigit;
		}

		//void ShowInfo();


	};
	/**
	 * @brief 水仙花数显示.
	 * 
	 */
	//void NarcissisticInfo::ShowInfo()
	//{

		//cout << "名称:" << this->getNarcissisticName() << ",水仙花数:" << this->getNarcissisticDigit() << endl;
	//}

}




#endif

  

// NarcissisticNumber.h : 
//练习案例:水仙花数 100 - 1000  NarcissisticNumber
//案例描述:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身
//例如:1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
//请利用do...while语句,求出所有3位数中的水仙花数// 2023年4月5日 涂聚文 Geovin Du edit.
// 
//

#pragma once
#ifndef NARCISSISTICNUMBER_H 
#define NARCISSISTICNUMBER_H 

#include <iostream> 
#include <string> 
#include <vector> 
#include <memory> 
#include <iostream> 
#include <string> 
#include "NarcissisticInfo.h"
#include "NarcissisticList.h"


using namespace std;


/**
 * @brief
 * \author geovindu.
 * \date 20230-4-10
 */
namespace geovindu
{

	/**
	 * @brief 水仙花数.
	 */
	class NarcissisticNumber
	{
	private:

		//typedef list<NarcissisticInfo> getlist;

	public:


		static int countA;
		/**
		 * @brief 水仙花数.
		 * 
		 */
		void setNarcissisticNumber();
		/**
		 * @brief .
		 * 
		 * \return 
		 */
		NarcissisticInfo GetNarcissisticInfo();
		/**
		 * @brief.
		 * 
		 * \param name
		 * \param digit
		 * \return 
		 */
		NarcissisticInfo GetNarcissisticInfo(string name, int digit);

		/**
		 * @brief 水仙花数.
		 *
		 * \return 返回实体类集合
		 */
		NarcissisticList setNarcissisticInfoList();


		/**
		 * @brief 水仙花数.
		 * 指定范围内的整数中的水仙花数
		 * \param min 输入参数 【int】 范围内的最小整数
		 * \param max 输入参数 【int】 范围内的最大整数
		 */
		void setNarcissisticNumber(int min,int max);
		/**
		 * @brief 水仙花数.
		 * 指定范围内的整数中的水仙花数
		 * \param min 输入参数 【int】 范围内的最小整数
		 * \param max 输入参数 【int】 范围内的最大整数
		 * \return 返回实体类集合
		 */
		NarcissisticList setNarcissisticInfoList(int min, int max);
		/**
		 * @brief .水仙花数
		 * 判断是否是水仙花数
		 * \param number 输入参数 【int】 整数
		 * \return 返回是否为真
		 */
		bool IsNarcissisticNumber(int number);
		/**
		 * @brief 水仙花数.
		 * function to count digits
		 * \param number 输入参数 【int】 整数
		 * \return 返回位数
		 */
		int NarcissisticCountDigit(int number);
		/**
		 * @brief 水仙花数.
		 * Returns true if n is Narcissistic number
		 * \param number 输入参数 【int】 整数
		 * \return  返回是否为真
		 */
		bool NarcissisticCheck(int number);
		/**
		 * @brief.
		 * 
		 * \param n
		 * \return 
		 */
		int NarcissisticCube(const int n);
		/**
		 * @brief
		 * .
		 */
		bool IsNarcissistic(const int n);
	};
}

#endif

  

// NarcissisticNumber.h : 
//练习案例:水仙花数 100 - 1000  NarcissisticNumber
//案例描述:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身
//例如:1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
//请利用do...while语句,求出所有3位数中的水仙花数
// 2023年4月5日 涂聚文 Geovin Du edit.
// https://mathworld.wolfram.com/NarcissisticNumber.html
//


#include "NarcissisticNumber.h"
#include <iostream> 
#include <string> 
#include <vector> 
#include <memory> 
#include <iostream> 
#include <list> 


using namespace std;

/**
 * @brief
 * \author geovindu.
 * \date 20230-4-10
 */
namespace geovindu
{

	/**
	 * @brief 计录个数
	 * .
	 */
	int NarcissisticNumber::countA;

	/**
	 * @brief 水仙花数.
	 * 100至1000内的三位数的水仙花数
	 * 153  370  371  407 
	 */
	void NarcissisticNumber::setNarcissisticNumber()
	{
		int i = 100;
		int min = 100;
		int max = 1000;
		int countA = 0;
		do
		{
			if (NarcissisticCheck(i)) //IsNarcissisticNumber
			{
				cout << "" << i <<"是水仙花数"<< endl;
				countA++;
			}
			i++;

		} while (i >= min && i < max);
	
		cout << "水仙花数有【" << countA <<"】个数。" << endl;
	}

	/**
	 * @brief 水仙花数.
	 * 100至1000内的三位数的水仙花数
	 * 153  370  371  407
	 */
	NarcissisticInfo NarcissisticNumber::GetNarcissisticInfo() {

		NarcissisticInfo info("水仙花数", 153);

		return info;
	}
	/**
	 * @brief 水仙花数.
	 * 
	 * \param name
	 * \param digit
	 * \return 
	 */
	NarcissisticInfo NarcissisticNumber::GetNarcissisticInfo(string name, int digit) {

		NarcissisticInfo info(name, digit);

		return info;
	}
	/**
	 * @brief 水仙花数.
	 * 
	 * \return 返回实体类集合
	 */	
	
	NarcissisticList NarcissisticNumber::setNarcissisticInfoList()
	{
		int i = 100;
		int min = 100;
		int max = 1000;
		//int countA = 0;
		list<NarcissisticInfo> list;
		//NarcissisticInfo info;
		NarcissisticList nlist;
		do
		{
			if (NarcissisticCheck(i)) //IsNarcissisticNumber
			{
				//cout << "" << i << "是水仙花数" << endl;
				//info.setNarcissisticDigit(i);
				//info.setNarcissisticName("水仙花数");
				NarcissisticInfo info("水仙花数", i);
				list.push_back(info);

				countA++;
			}
			i++;

		} while (i >= min && i < max);

		nlist.setNarcissisticList(list);
		cout << "实体类操作,水仙花数有【"  << countA << "】个数。" << endl;
		return nlist;
	}
	
	 /**/
	/**
	 * @brief 水仙花数.
	 * 指定范围内的整数中的水仙花数
	 * \param min 输入参数 【int】 范围内的最小整数
	 * \param max 输入参数 【int】 范围内的最大整数
	 */
	void NarcissisticNumber::setNarcissisticNumber(int min, int max)
	{
		int i = min;
		//int countA = 0;
		do
		{
			if (NarcissisticCheck(i)) //  IsNarcissisticNumber
			{
				cout << "" << i << "是水仙花数" << endl;
				countA++;
			}
			i++;

		} while (i >= min && i < max);
		cout << "水仙花数有【" << countA << "】个数。" << endl;
	}
	/**
	 * @brief 水仙花数.
	 * 指定范围内的整数中的水仙花数
	 * \param min 输入参数 【int】 范围内的最小整数
	 * \param max 输入参数 【int】 范围内的最大整数
	 * \return 返回实体类集合
	 */
	
	NarcissisticList NarcissisticNumber::setNarcissisticInfoList(int min, int max)
	{
		int i = min;
		//int countA = 0;

		static list<NarcissisticInfo> list;
		//NarcissisticInfo info;
		NarcissisticList nlist;
		do
		{
			if (NarcissisticCheck(i)) //IsNarcissisticNumber
			{
				//cout << "" << i << "是水仙花数" << endl;
				//info.setNarcissisticDigit(i);
				//info.setNarcissisticName("水仙花数");
				NarcissisticInfo info("水仙花数", i);
				list.push_back(info);

				countA++;
			}
			i++;

		} while (i >= min && i < max);
		nlist.setNarcissisticList(list);
		return nlist;
	}

	/**
	 * @brief .水仙花数
	 * 判断是否是水仙花数
	 * \param number 输入参数 【int】 整数
	 * \return 返回是否为真
	 */
	bool NarcissisticNumber::IsNarcissisticNumber(int number)
	{
		bool isok = false;

		int original, rem, sum = 0, digit = 0;

		original = number;
		/* Counting number of digit in a given number */
		while (number != 0)
		{
			digit++;
			number = number / 10;
		}

		/* After execution above loop number becomes 0
		   So copying original number to variable number */

		number = original;
		/* Finding sum */
		while (number != 0)
		{
			rem = number % 10;
			sum = sum + pow(rem, digit);
			number = number / 10;
		}
		/* Making decision */
		if (sum == original)
		{
			//printf("%d is ARMSTRONG.", original);
			isok = true;
		}
		else
		{
			//printf("%d is NOT ARMSTRONG.", original);
			isok = false;
		}

		return isok;
	}
	/**
	 * @brief 水仙花数.
	 * function to count digits
	 * \param number 输入参数 【int】 整数
	 * \return 返回位数
	 */
	int NarcissisticNumber::NarcissisticCountDigit(int number)
	{
		if (number == 0)
			return 0;

		return 1 + NarcissisticCountDigit(number / 10);
	}

	/**
	 * @brief 水仙花数.
	 * Returns true if n is Narcissistic number
	 * \param number 输入参数 【int】 整数
	 * \return  返回是否为真
	 */
	bool NarcissisticNumber::NarcissisticCheck(int number)
	{
		// count the number of digits
		int l = NarcissisticCountDigit(number);
		int dup = number;
		int sum = 0;

		// calculates the sum of digits
		// raised to power
		while (dup)
		{
			sum += pow(dup % 10, l);
			dup /= 10;
		}

		return (number == sum);
	}

	int NarcissisticNumber::NarcissisticCube(const int n)//求立方函数 
	{
		return n * n * n;
	}

	bool NarcissisticNumber::IsNarcissistic(const int n)//判断是否是水仙花数 
	{
		int hundreds = n / 100;
		int tens = n / 10 - hundreds * 10;
		int ones = n % 10;
		return NarcissisticCube(hundreds) + NarcissisticCube(tens) + NarcissisticCube(ones) == n;
	}

	/**
 * @brief 水仙花数.
 * function to count digits
 * \param number 输入参数 【int】 整数
 * \return 返回位数
 */
	int NarcissisticCountDigits(int number)
	{
		if (number == 0)
			return 0;

		return 1 + NarcissisticCountDigits(number / 10);
	}

	/**
	 * @brief 水仙花数.
	 * Returns true if n is Narcissistic number
	 * \param number 输入参数 【int】 整数
	 * \return  返回是否为真
	 */
	bool NarcissisticChecks(int number)
	{
		// count the number of digits
		int l = NarcissisticCountDigits(number);
		int dup = number;
		int sum = 0;

		// calculates the sum of digits
		// raised to power
		while (dup)
		{
			sum += pow(dup % 10, l);
			dup /= 10;
		}

		return (number == sum);
	}

}

  

// NarcissisticList.cpp : 
//练习案例:水仙花数 100 - 1000  NarcissisticList
//案例描述:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身
//例如:1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
//请利用do...while语句,求出所有3位数中的水仙花数// 2023年4月5日 涂聚文 Geovin Du edit.
//

#include "NarcissisticList.h"

#include <iostream> 
#include <string> 

using namespace std;


/**
 * @brief
 * \author geovindu.
 * \date 20230-4-10
 */
namespace geovindu
{

	/**
	 * @brief
	 * .
	 * 
	 */
	void NarcissisticList::ShowInfo()
	{

		for(auto info:getNarcissisticInfo())
		{ 
			cout << "实体类操作:【仙花数】:" << info.getNarcissisticName() << ",数字为:" << info.getNarcissisticDigit()<< endl;
			//cout << "个数:"<<getNarcissisticInfo().size() << endl;

		}
	}



}

  

/**
	 * @brief  水仙花数.
	 * 
	 * \return 
	 */
	NarcissisticList Geovin::getInfo()
	{

		NarcissisticNumber number;
		//NarcissisticInfo info = number.GetNarcissisticInfo("水仙花数", 153);
		//NarcissisticInfo info("水仙花数",153);
		// 
		//info.setNarcissisticDigit(39);
		//info.setNarcissisticName("");
		NarcissisticList infos = number.setNarcissisticInfoList();
		for (auto info : infos.getNarcissisticInfo())
		{
			cout << "实体类返回:" << info.getNarcissisticName() << "水仙花数" << info.getNarcissisticDigit() << endl;
		}
		cout << "共有个数:" << infos.getNarcissisticInfo().size() << "" << endl;
		return infos;
	}

	/**
	 * @brief 水仙花数.实体类
	 * 100至1000内的三位数的水仙花数
	 */
	list<NarcissisticInfo> Geovin::DisplayNarcissicticInfos()
	{
		NarcissisticNumber number;
		list<NarcissisticInfo> infos;
		//infos = number.setNarcissisticList();
		if (!infos.empty())
		{
			for (NarcissisticInfo info : infos)
			{
				cout << "" << info.getNarcissisticName() << ",水仙花数:" << info.getNarcissisticDigit() << endl;
				
			}
		}
		
		//cout << "水仙花数有【" << number.countA << "】个数。" << endl;
		return infos;
	}


	/**
	 * @brief 水仙花数.
	 * 100至1000内的三位数的水仙花数
	 */
	void Geovin::DisplayNarcissisticNumber()
	{
		NarcissisticNumber narcissistic;
		narcissistic.setNarcissisticNumber();

	}
	/**
	 * @brief 水仙花数.
	 * 指定范围内的整数中的水仙花数
	 * \param min 输入参数 【int】 范围内的最小整数
	 * \param max 输入参数 【int】 范围内的最大整数
	 */
	void Geovin::DisplayNarcissisticNumber(int min, int max)
	{
		NarcissisticNumber narcissistic;
		narcissistic.setNarcissisticNumber(min,max);
	}

  

调用:

	//水仙花数 100-1000  实体类
	//geovin.getInfo();
	geovin.getInfo().ShowInfo();

	//
	//geovin.DisplayNarcissicticInfos();

	//水仙花数 100-1000 
	geovin.DisplayNarcissisticNumber();

	//敲桌子
	geovin.DisplayDeskKnock(100);

  

 

posted @ 2023-04-11 00:31  ®Geovin Du Dream Park™  阅读(32)  评论(0)    收藏  举报