summaryrefslogtreecommitdiff
path: root/macros/generic/knst/knst.tex
blob: 7038f45883b261fe2cfd31ad9c56126f2d6b5790 (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
%%% AUTHOR: Jordi Saludes <saludes@grec.upc.es>
%%% DATE: 12/9/1996
%%% FILE: knst.tex
%%% knapsack test macros.
\catcode`@=11
%% Shift Register
%% from H. van der Meer, TUB 15, 1
\newcount\@SR
\def\@SRconst{2097152}
\def\SRset#1{\global\@SR#1\relax}%
\def\@SRadvance{\bgroup
 \ifnum\@SR<\@SRconst\relax
  \@A=0
 \else\@A=1\fi
 \ifodd\@SR
  \advance\@A by1 \fi
 \global\divide\@SR by2
 \ifodd\@A
  \global\advance\@SR\@SRconst\relax\fi
 \egroup}
%% Arithmetic
\newcount\@A
\newcount\@B
\newcount\@C
\newcount\@x \@x=0\relax
\newcount\@y \@y=0\relax
\newcount\@w
\newcount\key
\newcount\df \df=1\relax
\def\qtnno{\the\@x\relax}
\def\inc@#1{\advance#1 by1\relax}
\def\mod@A#1{\@B=\@A \divide\@B by#1
 \multiply\@B by#1
 \advance\@A by-\@B\relax}
\def\gcd#1#2{{\@A=#1 \@C=#2
 \loop\mod@A\@C
  \ifnum\@A>0
  \@B=\@C \@C=\@A \@A=\@B
 \repeat
 \ifnum\@C=1\else
  \errmessage{Invalid stepper/version
number "#1"}\fi}}
\def\adv@w{{\inc@\@y
 \global\multiply\@w by \@y}}
\def\labelno{{\@A=\@B
 \mod@A\key\multiply\@A by\df \the\@A}}
%% Boxing and unboxing
\newtoks\pfootline
\newtoks\pheadline
\newbox\lqtn@bx
\newbox\lanw@bx
\newbox\canw@bx
\newbox\cqtn@bx
\def\new@page{\unvbox\lqtn@bx
 \vfill\penalty-10000
 \global\@w=\verno \adv@w}
\def\append@bx#1#2{%
 \setbox#1=\vbox{\unvbox#1 #2}}
\def\prepend@bx#1#2{%
 \setbox#1=\vbox{#2\unvbox#1}}
\def\add@anw{\@SRadvance
 \iffix@anw
  \global\append@bx{\lanw@bx}{\theanw}%
 \else
  \ifodd\@SR
   \global\prepend@bx{\lanw@bx}{\theanw}%
  \else
   \global\append@bx{\lanw@bx}{\theanw}%
  \fi\fi}%
\def\add@qtn{\@SRadvance
 \iffix@qtn
  \global
   \append@bx{\lqtn@bx}{\unvbox
     \cqtn@bx}%
 \else
  \ifodd\@SR
   \global
    \prepend@bx{\lqtn@bx}{\unvbox
      \cqtn@bx}%
  \else
   \global
    \append@bx{\lqtn@bx}{\unvbox
      \cqtn@bx}%
  \fi\fi}
\def\fix{\global\fix@anwtrue\relax}%
\def\fixqtn{\global\fix@qtntrue\relax}%
\def\qtn{\global\inc@\@x
 \@y=0 \@B=0
 \ifplain\else
  \ifnum\@x>\botqtn
   \new@page\@next\fi\fi
 \rightanw=0 
 \global\fix@anwfalse
 \setbox\cqtn@bx=\vbox\bgroup
  \noindent\qtn@pr}
\def\endqtn{\egroup\add@anw
 \ifplain\ifnum\rightanw=0
  \immediate\write16{There is no
   option marked in question
   \the\@x}\fi\fi
 \setbox\cqtn@bx=\vbox{\box\cqtn@bx
  \preanwskip\box\lanw@bx
  \qtnskip}%
 \adv@w\wr@anw
 \ifplain
  \ifnum\@w>\key
   \message{*}\new@page\fi
 \else
  {\@A=\@w \mod@A\key \global\@w=\@A}\fi
 \mark{\noexpand
  \themark{\the\@x}{\the\@w}}
 \message{.}\add@qtn}
\def\Anw{\ifnum\rightanw>0
  \errmessage{There are several
  options marked in question
  \the\@x}\fi
 \global\rightanw=\@y
 \global\inc@\rightanw
 \anw}
\def\anw{\egroup
 \ifnum\@y=0\else
  \add@anw\fi
 \advance\@B by\@w\relax\inc@\@y
 \setbox\canw@bx=\vtop\bgroup
  \relax\noindent}
\newif\ifplain
\plainfalse
\newcount\verno \verno=0
\newcount\rightanw
\newif\iffix@anw
\newif\iffix@qtn
\fix@qtnfalse
\let\read@line=\relax
\let\@read=\relax
\def\theanw{\hbox{\vrule
 height10pt width\z@\relax
 \anwprompt\ \box\canw@bx\hfill}}
\newcount\verno
\newwrite\anwfile
\newwrite\auxfile
\newdimen\labelw
\def\test#1{\key=#1
 \setbox3=\hbox{\multiply\key by\df
  \the\key}%
 \labelw=\wd3\relax
 \ifnum\verno<1\relax
  \immediate
   \openin\auxfile=\jobname.aux\relax
  \read\auxfile to\@read \@read
  \immediate\closein\auxfile\relax\fi
 \gcd{\the\verno}{\the\key}
 \@SRadvance
 \global\@w=\verno
 \header
 \setbox\lqtn@bx=\vbox{}
 \ifplain
  \message{Plain version}\SRset{0}%
  \global\@w=1
 \else
  \message{Version \the\verno}
  \immediate
   \openin\anwfile=\jobname.ans\relax
 \fi\@next}
\def\endtest{
 \ifplain
  \write\anwfile{\string
   \Key\space\the\key\space
   \the\df\space}%
  \closeout\anwfile
 \else
  \closein\anwfile\fi
 \unvbox\lqtn@bx\vfill\supereject\end}
\def\stepversion#1{\gcd{#1}{\the\key}%
 \immediate
  \openout\auxfile=\jobname.aux\relax
 {\ifnum\@SR=0
   \@SR=\@SRconst
   \divide\@SR by3\fi
 \@SRadvance
 \@A=\verno
 \multiply\@A by#1\relax
 \mod@A\key\relax
 \immediate\write\auxfile{\string
  \verno=\the\@A\space
  \string\SRset{\the\@SR}}}%
 \immediate\closeout\auxfile}
\def\plainversion{\plaintrue\verno=1
 \let\newpage=\new@page
 \def\wr@anw{\immediate
   \write\anwfile{\string\opts
    \space\the\@x\space\the\@y\space
    \the\rightanw\space\the\@w\space}}
 \immediate
  \openout\anwfile=\jobname.ans\relax
 \def\qtn@pr{\llap{\qtnprompt}}
\def\anwprompt{\hbox to\labelw{\hss
 \ifnum\rightanw=\@y
  $\bullet$\ \fi}}
\footline=\pfootline
\headline=\pheadline
\let\@next=\relax
\output={\botmark\plainoutput}
\def\add@qtn{\unvbox\cqtn@bx\penalty-50}}
\def\themark#1#2{\immediate
 \write\anwfile{\string\pagebot
  \space #1\space}%
 \global\divide\@w by#2\relax}
\let\wr@anw=\relax
\let\qtn@pr=\relax
\def\@next{\read\anwfile to\@read
 \@read}
\def\Key #1 #2 {\@next}
\def\opts #1 #2 #3 #4 {\@next}
\def\pagebot #1 {\def\botqtn{#1}}
\def\botqtn{1000}
\let\newpage=\relax
%% Style commands
\def\qtnprompt{\bf\qtnno.\ }
\pfootline={\hss
 Plain version $\dots$\hss}
\pheadline={\ifnum\folio>1
 \thetitle\hss\folio\else\hss\fi}
\footline={Page \folio. Version
 {\tt\the\verno}.
 $\Sigma=$\hskip7em
 $\Sigma/\the\df=$\hfill}
\def\date#1{\def\thedate{#1}}
\def\title#1{\def\thetitle{#1}}
\def\header{
 \line{\bf\thetitle\hfill\thedate}%
 \smallskip
 \line{Name:\ \dotfill}%
 \medskip
{\baselineskip=9pt
 \noindent\small
 Each question has
 only one right answer.
 For each page, add the numbers at
 the left side of 
 your answers and write the total by
 the $\Sigma$. Divide the total
 by~\the\df. If your addition is right,
 the quotient must be integer
 (But this does not mean
 that your choices were correct).
 Do not forget to write down your name,
 but do not write anything else in the
 exam sheet.\par}
 \medskip}
\df=7
\def\anwprompt{\hbox to\labelw{\it
 \hss\labelno\ $\diamond$}}
\def\qtnskip{\vskip 20pt plus25pt }
\let\preanwskip=\smallskip
\font\small=cmr8