Use the system;
Namespace computation
{
///& lt; Summary & gt
///A summary description of the CAL utility.
///auxiliary tool for reading formulas
///& lt; /summary & gt;
Public class CalUtility
{
System. Text . StringBuilder StrB
private int ICU RR = 0;
private int iCount = 0;
///& lt; Summary & gt
///construction method
///& lt; /summary & gt;
Public CalUtility (string calStr)
{
New system. Text.StringBuilder(calStr。 trim());
ICount = system. text . encoding . default . get bytecount(calStr。 trim());
}
///& lt; Summary & gt
///segment, automatically analyze the numerical value or operator.
///& lt; /summary & gt;
///& lt; returns & gt& lt/returns & gt;
Public string getItem ()
{
//It's over
if(iCurr==iCount)
Return'';
char ChTmp = StrB[IC urr];
bool b = is num(ChTmp);
If (! b)
{
ICU rr++;
Return to ChTmp. ToString();
}
String strTmp =
while(is num(ChTmp)= = b & amp; & ampiCurr & ltiCount)
{
ChTmp = StrB[IC urr];
if(IsNum(ChTmp)==b)
strTmp+= ChTmp;
other
Break;
ICU rr++;
}
Return to strTmp
}
///& lt; Summary & gt
///Is it a number?
///& lt; /summary & gt;
///& lt; Param name = "c"> content
///& lt; returns & gt& lt/returns & gt;
public bool IsNum(char c)
{
if((c & gt; = ' 0 ' & amp& ampc & lt='9')|| c== ',')
{
Return true
}
other
{
Returns false
}
}
///& lt; Summary & gt
///Is it a number?
///& lt; /summary & gt;
///& lt; Param name = "c"> content
///& lt; returns & gt& lt/returns & gt;
Public bool IsNum (string c)
{
if(c.Equals(" ")
Returns false
if((c[0]& gt; = ' 0 ' & amp& ampc[0]& lt; ='9')|| c[0]== ',')
{
Return true
}
other
{
Returns false
}
}
///& lt; Summary & gt
///Compare the priorities of str 1 and str2, where TRUE means that str 1 is higher than str2 and false means that str 1 is lower than str2.
///& lt; /summary & gt;
///& lt; Param name="str 1 "> operator 1
///& lt; Param name = "str2"> operator 2
///& lt; returns & gt& lt/returns & gt;
Common boolean comparison (string str 1, string str2)
{
return getPriority(str 1)>= get priority(str 2);
}
///& lt; Summary & gt
///Gets the priority of the calculation symbol.
///& lt; /summary & gt;
///& lt; Param name = "str"> operator
///& lt; returns & gt& lt/returns & gt;
Public int getPriority(string string)
{
If (string. Equal to ("")
{
return- 1;
}
If (string. Equal to ("(")
{
Returns 0;
}
If (string. Is equal to ("+") || string. Equal to ("-")
{
Returns1;
}
If (string. Equal to ("*")||str. Equal to ("/")
{
return 2;
}
If (string. Equal to (")")
{
Returns 0;
}
Returns 0;
}
}
}
IOper.cs
Use the system;
Namespace computation
{
///& lt; Summary & gt
///a brief description of ioper.
///Operator interface
///& lt; /summary & gt;
Common interface connector
{
///& lt; Summary & gt
///operator calculation interface calculation method
///& lt; /summary & gt;
///& lt; Param name="o 1 "> parameter 1 < /param >
///& lt; Param name = "o2"> parameter 2
///& lt; returns & gt& lt/returns & gt;
Object Oper (object o 1, object O2);
}
}
Opers.cs
Use the system;
Namespace computation
{
///& lt; Summary & gt
///A brief description of the operation.
///Interface implements various operators, addition, subtraction, multiplication and division.
///& lt; /summary & gt;
Public class operad:IOper
{
Common operation d ()
{
//
// TODO: Add constructor logic here.
//
}
# Regional Operator Member
Common object Oper (object o 1, object o2)
{
Decimal d 1 = decimal. Analysis (o 1. ToString());
Decimal d2 = decimal. Analysis (o2. ToString());
Returns d1+d2;
}
# End area
}
Public class OperDec:IOper
{
Public OperDec ()
{
//
// TODO: Add constructor logic here.
//
}
# Regional Operator Member
Common object Oper (object o 1, object o2)
{
Decimal d 1 = decimal. Analysis (o 1. ToString());
Decimal d2 = decimal. Analysis (o2. ToString());
Returns d1-d2;
}
# End area
}
Common class operation: IOper
{
Public operation ()
{
//
// TODO: Add constructor logic here.
//
}
# Regional Operator Member
Common object Oper (object o 1, object o2)
{
Decimal d 1 = decimal. Analysis (o 1. ToString());
Decimal d2 = decimal. Analysis (o2. ToString());
Returns d1* d2;
}
# End area
}
Public class OperDiv:IOper
{
public OperDiv()
{
//
// TODO: Add constructor logic here.
//
}
# Regional Operator Member
Common object Oper (object o 1, object o2)
{
Decimal d 1 = decimal. Analysis (o 1. ToString());
Decimal d2 = decimal. Analysis (o2. ToString());
Returns d1/d2;
}
# End area
}
}
OperFactory.cs
Use the system;
Namespace computation
{
///& lt; Summary & gt
///A brief description of Operfactory.
///Operator Interface Factory
///& lt; /summary & gt;
Public class operation factory
{
Public operation factory ()
{
}
Public IOper CreateOper (string operation)
{
If (Oper. Equal to ("+")
{
IOper p = new OperAdd();
Return p;
}
If (Oper. Equal to ("-")
{
io per p = new oper dec();
Return p;
}
If (Oper. Equal to ("*"))
{
IOper p = new oper ride();
Return p;
}
If (Oper. Equal to ("/")
{
IOper p = new OperDiv();
Return p;
}
Returns null
}
}
}
Calculate.cs
Use the system;
Use the system. Collection;
Namespace computation
{
///& lt; Summary & gt
///A summary description of the calculation.
///Calculation implementation main class
///& lt; /summary & gt;
Common class computing
{
///& lt; Summary & gt
///Arithmetic symbol stack
///& lt; /summary & gt;
Private array list HList
///& lt; Summary & gt
///value stack
///& lt; /summary & gt;
Public array list Vlist
///& lt; Summary & gt
///reading and calculating tools
///& lt; /summary & gt;
Private CalUtility cu
///& lt; Summary & gt
///operator factory
///& lt; /summary & gt;
Private factory;
///& lt; Summary & gt
///construction method
///& lt; /summary & gt;
///& lt; Param name = "str"> equation