柚子快报激活码778899分享:c#四则运算

http://yzkb.51969.com/

来源:https://blog.csdn.net/slwsss/article/details/70432277

 

namespace ConsoleApp12

{

internal class Program

{

static void Main(string[] args)

{

var str = "1+2+3+4+5*0";

Console.WriteLine(Compute(str));

Console.ReadLine();

}

static decimal Compute(string expression)

{

int i = 0;

var r = _Compute(expression, ref i);

if (i != expression.Length) throw new ArgumentException("╮(︶︿︶)╭");

return r;

}

static decimal _Compute(string expression, ref int i)

{

int j;

char c;

decimal? r = null;

decimal t;

int f = 1, f3;

bool f2;

g1:

while (i < expression.Length)

{

c = expression[i];

if (c >= '0' && c <= '9')

{

for (j = i + 1; j < expression.Length; j++)

{

c = expression[j];

if ((c < '0' || c > '9') && c != '.') break;

}

t = decimal.Parse(expression.Substring(i, j - i));

i = j;

goto g2;

}

else if (c == '+' || c == '-')

{

f3 = c == '+' ? 1 : -1;

i++;

while (i < expression.Length)

{

c = expression[i];

if ((c >= '0' && c <= '9') || c == '.')

{

for (j = i + 1; j < expression.Length; j++)

{

c = expression[j];

if ((c < '0' || c > '9') && c != '.') break;

}

t = f3 * decimal.Parse(expression.Substring(i, j - i));

i = j;

goto g2;

}

else if (c == '(')

{

i++;

if (i >= expression.Length) goto exception;

t = _Compute(expression, ref i) * f3;

if (i >= expression.Length) goto exception;

i++;

goto g2;

}

else if (char.IsWhiteSpace(c))

{

i++;

continue;

}

else goto exception;

}

goto exception;

}

else if (c == '(')

{

i++;

if (i >= expression.Length) goto exception;

t = _Compute(expression, ref i);

if (i >= expression.Length) goto exception;

i++;

goto g2;

}

else if (char.IsWhiteSpace(c))

{

i++;

continue;

}

else goto exception;

}

exception:

throw new ArgumentException("╮(︶︿︶)╭");

g2:

while (i < expression.Length)

{

c = expression[i];

if (c == '+' || c == '-')

{

r = r.HasValue ? (r.Value + f * t) : (f * t);

f = c == '+' ? 1 : -1;

i++;

goto g1;

}

else if (c == '*' || c == '/')

{

f2 = c == '*';

i++;

while (i < expression.Length)

{

c = expression[i];

if (c >= '0' && c <= '9')

{

for (j = i + 1; j < expression.Length; j++)

{

c = expression[j];

if ((c < '0' || c > '9') && c != '.') break;

}

if (f2) t *= decimal.Parse(expression.Substring(i, j - i));

else t /= decimal.Parse(expression.Substring(i, j - i));

i = j;

goto g2;

}

else if (c == '+' || c == '-')

{

f3 = c == '+' ? 1 : -1;

i++;

while (i < expression.Length)

{

c = expression[i];

if ((c >= '0' && c <= '9') || c == '.')

{

for (j = i + 1; j < expression.Length; j++)

{

c = expression[j];

if ((c < '0' || c > '9') && c != '.') break;

}

if (f2) t *= (f3 * decimal.Parse(expression.Substring(i, j - i)));

else t /= (f3 * decimal.Parse(expression.Substring(i, j - i)));

i = j;

goto g2;

}

else if (c == '(')

{

i++;

if (i >= expression.Length) goto exception;

if (f2) t *= (_Compute(expression, ref i) * f3);

else t /= (_Compute(expression, ref i) * f3);

if (i >= expression.Length) goto exception;

i++;

goto g2;

}

else if (char.IsWhiteSpace(c))

{

i++;

continue;

}

else goto exception;

}

goto exception;

}

else if (c == '(')

{

i++;

if (i >= expression.Length) goto exception;

if (f2) t *= _Compute(expression, ref i);

else t /= _Compute(expression, ref i);

if (i >= expression.Length) goto exception;

i++;

goto g2;

}

else if (char.IsWhiteSpace(c))

{

i++;

continue;

}

goto exception;

}

goto exception;

}

else if (c == ')') goto g2_end;

else if (char.IsWhiteSpace(c))

{

i++;

continue;

}

goto exception;

}

g2_end:

if (r.HasValue) return r.Value + f * t;

return f * t;

}

}

}

  

柚子快报激活码778899分享:c#四则运算

http://yzkb.51969.com/

好文阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。