[C11] C#: Giải phương trình đến bậc 2

Vũ Long

Thành Viên PIF
Hôm bữa học về em có làm thử code PT đến bậc 2 như thế này:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Solving
{
class PTBac_2
{
public float a, b, c;
public float x1, x2;
private float delta;
public int k;
 
public void PTBac2()
{
delta = b*b - 4*a*c;
Console.WriteLine("Do it");
if (delta < 0)
k = -1;
if (delta > 0)
{
k = 1;
x1 = (-b + (float)Math.Sqrt(delta)) / 2 / a;
x2 = (-b - (float)Math.Sqrt(delta)) / 2 / a;
}
if (delta == 0)
{
k = 0;
x1 = -b / 2 / a;
x2 = x1;
}
}
}
 
class PTBac_1
{
public float a,b,x;
public void PTBac1()
{
x= -b/a;
}
}
 
class Program
{
public static void Main(string[] args)
{
float a, b, c;
 
//Input a,b,c;
string input_a, input_b, input_c;
Console.WriteLine("Nhap a = ");
input_a = Console.ReadLine();
Console.WriteLine("Nhap b = ");
input_b = Console.ReadLine();
Console.WriteLine("Nhap c = ");
input_c = Console.ReadLine();
 
//Convert string to float type;
bool result = float.TryParse(input_a, out a);
result = float.TryParse(input_b, out b);
result = float.TryParse(input_c, out c);
 
//Create new object;
PTBac_2 PT1 = new PTBac_2();
PT1.a = a;
PT1.b = b;
PT1.c = c;
 
PTBac_1 PT2 = new PTBac_1();
PT2.a = b;
PT2.b = c;
 
//Solving
if (a != 0)
{
if (PT1.k == -1)
Console.WriteLine("Phuong trinh vo nghiem!");
if (PT1.k == 0)
{
Console.WriteLine("Phuong trinh co nghiem kep: ");
Console.WriteLine("x = {0}", PT1.x1);
}
if (PT1.k == 1)
{
Console.WriteLine("Phuong trinh co 2 nghiem phan biet: ");
Console.WriteLine("x1 = {0}", PT1.x1);
Console.WriteLine("x2 = {0}", PT1.x2);
}
}
else
{
if (b != 0)
{
Console.WriteLine("Phuong trinh co nghiem la: ");
Console.WriteLine("x = {0}", PT2.x);
}
else
{
if (c == 0)
Console.WriteLine("Phuong trinh vo so nghiem!");
else
Console.WriteLine("Phuong trinh vo nghiem!");
}
}
Console.ReadKey();
 
}
}
}
Không có lỗi sai nhưng kết quả luôn là 0. Em nghĩ là mình chưa chạy được cái hàm trong 2 class trên mà không biết làm sao để chạy. Mong mấy anh chị chỉ giáo.
 

MrChips

Thành Viên PIF
Bạn thêm 2 dòng này vào là chạy được :D
Code:
            PT1.PTBac2();
            PT2.PTBac1();
Code hoàn chỉnh:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Giai_PT_Bac_2
{
    class PTBac_2
    {
        public float a, b, c;
        public float x1, x2;
        private float delta;
        public int k;
        public void PTBac2()
        {
            delta = b * b - 4 * a * c;
            if (delta < 0)
                k = -1;
            if (delta > 0)
            {
                k = 1;
                x1 = (-b + (float)Math.Sqrt(delta)) / 2 / a;
                x2 = (-b - (float)Math.Sqrt(delta)) / 2 / a;
            }
            if (delta == 0)
            {
                k = 0;
                x1 = -b / 2 / a;
                x2 = x1;
            }
        }
 
    }
    class PTBac_1
    {
        public float a, b, x;
        public void PTBac1()
        {
            x = -b / a;
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            float a, b, c;
         
            //Input a,b,c;
            string input_a, input_b, input_c;
            Console.WriteLine("Nhap a = ");
            input_a = Console.ReadLine();
            Console.WriteLine("Nhap b = ");
            input_b = Console.ReadLine();
            Console.WriteLine("Nhap c = ");
            input_c = Console.ReadLine();
 
            //Convert string to float type;
            bool result = float.TryParse(input_a, out a);
            result = float.TryParse(input_b, out b);
            result = float.TryParse(input_c, out c);
 
            //Create new object;
            PTBac_2 PT1 = new PTBac_2();
            PT1.a = a;
            PT1.b = b;
            PT1.c = c;
 
            PTBac_1 PT2 = new PTBac_1();
            PT2.a = b;
            PT2.b = c;
 
            //Solving
            PT1.PTBac2();
            PT2.PTBac1();
 
            if (a != 0)
            {
                if (PT1.k == -1)
                    Console.WriteLine("Phuong trinh vo nghiem!");
                if (PT1.k == 0)
                {
                    Console.WriteLine("Phuong trinh co nghiem kep: ");
                    Console.WriteLine("x = {0}", PT1.x1);
                }
                if (PT1.k == 1)
                {
                    Console.WriteLine("Phuong trinh co 2 nghiem phan biet: ");
                    Console.WriteLine("x1 = {0}", PT1.x1);
                    Console.WriteLine("x2 = {0}", PT1.x2);
                }
            }
            else
            {
                if (b != 0)
                {
                    Console.WriteLine("Phuong trinh co nghiem la: ");
                    Console.WriteLine("x = {0}", PT2.x);
                }
                else
                {
                    if (c == 0)
                        Console.WriteLine("Phuong trinh vo so nghiem!");
                    else
                        Console.WriteLine("Phuong trinh vo nghiem!");
                }
            }
            Console.ReadKey();
 
        }
    }
}
:1cool_byebye:
 

