Project 4-positive integer

Source: Internet
Author: User

[Cpp]/** Copyright (c) 2013, School of Computer Science, Yantai University * All rights reserved. * file name: test. cpp * Author: Fan Lulu * Completion Date: July 15, March 23, 2013 * version: v1.0 ** input Description: none * Problem description: * program output: * Problem Analysis: * Algorithm Design: slightly */# 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, false void p is returned. RintFactor (); // all factors of output data member n, including bool isPerfect () of n itself; // determine whether the number of data member n is full. If the sum of all the factors less than n of a positive integer n is equal to n, n is called the complete number. For example, = 1 + 2 + 3 is the complete number. Bool isReverse (int x); // determines whether the form parameter x is the inverse number of data member n (in example, the inverse number ). Bool isDaffodil (int x); // determines whether the form parameter x is the number of daffodils. The numbers of daffodils are equal to or equal to this number, for example, 1*1*1 + 5*5*5 + 3*3*3 void printDaffodils (); // display all the daffodils greater than 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 (29); cout <nn. getValue () <"has the following factors:"; nn. printFactor (); nn. setValue (6); cout <nn. getValue () <(nn. isPerfect ()? "Yes": "not") <"full number" <endl; nn. setValue (37); cout <nn. getValue () <(nn. isPerfect ()? "Yes": "not") <"full number" <endl; nn. setValue (1237); int x = 732; cout <nn. getValue () <"and" <x <"" <(nn. isReverse (x )? "Yes": "not") <"reverse number" <endl; cout <"All greater than and less than data member" <nn. getValue () <"the numbers of daffodils are:"; nn. printDaffodils (); system ("PAUSE") ;}// define each member function void NaturalNumber: setValue (int x) in the following class) // set the value of data member n to determine whether it is a positive integer {n = x;} int NaturalNumber: getValue () // return the value of private data member n {return n ;} bool NaturalNumber: isPrime () // checks whether the data member n is a prime number. true is returned; otherwise, false {bool prime = true; for (int I = 2; I <= sqrt (double (n); I ++) {if (n % I = 0) {prime = false; Break;} return prime;} void NaturalNumber: printFactor () // all factors of output data member n, including and n itself {for (int I = 1; I <= n; I ++) if (n % I = 0) cout <I <''; cout <endl;} bool NaturalNumber: isPerfect () // determine whether n is a full number of data members. If the sum of all the factors less than n of a positive integer n is equal to n, n is called the complete number. For example, = 1 + 2 + 3 is the complete number. {Bool perfect = false; int s = 1; for (int I = 2; I <n; I ++) if (n % I = 0) s = s + I; if (n = s) perfect = true; return perfect;} bool NaturalNumber: isReverse (int x) // determine whether the form parameter x is the inverse number of data member n (in example, the inverse number ). {Bool reverse = false; int s = 0; while (x> 0) {s = s * 10 + x % 10; x = x/10 ;} if (n = s) reverse = true; return reverse;} bool NaturalNumber: isDaffodil (int x) // determine whether the form parameter x is the number of daffodils. The number cubes of the daffodils are equal to this number, for example, = 1*1*1 + 5*5*5 + 3*3*3 {bool daffodil = false; int s = 0, p = x; int m; while (p> 0) {m = p % 10; s = s + m * m; p = p/10;} if (x = s) daffodil = true; return daffodil;} void NaturalNumber: printDaffodils () // display all values greater, and less than the number of daffodils of data member n; {for (int I = 2; I <n; ++ I) if (isDaffodil (I )) cout <I <""; cout <endl; return ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.