Równania kwadratowe z postaci algebraiczne, zespolone, kanoniczna

0

Mam problem z programem obliczającym Równania kwadratowe, wszystko działa, tylko w liczbach zespolonych wychodzi wynik typu "2-(-2)", a potrzebuje aby od razu program wyświetlał "2+2"

oto program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Ten program oblicza równanie kwadratowe z liczb, które podaje uzytkownik");
            Console.WriteLine("");

            double a, b, c, delta, pd, x1, x2, p, q, Re, Im;
            Console.WriteLine("Podaj liczbę a");
            a = double.Parse(Console.ReadLine());
            if (a == 0)
            {
                Console.WriteLine("Równanie kwadratowe nie istnieje, gdy 'a' jest równe 0");
                Console.WriteLine("Aby zakończyć, naciśnij dowolny klawisz...");
                Console.ReadKey(true);
            }
            else
            {


                Console.WriteLine("Podaj liczbę b");
                b = double.Parse(Console.ReadLine());

                Console.WriteLine("Podaj liczbę c");
                c = double.Parse(Console.ReadLine());
                Console.WriteLine();

                delta = (b * b) - 4 * (a * c);
                pd = Math.Sqrt(Math.Abs(delta));
                x1 = (-b + pd) / (2 * a);
                x2 = (-b - pd) / (2 * a);
                p = (-b) / (2 * a);
                Re = (-b / (2 * a));
                Im = pd / (2 * a);
                q = -delta / (4 * a);


                //wyswietlanie delty
                if (delta < 0)
                {
                    Console.WriteLine("Delta jest równa " + delta + ". Pierwiastki obliczone zostały za pomocą liczb zespolonych: ");
                    Console.WriteLine("X1= " + Re + " - " + Im + "j");
                    Console.WriteLine("X2= " + Re + " + " + Im + "j");
                }
                else if (delta == 0)
                {
                    Console.WriteLine("Delta jest równa 0. Równanie ma 2 takie same rozwiązania i jest to: ");
                    Console.WriteLine("X= " + p);
                }
                else
                {
                    Console.WriteLine("Delta jest równa " + delta + ". Równanie ma dwa rozwiązania:");
                    Console.WriteLine("X1= " + x1);
                    Console.WriteLine("X2= " + x2);
                }

                //wyswietlanie 3 postaci rownania
                Console.WriteLine();
                Console.WriteLine(" trzy postaci tego równania:");
                Console.WriteLine();

                //warunki dla postaci ogolnej(algebraicznej)
                Console.WriteLine("Postać ogólna(algebraiczna):");
                if (b < 0 && c < 0)
                {
                    Console.WriteLine("y=" + a + "*x^2  " + b + "*x  " + c);
                    Console.WriteLine();
                }
                else if (b < 0 && c > 0)
                {
                    Console.WriteLine("y=" + a + "*x^2  " + b + "*x + " + c);
                    Console.WriteLine();
                }
                else if (b > 0 && c < 0)
                {
                    Console.WriteLine("y=" + a + "*x^2 + " + b + "*x  " + c);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("y=" + a + "*x^2 + " + b + "*x + " + c);
                    Console.WriteLine();
                }

                //warunki dla postaci iloczynowej liczby zespolone
                if (delta < 0)
                {

                    if (Re < 0 && Im < 0)
                    {
                        Console.WriteLine("Postać iloczynowa:");
                        Console.WriteLine("y=" + a + "*(x - (" + Re + +Im + "j))*(x - (" + Re + "-" + Im + "j))");
                        Console.WriteLine();
                    }
                    else if (Re < 0 && Im > 0)
                    {
                        Console.WriteLine("Postać iloczynowa:");
                        Console.WriteLine("y=" + a + "*(x - (" + Re + "+" + Im + "j))*(x - (" + Re + "-" + Im + "j))");
                        Console.WriteLine();
                    }
                    else if (Re > 0 && Im < 0)
                    {
                        Console.WriteLine("Postać iloczynowa:");
                        Console.WriteLine("y=" + a + "*(x - " + Re + +Im + "j)*(x - (" + Re + "-" + Im + "j))");
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("Postać iloczynowa:");
                        Console.WriteLine("y=" + a + "*(x - " + Re + "+" + Im + "j)*(x - " + Re + "-" + Im + "j)");
                        Console.WriteLine();
                    }
                }

                //warunki dla postaci iloczynowej
                else
                {
                    if (x1 < 0 && x2 < 0)
                    {
                        Console.WriteLine("Postać iloczynowa:");
                        Console.WriteLine("y=" + a + "*(x - (" + x1 + "))*(x - (" + x2 + "j))");
                        Console.WriteLine();
                    }
                    else if (x1 < 0 && x2 > 0)
                    {
                        Console.WriteLine("Postać iloczynowa:");
                        Console.WriteLine("y=" + a + "*(x - (" + x1 + "))*(x - " + x2 + ")");
                        Console.WriteLine();
                    }
                    else if (x1 > 0 && x2 < 0)
                    {
                        Console.WriteLine("Postać iloczynowa:");
                        Console.WriteLine("y=" + a + "*(x - " + x1 + ")*(x - (" + x2 + "))");
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("Postać iloczynowa:");
                        Console.WriteLine("y=" + a + "*(x - " + x1 + ")*(x - " + x2 + ")");
                        Console.WriteLine();
                    }
                }

                //warunki dla postaci kanonicznej
                if (p < 0 && q < 0)
                {
                    Console.WriteLine("Postać kanoniczna");
                    Console.WriteLine("y=" + a + "*(x - (" + p + "))^2 " + q);
                    Console.WriteLine();
                }
                else if (p < 0 && q > 0)
                {
                    Console.WriteLine("Postać kanoniczna");
                    Console.WriteLine("y=" + a + "*(x - (" + p + "))^2 + " + q);
                    Console.WriteLine();
                }
                else if (p > 0 && q < 0)
                {
                    Console.WriteLine("Postać kanoniczna");
                    Console.WriteLine("y=" + a + "*(x -" + p + ")^2  " + q);
                    Console.WriteLine();
                }
                else if (p < 0 && q == 0)
                {
                    Console.WriteLine("Postać kanoniczna");
                    Console.WriteLine("y=" + a + "*(x -(" + p + "))^2 + " + q);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("Postać kanoniczna");
                    Console.WriteLine("y=" + a + "*(x -" + p + ")^2 + " + q);
                    Console.WriteLine();
                }
                Console.WriteLine("Aby zakończyć, naciśnij dowolny klawisz...");
                Console.ReadKey(true);


            }
        }

    }
}
0

