Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1.5.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <math.h>
- #include <iostream>
- #include <fstream>
- /*
- Во входном массиве – целые числа. Поместить в выходной файл только простые числа
- */
- using namespace std;
- int Prime(unsigned long a);
- bool read_write(char *f_ioput, int *ioput_mas, bool flag);
- int main()
- {
- char *f_in = new char[64];
- char *f_out = new char[64];
- int *mas = new int[256];
- int *o_mas = new int[256];
- cout << "Vvedite INPUT FILE with format(example: input.txt)" << endl;
- cin >> f_in;
- cout << "Vvedite OUTPUT FILE with format(example: output.txt)" << endl;
- cin >> f_out;
- if (read_write(f_in, mas, 1)) {
- int j = 0;
- int i = 0;
- while (mas[i] != '\0') {
- if (Prime((unsigned long)mas[i])) {
- o_mas[j] = mas[i];
- j++;
- }
- i++;
- }
- read_write(f_out, o_mas, 0);
- }
- return 0;
- }
- int Prime(unsigned long a)
- {
- unsigned long i1, i2, i3, i4, i5, i6, i7, i8, bound;
- if (a == 0 || a == 1)
- return 0;
- if (a == 2 || a == 3 || a == 5 || a == 7 || a == 11 || a == 13 || a == 17 || a == 19 || a == 23 || a == 29)
- return 1;
- if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0 || a % 7 == 0 || a % 11 == 0 || a % 13 == 0 || a % 17 == 0 || a % 19 == 0 || a % 23 == 0 || a % 29 == 0)
- return 0;
- bound = sqrt((double)a);
- i1 = 31; i2 = 37; i3 = 41; i4 = 43; i5 = 47; i6 = 49; i7 = 53; i8 = 59;
- while (i8 <= bound && a%i1 && a%i2 && a%i3 && a%i4 && a%i5 && a%i6 && a%i7 && a%i8)
- {
- i1 += 30; i2 += 30; i3 += 30; i4 += 30; i5 += 30; i6 += 30; i7 += 30; i8 += 30;
- }
- if (i8 <= bound ||
- i1 <= bound && a % i1 == 0 ||
- i2 <= bound && a % i2 == 0 ||
- i3 <= bound && a % i3 == 0 ||
- i4 <= bound && a % i4 == 0 ||
- i5 <= bound && a % i5 == 0 ||
- i6 <= bound && a % i6 == 0 ||
- i7 <= bound && a % i7 == 0)
- return 0;
- return 1;
- }
- bool read_write(char *f_ioput, int *ioput_mas, bool flag) {
- int n = 0;
- if (flag) {
- ifstream fin(f_ioput);
- if (fin) {
- while (!fin.eof()) {
- fin >> ioput_mas[n];
- n++;
- }
- fin.close();
- return 1;
- }
- else {
- cout << "ERROR: DON'T OPEN " << f_ioput << "!" << endl;
- return 0;
- }
- }
- else {
- ofstream fout(f_ioput);
- if (fout) {
- for (int i = 0; i < sizeof(ioput_mas[i]);i++){
- fout << ioput_mas[i] << " ";
- }
- return 1;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement