Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Using a dictionary like in the @Mat'sMug solution @Mat'sMug solution is one way to do it.

Using a dictionary like in the @Mat'sMug solution is one way to do it.

Using a dictionary like in the @Mat'sMug solution is one way to do it.

added 361 characters in body
Source Link
t3chb0t
  • 44.6k
  • 9
  • 84
  • 190
interface IBinaryOperation
{
 string Description { get; }
 string Operator { get; }
 bool CanCalculate(string binaryOperator);
 double Calculate(double x, double y);
}
class AddOperation : IBinaryOperation
{
 public AddOperation()
 {
 Description = "Addition";
 Operator = "+";
 }
 public string Description { get; }
 public string Operator { get; }
 public bool CanCalculate(string binaryOperator) => binaryOperator == "+";Operator;
 public double Calculate(double x, double y) => x + y;
}
class Program
{
 static void Main(string[] args)
 {
 Console.WriteLinevar calc = new Calculator("Whatnew[]
 would you like to calculate?"); {
 var expression = Console.ReadLine new AddOperation();

 var});
 calc = new Calculator(new[] Console.WriteLine("What would you like to {calculate?");
 Console.WriteLine($"You can use: [ new{string.Join(", AddOperation", calc.Operations.Select(o => o.Operator))} ]");
 }var expression = Console.ReadLine();
 var result = calc.Calcualte(expression);
 if (result.HasValue)
 {
 Console.WriteLine();
 Console.WriteLine($"The result is: {result.Value}");
 }
 else
 {
 Console.WriteLine();
 Console.WriteLine($"This operation is not supported.");
 }
 }
}
interface IBinaryOperation
{
 bool CanCalculate(string binaryOperator);
 double Calculate(double x, double y);
}
class AddOperation : IBinaryOperation
{
 public bool CanCalculate(string binaryOperator) => binaryOperator == "+";
 public double Calculate(double x, double y) => x + y;
}
class Program
{
 static void Main(string[] args)
 {
 Console.WriteLine("What would you like to calculate?");
 var expression = Console.ReadLine();

 var calc = new Calculator(new[] {
 new AddOperation()
 });
 var result = calc.Calcualte(expression);
 if (result.HasValue)
 {
 Console.WriteLine();
 Console.WriteLine($"The result is: {result.Value}");
 }
 else
 {
 Console.WriteLine();
 Console.WriteLine($"This operation is not supported.");
 }
 }
}
interface IBinaryOperation
{
 string Description { get; }
 string Operator { get; }
 bool CanCalculate(string binaryOperator);
 double Calculate(double x, double y);
}
class AddOperation : IBinaryOperation
{
 public AddOperation()
 {
 Description = "Addition";
 Operator = "+";
 }
 public string Description { get; }
 public string Operator { get; }
 public bool CanCalculate(string binaryOperator) => binaryOperator == Operator;
 public double Calculate(double x, double y) => x + y;
}
class Program
{
 static void Main(string[] args)
 {
 var calc = new Calculator(new[]
  {
  new AddOperation()
 });
 Console.WriteLine("What would you like to calculate?");
 Console.WriteLine($"You can use: [ {string.Join(", ", calc.Operations.Select(o => o.Operator))} ]");
 var expression = Console.ReadLine();
 var result = calc.Calcualte(expression);
 if (result.HasValue)
 {
 Console.WriteLine();
 Console.WriteLine($"The result is: {result.Value}");
 }
 else
 {
 Console.WriteLine();
 Console.WriteLine($"This operation is not supported.");
 }
 }
}
Source Link
t3chb0t
  • 44.6k
  • 9
  • 84
  • 190

Using a dictionary like in the @Mat'sMug solution is one way to do it.

Another way is to make it modular and extendable with a few interfaces and classs.

You start with an interface for a binary operation:

interface IBinaryOperation
{
 bool CanCalculate(string binaryOperator);
 double Calculate(double x, double y);
}

From this interface you can derive any binary operation you want. I did only for addition:

class AddOperation : IBinaryOperation
{
 public bool CanCalculate(string binaryOperator) => binaryOperator == "+";
 public double Calculate(double x, double y) => x + y;
}

You enter the expression as a string i.e. 2.5 + 1.2, you parse it and store the result as a binary expression:

class BinaryExpression
{
 public BinaryExpression(double x, double y, string binaryOperator)
 {
 X = x;
 Y = y;
 BinaryOperator = binaryOperator;
 }
 public double X { get; }
 public double Y { get; }
 public string BinaryOperator { get; }
}

finally you write the calculator that parses the expression and calls the right operation to calculate the result:

class Calculator
{
 private readonly Regex _matcher = new Regex(
 @"(?<x>\d+([.]\d+)?)\s*(?<operator>.+?)\s*(?<y>\d+([.]\d+)?)", 
 RegexOptions.ExplicitCapture
 );
 public Calculator(IEnumerable<IBinaryOperation> operations)
 {
 Operations = operations;
 }
 public IEnumerable<IBinaryOperation> Operations { get; }
 public double? Calcualte(string expression)
 {
 var expr = Parse(expression);
 return Operations.FirstOrDefault(o => o.CanCalculate(expr.BinaryOperator))?.Calculate(expr.X, expr.Y);
 }
 private BinaryExpression Parse(string expression)
 {
 var match = _matcher.Match(expression);
 if (!match.Success)
 {
 throw new ArgumentException($"Could not parse expression: {expression}");
 }
 return new BinaryExpression(
 double.Parse(match.Groups["x"].Value, CultureInfo.InvariantCulture),
 double.Parse(match.Groups["y"].Value, CultureInfo.InvariantCulture),
 match.Groups["operator"].Value);
 }
}

This is a very simple example. The parser itself could be a module too.

And you use it like this:

class Program
{
 static void Main(string[] args)
 {
 Console.WriteLine("What would you like to calculate?");
 var expression = Console.ReadLine();
 var calc = new Calculator(new[] 
 {
 new AddOperation()
 });
 var result = calc.Calcualte(expression);
 if (result.HasValue)
 {
 Console.WriteLine();
 Console.WriteLine($"The result is: {result.Value}");
 }
 else
 {
 Console.WriteLine();
 Console.WriteLine($"This operation is not supported.");
 }
 }
}
lang-cs

AltStyle によって変換されたページ (->オリジナル) /