DAY 23
0
Software Development

## Day 23 方法名稱與參數

``````public class ReverseExpression
{
private readonly Dictionary<string, Func<double, double, double>> _operatorDict = new Dictionary<string, Func<double, double, double>>()
{
{"+", (operator2, operator1) => operator1 + operator2 },
{"-", (operator2, operator1) => operator1 - operator2 },
{"*", (operator2, operator1) => operator1 * operator2 },
{"/", (operator2, operator1) => operator1 / operator2 },
};

public double Evaluate(string expression)
{
var operands = new Stack<double>();
foreach (var term in expression.Split(' '))
{
if (IsValidOperator(term))
{
operands.Push(Calculate(term, operands.Pop(), operands.Pop()));
}
else
{
operands.Push(double.Parse(term));
}
}

return operands.First();
}

private double Calculate(string @operator, double operand1, double operand2)
{
return _operatorDict[@operator](operand1, operand2);
}

private bool IsValidOperator(string term)
{
return _operatorDict.ContainsKey(term);
}
}
``````

``````public class BinaryExpression
{

private readonly Dictionary<string, Func<double, double, double>> _operatorDict = ...

public BinaryExpression(string term)
{
_term = term;
}

public double Calculate(double operand1, double operand2)
{
return _operatorDict[_term](operand1, operand2);
}

public bool IsValidOperator()
{
return _operatorDict.ContainsKey(_term);
}
}

public class ReverseExpression
{
public double Evaluate(string expression)
{
var operands = new Stack<double>();
foreach (var term in expression.Split(' '))
{
var binaryExpression = new BinaryExpression(term);
if (binaryExpression.IsValidOperator())
{
operands.Push(binaryExpression.Calculate(operands.Pop(), operands.Pop()));
}
else
{
operands.Push(double.Parse(term));
}
}

return operands.First();
}
}
``````

``````binaryExpression.Calculate(operands.Pop(), operands.Pop())
``````

``````binaryExpression.Calculate(@operator, operands.Pop(), operands.Pop())
``````

``````public class BinaryExpression
{
private readonly Dictionary<string, Func<double, double, double>> _operatorDict = new Dictionary<string, Func<double, double, double>>()
{
{"+", (operator2, operator1) => operator1 + operator2 },
{"-", (operator2, operator1) => operator1 - operator2 },
{"*", (operator2, operator1) => operator1 * operator2 },
{"/", (operator2, operator1) => operator1 / operator2 },
};

public double Calculate(string @operator, double operand1, double operand2)
{
return _operatorDict[@operator](operand1, operand2);
}

public bool IsValidOperator(string term)
{
return _operatorDict.ContainsKey(term);
}
}
``````

``````public class ReverseExpression
{
private readonly Dictionary<string, Func<double, double, double>> _operatorDict = new Dictionary<string, Func<double, double, double>>()
{
{"+", (operator2, operator1) => operator1 + operator2 },
{"-", (operator2, operator1) => operator1 - operator2 },
{"*", (operator2, operator1) => operator1 * operator2 },
{"/", (operator2, operator1) => operator1 / operator2 },
};

public double Evaluate(string expression)
{
var operands = new Stack<double>();
foreach (var term in expression.Split(' '))
{
if (IsValidOperator(term))
{
operands.Push(Calculate(term, operands.Pop(), operands.Pop()));
}
else
{
operands.Push(double.Parse(term));
}
}

return operands.First();
}

private double Calculate(string @operator, double operand1, double operand2)
{
return _operatorDict[@operator](operand1, operand2);
}

private bool IsValidOperator(string term)
{
return _operatorDict.ContainsKey(term);
}
}
``````

``````public class BinaryExpression
{

private readonly Dictionary<string, Func<double, double, double>> _operatorDict = ...

public BinaryExpression(string term)
{
_term = term;
}

public double Calculate(double operand1, double operand2)
{
return _operatorDict[_term](operand1, operand2);
}

public bool IsValidOperator()
{
return _operatorDict.ContainsKey(_term);
}
}

public class ReverseExpression
{
public double Evaluate(string expression)
{
var operands = new Stack<double>();
foreach (var term in expression.Split(' '))
{
var binaryExpression = new BinaryExpression(term);
if (binaryExpression.IsValidOperator())
{
operands.Push(binaryExpression.Calculate(operands.Pop(), operands.Pop()));
}
else
{
operands.Push(double.Parse(term));
}
}

return operands.First();
}
}
``````

``````binaryExpression.Calculate(operands.Pop(), operands.Pop())
``````

``````binaryExpression.Calculate(@operator, operands.Pop(), operands.Pop())
``````

``````public class BinaryExpression
{
private readonly Dictionary<string, Func<double, double, double>> _operatorDict = new Dictionary<string, Func<double, double, double>>()
{
{"+", (operator2, operator1) => operator1 + operator2 },
{"-", (operator2, operator1) => operator1 - operator2 },
{"*", (operator2, operator1) => operator1 * operator2 },
{"/", (operator2, operator1) => operator1 / operator2 },
};

public double Calculate(string @operator, double operand1, double operand2)
{
return _operatorDict[@operator](operand1, operand2);
}

public bool IsValidOperator(string term)
{
return _operatorDict.ContainsKey(term);
}
}
``````

``````public class ReverseExpression
{
private readonly Dictionary<string, Func<double, double, double>> _operatorDict = new Dictionary<string, Func<double, double, double>>()
{
{"+", (operator2, operator1) => operator1 + operator2 },
{"-", (operator2, operator1) => operator1 - operator2 },
{"*", (operator2, operator1) => operator1 * operator2 },
{"/", (operator2, operator1) => operator1 / operator2 },
};

public double Evaluate(string expression)
{
var operands = new Stack<double>();
foreach (var term in expression.Split(' '))
{
if (IsValidOperator(term))
{
operands.Push(Calculate(term, operands.Pop(), operands.Pop()));
}
else
{
operands.Push(double.Parse(term));
}
}

return operands.First();
}

private double Calculate(string @operator, double operand1, double operand2)
{
return _operatorDict[@operator](operand1, operand2);
}

private bool IsValidOperator(string term)
{
return _operatorDict.ContainsKey(term);
}
}
``````

``````public class BinaryExpression
{

private readonly Dictionary<string, Func<double, double, double>> _operatorDict = ...

public BinaryExpression(string term)
{
_term = term;
}

public double Calculate(double operand1, double operand2)
{
return _operatorDict[_term](operand1, operand2);
}

public bool IsValidOperator()
{
return _operatorDict.ContainsKey(_term);
}
}

public class ReverseExpression
{
public double Evaluate(string expression)
{
var operands = new Stack<double>();
foreach (var term in expression.Split(' '))
{
var binaryExpression = new BinaryExpression(term);
if (binaryExpression.IsValidOperator())
{
operands.Push(binaryExpression.Calculate(operands.Pop(), operands.Pop()));
}
else
{
operands.Push(double.Parse(term));
}
}

return operands.First();
}
}
``````

``````binaryExpression.Calculate(operands.Pop(), operands.Pop())
``````

``````binaryExpression.Calculate(@operator, operands.Pop(), operands.Pop())
``````

``````public class BinaryExpression
{
private readonly Dictionary<string, Func<double, double, double>> _operatorDict = new Dictionary<string, Func<double, double, double>>()
{
{"+", (operator2, operator1) => operator1 + operator2 },
{"-", (operator2, operator1) => operator1 - operator2 },
{"*", (operator2, operator1) => operator1 * operator2 },
{"/", (operator2, operator1) => operator1 / operator2 },
};

public double Calculate(string @operator, double operand1, double operand2)
{
return _operatorDict[@operator](operand1, operand2);
}

public bool IsValidOperator(string term)
{
return _operatorDict.ContainsKey(term);
}
}
``````