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: vuivengay on Thứ Năm, 21 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 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: vuivengay 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: vuivengay 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: vuivengay 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: vuivengay


//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: vuivengay


/*---  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: vuivengay 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 →