栈实现字符串表达式计算

最近频繁解决计算方面的问题,其中就有实现字符串表达式计算返回结果值需求,通过使用栈实现,需要定义运算符号优先级,其它就不细说,代码如下:

csStack.cs:

using System;

namespace PYHB
{
///

1<summary>   
2/// 栈堆设定。 
3
4/// 日期:2005-05-17   
5/// </summary>

public class clsStack
{
private long Top; //栈最大序号
private int MaxSize; // MaxSize 栈的容量
private string[] Element;
public clsStack()
{
//
// TODO: 在此处添加构造函数逻辑
//
Top=-1;
}
///

1<summary>   
2/// 设定栈大最大容量   
3/// </summary>

///

1<param name="Size"/>

public void Initialize(int Size)
{
MaxSize=Size;
Element=new string[Size];
}
///

1<summary>   
2/// 入栈   
3/// </summary>

///

1<param name="strItem"/>

public void Push(string strItem)
{
if(!IsFull())
{
Top=Top+1;
Element[Top] = strItem;
}

}
///

1<summary>   
2/// 出栈   
3/// </summary>

///

1<returns></returns>

public string Pop()
{
string strRtn=" ";
if(!IsEmptly())
{
strRtn = Element[Top];
Top=Top-1;
}
return strRtn;
}
public string GetTop()
{
string strRtn=" ";
if(!IsEmptly())
{
strRtn = Element[Top];
}
return strRtn;
}
public bool IsFull()
{
bool IsFull=Top==(MaxSize-1)?true:false;
return IsFull;
}
public void MakeEmptly()
{
Top = -1;
}
public bool IsEmptly()
{
bool IsEmptly=Top==-1?true:false;
return IsEmptly;
}

}
}

calculate.cs

using System;
using System.Text;
using System.Windows.Forms;

