Advertisement
XfreeBG

Untitled

Feb 21st, 2023
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace ConsoleApp1
  6. {
  7. internal class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. while (true)
  12. {
  13. bool isExceptionTriggered = false;
  14. try
  15. {
  16. double a = 0;
  17. double b = 0;
  18. double c = 0;
  19. Console.Write("Enter a quadratic equation: ");
  20. string input = Console.ReadLine();
  21. List<string> inputs = input.Split(' ').ToList();
  22. List<string> orderedInput = new List<string>();
  23. if (inputs[0].Contains("x^2"))
  24. {
  25. if (inputs[0][0] == '-')
  26. {
  27. orderedInput.Add("-");
  28. inputs[0] = inputs[0].Replace("-", "");
  29. }
  30. else
  31. {
  32. orderedInput.Add("+");
  33. }
  34. orderedInput.Add(inputs[0]);
  35. }
  36. else if (inputs[2].Contains("x^2"))
  37. {
  38. orderedInput.Add(inputs[1]);
  39. orderedInput.Add(inputs[2]);
  40. }
  41. else if (inputs[4].Contains("x^2"))
  42. {
  43. orderedInput.Add(inputs[3]);
  44. orderedInput.Add(inputs[4]);
  45. }
  46.  
  47.  
  48. if (inputs[0].Contains("x") && !inputs[0].Contains("x^2"))
  49. {
  50. if (inputs[0][0] == '-')
  51. {
  52. orderedInput.Add("-");
  53. inputs[0] = inputs[0].Replace("-", "");
  54. }
  55. else
  56. {
  57. orderedInput.Add("+");
  58. }
  59. orderedInput.Add(inputs[0]);
  60. }
  61. else if (inputs[2].Contains("x") && !inputs[2].Contains("x^2"))
  62. {
  63. orderedInput.Add(inputs[1]);
  64. orderedInput.Add(inputs[2]);
  65. }
  66. else if (inputs[4].Contains("x") && inputs[4].Contains("x^2") == false)
  67. {
  68. orderedInput.Add(inputs[3]);
  69. orderedInput.Add(inputs[4]);
  70. }
  71.  
  72.  
  73. if (int.TryParse(inputs[0], out int result0) == true)
  74. {
  75. if (inputs[0][0] == '-')
  76. {
  77. orderedInput.Add("-");
  78. inputs[0] = inputs[0].Replace("-", "");
  79. }
  80. else
  81. {
  82. orderedInput.Add("+");
  83. }
  84. orderedInput.Add(inputs[0]);
  85. }
  86. else if (int.TryParse(inputs[2], out int result2) == true)
  87. {
  88. orderedInput.Add(inputs[1]);
  89. orderedInput.Add(inputs[2]);
  90. }
  91. else if (int.TryParse(inputs[4], out int result4) == true)
  92. {
  93. orderedInput.Add(inputs[3]);
  94. orderedInput.Add(inputs[4]);
  95.  
  96. }
  97. if (orderedInput[1][0] == 'x')
  98. {
  99. orderedInput[1] = orderedInput[1].Replace("x^2", "1");
  100. }
  101. else
  102. {
  103. orderedInput[1] = orderedInput[1].Replace("x^2", "");
  104. }
  105. if (orderedInput[3][0] == 'x')
  106. {
  107. orderedInput[3] = orderedInput[3].Replace("x", "1");
  108. }
  109. else
  110. {
  111. orderedInput[3] = orderedInput[3].Replace("x", "");
  112. }
  113.  
  114.  
  115.  
  116. if (orderedInput[0] == "-")
  117. {
  118. a = double.Parse(orderedInput[0] + orderedInput[1]);
  119. }
  120. else if (orderedInput[0] == "+")
  121. {
  122. a = double.Parse(orderedInput[1]);
  123. }
  124.  
  125. if (orderedInput[2] == "-")
  126. {
  127. b = double.Parse(orderedInput[2] + orderedInput[3]);
  128. }
  129. else if (orderedInput[2] == "+")
  130. {
  131. b = double.Parse(orderedInput[3]);
  132. }
  133.  
  134. if (orderedInput[4] == "-")
  135. {
  136. c = double.Parse(orderedInput[4] + orderedInput[5]);
  137. }
  138. else if (orderedInput[4] == "+")
  139. {
  140. c = double.Parse(orderedInput[5]);
  141. }
  142.  
  143. double discriminant = Math.Pow(b, 2) - 4 * a * c;
  144. if (discriminant < 0)
  145. {
  146. Console.ForegroundColor = ConsoleColor.Green;
  147. Console.WriteLine("No real roots!");
  148. Console.ForegroundColor = ConsoleColor.White;
  149. }
  150. else if (discriminant == 0)
  151. {
  152. double x = -b / (2 * a);
  153. Console.Write($"x1 = ");
  154. Console.ForegroundColor = ConsoleColor.Green;
  155. Console.Write(x);
  156. Console.ForegroundColor = ConsoleColor.White;
  157. }
  158. else if (discriminant > 0)
  159. {
  160. double x1 = (-b + Math.Sqrt(discriminant)) / 2 * a;
  161. double x2 = (-b - Math.Sqrt(discriminant)) / 2 * a;
  162. Console.Write($"x1 = ");
  163. Console.ForegroundColor = ConsoleColor.Green;
  164. Console.WriteLine(x1);
  165. Console.ForegroundColor = ConsoleColor.White;
  166. Console.Write($"x2 = ");
  167. Console.ForegroundColor = ConsoleColor.Green;
  168. Console.Write(x2);
  169. Console.ForegroundColor = ConsoleColor.White;
  170. }
  171. }
  172. catch
  173. {
  174. Console.ForegroundColor = ConsoleColor.Red;
  175. Console.WriteLine("Not a quadratic equation!");
  176. Console.ForegroundColor = ConsoleColor.White;
  177. isExceptionTriggered = true;
  178. }
  179. if(isExceptionTriggered == false)
  180. {
  181. break;
  182. }
  183. }
  184.  
  185.  
  186.  
  187. }
  188. }
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement