Advertisement
green1ant

3_1 *1

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