Akaleaf

ListProj

Oct 31st, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.93 KB | None | 0 0
  1. program ListProj;
  2.  
  3. const MYSTERIOUS_NUMBER = 57978;
  4. // Эта константа вводится для возможности добавления в список полноправного числа 0
  5.  
  6. var
  7.   count, userCount, elementIndex : byte;
  8.   list : array[0..5] of integer;
  9.   element, elementOld, i, ii, iii : integer;
  10.   answer : string;
  11.   wasFound : boolean;
  12.  
  13. function isAMysteriousNumber(number : integer) : boolean;
  14. begin
  15.   if (number = MYSTERIOUS_NUMBER) then
  16.   begin
  17.     isAMysteriousNumber := true;
  18.   end else
  19.   begin
  20.     isAMysteriousNumber := false;
  21.   end;
  22. end;
  23.  
  24. function thereIsAPlaceOnTheRightBefore(index : integer) : boolean;
  25. var
  26.   cycleVar : integer;
  27. begin
  28.   for cycleVar := index to 5 do
  29.   begin
  30.     if (list[cycleVar] = MYSTERIOUS_NUMBER) then
  31.     begin
  32.       thereIsAPlaceOnTheRightBefore := true;
  33.       break;
  34.     end;
  35.     if (cycleVar = 5) then
  36.     begin
  37.       thereIsAPlaceOnTheRightBefore := false;
  38.       break;
  39.     end;
  40.   end;
  41. end;
  42.  
  43. function Push(element, elementIndex : integer) : integer;
  44.  begin
  45.    if (count <= 5) then
  46.    begin
  47.      List[elementIndex] := element;
  48.      count += 1;
  49.    end else
  50.    begin
  51.    if (count > 5) then
  52.      writeln;
  53.      writeln('!!!!!!!!!!ERROR: the List is overloaded!!!!!!!!!!');
  54.      writeln;
  55.    end;
  56.  end;
  57.  
  58. function Pop(elementIndex : integer) : integer;
  59. begin
  60.   if (count > 0) then
  61.   begin
  62.     if (List[elementIndex] <> 0) then
  63.     begin
  64.       List[elementIndex] := MYSTERIOUS_NUMBER;
  65.       count -= 1;
  66.     end else
  67.     begin
  68.       writeln;
  69.       writeln('!!!!!!!!!!ERROR: Element index is empty!!!!!!!!!!');
  70.     end;
  71.   end else
  72.   begin
  73.     writeln;
  74.     writeln('!!!!!!!!!!ERROR: List is empty!!!!!!!!!!');
  75.     writeln;
  76.   end;
  77. end;
  78.  
  79. procedure PushBefore();
  80. var
  81.   endOfCycle : boolean;
  82.   j : integer;
  83. begin
  84.   write('Enter the element BEFORE which you want to add a new element: ');
  85.   readln(elementOld);
  86.   write('Enter the new element: ');
  87.   readln(element);
  88.   wasFound := false;
  89.   if (count > 0) then
  90.   begin
  91.     for i := 0 to 5 do
  92.     begin
  93.       if (list[i] = elementOld) then
  94.       begin
  95.         if thereIsAPlaceOnTheRightBefore(i - 1) then
  96.         begin
  97.           if (list[i - 1] = MYSTERIOUS_NUMBER) then
  98.           begin
  99.             list[i - 1] := element;
  100.             count += 1;
  101.           end else
  102.           begin
  103.             repeat
  104.             for ii := 1 to 5 do                   // 0 7 6 4 0 0
  105.             begin
  106.               if (list[i + ii] = MYSTERIOUS_NUMBER) then
  107.               begin
  108.                 list[i + ii] := list[i + ii - 1];
  109.                 list[i + ii - 1] := MYSTERIOUS_NUMBER;
  110.                 if list[i] = MYSTERIOUS_NUMBER then
  111.                 begin
  112.                   list[i] := element;
  113.                   count += 1;
  114.                   endOfCycle := true;
  115.                   break;
  116.                 end;
  117.                 break;
  118.               end;
  119.             end;
  120.             until endOfCycle;
  121.           end;
  122.         end else
  123.         begin
  124.           writeln;
  125.           writeln('!!!!!!!!!!ERROR: The element ', list[i], ' is the first element!!!!!!!!!!');
  126.           writeln;
  127.         end;
  128.         wasFound := true;
  129.         break;
  130.       end;
  131.     end;
  132.     if (not wasFound) then
  133.     begin
  134.       writeln();
  135.       writeln('!!!!!!!!!!ERROR: The element ', elementOld, ' was not founded!!!!!!!!!!');
  136.       writeln();
  137.     end;
  138.   end else
  139.   begin
  140.     writeln('!!!!!!!!!!ERROR: List is empty!!!!!!!!!!');
  141.   end;
  142. end;
  143.  
  144. procedure PushAfter();
  145. var
  146.   endOfCycle : boolean;
  147. begin
  148.   write('Enter the element AFTER which you want to add a new element: ');
  149.   readln(elementOld);
  150.   write('Enter the new element: ');
  151.   readln(element);
  152.   wasFound := false;
  153.   if (count > 0) then
  154.   begin
  155.     for i := 0 to 5 do
  156.     begin
  157.       if (list[i] = elementOld) then
  158.       begin
  159.         if thereIsAPlaceOnTheRightBefore(i) then
  160.         begin
  161.           if (list[i + 1] = MYSTERIOUS_NUMBER) then
  162.           begin
  163.             writeln('i');
  164.             list[i + 1] := element;
  165.             count += 1;
  166.           end else
  167.           begin
  168.             repeat
  169.             for ii := 1 to 5 do                   // 0 7 6 4 3 0
  170.             begin
  171.               if (list[i + 1 + ii] = MYSTERIOUS_NUMBER) then
  172.               begin
  173.                 list[i + 1 + ii] := list[i + 1 + ii - 1];
  174.                 list[i + 1 + ii - 1] := MYSTERIOUS_NUMBER;
  175.                 if list[i + 1] = MYSTERIOUS_NUMBER then
  176.                 begin
  177.                   list[i + 1] := element;
  178.                   count += 1;
  179.                   endOfCycle := true;
  180.                   break;
  181.                 end;
  182.               end;
  183.             end;
  184.             until endOfCycle;
  185.           end;
  186.         end else
  187.           begin
  188.             writeln;
  189.             writeln('!!!!!!!!!!ERROR: The element ', list[i], ' is the last element or list is overloaded!!!!!!!!!!');
  190.             writeln;
  191.           end;
  192.         wasFound := true;
  193.         break;
  194.       end;
  195.     end;
  196.     if (not wasFound) then
  197.     begin
  198.       writeln();
  199.       writeln('!!!!!!!!!!ERROR: The element ', elementOld, ' was not founded!!!!!!!!!!');
  200.       writeln();
  201.     end;
  202.   end else
  203.   begin
  204.     writeln('!!!!!!!!!!ERROR: List is empty!!!!!!!!!!');
  205.   end;
  206. end;
  207.  
  208. begin
  209.   count := 0;
  210.   for i := 0 to 5 do list[i] := MYSTERIOUS_NUMBER;
  211.   repeat
  212.     begin
  213.       write('Current List:                     [ ');
  214.       for i := 0 to 5 do if isAMysteriousNumber(list[i]) then write(' - ') else write(' ', list[i], ' ');
  215.       writeln(']');
  216.       writeln('1. Add element to List');
  217.       writeln('2. Remove element from List');
  218.       writeln('3. Add element before...');
  219.       writeln('4. Add element after...');
  220.       writeln('5. Close the programm');
  221.       readln(answer);
  222.       writeln();
  223.  
  224.       case answer of
  225.       '1':
  226.         begin
  227.           write('Enter element index to add(first element index = 0): ');
  228.           readln(elementIndex);
  229.           write('Enter element: ');
  230.           readln(element);
  231.           if ((0 <= elementIndex) AND (elementIndex <= 5)) then Push(element, elementIndex) else writeln('error :0');
  232.         end;
  233.       '2':
  234.         begin
  235.           write('Enter element index to remove(first element index = 0): ');
  236.           readln(elementIndex);
  237.           if ((-1 < elementIndex) AND (elementIndex < 6)) then
  238.           begin
  239.             write('Removing element from List - ');
  240.             if (List[elementIndex] = MYSTERIOUS_NUMBER) then
  241.             begin
  242.               writeln('NaN');
  243.             end else writeln(List[elementIndex]);
  244.             Pop(elementIndex);
  245.           end else
  246.           begin
  247.             writeln;
  248.             writeln('element index must be from 0 to 5!');
  249.           end;
  250.         end;
  251.       '3': PushBefore();
  252.       '4': PushAfter();
  253.       end;
  254.  
  255.     end
  256.   until answer = ('5');
  257. end.
Advertisement
Add Comment
Please, Sign In to add comment