Advertisement
MadCortez

Untitled

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