Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. #define MAX 21
  5. using namespace std;
  6.  
  7.  
  8. void Output(int a[][MAX], int m, int n) { // In the he vi sinh vat
  9.  
  10. for (int i = 0; i < m; i++) {
  11. for (int j = 0; j < n; j++) {
  12. cout << a[i][j] << " ";
  13. }
  14. cout << endl;
  15. }
  16. }
  17.  
  18.  
  19.  
  20. int OriGen(int a[][MAX], int m, int n) { //Read vi sinh vat ban dau C:/Users/DELL/Desktop/work.dat
  21. ifstream myFile;
  22. myFile.open("C:/Users/DELL/Desktop/work.dat");
  23. if (!myFile) {
  24. cout << "Failed to open this file!" << endl;
  25. return 1;
  26. }
  27. while (!myFile.eof()) {
  28. //Doc mang vi sinh vat ban dau
  29. for (int i = 0; i < m; i++) {
  30. for (int j = 0; j < n; j++) {
  31. myFile >> a[i][j];
  32. }
  33. }
  34. }
  35. myFile.close();
  36. }
  37.  
  38.  
  39. void NextGen(int a[][MAX], int future[][MAX], int m, int n) { // The he tiep theo
  40. for (int h = 1; h < m - 1; h++) {
  41. for (int k = 1; k < n - 1; k++) {
  42. int aliveNeighbours = 0; // so hang xom
  43. for (int i = -1; i <= 1; i++) {
  44. for (int j = -1; j <= 1; j++) {
  45. aliveNeighbours += a[h + i][k + j];
  46.  
  47. }
  48. }
  49. aliveNeighbours -= a[h][k];
  50. //Cac quy tac can de song hoac chet
  51. //Chet do co don
  52. if ((a[h][k] == 1) && (aliveNeighbours < 2))
  53. future[h][k] = 0;
  54. //Chet do qua dong
  55. else if ((a[h][k] == 1) && (aliveNeighbours > 3))
  56. future[h][k] = 0;
  57.  
  58. //Mot te bao duoc sinh ra
  59.  
  60. else if ((a[h][k] == 0) && (aliveNeighbours == 3))
  61. future[h][k] = 1;
  62.  
  63. // Te bao con lai binh thuong
  64. else
  65. future[h][k] = a[h][k];
  66. }
  67. }
  68. }
  69.  
  70.  
  71. void aTob(int a[][MAX], int b[][MAX], int m, int n) { // Chuyen de lap
  72. for (int i = 0; i < m; i++) {
  73. for (int j = 0; j < n; j++) {
  74. a[i][j] = b[i][j];
  75. }
  76. }
  77. }
  78.  
  79.  
  80. int Checkdie(int b[][MAX], int m, int n) { // Kiem tra song chet
  81. int c = 0;
  82. for (int i = 0; i < m; i++) {
  83. for (int j = 0; j < n; j++) {
  84. c += b[i][j];
  85. }
  86. }
  87. return c;
  88. }
  89.  
  90.  
  91. int main() {
  92.  
  93. int c, m = 21, n = 21;
  94. int a[MAX][MAX];
  95. int b[MAX][MAX] = { 0 };
  96. cout << "Enter the number of generations: "; cin >> c;
  97.  
  98. OriGen(a, m, n);
  99. cout << endl << "Read successfully from the file: C:/Users/DELL/Desktop/work.dat" << endl << endl;
  100. cout << endl << "Original Generation: " << endl << endl;
  101. Output(a, m, n);
  102.  
  103. //Ghi File work.out
  104. ofstream wFile;
  105. wFile.open("C:/Users/DELL/Desktop/work.out");
  106.  
  107. int gen = 0;
  108. while (c != 0) {
  109. gen++;
  110. if (gen == 1) {
  111. wFile << endl << "1st Generation: " << endl << endl;
  112. cout << endl << "1st Generation: " << endl << endl;
  113. }
  114. else if (gen == 2) {
  115. wFile << endl << "2nd Generation: " << endl << endl;
  116. cout << endl << "2nd Generation: " << endl << endl;
  117. }
  118. else if (gen == 3) {
  119. wFile << endl << "3rd Generation: " << endl << endl;
  120. cout << endl << "3rd Generation: " << endl << endl;
  121. }
  122. else {
  123. wFile << endl << gen << "th Generation: " << endl << endl;
  124. cout << endl << gen << "th Generation: " << endl << endl;
  125. }
  126. NextGen(a, b, m, n);
  127.  
  128. if (Checkdie(b, m, n) == 0) {
  129. wFile << "\tGenerations die" << endl;
  130. cout << "\tGenerations die" << endl;
  131. break;
  132. }
  133.  
  134. Output(b, m, n);
  135. for (int i = 0; i < m; i++) {
  136. for (int j = 0; j < n; j++) {
  137. wFile << a[i][j] << " ";
  138. }
  139. wFile << endl;
  140. }
  141. aTob(a, b, m, n);
  142. c--;
  143. }
  144. cout << endl << endl << "Write successfully to the file C:/Users/DELL/Desktop/work.out" << endl << endl;
  145.  
  146. system("pause");
  147. return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement