Ejemplo n.º 1
0
 /// <summary>
 /// Hàm khởi tạo các giá trị cho hàm số.
 /// </summary>
 public Function()
 {
     this.functionString  = null;
     this.typeOfFunction  = PixelsGraph.LoaiHam.Undefined;
     this.functionForm    = DangHam.NonAbs;
     this.values          = null;
     this.colorOfFunction = Color.Empty;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Trả về chuỗi thể hiện cho phương trình hàm số thông qua loại hàm số và các thông số tương ứng.
        /// </summary>
        /// <param name="functionType">Kiểu hàm số.</param>
        /// <param name="Params">Các thông số của hàm số.</param>
        /// <returns>Trả về chuỗi thể hiện cho phương trình hàm số.</returns>
        public static string Format(LoaiHam functionType, params float[] Params)
        {
            switch (functionType)
            {
            case LoaiHam.BacNhat:
            {
                return("y = " + FunctionFormat(Params, "{0:0.##}x", " + {0:0.##}"));
            }

            case LoaiHam.BacHai:
            {
                return("y = " + FunctionFormat(Params, "{0:0.##}x²", " + {0:0.##}x", " + {0:0.##}"));
            }

            case LoaiHam.BacBa:
            {
                return("y = " + FunctionFormat(Params, "{0:0.##}x³", " + {0:0.##}x²", " + {0:0.##}x", " + {0:0.##}"));
            }

            case LoaiHam.TrungPhuong:
            {
                return("y = " + FunctionFormat(Params, "{0:0.##}x⁴", " + {0:0.##}x²", " + {0:0.##}"));
            }

            case LoaiHam.PhanThuc:
            {
                if (Params[0] == Params[2] && Params[1] == Params[3])
                {
                    return("y = 1");
                }

                return("y = (" + FunctionFormat(new float[] { Params[0], Params[1] }, "{0:0.##}x", " + {0:0.##}") + ") / (" + FunctionFormat(new float[] { Params[2], Params[3] }, "{0:0.##}x", " + {0:0.##}") + ")");
            }

            case LoaiHam.Sin:
            {
                return("y = " + FunctionFormat(new float[] { Params[0], Params[3] }, "{0:0.##}.sin(" + FunctionFormat(new float[] { Params[1], Params[2] }, "{0:0.##}x", " + {0:0.##}") + ")", " + {0:0.##}"));
            }

            case LoaiHam.Cos:
            {
                return("y = " + FunctionFormat(new float[] { Params[0], Params[3] }, "{0:0.##}.cos(" + FunctionFormat(new float[] { Params[1], Params[2] }, "{0:0.##}x", " + {0:0.##}") + ")", " + {0:0.##}"));
            }

            case LoaiHam.Log:
            {
                return(string.Format("y = log(x) / log({0:0.##})", Params[0]));
            }

            case LoaiHam.CanThuc:
            {
                return("y = √(" + FunctionFormat(Params, "{0:0.##}x", " + {0:0.##}") + ")");
            }

            default:
                return(null);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Hàm khởi tạo các giá trị cho hàm số.
 /// </summary>
 /// <param name="FunctionString">Chuỗi thể hiện cho phương trình hàm số.</param>
 /// <param name="color">Màu sắc của hàm số.</param>
 /// <param name="type">Kiểu hàm số.</param>
 /// <param name="form">Dạng hàm số.</param>
 /// <param name="discontinuited">Nếu hàm số là đường thẳng (hàm số bậc nhất),
 /// biến thể hiện đường thẳng có phải là đứt nét hay không.
 /// Lưu ý: biến chỉ nhận giá trị truyền vào nếu hàm số là đường thẳng (hàm bậc nhất).</param>
 /// <param name="values">Các thông số của hàm số.</param>
 public Function(Color color, LoaiHam type, DangHam form, bool discontinuited,
                 string note, params float[] values)
 {
     this.functionString  = Function.Format(type, values);
     this.typeOfFunction  = type;
     this.functionForm    = form;
     this.note            = note;
     this.values          = values;
     this.colorOfFunction = color;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Gán đối số và loại hàm của hàm đó.
        /// </summary>
        /// <param name="functionType">Kiểu hàm</param>
        /// <param name="Params">Các đối số của hàm</param>
        /// <returns>Trả về true nếu hàm không gặp lỗi, ngược lại trả về false.</returns>
        public bool SetFunctionParamsAndType(LoaiHam functionType, float[] Params)
        {
            if (Params == null)
            {
                return(false);
            }

            try
            {
                functionString = Format(functionType, Params);
            }
            catch
            {
                return(false);
            }

            values = Params;
            this.typeOfFunction = functionType;

            return(true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gán thông số hàm từ hàm này sang hàm khác.
        /// </summary>
        /// <param name="funct">Hàm số cần lấy giá trị.</param>
        /// <returns>Phương thức trả về true nếu không gặp lỗi, ngược lại trả về false.</returns>
        public bool SetFunctionParamsAndType(Function funct)
        {
            if (funct == null)
            {
                return(false);
            }

            try
            {
                functionString = Format(funct.typeOfFunction, funct.values);
            }
            catch
            {
                return(false);
            }

            values = funct.values;
            this.typeOfFunction = funct.typeOfFunction;

            return(true);
        }
Ejemplo n.º 6
0
        public bool Load(string FileName)
        {
            if (!File.Exists(FileName))
            {
                return(false);
            }

            bool         flag   = true;
            StreamReader reader = null;

            try
            {
                reader = new StreamReader(File.OpenRead(FileName));
            }
            catch (Exception e)
            {
                RadMessageBox.Show(string.Format("Không thể mở tệp!\r\n\r\nLỗi: {0}\r\nĐường dẫn: {1}", e.Message, FileName), "Lỗi", MessageBoxButtons.OK, RadMessageIcon.Error);
                return(false);
            }

            string a = null;

            try
            {
                do
                {
                    a = reader.ReadLine();
                    if (string.IsNullOrEmpty(a))
                    {
                        break;
                    }
                    string[] functionStrings = a.Split(',');

                    Function funct = new Function();

                    LoaiHam type = (LoaiHam)int.Parse(functionStrings[0]);
                    funct.FunctionForm = (DangHam)int.Parse(functionStrings[1]);
                    funct.Color        = Color.FromArgb(int.Parse(functionStrings[2]));
                    funct.Note         = functionStrings[3];
                    int n = int.Parse(functionStrings[4]);

                    float[] values = new float[n];
                    for (int i = 0; i < n; i++)
                    {
                        values[i] = float.Parse(functionStrings[5 + i]);
                    }
                    funct.SetFunctionParamsAndType(type, values);

                    FunctionList.Add(funct);
                } while (a != null);
            }
            catch (Exception e)
            {
                flag = false;
                RadMessageBox.Show(string.Format("Dữ liệu trong tệp không đúng định dạng hoặc đã bị lỗi!\r\n\r\nLỗi: {0}", e.Message), "Lỗi", MessageBoxButtons.OK, RadMessageIcon.Error);
                FunctionList.Clear();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            return(flag);
        }