Advertisement
green1ant

3_1 *2

Nov 25th, 2018
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.93 KB | None | 0 0
  1. program Laba_3_1;
  2. {$APPTYPE CONSOLE}
  3. uses
  4.   SysUtils;
  5.  
  6. type
  7.    TList = array of Integer;
  8.    TRomans = array of string;
  9.    TInputMode = (WithFile, Console);
  10.  
  11. resourcestring
  12.    InstructionMessage
  13.       = 'This program converts numbers to the Roman numeral system.';
  14.    ModeOfInputMessage = 'Where do you want to input data from? [F]ile or [C]onsole';
  15.    IncorrectInputFilePathMessage
  16.       = 'Incorrect input file path, check if file exists and try again';
  17.    IncorrectInputModeMessage = 'Incorrect way of input, choose [F]ile or [C]onsole';
  18.    OutputFileExistsErrorMessage
  19.       = 'Incorrect output file, such file already exists, try again';
  20.    ShouldCreateMessage = 'Do you want to save output data into file? [Y]es or [N]o';
  21.    ShouldCreateErrorMessage = 'Incorrect answer, choose [Y]es or [N]o';
  22.    SuccessfullySavedMessage = 'Output data was successfully saved into ';
  23.    InputFilePathMessage = 'Enter input file path';
  24.    OutputFilePathMessage = 'Enter output file path';
  25.    EnterAmountMessage = 'Enter amount of elements of your sequence';
  26.    EnterElemsMessage = 'Enter elements';
  27.    IncorrectFileDataError
  28.       = 'Error! Your input file contains invalid data, correct this and try again';
  29.    IncorrectConsoleInputMessage
  30.       = 'Error! It should be natural number from 1 to 2147483676';
  31.  
  32. function GetInputFilePath: string;
  33. var
  34.    Path: string;
  35. begin
  36.    Writeln(InputFilePathMessage);
  37.    Readln(Path);
  38.    while not FileExists(Path) do
  39.    begin
  40.       Writeln(IncorrectInputFilePathMessage);
  41.       Readln(Path);
  42.    end;
  43.    GetInputFilePath := Path;
  44. end;
  45.  
  46. function ShouldCreateOutputFile: Boolean;
  47. var
  48.    Answer: string;
  49. begin
  50.    Writeln(ShouldCreateMessage);
  51.    Readln(Answer);
  52.    Answer := LowerCase(Answer);
  53.    while (Answer <> 'y') and (Answer <> 'n') do
  54.    begin
  55.       Writeln(ShouldCreateErrorMessage);
  56.       Readln(Answer);
  57.    end;
  58.    ShouldCreateOutputFile := Answer = 'y';
  59. end;
  60.  
  61. function GetOutputFilePath: string;
  62. var
  63.    Path: string;
  64. begin
  65.    Writeln(OutputFilePathMessage);
  66.    Readln(Path);
  67.    while FileExists(Path) do
  68.    begin
  69.       Writeln(OutputFileExistsErrorMessage);
  70.       Readln(Path);
  71.    end;
  72.    GetOutputFilePath := Path;
  73. end;
  74.  
  75. function ChooseInputMode: TInputMode;
  76. var
  77.    Mode: string;
  78. begin
  79.    Writeln(ModeOfInputMessage);
  80.    Readln(Mode);
  81.    Mode := LowerCase(Mode);
  82.    while (Mode <> 'c') and (Mode <> 'f') do
  83.    begin
  84.       Writeln(IncorrectInputModeMessage);
  85.       Readln(Mode);
  86.    end;
  87.    if Mode = 'f' then
  88.       ChooseInputMode := WithFile
  89.    else
  90.       ChooseInputMode := Console;
  91. end;
  92.  
  93. function ReadFile(var InputFile: TextFile; Len: Integer): TList;
  94. var
  95.    Sequence: TList;
  96.    i: Integer;
  97. begin
  98.    SetLength(Sequence, Len);
  99.    Reset(InputFile);
  100.    i := 0;
  101.    while not EoF(InputFile) do
  102.    begin
  103.       Read(InputFile, Sequence[i]);
  104.       Inc(i);
  105.    end;
  106.    CloseFile(InputFile);
  107.    ReadFile := Sequence;
  108. end;
  109.  
  110. function GetFileLength(var InputFile: TextFile): Integer;
  111. var
  112.    AssumedLength, Item: Integer;
  113.    HasError: Boolean;
  114. begin
  115.    AssumedLength := 0;
  116.    GetFileLength := -1;
  117.    HasError := False;
  118.    try
  119.      
  120.       while not EoF(InputFile) do
  121.       begin
  122.          Read(InputFile, Item);
  123.          if (Item < 1) or (Item > 2000) then
  124.             HasError := True;
  125.          Inc(AssumedLength);
  126.       end;
  127.  
  128.       if HasError then
  129.          Writeln('Every number in file should be from 1 to 2000. Try again')
  130.       else
  131.          GetFileLength := AssumedLength;
  132.    except
  133.       Writeln(IncorrectFileDataError);
  134.    end;
  135. end;
  136.  
  137. function ReadConsole: TList;
  138. var
  139.    N, i: Integer;
  140.    IsCorrect: Boolean;
  141.    List: TList;
  142. begin
  143.    IsCorrect := False;
  144.    Writeln(EnterAmountMessage);
  145.    repeat
  146.       try
  147.          Readln(N);
  148.          IsCorrect := True;
  149.       except
  150.          Writeln(IncorrectConsoleInputMessage);
  151.       end;
  152.    until IsCorrect;
  153.    SetLength(List, N);
  154.    Writeln(EnterElemsMessage);
  155.    for i := 0 to N - 1 do
  156.    begin
  157.       IsCorrect := False;
  158.       repeat
  159.          try
  160.             Readln(List[i]);
  161.             if (List[i] < 1) or (List[i] > 2000) then
  162.                Writeln('Number should be from 1 to 2000')
  163.             else
  164.                IsCorrect := True;
  165.          except
  166.             Writeln(IncorrectConsoleInputMessage);
  167.          end;
  168.       until IsCorrect;
  169.    end;
  170.    ReadConsole := List;
  171. end;
  172.  
  173. function Translate(Num: Integer): string;
  174. var
  175.    Pattern, Triple, Roman, Temp: string;
  176.    i, j, Divider, Digit, Whole, Remainder: Integer;
  177. begin
  178.    Pattern := 'IVXLCDMMM';
  179.    Roman := '';
  180.    Divider := 1000;
  181.    for i := 1 to 4 do
  182.    begin
  183.       Temp := '';
  184.       Digit := Num div Divider mod 10;
  185.       Divider := Divider div 10;
  186.       //Writeln(Digit);
  187.  
  188.       Whole := Digit div 5;
  189.       Remainder := Digit mod 5;
  190.  
  191.       Triple := Copy(Pattern, 8 - (2*i - 1), 3);
  192.       //Writeln('Triple: ', Triple);
  193.  
  194.       if Whole = 1 then
  195.          Temp := Temp + Triple[2];
  196.  
  197.  
  198.       if (Remainder > 0) and (Remainder < 4) then
  199.          for j := 1 to Remainder do
  200.             Temp := Temp + Triple[1];
  201.  
  202.       if Remainder = 4 then
  203.          if Whole = 0 then
  204.             Temp := Triple[1] + Triple[2]
  205.          else
  206.             Temp := Triple[1] + Triple[3];
  207.       //Temp := Temp + ' ';
  208.       //Writeln(Temp);
  209.       Roman := Roman + Temp;
  210.  
  211.       //Writeln('--------------');
  212.    end;
  213.  
  214.    //Writeln('Roman: ', Roman);
  215.    Translate := Roman;
  216. end;
  217.  
  218. function ConvertListToRoman(List: TList): TRomans;
  219. var
  220.    i: Integer;
  221.    Romans: TRomans;
  222. begin
  223.    SetLength(Romans, Length(List));
  224.    for i := 0 to Length(List) - 1 do
  225.       Romans[i] := Translate(List[i]);
  226.  
  227.    ConvertListToRoman := Romans;
  228. end;
  229.  
  230. procedure WriteRomans(var OutputFile: TextFile; Romans: TRomans; List: TList);
  231. var
  232.    i: Integer;
  233. begin
  234.     for i := 0 to Length(Romans) - 1 do
  235.       Writeln(OutputFile, List[i], ' = ', Romans[i]);
  236. end;
  237.  
  238. procedure Main;
  239. var
  240.    Len: Integer;
  241.    List, ListToFile: TList;
  242.    InputFile, OutputFile: TextFile;
  243.    OutputFilePath: string;
  244.    Romans, RomansToFile: TRomans;
  245.  
  246.    Number: Integer;
  247. begin
  248.    Writeln(InstructionMessage);
  249.    case ChooseInputMode() of
  250.       WithFile:
  251.       begin
  252.          Assign(InputFile, GetInputFilePath());
  253.          Reset(InputFile);
  254.          Len := GetFileLength(InputFile);
  255.          if Len <> - 1 then
  256.             List := ReadFile(InputFile, Len);
  257.       end;
  258.       Console:
  259.          List := ReadConsole();
  260.    end;
  261.  
  262.    if Len <> -1 then
  263.    begin
  264.       Romans := ConvertListToRoman(List);
  265.       //RomansToFile := Copy(Romans);
  266.       WriteRomans(Output, Romans, List);
  267.       if ShouldCreateOutputFile() then
  268.       begin
  269.          OutputFilePath := GetOutputFilePath();
  270.          AssignFile(OutputFile, OutputFilePath);
  271.          Rewrite(OutputFile);
  272.          WriteRomans(OutputFile, Romans, List);
  273.          Writeln(SuccessfullySavedMessage, OutputFilePath);
  274.          CloseFile(OutputFile);
  275.       end;
  276.    end;
  277.    Writeln('Fin!');
  278.    Readln;
  279. end;
  280.  
  281. begin
  282.   Main();
  283. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement