using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Fraction
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FRACTION f1 = new FRACTION(Convert.ToInt32(txt1.Text), Convert.ToInt32(txt2.Text));
FRACTION f2 = new FRACTION(Convert.ToInt32(txt3.Text), Convert.ToInt32(txt4.Text));
FRACTION f3 = new FRACTION();
f3 = f3.add(f1, f2);
MessageBox.Show(f3.X.ToString() + "/" + f3.Y.ToString());
}
}
}
و این کلاس مورد نظره:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Fraction
{
class FRACTION
{
private int x;
private int y;
public FRACTION()
{
x = 0;
y = 1;
}
public FRACTION(int s, int m)
{
if (m != 0)
{
x = s;
y = m;
}
else
{
x = 0;
y = 1;
}
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public FRACTION add(FRACTION f1, FRACTION f2)
{
FRACTION f = new FRACTION();
f.x = f1.x * f2.y + f1.y * f2.x;
f.y = f1.y * f2.y;
return f;
}
public FRACTION sub(FRACTION f1)
{
FRACTION f = new FRACTION();
f.x = this.x * f1.y - this.y * f1.x;
f.y = this.y * f1.y;
return f;
}
public void mul(FRACTION f1, FRACTION f2, FRACTION f)
{
f.x = f1.x * f2.x;
f.y = f1.y * f2.y;
}
public static FRACTION operator /(FRACTION f1, FRACTION f2)
{
return new FRACTION(f1.x * f2.y, f1.y * f2.x);
}
}
}
علاقه مندی ها (Bookmarks)