C#. Квадратное уравнение
Квадратное уравнение.
Не получается вывести мнимую часть.
Спасибо!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace kvadurav
{
class Program
{
static void Main(string[] args)
{
Console.Write("Введите коэффицент a = ");
float a = float.Parse(Console.ReadLine());
Console.Write("Введите коэффицент b = ");
float b = float.Parse(Console.ReadLine());
Console.Write("Введите коэффицент c = ");
float c = float.Parse(Console.ReadLine());
float D = b * b - 4 * a * c;
float x1, x2;
if (D > 0)
{
float sqrtD = (float)Math.Sqrt(D);
x1 = (-b - sqrtD) / (2 * a);
x2 = (-b + sqrtD) / (2 * a);
Console.WriteLine("Корни:");
Console.WriteLine("D = " + D.ToString());
Console.WriteLine("x1 = " + x1.ToString());
Console.WriteLine("x2 = " + x2.ToString());
}
if (D == 0)
{
float sqrtD = (float)Math.Sqrt(D);
x1 = x2 = (-b - sqrtD) / (2 * a);
Console.WriteLine("Один корень:");
Console.WriteLine("D = " + D.ToString());
Console.WriteLine("x1 = " + x1.ToString());
}
if (D < 0)
{
float sqrtD = (float)Math.Sqrt(D);
float im = sqrtD / (2 * a);
D = -D;
if (im < 0)
{
im = -im;
}
x1 = (-b / 2 * a);
x2 = (-b / 2 * a);
Console.WriteLine("Комплексные корни:");
Console.WriteLine("D = " + D.ToString());
Console.WriteLine("x1 = " + x1.ToString() + (" + ") + im.ToString() + ("i"));
Console.WriteLine("x2 = " + x2.ToString() + (" - ") + im.ToString() + ("i"));
}
Console.Read();
}
}
}
Квадратное уравнение.
Не получается вывести мнимую часть.
Спасибо!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace kvadurav
{
class Program
{
static void Main(string[] args)
{
Console.Write("Введите коэффицент a = ");
float a = float.Parse(Console.ReadLine());
Console.Write("Введите коэффицент b = ");
float b = float.Parse(Console.ReadLine());
Console.Write("Введите коэффицент c = ");
float c = float.Parse(Console.ReadLine());
float D = b * b - 4 * a * c;
float x1, x2;
if (D > 0)
{
float sqrtD = (float)Math.Sqrt(D);
x1 = (-b - sqrtD) / (2 * a);
x2 = (-b + sqrtD) / (2 * a);
Console.WriteLine("Корни:");
Console.WriteLine("D = " + D.ToString());
Console.WriteLine("x1 = " + x1.ToString());
Console.WriteLine("x2 = " + x2.ToString());
}
if (D == 0)
{
float sqrtD = (float)Math.Sqrt(D);
x1 = x2 = (-b - sqrtD) / (2 * a);
Console.WriteLine("Один корень:");
Console.WriteLine("D = " + D.ToString());
Console.WriteLine("x1 = " + x1.ToString());
}
if (D < 0)
{
float sqrtD = (float)Math.Sqrt(D);
float im = sqrtD / (2 * a);
D = -D;
if (im < 0)
{
im = -im;
}
x1 = (-b / 2 * a);
x2 = (-b / 2 * a);
Console.WriteLine("Комплексные корни:");
Console.WriteLine("D = " + D.ToString());
Console.WriteLine("x1 = " + x1.ToString() + (" + ") + im.ToString() + ("i"));
Console.WriteLine("x2 = " + x2.ToString() + (" - ") + im.ToString() + ("i"));
}
Console.Read();
}
}
}