-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyLib.java
327 lines (253 loc) · 7.27 KB
/
MyLib.java
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* @(#)MyLib.java 2.0, 01/17/21
*
*/
/**
* The MyLib class is a direct port of mylib.c to java.
* As you already know in java when you pass literal strings like
* <P>
* writeline("a literal string\n", stream);
* <P>
* in C is considered a char[], but in java it's automatically
* converted and treated as a String object. Therefore
* the function writeline accepts literal strings and
* String types. The getline function returns a String type.
*
* @author Gary Gillespie
* @author Bryner Pancho (C ported to java)
* @version 1.0, 04/24/98
*
* @author Gary Gillespie
* @author Bora Yuksel (updated Java code)
* @version 2.0 01/17/21
*/
import java.io.*; // System.in and System.out
import java.util.*; // Stack
class MyLibCharacter {
private Character character;
public MyLibCharacter (char ch) {
character = Character.valueOf (ch);
}
public char charValue () {
return character.charValue ();
}
public String toString () {
return "" + character;
}
}
public class MyLib extends Object {
/**
* A constant for passing in the baseout function.to display digits in hex.
*/
static final int HEX = 16;
/**
* A constant for passing in the baseout function.to display digits
* in decimal.
*/
static final int DECIMAL = 10;
/**
* A constant for converting ASCII characters to a numeric value.
*/
static final long ASCII_ZERO = 48;
private static final int CR = 13; // Carriage Return (ASCII)
private static final int MAX_LINE_LENGTH = 80;
private static final int EOF = -1;
// Max string length for input.
private static final char digits[] =
new String("0123456789abcdefghijklmnopqrstuvwxyz").toCharArray();
private static Stack<MyLibCharacter> InStream = new Stack<MyLibCharacter> ();
/**
* Reads in a decimal number from the input stream.
*
* @return <code>long</code>
*/
public static long decin (Reader is) {
int character; // Character from user.
long digit; // Each digit as it is found.
long sum = 0;
// Input terminates with a non-digit character.
if ((character = getchar (is)) == EOF)
return EOF;
while (Character.isDigit (character)) {
// Change from ASCII to numeric value.
digit = character & ~ASCII_ZERO;
// Accumulate number.
sum *= DECIMAL;
sum += digit;
character = getchar (is);
}
// Return non-digit back to input stream.
ungetc ((char) character);
return sum;
}
public static void decout (long number) {
baseout( number, DECIMAL );
}
/**
* Takes in a positive or negative number and displays in a given
* base to the screen.
*
* @param Numeric positive or negative value to be displayed.
* @param Base to display number in.
*/
public static void baseout (long number, long base) {
if (number < 0) {
fputc('-', System.out);
posbaseout (-number, base);
}
else {
posbaseout (number, base);
}
}
/**
* Takes in a positive number and base to be displayed to the screen.
*
* @param A positive numeric value to be displayed.
* @param Base to display number in.
*/
private static void posbaseout (long number, long base) {
long quotient; // Quotient of div by 10.
long remainder; // Remainder of div by 10.
// Remove one digit from number.
// digit (remainder) is for output,
// the rest of the number (quotient)
// still needs to be printed.
remainder = number % base;
quotient = number / base;
// Call function to output remaining digits, if any.
if (quotient != 0)
posbaseout (quotient, base);
// Output digit.
fputc (digits [(int) remainder], System.out);
}
/**
* Takes in a positive number and displays it in hex.
*
* @param A positive numeric value to be displayed in hex.
*/
public static void hexout (long number) {
// Output "0x" for hexidecimal.
writeline ("0x", System.out);
posbaseout (number, HEX);
}
/**
* Clears the input stream buffer until the '\n' is
* encountered or has already been read indicated by
* the LastChar parameter..
*
* @param Last character taken from the input stream.
*/
public static void clrbuf (char LastChar, Reader is) {
// Check for buffer already being empty.
// then remove all characters from the buffer
// until empty.
while (LastChar != '\n') {
LastChar = (char) getchar (is);
if (LastChar == EOF)
return;
}
}
/**
* Returns a character from the input stream.
*
* @return <code>char</code>
*/
public static int getchar (Reader is) {
char ToRet = '\0';
// Check our local input stream first.
// If it's empty read from System.in
if (InStream.isEmpty ()) {
try {
// Java likes giving the user the
// CR character too. Dumb, so just
// ignore it and read the next character
// which should be the '\n'.
ToRet = (char) is.read ();
if (ToRet == CR)
ToRet = (char) is.read ();
// check for EOF
if ((long) ToRet == 0xFFFF)
return EOF;
}
// Catch any errors in IO.
catch (EOFException eof) {
// Throw EOF back to caller to handle
return EOF;
}
catch (IOException ioe) {
writeline ("Unexpected IO Exception caught!\n",
System.out);
writeline (ioe.toString (), System.out);
}
}
// Else just pop it from the InStream.
else
ToRet = ((MyLibCharacter) InStream.pop ()).charValue ();
return ToRet;
}
/**
* Displays a single character.
*
* @param Character to display.
*/
public static void fputc(char CharToDisp, PrintStream stream) {
// Print a single character.
stream.print (CharToDisp);
// Flush the system.out buffer, now.
stream.flush ();
}
/**
* Prints out a newline character.
*
*/
public static void newline () {
fputc ('\n', System.out);
}
/**
* Prints out a string.
*
* @param A string to print out.
* @return <code>long</code> The length of the string.
*/
public static long writeline (String string, PrintStream stream) {
for (int index = 0; index < (string.length ()); index++)
fputc (string.charAt (index), stream);
return string.length ();
}
/**
* Returns a string from the user.
*
* @return <code>String</code>
*/
public static String getline(Reader is) {
char buffer[] = new char[MAX_LINE_LENGTH];
int input;
int index = 0;
do {
input = getchar (is);
buffer[index] = (char) input;
index++;
} while (input != '\n' && (index < MAX_LINE_LENGTH-1));
return new String (buffer, 0, index - 1);
}
public static String namein (Reader is) {
char buffer[] = new char[MAX_LINE_LENGTH];
int character; // input from user
int index = 0; // index into buffer
while (Character.isLetterOrDigit (character = getchar (is))
|| character == '_')
buffer[index++] = (char) character;
/* put non-digit back into stdin */
ungetc ((char) character);
return new String (buffer, 0, index);
}
/**
* Places back a character into the input stream buffer.
*
* @param A character to putback into the input buffer stream.
*/
public static void ungetc (char ToPutBack) {
// Push the char back on our local input stream buffer.
InStream.push (new MyLibCharacter (ToPutBack));
}
}