c++第五次实验 项目二 正整数类
来源:互联网 发布:城乡居民收入差距数据 编辑:程序博客网 时间:2024/06/05 03:11
一、问题及代码
/* * 文件名称:c++第5次实验报告 项目 2 正整数类 * 作者: 刘祎洋 * 完成日期: 2017.4.24 * 版 本 号:5.11 * 对任务及求解方法的描述部分: 设计一个“正整数”类,并通过一系列的成员函数对其性质进行做出判断或列出相关联的数值。 * 输入描述: * 问题描述: * 程序输出:* 问题分析: * 算法设计: */ #include<iostream> using namespace std; class NaturalNumber {private: int n; public: void setValue (int x);//置数据成员n的值,要求判断是否是正整数 int getValue(); //返回私有数据成员n的值 bool isPrime(); //判断数据成员n是否为素数,是返回true,否则返回false void printFactor(); //输出数据成员n的所有因子,包括1和n自身 bool isPerfect(); //判断数据成员n是否为完全数。若一个正整数n的所有小于n的因子之和等于n, 则称n为完全数, 如6=1+2+3是完全数。 bool isReverse(int x);//判断形式参数x是否为数据成员n的逆向数(例321是123的逆向数)。 bool isDaffodil(int x); //判断形式参数x是否是水仙花数。水仙花数的各位数字立方和等于该数,如153=1*1*1+5*5*5+3*3*3 void printDaffodils(); //显示所有大于1,且小于数据成员n的水仙花数; }; 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(); cout << endl; nn.setValue(6); cout << nn.getValue() << (nn.isPerfect()?"是":"不是") << "完全数" << endl; nn.setValue(12); cout << nn.getValue() << (nn.isPerfect()?"是":"不是") << "完全数" << endl; nn.setValue(123); cout << "321" << (nn.isReverse(321)?"是":"不是") << nn.getValue() << "的逆向数" << endl; nn.setValue(123); cout << "123" << (nn.isReverse(123)?"是":"不是") << nn.getValue() << "的逆向数" << endl; nn.setValue(153); cout << nn.getValue() << (nn.isDaffodil(153)?"是":"不是") << "水仙花数" << endl; nn.setValue(36); cout << nn.getValue() << (nn.isDaffodil(30)?"是":"不是") << "水仙花数" << endl; nn.setValue(200); cout << nn.getValue() <<" 以内的水仙花数有:"; nn.printDaffodils(); system("PAUSE"); //随着成员函数的实现,增加代码以完成相关的测试。注意判断类的成员函数需要测试是或否两种情况…… } //请在下面定义类中的各个成员函数 void NaturalNumber::setValue(int x)//判断是否为正整数 { if(x > 0) { n = x; } return ; } int NaturalNumber::getValue()//得到该整数的值 { return n; } bool NaturalNumber::isPrime()//判断是不是素数 { bool prime = true; int i = 2 ; while(i < n) { if(n % i == 0) { prime = false; break; } ++i; } return prime; } void NaturalNumber::printFactor()//输出所有因子 { int i; for(i = 1; i <= n; ++i) { if(n % i == 0) { cout << i << '\t' ; } } return; } bool NaturalNumber::isPerfect()//判断是否是完全数 { bool perfect = false; int i,s = 0; for(i = 1;i < n; ++i) { if(n % i == 0) { s = s + i; } } if(s == n) { perfect = true; } return perfect; } bool NaturalNumber::isReverse(int x)//判断是否是逆向数 { bool reverse = false; int s = 0; while(x > 0)//转换成其逆向数 { s = s * 10 + x % 10; x = x / 10; } if(s == n) { reverse = true; } return reverse; } bool NaturalNumber::isDaffodil(int x)//判断是否是水仙花数 { bool daffodil = false; int s = 0, d, p = x; while(x > 0) { d = x % 10; s = s + d * d * d; x = x / 10; } if(p == s) { daffodil = true; } return daffodil; } void NaturalNumber::printDaffodils()//输出水仙花数 { int i; for(i = 2; i < n; ++i) { if(isDaffodil(i)) { cout << i << " " ; } } cout << endl; return ; }二、运行结果
三、心得体会
刚开始对类这一块感觉很陌生,但是经过这道题练习,对这一块的知识有了更深入的认识和了解。
四、知识点归纳
1、类的实现
2、如何判断素数、完全数以及水仙花数
0 0
- c++第五次实验 项目二 正整数类
- c++第五次实验-正整数类
- C++第五次实验——项目二
- C++第五次实验——项目二
- c++第五次实验项目一
- C++第五次实验 项目2
- 补作业 第五次实验 项目5
- 补作业 第五次实验 项目4
- 补做-第五次实验-项目四
- 补做- 第五次实验-项目五
- c++第五次实验—项目一
- 第六次实验:项目二
- 第五次C语言上机实验报告
- 第五次C程序设计实验报告
- 第五次C语言上机实验报告
- C语言第五次上机实验
- c++第五次上机实验--项目一、项目五
- c++第五次实验(三角形类)
- 实现搜索框(含历史搜索记录)
- TextView加粗、删除线、下划线
- 2017第九届中国云计算大会早鸟票报名
- mysql_内部函数
- Angular---作用域与事件
- c++第五次实验 项目二 正整数类
- Android 意图(Intent)和过滤器(Filter)
- JAVA 编辑工具JCreator的环境配置说明
- eclipse中如何在当前工程中查找一个字符串
- 声明式事务(XML)【理解】【重点】
- HTML-from表单
- Linux之ACL权限
- ROS Indigo安装和配置
- Valid Parentheses