74 lines
2.0 KiB
C#
74 lines
2.0 KiB
C#
|
|
public struct 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 myMethod(int a, int b)
|
|
{
|
|
return a + b;
|
|
}
|
|
|
|
public static double myMethod(double a, double b)
|
|
{
|
|
return a + b;
|
|
}
|
|
|
|
public static String myMethod(String a, String b)
|
|
{
|
|
return a + " " + b;
|
|
}
|
|
|
|
public static ComplexNumber myMethod(ComplexNumber a, ComplexNumber b)
|
|
{
|
|
return a + b;
|
|
}
|
|
static void Main(string[] args)
|
|
{
|
|
bool success = false;
|
|
|
|
while (!success)
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine($"Your name is {myMethod(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());
|
|
ComplexNumber ca = new ComplexNumber(car, cai);
|
|
Console.WriteLine("Enter the real part of the second complex number and press Enter key: ");
|
|
int cbr = Int32.Parse(Console.ReadLine());
|
|
|
|
ComplexNumber cb = new ComplexNumber(cbr, cbi);
|
|
Console.WriteLine($"The sum of the two complex numbers is {myMethod(ca, cb)}");
|
|
|
|
success = true;
|
|
}
|
|
catch
|
|
{
|
|
Console.WriteLine("Parse error, try again...");
|
|
}
|
|
}
|
|
}
|
|
}
|