Vũ Long

Thành Viên PIF
Bạn thêm 2 dòng này vào là chạy được :D
Code:
            PT1.PTBac2();
            PT2.PTBac1();
Code hoàn chỉnh:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Giai_PT_Bac_2
{
    class PTBac_2
    {
        public float a, b, c;
        public float x1, x2;
        private float delta;
        public int k;
        public void PTBac2()
        {
            delta = b * b - 4 * a * c;
            if (delta < 0)
                k = -1;
            if (delta > 0)
            {
                k = 1;
                x1 = (-b + (float)Math.Sqrt(delta)) / 2 / a;
                x2 = (-b - (float)Math.Sqrt(delta)) / 2 / a;
            }
            if (delta == 0)
            {
                k = 0;
                x1 = -b / 2 / a;
                x2 = x1;
            }
        }
 
    }
    class PTBac_1
    {
        public float a, b, x;
        public void PTBac1()
        {
            x = -b / a;
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            float a, b, c;
       
            //Input a,b,c;
            string input_a, input_b, input_c;
            Console.WriteLine("Nhap a = ");
            input_a = Console.ReadLine();
            Console.WriteLine("Nhap b = ");
            input_b = Console.ReadLine();
            Console.WriteLine("Nhap c = ");
            input_c = Console.ReadLine();
 
            //Convert string to float type;
            bool result = float.TryParse(input_a, out a);
            result = float.TryParse(input_b, out b);
            result = float.TryParse(input_c, out c);
 
            //Create new object;
            PTBac_2 PT1 = new PTBac_2();
            PT1.a = a;
            PT1.b = b;
            PT1.c = c;
 
            PTBac_1 PT2 = new PTBac_1();
            PT2.a = b;
            PT2.b = c;
 
            //Solving
            PT1.PTBac2();
            PT2.PTBac1();
 
            if (a != 0)
            {
                if (PT1.k == -1)
                    Console.WriteLine("Phuong trinh vo nghiem!");
                if (PT1.k == 0)
                {
                    Console.WriteLine("Phuong trinh co nghiem kep: ");
                    Console.WriteLine("x = {0}", PT1.x1);
                }
                if (PT1.k == 1)
                {
                    Console.WriteLine("Phuong trinh co 2 nghiem phan biet: ");
                    Console.WriteLine("x1 = {0}", PT1.x1);
                    Console.WriteLine("x2 = {0}", PT1.x2);
                }
            }
            else
            {
                if (b != 0)
                {
                    Console.WriteLine("Phuong trinh co nghiem la: ");
                    Console.WriteLine("x = {0}", PT2.x);
                }
                else
                {
                    if (c == 0)
                        Console.WriteLine("Phuong trinh vo so nghiem!");
                    else
                        Console.WriteLine("Phuong trinh vo nghiem!");
                }
            }
            Console.ReadKey();
 
        }
    }
}
:1cool_byebye:


Thank you :3 Cũng nghĩ là sẽ phải gọi hàm mà gọi nó không ra. Giờ thì làm được rồi :D
 
Top