Advertisement
MargaritaOwl

Laba3

May 15th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <locale>
  3. #include <fstream>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. int checkConsole()
  8. {
  9. int tmp;
  10. cin >> tmp;
  11. while (!cin)
  12. {
  13. cout << "Введите число, а не текст!\n : ";
  14. cin >> tmp;
  15. }
  16. return tmp;
  17. }
  18.  
  19. void inputConsoleMatrix(int **array, int size)
  20. {
  21. for (int i = 0; i < size; i++)
  22. for (int j = 0; j < size; j++)
  23. array[i][j] = checkConsole();
  24. }
  25.  
  26. bool check(char tmp[255])
  27. {
  28. int i;
  29. tmp[0] == '-' ? i = 1 : i = 0;
  30. for (; tmp[i] != '\0'; i++)
  31. if (!isdigit(tmp[i]))
  32. return false;
  33.  
  34. return true;
  35. }
  36.  
  37. bool inputFileMatrix(int **array, int size, int &i, int &j, ifstream &fin)
  38. {
  39. char tmp[255];
  40. for (i = 0; i < size; i++)
  41. for (j = 0; j < size;)
  42. {
  43. fin >> tmp;
  44. if (check(tmp))
  45. {
  46. array[i][j] = atoi(tmp);
  47. j++;
  48. }
  49.  
  50. if (fin.eof() && j < size) // т.е. еще не все элементы матрицы были заполнены, но в потоке уже нет чисел
  51. return false;
  52. }
  53.  
  54. return true;
  55. }
  56.  
  57. void inputFalseMatrix(int **array, int size, int ii, int jj)
  58. {
  59. cout << "В потоке было недостаточно элементов для матрицы. Введите недостающие элементы:" << endl;
  60. int j = jj;
  61. for (int i = ii; i < size; i++)
  62. {
  63. for (; j < size; j++)
  64. {
  65. cout << "a[" << i + 1 << "][" << j + 1 << "] = ";
  66. array[i][j] = checkConsole();
  67. }
  68. j = 0;
  69. }
  70.  
  71. }
  72.  
  73. void outOfFile(ifstream &fin, ofstream &fout)
  74. {
  75. cout << "Вы хотите вывести на экран данные из выходного файла?\n1)Да\n2)Нет" << endl;
  76. int temp;
  77. cin >> temp;
  78. while (temp != 1 && temp != 2)
  79. {
  80. cout << "Введите один из вариантов.";
  81. cin >> temp;
  82. }
  83. if (temp == 1)
  84. {
  85. fin.close();
  86. fout.close();
  87. fin.open("output.txt");
  88. char a[255];
  89. while (!fin.eof())
  90. {
  91. fin.getline(a, 255);
  92. cout << a << endl;
  93. }
  94. }
  95. }
  96.  
  97. void outarray(int **array, int size)
  98. {
  99. for (int i = 0; i < size; i++)
  100. {
  101. for (int j = 0; j < size; j++)
  102. cout << setw(4) << array[i][j] << " ";
  103. cout << endl;
  104. }
  105. cout << endl;
  106. }
  107.  
  108. void outarray(int **array, int size, ofstream &fout)
  109. {
  110. for (int i = 0; i < size; i++)
  111. {
  112. for (int j = 0; j < size; j++)
  113. fout << setw(4) << array[i][j] << " ";
  114. fout << endl;
  115. }
  116. fout << endl;
  117. }
  118.  
  119. int main()
  120. {
  121. setlocale(LC_ALL, "russian");
  122.  
  123. int size, ii = 0, jj = 0;
  124.  
  125. ifstream fin;
  126. fin.open("input.txt");
  127. ofstream fout;
  128. fout.open("output.txt");
  129.  
  130. if (!fin.is_open())
  131. {
  132. cout << "Файл не удалось открыть." << endl;
  133. return 1;
  134. }
  135.  
  136. cout << "Введите размер матрицы: ";
  137. cin >> size;
  138.  
  139. int **array = new int*[size];
  140. for (int i = 0; i < size; i++)
  141. array[i] = new int[size];
  142.  
  143. if (!inputFileMatrix(array, size, ii, jj, fin))
  144. inputFalseMatrix(array, size, ii, jj);
  145.  
  146. fout << "Вы ввели матрицу:" << endl;
  147. outarray(array, size, fout);
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156. outOfFile(fin, fout);
  157.  
  158. for (int i = 0; i < size; i++)
  159. delete[]array[i];
  160. delete[]array;
  161.  
  162. return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement