Thuchanh07

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down

Thuchanh07

Bài gửi  levientrinh_10TH on Sun Jan 09, 2011 6:02 pm

//Xây dựng class CComplex
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Thuchanh07
{
class CComplex
{
float m_imaginary;
float m_real;

public float Real
{
get { return m_real; }
set { m_real = value; }
}
public float Imaginary
{
get { return m_imaginary; }
set { m_imaginary = value; }
}

public CComplex()
{
m_imaginary = 0;
m_real = 0;
}
public CComplex(float real)
{
m_real = real;
m_imaginary = 0;
}
public CComplex(float Real, float Imaginary)
{
m_real = Real;
m_imaginary = Imaginary;
}
public static CComplex operator +(CComplex lhs, CComplex rhs)
{
CComplex result = new CComplex();
result.m_real = lhs.m_real + rhs.m_real;
result.m_imaginary = lhs.m_imaginary + rhs.m_imaginary;
return result;
}
public static CComplex operator -(CComplex lhs, CComplex rhs)
{
CComplex result = new CComplex();
result.m_real = lhs.m_real - rhs.m_real;
result.m_imaginary = lhs.m_imaginary - rhs.m_imaginary;
return result;
}
public static CComplex operator *(CComplex lhs, CComplex rhs)
{
CComplex result = new CComplex();
result.m_real = lhs.m_real*rhs.m_real-lhs.m_imaginary*rhs.m_imaginary;
result.m_imaginary = lhs.m_real*rhs.m_imaginary+lhs.m_imaginary*rhs.m_real;
return result;
}
public static bool operator ==(CComplex lhs, CComplex rhs)
{
if (lhs.m_real == rhs.m_real && lhs.m_imaginary == rhs.m_imaginary)
return true;
else
return false;
}
public static bool operator !=(CComplex lhs, CComplex rhs)
{
if (lhs.m_real != rhs.m_real || lhs.m_imaginary != rhs.m_imaginary)
return true;
else
return false;
}
public override string ToString()
{
return String.Copy(m_real + " + " + m_imaginary + "i");
}
}
}

//Xây dựng class CFraction (toán tử ++, -- và - tôi sẽ post lên sau)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Thuchanh07
{
class CFraction
{
float m_Den;//Mau so
float m_Num;//Tu so

public float Num
{
get { return m_Num; }
set { m_Num = value; }
}
public float Den
{
get { return m_Den; }
set { m_Den = value; }
}
public CFraction()
{
m_Num = 0;
m_Den = 1;
}
public CFraction(int num, int den)
{
m_Num = num;
m_Den = den;
}
public CFraction(int num)
{
m_Num = num;
m_Den = 1;
}
public CFraction(CFraction f)
{
m_Num = f.m_Num;
m_Den = f.m_Den;
}
public CFraction InverseFraction(CFraction f)
{
CFraction result = new CFraction();
result.m_Num = f.m_Den;
result.m_Den = f.m_Num;
return result;
}
public float UCLN()
{
float tu = m_Num;
float mau = m_Den;
while (tu != mau)
{
if (tu > mau)
tu = tu - mau;
else
mau = mau - tu;
}
return tu;
}
public static CFraction ReduceFraction(CFraction f)
{
CFraction result = new CFraction();
result.m_Num = f.m_Num / f.UCLN();
result.m_Den = f.m_Den / f.UCLN();
return result;
}
public static CFraction operator +(CFraction lhs, CFraction rhs)
{
CFraction result = new CFraction();
result.m_Num = lhs.m_Num * rhs.m_Den + rhs.m_Num * lhs.m_Den;
result.m_Den = lhs.m_Den * rhs.m_Den;
CFraction thuc = new CFraction();
thuc = ReduceFraction(result);
return thuc;
}
public static CFraction operator -(CFraction lhs, CFraction rhs)
{
CFraction result = new CFraction();
result.m_Num = lhs.m_Num * rhs.m_Den - rhs.m_Num * lhs.m_Den;
result.m_Den = lhs.m_Den * rhs.m_Den;
CFraction thuc = new CFraction();
thuc = ReduceFraction(result);
return thuc;
}
public static CFraction operator *(CFraction lhs, CFraction rhs)
{
CFraction result = new CFraction();
result.m_Num = lhs.m_Num * rhs.m_Num;
result.m_Den = lhs.m_Den * rhs.m_Den;
CFraction thuc = new CFraction();
thuc = ReduceFraction(result);
return thuc;
}
public static CFraction operator /(CFraction lhs, CFraction rhs)
{
CFraction result = new CFraction();
result.m_Num = lhs.m_Num * rhs.m_Den;
result.m_Den = lhs.m_Den * rhs.m_Num;
CFraction thuc = new CFraction();
thuc = ReduceFraction(result);
return thuc;
}
public static bool operator >(CFraction lhs, CFraction rhs)
{
return (lhs.m_Num / lhs.m_Den) > (rhs.m_Num / rhs.m_Den);
}
public static bool operator <(CFraction lhs, CFraction rhs)
{
return (lhs.m_Num / lhs.m_Den) < (rhs.m_Num / rhs.m_Den);
}
public static bool operator ==(CFraction lhs, CFraction rhs)
{
return (lhs.m_Num / lhs.m_Den) == (rhs.m_Num / rhs.m_Den);
}
public static bool operator !=(CFraction lhs, CFraction rhs)
{
return (lhs.m_Num / lhs.m_Den) != (rhs.m_Num / rhs.m_Den);
}
public static bool operator >=(CFraction lhs, CFraction rhs)
{
return (lhs.m_Num / lhs.m_Den) >= (rhs.m_Num / rhs.m_Den);
}
public static bool operator <=(CFraction lhs, CFraction rhs)
{
return (lhs.m_Num / lhs.m_Den) <= (rhs.m_Num / rhs.m_Den);
}

}
}

levientrinh_10TH

Tổng số bài gửi : 30
Join date : 05/01/2011

Xem lý lịch thành viên

Về Đầu Trang Go down

Class CFraction operator ++, --, -

Bài gửi  levientrinh_10TH on Tue Jan 11, 2011 2:24 am

public static CFraction operator ++(CFraction f)
{
f.m_Num = f.m_Num + f.m_Den;
return f;
}
public static CFraction operator --(CFraction f)
{
f.m_Num = f.m_Num - f.m_Den;
return f;
}
public static CFraction operator -(CFraction f)
{
CFraction result = new CFraction();
result.m_Num = -f.m_Num;
result.m_Den = f.m_Den;
return result;
}
public override string ToString()
{
return String.Format(m_Num + "/" + m_Den);
}

levientrinh_10TH

Tổng số bài gửi : 30
Join date : 05/01/2011

Xem lý lịch thành viên

Về Đầu Trang Go down

Re: Thuchanh07

Bài gửi  R.Keane on Fri Mar 25, 2011 8:44 am

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

namespace BTVNCFraction
{
    class CFraction
    {
        //Field
        private int m_Tuso;
        private int m_Mauso;

        //Property
        public int TUSO
        {
            get
            {
                return m_Tuso;
            }
            set
            {
                m_Tuso = value;
            }
        }
        public int MAUSO
        {
            get
            {
                return m_Mauso;
            }
            set
            {
                    m_Mauso = value;
            }
        }

        //Contructor
     
        public CFraction()
        {
            m_Mauso = 1;
            m_Tuso = 1;
        }
        public CFraction(int num, int den)
        {
            this.TUSO = num;
            this.MAUSO = den;
        }
        public CFraction(int num)
        {
            this.MAUSO = num;
        }
     
        public CFraction(CFraction f)
        {
            this.TUSO = f.TUSO;
            this.MAUSO = f.MAUSO;
        }


       
        //Methods
#region methods

        //nghich dao phan so
        public static CFraction InverseFraction(CFraction f)
        {
        CFraction c = new CFraction();
        int tmp;
        tmp = c.TUSO;
        c.TUSO = c.MAUSO;
        c.MAUSO = tmp;
        return f;
      }
        public int UCLN(int a, int b)
        {
            a = Math.Abs(TUSO);
            b = Math.Abs(MAUSO);
            while (a != b && b != 0 && a != 0)
            {
                if (a > b)
                    a = a - b;
                else
                    b = b - a;
            }
            return a;
        }

        // rut gon phan so
        public static CFraction RecudeFraction(CFraction f)
        {
            CFraction c = new CFraction();
            int uoc = c.UCLN(c.TUSO,c.MAUSO);
            if (uoc != 0)
            { 
                c.TUSO = (f.TUSO /uoc );
                c.MAUSO = (f.MAUSO / uoc);
            }
            else
            {
                c.TUSO = c.TUSO;
                c.MAUSO = c.MAUSO;
               
            }
            return c;
        }

        //nap chong toan tu
       
        public static CFraction operator+(CFraction lhs, CFraction rhs)
        {
            CFraction c= new CFraction();
            c.TUSO= (lhs.TUSO*rhs.MAUSO)+(lhs.MAUSO*rhs.TUSO);
            c.MAUSO= lhs.MAUSO*rhs.MAUSO;
            RecudeFraction(c);
            return c; 
        }

        public static CFraction operator-(CFraction lhs, CFraction rhs)
        {
            CFraction c= new CFraction();
            c.TUSO= (lhs.TUSO*rhs.MAUSO)-(lhs.MAUSO*rhs.TUSO);
            c.MAUSO= lhs.MAUSO*rhs.MAUSO;
            RecudeFraction(c);
            return c;         
        }

        public static CFraction operator*(CFraction lhs, CFraction rhs)
        {
            CFraction c = new CFraction();
            c.TUSO = lhs.TUSO * rhs.TUSO;
            c.MAUSO = lhs.MAUSO * rhs.MAUSO;
            RecudeFraction(c);
            return c;
        }

        public static CFraction operator/(CFraction lhs, CFraction rhs)
        {
            CFraction c = new CFraction();
            c.TUSO = lhs.TUSO * rhs.MAUSO;
            c.MAUSO = lhs.MAUSO * rhs.TUSO;
            RecudeFraction(c);
            return c;
        }
     
        public static bool operator>(CFraction lhs, CFraction rhs)
    {
        float tmp1, tmp2;
        tmp1 = (lhs.TUSO/lhs.MAUSO);
        tmp2 = (rhs.TUSO / rhs.MAUSO);
        if (tmp1 > tmp2)
            return true;
        else
            return false;

    }
        public static bool operator>=(CFraction lhs, CFraction rhs)
        {
            float tmp1, tmp2;
            tmp1 = (lhs.TUSO / lhs.MAUSO);
            tmp2 = (rhs.TUSO / rhs.MAUSO);
            if (tmp1 >= tmp2)
                return true;
            else
                return false;

        }

        public static bool operator <(CFraction lhs, CFraction rhs)
        {
            float tmp1, tmp2;
            tmp1 = (lhs.TUSO / lhs.MAUSO);
            tmp2 = (rhs.TUSO / rhs.MAUSO);
            if (tmp1 < tmp2)
                return true;
            else
                return false;
        }
        public static bool operator <=(CFraction lhs, CFraction rhs)
        {
            float tmp1, tmp2;
            tmp1 = (lhs.TUSO / lhs.MAUSO);
            tmp2 = (rhs.TUSO / rhs.MAUSO);
            if (tmp1 <= tmp2)
                return true;
            else
                return false;
        }
        public static bool operator==(CFraction lhs, CFraction rhs)
        {
            float tmp1, tmp2;
            tmp1 = (lhs.TUSO / lhs.MAUSO);
            tmp2 = (rhs.TUSO / rhs.MAUSO);
            if (tmp1 == tmp2)
                return true;
            else
                return false;

        }
        public static bool operator !=(CFraction lhs, CFraction rhs)
        {
            float tmp1, tmp2;
            tmp1 = (lhs.TUSO / lhs.MAUSO);
            tmp2 = (rhs.TUSO / rhs.MAUSO);
            if (tmp1 != tmp2)
                return true;
            else
                return false;
        }
        //tang 1 don vi
        public static CFraction operator++(CFraction f)
        {
            CFraction c = new CFraction();
            c.TUSO = c.TUSO + c.MAUSO;
         
            return c;
        }
        public static CFraction operator--(CFraction f)
        {
            CFraction c = new CFraction();
            c.TUSO = c.TUSO - c.MAUSO;
       
            return c;
        }
        public static CFraction operator-(CFraction f)
        { 
            CFraction c = new CFraction();
            c.TUSO = 0 - c.TUSO;
            return c;
        }
        public override string ToString()
        {
            return this.TUSO + "/" + this.MAUSO;
        }
#endregion
    }
}



Trình xem thử mình có lỗi chỗ nào ko nha ... cám ơn  :farao:

R.Keane

Tổng số bài gửi : 3
Join date : 05/01/2011
Age : 25
Đến từ : White Hart Lane

Xem lý lịch thành viên

Về Đầu Trang Go down

Re: Thuchanh07

Bài gửi  Sponsored content Today at 9:55 pm


Sponsored content


Về Đầu Trang Go down

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang


 
Permissions in this forum:
Bạn không có quyền trả lời bài viết