Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program laba3_1;
- uses
- System.SysUtils;
- function InputPath(): String; Forward;
- function InputLineFromConsole(): String; Forward;
- function InputLineFromFile(Path: String): String; Forward;
- function CheckLine(Line: String): Boolean; Forward;
- procedure OutputToFile(Path: String; RomanNumber: String; SourceNumber: String); Forward;
- procedure OutputToConsole(RomanNumber: String; SourceNumber: String); Forward;
- function ChooseSourceOfInput(): Integer;
- var
- Choice: Integer;
- IsCorrect: Boolean;
- begin
- Writeln('Choose where to enter data. Enter 1 or 2:');
- Writeln('1.File');
- Writeln('2.Console');
- repeat
- IsCorrect := True;
- try
- Readln(Choice);
- except
- Writeln('Enter an integer!');
- IsCorrect := False;
- end;
- if (IsCorrect) and (Choice <> 1) and (Choice <> 2) then
- begin
- Writeln('Enter 1 or 2!');
- IsCorrect := False;
- end;
- until IsCorrect;
- ChooseSourceOfInput := Choice;
- end;
- function InputLine(Source: Integer): String;
- var
- Line, PathInput: String;
- begin
- Case Source of
- 1:
- begin
- Writeln('Enter the absolute link to the input file');
- PathInput := InputPath();
- Line := InputLineFromFile(PathInput);
- end;
- 2:
- begin
- Line := InputLineFromConsole();
- end;
- end;
- InputLine := Line;
- end;
- function InputPath(): String;
- var
- Path: String;
- IsCorrect: Boolean;
- begin
- repeat
- IsCorrect := True;
- Readln(Path);
- if not FileExists(Path) then
- begin
- IsCorrect := False;
- Writeln('File not found! Enter the absolute link to the file');
- end;
- until IsCorrect;
- InputPath := Path;
- end;
- function InputLineFromFile(Path: String): String;
- var
- Line: String;
- InputFile: TextFile;
- IsCorrect: Boolean;
- begin
- AssignFile(InputFile, Path);
- Reset(InputFile);
- if not eof(InputFile) then
- Readln(InputFile, Line)
- else
- begin
- Writeln('The line is missing from your file! Enter it from console');
- Line := InputLineFromConsole();
- end;
- IsCorrect := CheckLine(Line);
- if not IsCorrect then
- begin
- Line := InputLineFromConsole();
- end;
- CloseFile(InputFile);
- InputLineFromFile := Line;
- end;
- function InputLineFromConsole(): String;
- var
- Line: String;
- IsCorrect: Boolean;
- begin
- Writeln('Enter the line');
- repeat
- IsCorrect := True;
- Readln(Line);
- IsCorrect := CheckLine(Line);
- until IsCorrect;
- InputLineFromConsole := Line;
- end;
- function CheckLine(Line: String): Boolean;
- var
- Flag: Boolean;
- Number: Integer;
- begin
- Flag := True;
- if Length(Line) = 0 then
- begin
- Writeln('You entered an empty line! Repeat enter');
- Flag := False;
- end;
- if Flag then
- begin
- try
- Number := StrToInt(Line);
- except
- Writeln('This line cannot be represented as an integer! Enter line from console');
- Flag := False
- end;
- end;
- if ((Number < 1) or (Number > 2000)) and (Flag) then
- begin
- Writeln('The number must be in the range from 1 to 2000 Enter line from console');
- Flag := False;
- end;
- CheckLine := Flag;
- end;
- function ConvertToRoman(SourceNumber: String): String;
- Const
- UnitsOfRoman: Array [0..9] of String = ('','I','II','III','IV','V','VI','VII','VIII','IX');
- TensOfRoman: Array [0..9] of String = ('','X','XX','XXX','XL','L','LX','LXX','LXXX','XC');
- HundredsOfRoman: Array [0..9] of String = ('','C','CC','CCC','CD','D','DC','DCC','DCCC','CM');
- ThousandsOfRoman: Array [0..2] of String = ('','M','MM');
- var
- Digits: Array [0..3] of Integer;
- I, J: Integer;
- RomanNumber: String;
- begin
- for I := 0 to 3 do
- Digits[I] := 0;
- J := 1;
- for I := 4 - Length(SourceNumber) to 3 do
- begin
- Digits[I] := StrToInt(SourceNumber[J]);
- Inc(J);
- end;
- RomanNumber := ThousandsOfRoman[Digits[0]] + HundredsOfRoman[Digits[1]] + TensOfRoman[Digits[2]] + UnitsOfRoman[Digits[3]];
- ConvertToRoman := RomanNumber;
- end;
- procedure OutputResult(Source: Integer; RomanNumber: String; SourceNumber: String);
- var
- PathOutput: String;
- begin
- if Source = 1 then
- begin
- Writeln;
- Writeln('Enter the absolute link to the output file');
- PathOutput := InputPath();
- OutputToFile(PathOutput, RomanNumber, SourceNumber);
- end
- else
- begin
- OutputToConsole(RomanNumber, SourceNumber);
- end;
- end;
- procedure OutputToFile(Path: String; RomanNumber: String; SourceNumber: String);
- var
- OutputFile: TextFile;
- begin
- AssignFile(OutputFile, Path);
- Reset(OutputFile);
- Rewrite(OutputFile);
- Writeln(OutputFile, SourceNumber, ' = ',RomanNumber);
- CloseFile(OutputFile);
- Writeln('The data is successfully recorded in the file');
- end;
- procedure OutputToConsole(RomanNumber: String; SourceNumber: String);
- begin
- Writeln(SourceNumber, ' = ',RomanNumber);
- end;
- procedure Main();
- var
- Source: Integer;
- SourceNumber, RomanNumber: String;
- begin
- Writeln('The program converts the number to the Roman numeral system');
- Source := ChooseSourceOfInput();
- SourceNumber := InputLine(Source);
- RomanNumber := ConvertToRoman(SourceNumber);
- OutputResult(Source, RomanNumber, SourceNumber);
- Readln;
- end;
- begin
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment