-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCS.cpp
1706 lines (1601 loc) · 56 KB
/
TCS.cpp
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include <windows.h>
using namespace std;
const int Min_Count = 40; /// ìèíèìàëüíûé äîïóñòèìûé ðàçìåð ìàññèâà, â êîòîðûé ìîãóò äîáàâëÿòüñÿ íîâûå çàäà÷è
const int inf = 9999; /// òèïà áåñêîíå÷íîñòü(íåëüçÿ äåëàòü ñëèøêîì áîëüøèì, âîçíèêíåò ïåðåïîëíåíèå)
const int Max_Reserve_Size = 100; /// ìàêñèìàëüíûé ðàçìåð ðåçåðâíîãî ìàññèâà
int Count = 0, q_add = 1; /// êîë-âî ýëåìåíòîâ â ôàéëå solve_problems è êîë-âî íîâûõ äîáàâëåííûõ çàäà÷
int Reserve_Size = 0;
bool tip = true; /// îòâå÷àåò çà ïîäñêàçêè(åñëè = true, òî îíè áóäóò âûâîäèòüñÿ)
bool tip_important = true; /// îòâå÷àåò çà âàæíûå ïîäñêàçêè(òèïà ââîäà êîíöåíòðàöèè)
bool tip_encumbering = true; /// ýòî ìåøàþùèå ïîäñêàçêè(òèïà Input numbers, ïðè add)
int Current_position = 0; /// ïîêàçûâàåò ïîçèöèþ â ôàéëå Buffer â äàííûé ìîìåíò
int q_buff; /// ïîêàçûâàåò, äî êàêîãî ýëåìåíòà íóæíî ñ÷èòûâàòü, ÷òîáû çàïèñàòü â áóôåð(â ðåàëüíîñòè, ýòî ñëèøêîì çàïóòàííàÿ øòêóà)
int Position; /// ïîêàçûâàåò, â êàêîì ñòîëáöå(â ôàéëå BP or BBP) íàõîäèòñÿ íóæíûé íàì ýëåìåíò
int Min_in_BP = -inf, Max_in_BP = inf; /// ïîêàçûâàåò íà ìàêñ è ìèí ýëåìåíòû â ôàéëå
/// BP(èçíà÷àëüíî îíè äîñòèãàþò ñâîåãî ìàêñèìóìàëüíîé àìïëèòóäû, ÷òîáû íå âîçíèêàëî
/// îøèáîê â óäàëåíèè èç BP
/// ðåàëüíîå êîë-âî ýëåìåíòîâ â ìàññèâå Solve_Problems = Count+q_add-1
int Reserve[Max_Reserve_Size+1]; /// ðåçåðâíûé ìàññèâ
bool flag_Recover_Back_Back_Up = false; /// åñëè îí ðàâåí true, òî ïðîèñõîäèò âîññòàíîâëåíèå ôàéëà Back_Back_Up
int count_Recover_Back_Back_Up; /// íîìåð íîìåðà â ôàéëå BBUp, äî êîòîðîãî ïîëüçîâàòåëü õî÷åò âîññòàíîâèòü
/// åñëè count_Recover_Back_Back_Up = 0, çíà÷èò íàäî âîññòàíîâèòü âñå
void print(int *F, int l, int r); /// âûâîäèò íà ýêðàí ó÷àñòîê ìàññèâà
void generation(int *F, int start, int finish, int concentration); /// ãåíåðèðóåò çàäà÷è èç äèàïîçîíà
void Add(int *F, int number); /// äîáàâëÿåò â íàáîð ðåøåííûõ çàäà÷ çàäà÷ó ïîä íîìåðîì number(change q_add)
void Delete(int *F, int number); /// óäàëÿåò èç íàáîðà ðåøåííûõ çàäà÷ çàäà÷ó ïîä íîìåðîì number(change q_add)
void Back_Up(int *F); /// çàïèñûâàåò â ôàéë áýêàï èçìåíåíèÿ, êîòîðûå ïðîèçîøëè
void Back_Back_Up(int *T, bool flag); /// çàïèñûâàåò â ôàéë áýêàïà îò áýêàïà îñíîâíîãî
/// ôàéëà(ñäåëàíî äëÿ óñêîðåíèÿ ïðîãðàììû). Åñëè flag = true, çíà÷èò ìû äîáàâëÿåì äàííûé îáúåêò, åñëè false - delete
void Buffer_Save(int *Buf, int n, int flag); /// äîáàâëÿåò â áóôôåð
void Add_in_BP(int *F, int n); /// äîáàâëÿåò â BP ýëåìåíòû èç ìàññèâà F
void return_min_max(int A[], int n, int &Min, int &Max){
Max = Min = A[1];
for (int i = 2; i < n+1; i++){
if (A[i] < Min) Min = A[i];
if (A[i] > Max) Max = A[i];
}
}
void Counting_Sort(int A[], int n){
int Min, Max;
return_min_max(A, n, Min, Max);
int B[n+1], C[Max-Min+1];
for (int i = 0; i < Max-Min+1; i++)
C[i] = 0;
for (int i = 1; i < n+1; i++)
C[A[i]-Min] += 1;
for (int j = 1; j < Max-Min+1; j++)
C[j] += C[j-1];
for (int i = 1; i < n+1; i++){
B[C[A[i]-Min]] = A[i];
C[A[i]-Min]--;
}
for (int i = 1; i < n+1; i++)
A[i] = B[i];
}
void Counting_Sort_with_Linking(int A[], int n, int Linking[]){
int Min, Max;
return_min_max(A, n, Min, Max);
int B[n+1], C[Max-Min+1], B_link[n+1];
for (int i = 0; i < Max-Min+1; i++)
C[i] = 0;
for (int i = 1; i < n+1; i++)
C[A[i]-Min] += 1;
for (int j = 1; j < Max-Min+1; j++)
C[j] += C[j-1];
for (int i = 1; i < n+1; i++){
B[C[A[i]-Min]] = A[i];
B_link[C[A[i]-Min]] = Linking[i];
C[A[i]-Min]--;
}
for (int i = 1; i < n+1; i++){
A[i] = B[i];
Linking[i] = B_link[i];
}
}
int bin_search(int *F, int l, int r, int key)
{
int mid;
while (r - l > 1)
{
mid = (r+l)/2;
if (F[mid] > key) r = mid-1;
else l = mid;
}
if (F[l] == key) return l;
if (F[r] == key) return r;
return -1;
}
/*
ôóíêöèÿ âîçâðàùàåò, â ñëó÷àå, åñëè key = true, òî ñàìûé áîëüøîé ýëåìåíò ñðåäè
ìåíüøèõ èëè ðàâíûõ ýòîãî ÷èñëà, â ïðîòèâíîì ñëó÷àå, ñàìûé ìàëåíüêèé
ñðåäè áîëüøèõ èëè ðàâíûõ åìó(òî÷íåå, èíäåêñ â ìàññèâå)
íèõ.
*/
int bin_search_in_range(int *F, int l, int r, int value, bool key)
{
if (key)
{
int mid;
while (r - l > 1)
{
mid = (r+l)/2;
if (F[mid] < value) l = mid+1;
else r = mid;
}
if (F[l] >= value) return l;
if (F[r] >= value) return r;
return -1;
}
else
{
int mid;
while (r - l > 1)
{
mid = (r+l)/2;
if (F[mid] > value) r = mid - 1;
else l = mid;
}
if (F[r] <= value) return r;
if (F[l] <= value) return l;
return -1;
}
}
/*
Âûâîäèò íà ýêðàí ñîäåðæèìîå ìàññèâà Solve_Problems(â èäåàëå ïðîèçâîëüíîãî, íî
áóäåò èñïîëüçîâàòüñÿ òîëüêî ýòîò ìàññèâ(ñêîðåå âñåãî))
*/
void print(int *F, int l, int r)
{
for (int i = l; i <= r; i++)
cout << F[i] << " ";
cout << "\n\n";
}
/*
Âûâîäèò íà ýêðàí ñîäåðæèìîå ôàéëà Back_Up
*/
void Print()
{
FILE *back_up = fopen(".tcs/Back_Up.txt", "r");
int count; fscanf(back_up, "%d", &count);
int data;
if (count == 0) {cout << "No solve problems\n"; return;}
for (int i = 0; i < count; i++)
{
fscanf(back_up, "%d", &data);
cout << data << " ";
}
cout << "\n\n";
fclose(back_up);
}
/*
Îáðàáàòûâàåò çàïðîñ ïîëüçîâàòåëÿ(ââîä) äëÿ îïðåäåëåíèÿ äèàïîçîíà íîìåðîâ,
êîòîðûå íóæíî âûâåñòè(ò.å., òå íîìåðà, êîòîðûå >= start, è <= finish).
Òàê æå, ýòà ôóíêöèÿ îòâå÷àåò çà ñàì âûâîä ýòîãî íà ýêðàí.
*/
void print_range(int *F, int n)
{
int start, finish;
if (tip) cout << "If you want to break, enter -1\n";
while (true)
{
if (tip_encumbering) cout << "Input start number: ";
cin >> start; if (start == -1) break;
if (tip_encumbering) cout << "Input finish number: ";
cin >> finish; if (finish == -1) break;
if (start > finish)
{
cout << "The start number is greater than finish number. Try again\n";
}
else
{
if (n == 0) {cout << "Range is empty\n\n"; break;}
else
{
int a = bin_search_in_range(F, 1, n, start, true);
int b = bin_search_in_range(F, 1, n, finish, false);
if (a == -1 || b == -1) cout << "Range is empty\n\n";
else print(F, a, b);
break;
}
}
}
}
/*
Ïðîèçâîäèò íàñòðîéêè ïîäñêàçîê.
*/
void tip_install()
{
char s[20];
if (tip) cout << "If you want to break, then enter 'exit'\n";
while (true)
{
if (tip) cout << "\nTip install command: ";
cin >> s;
if (!strcmp(s, "exit")) break;
else if (!strcmp(s, "print"))
{
cout << "\n";
cout << "tip_encumbering is really bad tip!\n";
cout << "tip is normal thing\n";
cout << "tip_important is really important thing\n\n";
}
else if (!strcmp(s, "tip"))
{
while (true)
{
char command[10];
if (tip) cout << "If you want to break, then enter 'exit'\n";
if (tip_encumbering) cout << "Tip command(off or on): ";
cin >> command;
if (!strcmp(command, "exit")) break;
else if (!strcmp(command, "off")) {tip = false; break;}
else if (!strcmp(command, "on")) {tip = true; break;}
}
break;
}
else if (!strcmp(s, "tip_important"))
{
while (true)
{
char command[10];
if (tip) cout << "If you want to break, then enter 'exit'\n";
if (tip_encumbering) cout << "Tip_important command(off or on): ";
cin >> command;
if (!strcmp(command, "exit")) break;
else if (!strcmp(command, "off")) {tip_important = false; break; }
else if (!strcmp(command, "on")) {tip_important = true; break;}
}
break;
}
else if (!strcmp(s, "tip_encumbering"))
{
while (true)
{
char command[10];
if (tip) cout << "If you want to break, then enter 'exit'\n";
if (tip_encumbering) cout << "Tip_encumbering command(off or on): ";
cin >> command;
if (!strcmp(command, "exit")) break;
else if (!strcmp(command, "off")) {tip_encumbering = false; break;}
else if (!strcmp(command, "on")) {tip_encumbering = true; break; }
}
break;
}
else if (!strcmp(s, "all"))
{
while (true)
{
char command[10];
if (tip) cout << "If you want to break, then enter 'exit'\n";
if (tip_encumbering) cout << "All tip command(off or on): ";
cin >> command;
if (!strcmp(command, "exit")) break;
else if (!strcmp(command, "off")) {
tip_encumbering = false; tip_important = false; tip = false; break;}
else if (!strcmp(command, "on")) {
tip_encumbering = true; tip_important = true; tip = true; break;}
}
break;
}
}
}
/*
Âûâîäèò íà ýêðàí ñîîáùåíèå. Åñëè key = true, òî on. Else, off.
Íóæíî äëÿ òîãî, ÷òîáû êîä áûë ìåíüøåãî îáúåìà, èáî ýòî ïðèìåíÿåòñÿ â tip_print.
*/
void logic_show(bool key)
{
if (key) cout << "on";
else cout << "off";
}
void tip_print()
{
cout << "\n\n";
cout << "tip_encumbering: "; logic_show(tip_encumbering);
cout << "\ntip: "; logic_show(tip);
cout << "\ntip_important: "; logic_show(tip_important);
cout << "\n\n";
}
void Add(int *F, int number)
{
if (Count+q_add == 1 || bin_search(F, 1, Count+q_add-1, number) == -1){
F[Count+q_add] = number;
q_add++;
Counting_Sort(F, Count+q_add-1);
}
}
/*
Ïðîèçâîäèò äîáàâëåíèå ýëåìåíòîâ èç äèàïîçîíà(ò.å., íàïðèìåð, îò 1 äî 50)
*/
void add_range(int *F)
{
int n = Count + q_add - 1; /// Ðàçìåð ìàññèâà Solve_Problems[1..n]
if (tip) cout << "If you want to break, enter '-1'\n\n";
while (true)
{
int start, finish;
if (tip_encumbering) cout << "Input start number: ";
cin >> start;
if (start == -1) break;
if (tip_encumbering) cout << "Input finish number: ";
cin >> finish; if (finish == -1) break;
if (start < 0 || start > finish) cout << "Input not correct. Try again.\n\n";
else
{
int count = finish-start+1;
int buff[count+1], w = 0; /// íóæíî äëÿ òîãî, ÷òîáû äîáàâèòü â áóôôåð
for ( ; start <= finish; start++)
{
if (bin_search(F, 1, Count+q_add-1, start) == -1)
{
buff[++w] = start;
}
Add(F, start);
}
Buffer_Save(buff, w, 1); /// äîáàâëÿåì â áóôôåð äîáàâëåííûå ýëåìåíòû
return;
}
}
}
void Delete(int *F, int number)
{
if (Count + q_add > 1) /// åñëè åñòü õîòü ÷òî-òî â ìàññèâå
{
int q = bin_search(F, 1, Count+q_add, number);
if (q != -1)
{
F[q] = inf;
q_add--;
Counting_Sort(F, Count+q_add);
}
}
}
/*
Ïðîèçâîäèò óäàëåíèå âñåõ íîìåðîâ ñ äàííîãî äèàïîçîíà(ò.å., íàïðèìåð, ñ 1 ïî 50 íîìåðà)
*/
void delete_range(int *F)
{
int n = Count + q_add - 1; /// Ðàçìåð ìàññèâà Solve_Problems[1..n]
if (tip) cout << "If you want to break, enter '-1'\n\n";
while (true)
{
int start, finish;
if (tip_encumbering) cout << "Input start number: ";
cin >> start;
if (start == -1) break;
if (tip_encumbering) cout << "Input finish number: ";
cin >> finish; if (finish == -1) break;
if (start < F[1] || finish > F[n] || start > finish)
{
while (true)
{
cout << "You sure, that range is correct?";
if (tip) cout << "(yes or no)"; cout << ": ";
char command[10];
cin >> command;
if (!strcmp(command, "yes"))
{
int buf_count = 0, start1 = start; /// ñîõðàíÿåì start, èáî îí
/// áóäåò ïîòåðÿí â öèêëå íèæå
for ( ; start <= finish; start++)
{
if (start <= F[n] && start >= F[1])
{
if (bin_search(F, 1, n, start) != -1)
buf_count++;
}
else if (start > F[n]) break;
}
/*
Ïðîõîäèì åùå ðàç ýòîò ïðîìåæóòîê, ïî ñëåäóþùèì ïðè÷èíàì:
Äåëî â òîì, ÷òî ïîëüçîâàòåëü ìîæåò ââåñòè ñëèøêîì áîëüøîé ïðîìåæóòîê, íàïðèìåð
îò -2 äî 100000000. Ïðîãðàììà ôèêñèðóåò ìîìåíò, êîãäà ìû äîøëè äî ñàìîãî
áîëüøîãî ÷èñëà èç ìàññèâà, è âûõîäèò èç öèêëà, ÷òîáû áåññìûññëåííî íå
ïðîñìàòðèâàòü îñòàëüíûå ÷èñëà, êîòîðûå, î÷åâèäíî, ò.ê. áîëüøå ñàìîãî áîëüøîãî
÷èñëà èç ìàññèâà, íå íàõîäÿòñÿ â ìàññèâå. Òàê âîò, ìû áû ïðîõîäèëè ýòîò
ïðîìåæóòîê 1 ðàç, åñëè áû çàðàíåå ñîçäàëè áû íóæíîãî ðàçìåðà ìàññèâ, äëÿ çàïèñè
åãî â áóôôåð. Îäíàêî, ïîëüçîâàòåëü ìîæåò ââåñòè ëþáîãî ðàçìåðà ïðîìåæóòîê, òàê
÷òî, äëÿ áåçîïàñíîñòè îò àâàðèéíûõ ñèòóàöèé è èñïîëüçîâàíèÿ êàê ìîæíî ìåíüøåãî
îáúåìà ïàìÿòè, ìû ïðîéäåìñÿ åùå ðàç ïî ýòîìó ïðîìåæóòêó, íî óæå çíàÿ, ñêîëüêî
ýëåìåíòîâ íóæíî óäàëèòü(ìû ýòî ïîäñ÷èòûâàëè, êîãäà ïðîõîäèëèñü 1 ðàç ïî
ïðîìåæóòêó).
*/
int buff[buf_count+1], w = 0;
for ( ; start1 <= finish; start1++)
{
if (start1 <= F[n] && start1 >= F[1])
{
if (bin_search(F, 1, n, start1) != -1)
buff[++w] = start1;
Delete(F, start1);
}
else if (start1 > F[n]) break;
}
Buffer_Save(buff, w, -1); /// äîáàâëÿåì â áóôôåð
return; /// âûõîäèì íàôèã èç ôóíêöèè
}
else if (!strcmp(command, "no")) break;
}
}
else
{
int buf_count = finish-start+1;
int buff[buf_count+1], w = 0;
for ( ; start <= finish; start++)
{
if (bin_search(F, 1, Count+q_add-1, start) != -1)
buff[++w] = start;
Delete(F, start);
}
Buffer_Save(buff, w, -1); /// Äîáàâëÿåì â áóôôåð
return;
}
}
}
/* â ôàéëå Back_Back_Up íà ïåðâîì ìåñòå èäåò flag - ïîêàçûâàåò, ÷òî ìû äåëàåì ñ
äàííûìè íîìåðàìè. Åñëè flag = 1 - äîáàâëåíèå, flag = 0 - óäàëåíèå, flag = -1 - ïóñòî
*/
void Back_Back_Up(int *T, int count, bool flag)
{
FILE *back_back_up = fopen(".tcs/Back_Back_Up.txt", "w");
if (count == -1) {fprintf(back_back_up, "%d%s", -1, "\n"); fclose(back_back_up);return;}
fprintf(back_back_up, "%d%s", flag, "\n");
fprintf(back_back_up, "%d%s", count, "\n");
for (int i = 1; i <= count; i++)
fprintf(back_back_up, "%d%s", T[i], " ");
fclose(back_back_up);
}
void Back_Up(int *F)
{
FILE *back_up = fopen(".tcs/Back_Up.txt", "w");
fprintf(back_up, "%d%s", Count+q_add-1, "\n");
for (int i = 1; i < Count + q_add; i++)
fprintf(back_up, "%d%s", F[i], " ");
fclose(back_up);
}
/*
Êîïèðóåò ôàéë Buffer, â ôàéë Buf_Buffer
*/
void Buffer_Copy()
{
FILE *buffer = fopen(".tcs/Buffer.txt", "r");
FILE *buf_buffer = fopen(".tcs/Buf_Buffer.txt", "w");
int data, Count, count;
fscanf(buffer, "%d",&Count);///ñ÷èòûâàåì Count
fprintf(buf_buffer, "%d%s%", Current_position, "\n");
for (int i = 0; i < Current_position; i++)
{
fscanf(buffer, "%d", &count);
fprintf(buf_buffer, "%d%s", count, " ");
for (int j = 0; j < count + 1; j++) /// ñ÷èòûâàåì äî count + 1, èáî îïåðàöèÿ òîæå ó÷èòûâàåòñÿ
{
fscanf(buffer, "%d", &data);
fprintf(buf_buffer, "%d%s", data, " ");
}
fprintf(buf_buffer, "%s", "\n");
}
fclose(buffer);
fclose(buf_buffer);
}
/*
Çàïèñûâàåò â ìàññèâ Buffer äîïîëíèòåëüíûå äàííûå äëÿ áóôåðà
*/
void Buffer_Save(int *Buf, int n, int flag) /// flag = 1 - äîáàâëåíèå, -1 - óäàëåíèå
{
Buffer_Copy(); /// äåëàåì êîïèþ ôàéëà Buffer.txt
/// Äåëàåì îáðàòíóþ êîïèþ
FILE *buffer = fopen(".tcs/Buffer.txt", "w");
FILE *buf_buffer = fopen(".tcs/Buf_Buffer.txt", "r");
int data, Count, count;
fscanf(buf_buffer, "%d", &Count);
fprintf(buffer, "%d%s", Count+1, "\n");
Current_position++;
for (int i = 0; i < Count; i++)
{
fscanf(buf_buffer, "%d", &count);
fprintf(buffer, "%d%s", count, " ");
for (int j = 0; j < count + 1; j++)
{
fscanf(buf_buffer, "%d", &data);
fprintf(buffer, "%d%s", data, " ");
}
fprintf(buffer, "%s", "\n");
}
/// Ïîñëå ïî÷òè ïîëíîé êîïèè(êðîìå ïåðâûõ ÷èñåë, óâåëè÷åííûõ íà 1)
/// ìîæíî ïðèñòóïàòü ê äîáàâëåíèþ íîâîé ñòðîêè
fprintf(buffer, "%d%s%d%s", n, " ", flag, " ");
for (int i = 1; i <= n; i++)
fprintf(buffer, "%d%s", Buf[i], " ");
fclose(buffer);
fclose(buf_buffer);
}
/*
Ïðîèçâîäèò ÀÊÊÓÐÀÒÍÎÅ äîáàâëåíèå íîâîé èíôîðìàöèè â áóôôåð.
Èìååòñÿ ââèäó, ÷òî íåëüçÿ ïðîñòî ñîõðàíÿòü â ìàññèâ ñïèñîê çàäà÷, êîòîðûå
ïîëüçîâàòåëü õî÷åò äîáàâèòü\óäàëèòü, èáî ñðåäè íèõ ìîãóò áûòü çàäà÷è, êîòîðûå
óæå ÿâëÿþòñÿ ðåøåííûìè, è åñëè ìû èõ äîáàâèì â áóôôåð, òî ïðè âûçîâå
ïîëüçîâàòåëåì áóôôåðà äëÿ âîññòàíîâëåíèÿ äàííûõ, íåêîòîðûå äàííûå ìîãóò
íåïðàâèëüíî äîáàâèòüñÿ îáðàòíî, èëè, äàæå óäàëèòüñÿ! Èç-çà ýòîãî, ïðè äîáàâëåíèè
â áóôåð, ìû äîëæíû ïðîâåðèòü, åñòü ëè ýòîò ýëåìåíò â ìàññèâå èëè åãî íåòó.
Åñëè ïîëüçîâàòåëü õî÷åò äîáàâèòü ýëåìåíò, òî ìû äîëæíû ïðîâåðèòü, ÷òî åãî íåòó
â ñïèñêå ðåøåííûõ çàäà÷. À åñëè õî÷åò óäàëèòü, òî ïðîâåðÿåì, ÷òî òàêîé ýëåìåíò
åñòü â ñïèñêå.
flag = 1 - çíà÷èò äîáàâëåíèå, -1 - óäàëåíèå
*/
void Add_Buffer(int *Solve_Problems, int *F, int n, int flag)
{
if (flag == 1)
{
int buff[n+1], q = 0;
for (int i = 1; i <= n; i++)
if (bin_search(Solve_Problems, 1, Count+q_add-1, F[i]) == -1)
{
buff[++q] = F[i];
}
if (q > 0) Buffer_Save(buff, q, 1);
}
else if (flag == -1)
{
int buff[n+1], q = 0;
for (int i = 1; i <= n; i++)
if (bin_search(Solve_Problems, 1, Count+q_add-1, F[i]) != -1)
buff[++q] = F[i];
if (q > 0) Buffer_Save(buff, q, -1);
}
}
/*
Ïðîèçâîäèò çàïèñü èç ôàéëà Buffer â ìàññèâ Solve_Problems
Ò.å., ôàêòè÷åñêè, îòâå÷àåò çà âîññòàíîâëåíèå èç äàííûõ èç áóôôåðà.
if back = 1, òî ñ÷èòûâàåì íàçàä èç áóôåðà(òî, ÷òî áûëî)
else, if back = 0, òî ñ÷èòûâàåì âïåðåä
*/
void Buffer_overwrite(int *solve_problems, int back)
{
int data, Count, count, operation;
FILE *buffer = fopen(".tcs/Buffer.txt", "r");
fscanf(buffer, "%d", &Count);
back--;
Current_position -= back;
if (Current_position > Count) Current_position--;
/// åñëè ìîæíî ñ÷èòàòü áóôôåð, òî ñ÷èòûâàåì
if (Current_position >= 1 && Current_position <= Count)
{
for (int j = 0; j < Current_position - 1; j++) /// äîõîäèì äî íóæíîé íàì
{ /// ñ÷èòûâàåìîé ñòðîêè
fscanf(buffer, "%d%d", &count, &operation);
for (int k = 0; k < count; k++) fscanf(buffer, "%d", &data);
}
fscanf(buffer, "%d%d", &count, &operation);
if (!back) operation *= -1; /// ìåíÿåì îïåðàöèþ íà ïðîòèâîïîëîæíóþ
/// åñëè èäåì íàçàä. Åñëè âïåðåä, òî íå ìåíÿåì, à îñòàâëÿåì êàê åñòü
if (operation == 1) /// äîáàâëåíèå
{
for (int i = 0; i < count; i++)
{
fscanf(buffer, "%d", &data);
Add(solve_problems, data);
}
}
else if (operation == -1) /// óäàëåíèå
{
for (int i = 0; i < count; i++)
{
fscanf(buffer, "%d", &data);
Delete(solve_problems, data);
}
}
if (!back) Current_position --;
}
fclose(buffer);
}
/*
Ôóíêöèÿ, âûâîäÿùàÿ íà ýêðàí ïîìîùü â êîìàíäàõ BP.
*/
void help_BP()
{
cout << "\n";
cout << "Command 'add' adds items to a file BP\n";
cout << "Construction: add count number1 ... number_count[add 2 67 94]\n\n";
cout << "Command 'delete' deletes items to a file BP\n";
cout << "Construction: delete count number1 ... number_count [delete 2 56 87]\n\n";
cout << "Command 'add:' adds a task list\n";
cout << "Construction: add start finish [add: 34 57]\n\n";
cout << "Command 'delete' deletes a task list\n";
cout << "Construction: delete start finish [delete: 1 20]\n\n";
cout << "Command 'copy' copies the BP file to the BBP file\n";
cout << "Construction: copy\n\n";
cout << "Command 'search' searches for an item in the BP file\n";
cout << "Construction: search number [search 127]\n\n";
cout << "Command 'print' displays the BP file\n";
cout << "Construction: print\n\n";
cout << "Command 'print:' displays the range of tasks from the BP file\n";
cout << "Construction: print: start finish [print: 25 68]\n\n";
}
/*
Èùåò â îäíîì èç ôàéëîâ (BP or BBP)
Return -1, åñëè íå íàøåë.  ïðîòèâíîì ñëó÷àå, âîçâðàùàåò íîìåð ñòðîêè â ôàéëå
Îäíîâðåìåííî ñ ýòèì, ãëîáàëüíàÿ ïåðåìåííàÿ Position
(â ñëó÷àå, åñëè íàøåëñÿ ýëåìåíò)
âîçâðàùàåò ïîçèöèþ â ñòðîêå(ò.å. íîìåð ñòîëáöà), ãäå ëåæèò íóæíûé ýëåìåíò.
*/
int search_in_BP(int key, bool flag)
{
FILE *f;
if (flag)
{
f = fopen(".tcs/Base_Problems.txt", "r");
}
else
{
f = fopen(".tcs/Base_Base_Problems.txt", "r");
}
int Count, count;
fscanf(f, "%d", &Count);
for (int i = 0; i < Count; i++)
{
fscanf(f, "%d", &count);
int H[count+1];
for (int j = 0; j < count; j++)
{
fscanf(f, "%d", &H[j+1]);
}
int ind = bin_search(H, 1, count, key);
if (ind != -1) /// íàøëè ýëåìåíò
{
Position = ind;
return (i+1);
}
}
return -1;
}
/*
if flag == true, òî Ïðîèçâîäèò êîïèðîâàíèå ôàéëà BP â ôàéë BBP.
Else, íàîáîðîò, êîïèþ BBP â BP.
*/
void Copy_BP(bool flag)
{
FILE *bp;
FILE *bbp;
if (flag)
{
bp = fopen(".tcs/Base_Problems.txt", "r");
bbp = fopen(".tcs/Base_Base_Problems.txt", "w");
}
else
{
bp = fopen(".tcs/Base_Base_Problems.txt", "r");
bbp = fopen(".tcs/Base_Problems.txt", "w");
}
int data, Count, count;
fscanf(bp, "%d", &Count);
fprintf(bbp, "%d%s", Count, "\n");
for (int i = 0; i < Count; i++)
{
fscanf(bp, "%d", &count);
fprintf(bbp, "%d%s", count, " ");
for (int j = 0; j < count; j++)
{
fscanf(bp, "%d", &data);
fprintf(bbp, "%d%s", data, " ");
}
fprintf(bbp, "%s", "\n");
}
fclose(bp); fclose(bbp);
}
/*
Ïðîèçâîäèò îáíîâëåííóþ êîïèþ ôàéëà BBP â ôàéë BP ñ ó÷åòîì êîððåêòèðîâêè
êîë-âà Count(óâåëè÷èâàåì íà 1) è äîáàâëåíèåì â êîíöå ôàéëà íîâîãî ìàññèâà
*/
void Copy_new_BP(int *H, int q)
{
FILE *bp = fopen(".tcs/Base_Problems.txt", "w");
FILE *bbp = fopen(".tcs/Base_Base_Problems.txt", "r");
int data, Count, count;
fscanf(bbp, "%d", &Count);
fprintf(bp, "%d%s", Count+1, "\n");
for (int i = 0; i < Count; i++)
{
fscanf(bbp, "%d", &count);
fprintf(bp, "%d%s", count, " ");
for (int j = 0; j < count; j++)
{
fscanf(bbp, "%d", &data);
fprintf(bp, "%d%s", data, " ");
}
fprintf(bp, "%s", "\n");
}
fprintf(bp, "%d%s", q, " ");
for (int i = 0; i < q; i++)
fprintf(bp, "%d%s", H[i], " ");
fclose(bp); fclose(bbp);
}
/*
Ôóíêöèÿ ïðîèçâîäèò äîáàâëåíèå(ñîõðàíåíèå) íîìåðîâ, ïðè ýòîì ïðåäúÿâëÿåòñÿ ìàññèâ
F, èçâåñòåí åãî ðàçìåð(îí íà÷èíàåòñÿ ñ 1), ââîäèòñÿ ñòàðò è ôèíèø.
Ïðîèçâîäèì äîáàâëåíèå îò ñòàðò äî ôèíèøà òîëüêî òåõ íîìåðîâ, êîòîðûå ëåæàò â F.
Íîìåðà start and finish ïðåäïîëàãàþòñÿ ââåäåííûìè êîððåêòíî!
*/
void Save_in_BP(int *F, int n, int start, int finish)
{
int st = bin_search(F, 1, n, start);
int fn = bin_search(F, 1, n, finish);
if (st <= fn)
{
if (st != -1 && fn != -1)
{
int H[fn-st+1], q = 0;
for (int i = st; i <= fn; i++)
H[q++] = F[i];
Add_in_BP(H, q);
}
}
}
/*
Ïðîèçâîäèò äîáàâëåíèå â ôàéë BP ñïèñêà çàäà÷, êîòîðûå ïîëüçîâàòåëü ðåøèë
ñîõðàíèòü(÷òîáû ðåøèòü ïîòîì). Íóæíî ïðîïèñàòü ñèñòåìó îïðåäåëåíèÿ àëãîðèòìà
ïîäñ÷åòà(åñëè ïàìÿòè ìíîãî, òî ïóñòü âûäåëÿåò ïàìÿòü, à íå ïðîõîäèò 2 ðàçà
öèêë, ðàäè ñîõðàíåíèÿ ïàìÿòè. Åñëè æå, íàîáîðîò, î÷åíü îïàñíàÿ ñèòóàöèÿ ñ
ïàìÿòüþ, òî ïóñòü æåðòâóåòñÿ ñêîðîñòü ðàáîòû, íî íå âûäåëÿåòñÿ ìíîãî ïàìÿòè,
íåæåëè íåîáõîäèìî(èáî ïîäâèñàíèå ñèñòåìû íà ìàëåíüêîå âðåìÿ ëó÷øå àâàðèéíîãî
âûõîäà èç ïðîãðàììû. Òàê æå, ïåðåïîëíåíèå îïàñíî äëÿ âñåé ñèñòåìû, èáî ìîãóò
áûòü ñòåðòû âàæíûå ôàéëû äëÿ ÎÑ).
*/
void Add_in_BP(int *F, int n) /// Ìàññèâ F íà÷èíàåòñÿ ñ 0 è äî n-1 !!!
{
Copy_BP(true); /// êîïèðóåì ôàéë BP â BBP
int q = 0; /// ñ÷åò÷èê
/*
Öèêëîì ïðîñìàòðèâàåì, ñêîëüêî çàäà÷ èç ìàññèâà F íóæíî äîáàâèòü â BP(èáî
íåêîòîðûå èç çàäà÷ òàì óæå ëåæàò)
*/
for (int i = 0; i < n; i++)
if (search_in_BP(F[i], false) == -1) q++;
int H[q], w = 0;
/*
Ïðîáëåìà â òîì, ÷òî ìû âî 2 ðàç ïðîõîäèìñÿ ïî ìàññèâó, ïðè ýòîì ïðîèçâîäÿ
ïîèñê â îãðîìíîì ôàéëå BP, âìåñòî òîãî, ÷òîáû ÷óòü áîëüøå âûäåëèòü ïàìÿòè.
Ýòî íåâûãîäíî ïî âðåìåíè, ïîýòîìó, ïîçæå, ëó÷øå ïðîðàáîòàòü ñèñòåìó îïðå-
äåëåíèÿ íàãðóçêè íà ïàìÿòü è â ñëó÷àå, åñëè ïàìÿòü åñòü, òî âûäåëèòü
åå, à íå ïðîõîäèòüñÿ ïî ìíîãó ðàç ïî îãðîìíûì ôàéëàì!
*/
for (int i = 0; i < n; i++)
if (search_in_BP(F[i], false) == -1) H[w++] = F[i];///Ýòî íå âûãîäíî ïî âðåìåíè
if (q) /// åñëè åñòü ÷òî äîáàâèòü íîâîãî â ôàéë BP
{
Copy_new_BP(H, q);
}
}
/*
Äîñòàòî÷íî äîëãîå(ïî âðåìåíè) óäàëåíèå. Èäåÿ çàêëþ÷àåòñÿ â òîì, ÷òî
ìû, âíà÷àëå, êîïèðóåì ôàéë BP â BBP, ïîñëå ÷åãî íà÷èíàåì èäòè ïî BBP.
Êàæäóþ ñòðî÷êó BBP ñðàíèâàåì íà íàëè÷èå â ìàññèâå F. Åñëè åñòü ýëåìåíòû, òî
íå äîáàâëÿåì èõ â íîâûé ôàéë BP.
*/
void Delete_in_BP(int *F, int n) /// ìàññèâ F íà÷èíàåòñÿ ñ 1 è äî n !!!
{
Copy_BP(true);
FILE *bp = fopen(".tcs/Base_Problems.txt", "w");
FILE *bbp = fopen(".tcs/Base_Base_Problems.txt", "r");
int data, Count, count, new_Count = 0; /// new_Count - íîâûé ñ÷åò÷èê äëÿ
/// ôàéëà BP, ÷òîáû ïîñëå çàïèñè òåõ çàäà÷, êîòîðûå íå ïîäëåæàò óäàëåíèþ, ìû
/// çàïèñàëè íàø ôàéë â íîðìàëüíîì âèäå(íåîáõîäèìîì äëÿ êîððåêòíîé ðàáîòû
/// è êîððåêòíîãî ñïîñîáà õðàíåíèÿ èíôîðìàöèè)
fscanf(bbp, "%d", &Count);
for (int i = 0; i < Count; i++)
{
fscanf(bbp, "%d", &count);
int H[count], q = 0; /// ìàññèâ, â êîòîðûé áóäåì äîáàâëÿòü òå çàäà÷è, êîòîðûõ
/// íåòó â ìàññèâå F(äëÿ çàïèñè â ôàéë BP). q - ñ÷åò÷èê
for (int j = 0; j < count; j++)
{
fscanf(bbp, "%d", &data);
if (bin_search(F, 1, n, data) == -1) H[q++] = data;
}
if (q) /// åñëè åñòü ÷òî äîáàâèòü â ôàéë BP, òî...
{
fprintf(bp, "%d%s", q, " ");
for (int k = 0; k < q; k++)
fprintf(bp, "%d%s", H[k], " ");
fprintf(bp, "%s", "\n");
new_Count++;
}
}
fclose(bp); fclose(bbp);
/// Òåïåðü ïåðåçàïèøåì íàø ôàéë â íîðìàëüíîì âèäå(âíà÷àëî íóæíî äîáàâèòü
/// new_Count, âñå îñòàëüíîå îñòàâèòü êàê åñòü(ïîñëå èçìåíåíèé).
bbp = fopen(".tcs/Base_Base_Problems.txt", "w");
bp = fopen(".tcs/Base_Problems.txt", "r");
fprintf(bbp, "%d%s", new_Count, "\n");
for (int i = 0; i < new_Count; i++)
{
fscanf(bp, "%d", &count);
fprintf(bbp, "%d%s", count, " ");
for (int j = 0; j < count; j++)
{
fscanf(bp, "%d", &data);
fprintf(bbp, "%d%s", data, " ");
}
fprintf(bbp, "%s", "\n");
}
fclose(bp); fclose(bbp);
Copy_BP(false);
}
/*
Ïðîõîäèòñÿ ïî ôàéëó BP è íàõîäèò ìàêñèìàëüíûé è ìèíèìàëüíûé ýëåìåíòû, è çàïè-
ñûâàåò èõ(ò.å. ýòè çíà÷åíèÿ ìàêñèìóìà è ìèíèìóìà) â ïåðåìåííûå Min_in_BP and
Max_in_BP. Ýòî íóæíî äëÿ Delete_range_in_BP.
*/
void Extremum_in_BP()
{
FILE *f = fopen(".tcs/Base_Problems.txt", "r");
int Min = inf, Max = -inf;
int Count, count, data;
fscanf(f, "%d", &Count);
for (int i = 0; i < Count; i++)
{
fscanf(f, "%d", &count);
for (int j = 0; j < count; j++)
{
fscanf(f, "%d", &data);
if (data > Max) Max = data;
if (data < Min) Min = data;
}
}
if (Count) /// åñëè â BP åñòü õîòü ÷òî-òî, òî òîëüêî òîãäà ìåíÿåì
{
Min_in_BP = Min;
Max_in_BP = Max;
}
}
/*
Ïðîèçâîäèò óäàëåíèå èç ôàéëà BP ïîñëåäîâàòåëüíî, îïðåäåëåííîãî ïîëüçîâàòåëåì,
ñïèñêà íîìåðîâ. Áóäåò ðåàëèçîâûâàòüñÿ ñëåäóþùèì îáðàçîì:
Ò.ê. ïîëüçîâàòåëü ìîæåò ââåñòè ñëèøêîì îãðîìíûé äèàïîçîí, òî ìû áóäåì äåëàòü
âñå àêêóðàòíî: áóäåì ñìîòðåòü íà òå íîìåðà, êîòîðûå ââåë ïîëüçîâàòåëü, è åñëè
íîìåðà âûõîäÿò çà ïðåäåëû ìàêñèìàëüíî è ìèíèìàëüíî âîçìîæíîãî â ôàéëå BP, òî
ìû èãíîðèì îñòàòîê, ýòî äàñò íàì ïðåèìóùåñòâî â ñêîðîñòè ðàáîòû è îãðàäèò îò
âîçìîæíîãî çàâèñàíèÿ ñèñòåìû.
*/
void Delete_range_in_BP()
{
Extremum_in_BP(); /// îáíîâèëè ìèí è ìàêñ â ÂÐ
int start, finish;
while (true)
{
if (tip_encumbering) cout << "Input start number: ";
cin >> start;
if (tip_encumbering) cout << "Input finish number: ";
cin >> finish;
if (start < Min_in_BP || finish > Max_in_BP)
{
cout << "Are you sure, that is's correct? ";
char s[15];
cin >> s;
if (!strcmp(s, "yes")) break;
}
else break;
}
if (start < Min_in_BP) start = Min_in_BP;
if (finish > Max_in_BP) finish = Max_in_BP;
int H[finish-start+2], q = 0;
for ( ; start <= finish; start++)
H[++q] = start;
Delete_in_BP(H, q);
}
/*
Åùå îäíà òàêàÿ æå ôóíêöèÿ, íî òóò óæå çàðàíåå èçâåñòíû çíà÷åíèÿ start and finish
Îíè íå ïðîâåðÿþòñÿ íà êîððåêòíîñòü, èáî âûçîâ ïðîèçâîäèòñÿ âíóòðè ïðîãðàììû, à
íå ïîëüçîâàòåëåì, òàê ÷òî îøèáîê çäåñü áûòü íå ìîæåò(ïîýòîìó íåò ïðîâåðêè
êîððåêòíîñòè ââîäà start and finish).
*/
void Delete_range_in_BP(int start, int finish)
{
if (start < Min_in_BP) start = Min_in_BP;
if (finish > Max_in_BP) finish = Max_in_BP;
int H[finish-start+2], q = 0;
for ( ; start <= finish; start++)
H[++q] = start;
Delete_in_BP(H, q);
}
/*
Ïðîèçâîäèò óäàëåíèå âñåõ çàäà÷ â ôàéëå BP.
*/
void delete_all_in_BP()
{
Extremum_in_BP();
Delete_range_in_BP(Min_in_BP, Max_in_BP);
}
void add_range_in_BP()
{
Extremum_in_BP(); /// îáíîâèëè ìèí è ìàêñ â ÂÐ
int start, finish;
while (true)
{
if (tip_encumbering) cout << "Input start number: ";
cin >> start;
if (tip_encumbering) cout << "Input finish number: ";
cin >> finish;
if (start < 0) cout << "Not correct. Start number < 0. Try again\n";
else if (finish > inf) cout << "Not correct. Finish number > inf. Try again\n";
else break;
}
int H[finish-start+1], q = 0;
for ( ; start <= finish; start++)
H[q++] = start;
Add_in_BP(H, q);
}
/*
Ïðîèçâîäèò âûâîä çàäà÷, êîòîðûå áûëè ïîñëåäíèìè äîáàâëåíû â BP.
*/
void print_last_BP()
{
FILE *f = fopen(".tcs/Base_Problems.txt", "r");
int Count, count, data;
fscanf(f, "%d", &Count);
if (!Count) cout << "In the BP file there are no tasks\n\n";
else
{
for (int i = 0; i < Count-1; i++)
{
fscanf(f, "%d", &count);
for (int j = 0; j < count; j++)
{
fscanf(f, "%d", &data);
}
}