C#实现计算器,支持括号运算
来源:互联网 发布:国外网络摄像头直播 编辑:程序博客网 时间:2024/06/04 22:05
有些面试题要实现一个计算器。自己尝试了一下。若有问题请留言说明。我好及时纠正。
public class CalcHelper { public double CalcBrackets(ref string str) { char[] array = str.ToArray(); string tex = str; int count = 1; int endIndex = 0; for(int i=1;i<array.Length;i++) { if (array[i] == '(') count++; if (array[i] == ')') count--; if (count == 0) { endIndex = i;break; } } tex = tex.Substring(1,endIndex-1); str = str.Substring(endIndex+1); var result = StrCalc(0, ref tex); return result; } public double GetANumber(ref string str) { char first = str.First(); if (first == '(') { double result = 0; result = CalcBrackets(ref str); return result; } char[] array = str.ToArray(); int end = 0; int point = -1; for (int i = 0; i < array.Length; i++) { if (array[i] < 0x30 && array[i] > 0x39 && array[i] != 0x2E) { end = i; break; } if (array[i] == 0x2E) { point = i; } } double Value = 0; Value += array[0]-0x30; if(point>=0) { for (int i = 1; i < point; i++) Value = Value * 10 + (array[i]-0x30); double except = 10.0; for(int i=point+1;i<end;i++) { Value += (array[i]-0x30) / except; except *= 10; } } else { for (int i = 1; i < end; i++) Value = Value * 10 + (array[i]-0x30); } str = str.Substring(++end); return Value; } public double StrCalc(double A,ref string str) { if (str.Length == 0) return A; char first = str.First(); double result = 0; if (first >= 0x30 && first <= 0x39) { result = GetANumber(ref str); result = StrCalc(result, ref str); } else if (first == '+') { str = str.Substring(1); result = A + StrCalc(0, ref str); } else if (first == '-') { str = str.Substring(1); result = A - StrCalc(0, ref str); } else if (first=='(') { result= CalcBrackets(ref str); result = StrCalc(result, ref str); } else if (first == '*') { str = str.Substring(1); result = A * GetANumber(ref str); } else if (first == '/') { str = str.Substring(1); result = A / GetANumber(ref str); } return result; } }
调用方法:
CalcHelper calc = new CalcHelper(); string cmd = "1+1+1+1+3*3+1+2-((3*4)+5)*(6-7)+9/(3-1)"; var result = calc.StrCalc(0, ref cmd); while(cmd.Length!=0) result=calc.StrCalc(result, ref cmd);
阅读全文