-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab2_cogwheel.pas
More file actions
343 lines (340 loc) · 10.1 KB
/
Lab2_cogwheel.pas
File metadata and controls
343 lines (340 loc) · 10.1 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
program lab2;
{22. На плоскости расположена система из N шестеренок,
которая приводится в движение вращением шестеренки 1 по
часовой стрелке. Сцепленные шестеренки могут вращаться только
в противоположных направлениях. Требуется определить
направление движения каждой шестеренки либо установить, что
систему заклинит. Некоторые шестеренки могут остаться
неподвижными (10).}
Const N = 20000;
var
cogwheel : array [1..N*2, 1..2] of integer;
fixed: array [1..N*20] of integer;
all_cogwheel: array[1..N*3] of integer;
cogwise_l: array[1..N*2] of integer;
cogwise_r: array[1..N*2] of integer;
cogwise_r_compressed: array[1..N*2] of integer;
cogwise_l_compressed: array[1..N*2] of integer;
fixed_compressed: array[1..N*2] of integer;
ender_array: array[1..N*3] of integer;
i, number,n_par,num_cog, j,k,spicok_par_left_int, spicok_par_right_int,flag_for_mass2,counting_rows,m,num,k2,k3,k4,a1,k5,num_cog1: integer;
count_r, count_l,flag1, flag_for_mass,prov,fixed_count:integer;
input_f: TEXT;
line,flag, filename, spicok_par_left, spicok_par_right :string;
function processing_clockwise(num_cog, flag_for_mass: integer): integer;
var i: integer;
begin
if flag_for_mass = 1 then
begin
for i:= 1 to N*2 do
if cogwise_r[i] = num_cog then
begin
processing_clockwise:= 1;
inc(prov);
end
else if cogwise_l[i] = num_cog then
begin
processing_clockwise:=0;
inc(prov);
end;
end
else if flag_for_mass = 0 then
begin
for i:= 1 to N*2 do
if cogwise_l[i] = num_cog then
begin
processing_clockwise:= 0;
inc(prov)
end
else if cogwise_r[i] = num_cog then
begin
processing_clockwise:= 1;
inc(prov);
end;
end;
//проверка на неподвижность, если неподвижна, то мы ее не включаем в массив
if prov < 1 then
begin
processing_clockwise:= 3;
end
else
prov:= 0;
end;
Begin
writeln('введите название файла(путь):');
readln(filename);
j:=1;
i:=1;
k:=1;
m:=1;
fixed_count:= 1;
if (FileExists(filename)=false) or (filename = '') then
writeln('неверное имя')
else
BEGIN
ASSIGN(input_f, filename);
RESET(input_f);
flag:='left';
counting_rows := 1;
While not EOF(input_f) do
Begin
readln(input_f,line);
if counting_rows < 3 then
begin
if counting_rows = 1 then
begin
number:= StrToInt(line)
end
else
begin
n_par:=StrToInt(line)
end;
end
else
begin
for a1:=1 to length(line) do
begin
if ',' = line[a1] then
flag1:= 1;
end;
if flag1=1 then
//if length(line) > 2 then
begin
for i:=1 to (length(line)) do
begin
if flag = 'left' then
begin
if line[i] <> ',' then
begin
spicok_par_left := spicok_par_left + line[i];
end
else
flag:= 'right';
end
else if flag = 'right' then
begin
if i = length(line) then
begin
flag:='left';
spicok_par_right := spicok_par_right + line[i];
end
else
spicok_par_right := spicok_par_right + line[i];
end;
end;
spicok_par_left_int:= StrToInt(spicok_par_left);
spicok_par_right_int:= StrToInt(spicok_par_right);
(cogwheel[k,j]) := spicok_par_left_int;
(cogwheel[k,j+1]) := spicok_par_right_int;
spicok_par_left := '';
spicok_par_right := '';
inc(k);
flag1:=0;
end
else
begin
//m1:=StrToInt(line);
//fixed[m]:= m1;
//write('шестеренка неподвижна');
//inc(m);
end;
//if length(line) < 2 then // åñëè ëèíèÿ èìååò çàïÿòóþ, òî ìû óâåëè÷èâàåì k åñëè íåò, òî èäåì äàëüøå, çàâòðà ïîìåíÿé, à ñåé÷àñ ñïàòåíüêè
end;
//for i:=1 to length(line) do
// if
inc(counting_rows);
End;
//writeln(cogwheel);
close(input_f);
//m1:=m-1;
num_cog := 1;
count_r:= 1;
count_l:= 1;
cogwise_r[count_r]:= 1;
inc(count_r);
flag_for_mass:= 1;
while num_cog1 < number+1 do
begin
while num_cog < number+1 do
begin
flag_for_mass:= processing_clockwise(num_cog,flag_for_mass); //êîòîðàÿ âîçâðàùàåò çíà÷åíèå ôëàãà
if flag_for_mass = 1 then
begin
For i :=1 to n_par do
begin
if (cogwheel[i,1] = num_cog) or (cogwheel[i,2] = num_cog) then
begin
if cogwheel[i,1] = num_cog then
begin
cogwise_l[count_l]:= cogwheel[i,2];
inc(count_l);
flag_for_mass2:= flag_for_mass;
end
else if cogwheel[i,2] = num_cog then
begin
cogwise_l[count_l]:= cogwheel[i,1];
inc(count_l);
flag_for_mass2:=flag_for_mass;
end;
end;
end;
end
else if flag_for_mass= 0 then
begin
For i :=1 to n_par do
begin
if (cogwheel[i,1] = num_cog) or (cogwheel[i,2] = num_cog) then
begin
if cogwheel[i,1] = num_cog then
begin
cogwise_r[count_r]:= cogwheel[i,2];
inc(count_r);
flag_for_mass2:=flag_for_mass;
end
else if cogwheel[i,2] = num_cog then
begin
cogwise_r[count_r]:= cogwheel[i,1];
inc(count_r);
flag_for_mass2:=flag_for_mass;
end;
end;
end;
end
else if flag_for_mass= 3 then
flag_for_mass:= flag_for_mass2;
inc(num_cog);
//write('массив против часовой: ');
//writeln(cogwise_l);
//write('массив по часовой: ');
//writeln(cogwise_r);
//writeln();
end;
num_cog:=1;
inc(num_cog1);
end;
//обход по часовой стрелки, удаление повторов++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
i:=0;
j:=0;
k:=0;
num:=N;
for i:=1 to num do begin
m:=0;
for j:=1 to k do
if (cogwise_r[i]=cogwise_r_compressed[j]) then
inc(m);
if m=0 then begin
inc(k);
cogwise_r_compressed[k]:=cogwise_r[i];
end;
end;
k2:= k;
for i:=1 to k-1 do
begin
all_cogwheel[i]:= cogwise_r_compressed[i];
inc(k5);
inc(k4);
end;
//+++++++++++++++++++++++++конец обхода+++++++++++++++++++++++++++++++++++++++++++++===
//обход против часовой стрелки, удаление повторов++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
i:=0;
j:=0;
k:=0;
num:=N;
for i:=1 to num do begin
m:=0;
for j:=1 to k do
if (cogwise_l[i]=cogwise_l_compressed[j]) then
inc(m);
if m=0 then begin
inc(k);
cogwise_l_compressed[k]:=cogwise_l[i];
end;
end;
//writeln(k3);
//writeln(k4);
//writeln(k);
//writeln(k2);
//writeln(k5);
for i:=1 to k-1 do
begin
all_cogwheel[k2]:= cogwise_l_compressed[i];
inc(k2);
inc(k3);
end;
//writeln;
//+++++++++++++++++++++++++конец обхода++++++++++++++++++++++++++++++++++++++++++++++++
//write('по час');
//writeln(cogwise_r_compressed);
//write('против час');
//writeln(cogwise_l_compressed);
//writeln(all_cogwheel);
dec(k2);
//++++++++++++++++++++++++Проверка заклинит систему или нет++++++++++++++++++++++++++++
i:=0;
j:=0;
k:=0;
num:=N;
for i:=1 to num do begin
m:=0;
for j:=1 to k do
if (all_cogwheel[i]=ender_array[j]) then
inc(m);
if m=0 then begin
inc(k);
ender_array[k]:=all_cogwheel[i];
end;
end;
//writeln(number);
num_cog:=1;
fixed_count:= 1;
//======================ПРОВЕРКА НА НЕПОДВИЖКУ=========
while num_cog < number+1 do
begin
for i:=1 to k2 do
begin
if all_cogwheel[i] = num_cog then
flag:= 'значит эта шестеренка подвижна';
end;
if flag = 'значит эта шестеренка подвижна' then
flag:= 'неизвестность'
else
begin
fixed[fixed_count]:= num_cog;
inc(fixed_count);
flag:= 'неизвестность';
end;
inc(num_cog);
end;
//==================КОНЕЦ ПРОВЕРКА=================
//writeln('pocle');
//writeln(all_cogwheel);
//writeln('фиксированые');
//writeln(fixed);
//writeln('неподвижка');
//writeln(fixed_compressed);
//проверка не неисправность
if counting_rows > 2 then
begin
if (k-1) <> k2 then
writeln('Ваша система неисправна')
//конец проверки,выдача на экран информации по шестеренкам
else
begin
writeln('всего шестеренок: ',number);
writeln('всего пар шестеренок: ',n_par);
writeln('Шестеренки по часовой: ');
for i:=1 to k5 do writeln(cogwise_r_compressed[i]:3);
writeln;
writeln('Шестеренки против часовой стрелки: ');
for i:=1 to k3 do writeln(cogwise_l_compressed[i]:3);
writeln;
writeln('Неподвижные шестеренки: ');
for i:=1 to fixed_count-1 do writeln(fixed[i]:3);
end;
//конец проверки
//writeln(fixed_count - 1); это число фиксиров шестер
end
else
writeln('задайте данные правильно');
END;
End.