Wygląda, że tak może być, jest przecież: Console.WriteLine("X1= " + Re + " - " + Im + "j"); . Dla jakich danych tak wyświetla?

1

tylko w liczbach zespolonych wychodzi wynik typu "2-(-2)", a potrzebuje aby od razu program wyświetlał "2+2"

Coś takiego? oczywiście daleko temu do perfekcji

static string ConvertTwoNegativesIntoPositive(string input)
{    
    return ClosingBracketsWithoutPairRemover( input.Replace("-(-", "+") );
}

static string ClosingBracketsWithoutPairRemover(string input)
{
    int countOfOpeningBrackets = 0;

    for (int i=0; i < input.Count(); i++)
    {
        if (input[i] == '(')
        {
            countOfOpeningBrackets++;
        }
        if (input[i] == ')' && countOfOpeningBrackets==0)
        {
            input = input.Remove(i, 1);
        }
        else if (input[i] == ')' && countOfOpeningBrackets>0)
        {
            countOfOpeningBrackets--;
        }
    }

    return input;
}
2-(-2) = 2+2

(-3)-(-15)+70-(-4) = (-3)+15+70+4
0
WeiXiao napisał(a):

tylko w liczbach zespolonych wychodzi wynik typu "2-(-2)", a potrzebuje aby od razu program wyświetlał "2+2"

Coś takiego? oczywiście daleko temu do perfekcji

static string ConvertTwoNegativesIntoPositive(string input)
{    
    return ClosingBracketsWithoutPairRemover( input.Replace("-(-", "+") );
}

static string ClosingBracketsWithoutPairRemover(string input)
{
    int countOfOpeningBrackets = 0;

    for (int i=0; i < input.Count(); i++)
    {
        if (input[i] == '(')
        {
            countOfOpeningBrackets++;
        }
        if (input[i] == ')' && countOfOpeningBrackets==0)
        {
            input = input.Remove(i, 1);
        }
        else if (input[i] == ')' && countOfOpeningBrackets>0)
        {
            countOfOpeningBrackets--;
        }
    }

    return input;
}
2-(-2) = 2+2

(-3)-(-15)+70-(-4) = (-3)+15+70+4

super tylko nie szczerze mówiąc nie wiem gdzie to dodać aby nie krzaczyło

1 użytkowników online, w tym zalogowanych: 0, gości: 1