OSG Community College C# Beginners Class #7 Log 03/19/2009

2 views
Skip to first unread message

AdelleF

unread,
Mar 24, 2009, 3:21:17 PM3/24/09
to OSG Community College
Pastebins at the bottom.


[2009/03/19 13:00] Snowdrop Short smiles
[2009/03/19 13:00] M1sha Dallin: Hi Snowdrop
[2009/03/19 13:00] Snowdrop Short: how are you today?
[2009/03/19 13:00] M1sha Dallin: Hi everyone
[2009/03/19 13:00] Adelle Fitzgerald: hi M1sha
[2009/03/19 13:00] Adelle Fitzgerald: im good thanks :)
[2009/03/19 13:01] Philippe Debevec: Hello everyone
[2009/03/19 13:01] Adelle Fitzgerald: hi Phillippe
[2009/03/19 13:01] Philippe Debevec: Sorry Adelle have been afk .)
[2009/03/19 13:01] Adelle Fitzgerald: np :)
[2009/03/19 13:01] kidd piko: hiya Philippe
[2009/03/19 13:01] Adelle Fitzgerald: http://www.pastebin.ca/1365572
[2009/03/19 13:01] M1sha Dallin: I finished my calculator - still had
trouble (and failed) with making parts of it generic
[2009/03/19 13:01] Adelle Fitzgerald: that is my calculator
[2009/03/19 13:01] Adelle Fitzgerald: and im kinda pleased with it
tbh, was a big thing for me
[2009/03/19 13:01] M1sha Dallin: http://www.pastebin.ca/1365537 is
mine
[2009/03/19 13:02] Adelle Fitzgerald: wow M1sha, you really went to
town on that!
[2009/03/19 13:02] Snowdrop Short: do you want me to do a review,
like we did last?
[2009/03/19 13:02] Adelle Fitzgerald: sure
[2009/03/19 13:02] Adelle Fitzgerald: just dont spend as long this
time hehe
[2009/03/19 13:02] M1sha Dallin: http://www.pastebin.ca/1365538 is
what I wanted the Token class to look like
[2009/03/19 13:02] Adelle Fitzgerald: ;)
[2009/03/19 13:03] Starky Rubble: yikes!
[2009/03/19 13:03] Adelle Fitzgerald: hiya Starky
[2009/03/19 13:03] M1sha Dallin: but couldn't find a way of using a
delegate to do a conversion in the middle of it :-(
[2009/03/19 13:04] M1sha Dallin: Hi Starky
[2009/03/19 13:04] Snowdrop Short: M1sha .. in a couple of weeks,
we'll return to delegates in the tuesday class
[2009/03/19 13:04] M1sha Dallin: ok
[2009/03/19 13:04] Starky Rubble: Hello all... here's mine such as it
is lol http://www.pastebin.ca/1365568
[2009/03/19 13:04] Snowdrop Short: but that part is quite advanced,
so I'll hold back on it a little
[2009/03/19 13:05] M1sha Dallin: probably wise - confused me a lot
(and interfaces) :-)
[2009/03/19 13:05] Snowdrop Short: ok ... I'll try to give a few
brief questions on each
[2009/03/19 13:05] Snowdrop Short: Adelle's first
[2009/03/19 13:05] Snowdrop Short: http://www.pastebin.ca/1365572
[2009/03/19 13:06] Snowdrop Short: I think this one is pretty
straight forward, and does exactly what it needs to do
[2009/03/19 13:06] Snowdrop Short: although I haven't run it
[2009/03/19 13:06] Snowdrop Short: I have two questions though
[2009/03/19 13:07] Snowdrop Short: why do you have the for loop in
the getInput method?
[2009/03/19 13:07] Snowdrop Short: it will execute each of the if
exactly once
[2009/03/19 13:08] Adelle Fitzgerald: the main reason was I wanted to
experiment with C# itself, so nothing more than that really, i could
have written it as just a procedure and it would have ran the same,
but i wouldnt have learned the 'for' if i didnt play with it
[2009/03/19 13:08] Snowdrop Short: ok .. that's a quite good reason
[2009/03/19 13:08] Snowdrop Short: next
[2009/03/19 13:08] Snowdrop Short: why do you use "ref" rather than
"out"
[2009/03/19 13:08] Adelle Fitzgerald: lol
[2009/03/19 13:09] Adelle Fitzgerald: because i didnt like the idea
of having to give a value to the ints and strings when they are not
used yet. if i used out in the getInput, i would ahve had to assign
the a value to them already, and I think that is pointless
[2009/03/19 13:10] Snowdrop Short: we'll kind of true, but "ref"
implies that the parameter will be used for both input and output
[2009/03/19 13:10] Adelle Fitzgerald: *assign a value to them
[2009/03/19 13:10] Adelle Fitzgerald: gotcha
[2009/03/19 13:11] Snowdrop Short: and in reality, the input values
are never used
[2009/03/19 13:11] Adelle Fitzgerald nods
[2009/03/19 13:11] Snowdrop Short: but it really isn't a big deal
[2009/03/19 13:11] Snowdrop Short: the important thing is to remember
that without "ref" or "out" you cannot change the values inside a
method
[2009/03/19 13:11] Snowdrop Short: ok ..
[2009/03/19 13:11] Snowdrop Short: quickly on to the next
[2009/03/19 13:12] Snowdrop Short: M1sha ..
[2009/03/19 13:12] Adelle Fitzgerald: one of the main things for me
with this excercise was to make use of menthods and classes
[2009/03/19 13:12] Snowdrop Short: http://www.pastebin.ca/1365537
[2009/03/19 13:12] Snowdrop Short: yes... that's exactly the point
[2009/03/19 13:12] Snowdrop Short: simply work with the syntax of
methods and parameters
[2009/03/19 13:13] Snowdrop Short: this one goes over the top
again :-)
[2009/03/19 13:13] M1sha Dallin: sorry
[2009/03/19 13:13] Snowdrop Short: but I think you've shown some very
good improvements
[2009/03/19 13:13] Snowdrop Short: the first code I read from you
didn't really feel like c#
[2009/03/19 13:13] Snowdrop Short: more like some other language
[2009/03/19 13:13] Snowdrop Short: but written in c# syntax
[2009/03/19 13:13] Snowdrop Short: this one feels like c#
[2009/03/19 13:14] M1sha Dallin: trying to think C#
[2009/03/19 13:14] M1sha Dallin: but currently I know how I'd do it
in Ada and am having to translate
[2009/03/19 13:14] Snowdrop Short nods
[2009/03/19 13:14] Snowdrop Short: yes, but you are getting there,
this is much more c# like
[2009/03/19 13:15] Snowdrop Short: ok .. as for the actual
implementation, I haven't run it
[2009/03/19 13:15] Snowdrop Short: but I guess it works, although I
was a bit surprised
[2009/03/19 13:15] M1sha Dallin: it does :-)
[2009/03/19 13:15] Starky Rubble: yep
[2009/03/19 13:16] Snowdrop Short: the thing is, I haven't used
regular expressions in c# very much
[2009/03/19 13:16] Snowdrop Short: and I'm wondering if the sequence
of your pattern matches corresponds to the sequence of tokens
[2009/03/19 13:17] Snowdrop Short: first you loop through your
matches
[2009/03/19 13:17] M1sha Dallin: it does - special case for unary
minus
[2009/03/19 13:17] M1sha Dallin: it's detected later
[2009/03/19 13:17] Snowdrop Short: and then a loop inside that for
your groups
[2009/03/19 13:17] M1sha Dallin: that bit came from msdn
[2009/03/19 13:18] Snowdrop Short: the conversion from infix to
postfix reads like an adaptation of Dijkstra's algorithm
[2009/03/19 13:18] M1sha Dallin: yep
[2009/03/19 13:18] Snowdrop Short: and evaluating postifx expressions
is pretty easy
[2009/03/19 13:19] Snowdrop Short: so I guess this will work
[2009/03/19 13:19] Snowdrop Short smiles
[2009/03/19 13:19] M1sha Dallin: it seems to
[2009/03/19 13:19] Starky Rubble: eseems to work fine
[2009/03/19 13:19] Snowdrop Short: so .. I don't really have much to
say
[2009/03/19 13:20] M1sha Dallin: I'll wait for delegates 2 :-)
[2009/03/19 13:20] Snowdrop Short: execpt ... this is more c# like,
than anything I've seen from you so far
[2009/03/19 13:20] M1sha Dallin: :-)
[2009/03/19 13:22] Snowdrop Short: ok ... that seems to work as well
[2009/03/19 13:22] Snowdrop Short: http://www.pastebin.ca/1365568
[2009/03/19 13:22] Snowdrop Short: I have one question though....
[2009/03/19 13:22] Snowdrop Short: why do you have three classes ?
[2009/03/19 13:23] Starky Rubble: Ummm
[2009/03/19 13:24] Starky Rubble: I thought the calc engine was its
own critter and uninvo;ved with the rest really
[2009/03/19 13:24] Starky Rubble: otherwise I dunno lol
[2009/03/19 13:25] Snowdrop Short: personally, but that's just my
personal opinion
[2009/03/19 13:25] Starky Rubble: I kind ook
[2009/03/19 13:25] Starky Rubble: oops
[2009/03/19 13:25] Starky Rubble: Ok
[2009/03/19 13:25] Snowdrop Short: I would have put "GetInput" into
the MainClass
[2009/03/19 13:25] Starky Rubble: Ah
[2009/03/19 13:26] Snowdrop Short: since the main class seems to have
the responsibility of managing your input and output
[2009/03/19 13:26] Starky Rubble: yes
[2009/03/19 13:26] Starky Rubble: ok
[2009/03/19 13:26] Snowdrop Short: so adding that to the MainClass
feels natural to me
[2009/03/19 13:26] Starky Rubble: it would be both easier and clearer
[2009/03/19 13:27] Snowdrop Short: whether you'd want a separate
class for DoCalc is really much more of an open question
[2009/03/19 13:27] Snowdrop Short: that really depends on the
intentention and future plans
[2009/03/19 13:27] Snowdrop Short: other than that
[2009/03/19 13:28] Snowdrop Short: it is also a solid and vaiable
solution
[2009/03/19 13:28] Snowdrop Short: and I see great progress made in
all three solutions
[2009/03/19 13:28] Starky Rubble: ok
[2009/03/19 13:28] Snowdrop Short: you have all gained some more
confidence in writing c# I think
[2009/03/19 13:28] M1sha Dallin: yep
[2009/03/19 13:28] Adelle Fitzgerald nods
[2009/03/19 13:29] Starky Rubble: yes... a little
[2009/03/19 13:29] Snowdrop Short: at least that's the feeling I get
from reading the code, and comparing to previous works
[2009/03/19 13:30] Snowdrop Short: ok ... today...
[2009/03/19 13:30] Snowdrop Short: I thought we should explore the
framework a bit
[2009/03/19 13:31] Snowdrop Short: so far we've been very focused on
syntax
[2009/03/19 13:31] Snowdrop Short: methods and stuff
[2009/03/19 13:31] Snowdrop Short: but we really haven't looked that
much into the framework
[2009/03/19 13:32] Snowdrop Short: I'll try to stick to the parts of
the framwork which is common to both .Net and Mono
[2009/03/19 13:32] kidd piko: excellent!
[2009/03/19 13:32] Snowdrop Short: which means, almost exclusively
the System. part
[2009/03/19 13:32] Snowdrop Short: System. and most of the things
inside that
[2009/03/19 13:33] Snowdrop Short: Microsoft has a big lump called
[2009/03/19 13:33] Snowdrop Short: Microsoft.
[2009/03/19 13:33] Snowdrop Short: and mono has the same
[2009/03/19 13:33] Snowdrop Short: called Mono.
[2009/03/19 13:33] Snowdrop Short: I'll try to steer clear of those
[2009/03/19 13:33] Adelle Fitzgerald: okies
[2009/03/19 13:33] Snowdrop Short: ok ...
[2009/03/19 13:33] Starky Rubble: generics are not in Mono?
[2009/03/19 13:34] Snowdrop Short: generics are part of the System
framework
[2009/03/19 13:34] Starky Rubble: ahh good
[2009/03/19 13:34] Snowdrop Short: and of the compiler/language
[2009/03/19 13:35] Snowdrop Short: but .... stuff like WCF (Windows
Communication Framework) isn't
[2009/03/19 13:35] Snowdrop Short: or Mono.Posix
[2009/03/19 13:35] Starky Rubble: oh right
[2009/03/19 13:35] Starky Rubble: and gui stuff probably
[2009/03/19 13:35] Snowdrop Short: some guis stuff is shared
[2009/03/19 13:35] Snowdrop Short: WinForms works under Mono
[2009/03/19 13:35] Snowdrop Short: although it looks pretty bad
[2009/03/19 13:35] Starky Rubble: ok
[2009/03/19 13:36] Snowdrop Short: but when you are used the the
customizability of Gnome or KDE
[2009/03/19 13:36] Snowdrop Short: then most other things starts to
look a bit bland
[2009/03/19 13:37] Snowdrop Short: note ... I don't want to start a
religious debate
[2009/03/19 13:37] kidd piko: ll
[2009/03/19 13:37] Starky Rubble: heh
[2009/03/19 13:37] Snowdrop Short: that's the beauty of .net/mono
[2009/03/19 13:37] Snowdrop Short: it is very but not completely
cross platform
[2009/03/19 13:38] Snowdrop Short: gtk works under windows
[2009/03/19 13:38] Snowdrop Short: and winforms works under linux/mac
[2009/03/19 13:38] Snowdrop Short: so both are valid options
[2009/03/19 13:38] Starky Rubble: ok
[2009/03/19 13:38] Snowdrop Short: and good gui's
[2009/03/19 13:39] Snowdrop Short: but I thought we should start out
with file input output
[2009/03/19 13:39] Snowdrop Short: and then look at xml
[2009/03/19 13:39] Snowdrop Short: the xml support is excelent
[2009/03/19 13:39] Snowdrop Short: at least I think so
[2009/03/19 13:40] Snowdrop Short: the way things work is that when
you need things from the framework you need to make sure it is in your
"references"
[2009/03/19 13:40] Snowdrop Short: no matter if you are using
MonoDevelop or Visual Studio
[2009/03/19 13:41] Snowdrop Short: your c# projects has a folder
called "references"
[2009/03/19 13:41] Snowdrop Short: if you open that folder, you'll
see a list of dll's
[2009/03/19 13:41] Snowdrop Short: containing prebuild and
precompiled classes
[2009/03/19 13:42] Snowdrop Short: some references are made by
default
[2009/03/19 13:42] Snowdrop Short: others you'll have to add your
selves on a need to have basis
[2009/03/19 13:42] Snowdrop Short: the part of the framework weel
look at today
[2009/03/19 13:43] Snowdrop Short: is part of the System.dll
[2009/03/19 13:43] Snowdrop Short: and is always included in your
project by default
[2009/03/19 13:43] Snowdrop Short: it is partitioned into several sub
namespaces
[2009/03/19 13:44] Snowdrop Short: specifically we'll look at
System.IO
[2009/03/19 13:44] Snowdrop Short: so either you'll add a "using
System.IO;"
[2009/03/19 13:45] Snowdrop Short: in the using section of your class
[2009/03/19 13:45] Snowdrop Short: or you'll have to write the
namespace "System.IO" over and over again in the code
[2009/03/19 13:46] M1sha Dallin: I prefer the latter
[2009/03/19 13:48] Snowdrop Short: well, that really depends both on
your temperament
[2009/03/19 13:48] Snowdrop Short: and the circumstances
[2009/03/19 13:48] M1sha Dallin: prevents accidents
[2009/03/19 13:50] Snowdrop Short: http://www.pastebin.ca/1365625
[2009/03/19 13:50] Snowdrop Short: this is probably the most minimal
example of accessing a file
[2009/03/19 13:50] Snowdrop Short: I can create
[2009/03/19 13:51] Snowdrop Short: let me know when you have it
[2009/03/19 13:51] M1sha Dallin: yep
[2009/03/19 13:51] Adelle Fitzgerald: yep
[2009/03/19 13:51] kidd piko: got it
[2009/03/19 13:51] Philippe Debevec: OK
[2009/03/19 13:51] Starky Rubble: ok
[2009/03/19 13:51] Snowdrop Short: great
[2009/03/19 13:52] Snowdrop Short: ok .. this has some uneeded stuff
[2009/03/19 13:52] Snowdrop Short: I can either opt to remove the
"using System.IO;" statement
[2009/03/19 13:53] Snowdrop Short: or remove "System.IO" in line 12
[2009/03/19 13:53] Snowdrop Short: both instances of it
[2009/03/19 13:54] Snowdrop Short: the interesting thing is that it
is called a filestream .. not just file
[2009/03/19 13:54] Snowdrop Short: but stream as well
[2009/03/19 13:54] Snowdrop Short: that is because the framework is
very object oriented
[2009/03/19 13:54] Snowdrop Short: so it has a base class called a
stream
[2009/03/19 13:55] Snowdrop Short: either an input stream or an
output stream
[2009/03/19 13:55] Snowdrop Short: and sometimes, but rarely, both
[2009/03/19 13:55] Snowdrop Short: on an output stream
[2009/03/19 13:56] Snowdrop Short: you can pump out data
[2009/03/19 13:56] Snowdrop Short: and simply let the data flow out
of the application
[2009/03/19 13:56] Snowdrop Short: and then there are specializations
of streams
[2009/03/19 13:56] Snowdrop Short: some for files
[2009/03/19 13:56] Snowdrop Short: some for network ports
[2009/03/19 13:56] Snowdrop Short: some for memory
[2009/03/19 13:57] Snowdrop Short: etc
[2009/03/19 13:57] Snowdrop Short: so in line 12 I create a kind of
stream, known as a filestream
[2009/03/19 13:57] Snowdrop Short: I specify that I want write access
to the file
[2009/03/19 13:58] Snowdrop Short: and that it should be openend if
it existes, otherwise it should be created
[2009/03/19 13:58] Snowdrop Short: but that's pretty much all that is
going on
[2009/03/19 13:58] Adelle Fitzgerald nods
[2009/03/19 13:59] Snowdrop Short: the beauty of this is that there
are a lot of other classes which extends the functionality of streams
[2009/03/19 13:59] Snowdrop Short: e.g. for reading integers, floats,
strings etc from a stream
[2009/03/19 13:59] Snowdrop Short: and they really don't care if the
stream is a file stream or a network stream or any other kind of
stream
[2009/03/19 13:59] Snowdrop Short: as long as it is a kind of stream
[2009/03/19 14:00] Starky Rubble: ok
[2009/03/19 14:00] Snowdrop Short: but lets focus on the basic stream
right now
[2009/03/19 14:01] Snowdrop Short: http://www.pastebin.ca/1365639
[2009/03/19 14:01] Snowdrop Short: ok
[2009/03/19 14:01] Snowdrop Short: here is the same thing ... with a
minor change
[2009/03/19 14:02] M1sha Dallin: got it
[2009/03/19 14:02] Adelle Fitzgerald: got it too
[2009/03/19 14:02] Starky Rubble: ok
[2009/03/19 14:02] Snowdrop Short: I've removed the superflurous
System.IO
[2009/03/19 14:03] Snowdrop Short: and added a "WriteByte" operation
[2009/03/19 14:03] Snowdrop Short: or method call
[2009/03/19 14:03] kidd piko: /nod
[2009/03/19 14:03] Snowdrop Short: the thing is
[2009/03/19 14:03] Snowdrop Short: you cannot do that much with
streams
[2009/03/19 14:03] Snowdrop Short: they are very basic
[2009/03/19 14:03] Snowdrop Short: I can either choose to write a
single byte
[2009/03/19 14:03] Snowdrop Short: or an array of bytes
[2009/03/19 14:03] Snowdrop Short: and that's pretty limited
[2009/03/19 14:04] Snowdrop Short: try running the program in your
ide
[2009/03/19 14:04] Snowdrop Short: you'll need to attach it to the
main class
[2009/03/19 14:04] Snowdrop Short: hold on.. I'll make one
[2009/03/19 14:06] Snowdrop Short: http://www.pastebin.ca/1365646
[2009/03/19 14:06] Adelle Fitzgerald: got it
[2009/03/19 14:07] M1sha Dallin: yep
[2009/03/19 14:07] Starky Rubble: ok
[2009/03/19 14:07] kidd piko: y
[2009/03/19 14:07] Philippe Debevec: ok
[2009/03/19 14:07] Snowdrop Short: try to run it
[2009/03/19 14:08] Adelle Fitzgerald: it seems to run
[2009/03/19 14:08] Starky Rubble: yes
[2009/03/19 14:08] Adelle Fitzgerald: but finighes quickly
[2009/03/19 14:08] Adelle Fitzgerald: *finishes
[2009/03/19 14:08] Snowdrop Short nods
[2009/03/19 14:08] Snowdrop Short: it only writes one byte
[2009/03/19 14:08] Snowdrop Short: .. if you're lucky
[2009/03/19 14:08] M1sha Dallin: Does the file need to be closed?
[2009/03/19 14:09] Snowdrop Short: the frame works handles that
automatically for you
[2009/03/19 14:09] M1sha Dallin: ok
[2009/03/19 14:09] Snowdrop Short: and that's both good and bad
[2009/03/19 14:09] Starky Rubble: yes
[2009/03/19 14:09] Snowdrop Short: when the program finishes, it
cleans up
[2009/03/19 14:09] Adelle Fitzgerald: but if the program crashes???
[2009/03/19 14:09] Adelle Fitzgerald: it leaves the mess?
[2009/03/19 14:09] Snowdrop Short: it's still cleaned up
[2009/03/19 14:09] Adelle Fitzgerald: gotcha
[2009/03/19 14:10] Snowdrop Short: the OS knows about the file
[2009/03/19 14:10] Adelle Fitzgerald: right
[2009/03/19 14:10] Snowdrop Short: and cleans it
[2009/03/19 14:10] Snowdrop Short: but ... it's not good to have a
file hanging about
[2009/03/19 14:10] Snowdrop Short: without being closed
[2009/03/19 14:10] Adelle Fitzgerald nods
[2009/03/19 14:10] Adelle Fitzgerald: so does the framework lock the
file open?
[2009/03/19 14:11] Snowdrop Short: yes, if instructed to
[2009/03/19 14:11] Adelle Fitzgerald: gotcha
[2009/03/19 14:11] Snowdrop Short: like I did
[2009/03/19 14:11] Snowdrop Short: with the "FileAccess.Write"
[2009/03/19 14:11] Snowdrop Short: now .. a stream has a "Close"
method
[2009/03/19 14:12] Adelle Fitzgerald: so during the time the file is
open, the framework has exclucivity to it?
[2009/03/19 14:12] Snowdrop Short: so one way would be to add a line
[2009/03/19 14:12] Snowdrop Short: after the "WriteByte" thing
[2009/03/19 14:12] Snowdrop Short: if you ask it to have exclusive
access
[2009/03/19 14:12] Snowdrop Short: if I had specified
"FileAccess.Read"
[2009/03/19 14:12] Snowdrop Short: the others could read
[2009/03/19 14:12] Adelle Fitzgerald: gotcha
[2009/03/19 14:12] Snowdrop Short: but then I couldn't write to it
[2009/03/19 14:13] Adelle Fitzgerald nods
[2009/03/19 14:13] Snowdrop Short: a write access prevents read
access
[2009/03/19 14:13] Starky Rubble: sure
[2009/03/19 14:14] Snowdrop Short: http://www.pastebin.ca/1365651
[2009/03/19 14:14] Snowdrop Short: so this could be one solution
[2009/03/19 14:14] Snowdrop Short: but there is a more elegant way
[2009/03/19 14:14] Snowdrop Short: which has some benefits
[2009/03/19 14:14] Adelle Fitzgerald: kk
[2009/03/19 14:15] Starky Rubble: yep
[2009/03/19 14:15] kidd piko: y
[2009/03/19 14:15] Snowdrop Short: because if for some reason the
"WriteByte" call fails
[2009/03/19 14:15] Snowdrop Short: an exception is thrown
[2009/03/19 14:16] Snowdrop Short: and the Files constructor is
exited right away
[2009/03/19 14:16] Snowdrop Short: without ever touching the "Close"
method
[2009/03/19 14:16] Snowdrop Short: so the file will remain open
[2009/03/19 14:16] Snowdrop Short: meaning we've lost control of when
the file is being closed
[2009/03/19 14:16] Snowdrop Short: a better way
[2009/03/19 14:17] Snowdrop Short: is to use a "using" clause
[2009/03/19 14:17] Snowdrop Short: like this:
[2009/03/19 14:17] Snowdrop Short: http://www.pastebin.ca/1365657
[2009/03/19 14:18] Starky Rubble: ok
[2009/03/19 14:18] M1sha Dallin: ok
[2009/03/19 14:18] Adelle Fitzgerald: kk
[2009/03/19 14:18] Philippe Debevec: yep
[2009/03/19 14:19] Snowdrop Short: notice the "using"
[2009/03/19 14:19] Snowdrop Short: statement in line 12
[2009/03/19 14:19] Snowdrop Short: and that I have removed the
"Close" call
[2009/03/19 14:20] Snowdrop Short: when execution leaves the code
inside the braces { ... }
[2009/03/19 14:20] Snowdrop Short: the close method is automatically
called by the framework
[2009/03/19 14:20] Starky Rubble: ok
[2009/03/19 14:20] Snowdrop Short: and the good thing is ... it will
always do that
[2009/03/19 14:20] Snowdrop Short: regardless of any errors,
exceptions or how we leave that block of code
[2009/03/19 14:21] Snowdrop Short: close will always be called
[2009/03/19 14:21] Snowdrop Short: so by having the "using" clause
[2009/03/19 14:21] kidd piko: so using 'using' will automatically
call the close method. can 'using' automatically call other methods?
[2009/03/19 14:21] Snowdrop Short: we have certain knowledge about
when the file is closed
[2009/03/19 14:21] Snowdrop Short: no
[2009/03/19 14:21] kidd piko: only 'close' ?>
[2009/03/19 14:21] Snowdrop Short: but it's a good question thoug :)
[2009/03/19 14:21] Starky Rubble: i see
[2009/03/19 14:22] Snowdrop Short: in order to under stand what is
going on
[2009/03/19 14:22] Snowdrop Short: I suggest we turn to MSDN
[2009/03/19 14:22] kidd piko: so, 'using' is only used when working
with FileStreams ?
[2009/03/19 14:22] kidd piko: ok
[2009/03/19 14:22] Snowdrop Short: it's a bit more complicated
[2009/03/19 14:22] Snowdrop Short: http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx
[2009/03/19 14:22] Snowdrop Short: take a look at the MSDN page for
FileStream
[2009/03/19 14:23] Snowdrop Short: as I said, it is a specialization
of the class "Stream"
[2009/03/19 14:23] Snowdrop Short: as you can see in the
documentation
[2009/03/19 14:24] Snowdrop Short: [ComVisibleAttribute(true)]
public class FileStream : Stream
[2009/03/19 14:24] Snowdrop Short: forget about "ComVisible"
[2009/03/19 14:24] Snowdrop Short: that's pretty much Microsoft
specific
[2009/03/19 14:24] Snowdrop Short: the important thing is:
[2009/03/19 14:24] Snowdrop Short: public class FileStream : Stream
[2009/03/19 14:24] Snowdrop Short: now... if we look at the
definition of Stream
[2009/03/19 14:25] Snowdrop Short: http://msdn.microsoft.com/en-us/library/system.io.stream.aspx
[2009/03/19 14:25] Snowdrop Short: we can see
[2009/03/19 14:25] Snowdrop Short: public abstract class Stream :
MarshalByRefObject, IDisposable
[2009/03/19 14:25] Snowdrop Short: I know we haven't taked about
interfaces yes
[2009/03/19 14:25] Snowdrop Short: yet
[2009/03/19 14:26] Snowdrop Short: and forget about the
"MarshalByRefObject"
[2009/03/19 14:26] Snowdrop Short: that's a story for another day
[2009/03/19 14:26] Snowdrop Short: the important one for our purposes
is "IDisposable"
[2009/03/19 14:27] Snowdrop Short: which just is a fancy way of
saying that the class will ALWAYS have a method called "Dispose()"
[2009/03/19 14:27] Snowdrop Short: defined as
[2009/03/19 14:27] Snowdrop Short: "void Dispose()"
[2009/03/19 14:27] Snowdrop Short: the compiler knows about this
methods
[2009/03/19 14:27] Starky Rubble: ok
[2009/03/19 14:27] Snowdrop Short: method, even (singular)
[2009/03/19 14:28] Snowdrop Short: and will generate code to execute
the Dispose method
[2009/03/19 14:28] Snowdrop Short: when exiting a "using" block
[2009/03/19 14:28] Snowdrop Short: so any logic implemented in the
"Dispose" method is guranteed to be executed when the block is exited
[2009/03/19 14:29] Snowdrop Short: no matter in which way we leave
the block
[2009/03/19 14:29] Snowdrop Short: (baring a powerfailure)
[2009/03/19 14:29] Starky Rubble: heh
[2009/03/19 14:30] Snowdrop Short: and looking at the documentation
for the Dispose method on the filestream class says
[2009/03/19 14:30] Snowdrop Short:
http://msdn.microsoft.com/en-us/library/system.io.filestream.dispose.aspx
[2009/03/19 14:31] Snowdrop Short: Infrastructure. Releases all
resources used by the Stream. (Inherited from Stream.)
[2009/03/19 14:31] Adelle Fitzgerald nods
[2009/03/19 14:31] Snowdrop Short: and that's really what Dispose is
used for
[2009/03/19 14:31] Snowdrop Short: making sure that everything is
cleaned up nicely
[2009/03/19 14:31] Adelle Fitzgerald: cool
[2009/03/19 14:32] Adelle Fitzgerald: makes easy work of using
files :)
[2009/03/19 14:32] Snowdrop Short: at least of cleaning up
[2009/03/19 14:32] Adelle Fitzgerald: yeah
[2009/03/19 14:32] Starky Rubble: yes
[2009/03/19 14:32] Snowdrop Short: the Dispose method is tightly
integrated into the framwork
[2009/03/19 14:33] Snowdrop Short: if you forget to use a "using"
clause of a class which uses Dispose
[2009/03/19 14:33] Snowdrop Short: the garbage collector will invoke
the Dispose method prior to performing a garbage collection
[2009/03/19 14:33] Adelle Fitzgerald: gotcha
[2009/03/19 14:34] Adelle Fitzgerald: ive crashed servers before
forgetting to close FSO in VBS :-/
[2009/03/19 14:34] Snowdrop Short: it can be used for all sort of
interesting things in your own code as well
[2009/03/19 14:34] Snowdrop Short: closing network connections
[2009/03/19 14:34] Snowdrop Short: I even once saw somebody use it in
a GUI
[2009/03/19 14:35] Snowdrop Short: he create a class which when
instantiated changed the cursor to a "wait" cursor
[2009/03/19 14:35] Snowdrop Short: and then in the dispose method
reverted the cursor back to a normal arrow
[2009/03/19 14:35] Snowdrop Short: and then had a lengthy calculation
in a using block
[2009/03/19 14:36] Snowdrop Short: that way, he was sure that the
mouse cursor reverted back to normal, even if the calculation would
crash
[2009/03/19 14:36] Starky Rubble: hmmm
[2009/03/19 14:36] Adelle Fitzgerald: gotchqa
[2009/03/19 14:37] Snowdrop Short: using is the preferred way in c#
[2009/03/19 14:37] Snowdrop Short: but sometimes it isn't practical
[2009/03/19 14:37] Snowdrop Short: so the Close method for doing it
the "old fashioned" way is still there
[2009/03/19 14:38] Snowdrop Short: so now we know how to open a file
[2009/03/19 14:38] Snowdrop Short: and close it again
[2009/03/19 14:38] Snowdrop Short: and even write bytes
[2009/03/19 14:38] Starky Rubble: right
[2009/03/19 14:38] kidd piko: yep
[2009/03/19 14:38] Snowdrop Short: but how do we write an integer
[2009/03/19 14:38] Snowdrop Short: or a string?
[2009/03/19 14:39] Snowdrop Short: for that we use a writer object
[2009/03/19 14:39] Snowdrop Short: or a reader object if we want
input
[2009/03/19 14:39] Adelle Fitzgerald: StreamWriter?
[2009/03/19 14:40] Snowdrop Short: more like
[2009/03/19 14:40] Snowdrop Short: BinaryWriter or TextWriter
[2009/03/19 14:41] Snowdrop Short: depending on what kind of file you
want
[2009/03/19 14:41] Adelle Fitzgerald nods
[2009/03/19 14:41] Snowdrop Short: if you use text writer
[2009/03/19 14:41] Snowdrop Short: you can read the contents
[2009/03/19 14:41] Snowdrop Short: e.g. an integer will be written as
"87"
[2009/03/19 14:41] Snowdrop Short: the letter 8 followed by the
letter 7
[2009/03/19 14:42] Snowdrop Short: if you use a textwriter
[2009/03/19 14:42] Snowdrop Short: sorry
[2009/03/19 14:42] Snowdrop Short: binarywriter
[2009/03/19 14:42] Snowdrop Short: it will take up 4 bytes
[2009/03/19 14:42] Snowdrop Short: regardless of the actual number
contained in the integer
[2009/03/19 14:42] Starky Rubble: sure
[2009/03/19 14:44] Snowdrop Short: http://www.pastebin.ca/1365677
[2009/03/19 14:44] Snowdrop Short: like this
[2009/03/19 14:45] Starky Rubble: ah
[2009/03/19 14:45] Adelle Fitzgerald: kk
[2009/03/19 14:45] Snowdrop Short: this will write 4 bytes to the
file
[2009/03/19 14:46] Starky Rubble: and BinaryWriter is inj System.IO?
[2009/03/19 14:46] Adelle Fitzgerald: yeah
[2009/03/19 14:46] Adelle Fitzgerald:
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx
[2009/03/19 14:47] Adelle Fitzgerald: if you look there, on the left
are all the namespaces
[2009/03/19 14:48] Adelle Fitzgerald: so in System.IO, you have
NinaryWriter, BinaryReader, TextWriter etc
[2009/03/19 14:48] Snowdrop Short: there are two different
constructors for BinaryWriter
[2009/03/19 14:48] Snowdrop Short: one simply takes a stream
[2009/03/19 14:48] Snowdrop Short: and the other takes an "Encoding"
parameter as well
[2009/03/19 14:48] Adelle Fitzgerald nods
[2009/03/19 14:49] Snowdrop Short: Encoding is an enumeration and can
be either "ASCIIEncoding", "UnicodeEncoding" or "UTF8Encoding"
[2009/03/19 14:50] Snowdrop Short looks around, trying to see if
everybody knows the difference
[2009/03/19 14:50] Adelle Fitzgerald: the most common is UTF8 isnt
it?
[2009/03/19 14:50] Adelle Fitzgerald: i kinda know, well no i dont, i
sorta have seen them in various server applications, but usually i
just leave them alone
[2009/03/19 14:51] Adelle Fitzgerald: its to do with the character
sets though i think
[2009/03/19 14:51] Adelle Fitzgerald: or something along those lines
[2009/03/19 14:51] Snowdrop Short: yes, more or less
[2009/03/19 14:51] Snowdrop Short: and for the binarywriter/reader it
really is only relevant for strings
[2009/03/19 14:52] Snowdrop Short: it's got to do with character
represensations
[2009/03/19 14:52] Adelle Fitzgerald: thats it
[2009/03/19 14:52] Adelle Fitzgerald: yeah i remember now
[2009/03/19 14:52] Snowdrop Short: how numbers are mapped to letters
[2009/03/19 14:52] Snowdrop Short: in ascii
[2009/03/19 14:52] Snowdrop Short: you have the values 0 .. 127
[2009/03/19 14:52] Snowdrop Short: representing different letters and
symbols
[2009/03/19 14:53] Snowdrop Short: e.g. 64 = @
[2009/03/19 14:53] Snowdrop Short: 97 = a
[2009/03/19 14:53] Snowdrop Short: 48 = 1
[2009/03/19 14:53] Snowdrop Short: as a symbol
[2009/03/19 14:53] Snowdrop Short: not the number on
[2009/03/19 14:53] Snowdrop Short: one
[2009/03/19 14:53] Starky Rubble: yes
[2009/03/19 14:53] Snowdrop Short: but for numbers that's a bit too
little
[2009/03/19 14:54] Snowdrop Short: but for other languages than
english
[2009/03/19 14:54] Snowdrop Short: rather
[2009/03/19 14:54] Snowdrop Short: that's a bit too little
[2009/03/19 14:54] Starky Rubble: sure korean for example
[2009/03/19 14:54] Starky Rubble: or Urdu
[2009/03/19 14:54] Starky Rubble: etc
[2009/03/19 14:54] Snowdrop Short: so a new standard was created
[2009/03/19 14:54] Snowdrop Short: called UNICODE
[2009/03/19 14:55] Snowdrop Short: which uses 4 bytes for every
symbol
[2009/03/19 14:55] Snowdrop Short: giving more than 4 billion
different symbols
[2009/03/19 14:55] Snowdrop Short: that's all well and good
[2009/03/19 14:56] Snowdrop Short: but for ordinary english text,
that means that it would take up 4 times a much space as is really
needed
[2009/03/19 14:56] Snowdrop Short: also when sent over the network
[2009/03/19 14:56] Starky Rubble: right
[2009/03/19 14:57] Snowdrop Short: so in order to reduce that waste,
diferent schemes of compression was build on top of unicode
[2009/03/19 14:57] Snowdrop Short: UTF-8
[2009/03/19 14:57] Snowdrop Short: which works much like ascii
[2009/03/19 14:57] Snowdrop Short: for all symbols with values
between 1 and 127
[2009/03/19 14:58] Snowdrop Short: but that makes dealing with
symbols above 128 a bit cumbersome
[2009/03/19 14:58] Snowdrop Short: for most european languages that's
still acceptable
[2009/03/19 14:59] Snowdrop Short: because they usually only have
minor adapations to the ordinary latin alphabet
[2009/03/19 14:59] Snowdrop Short: but for cyrillic (russian) and
Kanji (Japaneese) it is very impractical
[2009/03/19 14:59] Snowdrop Short: so there are other encodings
[2009/03/19 14:59] Snowdrop Short: one is UTF-16
[2009/03/19 15:00] Snowdrop Short: where every symbol takes up at
least two bytes
[2009/03/19 15:00] Snowdrop Short: UTF-16 and what .Net/Mono calls
unicode is the same thing
[2009/03/19 15:00] Snowdrop Short: (based on historical reasons)
[2009/03/19 15:01] Snowdrop Short: (the earliest versions of unicode
only used two bytes)
[2009/03/19 15:01] Starky Rubble: ok
[2009/03/19 15:01] Snowdrop Short: and incidently, internally .Net/
Mono uses UTF-16, or what it calls unicode
[2009/03/19 15:02] Snowdrop Short: so if you expect your program to
be used only in the US
[2009/03/19 15:02] Adelle Fitzgerald: so, if we dont specify the
encoding in BinaryWriter, it will assume UTF-8?
[2009/03/19 15:02] Snowdrop Short: ASCII is the most efficient
[2009/03/19 15:03] Snowdrop Short: it will assume unicode (utf-16)
since that is the default for the framework
[2009/03/19 15:03] Adelle Fitzgerald: hmm, msnd says this: '
Initializes a new instance of the BinaryWriter class based on the
supplied stream and using UTF-8 as the encoding for strings. '
[2009/03/19 15:03] Adelle Fitzgerald: i mean msdn
[2009/03/19 15:03] Snowdrop Short: UTF-8 is the most efficient choice
for programs which primarily will be used in the US and western europe
[2009/03/19 15:03] Starky Rubble: ok
[2009/03/19 15:04] Adelle Fitzgerald: ive not heard of UTF-16 before
[2009/03/19 15:04] Starky Rubble: but you get to choose as desired
[2009/03/19 15:04] Snowdrop Short: hmm .. I must have gotten the
default encoding wrong
[2009/03/19 15:06] Snowdrop Short: and if you want to cater fro most
of the world, and are willing to accept a little wasted space for the
american customers
[2009/03/19 15:06] Snowdrop Short: unicode or UTF-16 is your best
choice
[2009/03/19 15:06] Adelle Fitzgerald nods
[2009/03/19 15:06] Snowdrop Short: rarely will UTF-32 be the best
choice
[2009/03/19 15:06] Snowdrop Short: and I have only seen it used
rarely
[2009/03/19 15:07] Starky Rubble: ok
[2009/03/19 15:07] Snowdrop Short: so now we know how to open a file
[2009/03/19 15:07] Snowdrop Short: close a file
[2009/03/19 15:07] Snowdrop Short: and read and write data
[2009/03/19 15:07] Adelle Fitzgerald: :)
[2009/03/19 15:08] Snowdrop Short: pretty easy ... heh?
[2009/03/19 15:08] M1sha Dallin: :-)
[2009/03/19 15:08] Adelle Fitzgerald: in theory, yeah, in
practice......
[2009/03/19 15:08] Adelle Fitzgerald: hehe
[2009/03/19 15:08] Snowdrop Short: ok
[2009/03/19 15:08] Snowdrop Short: we're just past midnight
[2009/03/19 15:08] Snowdrop Short: time to close up I think
[2009/03/19 15:08] Adelle Fitzgerald: ok, one thing though about
reading and writing files....
[2009/03/19 15:08] Snowdrop Short: yes ....
[2009/03/19 15:09] Adelle Fitzgerald: on windows you would choose say
"c:\folder\.....", but that would be completely different in Linux
[2009/03/19 15:09] Adelle Fitzgerald: how would we cater for that?
[2009/03/19 15:09] Snowdrop Short: yes
[2009/03/19 15:09] Snowdrop Short: one big difference is that linux
uses "/"
[2009/03/19 15:09] Snowdrop Short: rather than "\"
[2009/03/19 15:10] Snowdrop Short: but the frame work is intelligent
about that
[2009/03/19 15:10] Adelle Fitzgerald: ahh
[2009/03/19 15:10] Snowdrop Short: so that's not a problem
[2009/03/19 15:10] Snowdrop Short: the C:
[2009/03/19 15:10] Snowdrop Short: thing is
[2009/03/19 15:10] Snowdrop Short: so it is always best to use
"relative" paths
[2009/03/19 15:10] Snowdrop Short: things like "../../myfile"
[2009/03/19 15:11] Adelle Fitzgerald: gotcha
[2009/03/19 15:11] Snowdrop Short: or "subdir/subsubdir/myfile"
[2009/03/19 15:11] Snowdrop Short: or "\"
[2009/03/19 15:11] Adelle Fitzgerald: and that would work in both
windows and linux without having to have a load of other code to
differnciate between the operating systems?
[2009/03/19 15:11] Starky Rubble: I did see drive methods if we
really need to address one or offer a list or whatever
[2009/03/19 15:12] Snowdrop Short: the biggest thing to watch out for
is upper and lower case in file names
[2009/03/19 15:12] Starky Rubble: right
[2009/03/19 15:12] Starky Rubble: case matters lol
[2009/03/19 15:12] Adelle Fitzgerald: case matters with filenames
too?
[2009/03/19 15:12] Snowdrop Short: because in linux case matters in
filenames
[2009/03/19 15:12] Starky Rubble: in *nix yes
[2009/03/19 15:12] Adelle Fitzgerald: right
[2009/03/19 15:12] Snowdrop Short: but it doesn't in windows
[2009/03/19 15:13] Adelle Fitzgerald: gotcha :)
[2009/03/19 15:13] Adelle Fitzgerald: okies, any homework for us?
[2009/03/19 15:13] Snowdrop Short: so in linux "my_file" is not the
same as "My_file"
[2009/03/19 15:13] Snowdrop Short: yes ...
[2009/03/19 15:14] Snowdrop Short: I'd like for you to adopt your
calculator classes
[2009/03/19 15:14] Snowdrop Short: so it logs what is is doing
[2009/03/19 15:14] Snowdrop Short: i.e.
[2009/03/19 15:14] Adelle Fitzgerald: okies :)
[2009/03/19 15:14] Starky Rubble: ok
[2009/03/19 15:14] M1sha Dallin: ok
[2009/03/19 15:14] Snowdrop Short: calculating "4" + "3" giving "7"
[2009/03/19 15:14] Adelle Fitzgerald nods
[2009/03/19 15:14] Starky Rubble: got it
[2009/03/19 15:15] Adelle Fitzgerald: im ooking forward to doing it
now im getting the hang of this a bit :)
[2009/03/19 15:15] Adelle Fitzgerald: *looking
[2009/03/19 15:15] Snowdrop Short: ok .. anything else
[2009/03/19 15:15] Adelle Fitzgerald: just one thing..... sleep
well ;)
[2009/03/19 15:15] Adelle Fitzgerald: hehe
[2009/03/19 15:15] Snowdrop Short: before I have my talk with the
sandman?
[2009/03/19 15:15] Starky Rubble whispers: nope - thanks! as
always...
[2009/03/19 15:15] Snowdrop Short smiles
[2009/03/19 15:15] Snowdrop Short: thanks
[2009/03/19 15:15] kidd piko: thanx a million Snowdrop
[2009/03/19 15:15] M1sha Dallin: ty Snowdrop
[2009/03/19 15:15] Snowdrop Short: sleep tight, when you get there
[2009/03/19 15:16] Starky Rubble: okie dokes
[2009/03/19 15:16] Snowdrop Short: and thank you for listening to my
ramblings
[2009/03/19 15:16] Adelle Fitzgerald: night Snowdrop, and thanks :)




