Advertisement
JewishCat

1.5

Jan 10th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. // 1.5.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <math.h>
  6. #include <iostream>
  7. #include <fstream>
  8.  
  9. /*
  10. Во входном массиве – целые числа. Поместить в выходной файл только простые числа
  11. */
  12.  
  13. using namespace std;
  14.  
  15. int Prime(unsigned long a);
  16. bool read_write(char *f_ioput, int *ioput_mas, bool flag);
  17. int main()
  18. {
  19. char *f_in = new char[64];
  20. char *f_out = new char[64];
  21. int *mas = new int[256];
  22. int *o_mas = new int[256];
  23. cout << "Vvedite INPUT FILE with format(example: input.txt)" << endl;
  24. cin >> f_in;
  25. cout << "Vvedite OUTPUT FILE with format(example: output.txt)" << endl;
  26. cin >> f_out;
  27. if (read_write(f_in, mas, 1)) {
  28. int j = 0;
  29. int i = 0;
  30. while (mas[i] != '\0') {
  31. if (Prime((unsigned long)mas[i])) {
  32. o_mas[j] = mas[i];
  33. j++;
  34. }
  35. i++;
  36. }
  37. read_write(f_out, o_mas, 0);
  38. }
  39.  
  40. return 0;
  41. }
  42.  
  43. int Prime(unsigned long a)
  44. {
  45. unsigned long i1, i2, i3, i4, i5, i6, i7, i8, bound;
  46. if (a == 0 || a == 1)
  47. return 0;
  48. if (a == 2 || a == 3 || a == 5 || a == 7 || a == 11 || a == 13 || a == 17 || a == 19 || a == 23 || a == 29)
  49. return 1;
  50. 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)
  51. return 0;
  52. bound = sqrt((double)a);
  53. i1 = 31; i2 = 37; i3 = 41; i4 = 43; i5 = 47; i6 = 49; i7 = 53; i8 = 59;
  54. while (i8 <= bound && a%i1 && a%i2 && a%i3 && a%i4 && a%i5 && a%i6 && a%i7 && a%i8)
  55. {
  56. i1 += 30; i2 += 30; i3 += 30; i4 += 30; i5 += 30; i6 += 30; i7 += 30; i8 += 30;
  57. }
  58. if (i8 <= bound ||
  59. i1 <= bound && a % i1 == 0 ||
  60. i2 <= bound && a % i2 == 0 ||
  61. i3 <= bound && a % i3 == 0 ||
  62. i4 <= bound && a % i4 == 0 ||
  63. i5 <= bound && a % i5 == 0 ||
  64. i6 <= bound && a % i6 == 0 ||
  65. i7 <= bound && a % i7 == 0)
  66. return 0;
  67. return 1;
  68. }
  69. bool read_write(char *f_ioput, int *ioput_mas, bool flag) {
  70. int n = 0;
  71. if (flag) {
  72. ifstream fin(f_ioput);
  73. if (fin) {
  74. while (!fin.eof()) {
  75. fin >> ioput_mas[n];
  76. n++;
  77. }
  78. fin.close();
  79. return 1;
  80. }
  81. else {
  82. cout << "ERROR: DON'T OPEN " << f_ioput << "!" << endl;
  83. return 0;
  84. }
  85. }
  86. else {
  87. ofstream fout(f_ioput);
  88. if (fout) {
  89. for (int i = 0; i < sizeof(ioput_mas[i]);i++){
  90. fout << ioput_mas[i] << " ";
  91. }
  92. return 1;
  93. }
  94. }
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement