Advertisement
Razzle

Lab2_final

May 27th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 8.59 KB | None | 0 0
  1. program PrjLAB2;  // два параметра – 1) имя типизир. файла, 2) имя текстового файла
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils,     // ANSIUpperCase
  7.   Windows;   // SetConsoleCP
  8. //--------------------------------------------------------------
  9. Type
  10.   TMonth = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, December); // с 0 до 11 по умолчанию
  11.   TEvent = record
  12.     EventName: string[30]; // Только статические строки можно сохранить в файл
  13.     Date : byte;
  14.     Month: TMonth;
  15.     Year : Short;
  16.  
  17.   end;
  18. Function Date(event : TEvent) : Integer;
  19. begin
  20.   result := (event.Date + ((ord(event.Month) * 31)+1) + (event.Year * 372));
  21. end;
  22. //----------первая часть: создание типизированного файла в диалоге
  23. Procedure CreateTypedFile1();
  24. var
  25.   Event: TEvent;
  26.   mFile: file of TEvent; // типизированный файл - файл записей
  27.   n, kol: integer;
  28.   ch: char;
  29.   FileName:string[80];
  30. begin
  31.   if ParamCount<1 then
  32.   begin
  33.     writeln('Мало параметров');
  34.     writeln('Введите имя типизированного файла');
  35.     readln(FileName);
  36.   end
  37.   else
  38.     FileName := ParamStr(1);
  39.  
  40.   AssignFile(mFile, FileName);
  41.   Try  ReWrite(mFile);    //  или  {$I-} ReWrite(fr); {$I+} if IOResult<>0 then
  42.   Except
  43.     writeln('Error: не удалось создать типизированный файл ',FileName);
  44.     write('Press ENTER'); readln;  exit
  45.   end;
  46.  
  47.   kol:=0;
  48.   repeat
  49.     write('Событие =?');    readln(Event.EventName);
  50.     write('Число =? ');       readln(Event.Date);
  51.     write('Месяц =? ');      readln(n); Event.Month:=TMonth(n-1);
  52.     write('Год: = ? ');      readln(Event.Year);
  53.  
  54.     write(mFile, Event);   // запись записи целиком в файл
  55.     inc(kol);
  56.  
  57.     write('Ещё? (y/n)');
  58.     readln(ch)
  59.   until UpCase(ch)='N';
  60.  
  61.   CloseFile(mFile);
  62.  
  63.   writeln('Создан типизированный файл из ',kol,' записей');
  64.   write('Press ENTER');
  65.   readln;
  66. end;
  67. //--------------------------------------------------------------
  68. //----------вторая часть: создание типизированного файла из текстового
  69. Procedure CreateTypedFile2();
  70. var
  71.   Event: TEvent;
  72.   texFile: TextFile; // текстовый файл
  73.   typedFile: file of TEvent; // типизированный файл - файл записей
  74.   n, kol: integer;
  75.   ch: char;
  76.   month : integer;
  77.   name : string;
  78. begin
  79.   if ParamCount<2 then
  80.   begin
  81.     writeln('Мало параметров ');
  82.     writeln('Введите название типизированного файла'); readln(name);
  83.     AssignFile(typedFile, name);
  84.     writeln('Введите название текстового файла'); readln(name);
  85.     AssignFile(texFile, name);
  86.     readln;
  87.   end
  88.   else
  89.   begin
  90.     AssignFile(typedFile, ParamStr(1));
  91.     AssignFile(texFile, ParamStr(2));
  92.   end;
  93.   Try  Reset(texFile); Rewrite(typedFile);
  94.   Except
  95.     writeln('Error: не удалось открыть текстовый файл ',Paramstr(2));
  96.     write('Press ENTER'); readln;  exit
  97.   end;
  98.  
  99.   while (not eof(texFile)) do
  100.   begin
  101.     Readln(texFile, Event.EventName);
  102.     Readln(texFile, Event.Date);
  103.     Readln(texFile, month); Event.Month := TMonth(month-1);
  104.     Readln(texFile, Event.Year);
  105.  
  106.     Write(typedFile, Event);
  107.   end;
  108.  
  109.   CloseFile(texFile);
  110.  
  111.   write('Press ENTER');
  112.   readln;
  113. end;
  114.  
  115. //--------------------------------------------------------------
  116. //-------------- третья часть: поиск в типизированном файле ----
  117. Procedure FindEvent();
  118. var
  119.   Event, tempEvent: TEvent;
  120.   temp1, temp2 : TEvent;
  121.   typedFile: file of TEvent;
  122.   n, kol: integer;
  123.   FileName:string[80];
  124.   Litera : char;
  125. begin
  126.   if ParamCount<1 then
  127.   begin
  128.     writeln('Мало параметров');
  129.     writeln('Введите имя типизированного файла');
  130.     readln(FileName);
  131.   end
  132.   else
  133.     FileName := ParamStr(1);
  134.  
  135.   AssignFile(typedFile, FileName);
  136.   Try  ReSet(typedFile);    //  или  {$I-} ReSet(fr); {$I+} if IOResult<>0 then
  137.   Except
  138.     writeln('Error: не удалось открыть типизированный файл ',FileName);
  139.     write('Press ENTER'); readln;  exit
  140.   end;
  141.  
  142.   kol:=0;
  143.   writeln('Первая буква названия события'); readln(Litera);
  144.   tempEvent.Date := 0;
  145.   tempEvent.Month := TMonth(0);
  146.   tempEvent.Year := 0;
  147.   while not eof(typedFile) and (kol=0) do
  148.   begin
  149.     read(typedFile, Event); // считывание
  150.     if (Event.Date <> tempEvent.Date) and
  151.        (Event.Month <> tempEvent.Month) and
  152.        (Event.Year <> tempEvent.Year) and (Event.EventName[1] = Litera) then
  153.     begin
  154.       if (date(Event) > date(tempEvent)) then
  155.       begin
  156.         tempEvent := Event;
  157.         n:=FilePos(typedFile)-1;
  158.         Inc(kol);
  159.       end;
  160.     end;
  161.   end;
  162.    writeln('Найдено событие ', tempEvent.EventName,
  163.       ' дата ', tempEvent.Date, '.', Ord(tempEvent.Month)+1,'.', tempEvent.Year );
  164.       //
  165.       Seek(typedFile, n);
  166.       Read(typedFile, temp1);
  167.       Seek(typedFile, filesize(typedFile)-1);
  168.       Read(typedFile, temp2);
  169.       Seek(typedFile, n);
  170.       Write(typedFile, temp2);
  171.       Seek(typedFile, filesize(typedFile)-1);
  172.       Write(typedFile, temp1);
  173.       // запись в файле изменена
  174.       writeln('В файле изменена последняя запись и запись номер ', n+1);
  175.   if kol=0 then
  176.     writeln('Данные, соответствующие запросу, не найдены');
  177.  
  178.   CloseFile(typedFile);
  179.  
  180.   write('Press ENTER');  readln;
  181. end;
  182.  
  183. //--------------------------------------------------------------
  184. //-------------- просмотр типизированного файла ----
  185. Procedure ViewFile();
  186. var
  187.   Event: TEvent;
  188.   typedFile: file of TEvent;
  189.   n, kol: integer;
  190.   FileName:string[80];
  191. begin
  192.  if ParamCount<1 then
  193.   begin
  194.     writeln('Мало параметров');
  195.     writeln('Введите имя типизированного файла');
  196.     readln(FileName);
  197.   end
  198.   else
  199.     FileName := ParamStr(1);
  200.  
  201.   AssignFile(typedFile, FileName);
  202.  
  203.   Try  ReSet(typedFile);    //  или  {$I-} ReSet(fr); {$I+} if IOResult<>0 then
  204.   Except
  205.     writeln('Error: не удалось открыть типизированный файл ',FileName);
  206.     write('Press ENTER'); readln;  exit
  207.   end;
  208.  
  209.   kol:=0;
  210.   while not eof(typedFile) do
  211.   begin
  212.     read(typedFile, Event); // считывание
  213.     writeln('Найдено событие ', Event.EventName,
  214.       ' дата ', Event.Date, '.', Ord(Event.Month)+1,'.', Event.Year );
  215.       inc(kol);
  216.   end;
  217.  
  218.   if kol=0 then
  219.     writeln('Данные не найдены')
  220.   else
  221.     writeln('Найдено ',kol, ' записей');
  222.  
  223.   CloseFile(typedFile);
  224.  
  225.   write('Press ENTER');  readln;
  226. end;
  227. //--------------------главная программа-------------------------
  228. var
  229.   ch: char;
  230. begin
  231.   SetConsoleCP(1251);      // из модуля Windows
  232.   SetConsoleOutputCP(1251);
  233.  
  234.   repeat
  235.     writeln('--------------------------------------');
  236.     writeln('D - создать новый тип.файл в диалоге; ');
  237.     writeln('N - создать из текстового файла; ');
  238.     writeln('F - поиск и изменение; ');
  239.     writeln('V - просмотр;');
  240.     writeln('E - конец.');
  241.     write('Ваш выбор?'); readln(ch);
  242.     writeln('--------------------------------------');
  243.  
  244.     ch:= UpCase(ch);
  245.     case ch of
  246. //--------------------------------------------------------------
  247. //----------первая часть: создание типизированного в диалоге ---
  248.     'D': CreateTypedFile1;
  249. //----------вторая часть: создание типизированного из текстового
  250.     'N': CreateTypedFile2;
  251. //----------третья часть: поиск и редактирование ---------------
  252.     'F': FindEvent;
  253. //-----------просмотр----------------------------------------------
  254.     'V': ViewFile;
  255. //-----------выход----------------------------------------------
  256.     'E': exit;
  257. //--------------------------------------------------------------
  258.       else
  259.         begin
  260.           writeln('Нет такой команды');
  261.           write('Press ENTER'); readln;
  262.         end;
  263.     end;
  264.  
  265.   until ch='E';
  266. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement