1 | #include <fstream.hfa>
|
---|
2 | #include <stdlib.hfa>
|
---|
3 | #include <array.hfa>
|
---|
4 | #include <locale.h> // setlocale
|
---|
5 |
|
---|
6 |
|
---|
7 |
|
---|
8 |
|
---|
9 |
|
---|
10 | forall( [C], [S] ) $\C{// Class size, Students in class}$
|
---|
11 | struct School {
|
---|
12 | @array( int, C )@ classIds; $\C{// nested VLAs}$
|
---|
13 | @array( int, S )@ studentIds;
|
---|
14 | @array( int, C, S )@ preferences; $\C{// multidimensional}$
|
---|
15 | };
|
---|
16 |
|
---|
17 |
|
---|
18 |
|
---|
19 | // TODO: understand (fix?) why these are needed (autogen seems to be failing ... is typeof as struct member nayok?)
|
---|
20 |
|
---|
21 | forall( [C], [S] )
|
---|
22 | void ?{}( School( C, S ) & this ) {}
|
---|
23 |
|
---|
24 | forall( [C], [S] )
|
---|
25 | void ^?{}( School( C, S ) & this ) {}
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 | forall( [C], [S] )
|
---|
41 | void init( @School( C, S ) & classes@, int class, int student, int pref ) with( classes ) {
|
---|
42 | classIds[class] = class; $\C{// handle dynamic offsets of fields within structure}$
|
---|
43 | studentIds[student] = student;
|
---|
44 | preferences[class][student] = pref;
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 |
|
---|
60 | int main() {
|
---|
61 | int classes, students;
|
---|
62 | sin | classes | students;
|
---|
63 | @School( classes, students ) school;@
|
---|
64 | int class, student, preference;
|
---|
65 | // read data into school calling init
|
---|
66 | // for each student's class/preferences
|
---|
67 | try {
|
---|
68 | for ( ) {
|
---|
69 | sin | class | student | preference;
|
---|
70 | init( school, class, student, preference );
|
---|
71 | }
|
---|
72 | } catch( end_of_file * ) {}
|
---|
73 | for ( s; students ) {
|
---|
74 | sout | "student" | s | nonl;
|
---|
75 | for ( c; classes ) {
|
---|
76 | sout | school.preferences[c][s] | nonl;
|
---|
77 | }
|
---|
78 | sout | nl;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 |
|
---|
84 | /*
|
---|
85 | $\$$ cat school1
|
---|
86 | 2 2
|
---|
87 | 0 0 1
|
---|
88 | 1 0 7
|
---|
89 | 0 1 12
|
---|
90 | 1 1 13
|
---|
91 | $\$$ a.out < school1
|
---|
92 | student 0 1 7
|
---|
93 | student 1 12 13
|
---|
94 |
|
---|
95 | $\$$ cat school2
|
---|
96 | 3 3
|
---|
97 | 0 0 1
|
---|
98 | 1 0 7
|
---|
99 | 2 0 8
|
---|
100 | 0 1 12
|
---|
101 | 1 1 13
|
---|
102 | 2 1 14
|
---|
103 | 0 2 26
|
---|
104 | 1 2 27
|
---|
105 | 2 2 28
|
---|
106 | $\$$ a.out < school2
|
---|
107 | student 0 1 7 8
|
---|
108 | student 1 12 13 14
|
---|
109 | student 2 26 27 28
|
---|
110 | */
|
---|
111 |
|
---|
112 | // Local Variables: //
|
---|
113 | // compile-command: "sed -f sedcmd hello-accordion.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" //
|
---|
114 | // End: //
|
---|