윈도우즈 프로그래밍 실습문제 4번입니다.
다음과 같이 Fraction
클래스를 이용해 분수의 입력을 받아,
new Fraction(5, 6);
아래와 같이 출력하면 되는 것입니다.
분수식 f1: (5 / 6)
분수식 f2: (2 / 4)
분수식 f1의 최대공약수: 1
분수식 f2의 최대공약수: 2
기약분수로 표현한 분수식: 5 / 6
기약분수로 표현한 분수식: 1 / 2
f1 + f2 = (32 / 24)
f1 - f2 = (8 / 24)
f1 * f2 = (10 / 24)
f1 / f2 = (20 / 12)
코드는 다음과 같습니다.
// include packages
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
// declaration Fraction variables
Fraction f1, f2;
// definition Fraction variables
f1 = new Fraction(5, 6);
f2 = new Fraction(2, 4);
// print
Console.WriteLine($"분수식 f1: {f1.ToString()}");
Console.WriteLine($"분수식 f2: {f2.ToString()}");
Console.WriteLine($"분수식 f1의 최대공약수: {f1.GreatestCommonDivisor()}");
Console.WriteLine($"분수식 f2의 최대공약수: {f2.GreatestCommonDivisor()}");
Console.WriteLine($"기약분수로 표현한 분수식: {f1.IrreducibleFraction()}");
Console.WriteLine($"기약분수로 표현한 분수식: {f2.IrreducibleFraction()}");
Console.WriteLine(Fraction.AddFraction(f1, f2));
Console.WriteLine(Fraction.SubFraction(f1, f2));
Console.WriteLine(Fraction.MulFraction(f1, f2));
Console.WriteLine(Fraction.DivFraction(f1, f2));
}
}
// Fraction class
class Fraction {
// instance variables
public int numerator;
public int denominator;
// constructor
public Fraction (int d)
{
numerator = 0;
denominator = d;
}
// constructor (overloading)
public Fraction (int n, int d)
{
numerator = n;
denominator = d;
}
// ToString (override)
public override string ToString()
{
return $"({numerator} / {denominator})";
}
// solve gcd
public int GreatestCommonDivisor ()
{
// tool (using lambda expresison)
Func<int, int, int, bool> mod0 = (a, b, c) => a % c == 0 && b % c == 0;
// init variables
int n = numerator > denominator ? denominator : numerator;
int i = n;
// find gcd
if (mod0(numerator, denominator, i))
{
return i;
}
n /= 2;
do
{
if (mod0(numerator, denominator, i))
{
break;
}
i--;
} while (true);
return i;
}
// print irreducible fraction
public Fraction IrreducibleFraction ()
{
int gcd = GreatestCommonDivisor();
return new Fraction(numerator / gcd, denominator / gcd);
}
// overload operators
public static Fraction operator + (Fraction f1, Fraction f2)
{
return new Fraction(f1.numerator * f2.denominator + f2.numerator * f1.denominator, f1.denominator * f2.denominator);
}
public static Fraction operator - (Fraction f1, Fraction f2)
{
return new Fraction(f1.numerator * f2.denominator - f2.numerator * f1.denominator, f1.denominator * f2.denominator);
}
public static Fraction operator * (Fraction f1, Fraction f2)
{
return new Fraction(f1.numerator * f2.numerator, f1.denominator * f2.denominator);
}
public static Fraction operator / (Fraction f1, Fraction f2)
{
return new Fraction(f1.numerator * f2.denominator, f1.denominator * f2.numerator);
}
public static Func<Fraction, Fraction, string> AddFraction = (f1, f2) => $"f1 + f2 = {f1 + f2}";
public static Func<Fraction, Fraction, string> SubFraction = (f1, f2) => $"f1 - f2 = {f1 - f2}";
public static Func<Fraction, Fraction, string> MulFraction = (f1, f2) => $"f1 * f2 = {f1 * f2}";
public static Func<Fraction, Fraction, string> DivFraction = (f1, f2) => $"f1 / f2 = {f1 / f2}";
}
문제 자체는 그저 GCD를 구해 출력하는 문제입니다.
중점으로 보아야 부분은 operator overloading 부분입니다. 이는 다음과 같이 구현이 가능합니다.
// overload operators
public static Fraction operator + (Fraction f1, Fraction f2)
{
return new Fraction(f1.numerator * f2.denominator + f2.numerator * f1.denominator, f1.denominator * f2.denominator);
}
public static Fraction operator - (Fraction f1, Fraction f2)
{
return new Fraction(f1.numerator * f2.denominator - f2.numerator * f1.denominator, f1.denominator * f2.denominator);
}
public static Fraction operator * (Fraction f1, Fraction f2)
{
return new Fraction(f1.numerator * f2.numerator, f1.denominator * f2.denominator);
}
public static Fraction operator / (Fraction f1, Fraction f2)
{
return new Fraction(f1.numerator * f2.denominator, f1.denominator * f2.numerator);
}
c#에서는 이와 같이 연산자를 재정의할 수 있습니다.
위의 'Problem 1' 을 이용하여 복소수의 계산을 하는 문제입니다. 다음과 같이 복소수를 입력한 뒤,
new Complex(5, 6);
아래와 같이 출력하면 되는 것입니다.
복소수 c1: (5 + 6i)
복소수 c2: (2 + 4i)
c1 + c2 = (7 + 10i)
c1 - c2 = (3 + 2i)
c1 * c2 = (10 + 24i)
c1 / c2 = (2 + 1i)
코드는 다음과 같습니다.
/**
* Windows programming (C#) assignment 4-2
* https://repl.it/@Gumball12/Windows-programming-c-sharp-assignment-4-2
*
* @autor HaeJun Seo
* @since Mar 25, 2019
*/
// include packages
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
// declaration Complex variables
Complex c1, c2;
// definition Complex variables
c1 = new Complex(5, 6);
c2 = new Complex(2, 4);
// print
Console.WriteLine($"복소수 c1: {c1.ToString()}");
Console.WriteLine($"복소수 c2: {c2.ToString()}");
Console.WriteLine(Complex.AddComplex(c1, c2));
Console.WriteLine(Complex.SubComplex(c1, c2));
Console.WriteLine(Complex.MulComplex(c1, c2));
Console.WriteLine(Complex.DivComplex(c1, c2));
}
}
// Complex class
class Complex {
// instance variables
public int real;
public int imaginary;
// constructor
public Complex (int r)
{
real = r;
imaginary = 0;
}
// constructor (overloading)
public Complex (int r, int i)
{
real = r;
imaginary = i;
}
// ToString (override)
public override string ToString()
{
return $"({real} + {imaginary}i)";
}
// overload operators
public static Complex operator + (Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public static Complex operator - (Complex c1, Complex c2)
{
return new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary);
}
public static Complex operator * (Complex c1, Complex c2)
{
return new Complex(c1.real * c2.real, c1.imaginary * c2.imaginary);
}
public static Complex operator / (Complex c1, Complex c2)
{
return new Complex(c1.real / c2.real, c1.imaginary / c2.imaginary);
}
public static Func<Complex, Complex, string> AddComplex = (c1, c2) => $"c1 + c2 = {c1 + c2}";
public static Func<Complex, Complex, string> SubComplex = (c1, c2) => $"c1 - c2 = {c1 - c2}";
public static Func<Complex, Complex, string> MulComplex = (c1, c2) => $"c1 * c2 = {c1 * c2}";
public static Func<Complex, Complex, string> DivComplex = (c1, c2) => $"c1 / c2 = {c1 / c2}";
}
마찬가지로 operator overloading을 통해 연산자를 재정의한 후, 이를 이용해 출력을 하는 코드입니다.