-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunJudge17.pas
1891 lines (1608 loc) · 50.1 KB
/
unJudge17.pas
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
{$MODE DELPHI}
////////////////////////////////////////////////////////////////////////////////
/// ///
/// Турнирная система Contester (www.contester.ru) ///
/// Вспомогательный модуль для тестирующих программ (чекеров) ///
/// unJudge v1.7.7 от 24.09.10. FreePascal 2.0.4+, Delphi 7+ ///
/// Клопов Игорь, Ковровская Государственная Технологическая Академия ///
/// По всем вопросам обращайтесь: www.contester.ru, [email protected] ///
/// ///
////////////////////////////////////////////////////////////////////////////////
// До начала работы чекера создаются три объекта -
// InputTxt, OutputTxt и PatternTxt -
// входной файл, выходной файл работы решения и эталонный выходной файл.
// Все три - объекты класса TJudgeFile. Порядок работы с TJudgeFile и
// этими объектами прмерно таков:
// InputTxt.ReadLongInt(a); // читает LongInt с текущей строки
// // если конец строки => тогда PE
// // если данные не являются LongInt => PE
//
// InputTxt.NextLine; // если на этой строке еще есть данные => PE
// // если это последняя строка файла => PE
// // иначе переходит на следующую строку
//
// InputTxt.ReadLongInt(b);
//
// InputTxt.MustBeEof; // если впереди есть данные => PE
//
// OutputTxt.ReadLongInt(s);
//
// OutputTxt.MustBeEof; // сначала надо вычитать все данные из Output
// // (проверить PE), а затем уже сравнивать (WA)
//
// if a + b <> s then VRejectWA;
//
// VAccept; // если выполнение чекера до этой строки
// // не прервалось на Reject, тогда Accept
// Рекомендуется проверять решения именно таким образом - сначала вычитывать
// весь InputTxt, затем весь PatternTxt, затем весь OutputTxt. После этого
// вычислять и сравнивать. Это позволит чекерам правильно определять PE,
// а также поможет проверке самих чекеров при внесении их в систему.
unit unJudge17;
interface
// {$DEFINE DEBUG}
const
GVerdictInternalCheckerInput = 3;
GVerdictInternalUnknownError = 3;
GVerdictRejectPresentationError = 2;
GVerdictRejectWrongAnswer = 1;
GVerdictAccept = 0;
GVerdictIsReject: array[0..12] of Boolean =
(False, True, True, True, True, True, True, True, True, True, True, True, True);
GVerdictIsAccept: array[0..12] of Boolean =
(True, False, False, False, False, False, False, False, False, False, False, False, False);
type
TFileType = (ftInput, ftOutput, ftPattern);
type
Boat = Extended;
function ProcessLine(var S: AnsiString; const X: AnsiString): Boolean;
function ProcessChar(var C: AnsiChar; const X: AnsiChar): Boolean;
function ProcessString(var S: AnsiString; const X: AnsiString): Boolean;
function ProcessLongInt(var I: LongInt; const X: AnsiString): Boolean;
function ProcessLongWord(var W: LongWord; const X: AnsiString): Boolean;
function ProcessLongLong(var N: Int64; const X: AnsiString): Boolean;
function ProcessFloat(var F: Boat; const X: AnsiString): Boolean;
function HashFloat(const F: Boat; const P: Boat): LongInt;
procedure Init();
type
TJudgeFile = class(TObject)
private
FType: TFileType;
FName: String;
FMemory: PAnsiChar;
FSize, FPosition, FRow: Cardinal;
FPrecision: Boat;
procedure SetSize(NewSize: Cardinal);
protected
function CheckBof: Boolean;
function CheckEof: Boolean;
function CheckLF: Boolean;
function CheckCRLF: Boolean;
function CheckSpace: Boolean;
function CheckValid: Boolean;
//
procedure SkipSpaces;
procedure SkipSpacesBackwards;
//
function FetchOneOctetIfCan(var X: AnsiChar): Boolean;
function FetchManyOctetsIfCan(var X: AnsiString): Boolean;
function FetchLineIfCan(var X: AnsiString): Boolean;
public
constructor Create(AType: TFileType);
destructor Destroy; override;
procedure Reset;
procedure Clear;
procedure LoadFromString(S: AnsiString);
procedure LoadFromFile(const FileName: String);
property Precision: Boat read FPrecision write FPrecision;
property Row: Cardinal read FRow;
//
function Eoln: Boolean;
function Eof: Boolean;
function SeekEoln: Boolean;
function SeekEof: Boolean;
//
procedure NextLine;
procedure MustBeEof;
//
procedure ReadLine(var L: AnsiString); // вся строка до eoln
//
procedure ReadChar(var C: AnsiChar); overload; // один символ
procedure ReadChar(var C1, C2: AnsiChar); overload;
procedure ReadChar(var C1, C2, C3: AnsiChar); overload;
procedure ReadChar(var C1, C2, C3, C4: AnsiChar); overload;
//
procedure ReadString(var S: AnsiString); overload; // строка до пробела
procedure ReadString(var S1, S2: AnsiString); overload;
procedure ReadString(var S1, S2, S3: AnsiString); overload;
procedure ReadString(var S1, S2, S3, S4: AnsiString); overload;
//
procedure ReadLongInt(var I: LongInt); overload;
procedure ReadLongInt(var I1, I2: LongInt); overload;
procedure ReadLongInt(var I1, I2, I3: LongInt); overload;
procedure ReadLongInt(var I1, I2, I3, I4: LongInt); overload;
//
procedure ReadLongWord(var W: LongWord); overload;
procedure ReadLongWord(var W1, W2: LongWord); overload;
procedure ReadLongWord(var W1, W2, W3: LongWord); overload;
procedure ReadLongWord(var W1, W2, W3, W4: LongWord); overload;
//
procedure ReadLongLong(var N: Int64); overload;
procedure ReadLongLong(var N1, N2: Int64); overload;
procedure ReadLongLong(var N1, N2, N3: Int64); overload;
procedure ReadLongLong(var N1, N2, N3, N4: Int64); overload;
//
procedure ReadFloat(var F: Boat); overload;
procedure ReadFloat(var F1, F2: Boat); overload;
procedure ReadFloat(var F1, F2, F3: Boat); overload;
procedure ReadFloat(var F1, F2, F3, F4: Boat); overload;
//
function CompareFloats(F1, F2: Boat): Boolean; // с учетом Precision
end;
procedure CompareOutputAndPattern(AConsiderWA: Boolean);
procedure VInternalCI(AReport: String = '');
procedure VRejectPE(AReport: String = '');
procedure VRejectWA(AReport: String = '');
procedure VAccept(AReport: String = '');
////////////////////////////////////////////////////////////////////////////////
/// ///
/// TTestLibStream. Эмулятор TestLib. Отображает inf, ouf и ans. ///
/// Для совместимости TestLib-чекеров с Contester. ///
/// ///
////////////////////////////////////////////////////////////////////////////////
type
TTestLibResult = (_OK, _WA, _PE, _FAIL, _DIRT);
{ _OK - accepted,
_WA - wrong answer,
_PE - output format mismatch,
_FAIL - when everything ***** up
_DIRT - for inner using }
TTestLibStream = class(TObject)
private
FJudgeFile: TJudgeFile;
public
constructor Create(AJudgeFile: TJudgeFile);
function Eof: Boolean;
function SeekEof: Boolean;
function Eoln: Boolean;
function SeekEoln: Boolean;
procedure NextLine;
function ReadLongInt: Integer;
function ReadLongLong: Int64;
function ReadInteger: Integer;
function ReadReal: Extended;
function ReadString: AnsiString;
procedure Quit(Res: TTestLibResult; Report: String);
end;
InStream = TTestLibStream;
procedure Quit(Res: TTestLibResult; Report: String);
var
InputTxt: TJudgeFile;
OutputTxt: TJudgeFile;
PatternTxt: TJudgeFile;
WF: TJudgeFile;
inf: TTestLibStream;
ouf: TTestLibStream;
ans: TTestLibStream;
implementation
uses
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
{$IFDEF LINUX}
//Libc,
{$ENDIF}
SysUtils, Classes;
{$IFDEF VER150}
{$DEFINE VCL7ORABOVE}
{$ENDIF}
{$IFDEF VER160}
{$DEFINE VCL7ORABOVE}
{$ENDIF}
{$IFDEF VER170}
{$DEFINE VCL7ORABOVE}
{$ENDIF}
{$IFDEF VER180}
{$DEFINE VCL7ORABOVE}
{$ENDIF}
{$IFDEF VER185}
{$DEFINE VCL7ORABOVE}
{$ENDIF}
{$IFDEF VER190}
{$DEFINE VCL7ORABOVE}
{$ENDIF}
{$IFDEF VER200}
{$DEFINE VCL7ORABOVE}
{$ENDIF}
{$IFDEF VER210}
{$DEFINE VCL7ORABOVE}
{$ENDIF}
{$IFDEF DEBUG}
var
GLastVerdict: Integer;
{$ENDIF}
procedure DropToReportTxt(Report: String);
const
CRLF: AnsiString = #13#10;
var
ReportTxt: TFileStream;
begin
if FileExists('report.txt') then
ReportTxt := TFileStream.Create('report.txt', fmOpenReadWrite)
else ReportTxt := TFileStream.Create('report.txt', fmCreate);
ReportTxt.Seek(0, soFromEnd);
ReportTxt.Write(PChar(Report)^, Length(Report) * SizeOf(Char));
ReportTxt.Write(PAnsiChar(CRLF)^, Length(CRLF) * SizeOf(AnsiChar));
ReportTxt.Free;
end;
procedure Verdict(AVerdict: Integer; AType: TFileType; AReport: String = '');
begin
{$IFDEF DEBUG}
if GLastVerdict = 0 then GLastVerdict := AVerdict;
{$ELSE}
if GVerdictIsReject[AVerdict] and (AType in [ftInput, ftPattern]) then begin
AVerdict := GVerdictInternalCheckerInput; // ошибка в чекере или в input.txt
end;
InputTxt.Free;
OutputTxt.Free;
PatternTxt.Free;
if AReport <> '' then begin
DropToReportTxt(AReport);
end;
Halt(AVerdict); // exit-код для передачи в Contester
{$ENDIF}
end;
procedure VInternalCI(AReport: String = '');
begin
Verdict(GVerdictInternalCheckerInput, ftInput, AReport);
end;
procedure VRejectPE(AReport: String = '');
begin
Verdict(GVerdictRejectPresentationError, ftOutput, AReport);
end;
procedure VRejectWA(AReport: String = '');
begin
Verdict(GVerdictRejectWrongAnswer, ftOutput, AReport);
end;
procedure VAccept(AReport: String = '');
begin
Verdict(GVerdictAccept, ftOutput, AReport);
end;
function ProcessLine(var S: AnsiString; const X: AnsiString): Boolean;
begin
S := X;
Result := True;
end;
function ProcessChar(var C: AnsiChar; const X: AnsiChar): Boolean;
begin
C := X;
Result := True;
end;
function ProcessString(var S: AnsiString; const X: AnsiString): Boolean;
begin
S := X;
Result := True;
end;
function ProcessLongInt(var I: LongInt; const X: AnsiString): Boolean;
var
Code: Integer;
begin
Result := False;
if Length(X) = 0 then Exit;
Val(String(X), I, Code);
if Code <> 0 then Exit;
Result := True;
end;
function ProcessLongWord(var W: LongWord; const X: AnsiString): Boolean;
var
Code: Integer;
X64: Int64;
begin
Result := False;
if Length(X) = 0 then Exit;
Val(String(X), X64, Code);
if Code <> 0 then Exit;
if X64 < 0 then Exit;
if X64 > $FFFFFFFF then Exit;
W := X64;
Result := True;
end;
function ProcessLongLong(var N: Int64; const X: AnsiString): Boolean;
var
Code: Integer;
begin
Result := False;
if Length(X) = 0 then Exit;
Val(String(X), N, Code);
if Code <> 0 then Exit;
Result := True;
end;
function ProcessFloat(var F: Boat; const X: AnsiString): Boolean;
var
E: Extended;
{$IFDEF VCL7ORABOVE}
FS: TFormatSettings;
{$ENDIF}
begin
Result := False;
if Length(X) = 0 then Exit;
if Pos('E', String(X)) > 0 then Exit;
if Pos('e', String(X)) > 0 then Exit;
{$IFDEF VCL7ORABOVE}
FS.DecimalSeparator := '.';
if not TextToFloat(PAnsiChar(X), E, fvExtended, FS) then Exit;
{$ELSE}
if not TextToFloat(PAnsiChar(X), E, fvExtended) then Exit;
{$ENDIF}
F := E;
Result := True;
end;
function HashFloat(const F: Boat; const P: Boat): LongInt;
begin
Result := Round(F / P);
end;
{ TJudgeFile }
constructor TJudgeFile.Create(AType: TFileType);
begin
inherited Create;
FType := AType;
FName := 'TJudgeFile';
FPrecision := 0.01;
end;
destructor TJudgeFile.Destroy;
begin
Clear;
inherited;
end;
procedure TJudgeFile.SetSize(NewSize: Cardinal);
begin
if NewSize = FSize then Exit;
if NewSize = 0 then begin
if FSize > 0 then begin
FreeMem(FMemory);
FMemory := nil;
end;
end else begin
if FSize > 0 then begin
ReallocMem(FMemory, NewSize);
end else begin
GetMem(FMemory, NewSize)
end;
end;
FSize := NewSize;
end;
procedure TJudgeFile.Reset;
begin
FPosition := 0;
FRow := 1;
end;
procedure TJudgeFile.Clear;
begin
SetSize(0);
end;
procedure TJudgeFile.LoadFromString(S: AnsiString);
begin
SetSize(Length(S));
Move(PAnsiChar(S)[0], FMemory^, FSize);
Reset;
end;
procedure TJudgeFile.LoadFromFile(const FileName: String);
var
FileStream: TFileStream;
begin
if FileExists(FileName) then
FileStream := TFileStream.Create(FileName, fmOpenRead)
else FileStream := TFileStream.Create(FileName, fmCreate);
FileStream.Seek(0, 0);
SetSize(FileStream.Size);
FileStream.Read(FMemory^, FSize);
FileStream.Free;
FName := FileName;
Reset;
end;
function TJudgeFile.CheckBof: Boolean;
begin
Result := (FPosition = 0);
end;
function TJudgeFile.CheckEof: Boolean;
begin
Result := (FPosition >= FSize);
end;
function TJudgeFile.CheckLF: Boolean;
begin
Result := (FMemory[FPosition] = #10);
end;
function TJudgeFile.CheckCRLF: Boolean;
begin
Result := (FPosition + 2 <= FSize);
if not Result then Exit;
Result := (FMemory[FPosition] = #13) and
(FMemory[FPosition + 1] = #10);
end;
function TJudgeFile.CheckSpace: Boolean;
begin
Result := (FMemory[FPosition] = ' ');
end;
function TJudgeFile.CheckValid: Boolean;
begin
Result := (FMemory[FPosition] >= ' ');
end;
procedure TJudgeFile.SkipSpaces;
begin
while True do begin
if CheckEof then Break;
if not CheckSpace then Break;
Inc(FPosition);
end;
end;
procedure TJudgeFile.SkipSpacesBackwards;
begin
while True do begin
if CheckBof then Break;
Dec(FPosition);
if not CheckSpace then begin
Inc(FPosition);
Break;
end;
end;
end;
function TJudgeFile.FetchOneOctetIfCan(var X: AnsiChar): Boolean;
begin
SkipSpaces;
Result := not CheckEof;
if not Result then Exit;
if CheckValid then begin
X := FMemory[FPosition];
Inc(FPosition);
Result := True;
end else begin
Result := False;
end;
end;
function TJudgeFile.FetchManyOctetsIfCan(var X: AnsiString): Boolean;
var
B: Cardinal;
begin
SkipSpaces;
B := FPosition;
while True do begin
if CheckEof then Break;
if CheckSpace or CheckLF or CheckCRLF then Break;
if not CheckValid then Break;
Inc(FPosition);
end;
if FPosition > B then begin
SetString(X, PAnsiChar(@FMemory[B]), FPosition - B);
Result := True;
end else begin
Result := False;
end;
end;
function TJudgeFile.FetchLineIfCan(var X: AnsiString): Boolean;
var
B: Cardinal;
begin
SkipSpaces;
B := FPosition;
while True do begin
if CheckEof then Break;
if CheckLF or CheckCRLF then Break;
if not CheckValid then Break;
Inc(FPosition);
end;
SkipSpacesBackwards;
if FPosition > B then begin
SetString(X, PAnsiChar(@FMemory[B]), FPosition - B);
Result := True;
end else begin
Result := False;
end;
end;
function TJudgeFile.Eoln: Boolean;
begin
Result := not CheckEof;
if not Result then Exit;
Result := CheckLF or CheckCRLF;
end;
function TJudgeFile.Eof: Boolean;
begin
Result := CheckEof;
end;
function TJudgeFile.SeekEoln: Boolean;
begin
SkipSpaces;
Result := not CheckEof;
if not Result then Exit;
Result := CheckLF or CheckCRLF;
end;
function TJudgeFile.SeekEof: Boolean;
begin
while SeekEoln do NextLine;
Result := CheckEof;
end;
procedure TJudgeFile.NextLine;
begin
if not SeekEoln then Verdict(GVerdictRejectPresentationError, FType, 'eoln expected, extra data or eof found (' + FName + ', row ' + IntToStr(FRow) + ')');
if CheckCRLF then Inc(FPosition, 2)
else if CheckLF then Inc(FPosition);
Inc(FRow);
end;
procedure TJudgeFile.MustBeEof;
begin
if not SeekEof then Verdict(GVerdictRejectPresentationError, FType, 'eof expected, extra data found (' + FName + ', row ' + IntToStr(FRow) + ')');
end;
procedure TJudgeFile.ReadLine(var L: AnsiString); // вся строка до eoln
var
X: AnsiString;
begin
if not FetchLineIfCan(X) then X := ''; // пустая строка - тоже строка
if not ProcessLine(L, X) then Verdict(GVerdictRejectPresentationError, FType, String(X) + ' is not line (' + FName + ', row ' + IntToStr(FRow) + ')');
end;
procedure TJudgeFile.ReadChar(var C: AnsiChar); // один символ
var
X: AnsiChar;
begin
if not FetchOneOctetIfCan(X) then Verdict(GVerdictRejectPresentationError, FType, 'char expected, "' + String(X) + '" found (' + FName + ', row ' + IntToStr(FRow) + ')');
if not ProcessChar(C, X) then Verdict(GVerdictRejectPresentationError, FType, String(X) + ' is not char (' + FName + ', row ' + IntToStr(FRow) + ')');
end;
procedure TJudgeFile.ReadChar(var C1, C2: AnsiChar);
begin
ReadChar(C1);
ReadChar(C2);
end;
procedure TJudgeFile.ReadChar(var C1, C2, C3: AnsiChar);
begin
ReadChar(C1, C2);
ReadChar(C3);
end;
procedure TJudgeFile.ReadChar(var C1, C2, C3, C4: AnsiChar);
begin
ReadChar(C1, C2);
ReadChar(C3, C4);
end;
procedure TJudgeFile.ReadString(var S: AnsiString); // строка до пробела
var
X: AnsiString;
begin
if not FetchManyOctetsIfCan(X) then Verdict(GVerdictRejectPresentationError, FType, 'string expected, "' + String(X) + '" found (' + FName + ', row ' + IntToStr(FRow) + ')');
if not ProcessString(S, X) then Verdict(GVerdictRejectPresentationError, FType, String(X) + ' is not string (' + FName + ', row ' + IntToStr(FRow) + ')');
end;
procedure TJudgeFile.ReadString(var S1, S2: AnsiString);
begin
ReadString(S1);
ReadString(S2);
end;
procedure TJudgeFile.ReadString(var S1, S2, S3: AnsiString);
begin
ReadString(S1, S2);
ReadString(S3);
end;
procedure TJudgeFile.ReadString(var S1, S2, S3, S4: AnsiString);
begin
ReadString(S1, S2);
ReadString(S3, S4);
end;
procedure TJudgeFile.ReadLongInt(var I: LongInt);
var
X: AnsiString;
begin
if not FetchManyOctetsIfCan(X) then Verdict(GVerdictRejectPresentationError, FType, 'longint expected, "' + String(X) + '" found (' + FName + ', row ' + IntToStr(FRow) + ')');
if not ProcessLongInt(I, X) then Verdict(GVerdictRejectPresentationError, FType, String(X) + ' is not longint (' + FName + ', row ' + IntToStr(FRow) + ')');
end;
procedure TJudgeFile.ReadLongInt(var I1, I2: LongInt);
begin
ReadLongInt(I1);
ReadLongInt(I2);
end;
procedure TJudgeFile.ReadLongInt(var I1, I2, I3: LongInt);
begin
ReadLongInt(I1, I2);
ReadLongInt(I3);
end;
procedure TJudgeFile.ReadLongInt(var I1, I2, I3, I4: LongInt);
begin
ReadLongInt(I1, I2);
ReadLongInt(I3, I4);
end;
procedure TJudgeFile.ReadLongWord(var W: LongWord);
var
X: AnsiString;
begin
if not FetchManyOctetsIfCan(X) then Verdict(GVerdictRejectPresentationError, FType, 'longword expected, "' + String(X) + '" found (' + FName + ', row ' + IntToStr(FRow) + ')');
if not ProcessLongWord(W, X) then Verdict(GVerdictRejectPresentationError, FType, String(X) + ' is not longword (' + FName + ', row ' + IntToStr(FRow) + ')');
end;
procedure TJudgeFile.ReadLongWord(var W1, W2: LongWord);
begin
ReadLongWord(W1);
ReadLongWord(W2);
end;
procedure TJudgeFile.ReadLongWord(var W1, W2, W3: LongWord);
begin
ReadLongWord(W1, W2);
ReadLongWord(W3);
end;
procedure TJudgeFile.ReadLongWord(var W1, W2, W3, W4: LongWord);
begin
ReadLongWord(W1, W2);
ReadLongWord(W3, W4);
end;
procedure TJudgeFile.ReadLongLong(var N: Int64);
var
X: AnsiString;
begin
if not FetchManyOctetsIfCan(X) then Verdict(GVerdictRejectPresentationError, FType, 'longlong expected, "' + String(X) + '" found (' + FName + ', row ' + IntToStr(FRow) + ')');
if not ProcessLongLong(N, X) then Verdict(GVerdictRejectPresentationError, FType, String(X) + ' is not longlong (' + FName + ', row ' + IntToStr(FRow) + ')');
end;
procedure TJudgeFile.ReadLongLong(var N1, N2: Int64);
begin
ReadLongLong(N1);
ReadLongLong(N2);
end;
procedure TJudgeFile.ReadLongLong(var N1, N2, N3: Int64);
begin
ReadLongLong(N1, N2);
ReadLongLong(N3);
end;
procedure TJudgeFile.ReadLongLong(var N1, N2, N3, N4: Int64);
begin
ReadLongLong(N1, N2);
ReadLongLong(N3, N4);
end;
procedure TJudgeFile.ReadFloat(var F: Boat);
var
X: AnsiString;
begin
if not FetchManyOctetsIfCan(X) then Verdict(GVerdictRejectPresentationError, FType, 'float expected, "' + String(X) + '" found (' + FName + ', row ' + IntToStr(FRow) + ')');
if not ProcessFloat(F, X) then Verdict(GVerdictRejectPresentationError, FType, String(X) + ' is not float (' + FName + ', row ' + IntToStr(FRow) + ')');
end;
procedure TJudgeFile.ReadFloat(var F1, F2: Boat);
begin
ReadFloat(F1);
ReadFloat(F2);
end;
procedure TJudgeFile.ReadFloat(var F1, F2, F3: Boat);
begin
ReadFloat(F1, F2);
ReadFloat(F3);
end;
procedure TJudgeFile.ReadFloat(var F1, F2, F3, F4: Boat);
begin
ReadFloat(F1, F2, F3);
ReadFloat(F4);
end;
function TJudgeFile.CompareFloats(F1, F2: Boat): Boolean;
begin
Result := (HashFloat(F1, Precision) = HashFloat(F2, Precision));
end;
procedure CompareOutputAndPattern(AConsiderWA: Boolean);
var
DidPat, DidOut: Boolean;
PatX, OutX: AnsiString;
PatS, OutS: AnsiString;
PatN, OutN: Int64;
PatI, OutI: LongInt;
PatW, OutW: LongWord;
PatF, OutF: Boat;
begin
PatternTxt.Reset;
OutputTxt.Reset;
while True do begin
DidPat := PatternTxt.FetchManyOctetsIfCan(PatX);
DidOut := OutputTxt.FetchManyOctetsIfCan(OutX);
//
if DidPat and DidOut then begin
if not AConsiderWA then begin
Continue;
end else
if ProcessLongLong(PatN, PatX) and ProcessLongLong(OutN, OutX) then begin
if (PatN <> OutN) then begin
Verdict(GVerdictRejectWrongAnswer, ftOutput);
Break;
end;
end else
if ProcessLongInt(PatI, PatX) and ProcessLongInt(OutI, OutX) then begin
if (PatI <> OutI) then begin
Verdict(GVerdictRejectWrongAnswer, ftOutput);
Break;
end;
end else
if ProcessLongWord(PatW, PatX) and ProcessLongWord(OutW, OutX) then begin
if (PatW <> OutW) then begin
Verdict(GVerdictRejectWrongAnswer, ftOutput);
Break;
end;
end else
if ProcessFloat(PatF, PatX) and ProcessFloat(OutF, OutX) then begin
if (not OutputTxt.CompareFloats(PatF, OutF)) then begin
Verdict(GVerdictRejectWrongAnswer, ftOutput);
Break;
end;
end else
if ProcessString(PatS, PatX) and ProcessString(OutS, OutX) then begin
if (PatS <> OutS) then begin
Verdict(GVerdictRejectWrongAnswer, ftOutput);
Break;
end;
end else
begin
Verdict(GVerdictRejectPresentationError, ftOutput);
Break; // оба Fetch удались, но Process не сработал
end;
Continue;
end;
//
if DidPat or DidOut then begin // один из Fetch не удался
Verdict(GVerdictRejectPresentationError, ftOutput);
Break;
end;
//
DidPat := PatternTxt.SeekEoln;
DidOut := OutputTxt.SeekEoln;
if DidPat then PatternTxt.NextLine;
if DidOut then OutputTxt.NextLine;
if DidPat and DidOut then begin
Continue;
end;
//
if PatternTxt.Eof and OutputTxt.Eof then begin
// VAccept вызывай снаружи
Break;
end;
//
if PatternTxt.SeekEoln then PatternTxt.NextLine;
if OutputTxt.SeekEoln then OutputTxt.NextLine;
//
if PatternTxt.Eof and OutputTxt.Eof then begin
// VAccept вызывай снаружи
Break;
end;
//
Verdict(GVerdictRejectPresentationError, ftOutput);
Break;
end;
end;
{$IFDEF DEBUG}
procedure TestJudgeFile;
var
OutF: TJudgeFile;
L, S: AnsiString;
C: AnsiChar;
I: LongInt;
W: LongWord;
N: Int64;
F: Boat;
begin
OutF := TJudgeFile.Create(ftOutput);
OutF.Precision := 0.01;
////////////////////////////// тест A0
GLastVerdict := 0;
OutF.LoadFromString(
'');
OutF.ReadString(S);
Assert(S = '');
Assert(GLastVerdict = GVerdictRejectPresentationError);
GLastVerdict := 0;
OutF.LoadFromString(
'');
OutF.ReadLine(L);
Assert(L = '');
Assert(GLastVerdict = 0);
GLastVerdict := 0;
OutF.LoadFromString(
' ');
OutF.ReadString(S);
Assert(S = '');
Assert(GLastVerdict = GVerdictRejectPresentationError);
GLastVerdict := 0;
OutF.LoadFromString(
' ');
OutF.ReadLine(L);
Assert(L = '');
Assert(GLastVerdict = 0);
GLastVerdict := 0;
OutF.LoadFromString(
'' + #13#10);
OutF.ReadString(S);
Assert(S = '');
Assert(GLastVerdict = GVerdictRejectPresentationError);
GLastVerdict := 0;
OutF.LoadFromString(
'' + #13#10);
OutF.ReadLine(L);
Assert(L = '');
Assert(GLastVerdict = 0);
GLastVerdict := 0;
OutF.LoadFromString(
' ' + #13#10);
OutF.ReadString(S);
Assert(S = '');
Assert(GLastVerdict = GVerdictRejectPresentationError);
GLastVerdict := 0;
OutF.LoadFromString(
' ' + #13#10);
OutF.ReadLine(L);
Assert(L = '');
Assert(GLastVerdict = 0);
////////////////////////////// тест A1
GLastVerdict := 0;
OutF.LoadFromString(
' 45 348 ' + #13#10 +
'23 54' + #13#10 +
' 46 ' + #13#10 +
'1' + #13#10);
Assert(OutF.SeekEoln = False);
Assert(OutF.SeekEoln = False);
OutF.ReadLine(L);
Assert(L = '45 348');
Assert(OutF.SeekEoln = True);
Assert(OutF.SeekEoln = True); OutF.NextLine;
OutF.ReadLine(L);
Assert(L = '23 54');
Assert(OutF.SeekEoln = True);
Assert(OutF.SeekEoln = True); OutF.NextLine;
OutF.ReadLine(L);
Assert(L = '46');
Assert(OutF.SeekEoln = True);
Assert(OutF.SeekEoln = True); OutF.NextLine;
OutF.ReadLine(L);
Assert(L = '1');
Assert(OutF.Eof = False);
Assert(OutF.SeekEoln = True);
Assert(OutF.SeekEoln = True); OutF.NextLine;
Assert(OutF.Eof = True);
Assert(OutF.SeekEof = True);
Assert(OutF.SeekEoln = False);
Assert(OutF.SeekEoln = False);
Assert(OutF.Eof = True);
Assert(OutF.SeekEof = True);
Assert(OutF.SeekEoln = False);
Assert(OutF.SeekEoln = False);
Assert(GLastVerdict = 0);
////////////////////////////// тест A2
GLastVerdict := 0;
OutF.LoadFromString(
' 45 348 ' + #13#10 +
'23 54' + #13#10 +
' 46 ' + #13#10 +
'1' + #13#10);
Assert(OutF.SeekEoln = False);
Assert(OutF.SeekEoln = False);
OutF.ReadChar(C);
Assert(C = '4');