Guest User

Untitled

a guest
Aug 10th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. Pars Text or script into C# code
  2. if(Vessel.Weight>200)
  3. {
  4. return Cargo.Weight*Cargo.Tariff*2
  5. }
  6. else
  7. {
  8. Cargo.Weight*Cargo.Tariff
  9. }
  10.  
  11. var cost = executeFormula("CalculateTariff",new {Cargo= GetCargo(), Vessel=GetVessel()});
  12.  
  13. result=(Vessel_Weight>200)
  14. ?(Cargo_Weight*Cargo_Tariff*2)
  15. :(Cargo_Weight*Cargo_Tariff)
  16.  
  17. Dictionary<string, dynamic> compute = new Dictionary<string, dynamic>();
  18. compute.Add("Vessel_Weight", 123);
  19. compute.Add("Cargo_Weight", 24);
  20. compute.Add("Cargo_Tariff", 9);
  21.  
  22. string rule = "result=(Vessel_Weight>200)
  23. ?(Cargo_Weight*Cargo_Tariff*2)
  24. :(Cargo_Weight*Cargo_Tariff)";
  25.  
  26. string process = rule.Replace(" ", "");
  27. foreach (Match level1 in Regex.Matches(process, "\([^\)]+\)"))
  28. {
  29. string parenthesis = level1.Value;
  30. string keepit = parenthesis;
  31. Console.Write("{0} -> ", parenthesis);
  32. // replace all named variable with values from the dictionary
  33. foreach (Match level2 in Regex.Matches(parenthesis, "[a-zA-z0-9_]+"))
  34. {
  35. string variable = level2.Value;
  36. if (Regex.IsMatch(variable, "[a-zA-z_]+"))
  37. {
  38. if (!compute.ContainsKey(variable))
  39. throw new Exception("Variable not found");
  40. parenthesis = parenthesis.Replace(variable, compute[variable].ToString());
  41. }
  42. }
  43. parenthesis = parenthesis.Replace("(", "").Replace(")", "");
  44. Console.Write("{0} -> ", parenthesis);
  45. // do the math
  46. List<double> d = new List<double>();
  47. foreach (Match level3 in Regex.Matches(parenthesis, "[0-9]+(\.[0-9]+)?"))
  48. {
  49. d.Add(double.Parse(level3.Value));
  50. parenthesis = Regex.Replace(parenthesis, level3.Value, "");
  51. }
  52. double start = d[0];
  53. for (var i = 1; i < d.Count; i++)
  54. {
  55. switch (parenthesis[i - 1])
  56. {
  57. case '+':
  58. start += d[i];
  59. break;
  60. case '-':
  61. start -= d[i];
  62. break;
  63. case '*':
  64. start *= d[i];
  65. break;
  66. case '/':
  67. start /= d[i];
  68. break;
  69. case '=':
  70. start = (start == d[i]) ? 0 : 1;
  71. break;
  72. case '>':
  73. start = (start > d[i]) ? 0 : 1;
  74. break;
  75. case '<':
  76. start = (start < d[i]) ? 0 : 1;
  77. break;
  78. }
  79. }
  80. parenthesis = start.ToString();
  81. Console.WriteLine(parenthesis);
  82. rule = rule.Replace(keepit, parenthesis);
  83. }
  84. Console.WriteLine(rule);
  85. // peek a value in case of a condition
  86. string condition = "[0-9]+(\.[0-9]+)?\?[0-9]+(\.[0-9]+)?:[0-9]+(\.[0-9]+)?";
  87. if (Regex.IsMatch(rule, condition))
  88. {
  89. MatchCollection m = Regex.Matches(rule, "[0-9]+(\.[0-9]+)?");
  90. int check = int.Parse(m[0].Value) + 1;
  91. rule = rule.Replace(Regex.Match(rule, condition).Value, m[check].Value);
  92. }
  93. Console.WriteLine(rule);
  94. // final touch
  95. int equal = rule.IndexOf("=");
  96. compute.Add(rule.Substring(0, equal - 1), double.Parse(rule.Substring(equal + 1)));
Add Comment
Please, Sign In to add comment