Introduction
C# is an interesting language,in my opinion,beginners always need some samples for learning,I hope my article can help them.As your know,the function of windows calculator is so strong even standard version,it can process expression like this:"2+==*.3-4+5===" or "2.5*3-.6+4.4=+==-=" and so on,I think make a calculator without any bug is hard,so a good design can help you to decrease your bug,so I will try to do this.
Using the code
CalculatorOperation - a class to provide basic function of calculator.
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace NewCalculator
{
public enum Operator { NO_OP, ADD, SUB, MUL, DIV, EQUAL, SQRT, BACKWARDS,PERCENT }
internal class CalculatorOperation
{
public delegate decimal MathematicOperating(decimal opt1, decimal opt2);
public delegate decimal OtherOperating(decimal opt1);
public static Hashtable hashTable;
static CalculatorOperation()
{
hashTable = new Hashtable();
hashTable.Add("+", Operator.ADD);
hashTable.Add("-", Operator.SUB);
hashTable.Add("*", Operator.MUL);
hashTable.Add("/", Operator.DIV);
hashTable.Add("=", Operator.EQUAL);
}
private static decimal Add(decimal op1, decimal op2)
{
return decimal.Add(op1, op2);
}
private static decimal Sub(decimal op1, decimal op2)
{
return decimal.Subtract(op1, op2);
}
private static decimal Mul(decimal op1, decimal op2)
{
return decimal.Multiply(op1, op2);
}
private static decimal Div(decimal op1, decimal op2)
{
return decimal.Divide(op1, op2);
}
private static decimal Sqrt(decimal op1)
{
return (decimal)Math.Sqrt((double)op1);
}
private static decimal Backwords(decimal op1)
{
return 1 / op1;
}
private static decimal Percent(decimal op1)
{
return op1 / 100;
}
public static MathematicOperating Mathematic(Operator opt)
{
MathematicOperating MO = null;
if (opt == Operator.ADD)
{
MO = new MathematicOperating(CalculatorOperation.Add);
return MO;
}
if (opt == Operator.SUB)
{
MO = new MathematicOperating(CalculatorOperation.Sub);
return MO;
}
if (opt == Operator.MUL)
{
MO = new MathematicOperating(CalculatorOperation.Mul);
return MO;
}
if (opt == Operator.DIV)
{
MO = new MathematicOperating(CalculatorOperation.Div);
return MO;
}
return null;
}
public static OtherOperating OtherMathematic(Operator opt)
{
OtherOperating MO;
if (opt == Operator.SQRT)
{
MO = new OtherOperating(CalculatorOperation.Sqrt);
return MO;
}
if (opt == Operator.BACKWARDS)
{
MO = new OtherOperating(CalculatorOperation.Backwords);
return MO;
}
if (opt == Operator.PERCENT)
{
MO = new OtherOperating(CalculatorOperation.Percent);
return MO;
}
return null;
}
}
}
CalculatorController - a class to simulate a calculator.
Blocks of code should be set as style "Formatted"
like this:
using System;
using System.Collections.Generic;
using System.Text;
namespace NewCalculator
{
public delegate void Change(string str);
public class CalculatorController:IController
{
#region private field
private decimal mStore = 0;
private decimal mResult = 0;
private string mDisplay = string.Empty;
History
08.01.2008 - Initial release