Advertisement
MadCortez

Untitled

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