This repository was archived by the owner on Apr 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_functions.cpp
More file actions
180 lines (165 loc) · 5.43 KB
/
io_functions.cpp
File metadata and controls
180 lines (165 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "io_functions.h"
namespace iof
{
bool isInt(std::string nval)
{
if(nval == "")
return false;
char *endptr;
try
{
std::strtol(nval.c_str(), &endptr, 10);
}
catch(const std::exception& e)
{
return false;
}
return (*endptr == '\0');
}
int **read_matrix_from_console(int &n)
{
while(true) {
std::string * ans = new std::string;
std::cout << "Введите размерность квадратной матрицы: ";
std::getline(std::cin, *ans);
if (isInt(*ans)) {
n = stoi(*ans);
if (n <= 0) {
std::cout << "Ошибка. Число должно быть положительным!" << std::endl;
delete ans;
continue;
} else {
delete ans;
break;
}
}
}
int **matrix = new int*[n];
for (int i = 0; i < n; i++)
{
matrix[i] = new int[n];
std::cout << "Введите элементы " << i + 1 << "-й строки матрицы (После каждого значения переносите строку): " << std::endl;
for (int j = 0; j < n; j++)
{
while (true)
{
std::string * nval = new std::string;
std::getline(std::cin, *nval);
if(isInt(*nval))
{
try
{
matrix[i][j] = stoi(*nval);
delete nval;
break;
}
catch(const std::exception& e)
{
std::cout << "Введено некорректное значение! Повторите ввод." << std::endl;
continue;
}
}
else
{
std::cout << "Введено неверное значение! Повторите ввод." << std::endl;
continue;
}
}
}
}
return matrix;
}
int **read_matrix_from_file(int &n)
{
std::ifstream file("input.txt");
if (!file)
{
throw std::runtime_error("Не удалось открыть файл. Создайте файл и перезапустите программу.");
}
file >> n;
if (n <= 0)
{
throw std::runtime_error("Некорректный формат данных в файле. Исправьте содержимое файла и перезапустите программу.");
}
int **matrix = new int*[n];
for (int i = 0; i < n; i++)
{
matrix[i] = new int[n];
for (int j = 0; j < n; j++)
{
try
{
file >> matrix[i][j];
}
catch(const std::exception& e)
{
throw std::runtime_error("Некорректное содержание файла. Проверьте файл и перезапустите программу");
}
}
}
return matrix;
}
void write_results_to_console(int **arr, int *resultA, int *resultB, int *resultC, int *resultD, int &n)
{
using namespace std;
cout << "Исходная матрица:" << endl << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << endl;
cout << "А) ";
for (int i = 0; i < n; i++)
cout << resultA[i] << " ";
cout << endl;
cout << "Б) ";
for (int i = 0; i < n; i++)
cout << resultB[i] << " ";
cout << endl;
cout << "В) ";
for (int i = 0; i < n; i++)
cout << resultC[i] << " ";
cout << endl;
cout << "Г) ";
for (int i = 0; i < n; i++)
cout << resultD[i] << " ";
cout << endl;
}
void wirte_results_to_file(int **arr, int *resultA, int *resultB, int *resultC, int *resultD, int &n)
{
using namespace std;
ofstream output("output.txt");
output << "Исходная матрица:" << endl << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
output << arr[i][j] << " ";
}
output << endl;
}
output << endl;
output << "А) ";
for (int i = 0; i < n; i++)
output << resultA[i] << " ";
output << endl;
output << "Б) ";
for (int i = 0; i < n; i++)
output << resultB[i] << " ";
output << endl;
output << "В) ";
for (int i = 0; i < n; i++)
output << resultC[i] << " ";
output << endl;
output << "Г) ";
for (int i = 0; i < n; i++)
output << resultD[i] << " ";
output << endl;
output.close();
}
}