Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3. using Z.Expressions;
  4.  
  5. namespace MathShit
  6. {
  7. class Program
  8. {
  9. public static string FormatComplex(Complex num)
  10. {
  11. var res = "";
  12. var real = (int)Math.Round(num.Real);
  13. var im = (int)Math.Round(num.Imaginary);
  14. if (im == 0)
  15. {
  16. return real.ToString();
  17. }
  18. if (real == 0)
  19. {
  20. if (im == 1)
  21. {
  22. return "i";
  23. }
  24. else if (im == -1)
  25. {
  26. return "-i";
  27. }
  28. else
  29. {
  30. return im.ToString() + "i";
  31. }
  32. }
  33. res += real;
  34. if (im == 1)
  35. {
  36. return res + "+i";
  37. }
  38. else if (im == -1)
  39. {
  40. return res + "-i";
  41. }
  42. else if (im > 0)
  43. {
  44. return res + "+" + im.ToString() + "i";
  45. }
  46. else
  47. {
  48. return res + im.ToString() + "i";
  49. }
  50. }
  51. public static string SolveMath(string expression)
  52. {
  53. expression.Replace("/", "//");
  54. while (expression.IndexOf('i') != -1)
  55. {
  56. var ind = expression.IndexOf('i');
  57. if (expression.IndexOf('i') > 0 && char.IsDigit(expression[expression.IndexOf('i') - 1]))
  58. {
  59. expression = expression.Remove(ind, 1);
  60. expression = expression.Insert(ind, "*(new Complex(0.0, 1.0))");
  61. }
  62. else
  63. {
  64. expression = expression.Remove(ind, 1);
  65. expression = expression.Insert(ind, "(new Complex(0.0, 1.0))");
  66. }
  67. }
  68. Console.WriteLine(expression);
  69. var context = new EvalContext();
  70. var c = new Complex(0, 0);
  71. context.RegisterType(c.GetType());
  72.  
  73. var res = context.Execute(expression);
  74. if (expression.IndexOf('m') > 0)
  75. return (FormatComplex((Complex)res));
  76. return (res.ToString());
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement