Click here to Skip to main content
15,890,947 members
Home / Discussions / C#
   

C#

 
GeneralRe: Streamwriter output Pin
Pierre besquent16-Mar-11 0:09
Pierre besquent16-Mar-11 0:09 
AnswerRe: Streamwriter output Pin
Luc Pattyn16-Mar-11 0:06
sitebuilderLuc Pattyn16-Mar-11 0:06 
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 
Seeing as this is an assignment, I'll try to give you a rough guide more so than a fully detailed answer, so the way I'm thinking is using a builder and a set of Operator types. Something like:

public interface IOperator
{
    double Operate(double leftOperand, double rightOperand);
}

public static class OperatorBuilder
{

    public IOperator BuildFromToken(string operatorToken)
    {

        switch (operatorToken)
        {
            case "*":
                return new MultiplyOperator();
        }

    }

}

public class MultiplyOperator : IOperator
{
    public double Operate(double leftOperand, double rightOperand)
    {
        return leftOperand * rightOperand;
    }
}

// example usage
public void Main(string[] args)
{

    // program is run like:
    // CalculatorExample.exe 5.0 6.0 *

    var operator = OperatorBuilder.BuildFromToken(args[2]);
    
    Console.WriteLine(
        operator.Operate(
            double.Parse(args[0]), 
            double.Parse(args[1])
        )
        .ToString()
    );

    // should return 30.0

}


Basically then your processing code doesn't give a damn what kind of operator it is, just so long as the OperatorBuilder returns the right one, it should call its Operate method with the operands in the post-fix sequence and get an answer. This way you are delegating three distinct responsibilities to different classes:


  • OperatorBuilder - responsible for identifying tokens and returning the correct type of operator
  • MultiplyOperator, AdditionOperator etc. - responsible for performing an operation on some operands
  • Your processing code - the code you already have is doing the scanning part, which is separate (IMHO) from the semantic understanding of the text you are reading


I'm sure you can figure out how to expand this to your needs. This is just one idea off the top of my head, it's almost certainly not the best way to go about it, but I believe it should at least do the trick.

PS Just proof-reading this... if you're lucky that example might even legitimately compile... ?
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 
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 

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.