📄 pex8_7.cpp
字号:
#include <iostream.h>
#include <iomanip.h>
#pragma hdrstop
#include "array.h"
void main(void)
{
// array holding primes
Array<int> A(10);
// user supplies upperlimit which determines range of primes
int upperlimit, primecount = 0, i, j;
cout << "Enter a value >= 2 as upper limit for prime numbers: ";
cin >> upperlimit;
A[primecount++] = 2; // 2 is a prime
for(i = 3; i < upperlimit; i++)
{
// if prime list is full, allocate 10 more elements
if (primecount == A.ListSize())
A.Resize(primecount + 10);
// even numbers > 2 not prime. contine next iteration
if (i % 2 == 0)
continue;
// check prime divisors
j = 0;
while (j < primecount && i % A[j] != 0)
j++;
// i has no prime divisor, add i to list of primes
if (j == primecount)
A[primecount++] = i;
}
for (i = 0; i < primecount; i++)
{
cout << setw(5) << A[i];
// output newline every 10 primes
if ((i+1) % 10 == 0)
cout << endl;
}
cout << endl;
}
/*
<Run>
Enter a value >= 2 as upper limit for prime numbers: 100
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -