This repository was archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentList.mj
More file actions
87 lines (75 loc) · 1.68 KB
/
StudentList.mj
File metadata and controls
87 lines (75 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
program StudentList
final int MAXLEN = 20;
class Student {
int matrNr;
char[] name;
}
Student[] list;
int stCnt;
{
void printString (char[] str)
int i;
{
if (str != null) {
i = 0;
while (i < len(str)) {
print(str[i]);
i++;
}
}
}
void init () {
list = new Student[MAXLEN];
stCnt = 0;
}
void add (Student s)
int i;
{
/* insert sorted by matrNr */
i = stCnt - 1;
while (i >= 0 && s.matrNr < list[i].matrNr) {
list[i+1] = list[i];
i--;
}
list[i+1] = s;
stCnt++;
}
int find (int matrNr)
int l, r, x;
{
/* binary search */
l = 0; r = stCnt-1;
while (l <= r && matrNr != list[x].matrNr) {
x = (l+r)/2;
if (matrNr < list[x].matrNr) r = x-1;
else l = x+1;
}
if (matrNr == list[x].matrNr) return x;
return -1;
}
void printStudent (int i) {
print('m'); print('['); print(i); print(']'); print('=');
print(list[i].matrNr); print(','); printString(list[i].name);
print('\n');
}
void main()
Student s;
{
init();
s = new Student; s.matrNr = 1234567; s.name = new char[3];
s.name[0] = 'X'; s.name[1] = '\\'; s.name[2] = 'Y';
add(s);
s = new Student; s.matrNr = 9876543; s.name = new char[4];
s.name[0] = 'M'; s.name[1] = 'r'; s.name[2] = '.'; s.name[3] = 'X';
add(s);
s = new Student; s.matrNr = 9090900; s.name = new char[2];
s.name[0] = 'A'; s.name[1] = 'l';
add(s);
printStudent(0);
printStudent(1);
printStudent(2);
print(9876543); print(' '); printString(list[find(9876543)].name); print('\n');
print(1234567); print(' '); printString(list[find(1234567)].name); print('\n');
print(9090900); print(' '); printString(list[find(9090900)].name); print('\n');
}
}