Advertisement
MadCortez

Untitled

Nov 13th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.86 KB | None | 0 0
  1. program laba3_1;
  2.  
  3. Uses
  4.    System.SysUtils;
  5.  
  6. procedure Main(); forward;
  7. function CheckString(s: string): Boolean; forward;
  8. procedure PrintTask; forward;
  9. function UserInputFromConsole(): string; forward;
  10. function UserInputFromFile(Path: String): string; forward;
  11. function CheckPath(Path: String): Boolean; forward;
  12. function UserOutputPath(): String; forward;
  13. procedure PrintInConsole(value: Integer; s: String); forward;
  14. procedure PrintInFile(Path: String; value: Integer; s: string); forward;
  15. function CheckFile(Path: String): Boolean; forward;
  16. function UserInputPath(): String; forward;
  17. function InputMethod: Word; forward;
  18. function OutputMethod(): Word; forward;
  19. function DeleteNotSym(s: string): string; forward;
  20. function ConvertToRim(a: Integer): String; forward;
  21. function Check4Same(s: string): string; forward;
  22. function FixNotValid(s: string): string; forward;
  23. function UserInput(): String; forward;
  24. procedure ResultPrint(value: Integer; s: string); forward;
  25.  
  26. const
  27.    sym: array[1..7] of Char = ('I', 'V', 'X', 'L', 'C', 'D', 'M');
  28.    num: array[1..7] of Integer = (1, 5, 10, 50, 100, 500, 1000);
  29.  
  30. function CheckString(s: string): Boolean;
  31. begin
  32.    s := DeleteNotSym(s);
  33.    if (Length(s) > 4) or (Length(s) = 0) then
  34.       CheckString := False
  35.    else
  36.       CheckString := True;
  37. end;
  38.  
  39. function UserInputFromConsole(): string;
  40. var
  41.    s: string;
  42. begin
  43.    repeat
  44.    Writeln('Введите строку, содержащую до 4х цифровых символов');
  45.    Readln(s);
  46.    until (CheckString(s));
  47.    UserInputFromConsole := s;
  48. end;
  49.  
  50. function UserInputFromFile(Path: String): string;
  51. var
  52.    s: string;
  53.    MyFile: TextFile;
  54. begin
  55.    AssignFile(MyFile, Path);
  56.    reset(MyFile);
  57.    Readln(MyFile, s);
  58.    closefile(MyFile);
  59.    UserInputFromFile := s;
  60. end;
  61.  
  62. function CheckPath(Path: String): Boolean;
  63. begin
  64.    if FileExists(Path) then
  65.    begin
  66.       Writeln(Path, ' существует');
  67.       CheckPath := True;
  68.    end
  69.    else
  70.    begin
  71.       Writeln(Path, ' не существует');
  72.       Writeln('Введите корректный путь к файлу');
  73.    end;
  74. end;
  75.  
  76. function UserOutputPath(): String;
  77. var
  78.    Path: String;
  79. begin
  80.    Writeln('Введите абсолютный путь к файлу для вывода результата');
  81.    Readln(Path);
  82.    UserOutputPath := Path;
  83. end;
  84.  
  85. procedure PrintInConsole(value: Integer; s: String);
  86. begin
  87.    Writeln('Введённное число: ', value);
  88.    Writeln('Данное число в римской системе счисления: ', s);
  89. end;
  90.  
  91. procedure PrintInFile(Path: String; value: Integer; s: string);
  92. var
  93.    i: Integer;
  94.    MyFile: TextFile;
  95. begin
  96.    AssignFile(MyFile, Path);
  97.    rewrite(MyFile);
  98.    Writeln(MyFile, 'Введённное число: ', value);
  99.    Writeln(MyFile, 'Данное число в римской системе счисления: ', s);
  100.    close(MyFile);
  101.    Writeln('Результат работы помещён в файл');
  102. end;
  103.  
  104. function CheckFile(Path: String): Boolean;
  105. var
  106.    IsValid: Boolean;
  107.    s: string;
  108.    MyFile: TextFile;
  109. begin
  110.    AssignFile(MyFile, Path);
  111.    reset(MyFile);
  112.    Readln(MyFile, s);
  113.    if s <> '' then IsValid := True;
  114.    close(MyFile);
  115.    CheckFile := IsValid;
  116. end;
  117.  
  118. function UserInputPath(): String;
  119. var
  120.    Path: String;
  121. begin
  122.    repeat
  123.       repeat
  124.          Writeln('Введите абсолютный путь к файлу с входными данными');
  125.          Readln(Path);
  126.       until CheckPath(Path);
  127.       if not(CheckFile(Path)) then
  128.          Writeln('Неккоректные данные в файле, исправьте файл');
  129.    until (CheckFile(Path));
  130.    UserInputPath := Path;
  131. end;
  132.  
  133. function InputMethod: Word;
  134. var
  135.    Method: Word;
  136.    IsValid: Boolean;
  137. begin
  138.    Writeln('Каким способом хотите ввести данные?');
  139.    Writeln('1 - с помощью консоли');
  140.    Writeln('2 - с помощью файла');
  141.    repeat
  142.       IsValid := True;
  143.       try
  144.          Readln(Method);
  145.       except
  146.          begin
  147.             IsValid := False;
  148.             Writeln('Введено нецелое число');
  149.          end;
  150.       end;
  151.       if IsValid then
  152.          if (Method <> 1) and (Method <> 2) then
  153.          begin
  154.             IsValid := False;
  155.             Writeln('Введите 1 или 2');
  156.          end;
  157.    until IsValid;
  158.    InputMethod := Method;
  159. end;
  160.  
  161. function OutputMethod(): Word;
  162. var
  163.    Method: Word;
  164.    IsValid: Boolean;
  165. begin
  166.    Writeln('Куда хотите вывести результат?');
  167.    Writeln('1 - в консоль');
  168.    Writeln('2 - в файл');
  169.    repeat
  170.       IsValid := True;
  171.       try
  172.          Readln(Method);
  173.       except
  174.          begin
  175.             IsValid := False;
  176.             Writeln('Введено нецелое число');
  177.          end;
  178.       end;
  179.       if IsValid then
  180.          if (Method <> 1) and (Method <> 2) then
  181.          begin
  182.             IsValid := False;
  183.             Writeln('Введите 1 или 2');
  184.          end;
  185.    until IsValid;
  186.    OutputMethod := Method;
  187. end;
  188.  
  189. function DeleteNotSym(s: string): string;
  190. var
  191.    i: Integer;
  192.    const sym = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
  193. begin
  194.    i := 1;
  195.    while (i <= length(s)) do
  196.       if not(s[i] in sym) then
  197.          delete(s, i, 1)
  198.       else
  199.          Inc(i);
  200.    DeleteNotSym := s;
  201. end;
  202.  
  203. function ConvertToRim(a: Integer): String;
  204. var
  205.    s: string;
  206.    i: Integer;
  207. begin
  208.    i := 7;
  209.    while (a > 0) do
  210.    begin
  211.       while (a div num[i] > 0) do
  212.       begin
  213.          Dec(a, num[i]);
  214.          s := s + sym[i];
  215.       end;
  216.       Dec(i);
  217.    end;
  218.    ConvertToRim := s;
  219. end;
  220.  
  221. function Check4Same(s: string): string;
  222. var
  223.    i, j, Temp: Integer;
  224. begin
  225.    i := 1;
  226.    while (i < Length(s) - 2) do
  227.    begin
  228.       if (s[i] = s[i + 1]) and (s[i + 1] = s[i + 2]) and (s[i + 2] = s[i + 3]) then
  229.       begin
  230.          for j := 1 to 7 do
  231.             if sym[j] = s[i] then
  232.                Temp := j;
  233.          Delete(s, i + 1, 3);
  234.          Insert(sym[Temp + 1], s, i + 1);
  235.       end;
  236.       Inc(i);
  237.    end;
  238.    Check4Same := s;
  239. end;
  240.  
  241. function FixNotValid(s: string): string;
  242. var
  243.    i, j, Temp, Temp1: Integer;
  244. begin
  245.    i := 1;
  246.    while (i < length(s) - 1) do
  247.    begin
  248.       if (s[i] = s[i + 2]) then
  249.       begin
  250.          for j := 1 to 7 do
  251.             if sym[j] = s[i] then
  252.                Temp := j;
  253.          for j := 1 to 7 do
  254.             if sym[j] = s[i + 1] then
  255.                Temp1 := j;
  256.          if Temp = Temp1 + 1 then
  257.          begin
  258.             delete(s, i, 3);
  259.             insert(sym[Temp1], s, i);
  260.             insert(sym[Temp + 1], s, i + 1);
  261.          end;
  262.       end;
  263.       Inc(i);
  264.    end;
  265.    FixNotValid := s;
  266. end;
  267.  
  268. function UserInput(): String;
  269. var
  270.    Method: Word;
  271.    Path: String;
  272. begin
  273.    Method := InputMethod;
  274.    if (Method = 1) then
  275.       UserInput := UserInputFromConsole
  276.    else
  277.    begin
  278.       Path := UserInputPath;
  279.       UserInput := UserInputFromFile(Path);
  280.    end;
  281. end;
  282.  
  283. procedure ResultPrint(value: Integer; s: string);
  284. var
  285.    Method: Word;
  286.    Path: String;
  287. begin
  288.    Method := OutputMethod;
  289.    if (Method = 1) then
  290.       PrintInConsole(value, s)
  291.    else
  292.    begin
  293.       Path := UserOutputPath;
  294.       PrintInFile(Path, value, s);
  295.    end;
  296. end;
  297.  
  298. procedure PrintTask;
  299. begin
  300.    Writeln('Данная программа переводит введёное число в римскую систему счисления');
  301. end;
  302.  
  303. procedure Main();
  304. var
  305.    s: String;
  306.    Value, Err: Integer;
  307. begin
  308.    PrintTask;
  309.    s := UserInput;
  310.    s := DeleteNotSym(s);
  311.    val(s, Value, Err);
  312.    s := ConvertToRim(Value);
  313.    s := Check4Same(s);
  314.    s := FixNotValid(s);
  315.    ResultPrint(value, s);
  316.    Writeln('Нажмите Enter для выхода из программы');
  317.    Readln;
  318. end;
  319.  
  320. begin
  321.    Main();
  322. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement