Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace QuadraticEquationSolver
- {
- class Program
- {
- static void Main(string[] args)
- {
- while (true)
- {
- double x1 = 0;
- double x2 = 0;
- double delta = 0;
- int a = 0;
- int b = 0;
- int c = 0;
- char[] aBC = new char[1000];
- 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");
- string equation = Console.ReadLine();
- for (int i = 0; i < equation.Length; i++)
- {
- if (equation[i] == 'x' && i == 0 || (equation[i] == '-' && equation[i + 1] == 'x' && i == 0))
- {
- aBC[i] = '1';
- a = 0;
- break;
- }
- if (equation[i] == 'x' && i != 0)
- {
- a = i - 1;
- break;
- }
- aBC[i] = equation[i];
- }
- if (a != 0)
- for (int i = a + 5; i < equation.Length; i++)
- {
- if (equation[i] == 'x')
- {
- b = i - 1;
- break;
- }
- aBC[i] = equation[i];
- }
- else
- for (int i = a + 4; i < equation.Length; i++)
- {
- if (equation[i] == 'x')
- {
- b = i - 1;
- break;
- }
- aBC[i] = equation[i];
- }
- for (int i = b + 3; i < equation.Length; i++)
- {
- if (equation[i] == '=')
- {
- c = i - 1;
- break;
- }
- else if (equation[equation.Length - 1] != '=' && i == equation.Length - 1)
- c = equation.Length;
- aBC[i] = equation[i];
- }
- string strA = "";
- string strB = "";
- string strC = "";
- for (int i = 0; i <= c; i++)
- {
- if (i <= a)
- strA += aBC[i];
- else if (i <= b && i >= a + 5 && a != 0)
- strB += aBC[i];
- else if (i <= b && i >= a + 4 && a == 0)
- strB += aBC[i];
- else if (i >= b + 3)
- strC += aBC[i];
- }
- int aNew = Convert.ToInt32(strA);
- if (equation[0] == '-' && equation[1] == 'x')
- aNew = -1;
- int bNew = Convert.ToInt32(strB);
- if (equation[a + 4] == '-' || equation[a + 3] == '-')
- bNew = -bNew;
- int cNew = Convert.ToInt32(strC);
- if (equation[b + 2] == '-' || equation[b + 1] == '-')
- cNew = -cNew;
- Console.WriteLine("a=" + aNew + "\nb=" + bNew + "\nc=" + cNew);
- quadraticEquation(aNew, bNew, cNew, ref x1, ref x2, ref delta);
- if (x1 == x2)
- {
- if (x1 != delta)
- Console.WriteLine("\nx = " + x1);
- if (x1 == delta)
- Console.WriteLine("No solution over the reals.");
- }
- else
- Console.WriteLine("\nx1 = " + x1 + "\nx2 = " + x2);
- }
- }
- static void quadraticEquation(double aNew, double bNew, double cNew, ref double x1, ref double x2, ref double delta)
- {
- delta = Math.Pow(bNew, 2) - 4 * aNew * cNew;
- if (delta >= 0)
- {
- x1 = (-bNew + Math.Sqrt(delta)) / (2 * aNew);
- x2 = (-bNew - Math.Sqrt(delta)) / (2 * aNew);
- }
- else
- x1 = x2 = delta;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement