Lab report for week 4 (iii)

Source: Internet
Author: User
01. # include <iostream> 02. 03. using namespace STD; 04. 05. class naturalnumber 06. {07. PRIVATE: 08. int N; 09. public: 10. void setvalue (int x); // sets the value of data member N, and determines whether the value is positive 11. int getvalue (); // returns the value of private data member N 12. bool isprime (); // determines whether the data member N is a prime number. True is returned; otherwise, false 13 is returned. void printfactor (); // all factors of output data member N, including and N itself 14. bool isperfect (); // determines 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. 15. // bool isreverse (int x); // determine whether the form parameter X is the inverse number of data member N (in example, the inverse number ). 16. bool isdaffodil (int x); // judge whether the form parameter X is the number of daffodils. Numbers of daffodils Cube And equal to this number, for example, = 1*1*1 + 5*5*5 + 3*3*3 17. void printdaffodils (); // display all daffodils larger than and less than data member N; 18 .}; 19. 20. void main (void) 21. {22. naturalnumber nn; // defines an instance (object) of the Class 23. nn. setvalue (6); 24. cout <NN. getvalue () <(NN. isprime ()? "Yes": "not") <"prime number" <Endl; 25. 26. nn. setvalue (37); 27. cout <NN. getvalue () <(NN. isprime ()? "Yes": "not") <"prime number" <Endl; 28. 29. nn. setvalue (84); 30. cout <NN. getvalue () <"has the following factors:"; 31. nn. printfactor (); 32. 33. nn. setvalue (6); 34. cout <NN. getvalue () <(NN. isperfect ()? "Yes": "not") <"Full count" <Endl; 35. 36. nn. setvalue (1, 352); 37. cout <NN. getvalue () <(NN. isperfect ()? "Yes": "not") <"Full count" <Endl; 38. 39. nn. setvalue (1, 968); 40. cout <"All greater than and less than data members" <NN. getvalue () <"the numbers of daffodils are:"; 41. nn. printdaffodils (); 42. 43. system ("pause"); 44 .} 45. 46. 47. // define the member functions in the class below. //// set the value of data member N to determine whether it is a positive integer 49. void naturalnumber: setvalue (int x) 50. {51. N = x; 52 .} 53. 54. // return the value of private data member N 55.int naturalnumber: getvalue () 56. {57. return N; 58 .} 59. 60. // determine whether the data member N is a prime number. True is returned; otherwise, false 61.bo is returned. Ol naturalnumber: isprime () 62. {63. int I; 64. 65. for (I = 2; I <= n/2; I ++) 66. {67. if (N % I = 0) 68. {69. return false; 70. break; 71 .} 72. else 73. return true; 74 .} 75 .} 76. 77. // all factors of output data member N, including and N itself 78. void naturalnumber: printfactor () 79. {80. for (INT I = 1; I <= N; I ++) 81. {82. if (N % I = 0) 83. cout <I <""; 84. 85 .} 86. cout <Endl; 87. return; 88 .} 89. // determine whether the form parameter X is 90 daffodils. bool naturalnumbe R: isperfect () 91. {92. int I, S = 0, T = 0; 93. for (I = 1; I <= (n/2); I ++) 94. {95. if (N % I = 0) 96. {97. S = S + I; 98 .} 99 .} 100. if (S = N) 101. return true; 102. else 103. return false; 104 .} 105. 106. // determine whether N is a full number of data members. 107. bool naturalnumber: isdaffodil (int x) 108. {109. int T = 0, M, A; 110. M = x; 111. while (M> 0) 112. {113. A = m % 10; 114. T = T + A * a * A; 115. M = m/10; 116 .} 117. if (t = x) 118. return true; 119. else 120. return false; 121 .} 122. 123. // display all the daffodils greater than and less than the data member N 124. void naturalnumber: printdaffodils () 125. {126. inti; 127. for (I = 1; I <n; I ++) 128. {129. if (isdaffodil (I) 130. cout <I <""; 131 .} 132. cout <Endl; 133. 134 .}

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.