Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Text.RegularExpressions;
  7. using System.Diagnostics;
  8.  
  9. namespace testconsole
  10. {
  11. class Program
  12. {
  13. //0 1 2
  14. //?+?-?*?+?*?*?+?+?-?*?+?*?*?+?+?-?*?+?*?*?+?+?-?*?+?*?*?
  15. static void Main(string[] args)
  16. {
  17. #region INPUT
  18. //int first = Convert.ToInt32(Console.ReadLine());
  19. int length = 7;
  20. //int length = 455;
  21.  
  22. //string expression = Console.ReadLine();
  23. string expression = "?+?-?*?+?*?*?";
  24. //string expression = "?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?+?-?*?+?*?*?";
  25.  
  26. //string[] temp = Console.ReadLine().Split(' ');
  27. //string[] temp = "1 2 4".Split(' ');
  28. string[] temp = "0 0 7".Split(' ');
  29. List<int> counts = new List<int>();
  30. foreach (var item in temp)
  31. {
  32. counts.Add(Convert.ToInt32(item));
  33. }
  34. #endregion
  35.  
  36. #region CALCULATE
  37. expression = expression.Replace("+?*", "+MP0*");
  38. expression = expression.Replace("-?*", "-MN0*");
  39. expression = expression.Replace("*?+", "*MP0+");
  40. expression = expression.Replace("*?-", "*MN0-");
  41.  
  42. expression = expression.Replace("-?-", "-NN-");
  43. expression = expression.Replace("-?+", "-NN+");
  44. expression = expression.Replace("+?-", "+PP-");
  45. expression = expression.Replace("+?+", "-PP+");
  46. expression = expression.Replace("-?", "-NN");
  47. expression = expression.Replace("?-", "PP-");
  48. expression = expression.Replace("+?", "+PP");
  49. expression = expression.Replace("?+", "PP+");
  50.  
  51. if (expression.Substring(0, 2) == "?*")
  52. expression = "MP0*" + expression.Substring(2);
  53.  
  54. if (expression.Substring(expression.Length - 2) == "*?")
  55. expression = expression.Substring(0, expression.Length - 2) + "*MP0";
  56.  
  57. int mpCount = 0;
  58. int mnCount = 0;
  59.  
  60. while (expression.Contains("MP" + mpCount + "*?*"))
  61. {
  62. expression = expression.Replace("MP" + mpCount + "*?*", "MP" + mpCount + "*MP" + (mpCount + 1) + "*");
  63. mpCount++;
  64. }
  65.  
  66. int tempmnCount = 0;
  67. while (expression.Contains("MN" + mnCount + "*?*"))
  68. {
  69. expression = expression.Replace("MN" + mnCount + "*?*", "MN" + mnCount + "*MN" + (mnCount + 1) + "*");
  70. if(expression.Contains("MN" + mnCount + "*MP0"))
  71. {
  72. expression = expression.Replace("MN" + mnCount + "*MP0", "MN" + mnCount + "*MN" + mnCount + 1);
  73. tempmnCount = mnCount + 1;
  74. }
  75. mnCount++;
  76. }
  77.  
  78. if (expression.Contains("MN0*MP0"))
  79. {
  80. expression = expression.Replace("MN0*MP0", "MN0*MN0");
  81. }
  82.  
  83. if (tempmnCount > mnCount) mnCount = tempmnCount;
  84.  
  85. int countsIndex = 0;
  86.  
  87. while (counts[countsIndex] <= 0 && countsIndex < 2)
  88. countsIndex++;
  89.  
  90. while (counts[countsIndex] > 0 && mnCount >= 0)
  91. {
  92. if (expression.Contains("MN" + mnCount))
  93. {
  94. expression = ReplaceFirst(expression, "MN" + mnCount, countsIndex.ToString());
  95. counts[countsIndex]--;
  96.  
  97. }
  98. else
  99. mnCount--;
  100.  
  101. while (counts[countsIndex] <= 0 && countsIndex < 2)
  102. countsIndex++;
  103. }
  104.  
  105. while (counts[countsIndex] <= 0 && countsIndex < 2)
  106. countsIndex++;
  107.  
  108. while (counts[countsIndex] > 0 && expression.Contains("NN"))
  109. {
  110. expression = ReplaceFirst(expression, "NN", countsIndex.ToString());
  111. counts[countsIndex]--;
  112.  
  113. while (counts[countsIndex] <= 0 && countsIndex < 2)
  114. countsIndex++;
  115. }
  116.  
  117. while (counts[countsIndex] <= 0 && countsIndex < 2)
  118. countsIndex++;
  119.  
  120. while (counts[countsIndex] > 0 && expression.Contains("PP"))
  121. {
  122. expression = ReplaceFirst(expression, "PP", countsIndex.ToString());
  123. counts[countsIndex]--;
  124.  
  125. while (counts[countsIndex] <= 0 && countsIndex < 2)
  126. countsIndex++;
  127. }
  128.  
  129. while (counts[countsIndex] <= 0 && countsIndex < 2)
  130. countsIndex++;
  131.  
  132. while (counts[countsIndex] > 0 && mpCount >= 0)
  133. {
  134. if (expression.Contains("MP" + mpCount))
  135. {
  136. expression = ReplaceFirst(expression, "MP" + mpCount, countsIndex.ToString());
  137. counts[countsIndex]--;
  138.  
  139. }
  140. else
  141. mpCount--;
  142.  
  143. while (counts[countsIndex] <= 0 && countsIndex < 2)
  144. countsIndex++;
  145. }
  146.  
  147.  
  148. #endregion
  149.  
  150.  
  151. //Stopwatch stopwatch1 = new Stopwatch();
  152. //Stopwatch stopwatchX = new Stopwatch();
  153.  
  154. //stopwatch1.Start();
  155. //for (var i = 0; i<200; i++)
  156. Console.WriteLine(Evaluate(expression));
  157.  
  158. //stopwatch1.Stop();
  159. //stopwatchX.Start();
  160. //for(var j = 0; j<200; j++)
  161. Console.WriteLine(EvaluateX(expression));
  162.  
  163. //stopwatchX.Stop();
  164.  
  165. //Console.WriteLine(stopwatch1.Elapsed);
  166. //Console.WriteLine(stopwatchX.Elapsed);
  167. Console.ReadLine();
  168. }
  169.  
  170. public static long EvaluateX(string expression)
  171. {
  172. long result = 0;
  173.  
  174. //while((index = expression.IndexOf('*')) != -1)
  175. //{
  176. // string calc = expression.Substring(index-1, 3);
  177. // expression = expression.Replace(calc, (Convert.ToInt16(calc[0]) * Convert.ToInt16(calc[2])).ToString());
  178. //}
  179.  
  180. //while(((index = expression.IndexOf('-')) != -1) || ((index = expression.IndexOf('+')) != -1))
  181. //{
  182. // //thiswillnotwork :/
  183. //}
  184.  
  185. List<string> numbers = expression.Split(new char[] { '*', '+', '-' }).ToList();
  186. var temp = expression.Replace("0", "");
  187. temp = temp.Replace("1", "");
  188. List<char> signs = temp.Replace("2", "").ToList();
  189.  
  190. while(signs.Contains('*'))
  191. {
  192. int index = signs.IndexOf('*');
  193. numbers[index] = (Convert.ToInt64(numbers[index]) * Convert.ToInt64(numbers[index + 1])).ToString();
  194. numbers.RemoveAt(index + 1);
  195. signs.RemoveAt(index);
  196. }
  197.  
  198. while(signs.Contains('-'))
  199. {
  200. int index = signs.IndexOf('-');
  201. result -= Convert.ToInt64(numbers[index + 1]);
  202. numbers.RemoveAt(index + 1);
  203. signs.RemoveAt(index);
  204. }
  205.  
  206. for(var i = 0; i < numbers.Count; i++)
  207. {
  208. result += Convert.ToInt64(numbers[i]);
  209. }
  210.  
  211. //result = 0;
  212. return result;
  213. }
  214.  
  215. public static string ReplaceFirst(string text, string search, string replace)
  216. {
  217. int pos = text.IndexOf(search);
  218. if (pos < 0)
  219. {
  220. return text;
  221. }
  222. return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
  223. }
  224.  
  225. public static double Evaluate(string expression)
  226. {
  227. DataTable table = new DataTable();
  228. table.Columns.Add("expression", typeof(string), expression);
  229. DataRow row = table.NewRow();
  230. table.Rows.Add(row);
  231. return double.Parse((string)row["expression"]);
  232. }
  233. }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement