Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Numerics;
- using Z.Expressions;
- namespace MathShit
- {
- class Program
- {
- public static string FormatComplex(Complex num)
- {
- var res = "";
- var real = (int)Math.Round(num.Real);
- var im = (int)Math.Round(num.Imaginary);
- if (im == 0)
- {
- return real.ToString();
- }
- if (real == 0)
- {
- if (im == 1)
- {
- return "i";
- }
- else if (im == -1)
- {
- return "-i";
- }
- else
- {
- return im.ToString() + "i";
- }
- }
- res += real;
- if (im == 1)
- {
- return res + "+i";
- }
- else if (im == -1)
- {
- return res + "-i";
- }
- else if (im > 0)
- {
- return res + "+" + im.ToString() + "i";
- }
- else
- {
- return res + im.ToString() + "i";
- }
- }
- public static string SolveMath(string expression)
- {
- expression.Replace("/", "//");
- while (expression.IndexOf('i') != -1)
- {
- var ind = expression.IndexOf('i');
- if (expression.IndexOf('i') > 0 && char.IsDigit(expression[expression.IndexOf('i') - 1]))
- {
- expression = expression.Remove(ind, 1);
- expression = expression.Insert(ind, "*(new Complex(0.0, 1.0))");
- }
- else
- {
- expression = expression.Remove(ind, 1);
- expression = expression.Insert(ind, "(new Complex(0.0, 1.0))");
- }
- }
- Console.WriteLine(expression);
- var context = new EvalContext();
- var c = new Complex(0, 0);
- context.RegisterType(c.GetType());
- var res = context.Execute(expression);
- if (expression.IndexOf('m') > 0)
- return (FormatComplex((Complex)res));
- return (res.ToString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement