1 vài code ngôn ngữ lập trình C++ và C#


Đây là 1 số bài tập cơ bản của ngôn ngữ lập trình C++ và C# của chương trình học năm thứ 2 bậc cao đẳng chuyên ngành công nghệ thông tin @@

Còn đại học thì mình chưa được học lên chưa biết @_^





**C++
1: Mảng 1 Chiều Số Nguyên

Mã:
namespace Mang1Chieu
{
    class Mang1ChieuSoNguyen
    {
        static void NhapMang(out int[] a, out int n)
        {
            Console.Write("Nhap vao so phan tu mang: ");
            n = int.Parse(Console.ReadLine());
            a = new int[n];// cap nhat vung nho n phan tu
            for (int i = 0; i < n; i++)
            {
                Console.Write("Nhap vao phan tu thu "+i+": ");
                a[i] = int.Parse(Console.ReadLine());
            }
        }
        static void NhapRanDom(out int[] a, out int n)
        {
            Random R = new Random();
            n = R.Next(1, 20);
            a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = R.Next(100);
            }
        }
        static void Xuat(int[] a, int n)
        {
          
            for (int i = 0; i < n; i++)
            {
                Console.Write(a[i]+ " ");
            }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            int n;
            int[] a;
            NhapMang(out a, out n);
            Console.Write("Mang la: ");
            Xuat(a, n);
            NhapRanDom(out a,out n);
            Console.Write("Mang RanDom la: ");
            Xuat(a, n);
        }
    }
}
**C#
1: Tính Chu Vi Diện Tích HCN

Mã:
namespace ChuVivaDienTichHCN
{
    class CHCN
    {
        uint Dai, Rong;
        public void Nhap()
        {
            Console.Write("Nhap vao chieu dai HCN: ");
            Dai = uint.Parse(Console.ReadLine());
            Console.Write("Nhap vao chieu rong HCN: ");
            Rong = uint.Parse(Console.ReadLine());
        }
        public void Xuat()
        {
            Console.WriteLine("|--> HCN "+"(" + Dai + "," + Rong + ")");
        }
        public static CHCN operator++ (CHCN h)
        {
            CHCN h1 = new CHCN();
            h1.Dai = h.Dai++;
            h1.Rong = h.Rong++;
            return h;
        }
        public CHCN() { }
        public CHCN(uint Dai, uint Rong)
        {
            this.Dai = Dai;
            this.Rong = Rong;
        }
        //public CHCN (CHCN HCN)
        //{
        //    Dai=HCN.Dai;
        //    Rong=HCN.Rong;
        //}
        public double S()
        {
            double S;
            S = Dai * Rong;
            return S;
        }
        public static bool operator >(CHCN a, CHCN b)
        {
            double S1 = a.S();
            double S2 = b.S();
            if (S1 > S2)
                return true;
            return false;
        }
        public static bool operator <(CHCN a, CHCN b)
        {
            double S1 = a.S();
            double S2 = b.S();
            if (S1 < S2)
                return true;
            return false;
        }


        public override string ToString()
        {
            string s = "HCN :" + "(" + Dai + "," + Rong + ")";
            return s;
        }
    }
}


namespace ChuVivaDienTichHCN
{
    class Program
    {
        static void Main(string[] args)
        {
            CHCN h1 = new CHCN();
            //pt mac dinh
            h1.Nhap();
            h1.Xuat();
            h1++;
            Console.Write("HCN Sau Khi Tang ");
            h1.Xuat();
            CHCN h2 = new CHCN(4,6);
            Console.Write("HCN Pt2: ");
            h2.Xuat();
            if (h1 > h2)
                Console.Write("{0} > {1}", h1, h2);
            else
                Console.Write("{0} <= {1}",h1,h2);
            
        }
    }
}
2: BT Chạy chữ trên màn hình Console

Mã:
namespace chaychu
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Nhap chuoi can chay: ");
            string str = Console.ReadLine();
            int x = 0;
            int y = str.Length;
            while (Console.KeyAvailable != true)
            {
                if (x + y == 80)
                {
                    x = 0;
                }
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.SetCursorPosition(x++, 0);
                Console.Write(str);
                //Console.SetCursorPosition(80 - x - y, 0);
                //Console.Write(str);
                //x++;
                ////while (!Console.KeyAvailable)
                //{
                //    DateTime d = DateTime.Now;
                //    Console.SetCursorPosition(5, 10);
                //    Console.Write("{0:00}:{1:00}:{2:00}", d.Hour, d.Minute, d.Second);
                    
                //    Thread.Sleep(100); //using System.Threading;
                //    Console.Clear();
                //}


                Thread.Sleep(80);
            }
        }
    }
}
3: Game bắn chữ trên màn hình Console

Mã:
namespace BanChu
{
    class CDIEM
    {
        private int x;
        private int y;
        public char c;


        public CDIEM()
        {
        }


        public CDIEM(int xx, int yy, char cc)
        {
            x = xx;
            y = yy;
            c = cc;
        }


        public bool KiemTraX(int xx)
        {
            if (xx <= 0 || xx >= Console.WindowWidth)
            {
                return false;
            }
            return true;
        }
        public bool KiemTraY(int yy)
        {
            if (yy <= 0 || yy >= Console.WindowHeight)
            {
                return false;
            }
            return true;
        }


        public void DichPhai(int k)
        {
            x += k;
        }
        public void DichTrai(int k)
        {
            x -= k;
        }
        public void DichLen(int k)
        {
            y -= k;
        }
        public void DichXuong(int k)
        {
            y += k;
        }


        public void Xuat()
        {
            Console.SetCursorPosition(x, y);
            Console.Write(c);
        }


        public void Xoa()
        {
            Console.Clear();
        }


        public float KhoangCach(CDIEM M)
        {
            return 0;
        }
        public int KhoangCachX()
        {
            return x;
        }
        public int KhoangCachY()
        {
            return y;
        }


    }
}


namespace BanChu
{
    class Program
    {
        static ArrayList list = new ArrayList();
        static Random rnd = new Random();
        static int TOCDO = 500;


        static void Main(string[] args)
        {
            Console.CursorVisible = false;
            do
            {
                NhanPhimBam();
                TaoChu();
                RoiChu();
                Thread.Sleep(TOCDO);
            } while (KiemTraTiepTuc());
        }


        static void RoiChu()
        {
            Console.Clear();
            for (int i = 0; i < list.Count; i++)
            {
                ((CDIEM)list[i]).DichXuong(1);
                ((CDIEM)list[i]).Xuat();
            }


        }


        static void TaoChu()
        {
            char c;
            //do
            //{
            //    c = (char)(rnd.Next(65, 90));
            //} while(KiemTraChuTonTai(c));
            c = (char)(rnd.Next(65, 90));
            list.Add(new CDIEM(rnd.Next(1, Console.WindowWidth - 1), 0, c));
        }


        static bool KiemTraTiepTuc()
        {
            if (((CDIEM)list[0]).KhoangCachY() > Console.WindowHeight)
                return false;
            return true;
        }


        static void NhanPhimBam()
        {
            while (Console.KeyAvailable)
            {
                char cc = Console.ReadKey(true).KeyChar;


                for (int i = 0; i < list.Count; i++)
                {
                    if (((CDIEM)list[i]).c == cc)
                    {
                        list.RemoveAt(i);
                    }
                }


            }
        }
    }
}
4: Bài Tập lớp điểm

Mã:
namespace lopdiem
{
    class CDiem
    {
        private int x, y;
        char Ten;
        public void Nhap()
        {
            do
            {
                Console.Write("Nhap vao x: ");
                x = int.Parse(Console.ReadLine());
                Console.Write("Nhap vao y: ");
                y = int.Parse(Console.ReadLine());
                Console.Write("Nhap ten diem: ");
                Ten = char.Parse(Console.ReadLine());
                Ten = char.ToUpper(Ten);
              
            } while (x > Console.WindowWidth || y > Console.WindowHeight);
        }
        public CDiem (int x, int y, char Ten)
        {
            this.x = x; this.y = y; this.Ten = Ten;
        }
        public CDiem()
        { }
        public CDiem(CDiem cd)
        {
            x = cd.x; y = cd.y; Ten = cd.Ten;
        }
        public int X
        {
            get { return x; }
            set { x=value ;}
        }
        public int Y
        {
            get { return y; }
            set { y = value; }
        }
        public char TEN
        {
            get { return Ten; }
            set { Ten = value; }
        }
        public bool Ktra()
        {
            if (x >= 0 && x <= Console.WindowWidth && y >= 0 && y <= Console.WindowHeight)
                return true;
            return false;
        }
        public double Khoangcach(CDiem cd)
        {
            double q = Math.Sqrt((x - cd.x) * (x - cd.x) + (y - cd.y) * (y - cd.y));
            return q;
        }
        public void Xuat()
        {
            //Console.Clear();
            //Console.SetCursorPosition(x,y);
            //Console.Write("*");
            Console.WriteLine("Diem 1: " + Ten + "(" + x + "," + y + ")");
            //Console.WriteLine("Diem 2: " + Ten1 + "(" + x1 + "," + y1 + ")");
            
        }

namespace lopdiem
{
    class Program
    {


        static void Main(string[] args)
        {
            CDiem cd = new CDiem();
            cd.Nhap();
            cd.Xuat();
            CDiem cd1 = new CDiem(4,6,'H');
            cd1.Xuat();
            double p= cd.Khoangcach(cd1);
            Console.Write("Khoang cach la: " + p);
            
            //Console.WriteLine("Phuong thuc thu 1: ");
            //CDiem cd = new CDiem();
            //cd.Nhap();
            //cd.Xuat();
            //Console.ReadLine();
            //Console.WriteLine("Phuong thuc thu 2: ");
            //CDiem cd1 = new CDiem(15, 5, 'G');
            //cd1.Xuat();
            //Console.ReadLine();
            //Console.WriteLine("Phuong thuc thu 3: ");
            //CDiem cd2 = new CDiem(cd);
            //cd2.Xuat();
            //cd.X = 2;
            //cd.Y = 9;
            //cd.TEN='A';
            //cd.Xuat();
        }
    }
}
5: Tìm USCLN & BSCNN

Mã:
namespace BSCNNUSCLN{
    class Program
    {
        static void Nhap(out int a, out int b)
        {
            
                Console.WriteLine("Nhap so a: ");
                a = int.Parse(Console.ReadLine());
                Console.WriteLine("Nhap so b: ");
                b = int.Parse(Console.ReadLine());
                      
        }
        static int USCLN(int a, int b)
        {
            if (a < 0) a=Math.Abs(a);
            if (b < 0) b=Math.Abs(b);
            if (a == 0 || b == 0)
                return a + b;
            while (a != b)
            {
                if (a > b)
                    a -= b;
                else
                    b -= a;
            }
            return a;
        }
        static int BSCNN(int a, int b)
        {
            int Bs= (a * b) / USCLN(a, b);
            return Bs;
        }
        static void Main(string[] args)
        {
            int a, b;
            Nhap(out a, out b);
            int Us = USCLN(a, b);
            int Bs = BSCNN(a, b);
           Console.WriteLine("Uoc so chung lon nhat la: " + Us);
           Console.WriteLine("Boi so chung nho nhat la: " + Bs);
        }
    }
}
6: Kiểm Tra Ngày Tháng Năm

Mã:
namespace KTraNTN
{
    class Program
    {
        static void Nhap(out sbyte ng, out sbyte th, out int nam)
        {
            Console.WriteLine("Nhap vao ngay: ");
            ng = sbyte.Parse(Console.ReadLine());
            Console.WriteLine("Nhap vao thang: ");
            th = sbyte.Parse(Console.ReadLine());
            Console.WriteLine("Nhap vao nam: ");
            nam = int.Parse(Console.ReadLine());
        }
        static bool Namnhuan(int nam)
        {
            if ((nam % 4 == 0 && nam % 100 != 0) || (nam % 400 == 0))
                return true;
            return false;
        }
        static bool KThople(sbyte ng, sbyte th, int nam)
        {
            if (nam > 0 && th > 0 && th < 13 && ng > 0 && ng <= Ngaymax(th, nam))
                return true;
            return false;
        }
        static sbyte Ngaymax(sbyte th, int nam)
        {
            if (th == 1 || th == 3 || th == 5 || th == 8 || th == 7||th==10||th==12)
            {
                return 31;
            }
            if (th==4||th==6||th==9||th==11)
            {
                return 30;
            }
            if (th==2)
            {
                if(Namnhuan(nam)==true)
                    return 29;
                return 28;
            }
            return 0;
        }
        static void Xuat(sbyte ng, sbyte th, int nam)
        {
            
                Console.WriteLine("Ngay " + ng + " thang " + th + " nam " + nam);
        }
        static void Main(string[] args)
        {
            sbyte ng, th;
            int nam;
            Nhap(out ng, out th, out nam);
            Console.WriteLine("Ngay lon nhat cua thang 2 la: "+Ngaymax(2, 2000));
            if (KThople(ng, th, nam) == true)
            {
            Console.WriteLine("Ngay thang nam hop le");
                Xuat(ng, th, nam);
            }
            else
                Console.WriteLine("Ngay thang khong hop le");
        }
    }
}
7: Nhập vào sinh viên và tính DTB

Mã:
namespace SV
{
    class Program
    {
        struct HocSinh
        {
            public string hoten;
            public float toan, van;
        };
        static void Nhap(out HocSinh hs)
        {
            Console.Write("Nhap ho ten: ");
            hs.hoten = Console.ReadLine();
            Console.Write("Nhap diem toan: ");
            hs.toan = float.Parse(Console.ReadLine());
            Console.Write("Nhap diem van: ");
            hs.van = float.Parse(Console.ReadLine());
        }
        static bool KTHopLe(HocSinh hs)
        {
            if (hs.toan < 0 || hs.van < 0 || hs.toan > 10 || hs.van > 10)
                return false;
            return true;
        }
        static float TinhDTB(HocSinh hs)
        {
            float TB = (hs.toan + hs.van) / 2;
            return TB;
        }
        static void Xuat(HocSinh hs)
        {
            Console.WriteLine("Thong tin hoc sinh:");
            Console.WriteLine("Ho ten: " + hs.hoten);
            Console.WriteLine("Diem toan: "+hs.toan);
            Console.WriteLine("Diem van: " + hs.van);
            Console.WriteLine("Diem trung binh: " + TinhDTB(hs));
        }
        static void Main(string[] args)
        {
            HocSinh hs;
            Nhap(out hs);
            while (KTHopLe(hs) == false)
            {
                Console.WriteLine("Nhap diem khong hop le.");
                Nhap(out hs);
            }
            Xuat(hs);
        }
    }
}
8: Nhập vào 2 phân số và tính toán

Mã:
namespace PhanSo
{
    class Program
    {
        struct PhanSo
        {
            public int tu, mau;
        };
        static void Nhap (out PhanSo ps)
        {
            Console.Write("Nhap tu: ");
            ps.tu = int.Parse(Console.ReadLine());
            Console.Write("Nhap mau: ");
            ps.mau = int.Parse(Console.ReadLine());
        }
        static bool KTra(PhanSo ps)
        {
            if (ps.mau == 0)
                return false;
            return true;
        } 
        static int USCLN(PhanSo ps)
        {
            ps.tu = Math.Abs(ps.tu);
            ps.mau = Math.Abs(ps.mau);
            while (ps.tu != ps.mau)
            {
                if (ps.tu > ps.mau)
                    ps.tu = ps.tu - ps.mau;
                else
                    ps.mau = ps.mau - ps.tu;
            }
            return ps.tu;
        }
        static void RutGon(ref PhanSo ps)
        {
            int temp = USCLN(ps);
            ps.tu = ps.tu / temp;
            ps.mau = ps.mau / temp;


        }
        static void Xuat(PhanSo ps)
        {
            Console.WriteLine("Phan so : "+ps.tu+"/"+ps.mau);
          
        }
        static void Main(string[] args)
        {
            PhanSo ps;
            Nhap(out ps);
            Console.WriteLine("Phan so vua nhap la: ");
            Xuat(ps);
            Console.WriteLine("Phan so sau khi rut gon la: ");
            RutGon(ref ps);
            Xuat(ps);
        }
    }
}
9: Nhập vào 2 tạo độ bất kỳ tính khoảng các của 2 tạo độ đó

Mã:
namespace ToaDo
{
    class Program
    {
        struct ToaDo
        {
            public float x, y;
            public char ten;
        };
        struct TamGiac
        {
            public ToaDo A, B, C;
        }
        static void nhap(out ToaDo td)
        {
            Console.Write("Nhap toa do x: ");
            td.x = float.Parse(Console.ReadLine());
            Console.Write("Nhap toa do y: ");
            td.y = float.Parse(Console.ReadLine());
            Console.Write("Nhap ten toa do: ");
            td.ten = char.Parse(Console.ReadLine());
        }
        static void Xuat(ToaDo td)
        {
            Console.WriteLine("{0}({1},{2})", td.ten, td.x, td.y);
        }
        static void NhapTG(out TamGiac tg)
        {
            nhap(out tg.A);
            nhap(out tg.B);
            nhap(out tg.C);
        }
        static void XuatTG(TamGiac tg)
        {
            Console.Write("Toa do dinh ");
            Xuat(tg.A);
            Console.Write("Toa do dinh ");
            Xuat(tg.B);
            Console.Write("Toa do dinh ");
            Xuat(tg.C);
        }
        //static double KhoangCach(ToaDo td1, ToaDo td2)
        //{
        //    double kq;
        //    kq = Math.Sqrt(((td1.x - td2.x) * (td1.x - td2.x) + (td1.y - td2.y) * (td1.y - td2.y)));
        //    return kq;
        //}
        //static double TinhCV(ToaDo td1, ToaDo td2, ToaDo td3)
        //{
        //    double kq;
        //    kq = KhoangCach(td1,
        //}
        static void Main(string[] args)
        {
            ToaDo td;
            TamGiac tg;
            NhapTG(out tg);
            XuatTG(tg);
        }
    }
}
10: Chạy chữ khi chạm vào thành màn hình sẽ quay lại

Mã:
namespace baitap15{
    class ChayChu
    {
        static void ChayChuQuaLai(string s)
        {
            int x = 0;
            int y = 0;
            int huong = 1;
            while (!Console.KeyAvailable)  //nhan phim bat ki thi dung chuoi
            {
                if (huong == 1)
                {
                    Console.Clear();
                    Console.SetCursorPosition(x++, y); //phuong thuc dat vi tri chuot
                    Console.Write(s);
                    System.Threading.Thread.Sleep(50);  //dung thoi gian
                    if (x == (Console.WindowWidth - s.Length))
                        huong = 3;
                }
                if (huong == 2)
                {
                    Console.Clear();
                    Console.SetCursorPosition(x--, y); //phuong thuc dat vi tri chuot
                    Console.Write(s);
                    System.Threading.Thread.Sleep(50);  //dung thoi gian
                    if (x == 0)
                        huong = 4;
                }
                if (huong == 3)
                {
                    Console.Clear();
                    Console.SetCursorPosition(x, y++); //phuong thuc dat vi tri chuot
                    Console.Write(s);
                    System.Threading.Thread.Sleep(50);  //dung thoi gian
                    if (y == (Console.WindowHeight - s.Length))
                        huong = 2;
                }
                if (huong == 4)
                {
                    Console.Clear();
                    Console.SetCursorPosition(x, y--); //phuong thuc dat vi tri chuot
                    Console.Write(s);
                    System.Threading.Thread.Sleep(50);  //dung thoi gian
                    if (y == 0)
                        huong = 1;
                }
            }
        }
        static void Main(string[] args)
        {   
            Console.Write("Nhap chuoi: ");
            string s = Console.ReadLine();
            Console.BackgroundColor = ConsoleColor.DarkGray;
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            ChayChuQuaLai(s);
        }
    }
}
11: Mảng 1 chiều C#
Mã:
namespace Mang1Chieu{
    class Program
    {
        static void NhapMang(int[] a, int n)
        {
            for (int i = 0; i < n; i++)
            {
                Console.Write("Nhap phan tu thu"+i+":");
                a[i] = int.Parse(Console.ReadLine());
            }
        }
        static void NhapNN(out int[] a, out int n)
        {
            Random r = new Random();
            n = r.Next(1, 20);
            a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = r.Next(-100, 200);
            }
        }
        static void Xuat(int[] a, int n)
        {
            for (int i = 0; i < n; i++)
            {
                Console.Write(a[i] + "\t");
            }
            Console.WriteLine();
        }
        static void Xuat_Foreach(int[] a)
        {
            foreach (int x in a)
                Console.Write(x + "\t");
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            int[] a;
            int n,m;
            Console.Write("Nhap so phan tu cua mang: ");
            n = int.Parse(Console.ReadLine());
            a = new int[n];
            NhapMang(a, n);
            Xuat(a, n);
            int[] b;
            NhapNN(out b, out m);
            Xuat_Foreach(b);
        }
    }
}
12: Mảng kí tự

Mã:
namespace Mangkytu
{
    class CMangKT
    {
        //Thuoc tinh
        private char[] a;
        private int n;


        //Tao Property
        public int N
        {
            get { return n; }
            set { n = value; }
        }
        public char[] A
        {
            get { return a; }
            set { a = value; }
        }
        //Constructor
        public CMangKT() { } //khong co tham so
        public CMangKT(char[] b, int x) //Co tham so
        {
            n = x;
            a = new char[n]; //Cap vung nho cho mang a
            Array.Copy(b, a, n); //Copy tu mang b sang mang a vs n ky tu
        }
        public CMangKT(CMangKT m1) // Sao chep
        {
            n = m1.n;
            a = new char[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = m1.a[i];
            }
        }
        //Nhap tu ban phim
        public void Nhap()
        {
            Console.Write("Nhap so pt n:");
            n = int.Parse(Console.ReadLine());
            a=new char[n];
            for (int i = 0; i < n; i++)
            {
                Console.Write("Nhap ky tu thu"+i+":");
                a[i] = char.Parse(Console.ReadLine());
            }
        }
        //Nhap ngau nhien
        public void NhapNN()
        {
            Random r = new Random();
            n = r.Next(1, 20);
            a = new char[n];
            int x;
            for (int i = 0; i < n; i++)
            {
                x = r.Next(65, 91);//x co gia tri tu 65 den 91
                a[i] = Convert.ToChar(x); // hoac a[i] = Convert.ToChar.Next(65,91));
            }
        }
        public void Xuat()
        {
            foreach (char x in a)
                Console.Write(x + "\t");
            Console.WriteLine();
        }


    }
}

namespace Mangkytu
{
    class Program
    {
        static void Main(string[] args)
        {
            CMangKT m1 = new CMangKT();
            m1.Nhap();
            Console.WriteLine("Mang thu nhat");
            m1.Xuat();
            CMangKT m2 = new CMangKT();
            m2.NhapNN();
            Console.WriteLine("Mang thu hai");
            m2.Xuat();


        }
    }
}

ConversionConversion EmoticonEmoticon

:)
:(
=(
^_^
:D
=D
=)D
|o|
@@,
;)
:-bd
:-d
:p
:ng