Fraction-Point
using System;
public class Fraction
{
public Fraction(int TuSo, int MauSo)
{
Console.WriteLine("Phan so dang( int, int).");
this.TuSo = TuSo;
this.MauSo = MauSo;
}
public Fraction(int songuyen)
{
Console.WriteLine("Phan so dang( int ).");
TuSo = songuyen;
MauSo = 1;
}
/*public static implicit operator Fraction(int theInt)
{
Console.WriteLine("Chuyen doi ngam dinh ve phan so.");
return new Fraction(theInt);
}
public static explicit operator int(Fraction theFraction)
{
Console.WriteLine("Chuyen doi tuong minh ve so nguyen.");
return theFraction.TuSo / theFraction.MauSo;
}
public static bool operator ==(Fraction trai, Fraction phai)
{
Console.WriteLine("Toan tu ==");
if (trai.TuSo == phai.TuSo && trai.MauSo == phai.MauSo)
return true;
return false;
}
public static bool operator !=(Fraction trai, Fraction phai)
{
Console.WriteLine("Toan tu !=");
return !(trai == phai);
}
public override bool Equals(object o)
{
Console.WriteLine("Phuong thuc Equals");
if (!(o is Fraction))
return false;
return this == (Fraction)o;
}*/
public static Fraction operator +(Fraction trai, Fraction phai)
{
Console.WriteLine("Toan tu +");
if (trai.MauSo == phai.MauSo)
{
return new Fraction(trai.TuSo + phai.TuSo, trai.MauSo);
}
return new Fraction(trai.TuSo * phai.MauSo + phai.TuSo * trai.MauSo, trai.MauSo * phai.MauSo);
}
public static Fraction operator -(Fraction trai, Fraction phai)
{
Console.WriteLine("Toan tu -");
if (trai.MauSo == phai.MauSo)
{
return new Fraction(trai.TuSo - phai.TuSo, trai.MauSo);
}
return new Fraction(trai.TuSo * phai.MauSo - phai.TuSo * trai.MauSo, trai.MauSo * phai.MauSo);
}
public static Fraction operator *(Fraction trai, Fraction phai)
{
Console.WriteLine("Toan tu *");
return new Fraction(trai.TuSo * phai.TuSo, trai.MauSo * phai.MauSo);
}
public static Fraction operator /(Fraction trai, Fraction phai)
{
return new Fraction(trai.TuSo * phai.MauSo, phai.TuSo * trai.MauSo);
}
public override string ToString()
{
string s = TuSo.ToString() + "/" + MauSo.ToString();
return s;
}
private int TuSo;
private int MauSo;
}
public class Tester
{
static void Main()
{
Fraction f1 = new Fraction(3);
Console.WriteLine("\t\tf1:{0}", f1.ToString());
Fraction f2 = new Fraction(2, 4);
Console.WriteLine("\t\tf2:{0}", f2.ToString());
Fraction f3 = f1 + f2;
Console.WriteLine("\t\tf1 + f2 = f3:{0}", f3.ToString());
Fraction f4 = f1 - f2;
Console.WriteLine("\t\tf4 = f1 - f2:{0}", f4.ToString());
Fraction f5 = f1 * f2;
Console.WriteLine("\t\tf4 = f1 * f2:{0}", f5.ToString());
Fraction f6 = f1 / f2;
Console.WriteLine("\t\tf4 = f1 / f2:{0}", f6.ToString());
}
}
-----
-----
------
using System;
using System.Collections.Generic;
using System.Text;
namespace Class_Point
{
public class Point
{
public int m_x; public int m_y;
public Point() { }
public Point (int xx, int yy)
{
m_x = xx;
m_y = yy;
}
public static Point operator +(Point p1, Point p2)
{
Point kq = new Point();
kq.m_x = p1.m_x + p2.m_x;
kq.m_y = p1.m_y + p2.m_y;
return kq;
}
public static Point operator -(Point p1, Point p2)
{
Point kq = new Point();
kq.m_x = p1.m_x - p2.m_x;
kq.m_y = p1.m_y - p2.m_y;
return kq;
}
static void Main(string[] args)
{
Point P1 = new Point(1,1);
Point P2 = new Point(2,2);
Point P3 = new Point();
Console.WriteLine("Toan tu + ...");
P3 = P1 + P2;
Console.WriteLine("P1({0},{1}) + P2({2},{3}) = P3({4},{5})", P1.m_x, P1.m_y, P2.m_x, P2.m_y, P3.m_x, P3.m_y);
Point P4 = new Point();
Console.WriteLine("Toan tu - ...");
P4 = P1 - P2;
Console.WriteLine("P1({0},{1}) - P2({2},{3}) = P3({4},{5})", P1.m_x, P1.m_y, P2.m_x, P2.m_y, P4.m_x, P4.m_y);
}
}
}
Bạn đang đọc truyện trên: Truyen247.Pro