prime.h头文件中
#include <iostream>
#include <cmath>//注意cmath
using namespace std;
class Prime
{
public:
Prime(int start, int end) : m_start(start), m_end(end){};
void ShowPrime(const Prime &p);
private:
int m_start, m_end;
};
prime.cpp文件
#include "prime.h"
void Prime::ShowPrime(const Prime &p)
{
int i, j, row = 0;
for(i = p.m_start; i <= p.m_end; i++)
{
double k = sqrt(double(i));
for(j = 2; j <= k; j++)
{
if(i%j == 0) break;
}
if(j > k)
{
row = row++;
cout<<i<<"\t";
if(row%8 == 0) cout<<endl;
}
}
cout<<endl;
}
main函数
#include "prime.h"
int main ()
{
Prime p(2, 500);
p.ShowPrime(p);
return 0;
}