http://www.pastebin.ca/1365572

using System;
using System.Collections;
using System.Collections.Generic;

namespace Calculator
{
class Calculator
{
public void getInput(ref string op, ref int a, ref int b)
{
string getInput;
for (int i = 0; i < 3; i++)
{
getInput = Console.ReadLine();
if (i == 0)
{
if (!int.TryParse(getInput, out a))
{
Console.WriteLine("The input must be an
integer");
return;
}
}
else if (i == 1)
{
op = getInput;
if ((op != "+") & (op != "-") & (op != "/") & (op !
= "*"))
{
Console.WriteLine("The input {0} is not the
correct operand. Allowed operands are: + - / *", op);
return;
}
}
else if (i == 2)
{
if (!int.TryParse(getInput, out b))
{
Console.WriteLine("The input must be an
integer");
return;
}
}
}
}
public int doCalc(string op, int a, int b)
{
int result;
switch (op)
{
case "+":
result = a + b;
return result;
case "-":
result = a - b;
return result;
case "/":
result = a / b;
return result;
case "*":
result = a * b;
return result;
default:
result = 0;
return result;
}
}
}
class MainClass
{
static void Main(string[] args)
{
int a = 0, b = 0;
string op = "";
Calculator calc = new Calculator();
Console.WriteLine("Please input calculation, hitting
[Enter] after each line.");
calc.getInput(ref op, ref a, ref b);
Console.WriteLine("The answer is {0}", calc.doCalc(op, a,
b));
}
}
}




http://www.pastebin.ca/1365537

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;

namespace Calculator
{
//
----------------------------------------------------------------------------
public class ExpressionCalculator
{
// Basic type definitions for Postfix conversion
private enum TokenType
{
None = 0,
WhiteSpace = 1,
Operand = 2,
Op_OpenParen = 3,
Op_CloseParen = 4,
Op_Plus = 5,
Op_Minus = 6,
Op_Multiply = 7,
Op_Divide = 8,
Op_Exponentiate = 9,
Op_UnaryMinus = 10,
Invalid = 11
}

private enum OperatorAssociativity
{
LeftRight,
RightLeft,
None
}

// The following array MUST match the order for TokenType
private static OperatorAssociativity[] TokenAssociativity ={

OperatorAssociativity.None,

OperatorAssociativity.None,

OperatorAssociativity.None,

OperatorAssociativity.LeftRight,

OperatorAssociativity.LeftRight,

OperatorAssociativity.LeftRight,

OperatorAssociativity.LeftRight,

OperatorAssociativity.LeftRight,

OperatorAssociativity.LeftRight,

OperatorAssociativity.RightLeft,

OperatorAssociativity.RightLeft
};
// Regex definition for tokenizing
private const string CalculatorPattern =
@"(?<whitespace>\s+)|" +
@"(?<operand>\d+\.?\d*|\.\d+)|" +
@"(?<open>\()|" +
@"(?<close>\))|" +
@"(?<plus>\+)|" +
@"(?<minus>\-)|" +
@"(?<multiply>\*)|" +
@"(?<divide>\/)|" +
@"(?<exponentiate>\^)|";

// Basic element of an expression
private class Token
{
public TokenType m_Index;
public Double m_Value;

public Token(TokenType index, Double value)
{
m_Index = index;
m_Value = value;
}
}

// Structures for representing and converting an expression
private class TokenStack : Stack<Token> { }

private class TokenList : List<Token>
{
public TokenList () {}

public TokenList (Regex re, string theString)
{
MatchCollection Matches = re.Matches(theString);

foreach (Match match in Matches)
{
int i = 0;
foreach (Group group in match.Groups)
{
string matchValue = group.Value;
bool success = group.Success;

// Ignore Whitespace
if (success && i > 1)
{
if ((TokenType)i == TokenType.Operand)
this.Add(new Token((TokenType)i,
Double.Parse(matchValue)));
else
this.Add(new Token((TokenType)i,
0.0));
}
i++;
}
}
}

// Method: ToPostfix - translates a list of tokens
representing an infix expression to a list representing postfix
// input: TokenList
// output: TokenList
// return: Boolean
//
public static Boolean ToPostfix(TokenList infixList, out
TokenList postfixList)
{
TokenStack OperandStack = new TokenStack();
TokenList PostfixList = new TokenList();
Boolean ExpectOperand = true;

postfixList = PostfixList;

foreach (Token token in infixList)
{
switch (token.m_Index)
{
case TokenType.Operand:
if (!ExpectOperand) return false;
PostfixList.Add(token);
ExpectOperand = false;
break;

case TokenType.Op_Minus:
case TokenType.Op_Plus:
case TokenType.Op_Multiply:
case TokenType.Op_Divide:
case TokenType.Op_Exponentiate:
if (ExpectOperand && token.m_Index ==
TokenType.Op_Minus) token.m_Index = TokenType.Op_UnaryMinus;
do
{
if (OperandStack.Count == 0 ||
token.m_Index > OperandStack.Peek
().m_Index && TokenAssociativity[(int)token.m_Index] ==
OperatorAssociativity.LeftRight ||
token.m_Index >= OperandStack.Peek
().m_Index && TokenAssociativity[(int)token.m_Index] ==
OperatorAssociativity.RightLeft)
{
OperandStack.Push(token);
break;
}
else if (token.m_Index <
OperandStack.Peek().m_Index)
PostfixList.Add(OperandStack.Pop
());
else
return false;
}
while (true == true);

ExpectOperand = true;
break;

case TokenType.Op_OpenParen:
OperandStack.Push(token);
break;

case TokenType.Op_CloseParen:
while (OperandStack.Peek().m_Index !=
TokenType.Op_OpenParen) PostfixList.Add(OperandStack.Pop());

OperandStack.Pop();
break;

default:
Console.WriteLine("Token invalid in this
context");
return false;
}
}

// Transfer remaining stack
while (OperandStack.Count > 0) PostfixList.Add
(OperandStack.Pop());

postfixList = PostfixList;
return true;
}

// Method: EvaluatePostfixList - evaluates the expression
in a postfix list
// input: TokenList
// output: Double
// return: Boolean
public static Boolean EvaluatePostfixList(TokenList
postfixList, out Double result)
{
Stack<Token> ProcessStack = new Stack<Token>();
Token OperA;

foreach (Token token in postfixList)
{
try
{
switch (token.m_Index)
{
case TokenType.Operand:
ProcessStack.Push(token);
break;

case TokenType.Op_UnaryMinus:
ProcessStack.Peek().m_Value = -
ProcessStack.Peek().m_Value;
break;

case TokenType.Op_Plus:
OperA = ProcessStack.Pop();
ProcessStack.Peek().m_Value +=
OperA.m_Value;
break;

case TokenType.Op_Minus:
OperA = ProcessStack.Pop();
ProcessStack.Peek().m_Value -=
OperA.m_Value;
break;

case TokenType.Op_Multiply:
OperA = ProcessStack.Pop();
ProcessStack.Peek().m_Value *=
OperA.m_Value;
break;

case TokenType.Op_Divide:
OperA = ProcessStack.Pop();
ProcessStack.Peek().m_Value /=
OperA.m_Value;
break;

case TokenType.Op_Exponentiate:
OperA = ProcessStack.Pop();
ProcessStack.Peek().m_Value = Math.Pow
(ProcessStack.Peek().m_Value, OperA.m_Value);
break;

default:
Console.WriteLine("Token invalid in
this context");
result = 0.0;
return false;
}
}
catch
{
result = 0.0;
return false;
}
}

result = ProcessStack.Pop().m_Value;
return true;
}

}





// Method: EvaluateStringExpression - evaluates the
expression in a string
// input: string
// output: Double
// return: Boolean - true if a successful evaluation
//
public static Boolean EvaluateStringExpression(string
expression, out Double result)
{
Regex Re = new Regex(CalculatorPattern);
TokenList InfixList = new TokenList(Re, expression);
TokenList PostfixList = new TokenList();

Boolean status;
Double ExpressionResult;

if (TokenList.ToPostfix(InfixList, out PostfixList))
{
status = TokenList.EvaluatePostfixList(PostfixList,
out ExpressionResult);

result = ExpressionResult;
return status;
}

result = 0.0;
return false;
}

}

//
----------------------------------------------------------------------------
class Program
{
// Method - Get a string from the console after displaying a
prompt
// input: string
// return: string
//
public static string GetStringWithPrompt(string prompt)
{
Console.Write("\n\n{0}\n==> ", prompt);
return Console.ReadLine();
}


// Main program
static void Main(string[] args)
{
string CalcString;

Double Answer = 0.0;
Boolean Converted = false;

Console.WriteLine(" An 'Expression Calculator' written in
C#.");
Console.WriteLine(" It evaluates expressions (and
parenthesised expressions) written using");
Console.WriteLine("(double precision) numerics and the
standard operators +, -, *, /");
Console.WriteLine("as well as ^ (for exponentiation).");
Console.WriteLine(" The normal rules for operator
precedence apply, ");
Console.WriteLine("e.g. (2+3.01^3*4)/2 evaluates to
55.541802");

do
{
CalcString = GetStringWithPrompt("Enter expression or
q (or Q) to quit");
if (CalcString.ToLower()[0] == 'q') break;

Converted =
ExpressionCalculator.EvaluateStringExpression(CalcString, out Answer);
if (Converted)
Console.WriteLine("\nThe expression \"{0}\"
evaluated to: {1}", CalcString, Answer);
else
Console.WriteLine("\nThe expression \"{0}\"
contained errors", CalcString);
} while (true == true);
}
}
}




http://www.pastebin.ca/1365538

public delegate Token<T1, T2> BuildTokenDelegate<T1, T2>(int
anInt, string aString);

// Basic element of an expression
public class Token<T1,T2>
{
public T1 m_Index;
public T2 m_Value;

public Token(T1 index, T2 value)
{
m_Index = index;
m_Value = value;
}

// Structures for representing and converting an expression
public class TokenStack : Stack<Token<T1,T2>> { }

public class TokenList : List<Token<T1, T2>>
{
public TokenList() { }

public TokenList (string theString, Regex re)
{
MatchCollection Matches = re.Matches(theString);

foreach (Match match in Matches)
{
int i = 0;
foreach (Group group in match.Groups)
{
string matchValue = group.Value;
bool success = group.Success;

// Ignore Whitespace
if (success && i > 1)
{
BuildTokenDelegate<T1, T2> BuildToken;

this.Add();

//if ((T1)i == T1.Operand)
// TheList.Add(new Token((T1)i, T2.Parse
(matchValue)));
//else
// TheList.Add(new Token((T1)i, (T2)
0.0));
}
i++;
}
}
}
}
}




http://www.pastebin.ca/1365568

/* NewCalc2b - A Simple Arithmatic Calculator C# Exercise
* By Starky Rubble
* The Ides Of March, 2009
*
* 0. show entry message - set start flag
*
* 1. prompt for, read, and validate, number1 (checking for Quit)
* if it is an error reprompt and read another until the number is
valid (or Quit)
* show the number
*
* 2. prompt for, read, and validate, an arithmatic operator
(checking for Quit)
* if it is an error reprompt and read another until the operator is
valid (or Quit)
* show the operator - remove start flag
*
* 3. prompt for, read, validate, number2 (checking for Quit)
* if it is an error reprompt and read another until the number is
valid (or Quit)
* show the number and calc the result
*
* 4. prompt for, read, validate, an arithmatic operator including
the = sign (checking for Quit)
* if it is an error reprompt and read another until the operator is
valid (or Quit)
*
* 5 if operator is =, show it and the calced result and then clear
all variables and start over.
* Otherwise make number1 equal result and loop at number 2 until it
is an - sign (or Quit).
*
* `6. On Quit, display exit message
*
*
*
*
*/


using System;

namespace Calculator
{

//Declare ProcessInput class which handles the keyboad input
public class ProcessInput
{


//GetInput accepts a string and string type checks the rules
and if happy returns true.
//It also pases back a Quit flag and returns a parsed float.
public bool GetInput(string inString, int strType, out bool
quit, out float num)
{
//internal variables
string str = inString;
sType = strType;

//boolean exit flag and float defaults - returned via
'out'
quit = false;
num = 0;

if (str.ToUpper() == "Q") { quit = true; return false; }

//Check if operator first - if it's not one, it must be a
number
if (sType == OPTYPE)
{
if ((str == "") || (OPERATORS.Contains(str) == false))
{
Console.Write("'" + str + "' is not useful here.
Please enter a +,-,*,/, or a Q to Quit");
return false;
}
}
else
{
if (float.TryParse(str, out num) == false)
{
Console.Write("'" + str + "' is not a number.
Please enter a NUMBER or Q to Quit");
return false;
}
}
//return success
return true;
}



//Constants used throughout the class
public const int NUMTYPE = 1;
public const int OPTYPE = 2;
public const string OPERATORS = "+-*/";
public int sType;

}


//class declaration - create a new variable type named
DoCalculation
public class DoCalculation
{
//Constructor of class - define the shape of DoCalculation
public float DoCalc(string inOp, float inNum1, float inNum2)
{
//internal variables
string op = inOp;
float num1 = inNum1;
float num2 = inNum2;
float result = 0;

switch (op)
{
case "-":
result = num1 - num2;
break;
case "+":
result = num1 + num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
default:
result = 0;
Console.Write("Program error in DoCalculation: op
= " + op + "'");
break;
}


//return the float result
return result;
}

}

class MainClass
{

public static void Main(string[] args)
{
const int NUMTYPE = 1;
const int OPTYPE = 2;

string inputStr = "";
string strOp = "";
string bigStr = "";
float numTemp = 0;
float numOne = 0;
float numTwo = 0;
float numResult = 0;

// start not quitting and not OK
bool flagQuit = false;
bool flagOk = false;

// Get the first number - setting flagOk to false
while (flagQuit == false)
{
flagOk = false; //reset control bool - it is true if
we are happy

Console.Write(" -+*/ A Simple Calulator \\*+-
\n");
Console.Write("Please enter a number i.e. 10 or -22.33
or press Q to Quit: ");
while ((flagOk == false) && (flagQuit == false))
{
inputStr = Console.ReadLine();
ProcessInput IsEntryOk = new ProcessInput();
flagOk = IsEntryOk.GetInput(inputStr, NUMTYPE, out
flagQuit, out numTemp);
if (flagQuit == false)
{
numOne = numTemp;
bigStr = bigStr + inputStr;
}
}

//loop here getting more numbers and operators until
they select Q
while (flagQuit == false)
{
Console.WriteLine(numOne);
//if we're not quitting, reset flagOk and get the
operatior
if (flagQuit == false)
{
flagOk = false;
Console.Write("Please enter an operator from
'-+*/', or 'Q' to Quit: ");
while ((flagOk == false) && (flagQuit ==
false))
{
inputStr = Console.ReadLine();
ProcessInput IsOpOk = new ProcessInput();
flagOk = IsOpOk.GetInput(inputStr, OPTYPE,
out flagQuit, out numTwo);
}
Console.WriteLine("" + inputStr);
strOp = inputStr;
bigStr = bigStr + inputStr;
}

//if we're still not quitting, reset flagOk get
the next number
if (flagQuit == false)
{
flagOk = false;
Console.Write("Please enter another number
i.e. 10 or -22.33 or press Q to Quit: ");
while ((flagOk == false) && (flagQuit ==
false))
{
inputStr = Console.ReadLine();
ProcessInput IsNum2Ok = new ProcessInput
();
flagOk = IsNum2Ok.GetInput(inputStr,
NUMTYPE, out flagQuit, out numTwo);
}
}

//If we have two numbers and an op its calulate
time - so do so!
if ((flagOk == true) && (flagQuit == false))
{
bigStr = bigStr + inputStr;
DoCalculation Calc = new DoCalculation();
numResult = Calc.DoCalc(strOp, numOne,
numTwo);
Console.WriteLine(numOne + strOp + numTwo +
"=" + numResult);
Console.WriteLine(bigStr + "=" + numResult +
"\n\n");

//numOne gets the result - get another
operator
numOne = numResult;
}
}



Console.WriteLine("");
Console.WriteLine("");
}
Console.WriteLine("Thanks and bye!");
}
}
}




http://www.pastebin.ca/1365625

using System;
using System.IO;
namespace Lesson06
{


public class Files
{

public Files()
{
System.IO.FileStream f = new System.IO.FileStream("My_file",
FileMode.OpenOrCreate, FileAccess.Write);
}
}
}




http://www.pastebin.ca/1365639

using System;
using System.IO;
namespace Lesson06
{


public class Files
{

public Files()
{
FileStream f = new FileStream("My_file", FileMode.OpenOrCreate,
FileAccess.Write);
f.WriteByte(23);
}
}
}




http://www.pastebin.ca/1365646

using System;
using System.IO;
namespace Lesson06
{


public class Files
{

public Files()
{
FileStream f = new FileStream("My_file", FileMode.OpenOrCreate,
FileAccess.Write);
f.WriteByte(23);
}
}

class main
{
public static void Main(string[] args)
{
Files f = new Files();
}
}
}




http://www.pastebin.ca/1365651

using System;
using System.IO;
namespace Lesson06
{


public class Files
{

public Files()
{
FileStream f = new FileStream("My_file", FileMode.OpenOrCreate,
FileAccess.Write);
f.WriteByte(23);
f.Close();
}
}

class main
{
public static void Main(string[] args)
{
Files f = new Files();
}
}
}




http://www.pastebin.ca/1365657

using System;
using System.IO;
namespace Lesson06
{


public class Files
{

public Files()
{
using(FileStream f = new FileStream("My_file",
FileMode.OpenOrCreate, FileAccess.Write))
{
f.WriteByte(23);
}
}
}

class main
{
public static void Main(string[] args)
{
Files f = new Files();
}
}
}




http://www.pastebin.ca/1365677

using System;
using System.IO;
namespace Lesson06
{


public class Files
{

public Files()
{
using(FileStream f = new FileStream("My_file",
FileMode.OpenOrCreate, FileAccess.Write))
{
using(BinaryWriter bw = new BinaryWriter(f))
{
bw.Write(23);
}
}
}
}

class main
{
public static void Main(string[] args)
{
Files f = new Files();
}
}
}
Reply all
Reply to author
Forward
0 new messages