Vanya_Shestakov

laba3.1 (Delphi)

Oct 25th, 2020
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.58 KB | None | 0 0
  1. program laba3_1;
  2. uses
  3.   System.SysUtils;
  4.  
  5. function InputPath(): String; Forward;
  6. function InputLineFromConsole(): String; Forward;
  7. function InputLineFromFile(Path: String): String; Forward;
  8. function CheckLine(Line: String): Boolean; Forward;
  9. procedure OutputToFile(Path: String; RomanNumber: String; SourceNumber: String); Forward;
  10. procedure OutputToConsole(RomanNumber: String; SourceNumber: String); Forward;
  11.  
  12. function ChooseSourceOfInput(): Integer;
  13. var
  14.     Choice: Integer;
  15.     IsCorrect: Boolean;
  16. begin
  17.     Writeln('Choose where to enter data. Enter 1 or 2:');
  18.     Writeln('1.File');
  19.     Writeln('2.Console');
  20.     repeat
  21.         IsCorrect := True;
  22.         try
  23.             Readln(Choice);
  24.         except
  25.             Writeln('Enter an integer!');
  26.             IsCorrect := False;
  27.         end;
  28.  
  29.         if (IsCorrect) and (Choice <> 1) and (Choice <> 2) then
  30.         begin
  31.             Writeln('Enter 1 or 2!');
  32.             IsCorrect := False;
  33.         end;
  34.     until IsCorrect;
  35.     ChooseSourceOfInput := Choice;
  36. end;
  37.  
  38. function InputLine(Source: Integer): String;
  39. var
  40.     Line, PathInput: String;
  41. begin
  42.     Case Source of
  43.         1:
  44.         begin
  45.             Writeln('Enter the absolute link to the input file');
  46.             PathInput := InputPath();
  47.             Line := InputLineFromFile(PathInput);
  48.         end;
  49.         2:
  50.         begin
  51.             Line := InputLineFromConsole();
  52.         end;
  53.     end;
  54.     InputLine := Line;
  55. end;
  56.  
  57. function InputPath(): String;
  58. var
  59.     Path: String;
  60.     IsCorrect: Boolean;
  61. begin
  62.     repeat
  63.         IsCorrect := True;
  64.         Readln(Path);
  65.  
  66.         if not FileExists(Path) then
  67.         begin
  68.             IsCorrect := False;
  69.             Writeln('File not found! Enter the absolute link to the file');
  70.         end;
  71.     until IsCorrect;
  72.     InputPath := Path;
  73. end;
  74.  
  75. function InputLineFromFile(Path: String): String;
  76. var
  77.     Line: String;
  78.     InputFile: TextFile;
  79.     IsCorrect: Boolean;
  80. begin
  81.     AssignFile(InputFile, Path);
  82.     Reset(InputFile);
  83.     if not eof(InputFile) then
  84.         Readln(InputFile, Line)
  85.     else
  86.     begin
  87.         Writeln('The line is missing from your file! Enter it from console');
  88.         Line := InputLineFromConsole();
  89.     end;
  90.  
  91.     IsCorrect := CheckLine(Line);
  92.     if not IsCorrect then
  93.     begin
  94.         Line := InputLineFromConsole();
  95.     end;
  96.  
  97.     CloseFile(InputFile);
  98.     InputLineFromFile := Line;
  99. end;
  100.  
  101. function InputLineFromConsole(): String;
  102. var
  103.     Line: String;
  104.     IsCorrect: Boolean;
  105. begin
  106.     Writeln('Enter the line');
  107.     repeat
  108.         IsCorrect := True;
  109.         Readln(Line);
  110.         IsCorrect := CheckLine(Line);
  111.     until IsCorrect;
  112.     InputLineFromConsole := Line;
  113. end;
  114.  
  115. function CheckLine(Line: String): Boolean;
  116. var
  117.     Flag: Boolean;
  118.     Number: Integer;
  119. begin
  120.     Flag := True;
  121.  
  122.     if Length(Line) = 0 then
  123.     begin
  124.         Writeln('You entered an empty line! Repeat enter');
  125.         Flag := False;
  126.     end;
  127.  
  128.     if Flag then
  129.     begin
  130.         try
  131.             Number := StrToInt(Line);
  132.         except
  133.             Writeln('This line cannot be represented as an integer! Enter line from console');
  134.             Flag := False
  135.         end;
  136.     end;
  137.  
  138.     if ((Number < 1) or (Number > 2000)) and (Flag) then
  139.     begin
  140.         Writeln('The number must be in the range from 1 to 2000 Enter line from console');
  141.         Flag := False;
  142.     end;
  143.  
  144.     CheckLine := Flag;
  145. end;
  146.  
  147. function ConvertToRoman(SourceNumber: String): String;
  148. Const
  149.     UnitsOfRoman: Array [0..9] of String = ('','I','II','III','IV','V','VI','VII','VIII','IX');
  150.     TensOfRoman: Array [0..9] of String = ('','X','XX','XXX','XL','L','LX','LXX','LXXX','XC');
  151.     HundredsOfRoman: Array [0..9] of String = ('','C','CC','CCC','CD','D','DC','DCC','DCCC','CM');
  152.     ThousandsOfRoman: Array [0..2] of String = ('','M','MM');
  153. var
  154.     Digits: Array [0..3] of Integer;
  155.     I, J: Integer;
  156.     RomanNumber: String;
  157. begin
  158.     for I := 0 to 3 do
  159.         Digits[I] := 0;
  160.  
  161.     J := 1;
  162.     for I := 4 - Length(SourceNumber) to 3 do
  163.     begin
  164.         Digits[I] := StrToInt(SourceNumber[J]);
  165.         Inc(J);
  166.     end;
  167.     RomanNumber := ThousandsOfRoman[Digits[0]] + HundredsOfRoman[Digits[1]] + TensOfRoman[Digits[2]] + UnitsOfRoman[Digits[3]];
  168.     ConvertToRoman := RomanNumber;
  169. end;
  170.  
  171. procedure OutputResult(Source: Integer; RomanNumber: String; SourceNumber: String);
  172. var
  173.     PathOutput: String;
  174. begin
  175.     if Source = 1 then
  176.     begin
  177.         Writeln;
  178.         Writeln('Enter the absolute link to the output file');
  179.         PathOutput := InputPath();
  180.         OutputToFile(PathOutput, RomanNumber, SourceNumber);
  181.     end
  182.     else
  183.     begin
  184.         OutputToConsole(RomanNumber, SourceNumber);
  185.     end;
  186. end;
  187.  
  188. procedure OutputToFile(Path: String; RomanNumber: String; SourceNumber: String);
  189. var
  190.     OutputFile: TextFile;
  191. begin
  192.     AssignFile(OutputFile, Path);
  193.     Reset(OutputFile);
  194.     Rewrite(OutputFile);
  195.     Writeln(OutputFile, SourceNumber, ' = ',RomanNumber);
  196.     CloseFile(OutputFile);
  197.     Writeln('The data is successfully recorded in the file');
  198. end;
  199.  
  200. procedure OutputToConsole(RomanNumber: String; SourceNumber: String);
  201. begin
  202.     Writeln(SourceNumber, ' = ',RomanNumber);
  203. end;
  204.  
  205. procedure Main();
  206. var
  207.     Source: Integer;
  208.     SourceNumber, RomanNumber: String;
  209. begin
  210.     Writeln('The program converts the number to the Roman numeral system');
  211.     Source := ChooseSourceOfInput();
  212.     SourceNumber := InputLine(Source);
  213.     RomanNumber := ConvertToRoman(SourceNumber);
  214.     OutputResult(Source, RomanNumber, SourceNumber);
  215.     Readln;
  216. end;
  217.  
  218. begin
  219.     Main;
  220. end.
Advertisement
Add Comment
Please, Sign In to add comment