Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 4 của 4
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    lớp đa thức trong C#. Giúp mình sửa lỗi?

    sửa giúp mình cái chỗ tính giá trị đa thức cái!!!nó chả tính gì cả
    Mã nguồn PHP:
    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DTHUC_PVA { class Program { class DATHUC { public int bac; public double[] arrheso = new double[10]; public int BAC { get { return bac; } set { bac = value; } } public double[] ARRHESO { get { return arrheso; } set { arrheso = value; } } //phuong thuc khoi tao co tham so public DATHUC(int _bac) { _bac = bac + 1; //farr1=new float[]; } //phuong thuc khoi tao co tham so public DATHUC(params double[] arr1) { bac = arr1.Length - 1; arr1 = new double[bac + 1]; for (int i = 0; i <= Convert.ToInt32(bac); i++) arrheso[i] = arr1[i]; } public double this[int index] { get { return arrheso[index]; } set { arrheso[index] = value; } } //TIN GIA TRI CUA DA THUC VOI GIA TRI X0=? public double GiaTriDaThuc(double x,DATHUC f) { double s = 0; for (int i = 0; i <= bac; i++) { s += f[i] * Math.Pow(x, i); } return s; } //cong 2 da thuc public static DATHUC operator +(DATHUC f, DATHUC g) { DATHUC h = new DATHUC(); int maxbac = Math.Max(f.bac, h.bac); h.bac = maxbac; for (int i = 0; i <= h.bac; i++) { h.arrheso[i] = f.arrheso[i] + g.arrheso[i]; } return h; } public void nhap() { bdt: try { Console.Write("Nhap vao bac cua da thuc: "); bac = Convert.ToInt32(Console.ReadLine()); } catch (Exception loi) { Console.Write(loi.Message); goto bdt; } Console.WriteLine("Nhap vao cac he so cua da thuc."); { for (int i = 0; i <= BAC; i++) { Console.Write("a{0}:", i); arrheso[i] = int.Parse(Console.ReadLine()); } } } public void xuat() { Console.Write("
    DA THUC:
    "
    ); for (int i = BAC; i > 0; i--) { Console.Write("{0}X^{1}+", arrheso[i], BAC--); } Console.Write("{0} ", arrheso[0]); } } static void Main(string[] args) { Console.WriteLine("==========XAY DUNG LOP DA THUC========="); Console.WriteLine("1.Tinh gia tri cua da thuc tai x0"); Console.WriteLine("2.Cong 2 da thuc"); Console.WriteLine("3.Tinh bac cua da thuc"); Console.WriteLine("Chon chuc nang(nhan Esc de thoat)"); DATHUC DT = new DATHUC(); DATHUC DT1 = new DATHUC(); DATHUC DT2 = new DATHUC(); int chon; ConsoleKey keypress = ConsoleKey.A; do { chon = int.Parse(Console.ReadLine()); switch (chon) { case 1: { DT.nhap(); Console.Clear(); Console.WriteLine("====================="); DT.xuat(); Console.Write("
    Nhap gia tri x0="
    ); double x0 = double.Parse(Console.ReadLine()); double gt = DT.GiaTriDaThuc(x0, DT); Console.WriteLine("GIA TRI DA THUC f{0}={1}",x0,gt);//Khong in ra duoc gt cua da thuc } break; case 2: { Console.Write("==============DT1=============
    "
    ); DT1.nhap(); Console.Write("==============DT2=============
    "
    ); DT2.nhap(); Console.Clear(); Console.WriteLine("====================="); DT1.xuat(); DT2.xuat(); int max = Math.Max(DT1.BAC, DT2.BAC); DATHUC h = new DATHUC(max); h = DT1 + DT2;//khong in ra duoc phep cong 2 da thuc Console.WriteLine("
    f(x)+g(x)="
    ,h.ToString()); } break; default: break; } keypress = Console.ReadKey().Key; Console.Clear(); } while (keypress != ConsoleKey.Escape); } } }  

  3. #3
    Ngày tham gia
    Dec 2015
    Bài viết
    0
    Lỗi hàm GiaTriDaThuc

    Mã nguồn PHP:
    //TIN GIA TRI CUA DA THUC VOI GIA TRI X0=? public double GiaTriDaThuc(double x,DATHUC f) { double s = 0; for (int i = 0; i <= bac; i++) { s += f[i] * Math.Pow(x, i); // f là tham số kiểu DATHUC, dùng theo dạng mảng, nên nó có trị 0 } return s; }  
    Hàm GiaTriDaThuc viết tùm lum quá. Viết theo kiểu static và gọi theo kiểu static, nhưng khai báo là hàm object, cho nên thông số lẫn lộn hết trơn.

    Thử viết lại hàm này như sau:

    Mã nguồn PHP:
    //TINH GIA TRI CUA DA THUC VOI GIA TRI X0=? public double GiaTriDaThuc(double x) // dùng cách gọi hàm object { double s = 0, pw = 1.0; // hàm pow tính rất chậm, người ta tính theo cấp bậc for (int i = 0; i <= bac; i++) { s += arrheso[i] * pw; pw *= x; } return s; } //------ gọi hàm double gt = DT.GiaTriDaThuc(x0);  
    tb. bài của bạn còn nhiều lỗi nữa:

    vd hàm
    Mã nguồn PHP:
    //phuong thuc khoi tao co tham so public DATHUC(params double[] arr1)  
    không xài được: trong hàm bạn đặt lại arr1 là mảng mới nên tham sô truyền vào mất tiêu luôn

    Phần này nữa, không lỗi nhưng không đúng với tinh thần LTHDT:
    Mã nguồn PHP:
    public int bac; public double[] arrheso = new double[10]; public int BAC { get { return bac; } set { bac = value; } } public double[] ARRHESO { get { return arrheso; } set { arrheso = value; } }  
    đã có BAC và ARRHESO rồi thì bac và arrheso phải đặt là private

  4. #4
    Mã:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class DaThuc
        {
            //propertier
            private const int maxsize = 101;
            private int degree;
            private int[] coefficient;
            //methods
    
            #region Contructor
            public DaThuc()
            {
                coefficient = new int[maxsize];
                for (int i = 0; i < maxsize; i++)
                    coefficient[i] = 0;
                degree = 0;
            }
    
            public DaThuc(int deg, int start, int increment)
            {
                coefficient = new int[maxsize];
                for (int i = 0; i < maxsize; i++)
                    coefficient[i] = 0;
                degree = 0;
                int current = start;
                for (int i = 0; i <= deg; i++)
                {
                    coefficient[i] = current;
                    current += increment;
                }
                degree = deg;
            }
    
            public DaThuc(int[] cof)
            {
                degree = cof.Length - 1;
                for (int i = 0; i <= degree; i++)
                    coefficient[i] = cof[i];
                if (cof[cof.Length - 1] == 0)
                    coefficient[degree] = 1;
    
            }
            #endregion
            #region Set/get
            public int Setdegree
            {
                get { return degree; }
                set{ degree=value;}
               
            }
            public int[] Setcof
            {
                get { return coefficient; }
                set{coefficient=value;}
            }
            public DaThuc(int deg,int[] cof)
             {
                 this.degree = deg;
                 this.coefficient = cof;
               
             }
           
    
            #endregion
            #region Input/output
           
            public void ReadPolynomial()
            {
                //int degree;
                coefficient = new int[maxsize];
                Console.WriteLine("Polynomial");
                Console.Write("Enter Degree:");
                degree = int.Parse(Console.ReadLine());
                //Nhap he so
                for (int i = degree; i>=0; i--)
                {
                    Console.Write("Nhap he so thu {0}: ", i);
                    coefficient[i] = int.Parse(Console.ReadLine());
                }
                 }
            public void PrintPolynomial()
            {
                string Output = "";
    
                for (int i = degree; i >=0; i--)
                {
                    if (i != 0)
                        Output += coefficient[i] + "x^" + i + "+";
                    else
                        Output += coefficient[i];
                }
                //Output += coefficient[degree] + "x^" 
                Output = Output.Replace("x^0", "");
                Output = Output.Replace("x^1", "x");
                Output = Output.Replace("+-", "-");
                Output = Output.Replace("0x", "");
                Output = Output.Replace("x+ ", "");
                Output = Output.Replace("1x","x");
                Output = Output.Replace("0^", "");
                Output = Output.Replace("++", "+");
                Output.Reverse();
                Console.WriteLine(Output);
            }
            #endregion
            #region Operrator Overloading
            public void TinhGT()
            {
                float n;
            }
            public static DaThuc operator +(DaThuc a, DaThuc b)
            {
                DaThuc c = new DaThuc();
                if (a.degree > b.degree)
                { c.degree = a.degree; }
                else
                c.degree = b.degree;
                    
                for (int i = 0; i <= c.degree ; i++)
                        {
                            c.coefficient[i] = a.coefficient[i] + b.coefficient[i];
                        }
                
    
                return c;
            }
            public static DaThuc operator -(DaThuc a, DaThuc b)
            {
                DaThuc c = new DaThuc();
                if (a.degree > b.degree)
                {
                    c.degree = a.degree;
                }
                else
                c.degree = b.degree;
    
                for (int i = 0; i <= c.degree; i++)
                {
                    c.coefficient[i] = a.coefficient[i] - b.coefficient[i];
                }
    
    
                return c;
            }
            public static DaThuc operator *(DaThuc a, DaThuc b)
            {
                DaThuc c = new DaThuc();
                for (int i = 0; i <= a.degree; i++)
                {
                    for (int j = 0; j <= b.degree; j++)
                    {
                        
                    }
                 
                    
                }
                return c;
            }
            public static DaThuc operator /(DaThuc a, DaThuc b)
            {
                DaThuc c = new DaThuc();
                c.degree = 0;
                c.coefficient=new int[maxsize];
    
                if (a.degree > b.degree)
                    for (int i = a.degree; i >= b.degree; i--)
                    {
                        c.degree = a.degree - b.degree;
                        c.coefficient[i] = a.coefficient[i] / b.coefficient[b.degree];
                        for (int j = a.degree; j >= 0; j--)
                        {
                            if (a.degree == c.degree + b.degree)
                            {
                                a.degree -= c.degree * b.degree;
                                a.coefficient[i] -= c.coefficient[i] * b.coefficient[i];
    
                            }
                        }
                        a.degree--;
                        }
    
                else
                {
                    Console.Write("Thuong cua 2 da thuc = ");               
                 }
                return c;
            }
            public static bool operator ==(DaThuc p, DaThuc q)
            {
                if (p.degree == q.degree)
                    for (int i = 0; i <= p.degree; i++)
                    {
                        for (int j = 0; j <= q.degree; j++)
                        {
                            if (p.coefficient[i] == q.coefficient[j])
                            {
                                return true;
                            }
                        }
                    }
               
                return false;  
                  }
    
            public static bool operator !=(DaThuc p, DaThuc q)
            {
                if (p.degree != q.degree)
                    for (int i = 0; i <= p.degree; i++)
                    {           
                        for (int j = 0; j <= q.degree; j++)
                        {
                            if (p.coefficient[i] != q.coefficient[j])
                            {
                                return true;
                            }
                        }
                }
               return false;
                
                //return !(p == q);
            }
            #endregion
        }
    }
    Cái này nhân và chia mình chưa làm[IMG]images/smilies/biggrin.png[/IMG]
    program:
    Mã:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
     
        class Program
        {
           
            static void Main(string[] args)
            {
               
              lap://chon Menu
                Console.WriteLine("Menu Polymonial  
    1.ADD(+) 
    2.subduct(-)  
    3.multiply(*) 
    4.Dispart(/) 
    5.Compare(so.sanh)    
    6.End");
    		Console.Write("Bam so de vao menu tuong ung:");
    		string chon=Console.ReadLine();
    		switch (chon)
    		{
    			case "1":menu1();break;//chon 1 thi no se vao menu 1
    			case "2":menu2();break;//Chon 2 thi no se vao menu 2
                case "3":menu3();break;//Chon 3 thi no se vao menu 3
                case "4":menu4();break;//Chon 4 thi no se vao menu 4
                case "5": menu5(); break;//Chon 5 thi no se vao menu 5
                case "6":return;//Chon 6 thi no se thoat
    			default:{Console.WriteLine("Ban nhap chua dung, nhap lai");goto lap;}//Nhap khac thi nhap lai
    				
    		}
    		Console.ReadLine();
    	}
    	public static void menu1()
    	{
            Console.WriteLine("Go To Menu ADD(+)");
             DaThuc p = new DaThuc();
                p.ReadPolynomial();
                p.PrintPolynomial();
                DaThuc q = new DaThuc();
                q.ReadPolynomial();
                q.PrintPolynomial();
            DaThuc c = new DaThuc();
                c = p + q;
                Console.Write("Tong 2 Da Thuc la : ");
               c.PrintPolynomial();
               Console.WriteLine("");
               Console.WriteLine("");
           lap://chon Menu
               Console.WriteLine("Menu Polymonial  
    1.ADD(+) 
    2.subduct(-)  
    3.multiply(*) 
    4.Dispart(/) 
    5.Compare(so.sanh)    
    6.End");
               Console.Write("Bam so de vao menu tuong ung:");
               string chon = Console.ReadLine();
               switch (chon)
               {
                   case "1": menu1(); break;//chon 1 thi no se vao menu 1
                   case "2": menu2(); break;//Chon 2 thi no se vao menu 2
                   case "3": menu3(); break;//Chon 3 thi no se vao menu 3
                   case "4": menu4(); break;//Chon 4 thi no se vao menu 4
                   case "5": menu5(); break;//Chon 5 thi no se vao menu 5
                   case "6": return;//Chon 6 thi no se thoat
                   default: { Console.WriteLine("Ban nhap chua dung, nhap lai"); goto lap; }//Nhap khac thi nhap lai
    
               }
               Console.ReadLine();
    	}
    	public static void menu2()
    	{
            Console.WriteLine("Go To Menu subduct(-) ");
              DaThuc p = new DaThuc();
                p.ReadPolynomial();
                p.PrintPolynomial();
                DaThuc q = new DaThuc();
                q.ReadPolynomial();
                q.PrintPolynomial();
            DaThuc c = new DaThuc();
                c = p - q;
                Console.WriteLine("Hieu 2 Da Thuc la : ");
                c.PrintPolynomial();
                Console.WriteLine("");
                Console.WriteLine("");
            lap://chon Menu
                Console.WriteLine("Menu Polymonial  
    1.ADD(+) 
    2.subduct(-)  
    3.multiply(*) 
    4.Dispart(/) 
    5.Compare(so.sanh)    
    6.End");
                Console.Write("Bam so de vao menu tuong ung:");
                string chon = Console.ReadLine();
                switch (chon)
                {
                    case "1": menu1(); break;//chon 1 thi no se vao menu 1
                    case "2": menu2(); break;//Chon 2 thi no se vao menu 2
                    case "3": menu3(); break;//Chon 3 thi no se vao menu 3
                    case "4": menu4(); break;//Chon 4 thi no se vao menu 4
                    case "5": menu5(); break;//Chon 5 thi no se vao menu 5
                    case "6": return;//Chon 6 thi no se thoat
                    default: { Console.WriteLine("Ban nhap chua dung, nhap lai"); goto lap; }//Nhap khac thi nhap lai
    
                }
                Console.ReadLine();
    
    	}
            public static void menu3()
    	{
            Console.WriteLine("Go To Menu multiply(*)  ");
            DaThuc p = new DaThuc();
            p.ReadPolynomial();
            p.PrintPolynomial();
            DaThuc q = new DaThuc();
            q.ReadPolynomial();
            q.PrintPolynomial();
            DaThuc c = new DaThuc();
            c = p * q;
            Console.WriteLine("Tich 2 Da Thuc la : ");
            c.PrintPolynomial();
            Console.WriteLine("");
            Console.WriteLine("");
        lap://chon Menu
            Console.WriteLine("Menu Polymonial  
    1.ADD(+) 
    2.subduct(-)  
    3.multiply(*) 
    4.Dispart(/) 
    5.Compare(so.sanh)    
    6.End");
            Console.Write("Bam so de vao menu tuong ung:");
            string chon = Console.ReadLine();
            switch (chon)
            {
                case "1": menu1(); break;//chon 1 thi no se vao menu 1
                case "2": menu2(); break;//Chon 2 thi no se vao menu 2
                case "3": menu3(); break;//Chon 3 thi no se vao menu 3
                case "4": menu4(); break;//Chon 4 thi no se vao menu 4
                case "5": menu5(); break;//Chon 5 thi no se vao menu 5
                case "6": return;//Chon 6 thi no se thoat
                default: { Console.WriteLine("Ban nhap chua dung, nhap lai"); goto lap; }//Nhap khac thi nhap lai
    
            }
            Console.ReadLine();
    	}
            public static void menu4()
    	{
            Console.WriteLine("Go To Menu Dispart(/)  ");
            DaThuc p = new DaThuc();
            p.ReadPolynomial();
            p.PrintPolynomial();
            DaThuc q = new DaThuc();
            q.ReadPolynomial();
            q.PrintPolynomial();
            DaThuc c = new DaThuc();
            c = p / q;
            c.PrintPolynomial();
    
            Console.WriteLine("");
            Console.WriteLine("");
        lap://chon Menu
            Console.WriteLine("Menu Polymonial  
    1.ADD(+) 
    2.subduct(-)  
    3.multiply(*) 
    4.Dispart(/) 
    5.Compare(so.sanh)    
    6.End");
            Console.Write("Bam so de vao menu tuong ung:");
            string chon = Console.ReadLine();
            switch (chon)
            {
                case "1": menu1(); break;//chon 1 thi no se vao menu 1
                case "2": menu2(); break;//Chon 2 thi no se vao menu 2
                case "3": menu3(); break;//Chon 3 thi no se vao menu 3
                case "4": menu4(); break;//Chon 4 thi no se vao menu 4
                case "5": menu5(); break;//Chon 5 thi no se vao menu 5
                case "6": return;//Chon 6 thi no se thoat
                default: { Console.WriteLine("Ban nhap chua dung, nhap lai"); goto lap; }//Nhap khac thi nhap lai
    
            }
            Console.ReadLine();
    	}
            public static void menu5()
            {
                Console.WriteLine("Go To Menu Compare(sosanh)  ");
                DaThuc p = new DaThuc();
                p.ReadPolynomial();
                p.PrintPolynomial();
                DaThuc q = new DaThuc();
                q.ReadPolynomial();
                q.PrintPolynomial();
                if (p == q)
                {
                    Console.WriteLine("hai da thuc bang nhau");
                }
                else
                {
                    Console.WriteLine("hai da thuc khong bang nhau");
                }
                Console.WriteLine("");
                Console.WriteLine("");
            lap://chon Menu
                Console.WriteLine("Menu Polymonial  
    1.ADD(+) 
    2.subduct(-)  
    3.multiply(*) 
    4.Dispart(/) 
    5.Compare(so.sanh)    
    6.End");
                Console.Write("Bam so de vao menu tuong ung:");
                string chon = Console.ReadLine();
                switch (chon)
                {
                    case "1": menu1(); break;//chon 1 thi no se vao menu 1
                    case "2": menu2(); break;//Chon 2 thi no se vao menu 2
                    case "3": menu3(); break;//Chon 3 thi no se vao menu 3
                    case "4": menu4(); break;//Chon 4 thi no se vao menu 4
                    case "5": menu5(); break;//Chon 5 thi no se vao menu 5
                    case "6": return;//Chon 6 thi no se thoat
                    default: { Console.WriteLine("Ban nhap chua dung, nhap lai"); goto lap; }//Nhap khac thi nhap lai
    
                }
                Console.ReadLine();
                
            }
            
        }
    }

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •