- /*
- * 文件名称:Ex5-2.cpp
- * 作 者:徐浩宁
- * 完成日期:2017 年 4 月 24 日
- * 版 本 号:v1.0
- * 对任务及求解方法的描述部分: 设计一个“正整数”类,并通过一系列的成员函数对其性质进行做出判断或列出相关联的数值。下面给出类声明,请实现各成员函数。另外,模仿已经给出的main()函数,完成你所设计的各个成员函数的测试。
- * 输入描述:
- * 问题描述:
- * 程序输出:
- * 问题分析:
- * 算法设计:
- #include<iostream>
using namespace std;
class NaturalNumber
{private:
int n;
public:
void setValue (int x);
int getValue();
bool isPrime();
void printFactor();
bool isPerfect();
bool isReverse(int x);
bool isDaffodil(int x);
void print Daffodils();
};
void main(void)
{
NaturalNumber nn;
nn.setValue (6);
cout<<nn.getValue()<<(nn.isPrime()?”是”:”不是”)<<”素数” <<endl;
nn.setValue (37);
cout<<nn.getValue()<<(nn.isPrime()?”是”:”不是”)<<”素数” <<endl;
nn.setValue (84);
cout<<nn.getValue()<<”的因子有:”;
printFactor();
}
- </pre><br>
<pre></pre>
<pre class="plain" name="code">
{private:
int n;
public:
void setValue (int x);
int getValue();
bool isPrime();
void printFactor();
bool isPerfect();
bool isReverse(int x);
bool isDaffodil(int x);
void printDaffodils();
};</pre><pre class="cpp" name="code">
<pre class="cpp" name="code">#include<iostream>
#include <Cmath>
#include "NaturalNumber.h"
using namespace std;
void main(void)
{
NaturalNumber nn;
nn.setValue (6);
cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;
nn.setValue (37);
cout<<nn.getValue()<<(nn.isPrime()?"是":"不是")<<"素数" <<endl;
nn.setValue (84);
cout<<nn.getValue()<<"的因子有:";
nn.printFactor();
system("PAUSE");
}</pre><pre class="cpp" name="code">
<pre class="cpp" name="code">#include<iostream>
#include <Cmath>
#include "NaturalNumber.h"
using namespace std;
void NaturalNumber::setValue (int x)
{
cout << "请输入一个数据成员的值:";
cout<< x << endl;
if (x > 0)
{
cout << "数据为正整数!"<<endl;
n = x;
}
else
{
cout << "数据不是正整数!请重新输入:"<< endl;
}
}
int NaturalNumber::getValue()
{
return n;
}
bool NaturalNumber::isPrime()
{
bool pri = true;
for (int i = 2;i <= sqrt(x);i++)
{
if( n % i == 0)
{
pri = false;
break;
}
}
return pri;
}
void NaturalNumber::printFactor()
{
for(int i = 1;i <= n;i++)
{
if( n % i == 0)
{
cout << i << " ";
}
}
cout << endl;
}
bool NaturalNumber::isPerfect()
{
int s = 0;
for(int i = 1;i < n;i++)
{
if( n % i == 0)
{
s = s + i;
}
}
if(n == s)
{
cout << "n为完全数!"<< endl;
}
else
{
cout << "n不是完全数!"<< endl;
}
return 0;
}
bool NaturalNumber::isReverse(int x)
{
int t,s = 0;
while(x > 0)
{
t = x % 10;
s = s + t;
x = x / 10;
}
if (n == x)
{
cout << "形式参数x是数据成员n的逆向数"<< endl;
}
else
{
cout << "形式参数x不是数据n的逆向数"<< endl;
}
return 0;
}
bool NaturalNumber::isDaffodil(int x)
{
int t,s = 0;
while(x > 0)
{
t = x % 10;
s = s + t * t * t;
x = x / 10;
}
if (n == s)
{
cout << "形式参数x是水仙花数"<< endl;
}
else
{
cout << "形式参数x不是水仙花数"<< endl;
}
return 0;
}
void NaturalNumber::printDaffodils()
{
int i,t,s = 0;
for(i = 1;i < n;i++)
{
while(i > 0)
{
t = i % 10;
s = s + t * t *t;
i = i / 10;
}
if(s == i)
{
cout << "1--n的水仙花数有:"<< i << " ";
}
}
}
</pre><pre class="cpp" name="code"><img src="http://hi.csdn.net/attachment/201203/14/0_13317173344u24.gif" alt="">
</pre><br>
<pre></pre>
</pre> - 二.运算结果
-
- 三.心得体会
- 无
- 四.知识点总结
- 无
c++第五次实验
最新推荐文章于 2024-05-28 09:35:56 发布