我比较笨
剑栈风樯各苦辛,别时冰雪到时春
#include <cstdio> #include <cstdlib> #include <conio.h> #include "Prime.h" void Pause(void); const size_t NUMBER_PER_ROW = 10; int main(void) { OpenPrimeFile(); unsigned int start = 1000000U; unsigned int end = 1002000U; size_t count = 0; for (unsigned int x = start; x <= end; ++x) { if (IsPrime(x)) { printf_s("%6u", x); putchar((++count % NUMBER_PER_ROW == 0) ? '\n' : ' '); } } ClosePrimeFile(); Pause(); return EXIT_SUCCESS; } void Pause(void) { printf_s("\nPress any key to continue..."); _getch(); }
#pragma once bool OpenPrimeFile(void); void ClosePrimeFile(void); bool IsPrime(unsigned int n);
#include <cstdio> #include <cstdlib> #include "Prime.h" const char* File_Name = "Prime_Number.DAT"; // 素數表文件名 FILE* hPrime = NULL; // 素數表文件句柄 bool OpenPrimeFile(void) { return (fopen_s(&hPrime, File_Name, "rb") == 0); } void ClosePrimeFile(void) { if (hPrime) { fclose(hPrime); } } /* 功能: 判斷指定的整數是否素數 */ bool IsPrime(unsigned int n) { fseek(hPrime, n, SEEK_SET); return fgetc(hPrime) == 1; }