お役に立てれば
setp 1:
public abstract class Expression
{
public abstract decimal Evaluate();
public abstract string toString();
}
ステップ2:
public abstract class ValueNode:Expression
{
public int intvalue;
public decimal decvalue;
public abstract decimal TEvaluate();
public abstract string TtoString();
public override decimal Evaluate()
{
return TEvaluate();
}
public override string toString()
{
return TtoString();
}
}
ステップ2.1:
public abstract class OperationNode:Expression
{
public Expression left;
public Expression right;
public override decimal Evaluate()
{
return this.EEvaluate();
}
public override string toString()
{
return this.EtoString();
}
public abstract decimal EEvaluate();
public abstract string EtoString();
}
ステップ3:
public class UnaryMinus:OperationNode
{
public UnaryMinus(Expression Left)
{
this.left = Left;
}
public override decimal EEvaluate()
{
return -(this.left.Evaluate());
}
public override string EtoString()
{
return string.Format("(-({0}))",left.toString()); ;
}
}
ステップ4:
public class DecimalClass:ValueNode
{
public DecimalClass(decimal decimalValue)
{
this.decvalue = decimalValue;
}
public override decimal TEvaluate()
{
return this.decvalue;
}
public override string TtoString()
{
return this.decvalue.ToString();
}
}
ステップ5:
public class Integer : ValueNode
{
public Integer(int decimalValue)
{
this.intvalue = decimalValue;
}
public override decimal TEvaluate()
{
return this.intvalue;
}
public override string TtoString()
{
return this.intvalue.ToString();
}
}
ステップ6:
public class Addition:OperationNode
{
public Addition(Expression Left, Expression Right)
{
this.left = Left;
this.right = Right;
}
public override decimal EEvaluate()
{
return left.Evaluate()+ right.Evaluate();
}
public override string EtoString()
{
return string.Format("({0}+{1})",left.toString(),right.toString()); ;
}
}
ステップ7:
public class Multiplication : OperationNode
{
public Multiplication(Expression Left, Expression Right)
{
this.left = Left;
this.right = Right;
}
public override decimal EEvaluate()
{
return left.Evaluate()* right.Evaluate();
}
public override string EtoString()
{
return string.Format("({0}*{1})",left.toString(),right.toString()); ;
}
}
ステップ8:
public class Substraction:OperationNode
{
public Substraction(Expression Left, Expression Right)
{
this.left = Left;
this.right = Right;
}
public override decimal EEvaluate()
{
return left.Evaluate()- right.Evaluate();
}
public override string EtoString()
{
return string.Format("({0}-{1})",left.toString(),right.toString()); ;
}
}
ステップ9:
public class Division: OperationNode
{
public Division(Expression Left, Expression Right)
{
this.left = Left;
this.right = Right;
}
public override decimal EEvaluate()
{
return left.Evaluate()/ right.Evaluate();
}
public override string EtoString()
{
return string.Format("({0}/{1})",left.toString(),right.toString()); ;
}
}
ステップ10:
class Program
{
static void Main(string[] args)
{
callComposit();
Console.ReadKey();
}
private static void callComposit()
{
//Expression ((2.5+3.5)*(-(5)))
Multiplication multiplication = new Multiplication(new Addition(new DecimalClass(2.5m), new DecimalClass(3.5m)), new UnaryMinus(new Integer(5)));
Console.WriteLine(string.Format("\r\n Expression {0} resulted in {1}", multiplication.toString(), multiplication.Evaluate()));
//Expression (5/6)
Division division = new Division(new Integer(5), new Integer(6));
Console.WriteLine(string.Format("\r\n Expression {0} resulted in {1}", division.toString(), division.Evaluate()));
//Expression ((2.5-3.5)*(-(5)))
Multiplication multiplication2 = new Multiplication(new Substraction(new DecimalClass(2.5m), new DecimalClass(3.5m)), new UnaryMinus(new Integer(5)));
Console.WriteLine(string.Format("\r\n Expression {0} resulted in {1}", multiplication2.toString(), multiplication2.Evaluate()));
//Expression ((2.5/3.5)*(-(5)))
Multiplication multiplication3 = new Multiplication(new Division(new DecimalClass(2.5m), new DecimalClass(3.5m)), new UnaryMinus(new Integer(5)));
Console.WriteLine(string.Format("\r\n Expression {0} resulted in {1}", multiplication3.toString(), multiplication3.Evaluate()));
//Expression ((2.5/3.5)*(-(5))* 3.5)
Multiplication multiplication4 = new Multiplication(new Multiplication(new Division(new DecimalClass(2.5m), new DecimalClass(3.5m)), new UnaryMinus(new Integer(5))), new DecimalClass(3.5m));
Console.WriteLine(string.Format("\r\n Expression {0} resulted in {1}", multiplication4.toString(), multiplication4.Evaluate()));
//Expression ( 3.5*(2.5/3.5)*(-(5)))
Multiplication multiplication5 = new Multiplication(new Multiplication(new DecimalClass(3.5m), new Division(new DecimalClass(2.5m), new DecimalClass(3.5m))), new UnaryMinus(new Integer(5)));
Console.WriteLine(string.Format("\r\n Expression {0} resulted in {1}", multiplication5.toString(), multiplication5.Evaluate()));
//Expression ( 3.5*(2.5/3.5)+ 3.5 *(-(5)))
Multiplication multiplication6 = new Multiplication(new Addition(new Multiplication(new DecimalClass(3.5m), new Division(new DecimalClass(2.5m), new DecimalClass(3.5m))), new DecimalClass(3.5m)), new UnaryMinus(new Integer(5)));
Console.WriteLine(string.Format("\r\n Expression {0} resulted in {1}", multiplication6.toString(), multiplication6.Evaluate()));
}
}
この方法よりも優れた方法を実行するために何かを行う必要がある場合は、コメントしてください。ハッピーコーディング