Java Reference
In-Depth Information
/* Driver - main program */
public
public static
static
void
void
main
(
String
[]
av
)
throws
throws
IOException
{
iif
(
av
.
length
==
0
)
new
new
SimpleCalcStreamTok
(
new
new
InputStreamReader
(
System
.
in
)).
doCalc
();
else
else
for
(
int
int
i
=
0
;
i
<
av
.
length
;
i
++)
new
new
SimpleCalcStreamTok
(
av
[
i
]).
doCalc
();
}
/** Construct by filename */
public
public
SimpleCalcStreamTok
(
String fileName
)
throws
throws
IOException
{
this
this
(
new
new
FileReader
(
fileName
));
}
/** Construct from an existing Reader */
public
public
SimpleCalcStreamTok
(
Reader rdr
)
throws
throws
IOException
{
tf
=
new
new
StreamTokenizer
(
rdr
);
// Control the input character set:
tf
.
slashSlashComments
(
true
true
);
// treat "//" as comments
tf
.
ordinaryChar
(
'-'
);
// used for subtraction
tf
.
ordinaryChar
(
'/'
);
// used for division
s
=
new
new
Stack
();
}
/** Construct from a Reader and a PrintWriter
*/
public
public
SimpleCalcStreamTok
(
Reader in
,
PrintWriter out
)
throws
throws
IOException
{
this
this
(
in
);
setOutput
(
out
);
}
/**
* Change the output destination.
*/
public
public
void
void
setOutput
(
PrintWriter out
) {
this
this
.
out
=
out
;
}
protected
protected
void
void
doCalc
()
throws
throws
IOException
{
int
int
iType
;
double
double
tmp
;
while
while
((
iType
=
tf
.
nextToken
()) !=
StreamTokenizer
.
TT_EOF
) {
switch
switch
(
iType
) {