* / 完善輸入過載函式:要求可以接受a+bi的形式。
阿新 • • 發佈:2019-01-23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 過載a_bi
{
public class Complex
{
private int real;
private int imag;
public Complex(int real, int imag)
{
this.real = real;
this.imag = imag;
}
public int Real
{
get { return real;}
set { real = value; }
}
public int Imag
{
get { return imag; }
set { imag = value; }
}
//public static Complex Add(Complex a, Complex b)
//{
// return new Complex(a.real + b.real, a.imag + b.imag);
//}
//public Complex Add(Complex a)
//{
// return new Complex(real + a.real, imag + a.imag);
//}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imag + c2.imag);
}
//overload operator(-)
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex(c1.real - c2.real, c1.imag - c2.imag);
}
//overload operator(*)
public static Complex operator *(Complex c1, Complex c2)
{
return new Complex(c1.real * c2.real - c1.imag * c2.imag,
c1.real * c2.imag + c1.imag * c2.real);
}
//overload operator(/)
public static Complex operator /(Complex c1, Complex c2)
{
return new Complex(-c1.real * c2.real +
c1.imag * c2.imag, -c1.real * c2.imag + c1.imag * c2.real);
}
public override string ToString()
{
if (Real == 0 && Imag == 0)
{ return string.Format("{0}i", 0); }
if (Real == 0 && (Imag != 1 && Imag != -1))
{ return string.Format("{0}i", Imag); }
if (Imag == 0)
{
return string.Format("{0}", Real);
}
if (Imag == 1)
{
return string.Format("i");
}
if (Imag == -1)
{
return string.Format("- i");
}
if (Imag < 0)
{
return string.Format("{0} - {1} i", Real, -Imag);
}
return string.Format("{0} + {1} i", Real, Imag);
}
}
class Program
{
static void Main(string[] args)
{
int e, f, g, h;
Console.WriteLine("請輸入第一個數:");
e = Convert.ToInt32(Console.ReadLine());
f = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入第二個數:");
g = Convert.ToInt32(Console.ReadLine());
h = Convert.ToInt32(Console.ReadLine());
Complex num1 = new Complex(e,f );
Complex num2 = new Complex(g ,h );
Complex a = num1 + num2;
Complex b = num1 - num2;
Complex c = num1 * num2;
Complex d = num1 / num2;
//Print the numbers and the Result using the overriden ToString method:
Console.WriteLine("First complex number: {0}", num1);
Console.WriteLine("Second complex number: {0}", num2);
Console.WriteLine("加法結果是是: {0}", a);
Console.WriteLine("減法結果是: {0}", b);
Console.WriteLine("乘法結果是: {0}", c);
Console.WriteLine("除法結果是: {0}", d);
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 過載a_bi
{
public class Complex
{
private int real;
private int imag;
public Complex(int real, int imag)
{
this.real = real;
this.imag = imag;
}
public int Real
{
get { return real;}
set { real = value; }
}
public int Imag
{
get { return imag; }
set { imag = value; }
}
//public static Complex Add(Complex a, Complex b)
//{
// return new Complex(a.real + b.real, a.imag + b.imag);
//}
//public Complex Add(Complex a)
//{
// return new Complex(real + a.real, imag + a.imag);
//}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imag + c2.imag);
}
//overload operator(-)
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex(c1.real - c2.real, c1.imag - c2.imag);
}
//overload operator(*)
public static Complex operator *(Complex c1, Complex c2)
{
return new Complex(c1.real * c2.real - c1.imag * c2.imag,
c1.real * c2.imag + c1.imag * c2.real);
}
//overload operator(/)
public static Complex operator /(Complex c1, Complex c2)
{
return new Complex(-c1.real * c2.real +
c1.imag * c2.imag, -c1.real * c2.imag + c1.imag * c2.real);
}
public override string ToString()
{
if (Real == 0 && Imag == 0)
{ return string.Format("{0}i", 0); }
if (Real == 0 && (Imag != 1 && Imag != -1))
{ return string.Format("{0}i", Imag); }
if (Imag == 0)
{
return string.Format("{0}", Real);
}
if (Imag == 1)
{
return string.Format("i");
}
if (Imag == -1)
{
return string.Format("- i");
}
if (Imag < 0)
{
return string.Format("{0} - {1} i", Real, -Imag);
}
return string.Format("{0} + {1} i", Real, Imag);
}
}
class Program
{
static void Main(string[] args)
{
int e, f, g, h;
Console.WriteLine("請輸入第一個數:");
e = Convert.ToInt32(Console.ReadLine());
f = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入第二個數:");
g = Convert.ToInt32(Console.ReadLine());
h = Convert.ToInt32(Console.ReadLine());
Complex num1 = new Complex(e,f );
Complex num2 = new Complex(g ,h );
Complex a = num1 + num2;
Complex b = num1 - num2;
Complex c = num1 * num2;
Complex d = num1 / num2;
//Print the numbers and the Result using the overriden ToString method:
Console.WriteLine("First complex number: {0}", num1);
Console.WriteLine("Second complex number: {0}", num2);
Console.WriteLine("加法結果是是: {0}", a);
Console.WriteLine("減法結果是: {0}", b);
Console.WriteLine("乘法結果是: {0}", c);
Console.WriteLine("除法結果是: {0}", d);
Console.ReadKey();
}
}
}