Массивы
Описание типов • Type <имя_типа> = <задание_типа> ; • Type fruits=integer; week=1. . 7; {ограниченный тип} cifra=‘ 0’. . ’ 9’; {перечислимый тип} mweek=(mon, tu, wen, th, fr, sat, sun); Var day: week; mday: mweek; rusday: (pon, vt, cr, chet, piat, sub, voc);
Массив • Упорядоченный набор переменных одного типа. • M M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] • Array [<тип_индекса>] of <тип_компоненты>; • Var M: array[0. . 9] of integer; • Обращение к элементам массива: M[0]: =1; M[1]: =1; for I: =2 to 9 do M[i]: =M[i-1]+M[i-2];
• M[0]: =1; M[1]: =1; for I: =2 to 9 do M[i]: =M[i-1]+M[i-2]; • i M M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] 1 1
• M[0]: =1; M[1]: =1; for I: =2 to 9 do M[i]: =M[i-1]+M[i-2]; • i M 2 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] 1 1 2
• M[0]: =1; M[1]: =1; for I: =2 to 9 do M[i]: =M[i-1]+M[i-2]; • i M 3 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] 1 1 2 3
• M[0]: =1; M[1]: =1; for I: =2 to 9 do M[i]: =M[i-1]+M[i-2]; • i M 4 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] 1 1 2 3 5
• M[0]: =1; M[1]: =1; for I: =2 to 9 do M[i]: =M[i-1]+M[i-2]; • i M 5 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] 1 1 2 3 5 8
• M[0]: =1; M[1]: =1; for I: =2 to 9 do M[i]: =M[i-1]+M[i-2]; • i M 6 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] 1 1 2 3 5 8 13
• M[0]: =1; M[1]: =1; for I: =2 to 9 do M[i]: =M[i-1]+M[i-2]; • i M 7 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] 1 1 2 3 5 8 13 21
• M[0]: =1; M[1]: =1; for I: =2 to 9 do M[i]: =M[i-1]+M[i-2]; • i M 8 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] 1 1 2 3 5 8 13 21 34
• M[0]: =1; M[1]: =1; for I: =2 to 9 do M[i]: =M[i-1]+M[i-2]; • i M 9 M[0] M[1] M[2] M[3] M[4] M[5] M[6] M[7] M[8] M[9] 1 1 2 3 5 8 13 21 34 55
Примеры • Type zarplata =array[1. . 12] of real; coordtype=(x, y, z); Var m: zarplata; coord: array[coordtype] of real; • Д. З: Стр. 89 -90 № 10, 23, 35, 36 Стр120 № 1, 2, 3, 4, 5, 7
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i M 1 j 4 imax 0 M[0] M[1] M[2] M[3] M[4] 2 1 10 8 5
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i M 2 j 4 imax 0 M[0] M[1] M[2] M[3] M[4] 2 1 10 8 5
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i M 2 j 4 imax 2 M[0] M[1] M[2] M[3] M[4] 2 1 10 8 5
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i M 3 j 4 imax 2 M[0] M[1] M[2] M[3] M[4] 2 1 10 8 5
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i M 4 j 4 imax 2 M[0] M[1] M[2] M[3] M[4] 2 1 10 8 5
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i 4 j 4 imax 2 M M[0] M[1] M[2] M[3] M[4] 2 1 10 8 5 • max 10
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i 4 j 4 imax 2 M M[0] M[1] M[2] M[3] M[4] 2 1 5 8 5 • max 10
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i 4 j 4 imax 2 M M[0] M[1] M[2] M[3] M[4] 2 1 5 8 10 • max 10
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i M 1 j 3 imax 0 M[0] M[1] M[2] M[3] M[4] 2 1 5 8 10
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i M 2 j 3 imax 0 M[0] M[1] M[2] M[3] M[4] 2 1 5 8 10
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i M 2 j 3 imax 2 M[0] M[1] M[2] M[3] M[4] 2 1 5 8 10
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i M 3 j 3 imax 2 M[0] M[1] M[2] M[3] M[4] 2 1 5 8 10
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i M 3 j 3 imax 3 M[0] M[1] M[2] M[3] M[4] 2 1 5 8 10
Сортировка массива • For j: =4 downto 1 do begin imax: =0; for i: =1 to j do if m[i]>m[imax] then imax: =i; max: =m[imax]; m[imax]: =m[j]; m[j]: =max; end; • i M 3 j 3 imax 3 M[0] M[1] M[2] M[3] M[4] 2 1 5 8 10 • Max 8 и т. д….
строки • Packed array [1. . N] of char или String[N] – одномерный массив символов пронумерованный от 1 до N>0. • Var str: string[10]; {str это строка состоящая из не более 10 символов} • String соответствует string[255].
Свойства строки Var str 1, str 2, S: string[10]; begin str 1: =‘hello’ str 2: =‘world’ end; 1. Строки можно сравнивать используя <, >, <=, >=, <>, =; str 1
Процедуры и функции для работы со строками • L: =Length(S) Возвращает длину L строки S • Delete(S, i, k) Удаляет из строки S, k символов начиная с номера i • Insert(Z, S, i) вставляет в строку S подстроку Z начиная с номера i • P: =pos(Z, S) отыскивает в строке S первое вхождение подстроки Z возвращает позицию P • Z: =copy(S, I, k) копирует в Z, k символов из S начиная с i • Val(S, r, p) переводит строку S в число r • Str(r, S) переводить число r в строку S
Д/з • Стр 121: 21, 41 или 36


