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

Người đăng: chisenhungsuutam 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: chisenhungsuutam 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: chisenhungsuutam 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: chisenhungsuutam 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

Code C#: Nhập vào độ dài 3 đoạn. Kiểm tra 3 đoạn đó có tạo thành tam giác không? Và thuộc loại tam giác nào.

Người đăng: chisenhungsuutam


//Yêu cầu: Viết chương trình với loại ứng dụng Console Application, nhập vào 3 số. Kiểm tra xem đó có phải là 3 cạnh của một tam giác không. Nếu phải thì xem đó là loại tam giác gì.
class Program{
        static void Main(){
            floatx, y, z;
            Console.Write("Nhap chieu dai canh x: ");
            x = float.Parse(Console.ReadLine());
            Console.Write("Nhap chieu dai canh y: ");
            y = float.Parse(Console.ReadLine());
            Console.Write("Nhap chieu dai canh z: ");
            z = float.Parse(Console.ReadLine());
            if(x <= 0 || y <= 0 || z <= 0 || x + y <= z || y + z <= x || x + z <= y)
                Console.WriteLine("Ban nhap sai");
            else{
                if((x == y && x * x * 2 == z * z) || (x == z && x * x * 2 == y * y) || (y == z && y * y * 2 == x * x))
                    Console.WriteLine("Tam giac vuong can");
                else{
                    if(x == y && y == z && z == x)
                        Console.WriteLine("Tam giac deu");
                    else{
                        if (x == y || y == z || z == x)
                            Console.WriteLine("Tam giac can");
                        else
                            Console.WriteLine("Tam giac thuong");
                    }
                }
            }
            Console.ReadLine();
        }
}
More about

Code C#: Nhập vào số nguyên N có 4 chữ số. Tìm chữ số lớn thứ nhì trong 4 chữ số.

Người đăng: chisenhungsuutam on Thứ Tư, 20 tháng 3, 2013


//Yêu cầu: Viết chương trình với loại ứng dụng Console Application, nhập vào số nguyên n có 4 chữ số. Hãy tìm chữ số lớn thứ nhì.
Ví dụ: n = 1895. n có 4 chữ số 1,8,9,5. Chữ số lớn thứ nhì: 8.
class Program{
        static void Main(){           
            intn;
            int[] a = new int[4];
            do{
                Console.Write("Nhập số n có 4 chữ số: ");
                n = int.Parse(Console.ReadLine());
            } while(n < 1000 || n > 9999);           
            for(int i = 0; i < 4; i++) {
                a[i] = n % 10;
                n = n / 10;
            }
            for(int i = 0; i < 3; i++)
                for(int j = i; j < 4;j++)
                    if(a[i] <= a[j]){
                        int tg = a[i];
                        a[i] = a[j];
                        a[j] = tg;
                    }            
            Console.WriteLine("So lon thu nhi trong 3 so la: {0}", a[1]);   
            Console.ReadLine();
        }
}
More about

Code VBA: Câu lệnh tạo Bảng trong Access (Microsoft Access SQL)

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


Ví dụ 1: Tạo bảng mới có tên là SINHVIEN

Sub CreateTable1()
    Dim dbs As Database
    Set dbs = OpenDatabase("QLSV.mdb")
 
   'Tạo bảng mới với 2 trường (field)
    dbs.Execute "CREATE TABLE SINHVIEN (MaSV CHAR, TenSV CHAR);"

    dbs.Close

End Sub

Ví dụ 2: Tạo 1 bảng mới có tên là SINHVIEN với 2 trường có kiểu dữ liệu text, 1 trường có kiểu dữ liệu Date/Time, một chỉ mục duy nhất với cả 3 trường.


Sub CreateTable2()

    Dim dbs As Database
    Set dbs = OpenDatabase("QLSV.mdb")
   
    dbs.Execute "CREATE TABLE SINHVIEN " _
        & "(MaSV CHAR, TenSV CHAR, " _
        & "NgaySinh DATETIME, " _
        & "CONSTRAINT SinhVienConstraint UNIQUE " _
        & "(MaSV , TenSV, NgaySinh));"

    dbs.Close

End Sub

More about

Code C#: Nạp chồng toán tử (operator) trong C#

Người đăng: chisenhungsuutam on Thứ Hai, 18 tháng 3, 2013


//Ví dụ minh họa nạp chồng toán tử (operator) ==, !=, + , - trong C# sử dụng implicit và explicit
using System;
public class Fraction{
publicFraction(int numerator, int denominator){
     Console.WriteLine("In Fraction Constructor(int, int)");
     this.numerator=numerator;
     this.denominator=denominator;
}
publicFraction(int wholeNumber){
     Console.WriteLine("In Fraction Constructor(int)");
     numerator = wholeNumber;
     denominator = 1;
}
public static implicit operator Fraction(inttheInt){
     System.Console.WriteLine("In implicit conversion to Fraction");
     return new Fraction(theInt);
}
public static explicit operator int(Fraction theFraction){
     System.Console.WriteLine("In explicit conversion to int");
     returntheFraction.numerator / theFraction.denominator;
}
public static bool operator==(Fraction lhs, Fraction rhs){
     Console.WriteLine("In operator ==");
     if(lhs.denominator == rhs.denominator &&
         lhs.numerator == rhs.numerator)
     {
        return true;
     }
     // code here to handle unlike fractions
     return false;
}
public static bool operator !=(Fraction lhs, Fraction rhs){
     Console.WriteLine("In operator !=");
     return!(lhs==rhs);
}
public override bool Equals(object o){
     Console.WriteLine("In method Equals");
     if (! (o is Fraction) ){
        return false;
     }
     return this == (Fraction) o;
}
public static Fraction operator+(Fraction lhs, Fraction rhs){
     Console.WriteLine("In operator+");
     if(lhs.denominator == rhs.denominator){
        return new Fraction(lhs.numerator+rhs.numerator,
         lhs.denominator);
     }
     // simplistic solution for unlike fractions
     // 1/2 + 3/4 == (1*4) + (3*2) / (2*4) == 10/8
     intfirstProduct = lhs.numerator * rhs.denominator;
     intsecondProduct = rhs.numerator * lhs.denominator;
     return new Fraction(
        firstProduct + secondProduct,
        lhs.denominator * rhs.denominator
        );
}
public override stringToString(){
     String s = numerator.ToString( ) + "/" +
        denominator.ToString( );
     return s;
}
private int numerator;
private int denominator;
}
public class Tester{
static void Main(){
     //implicit conversion to Fraction
     Fraction f1 = newFraction(3);
     Console.WriteLine("f1: {0}", f1.ToString( ));
     Fraction f2 = newFraction(2,4);
     Console.WriteLine("f2: {0}", f2.ToString( ));
     Fraction f3 = f1 + f2;
     Console.WriteLine("f1 + f2 = f3: {0}", f3.ToString());
     Fraction f4 = f3 + 5;
     Console.WriteLine("f3 + 5 = f4: {0}", f4.ToString());
     Fraction f5 = newFraction(2,4);
     if (f5 == f2){
        Console.WriteLine("F5: {0} == F2: {1}", f5.ToString(),
       f2.ToString());
     }
     int k = (int)f4; //explicit conversion to int
     Console.WriteLine("int: F5 = {0}", k.ToString());
}
}
More about

Code C-C++: Bài toán kiểm tra năm nhuận, với năm nhập từ bàn phím.

Người đăng: chisenhungsuutam


//Yêu cầu: Nhập vào năm, in ra màn hình thông báo năm nhuận hay không.
//Code C++:
#include <iostream.h>
void main(){
          int nam;
          cout << “Nhap nam = “ ; cin >> nam ;
                    /*---Điều kiện để tính năm nhuận là năm chia hết cho 4 và không chia hết cho 100     hoặc năm chia hết cho 400 -----&/
                    if ((nam%4 == 0 && nam%100 !=0) || nam%400 == 0)
                              cout << nam << "la nam nhuan” ;
                    else
                              cout << nam << "la nam khong nhuan” ;
}
More about

Code C#: Nhập vào số nguyên n có 8 chữ số. Tính tổng các chữ số, trung bình cộng của tổng

Người đăng: chisenhungsuutam


/*---  Yêu cầu: Nhập vào số nguyên n có 8 chữ số. Hãy tìm:
        - Giá trị tổng của các chữ số
        - Giá trị hàng đơn vị của tổng
        - Trung bình cộng của tổng   -------*/
class Program{
        static void Main(){
            intn;
            do{
                Console.Write("Nhap so n co 8 chu so: ");
                n = int.Parse(Console.ReadLine());
            }while(n<10000000 || n>99999999);
            //Tinh tong cua cac chu so
            ints = 0;
            intdu;
            intsbd = n;
            while(n != 0) {
                du = n % 10;
                s += du;
                n /= 10;
            }
            //Tinh gia tri hang don vi cua tong
            inthdv;
            hdv = s % 10;
            //Trung binh cong cua tong
            floattbc;
            tbc = (float) s / 8;
            Console.WriteLine("Tong cac chu so cua {0} la: {1}",sbd,s);
            Console.WriteLine("Gia tri hang don vi cua {0} la: {1}",s,hdv);
            Console.WriteLine("Trung binh cong cua {0} la: {1}",s,tbc);
            Console.ReadLine();
        }
}
More about

Code C#: Kiểm tra số N (nguyên) nhập từ bàn phím có phải là số nguyên tố không

Người đăng: chisenhungsuutam on Thứ Bảy, 16 tháng 3, 2013


/*Yêu cầu: Viết chương trình với loại ứng dụng Console Application, nhập số N (nguyên) từ bàn phím. Kiểm tra N có phải là số Nguyên tố không. */
class Program{
        static void Main(){
            intn;
            boolok = true;
            Console.Write("Nhap so N can kiem tra: ");
            n = int.Parse(Console.ReadLine());
            if(n <= 1) ok = false;
            else{
                if(n == 2) ok = true;
                for(int i = 2; i <= Math.Sqrt(n); i++)
                    if(n % i == 0) ok = false;
                    elseok = true;                   
            }
            if(ok == true) Console.WriteLine("{0} la so nguyen to", n);
            elseConsole.WriteLine("{0} khong la so nguyen to",n);
            Console.ReadLine();
        }
    }
More about