Jump to content
  • Advertisement
  • entries
    743
  • comments
    1924
  • views
    584348

I love C# lots

Sign in to follow this  
Aardvajk

155 views

Really starting to feel like I'm getting to grips with C# now. I've just written a console based expression calculator that scans the input into tokens like numbers, math operators and brackets, then builds an expression tree from the input made up of classes inheriting from an abstract Node class, then uses recursion to calculate the value of the expression.

The interesting bit is the Expr class really:


using System;

class ExprException: Exception
{
public ExprException(string m): base(m) { }
}

abstract class Node
{
public Node(){ }

public abstract int Calc();
}

class LitNode: Node
{
public int Value;

public LitNode(int V){ Value=V; }

public override int Calc(){ return Value; }
}

class AddNode: Node
{
public Node Ln,Rn;

public AddNode(Node L,Node R){ Ln=L; Rn=R; }

public override int Calc()
{
return Ln.Calc()+Rn.Calc();
}
}

class SubNode: Node
{
public Node Ln,Rn;

public SubNode(Node L,Node R){ Ln=L; Rn=R; }

public override int Calc()
{
return Ln.Calc()-Rn.Calc();
}
}

class MulNode: Node
{
public Node Ln,Rn;

public MulNode(Node L,Node R){ Ln=L; Rn=R; }

public override int Calc()
{
return Ln.Calc()*Rn.Calc();
}
}

class DivNode: Node
{
public Node Ln,Rn;

public DivNode(Node L,Node R){ Ln=L; Rn=R; }

public override int Calc()
{
return Ln.Calc()/Rn.Calc();
}
}

class NegNode: Node
{
public Node Nn;

public NegNode(Node N){ Nn=N; }

public override int Calc()
{
return -Nn.Calc();
}
}

class Expr
{
public static Node Norm(Parser P,bool Get)
{
Node N=Term(P,Get);

while(true)
{
switch(P.Type)
{
case Parser.Types.Add: N=new AddNode(N,Term(P,true)); break;
case Parser.Types.Sub: N=new SubNode(N,Term(P,true)); break;

default: return N;
}
}
}

public static Node Term(Parser P,bool Get)
{
Node N=Prim(P,Get);

while(true)
{
switch(P.Type)
{
case Parser.Types.Mul: N=new MulNode(N,Prim(P,true)); break;
case Parser.Types.Div: N=new DivNode(N,Prim(P,true)); break;

default: return N;
}
}
}

public static Node Prim(Parser P,bool Get)
{
Parser.Types T=P.Next(Get); Node N=null;

switch(T)
{
case Parser.Types.Number: N=new LitNode(Convert.ToInt32(P.Text)); P.Next(true); return N;

case Parser.Types.Lp: N=Norm(P,true);
if(P.Type!=Parser.Types.Rp) throw new ExprException("right bracket expected - "+P.Text);
P.Next(true); return N;

case Parser.Types.Sub: return new NegNode(Prim(P,true));

default: throw new ExprException("primary expected - "+P.Text);
}
}
}



Obviously this is something I've done a lot in C++ but I found it really easy to translate the idea into C#. It really is a very beautiful language.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!