Click here to Skip to main content
15,891,033 members
Home / Discussions / C#
   

C#

 
GeneralRe: Streamwriter output Pin
Pierre besquent16-Mar-11 0:11
Pierre besquent16-Mar-11 0:11 
GeneralRe: Streamwriter output Pin
Pete O'Hanlon16-Mar-11 0:23
mvePete O'Hanlon16-Mar-11 0:23 
GeneralRe: Streamwriter output Pin
Luc Pattyn16-Mar-11 0:27
sitebuilderLuc Pattyn16-Mar-11 0:27 
GeneralRe: Streamwriter output Pin
Pierre besquent16-Mar-11 3:41
Pierre besquent16-Mar-11 3:41 
Questionbest data strcuture for numeric parsing. Pin
shivamkalra15-Mar-11 18:24
shivamkalra15-Mar-11 18:24 
AnswerRe: best data strcuture for numeric parsing. Pin
GlobX15-Mar-11 19:27
GlobX15-Mar-11 19:27 
GeneralRe: best data strcuture for numeric parsing. Pin
shivamkalra15-Mar-11 20:22
shivamkalra15-Mar-11 20:22 
GeneralRe: best data strcuture for numeric parsing. Pin
GlobX15-Mar-11 20:32
GlobX15-Mar-11 20:32 
Sure, but as Maxx points out it's necessary complexity - just by the smell of it I don't think you can avoid the iterative approach. The program needs to know what "*" means and how to do it. It also needs to know what "+" means and how to do it, etc. ad infinitum.

Besides, the operator class I gave you was what, 6 lines long? It won't take you long at all!

Having said that, you could do something fancy and add attributes to the Operator classes that specify what token identifies that operation. You could then use reflection to, once again, loop over all the Operator classes in the Assembly and find the one with the matching token.

This way you avoid a big set of if/else or switch/case statements, but it's going to be slower. Admittedly more fun, IMO, but definitely slower. In fact, as a practical solution I think that's crazy (but interesting).

You could reduce the number of classes by changing IOperator into a single delegate:

// syntax is dubious
delegate double OperateDelegate(double left, double right);


Then you could have a Dictionary/Hashtable mapping tokens ("*", "+", "sin" etc.) to delegates, which you could declare nice and compactly, like so:

var operatorLookup = new Dictionary<string, OperateDelegate>();

operatorLookup.Add("*", ((left, right) => left * right));
operatorLoojup.Add("+", ((left, right) => left + right));

// etc.
// OR you could go even MORE terse!

var operatorLookup = new Dictionary<string, OperateDelegate>
{
    { "*", ((left, right) => left * right) },
    { "+", ((left, right) => left + right) }
};

// etc.


It's a trade-off of file-size vs. readability, which it is up to you as the developer to decide which one is important.
GeneralRe: best data strcuture for numeric parsing. Pin
shivamkalra15-Mar-11 20:37
shivamkalra15-Mar-11 20:37 
GeneralRe: best data strcuture for numeric parsing. Pin
GlobX15-Mar-11 20:53
GlobX15-Mar-11 20:53 
GeneralRe: best data strcuture for numeric parsing. Pin
shivamkalra15-Mar-11 20:33
shivamkalra15-Mar-11 20:33 
AnswerRe: best data strcuture for numeric parsing. Pin
_Maxxx_15-Mar-11 19:43
professional_Maxxx_15-Mar-11 19:43 
QuestionRe: best data strcuture for numeric parsing. Pin
GlobX15-Mar-11 19:58
GlobX15-Mar-11 19:58 
AnswerRe: best data strcuture for numeric parsing. Pin
shivamkalra15-Mar-11 20:23
shivamkalra15-Mar-11 20:23 
GeneralRe: best data strcuture for numeric parsing. Pin
shivamkalra15-Mar-11 20:25
shivamkalra15-Mar-11 20:25 
AnswerRe: best data strcuture for numeric parsing. Pin
PIEBALDconsult16-Mar-11 2:56
mvePIEBALDconsult16-Mar-11 2:56 
AnswerRe: best data strcuture for numeric parsing. Pin
_Erik_16-Mar-11 3:44
_Erik_16-Mar-11 3:44 
GeneralRe: best data strcuture for numeric parsing. Pin
shivamkalra16-Mar-11 8:01
shivamkalra16-Mar-11 8:01 
AnswerRe: best data strcuture for numeric parsing. Pin
Alan Balkany16-Mar-11 4:27
Alan Balkany16-Mar-11 4:27 
AnswerRe: best data strcuture for numeric parsing. Pin
David198716-Mar-11 4:57
David198716-Mar-11 4:57 
QuestionObtaining Administrator Authority Pin
gmhanna15-Mar-11 17:21
gmhanna15-Mar-11 17:21 
AnswerRe: Obtaining Administrator Authority Pin
Dave Kreskowiak15-Mar-11 17:43
mveDave Kreskowiak15-Mar-11 17:43 
AnswerRe: Obtaining Administrator Authority Pin
DaveyM6916-Mar-11 2:15
professionalDaveyM6916-Mar-11 2:15 
AnswerRe: Obtaining Administrator Authority Pin
PIEBALDconsult16-Mar-11 17:03
mvePIEBALDconsult16-Mar-11 17:03 
Questioncpu performance with wmi ... Pin
SungBae.Han15-Mar-11 13:19
SungBae.Han15-Mar-11 13:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.