Category : Files from Magazines
Archive   : BYTE1188.ZIP
Filename : CPLUSPLS.LST

 
Output of file : CPLUSPLS.LST contained in archive : BYTE1188.ZIP

/* C++ version of sieve */

#include

const TRUE = 1;
const FALSE = 0;
const SIZE = 8190;
const MAX_ITER = 100;

class primes {

char* flags;
int size;
int count;
public:
primes(int dsize) { size = dsize; count = 0; flags = new char[dsize]; }
~primes() { size = 0; count = 0; delete flags; }
int count_primes()
{
int prime, k;

count = 0; /* prime counter */
for (int i = 0; i <= size; i++) /* set all flags true */
*(flags + i) = TRUE;
for (i = 0; i <= size; i++)
{
if (*(flags + i)) /* found a prime */
{
prime = i + i + 3; /* twice index + 3 */
for (k = i + prime; k <= size; k += prime)
*(flags +k ) = FALSE; /* kill all multiple */
count++; /* primes found */
}
}

return count;
}

};

main()
{
int result;
primes num(SIZE);

printf ("%d iterations\n", MAX_ITER);
/* do program MAX_ITER times */
for (int iter = 1; iter <= MAX_ITER; iter++)
result = num.count_primes();
printf("%d primes.\n", result); /* primes found on 100th pass */

}

/* end sieve */


/* C++ version of complex math benchmark */

#include

class complex
{
double real, imag;

public:
complex(double xreal = 0.0, double ximag = 0.0)
{
real = xreal;
imag = ximag;
}
complex(complex &);
void print_cmplx();
complex operator =(complex &);
complex operator *(complex &);
complex operator /(complex &);
};

complex::complex(complex &c)
{
real = c.real;
imag = c.imag;
}

void complex::print_cmplx()
{
printf("%lf + i %lf", real, imag);
}

complex complex::operator = (complex &c)
{
real = c.real;
imag = c.imag;
return *this;
}

complex complex::operator *(complex &c)
{
complex result;

result.real = real * c.real - imag * c.imag;
result.imag = real * c.imag + c.real * imag;
return result;
}

complex complex::operator /(complex &c)
{
complex result;
double denom;

denom = c.real * c.real + c.imag * c.imag;
result.real = (real * c.real + imag * c.imag) / denom;
result.imag = (c.real * imag - real * c.imag) / denom;

return result;
}

main ()
{
const int count = 5000;

complex a(10.0,10.0), b(2.0,2.0), c(0.0,0.0);

for (int i = 0; i < count; ++i)
{
c = a * b;
c = c / a;
c = a * b;
c = c / a;
c = a * b;
c = c / a;
c = a * b;
c = c / a;
c = a * b;
c = c / a;
c = a * b;
c = c / a;
c = a * b;
c = c / a;
}
puts("Done!");
c.print_cmplx();
printf("\n\n");
}


/* end complex */