cpp: Narcissistic in C++11

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 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

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// 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

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// 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);
    }
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// 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;
 
        }
    }
 
 
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
     * @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);
    }

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
//水仙花数 100-1000  实体类
//geovin.getInfo();
geovin.getInfo().ShowInfo();
 
//
//geovin.DisplayNarcissicticInfos();
 
//水仙花数 100-1000
geovin.DisplayNarcissisticNumber();
 
//敲桌子
geovin.DisplayDeskKnock(100);

  

 

posted @   ®Geovin Du Dream Park™  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2013-04-11 Sql: 請假跨月份問題,或跨年份問題 日期部分边界
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示