用栈来实现分解一个数成素因子乘积的形式。
2005-04-17 13:59
423 查看
stack.h源文件:
#include <math.h>
#include <iostream.h>
const int maxentry=20;
enum error_code{fail,success,overflow,underflow};
template<class type>
class stack
{
private:
int count;
type entry[maxentry];
public:
stack()
{count=0;}
error_code push(const type &a);
error_code pop();
bool empty()const;
error_code top(type &a)const;
int numbers();
};
template<class type>
error_code stack<type>::push(const type &a)
{
error_code outcome=success;
if(count<maxentry)
{
entry[count++]=a;
}
else outcome=fail;
return outcome;
}
template<class type>
error_code stack<type>::pop()
{
error_code outcome=success;
if(count>0)
{
count--;
}
else outcome=fail;
return outcome;
}
template<class type>
bool stack<type>::empty() const
{
return count==0;
}
template<class type>
error_code stack<type>::top(type &a) const
{
error_code outcome=success;
if(count>0)
{
a=entry[count-1];
}
else outcome=fail;
return outcome;
}
template<class type>
int stack<type>::numbers()
{
return count;
}
bool isprime(int n)
{
if (n==1)
return false;
int k;
k=sqrt(n);
for (int i=2;i<=k;i++)
{
if (n%i==0)
{return false;}
if (i>=k+1)
{return true;}
else {return false;}
}
}
void prime(stack<int> num)
{
int m=2;
//int a[255]={0,};
//int i;
int n;
num.top(n);
//num.pop();
//cout<<n<<endl;
if (n==1||n==0||n<0)
{cout<<"您所输入的数没有素因子。"<<endl;return;}
while (n>1)
{
loop1:{
if (isprime(n))
{goto loop2;}
else if (n%m==0)
{
//cout<<num.top()<<endl;
num.pop();
n=n/m;
num.push(m);
if (n!=1)
num.push(n);
goto loop1;
}
else m++;
if (!isprime(m))
m++;
else goto loop1;
}
}
/*if (num.top(i)==1)
num.pop();*/
loop2:{ int x=num.numbers();
for (int j=0;j<=x-1;j++)
{
int m;
num.top(m);
cout<<m<<" ";
num.pop();
}
cout<<endl;
}
}
prime.cpp:
#include <iostream>
using namespace std;
#include "stack.h"
#include <math.h>
void prime(stack<int>);
bool isprime(int);
void main()
{
stack<int> num;
int n;
char s;
loop1:{cout<<"Input a Number:"<<endl;
cin>>n;
cout<<"The prime of the number is:"<<endl;
//cout<<isprime(n)<<endl;
num.push(n);
prime(num);}
cout<<"是否继续?"<<endl;
cin>>s;
if (s=='y')
{num.pop();
goto loop1;}
else return;
}
#include <math.h>
#include <iostream.h>
const int maxentry=20;
enum error_code{fail,success,overflow,underflow};
template<class type>
class stack
{
private:
int count;
type entry[maxentry];
public:
stack()
{count=0;}
error_code push(const type &a);
error_code pop();
bool empty()const;
error_code top(type &a)const;
int numbers();
};
template<class type>
error_code stack<type>::push(const type &a)
{
error_code outcome=success;
if(count<maxentry)
{
entry[count++]=a;
}
else outcome=fail;
return outcome;
}
template<class type>
error_code stack<type>::pop()
{
error_code outcome=success;
if(count>0)
{
count--;
}
else outcome=fail;
return outcome;
}
template<class type>
bool stack<type>::empty() const
{
return count==0;
}
template<class type>
error_code stack<type>::top(type &a) const
{
error_code outcome=success;
if(count>0)
{
a=entry[count-1];
}
else outcome=fail;
return outcome;
}
template<class type>
int stack<type>::numbers()
{
return count;
}
bool isprime(int n)
{
if (n==1)
return false;
int k;
k=sqrt(n);
for (int i=2;i<=k;i++)
{
if (n%i==0)
{return false;}
if (i>=k+1)
{return true;}
else {return false;}
}
}
void prime(stack<int> num)
{
int m=2;
//int a[255]={0,};
//int i;
int n;
num.top(n);
//num.pop();
//cout<<n<<endl;
if (n==1||n==0||n<0)
{cout<<"您所输入的数没有素因子。"<<endl;return;}
while (n>1)
{
loop1:{
if (isprime(n))
{goto loop2;}
else if (n%m==0)
{
//cout<<num.top()<<endl;
num.pop();
n=n/m;
num.push(m);
if (n!=1)
num.push(n);
goto loop1;
}
else m++;
if (!isprime(m))
m++;
else goto loop1;
}
}
/*if (num.top(i)==1)
num.pop();*/
loop2:{ int x=num.numbers();
for (int j=0;j<=x-1;j++)
{
int m;
num.top(m);
cout<<m<<" ";
num.pop();
}
cout<<endl;
}
}
prime.cpp:
#include <iostream>
using namespace std;
#include "stack.h"
#include <math.h>
void prime(stack<int>);
bool isprime(int);
void main()
{
stack<int> num;
int n;
char s;
loop1:{cout<<"Input a Number:"<<endl;
cin>>n;
cout<<"The prime of the number is:"<<endl;
//cout<<isprime(n)<<endl;
num.push(n);
prime(num);}
cout<<"是否继续?"<<endl;
cin>>s;
if (s=='y')
{num.pop();
goto loop1;}
else return;
}
相关文章推荐
- 分解一个整数为素数的乘积形式
- 编程珠玑第六章习题二——C++实现一个数的因子分解
- 整数因子分解:计算一个整数所有的分解式(递归实现)
- 给定一个正整数n,则在n所有的分解式中,求因子乘积最大的一个分解及此乘积。
- C语言:求分解一个任意合数为质数乘积形式
- 把任意一个正整数分解成几个素因子的幂的乘积
- 输入一个整数,将其分解成质因式的乘积的形式 如12=2*2*3
- 【C/C++】任意大于1的整数分解成素数因子乘积的形式
- 将一个数分解成质因数相乘形式(如435234=251*17*17*3*2)
- 因数分解,把一个整数分解成质数相乘的形式
- 面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用就可以了;面向对象是把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的行为(转)
- C语言求一个整数的二进制形式表示中1的个数,用函数实现
- 汇编 编程实现把键入的一个字符,用二进制形式(0/1)显示它的ASCII代码值。
- FOJ 1075假设x是一个正整数,它的值不超过65535(即1<x<=65535),将x分解为若干个素数的乘积。
- 目的:实现一个2008serve的IIS的虚拟目录(通过网络路径(UNC)的形式,共享在另外一个2008服务器上
- 统计一个数分解质因数中不同因子的个数
- 之江学院 G 一个正整数分解得更多的3,乘积更大
- 【源码】将一个整数的每位数分解并按逆序放入一个数组中(用递归算法)(C语言实现)
- PAT 1096. Consecutive Factors (20)(最大连续乘积因子)(sqrt(n)判断一个数是否为质数的深刻理解)
- 本程序主要实现了一个方阵的求逆与实现了逆矩阵和原矩阵的乘积为单位矩阵