Составить описание класса для представления комплексных чисел - C# (189637)
Формулировка задачи:
Здравствуйте. Помогите пожалуйста реализовать меню в программе.
Тут написан класс по заданию:
Составить описание класса для представления комплексных чисел. Обеспечить выполнение операций сложения, вычитания и умножения комплексных чисел. Написать программу, демонстрирующую работу с этим классом. Программа должна содержать меню, позволяющее осуществить проверку всех методов класса.
Остался только главный класс с меню, чтобы можно было проверить работоспособность программы.
using System;
namespace fdfd
{
public class Complex : ICloneable
{
private double m_real = 0.0;
private double m_imag = 0.0;
#region Конструкторы
public Complex()
{
}
public Complex (double re)
{
m_real = re;
}
public Complex (double re, double im)
{
m_real = re;
m_imag = im;
}
public Complex (Complex x)
{
m_real = x.Real;
m_imag = x.Imag;
}
#endregion
public double Real
{
get { return m_real; }
set { m_real = value; }
}
public double Imag
{
get { return m_imag; }
set {m_imag = value; }
}
public double Abs
{
get { return Math.Sqrt(m_imag * m_imag + m_real * m_real); }
}
public double Arg
{
get { return Math.Atan(m_imag / m_real); }
}
/// <summary>
/// Получить комплексно-сопряженное число
/// </summary>
public Complex GetConjugate()
{
return new Complex (m_real, -m_imag);
}
public override string ToString()
{
string res = "";
if (m_real != 0.0)
{
res = m_real.ToString();
}
if (m_imag != 0.0)
{
if (m_imag > 0)
{
res += "+";
}
res += m_imag.ToString() + "i";
}
return res;
}
#region Перегруженные операторы сложения
public static Complex operator + (Complex c1, Complex c2)
{
return new Complex (c1.Real + c2.Real, c1.Imag + c2.Imag);
}
public static Complex operator + (Complex c1, double c2)
{
return new Complex (c1.Real + c2, c1.Imag);
}
public static Complex operator + (double c1, Complex c2)
{
return new Complex (c1 + c2.Real, c2.Imag);
}
#endregion
#region Перегруженные операторы вычитания
public static Complex operator - (Complex c1, Complex c2)
{
return new Complex (c1.Real - c2.Real, c1.Imag - c2.Imag);
}
public static Complex operator - (Complex c1, double c2)
{
return new Complex (c1.Real - c2, c1.Imag);
}
public static Complex operator - (double c1, Complex c2)
{
return new Complex (c1 - c2.Real, -c2.Imag);
}
#endregion
#region Перегруженные операторы умножения
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 static Complex operator * (Complex c1, double c2)
{
return new Complex (c1.Real * c2, c1.Imag * c2);
}
public static Complex operator * (double c1, Complex c2)
{
return new Complex (c1 * c2.Real, c1 * c2.Imag);
}
#endregion
#region Перегруженные операторы деления
public static Complex operator / (Complex c1, Complex c2)
{
double Denominator = c2.Real * c2.Real + c2.Imag * c2.Imag;
return new Complex ((c1.Real * c2.Real + c1.Imag * c2.Imag) / Denominator,
(c2.Real * c1.Imag - c2.Imag * c1.Real) / Denominator);
}
public static Complex operator / (Complex c1, double c2)
{
return new Complex (c1.Real / c2, c1.Imag / c2);
}
public static Complex operator / (double c1, Complex c2)
{
double Denominator = c2.Real * c2.Real + c2.Imag * c2.Imag;
return new Complex ((c1 * c2.Real) / Denominator, (-c2.Imag * c1) / Denominator);
}
#endregion
public static bool operator == (Complex c1, Complex c2)
{
return c1.Real == c2.Real && c1.Imag == c2.Imag;
}
public static bool operator != (Complex c1, Complex c2)
{
return c1.Real != c2.Real || c1.Imag != c2.Imag;
}
public override bool Equals(object obj)
{
return this == (Complex)obj;
}
public override int GetHashCode()
{
return m_real.GetHashCode() + m_imag.GetHashCode();
}
#region ICloneable Members
public object Clone()
{
return new Complex (this.m_real, this.m_imag);
}
#endregion
}
class Program
{
public static void Main(string[] args)
{
Complex re = new Complex(12.1, 11.2);
String key;
do{
Console.WriteLine("Задание:\nСоставить описание класса для представления комплексных чисел. Обеспечить выполнение операций сложения, вычитания и умножения комплексных чисел. Написать программу, демонстрирующую работу с этим классом. Программа должна содержать меню, позволяющее осуществить проверку всех методов класса.\n\n");
Console.WriteLine("1) Показать числа");
Console.WriteLine("2) Сложение");
Console.WriteLine("3) Вычитание");
Console.WriteLine("4) Умножение");
Console.WriteLine("5) Деление");
Console.WriteLine("6) Выход");
key=Console.ReadLine();
Convert.ToInt32(key);
switch(key){
case "1": Console.WriteLine(re);break;
case "2": Console.WriteLine(); break;
case "3": Console.WriteLine(); break;
case "4": Console.WriteLine(); break;
}
}while(key!="6");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}Решение задачи: «Составить описание класса для представления комплексных чисел»
textual
Листинг программы
public static void Main(string[] args)
{
Console.WriteLine("Задание:\nСоставить описание класса для представления комплексных чисел. Обеспечить выполнение операций сложения, вычитания и умножения комплексных чисел. Написать программу, демонстрирующую работу с этим классом. Программа должна содержать меню, позволяющее осуществить проверку всех методов класса.\n\n");
Complex re1 = new Complex(12.1, 11.2);
Complex re2 = new Complex(10.1, 1.2);
String key;
do
{
Console.WriteLine("1) Показать числа");
Console.WriteLine("2) Сложение");
Console.WriteLine("3) Вычитание");
Console.WriteLine("4) Умножение");
Console.WriteLine("5) Деление");
Console.WriteLine("6) Выход");
key = Console.ReadLine();
Convert.ToInt32(key);
switch (key)
{
case "1":
Console.WriteLine(re1);
Console.WriteLine(re2);
break;
case "2": Console.WriteLine(re1+re2); break;
case "3": Console.WriteLine(re1-re2); break;
case "4": Console.WriteLine(re1*re2); break;
case "5": Console.WriteLine(re1/re2); break;
}
} while (key != "6");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}