/* Name: 求指定范围内的所有素数 Copyright: tonyee Author: tonyee Date: 20-03-09 18:50 Description: C++ 练习题 */ #include <iostream> using namespace std; class PRIMENUM { int data[25]; int span1, span2; int num; public: PRIMENUM(int n1,int n2) { num = 0; span1 = n1, span2 = n2; } int isprime(int x); void process() { for(int i = span1; i<=span2; i++) { if(isprime(i)) data[num++]=i; } } void print() { cout << "num =" << num << endl; int k = 0; for(int i=0; i<num; i++) { cout << data[i] << '/t'; k++; if(k%5==0) cout << endl; } cout << endl; } }; int PRIMENUM::isprime(int x) { for(int i=2; i<x/2; i++) if(x%i == 0) return 0; return 1; } int main() { PRIMENUM test(100,200); test.process(); test.print(); system("PAUSE"); return EXIT_SUCCESS; }