01. /* 02. * copyright and version Declaration of the program part 03. * Copyright (c) 2013, Yantai University Computer college student 04. * All rightsreserved. 05. * file name: NaturalNumber. cpp 06. * Author: Zhao guanzhe 07. * Completion Date: April 8, March 22, 2013. * version: v1.0 09. * input Description: 10. * Problem description: 11. */# include <iostream> # include <cmath> using namespace std; class NaturalNumber {private: int n; public: void setValue (int x ); // set the value of data member n to determine whether it is a positive integer int getValue (); // return the value of private data member n bool isPrime (); // determine whether the data member n is a prime number. true is returned; otherwise, true is returned. Falsevoid printFactor (); // all factors of output data member n, including 1 and n bool isPerfect (); // determine whether data member n is a full number. If the sum of all the factors less than n of a positive integer n is equal to n, n is called a complete number. For example, 6 = 1 + 2 + 3 is a complete number. Bool isReverse (); // determines whether the form parameter x is the inverse number of data member n (for example, 321 is the inverse Number of 123 ). Bool isDaffodil (); // determines whether the form parameter x is the number of daffodils. The numbers of daffodils are equal to or equal to the numbers, such as 153 = 1*1*1 + 5*5*5 + 3*3 // void printDaffodils (void ); // display all the daffodils greater than 1 and less than the data member n;}; void main (void) {NaturalNumber nn; // defines an instance (object) nn of the class. setValue (6); cout <nn. getValue () <(nn. isPrime ()? "Yes": "not") <"prime number! "<Endl; nn. setValue (37); cout <nn. getValue () <(nn. isPrime ()? "Yes": "not") <"prime number! "<Endl; nn. setValue (84); cout <nn. getValue () <"has the following factors:"; nn. printFactor (); nn. setValue (6); cout <nn. getValue () <(nn. isPerfect ()? "Yes": "not") <"count! "<Endl; nn. setValue (123); cout <nn. getValue () <(nn. isReverse ()? "Yes": "not") <"reverse number! "<Endl; nn. setValue (153); cout <nn. getValue () <(nn. isDaffodil ()? "Yes": "no") <"daffodils! "<Endl; nn. setValue (1000); cout <nn. getValue () <"the numbers of inner daffodils are:"; nn. printDaffodils (); // nn. isPerfect (6); // cout <nn. getValue (); // with the implementation of member functions, add code to complete related tests. Note: To judge the member functions of a class, you need to test whether or not ......} // Define each member function void NaturalNumber: setValue (int x) {if (x % 1 = 0) & (x> 0) in the following class )) {cout <x <"is a positive integer! "<Endl; n = x;} else {cout <x <" is not a positive integer! "<Endl ;}} int NaturalNumber: getValue () {return n;} bool NaturalNumber: isPrime () {int m; for (m = 2; m <n; m ++) {if (n % m = 0) return false; else return true ;}} void NaturalNumber: printFactor () {int I; for (I = 1; I <n; I ++) {if (n % I) = 0) cout <I <";}} bool NaturalNumber: isPerfect () {int I, s; s = 0; for (I = 1; I <n; I ++) {if (n % I) = 0) s = s + I;} if (s = n) return true; else return false;} bool NaturalNumber: isReverse () {int a, B, c, d; cout <"enter a three-digit number:"; cin> a; B = a/100; c = a/10-b * 10; d = a % 10; if (d * 100 + c * 10 + B) = n) return true; else return false;} bool NaturalNumber: isDaffodil () {int B, c, d; B = n/100; c = n/10-b * 10; d = n % 10; if (n = B * B + c * c + d * d) return true; else return false;} void NaturalNumber: printDaffodils () {int a, B, c, d; for (a = 2; a <n; a ++) {B = a/100; c = a/10-b * 10; d = a % 10; if (a = B * B + c * c + d * d) cout <a <"";} cout <endl; return ;}
Running result: