Вот, отформатировал код(люблю RAD C++ XE, Ctrl+D = Format Source ), может кому-то еще пригодится:
| Код | #include <iostream.h> #include <conio.h>
const int n = 3;
template<class T> class mass { public: T mas[n][n];
void show() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << mas[i][j] << " "; cout << "\n"; } }
mass() { char v; cout << "Vruchnu: y - Yes, n - No: "; cin >> v; if (v == 'n') { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mas[i][j] = (T)(rand() % 8 + 2); } else { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { cout << "mas[" << i << "][" << j << "]: "; cin >> mas[i][j]; } cout << endl; } }
void variant() { // поиск мин елемента T min = mas[0][0]; int ns, i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) if (min > mas[i][j]) { min = mas[i][j]; ns = j; } cout << "Min element massiva = " << min; cout << ", nahoditsy v stolbce pod #" << ns + 1 << ";" << endl << endl;
// сортировка: T mas2[n]; // доп. массив for (int i = 0; i < n; i++) { mas2[i] = mas[i][ns]; } for (int i = 0; i < n; i++) { cout << mas2[i] << "\n"; } cout << endl << endl; T tmp; int moves = 0, srav = 0; int indexmin = 0; for (i = 0; i < n; i++) { indexmin = i; for (int j = indexmin + 1; j < n; j++, srav++) if (mas2[j] < mas2[indexmin]) indexmin = j; if (indexmin != i) {
tmp = mas2[indexmin]; mas2[indexmin] = mas2[i]; mas2[i] = tmp; moves++; } } cout << endl << "Sravneny = " << srav << endl << "Perestanovok = " << moves << endl; for (int i = 0; i < n; i++) { cout << mas2[i] << "\n"; } cout << endl << endl; for (int i = 0; i < n; i++) { mas[i][ns] = mas2[i]; } }
};
void main() { mass<int>m1; cout << "-----Mass1-----\n"; m1.show(); cout << endl; m1.variant(); m1.show(); cout << endl << "-----Mass2-----\n"; mass<float>m2; m2.show(); cout << endl; m2.variant(); m2.show(); cout << endl; cout << "Press any key to EXIT..."; getch(); }
|
|