Advertisement
XfreeBG

Untitled

Oct 18th, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1.  double a = 0;
  2.         double b = 0;
  3.         double c = 0;
  4.         string input = Console.ReadLine();
  5.         List<string> inputs = input.Split(' ').ToList();
  6.         List<string> orderedInput = new List<string>();
  7.         if (inputs[0].Contains("x^2"))
  8.         {
  9.             if(inputs[0][0] == '-')
  10.             {
  11.                 orderedInput.Add("-");
  12.                 inputs[0] = inputs[0].Replace("-", "");
  13.             }
  14.             else
  15.             {
  16.                 orderedInput.Add("+");
  17.             }
  18.             orderedInput.Add(inputs[0]);
  19.         }
  20.         else if (inputs[2].Contains("x^2"))
  21.         {
  22.             orderedInput.Add(inputs[1]);
  23.             orderedInput.Add(inputs[2]);
  24.         }
  25.         else if (inputs[4].Contains("x^2"))
  26.         {
  27.             orderedInput.Add(inputs[3]);
  28.             orderedInput.Add(inputs[4]);
  29.         }
  30.  
  31.  
  32.         if (inputs[0].Contains("x") && !inputs[0].Contains("x^2"))
  33.         {
  34.             if (inputs[0][0] == '-')
  35.             {
  36.                 orderedInput.Add("-");
  37.                 inputs[0] = inputs[0].Replace("-", "");
  38.             }
  39.             else
  40.             {
  41.                 orderedInput.Add("+");
  42.             }
  43.             orderedInput.Add(inputs[0]);
  44.         }
  45.         else if (inputs[2].Contains("x") && !inputs[2].Contains("x^2"))
  46.         {
  47.             orderedInput.Add(inputs[1]);
  48.             orderedInput.Add(inputs[2]);
  49.         }
  50.         else if (inputs[4].Contains("x") && inputs[4].Contains("x^2") == false)
  51.         {
  52.             orderedInput.Add(inputs[3]);
  53.             orderedInput.Add(inputs[4]);
  54.         }
  55.  
  56.  
  57.         if (int.TryParse(inputs[0], out int result0) == true)
  58.         {
  59.             if (inputs[0][0] == '-')
  60.             {
  61.                 orderedInput.Add("-");
  62.                 inputs[0] = inputs[0].Replace("-", "");
  63.             }
  64.             else
  65.             {
  66.                 orderedInput.Add("+");
  67.             }
  68.             orderedInput.Add(inputs[0]);
  69.         }
  70.         else if (int.TryParse(inputs[2], out int result2) == true)
  71.         {
  72.             orderedInput.Add(inputs[1]);
  73.             orderedInput.Add(inputs[2]);
  74.         }
  75.         else if(int.TryParse(inputs[4], out int result4) == true)
  76.         {
  77.             orderedInput.Add(inputs[3]);
  78.             orderedInput.Add(inputs[4]);
  79.  
  80.         }
  81.  
  82.         orderedInput[1] = orderedInput[1].Replace("x^2", "");
  83.         orderedInput[3] = orderedInput[3].Replace("x", "");
  84.  
  85.  
  86.         if (orderedInput[0] == "-")
  87.         {
  88.             a = double.Parse(orderedInput[0] + orderedInput[1]);
  89.         }
  90.         else if(orderedInput[0] == "+")
  91.         {
  92.             a = double.Parse(orderedInput[1]);
  93.         }
  94.  
  95.         if (orderedInput[2] == "-")
  96.         {
  97.             b = double.Parse(orderedInput[2] + orderedInput[3]);
  98.         }
  99.         else if (orderedInput[2] == "+")
  100.         {
  101.             b = double.Parse(orderedInput[3]);
  102.         }
  103.  
  104.         if (orderedInput[4] == "-")
  105.         {
  106.             c = double.Parse(orderedInput[4] + orderedInput[5]);
  107.         }
  108.         else if (orderedInput[4] == "+")
  109.         {
  110.             c = double.Parse(orderedInput[5]);
  111.         }
  112.  
  113.         double discriminant = Math.Pow(b, 2) + 4 * a * c;
  114.         if(discriminant < 0)
  115.         {
  116.             Console.WriteLine("No real roots");
  117.         }
  118.         else if(discriminant == 0)
  119.         {
  120.             double x = -b / (2 * a);
  121.             Console.WriteLine(x);
  122.         }
  123.         else if(discriminant > 0)
  124.         {
  125.             double x1 = (-b + Math.Sqrt(discriminant)) / 2 * a;
  126.             double x2 = (-b - Math.Sqrt(discriminant)) / 2 * a;
  127.             Console.WriteLine(x1);
  128.             Console.WriteLine(x2);
  129.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement