C++的一个问题高手帮帮忙
这是源代码。。
#ifndef SIEVE_H_H
#define SIEVE_H_H
#include <cmath>
#include <cstddef>
#include <string>
#include"Test.h"
using namespace std;
class SieveTest:public TestSuite::Test{
string sieveChars;
public:
SieveTest():sieveChars(50,'p'){}
void run(){
findPrimes();
testPrimes();
}
bool isPrime(int p){
if(p==0||p==1)return false;
int root=int(sqrt(double(p)));
for(int i=2;i <=root;++i)
if(p%i==0)return false;
return true;
}
void findPrimes(){
sieveChars.replace(0,2,"NN");
size_t sieveSize=sieveChars.size();
int root=int(sqrt(double(sieveSize)));
for(int i=2;i <=root;++i)
for(size_t factor=2;factor * i <sieveSize;
++factor)
sieveChars[factor * i]='N';
}
void testPrimes(){
size_t i=sieveChars.find('p');
while(i!=string::npos){
test_(isPrime(i++));
i=sieveChars.find('p',i);
}
i=sieveChars.find_first_not_of('p');
while(i!=string::npos){
test_(!isPrime(i++));
i=sieveChars.find_first_not_of('p',i);
}
}
};
#endif
///////////////////////////////////////////////////////////
//: TestSuite:Test.h
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#ifndef TEST_H
#define TEST_H
#include <string>
#include <iostream>
#include <cassert>
using std::string;
using std::ostream;
using std::cout;
// fail_() has an underscore to prevent collision with
// ios::fail(). For consistency, test_() and succeed_()
// also have underscores.
#define test_(cond) \
do_test(cond, #cond, __FILE__, __LINE__)
#define fail_(str) \
do_fail(str, __FILE__, __LINE__)
namespace TestSuite{
class Test {
ostream* osptr;
long nPass;
long nFail;
// Disallowed:
Test(const Test&);
Test& operator=(const Test&);
protected:
void do_test(bool cond, const string& lbl,
const char* fname, long lineno);
void do_fail(const string& lbl,
const char* fname, long lineno);
public:
Test(ostream* osptr = &cout) {
this->osptr = osptr;
nPass = nFail = 0;
}
virtual ~Test() {}
virtual void run() = 0;
long getNumPassed()const{return nPass;}
long getNumFailed()const{return nFail;}
const ostream* getStream() const { return osptr; }
void setStream(ostream* osptr) { this->osptr = osptr; }
void succeed_() { ++nPass; }
long report()const;
virtual void reset() { nPass = nFail = 0; }
};
}// namespace TestSuite
#endif // TEST_H ///:~
/////////////////////////
#include"sie.h"
int main(){
SieveTest t;
t.run();
return t.report();
}