namespace PYHB
{
///

1<summary>   
2/// 字符串表达式计算实现,返回计算结果字符数组   
3  
4/// 日期:2005-05-17   
5/// </summary>

public class Calculate
{
private clsStack S=new clsStack();
public Calculate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

///

1<summary>   
2/// 根据数字表达式字符串数组,返回计算结果字符数组   
3/// </summary>

///

1<param name="strSoure"/>

strSour 中缀表达式字符串,头部没有“#”,尾部需要加上“#”
///

1<returns>计算结果</returns>

public string[] Run(string[] strSoure)
{
if(strSoure==null)
{
return null;
}
string[] dRtn=new string[strSoure.Length];
for(int k=0;k

  1<strsoure.length;k++) ');="" <summary="" atemp="strRPN.Trim().Split('" atemp;="" catch{}="" dooperate(atemp[i]);="" drtn;="" drtn[k]="S.Pop();" else="" for(int="" i="0;i&lt;ATemp.Length;i++)" if(sysfun.isnumber(atemp[i]))="" return="" s.push(atemp[i]);="" string="" string[]="" strrpn="GetRPN(strSoure[k]);" strrpn;="" try="" {="" }="">   
  2/// Run 返回后缀表达式   
  3/// strSour 中缀表达式字符串,头部没有“#”,尾部需要加上“#”   
  4/// String 后缀表达式字符串,头尾都没有“#”   
  5  
  6///    
  7/// <param name="strSource"/>   
  8/// <returns></returns>   
  9private string GetRPN(string strSource)   
 10{   
 11string[] ATemp;   
 12string strRPN="",Y;   
 13ATemp=strSource.Trim().Split(' ');   
 14S.Initialize(ATemp.Length);   
 15S.MakeEmptly();   
 16S.Push("#");   
 17try   
 18{   
 19for(int k=0;k<atemp.length;k++) (getisp(y)="" )="" );="" +=" " +atemp[k];="" +y;="" do="" else="" if="" if(atemp[k]=")" if(sysfun.isnumber(atemp[k]))="" if(y!="(" strrpn="" while(y.trim()!="(" y="S.Pop();" {="" }="" 字符="" 数字=""> GetICP(ATemp[k]))   
 20strRPN += " "+Y;   
 21}   
 22while(GetISP(Y) &gt; GetICP(ATemp[k]));   
 23S.Push(Y);   
 24S.Push(ATemp[k]);   
 25}   
 26}   
 27}   
 28do   
 29{   
 30Y=S.Pop();   
 31if(Y!="#")   
 32strRPN+=" "+Y;   
 33}   
 34while(Y!="#");   
 35}   
 36catch{}   
 37return strRPN;   
 38} 
 39
 40#region 运算符优先级定义   
 41private enum isp   
 42{   
 43s35 = 0,   
 44s40 = 1,   
 45s94 = 7,   
 46s42 = 5,   
 47s47 = 5,   
 48s37 = 5,   
 49s43 = 3,   
 50s45 = 3,   
 51s41 = 8   
 52}   
 53private enum icp   
 54{   
 55s35 = 0,   
 56s40 = 8,   
 57s94 = 6,   
 58s42 = 4,   
 59s47 = 4,   
 60s37 = 4,   
 61s43 = 2,   
 62s45 = 2,   
 63s41 = 1   
 64}   
 65private int GetISP(string a1)   
 66{   
 67Encoding ascii =Encoding.ASCII;   
 68byte[] a=ascii.GetBytes(a1);   
 69switch(Convert.ToInt32(a[0]))   
 70{   
 71case 35:   
 72return (int)isp.s35; 
 73
 74case 40:   
 75return (int)isp.s40;   
 76  
 77case 94:   
 78return (int)isp.s94;   
 79  
 80case 42:   
 81return (int)isp.s42;   
 82  
 83case 47:   
 84return (int)isp.s47;   
 85  
 86case 37:   
 87return (int)isp.s37;   
 88  
 89case 43:   
 90return (int)isp.s43;   
 91  
 92case 45:   
 93return (int)isp.s45;   
 94  
 95case 41:   
 96return (int)isp.s41;   
 97default:   
 98return (int)isp.s35;   
 99}   
100  
101}   
102private int GetICP(string a1)   
103{   
104Encoding ascii =Encoding.ASCII;   
105byte[] a=ascii.GetBytes(a1);   
106switch(Convert.ToInt32(a[0]))   
107{   
108case 35:   
109return (int)icp.s35;   
110  
111case 40:   
112return (int)icp.s40;   
113  
114case 94:   
115return (int)isp.s94;   
116  
117case 42:   
118return (int)icp.s42;   
119  
120case 47:   
121return (int)icp.s47;   
122  
123case 37:   
124return (int)icp.s37;   
125  
126case 43:   
127return (int)icp.s43;   
128  
129case 45:   
130return (int)icp.s45;   
131  
132case 41:   
133return (int)icp.s41;   
134default:   
135return (int)icp.s35;   
136  
137}   
138  
139}   
140#endregion 
141
142/// <summary>   
143/// 判断是否存在左右数字,并且复制   
144/// </summary>   
145/// <param name="dLeft"/>左数值   
146/// <param name="dRight"/>右的数值   
147/// <returns>是否成功</returns>   
148private bool GetTwoItem(ref decimal dLeft,ref decimal dRight)   
149{   
150bool bRtn=true;   
151try   
152{   
153if(S.IsEmptly())   
154return false;   
155else   
156dRight = Convert.ToDecimal(S.Pop());   
157if(S.IsEmptly())   
158return false;   
159else   
160dLeft = Convert.ToDecimal(S.Pop());   
161}   
162catch   
163{   
164}   
165return bRtn;   
166}   
167/// <summary>   
168/// 根据运算符号计算,并且把计算结果以字符形式填充入栈   
169/// </summary>   
170/// <param name="op"/>   
171private void DoOperate(string op)   
172{   
173decimal NumLeft=0,NumRight=0;   
174bool r;   
175r=GetTwoItem(ref NumLeft,ref NumRight);   
176if(r)   
177{   
178switch(op.Trim())   
179{   
180case "+":   
181S.Push((NumLeft+NumRight).ToString());   
182break;   
183case "-":   
184S.Push((NumLeft-NumRight).ToString());   
185break;   
186case "*":   
187S.Push((NumLeft*NumRight).ToString());   
188break;   
189case "/":   
190if(NumRight==0)   
191S.Push("0");   
192else   
193S.Push((NumLeft/NumRight).ToString());   
194break; 
195
196}   
197}   
198} 
199
200} 
201
202}</atemp.length;k++)></strsoure.length;k++)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus