113 lines
4.1 KiB
C#

namespace anonymous
{
using ComplexNumberTuple = Tuple<int, int>;
public class ComplexNumber
{
public int real;
public int imaginary;
public ComplexNumber(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
public static ComplexNumber operator +(ComplexNumber a, ComplexNumber b)
{
return new ComplexNumber(a.real + b.real, a.imaginary + b.imaginary);
}
public override string ToString()
{
return this.real.ToString() + "+ i" + this.imaginary.ToString();
}
}
public class MethodOverloading
{
public static int myAdditionMethod(int a, int b)
{
return a + b;
}
public static double myAdditionMethod(double a, double b)
{
return a + b;
}
public static String myAdditionMethod(String a, String b)
{
return a + " " + b;
}
public static ComplexNumberTuple myAdditionMethod(ComplexNumberTuple a, ComplexNumberTuple b)
{
int real = a.Item1 + b.Item1;
int imaginary = a.Item2 + b.Item2;
return new ComplexNumberTuple(real, imaginary);
}
public static ComplexNumber myAdditionMethod(ComplexNumber a, ComplexNumber b)
{
return a + b;
}
static void Main(string[] args)
{
bool success = false;
while (!success)
{
try
{
Console.WriteLine("Enter an integer and press Enter key: ");
int a = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter an integer and press Enter key: ");
int b = Int32.Parse(Console.ReadLine());
Console.WriteLine($"The sum of the two integers is {myAdditionMethod(a, b)}");
Console.WriteLine("Enter a float number and press Enter key: ");
double da = Double.Parse(Console.ReadLine());
Console.WriteLine("Enter a float number and press Enter key: ");
double db = Double.Parse(Console.ReadLine());
Console.WriteLine($"The sum of the two floats is {myAdditionMethod(da, db)}");
Console.WriteLine("Enter your last name and press Enter key: ");
string sa = Console.ReadLine();
Console.WriteLine("Enter your first name and press Enter key: ");
string sb = Console.ReadLine();
Console.WriteLine($"Your name is {myAdditionMethod(sa, sb)}");
Console.WriteLine("Enter the real part of the first complex number and press Enter key: ");
int car = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter the imaginary part of the first complex number and press Enter key: ");
int cai = Int32.Parse(Console.ReadLine());
var ca = new ComplexNumber(car, cai);
var cta = new ComplexNumberTuple(car, cai);
Console.WriteLine("Enter the real part of the second complex number and press Enter key: ");
int cbr = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter the imaginary part of the second complex number and press Enter key: ");
int cbi = Int32.Parse(Console.ReadLine());
var cb = new ComplexNumber(cbr, cbi);
var ctb = new ComplexNumberTuple(cbr, cbi);
Console.WriteLine($"The sum of the two complex numbers (new class) is {myAdditionMethod(ca, cb)}");
Console.WriteLine($"The sum of the two complex numbers (tuple) is {myAdditionMethod(ctb.Item1, ctb.Item2)}");
success = true;
}
catch
{
Console.WriteLine("Parse error, try again...");
}
}
}
}
}