-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15-2_Move2
More file actions
201 lines (171 loc) · 5.93 KB
/
Copy path15-2_Move2
File metadata and controls
201 lines (171 loc) · 5.93 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package lesson15;
/* Дана матрица порядка MxN (M строк, N столбцов). Необходимо заполнить ее значениями и написать функцию,
* осуществляющую циклический сдвиг строк и/или столбцов массива указанное количество раз
* и в указанную сторону.
*/
// наполнение матрицы - реализуем через метод
// вывод матрицы - реализуем через метод
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class Move2 {
// размерность матрицы
private static int m, n;
private static int direction;
// кол-во сдвигов
private static int quantity;
private static boolean correctDirection, correctQuantity;
// создаем строку-алфавит (набор символов)
private static String alphabet = "abcdefghijklmnopqrstuvwxyz";
// длина строки нужна для границы рендомного выбора символов из строки-алфавита
private static int length = alphabet.length();
private static Random r;
private static Scanner scanner;
// список list являет собой матрицу
private static ArrayList <ArrayList<Character>> list;
// метод - наполняем матрицу
public static void fill (ArrayList <ArrayList<Character>> list) {
for (int i=0; i<m; i++) {
list.add(new ArrayList<Character>());
for (int j=0; j<n; j++) {
list.get(i).add(j, alphabet.charAt(r.nextInt(length)));
}
}
}
// метод - вывод матрицы
public static void showMatrix(ArrayList <ArrayList<Character>> list) {
for (ArrayList<Character> e : list) {
System.out.print(e + "\n");
}
}
// метод - добавление строки (сдвиг вниз)
public static void insertLine(ArrayList <ArrayList<Character>> list) {
list.add(0, new ArrayList<Character>());
}
// метод - удаление строки (сдвиг вверх)
public static void deleteLine(ArrayList <ArrayList<Character>> list) {
list.remove(0);
}
// метод - добавление столбца (сдвиг вправо)
public static void insertColumn(ArrayList <ArrayList<Character>> list) {
for (ArrayList<Character> element : list) {
element.add(0, ' ');
}
}
// метод - удаление столбца (сдвиг влево)
public static void deleteColumn(ArrayList <ArrayList<Character>> list) {
for (ArrayList<Character> element : list) {
element.remove(0);
}
}
public static void main(String[] args) {
m = 4; // строк
n = 7; // столбцов
r = new Random();
list = new ArrayList();
// наполняем матрицу
fill(list);
// выводим матрицу
System.out.println("Матрица:");
showMatrix(list);
System.out.println();
// считываем направление добавляемой строки
while (! correctDirection) {
System.out.println("Куда сдвигать?"
+ "\n 1. Вверх (удалить строки)."
+ "\n 2. Вниз (добавить строки)."
+ "\n 3. Влево (удалить столбцы)."
+ "\n 4. Вправо (добавить столбцы).");
scanner = new Scanner(System.in);
try {
direction = scanner.nextInt();
if (direction == 1 || direction == 2 ||
direction == 3 || direction == 4) {
correctDirection = true;
} else {
System.out.println("Введите от 1 до 4");
}
} catch (InputMismatchException e) {
System.out.println("Введите число!");
}
}
// ограничения количества сдвигов
while (! correctQuantity) {
System.out.println("Сколько раз?");
scanner = new Scanner(System.in);
try {
quantity = scanner.nextInt();
if (quantity > 0) {
// ограничить сдвиг вверх количеством строк
if (direction == 1) {
if ( quantity <= list.size()) {
correctQuantity = true;
} else {
System.out.println("Введите от 1 до " + list.size());
continue;
}
}
// ограничить сдвиг влево количеством столбцов
if (direction == 3) {
if (quantity <= list.get(0).size()) {
correctQuantity = true;
} else {
System.out.println("Введите от 1 до " + list.get(0).size());
continue;
}
}
correctQuantity = true;
}
} catch (InputMismatchException e) {
System.out.println("Введите натуральное число!");
}
}
// обработка матрицы в зависимости от выбранного направления
switch (direction) {
// сдвиг вверх
case 1: {
for (int i=0; i<quantity; i++) {
System.out.println("Сдвиг вверх - " + (i+1));
deleteLine(list);
showMatrix(list);
System.out.println();
}
break;
}
// сдвиг вниз
case 2: {
for (int i=0; i<quantity; i++) {
System.out.println("Сдвиг вниз - " + (i+1));
insertLine(list);
showMatrix(list);
System.out.println();
}
break;
}
// сдвиг влево
case 3: {
for (int i=0; i<quantity; i++) {
System.out.println("Сдвиг влево - " + (i+1));
deleteColumn(list);
showMatrix(list);
System.out.println();
}
break;
}
// сдвиг вправо
case 4: {
for (int i=0; i<quantity; i++) {
System.out.println("Сдвиг вправо - " + (i+1));
insertColumn(list);
showMatrix(list);
System.out.println();
}
break;
}
default : {
break;
}
}
}
}