Advertisement
AvengersAssemble

quadraticEquation Solver Code Needs Revesiting

Sep 9th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace QuadraticEquationSolver
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             while (true)
  13.             {
  14.                 double x1 = 0;
  15.                 double x2 = 0;
  16.                 double delta = 0;
  17.                 int a = 0;
  18.                 int b = 0;
  19.                 int c = 0;
  20.                 char[] aBC = new char[1000];
  21.                 Console.WriteLine("\nEquation must be entered in the following format: ax^2+bx+c=0. It is extremely\nimportant, because otherwise the program won't be able to run the neccesary\ncalculations. If b=0, type 'ax^2+0x+c', if c=0, type 'ax^2+bx+0'.\n");
  22.                 string equation = Console.ReadLine();
  23.                 for (int i = 0; i < equation.Length; i++)
  24.                 {
  25.                     if (equation[i] == 'x' && i == 0 || (equation[i] == '-' && equation[i + 1] == 'x' && i == 0))
  26.                     {
  27.                         aBC[i] = '1';
  28.                         a = 0;
  29.                         break;
  30.                     }
  31.                     if (equation[i] == 'x' && i != 0)
  32.                     {
  33.                         a = i - 1;
  34.                         break;
  35.                     }
  36.                     aBC[i] = equation[i];
  37.                 }
  38.                 if (a != 0)
  39.                     for (int i = a + 5; i < equation.Length; i++)
  40.                     {
  41.                         if (equation[i] == 'x')
  42.                         {
  43.                             b = i - 1;
  44.                             break;
  45.                         }
  46.                         aBC[i] = equation[i];
  47.                     }
  48.                 else
  49.                     for (int i = a + 4; i < equation.Length; i++)
  50.                     {
  51.                         if (equation[i] == 'x')
  52.                         {
  53.                             b = i - 1;
  54.                             break;
  55.                         }
  56.                         aBC[i] = equation[i];
  57.                     }
  58.                 for (int i = b + 3; i < equation.Length; i++)
  59.                 {
  60.                     if (equation[i] == '=')
  61.                     {
  62.                         c = i - 1;
  63.                         break;
  64.                     }
  65.                     else if (equation[equation.Length - 1] != '=' && i == equation.Length - 1)
  66.                         c = equation.Length;
  67.                     aBC[i] = equation[i];
  68.                 }
  69.                 string strA = "";
  70.                 string strB = "";
  71.                 string strC = "";
  72.                 for (int i = 0; i <= c; i++)
  73.                 {
  74.                     if (i <= a)
  75.                         strA += aBC[i];
  76.                     else if (i <= b && i >= a + 5 && a != 0)
  77.                         strB += aBC[i];
  78.                     else if (i <= b && i >= a + 4 && a == 0)
  79.                         strB += aBC[i];
  80.                     else if (i >= b + 3)
  81.                         strC += aBC[i];
  82.                 }
  83.                 int aNew = Convert.ToInt32(strA);
  84.                 if (equation[0] == '-' && equation[1] == 'x')
  85.                     aNew = -1;
  86.                 int bNew = Convert.ToInt32(strB);
  87.                 if (equation[a + 4] == '-' || equation[a + 3] == '-')
  88.                     bNew = -bNew;
  89.                 int cNew = Convert.ToInt32(strC);
  90.                 if (equation[b + 2] == '-' || equation[b + 1] == '-')
  91.                     cNew = -cNew;
  92.                 Console.WriteLine("a=" + aNew + "\nb=" + bNew + "\nc=" + cNew);
  93.                 quadraticEquation(aNew, bNew, cNew, ref x1, ref x2, ref delta);
  94.                 if (x1 == x2)
  95.                 {
  96.                     if (x1 != delta)
  97.                         Console.WriteLine("\nx = " + x1);
  98.                     if (x1 == delta)
  99.                         Console.WriteLine("No solution over the reals.");
  100.                 }
  101.                 else
  102.                     Console.WriteLine("\nx1 = " + x1 + "\nx2 = " + x2);
  103.             }
  104.         }
  105.         static void quadraticEquation(double aNew, double bNew, double cNew, ref double x1, ref double x2, ref double delta)
  106.         {
  107.             delta = Math.Pow(bNew, 2) - 4 * aNew * cNew;
  108.             if (delta >= 0)
  109.             {
  110.                 x1 = (-bNew + Math.Sqrt(delta)) / (2 * aNew);
  111.                 x2 = (-bNew - Math.Sqrt(delta)) / (2 * aNew);
  112.             }
  113.             else
  114.                 x1 = x2 = delta;
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement