Пример #1
0
        private void buttonDocfile_Click(object sender, EventArgs e)
        {
            // xoa het cac phan tu dang co combobox
            comboBoxMang.Items.Clear();
            // hien thi dialog doc file
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "Doc tap tin txt|*.txt";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                // lay duong dan file can doc
                string duongdan = dialog.FileName;
                // doc mang tu file
                StreamReader doc = new StreamReader(duongdan);
                PhanSo       phanso1;
                while (!doc.EndOfStream)
                {
                    string str = doc.ReadLine();
                    phanso1 = new PhanSo(str);
                    comboBoxMang.Items.Add(str);
                }

                doc.Close();
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            PhanSo k = new PhanSo();

            k.nhap();
            k.show();
            Console.ReadLine();
        }
Пример #3
0
            public static PhanSo operator /(PhanSo a, PhanSo b)
            {
                int    tuMoi  = a.tu * b.mau;
                int    mauMoi = a.mau * b.tu;
                PhanSo c      = new PhanSo(tuMoi, mauMoi);

                return(c);
            }
Пример #4
0
        public void TestHamDung2ThamSo()
        {
            PhanSo p;

            p = new PhanSo(3, 4);
            Assert.AreEqual(3, p.Tu, "Loi o tu so");
            Assert.AreEqual(4, p.Mau, "loi o mau so");
        }
Пример #5
0
        public void TestTru()
        {
            PhanSo p1 = new PhanSo(1, 2);
            PhanSo p2 = new PhanSo(3, 4);
            PhanSo p3 = p1.Tru(p2);

            Assert.AreEqual(-2, p3.Tu, "Loi o tu so");
            Assert.AreEqual(8, p3.Mau, "loi o mau so");
        }
Пример #6
0
        public void TestChia()
        {
            PhanSo p1 = new PhanSo(1, 2);
            PhanSo p2 = new PhanSo(3, 4);
            PhanSo p3 = p1.Chia(p2);

            Assert.AreEqual(4, p3.Tu, "Loi o tu so");
            Assert.AreEqual(6, p3.Mau, "loi o mau so");
        }
Пример #7
0
            public PhanSo Chia(PhanSo PS2)
            {
                int TS = tu * PS2.mau;
                int MS = mau * PS2.tu;

                PhanSo kq = new PhanSo(TS, MS);

                return(kq);
            }
Пример #8
0
        public void TestPS2()
        {
            PhanSo ps1 = new PhanSo(1, 2);
            PhanSo ps2 = new PhanSo(1, 4);
            PhanSo ps3 = new PhanSo(1, 4);
            PhanSo add = ps2.add(ps3);

            Assert.AreNotEqual(ps1, add);
        }
        public void TestHamDungMacDinh()
        {
            PhanSo p;

            p = new PhanSo();

            Assert.AreEqual(0, p.Tu, "Loi o tu so");
            Assert.AreEqual(1, p.Mau, "Loi o mau so");
        }
Пример #10
0
        public void TestPS2()
        {
            PhanSo ps1 = new PhanSo(1, 2);
            PhanSo ps2 = new PhanSo(1, 3);
            PhanSo ps3 = new PhanSo(1, 4);

            Assert.AreEqual(ps1, ps2);
            Assert.AreNotEqual(ps2, ps3);
        }
Пример #11
0
        public void TestCong_PhanSoAm()
        {
            PhanSo p1 = new PhanSo(-1, 2);
            PhanSo p2 = new PhanSo(3, 4);
            PhanSo p3 = p1.Cong(p2);

            Assert.AreEqual(2, p3.Tu, "Loi o tu so");
            Assert.AreEqual(8, p3.Mau, "loi o mau so");
        }
Пример #12
0
        static void Main(string[] args)
        {
            PhanSo ob1 = new PhanSo(2, 5);

            ob1.show();
            PhanSo ob2 = new PhanSo(ob1);

            ob2.show();
            Console.ReadLine();
            return;
        }
Пример #13
0
        /*
         * Hàm xuất chuỗi
         */

        private string Xuat(PhanSo ps)
        {
            if (ps.tu == ps.mau)
            {
                return("1");
            }
            else
            {
                return(ps.tu + "/" + ps.mau);
            }
        }
Пример #14
0
        private void bt_Calc_Click(object sender, EventArgs e)
        {
            try
            {
                //Biến để tính toán
                PhanSo[] ps     = new PhanSo[n];
                string[] dau    = new string[n];
                PhanSo   Dapan  = new PhanSo();
                Label    daTu   = new Label();
                Label    daMau  = new Label();
                Panel    daGach = new Panel();
                for (i = 0; i < n; i++)
                {
                    ps[i] = new PhanSo();
                    int a, b;
                    a            = int.Parse(Tuso[i].Text);
                    b            = int.Parse(Mauso[i].Text);
                    ps[i].iTuSo  = a;
                    ps[i].iMauSo = b;
                    if (i + 1 != n)
                    {
                        dau[i] = Dau[i].Text;
                    }
                }

                for (i = 0; i < n - 1; i++)
                {
                    if (dau[i] == "+")
                    {
                        Dapan = ps[i] + ps[i + 1];
                    }
                    else if (dau[i] == "-")
                    {
                        Dapan = ps[i] - ps[i + 1];
                    }
                    else if (dau[i] == "*")
                    {
                        Dapan = ps[i] * ps[i + 1];
                    }
                    else if (dau[i] == "/")
                    {
                        Dapan = ps[i] / ps[i + 1];
                    }
                    else
                    {
                        MessageBox.Show("Xin nhập đúng định dạng");
                    }
                }

                Txt_Numerator.Text   = Convert.ToString(Dapan.iTuSo);
                Txt_Denominator.Text = Convert.ToString(Dapan.iMauSo);
            }
            catch { MessageBox.Show("Xin nhập đúng định dạng"); }
        }
Пример #15
0
        /*
         * Hàm rút gon
         */

        private PhanSo RutGon(PhanSo ps1)
        {
            PhanSo ketqua;

            int uchung = USChung(ps1.tu, ps1.mau);

            ketqua.tu  = ps1.tu / uchung;
            ketqua.mau = ps1.mau / uchung;

            return(ketqua);
        }
Пример #16
0
        public static PhanSo TinhTongPhanSo(PhanSo A, PhanSo B)
        {
            PhanSo kq;

            kq.TuSo  = A.TuSo * B.MauSo + B.TuSo * A.MauSo;
            kq.MauSo = A.MauSo * B.MauSo;

            Console.WriteLine("Ket qua tong 2 phan so: ");
            Console.WriteLine($"{kq.TuSo} / {kq.MauSo}");
            return(kq);
        }
Пример #17
0
        /*
         * Tính tổng
         */
        private PhanSo Cong(PhanSo ps1, PhanSo ps2)
        {
            PhanSo ketqua;

            ketqua.tu = (ps1.tu * ps2.mau) + (ps1.mau * ps2.tu);

            ketqua.mau = ps1.mau * ps2.mau;

            PhanSo kqrutgon = RutGon(ketqua);

            return(kqrutgon);
        }
Пример #18
0
        static void Main(string[] args)
        {
            PhanSo ob = new PhanSo();

            ob.show();
            ob.Cong();
            ob.Tru();
            ob.Nhan();
            ob.Chia();
            ob.SoSanh();
            Console.ReadLine();
        }
Пример #19
0
 private void buttonSua_Click(object sender, EventArgs e)
 {
     if (comboBoxMang.SelectedIndex != -1)
     {
         string str1 = textBoxGiaTri.Text;
         PhanSo ps   = new PhanSo(str1);
         comboBoxMang.Items[comboBoxMang.SelectedIndex] = ps;
     }
     else
     {
         MessageBox.Show("Ban chua chon phan tu can sua", "Thong bao");
     }
 }
Пример #20
0
 private void buttonSua_Click(object sender, EventArgs e)
 {
     if (comboBoxMang.SelectedIndex != -1)
     {
         string str1 = textBoxGiaTri.Text;
         PhanSo ps = new PhanSo(str1);
         comboBoxMang.Items[comboBoxMang.SelectedIndex] = ps;
     }
     else
     {
         MessageBox.Show("Ban chua chon phan tu can sua", "Thong bao");
     }
 }
Пример #21
0
            //Cộng hai phân số
            public static PhanSo operator +(PhanSo a, PhanSo b)
            {
                if (a.iMauSo == b.iMauSo)
                {
                    return(new PhanSo(a.iTuSo + b.iTuSo, a.iMauSo));
                }
                int    tu1 = a.iTuSo * b.iMauSo;
                int    tu2 = b.iTuSo * a.iMauSo;
                PhanSo ps  = new PhanSo(tu1 + tu2, a.iMauSo * b.iMauSo);

                ps.rutGon();
                return(ps);
            }
Пример #22
0
        public void Cong2PhanSo_DeuLaPhanSoDuong_TraVeTong()
        {
            //Given
            var ps1      = new PhanSo(1, 4);
            var ps2      = new PhanSo(2, 4);
            var expected = new PhanSo(3, 4);

            //When
            var actual = ps1 + ps2;

            //Then
            Assert.Equal(expected.TuSo, actual.TuSo);
            Assert.Equal(expected.MauSo, actual.MauSo);
        }
Пример #23
0
        static void Main(string[] args)
        {
            PhanSo k1 = new PhanSo();

            k1.show();
            PhanSo k2 = new PhanSo(7);

            k2.show();
            PhanSo k3 = new PhanSo(6, 9);

            k3.show();
            Console.ReadLine();
            return;
        }
Пример #24
0
        public void Cong2PhanSo_2PhanSoAm_TraVeTong()
        {
            //Given
            var ps1      = new PhanSo(-1, 4);
            var ps2      = new PhanSo(-2, 4);
            var expected = new PhanSo(-3, 4);

            //When
            var actual = ps1 + ps2;

            //Then
            Assert.Equal(expected.TuSo, actual.TuSo);
            Assert.Equal(expected.MauSo, actual.MauSo);
            Assert.Equal(expected.Dau, actual.Dau);
        }
Пример #25
0
        private void buttonThem_Click(object sender, EventArgs e)
        {
            string chuoi = textBoxGiaTri.Text;
            PhanSo phanso1;

            if (PhanSo.KiemTraChuoiPhanSo(chuoi) == true)
            {
                phanso1 = new PhanSo(chuoi);
                comboBoxMang.Items.Add(phanso1);
            }
            else
            {
                MessageBox.Show("Sai du lieu nhap", "Thong bao");
            }
        }
Пример #26
0
        /*
         * Ham tach chuoi
         */

        private void TachChuoi(ref PhanSo ps1, ref PhanSo ps2)
        {
            //phan so 1

            string[] a = { txtPhanSo1.Text };

            int i = 0;

            while (a[i] != "\0")
            {
                ps1.tu  = int.Parse(a[i]);
                ps1.mau = int.Parse(a[i]);
                i++;
            }

            //phan so 2
        }
Пример #27
0
        static void Main(string[] args)
        {
            PhanSo a = new PhanSo();

            a.Nhap();
            a.Xuat();
            PhanSo b = new PhanSo();

            b.Nhap();
            b.Xuat();

            PhanSo c = a + b;

            Console.WriteLine("\nKet qua phep cong 2 phan so a va b: {0}", c.ToString());
            PhanSo d = a - b;

            Console.WriteLine("Ket qua phep tru 2 phan so a va b: {0}", d.ToString());
            PhanSo e = a * b;

            Console.WriteLine("Ket qua phep nhan 2 phan so a va b: {0}", e.ToString());
            PhanSo f = a / b;

            Console.WriteLine("Ket qua phep chia 2 phan so a va b: {0}", f.ToString());

            if (a == b)
            {
                Console.Write("Hai phan so a va b bang nhau : TRUE ");
            }
            else
            {
                Console.WriteLine("Hai phan so a va b bang nhau : FALSE ");
            }
            if (a != b)
            {
                Console.WriteLine("Hai phan so a va b khac nhau : TRUE ");
            }
            else
            {
                Console.WriteLine("Hai phan so a va b khac nhau : FALSE ");
            }
            PhanSo g = a++;

            Console.WriteLine("Phan so a tang them 1 don vi : " + g.ToString());
            Console.ReadLine();
        }
Пример #28
0
            static void Main()
            {
                PhanSo f1 = new PhanSo(3, 4);

                Console.WriteLine("f1 : {0}", f1.ToString());
                PhanSo f2 = new PhanSo(2, 4);

                Console.WriteLine("f2 : {0}", f2.ToString());
                PhanSo f3 = f1 + f2;

                Console.WriteLine("f1  + f2  = f3 : {0}", f3.ToString());
                PhanSo f4 = f3 + f1;

                Console.WriteLine("f3 + 5 = f4 : {0}", f4.ToString());
                PhanSo f5 = new PhanSo(4, 8);

                if (f5 == f2)
                {
                    Console.WriteLine("f5 :{0} == f2 : {1}", f5.ToString(), f2.ToString());
                }
                Console.ReadLine();
            }
Пример #29
0
        private void buttonDocfile_Click(object sender, EventArgs e)
        {
            // xoa het cac phan tu dang co combobox
            comboBoxMang.Items.Clear();
            // hien thi dialog doc file
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Doc tap tin txt|*.txt";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                // lay duong dan file can doc
                string duongdan = dialog.FileName;
                // doc mang tu file
                StreamReader doc = new StreamReader(duongdan);
                PhanSo phanso1;
                while (!doc.EndOfStream)
                {
                    string str = doc.ReadLine();
                    phanso1 = new PhanSo(str);
                    comboBoxMang.Items.Add(str);
                }

                doc.Close();
            }
        }
Пример #30
0
        /*
         * Hàm Nhập
         */

        private void Nhap(ref PhanSo ps1, ref PhanSo ps2)
        {
            // phan so 1

            string phanso1 = txtPhanSo1.Text;

            string[] tam1 = phanso1.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            if (tam1.Length > 1)
            {
                ps1.tu  = int.Parse(tam1[0]);
                ps1.mau = int.Parse(tam1[1]);
            }

            //phan so 2

            string phanso2 = txtPhanSo2.Text;

            string[] tam2 = phanso2.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            ps2.tu = int.Parse(tam2[0]);

            ps2.mau = int.Parse(tam2[1]);
        }
Пример #31
0
 private void buttonThem_Click(object sender, EventArgs e)
 {
     string chuoi = textBoxGiaTri.Text;
     PhanSo phanso1;
     if (PhanSo.KiemTraChuoiPhanSo(chuoi) == true)
     {
         phanso1 = new PhanSo(chuoi);
         comboBoxMang.Items.Add(phanso1);
     }
     else
     {
         MessageBox.Show("Sai du lieu nhap", "Thong bao");
     }
 }
Пример #32
0
 public bool SoSanhBang(PhanSo ps)
 {
     return(tu * ps.mau == mau * ps.tu);
 }
Пример #33
0
 public void TestChia0()
 {
     PhanSo p1 = new PhanSo(1, 2);
     PhanSo p2 = new PhanSo(0, 4);
     PhanSo p3 = p1.Chia(p2);
 }