⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 polandcompute.cs

📁 自动计算公式
💻 CS
字号:
using System;
using System.Collections;

/*   傻瓜计算机算法
 *   版本 1.0
 *   日期 2006-2-11
 *   程序 +斌同学(又名:最爱 BO。BO)
 *   QQ  83731490
 *   MSN  chenjiabin@hotmail.com
 *	 
 *   由于时间关系,没有进行全面的测试。(我预感还有好多BUG)
 *   所以,大家如果发现有bug,请通过以上联系方法告诉我。
 *   谢谢大家。
 * */

namespace Poland
{
	/// <summary>
	/// PolandCompute 的摘要说明。
	/// </summary>
	public class PolandCompute
	{
		//普通表达式
		private string infix;
		//Poland表达式
		private string[] postfix;

		public PolandCompute()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}

		public string setInfix
		{
			get
			{
				return infix;
			}
			set
			{
				if(Convert(value))
					infix = value;
			}
		}

		public string[] getPostfix
		{
			get
			{
				if(postfix!=null)		
					if(postfix.Length> 0)
						return postfix;
				
				return null;
			}
		}

		/// <summary>
		/// 用换算好的Poland表达式进行计算
		/// </summary>
		/// <returns>结果</returns>
		public double getResult()
		{
			if(postfix==null)	
				return 0.0;

			if(postfix.Length==0)
				return 0.0;

			double result;
			Stack save;
			save = new Stack();
			result = 0;		

			for(int i=0; i<postfix.Length; i++)
			{
				if(postfix[i].Equals("+"))
				{
					save.Push(Convert2Number(save.Pop()) + Convert2Number(save.Pop()));
				}
				if(postfix[i].Equals("-"))
				{
					//小心逻辑错误
					//save.Push(Convert2Number(save.Pop()) - Convert2Number(save.Pop()));
					save.Push( -Convert2Number(save.Pop()) + Convert2Number(save.Pop()));
				}
				if(postfix[i].Equals("*"))
				{
					save.Push(Convert2Number(save.Pop()) * Convert2Number(save.Pop()));
				}
				if(postfix[i].Equals("/"))
				{
					//小心逻辑错误
					//save.Push(Convert2Number(save.Pop()) / Convert2Number(save.Pop()));				
					double t = Convert2Number(save.Pop());
					save.Push(Convert2Number(save.Pop()) / t);
				}
				if(isNumber(postfix[i]))
				{
					save.Push(postfix[i]);
				}
			}
			
			result = (double)save.Pop();

			return result;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		private double Convert2Number(object obj)
		{
			try
			{
				return double.Parse(obj.ToString());
			}
			catch
			{
				return 0.0;
			}
		}

		private double Convert2Number(string str)
		{
			try
			{
				return double.Parse(str);
			}
			catch
			{
				return 0.0;
			}
		}

		/// <summary>
		/// 将普通表达式转换为Poland表达式
		/// </summary>
		/// <returns></returns>
		private bool Convert2Postfix()
		{
			Stack a,b,c;
			string fix;
			int i;
			fix = string.Empty;
			a = new Stack();
			b = new Stack();
			c = new Stack();

			for(i=0; i<postfix.Length; i++)
			{
				if(postfix[i].Equals("("))
				{
					a.Push(postfix[i]);
				}
				else if(isNumber(postfix[i]))
				{
					b.Push(postfix[i]);
				}
				else if(postfix[i].Equals("+") || postfix[i].Equals("-") || postfix[i].Equals("*") || postfix[i].Equals("/"))
				{
					c.Push(postfix[i]);
				}
				else if(postfix[i].Equals(")"))
				{
					if(a.Count > 0)
						a.Pop();
					fix = (string)b.Pop() + "|";
					fix = (string)b.Pop() + "|" + fix + c.Pop() + "|";
					b.Push(fix);
					fix = string.Empty;
				}
				else
				{
					return false;
				}
			}
			
			//fix bug
			//for(i=0; i<c.count; i++)   //error
			while(c.Count > 0)
			{
				fix = (string)b.Pop() + "|";
				fix = (string)b.Pop() + "|" + fix + c.Pop() + "|";
				b.Push(fix);
				fix = string.Empty;			
			}
			
			if(b.Count > 1)
				return false;

			fix = (string)b.Pop();
			fix = fix.Replace("||","|");
			fix = fix.Substring(0, fix.Length-1);
			postfix = fix.Split("|".ToCharArray());
			
			for(i=0; i<postfix.Length; i++)
			{
				Console.Write(postfix[i] + " ");
			}
			Console.WriteLine();

			return true;
		}

		private bool isNumber(string k)
		{
			try
			{
				// fix bug
				//int.Parse(k);  如果k="0.9" 这就会出现异常   -_-  
				double.Parse(k);
				return true;
			}
			catch
			{
				return false;
			}
		}

		private bool isNumber(char k)
		{
			try
			{
				double.Parse(k.ToString());
				return true;
			}
			catch
			{
				return false;
			}
		}

		private bool meetAdd(string str)
		{
			if(!str.Substring(0,1).Equals("(") || !str.Substring(str.Length-1, 1).Equals(")"))
			{
				return true;
			}

			Stack a;
			char[] k;
			k = str.ToCharArray();
			a = new Stack();
			
			for(int i=0; i<k.Length; i++)
			{
				if(k[i].Equals('('))
					a.Push(k[i]);

				if(k[i].Equals(')'))
				{
					a.Pop();
					if(a.Count==0 && k.Length==i+1)
						return true;
				}
			}

			return false;
		}


		/// <summary>
		/// 规范化普通表达式
		/// </summary>
		/// <param name="m_fix"></param>
		/// <returns></returns>
		private bool Convert(string m_fix)
		{
			int count,i,j;
			int lf,rf;
			string[] g;

			//表达式必须有(),转换为postFix才不会出错.
			string fix;
			if(meetAdd(m_fix))
				fix = "(" + m_fix + ")";
			else
				fix = m_fix;

			//去掉表达式中所有空格
			fix = fix.Replace(" ","");

			#region - 号问题
			//fix bug 判断负号与减号
			int s,e;
			for(i=0; i<fix.Length; i++)
			{
				if(fix.Substring(i,1).Equals("-"))
				{
					//如果 -号 前是数字和),它就是减号,不是数字,代表的就是负号.
					if(!isNumber(fix.Substring(i-1,1)) && !fix.Substring(i-1,1).Equals(")"))
					{
						s = i;
						i++;
						while(isNumber(fix.Substring(i,1)) || fix.Substring(i,1).Equals("."))
						{
							i++;
							if(i > fix.Length)
								break;
						}
						i--;
						e = i;

						//   防止 1+((-1))  这种双()情况
						if(fix.Substring(s-1,1).Equals("(") && fix.Substring(e+1,1).Equals(")"))
						{
							fix = fix.Insert(s, "0");
						}
						else
						{
							fix = fix.Insert(s, "(0");
							fix = fix.Insert(e+3, ")");
						}
					}
				}
			}
			//
			//fix bug - 号带来的问题2   5-5-5 -> 5+(0-5)+(0-5) 这样转换出来的Poland才能计算正确
			for(i=0; i<fix.Length; i++)
			{
				if(!fix.Substring(i,1).Equals("-"))
					continue;

				if(fix.Substring(i-1,1).Equals("0"))
					continue;

				if(!isNumber(fix.Substring(i+1,1)))
					continue;

				s = i;
				i++;
				while(isNumber(fix.Substring(i,1)) || fix.Substring(i,1).Equals("."))
				{
					i++;
					if(i > fix.Length)
						break;
				}
				i--;
				e = i;
				
				fix = fix.Insert(s, "+(0");
				fix = fix.Insert(e+4, ")");
			}
			//
			#endregion

			char[] temp = fix.ToCharArray();

			lf = rf = count = 0;
			
			for(i=0; i<temp.Length; i++)
			{
				if(isNumber(temp[i]))
				{
					//temp[i].Equals('.')   对小数的支持
					while(isNumber(temp[i]) || temp[i].Equals('.'))
					{
						i++;	
						if(i >= temp.Length)
							break;
					}
					i--;
					count++;
				}
				else
				{
					count++;
					if(temp[i].Equals('('))
						lf++;
					if(temp[i].Equals(')'))
						rf++;
				}
			}

			//括号是否匹对
			if(lf != rf)
				return false;
			
			//重新组织
			g = new string[count];
			for(i=0,j=0; i<count; i++,j++)
			{
				if(isNumber(temp[j]))
				{
					while(isNumber(temp[j]) || temp[j].Equals('.'))
					{
						g[i] += temp[j].ToString();
						j++;
						if(j>=temp.Length)
							break;
					}
					j--;
				}
				else
				{
					g[i] = temp[j].ToString();
				}
			}
			
			postfix = g;

			for(i=0; i<postfix.Length; i++)
				Console.WriteLine(postfix[i]);
			Console.WriteLine("\n");

			return Convert2Postfix();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -