Code C#: MultiCast Delegate - Cơ chế ủy quyền (Delegate) đa phương thức trong C#

Người đăng: culaoxanh88 on Thứ Năm, 4 tháng 4, 2013



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyMulticastDelegate{
    public delegate void MulticastDelegate(int x, int y);

    public class Vidu2{
        public static void Cong(int x, int y) {
            Console.WriteLine("Ban dang goi phuong thuc Cong()");
            Console.WriteLine("{0} + {1} = {2}", x,y,x+y);
        }
        public static void Nhan(int x, int y) {
            Console.WriteLine("Ban dang goi phuong thuc Nhan()");
            Console.WriteLine("{0} x {1} = {2}", x, y, x * y);
        }
    }
    
    class Program{
        static void Main(string[] args){
            MulticastDelegate del = new MulticastDelegate(Vidu2.Cong);
            del += new MulticastDelegate(Vidu2.Nhan);
            Console.WriteLine("Goi dong thoi 2 phuong thuc Cong va Nhan\n\n");
            del(7, 6);

            del -= new MulticastDelegate(Vidu2.Cong);
            Console.WriteLine("\n\n****Da xoa phuong thuc Cong, chi goi phuong thuc Nhan****\n\n");
            del(4, 5);
            Console.ReadLine();
        }
    }
}


More about →

Code C#: Cơ chế ủy quyền (Delegate) trong C# (P.1)

Người đăng: culaoxanh88



Ví dụ 1: Khai báo cơ chế ủy quyền (delegate) trong C# gọi các phương thức thực thi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegates{
    public delegate int MyDelegate(int x, int y);
    
    public class Vidu1{
        public static int Cong(int x, int y){
            return x + y;
        }
        public static int Nhan(int x, int y) {
            return x * y;
        }
    }
    
    class Program{
        static void Main(string[] args){
            MyDelegate del1 = new MyDelegate(Vidu1.Cong);
            int KetquaCong = del1(5, 5);
            Console.WriteLine("5 + 5 = {0}", KetquaCong);
            MyDelegate del2 = new MyDelegate(Vidu1.Nhan);
            int KetquaNhan = del2(5, 5);
            Console.WriteLine("5 x 5 = {0}", KetquaNhan);
            Console.ReadLine();
        }
    }
}


More about →

Code Pascal: Cấu trúc lặp WHILE...DO

Người đăng: culaoxanh88 on Thứ Ba, 2 tháng 4, 2013


Viết chương trình tính trung bình của n số (nhập từ bàn phím)
Program Trung_binh_day_so ;
VAR
                        n, count : Integer ;
                        x, sum, average : real ;
BEGIN
                        count := 1 ;
                        sum   := 0 ;
            Write (' Nhập n = ');      readln (n) ;
                        WHILE  count  < n+1  DO
                                    BEGIN
                                                Write (' Nhập giá trị thứ' , count,' của x = ' ) ;
                                                readln (x) ;
                                                sum  := sum + x ;
                                                count := count + 1 ;
                                    END ;
                        average:=  sum/n ;
                        Writeln (' Trung bình là ='  , average : 10 : 3 ) ;
                        Writeln (' Nhấn Enter để thoát ...' ) ;
                        Readln ;
END. 
More about →

Code Pascal: Cấu trúc rẽ nhánh IF..THEN..ELSE (cơ bản)

Người đăng: culaoxanh88 on Thứ Sáu, 29 tháng 3, 2013


//Tính căn bậc 2 của một số
PROGRAM Tinh_can_bac_hai ;
VAR  
            a : Real ;
BEGIN
            Write ( Nhập số a =  );
            Readln(a) ;
            IF a < 0 THEN Write (' a : 10 : 2 , là số âm nên không lấy căn được !!! ')
            ELSE
                  Writeln (' Căn số bậc 2 của , a : 2 : 2 , la , SQRT(a) :10 : 3 ');
            Readln; {Dừng màn hình để xem kết quả}
END. 
More about →

Code C#: Xây dựng lớp trừu tượng và cách ghi đè phương thức trừu tượng ở lớp kế thừa

Người đăng: culaoxanh88 on Thứ Ba, 26 tháng 3, 2013


Xây dựng lớp trừu tượng Hình có thuộc tính PI, phương thức trừu tượng: TinhDienTich và TinhTheTich.
- Xây dựng lớp HinhTron kế thừa từ lớp Hinh, cài đặt phương thức ảo để tính diện tích, thể tích của hình tròn.
- Xây dựng lớp HinhLapPhuong kế thừa từ lớp Hinh, cài đặt phương thức ảo để tính diện tích, tính thể tích của hình lập phương.
namespaceLop_TruuTuong{
    abstract public class Hinh {
        protecteddouble PI = 3.14159;
        abstractpublic doubleTinhDienTich();
        abstractpublic doubleTinhTheTich();
    }
    public class HinhTron : Hinh {
        privatedouble bankinh;
        publicHinhTron(double r){
            this.bankinh = r;
        }
        public override doubleTinhDienTich(){
            returnPI * bankinh * bankinh;
        }
        public override doubleTinhTheTich() {
            return0;
        }
    }
    public class HinhLapPhuong : Hinh {
        privatedouble a, b, c;
        publicHinhLapPhuong(double a, double b, double c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
        public override doubleTinhDienTich() {
            return2*(a*b + b*c + c*a);
        }
        public override doubleTinhTheTich() {
            returna * b * c;
        }
    }
    class Program
    {       
        public static void Main()
        {
            HinhTronht1 = new HinhTron(5);
            HinhLapPhuonghlp1 = new HinhLapPhuong(2, 3, 4);
            Console.WriteLine("Dien tich hinh tron: {0}. The tich hinh tron: {1}",ht1.TinhDienTich(),ht1.TinhTheTich());
            Console.WriteLine("Dien tich hinh lap phuong: {0}. The tich hinh lap phuong: {1}",hlp1.TinhDienTich(),hlp1.TinhTheTich());
            Console.ReadLine();           
        }
    }
}
More about →

Code C#: Bài tập cơ bản về LỚP (CLASS) - Thiết kế lớp SINH VIÊN

Người đăng: culaoxanh88 on Chủ Nhật, 24 tháng 3, 2013


//Yêu cầu: Thiết kế lớp sinh viên bao gồm các thuộc tính:
- Họ tên, tuổi, điểm toán, điểm văn, điểm trung bình của 1 sinh viên. 
- Khai báo mảng sử dụng lớp sinh viên trên để nhập thông tin cho n sinh viên (n nhập từ bàn phím). 
- Tính điểm trung bình và in ra màn hình danh sách các sinh viên đó.
namespaceBaiThucHanhLop{      
        class Student{
            privatestring _hoTen;
            privateint _tuoi;
            privatedouble _diemToan;
            privatedouble _diemVan;
            privatedouble _dtb;
            //Hàm khởi tạo không có tham số
            publicStudent(){
                HoTen = "";
                DiemVan = 0;
                DiemToan = 0;
                Dtb = 0;
            }
            //Các phương thức Properties để get/set giá trị cho các thuộc tính
            publicstring HoTen{
                get{ return _hoTen; }
                set{ _hoTen = value; }
            }
            publicint Tuoi{
                get{ return _tuoi; }
                set{ _tuoi = value; }
            }
            publicdouble DiemToan{
                get{ return _diemToan; }
                set{ _diemToan = value; }
            }
            publicdouble DiemVan{
                get{ return _diemVan; }
                set{ _diemVan = value; }
            }
            publicdouble Dtb{
                get{ return Math.Round(((DiemToan + DiemVan) / 2), 2); }
                set{ _dtb = value; }
            }                    
            //Các phương thức nhập/xuất dữ liệu                    

            publicvoid nhap()
            {
                Console.Write(" \t -Nhap ho ten:");
                HoTen = Console.ReadLine();
                Console.Write(" \t -Nhap diem toan:");
                Doubletemp;
                temp = double.Parse(Console.ReadLine());
                if(temp > 10 || temp < 0)
                {
                    Console.WriteLine(" \t !!! Diem phai nam trong khoang 0 -> 10");
                    Console.Write(" \t -Nhap lai diem toan:");
                    temp = double.Parse(Console.ReadLine());
                }
                DiemToan = temp;

                Console.Write(" \t -Nhap diem van:");
                temp = double.Parse(Console.ReadLine());
                if(temp > 10 || temp < 0)
                {
                    Console.WriteLine(" \t -Diem phai nam trong khoang 0 -> 10");
                    Console.Write(" \t -Nhap lai diem Van:");
                    temp = double.Parse(Console.ReadLine());
                }
                DiemVan = temp;

            }
            publicvoid xuat(){
                Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}", HoTen, DiemToan, DiemVan, Dtb);
            }
        }

    class Program{       
        public static void Main(){
            intn;
            Console.Write(" Nhap so luong hoc sinh: ");
            n = int.Parse(Console.ReadLine());

            Student[] _arrStudent = new Student[n];
            for(int i = 0; i < n; i++){
                Console.WriteLine(" Nhap thong tin sinh vien thu: " + (i + 1).ToString());
                _arrStudent[i] = new Student();
                _arrStudent[i].nhap();
            }

            Console.WriteLine(" Danh sach hoc sinh: ");
            Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}", "Ho Ten", "Diem Toan", "Diem Van", "DTB");

            for(int i = 0; i < n; i++){
                _arrStudent[i].xuat();
            }
            Console.ReadLine();           
        }
    }
}
More about →

Code C#: Tính tổng của n số (nhập từ bàn phím). Tìm giá trị lớn nhất, nhỏ nhất trong n số đó. Sử dụng MẢNG

Người đăng: culaoxanh88 on Thứ Năm, 21 tháng 3, 2013


//Yêu cầu: Viết chương trình Console Application. Tính tổng của n số nhập từ bàn phím. Tìm giá trị lớn nhất, giá trị nhỏ nhất trong n số đó. 
//Cách 1: Sử dụng mảng lưu trữ

class Program{
        static void Main(){
            intn,s=0;
            int[] a;
            Console.Write("Nhap so can tinh tong: ");
            n = int.Parse(Console.ReadLine());
            a = newint[n];
            for (inti = 0; i < n; i++){
                Console.Write("Nhap phan tu thu {0}: ",i+1);
                a[i] = int.Parse(Console.ReadLine());
            }
            for(int i = 0; i < n; i++){
                s += a[i];
            }
            for(int i = 0; i < n - 1; i++)
                for(int j = i; j < n;j++ )
                    if(a[i] <= a[j]) {
                        int tg = a[i];
                        a[i] = a[j];
                        a[j] = tg;
                    }
            Console.WriteLine("Tong cua {0} so nhap tu ban phim la: {1}", n, s);
            Console.WriteLine("Gia tri lon nhat: {0}",a[0]);
            Console.WriteLine("Gia tri nho nhat: {0}",a[n-1]);
            Console.ReadLine();
        }
}
More about →